11use std:: collections:: BTreeMap ;
22
33use p3_field:: { PrimeCharacteristicRing , PrimeField } ;
4- use pest:: { Parser , iterators:: Pair } ;
4+ use pest:: iterators:: Pair ;
55use pest_derive:: Parser ;
66
77use crate :: {
@@ -15,46 +15,15 @@ use crate::{
1515
1616pub mod error;
1717pub use error:: * ;
18+ pub mod utils;
19+ pub ( crate ) use utils:: * ;
20+ pub mod program;
21+ pub use program:: * ;
1822
1923#[ derive( Parser , Debug ) ]
2024#[ grammar = "grammar.pest" ]
2125pub struct LangParser ;
2226
23- pub fn parse_program ( input : & str ) -> Result < Program , ParseError > {
24- let input = remove_comments ( input) ;
25- let mut pairs = LangParser :: parse ( Rule :: program, & input) ?;
26- let program_pair = pairs. next ( ) . unwrap ( ) ;
27-
28- let mut constants = BTreeMap :: new ( ) ;
29- let mut functions = BTreeMap :: new ( ) ;
30- let mut trash_var_count = 0 ;
31-
32- for pair in program_pair. into_inner ( ) {
33- match pair. as_rule ( ) {
34- Rule :: constant_declaration => {
35- let ( name, value) = parse_constant_declaration ( pair, & constants) ?;
36- constants. insert ( name, value) ;
37- }
38- Rule :: function => {
39- let function = parse_function ( pair, & constants, & mut trash_var_count) ?;
40- functions. insert ( function. name . clone ( ) , function) ;
41- }
42- Rule :: EOI => break ,
43- _ => { }
44- }
45- }
46-
47- Ok ( Program { functions } )
48- }
49-
50- fn remove_comments ( input : & str ) -> String {
51- input
52- . lines ( )
53- . map ( |line| line. find ( "//" ) . map_or ( line, |pos| & line[ ..pos] ) )
54- . collect :: < Vec < _ > > ( )
55- . join ( "\n " )
56- }
57-
5827fn parse_constant_declaration (
5928 pair : Pair < ' _ , Rule > ,
6029 constants : & BTreeMap < String , usize > ,
0 commit comments