Skip to content

Commit a156d48

Browse files
committed
parser: mv program parser
1 parent 768d6e6 commit a156d48

File tree

3 files changed

+47
-36
lines changed

3 files changed

+47
-36
lines changed

crates/leanVm/src/parser/mod.rs

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::BTreeMap;
22

33
use p3_field::{PrimeCharacteristicRing, PrimeField};
4-
use pest::{Parser, iterators::Pair};
4+
use pest::iterators::Pair;
55
use pest_derive::Parser;
66

77
use crate::{
@@ -15,46 +15,15 @@ use crate::{
1515

1616
pub mod error;
1717
pub 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"]
2125
pub 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-
5827
fn parse_constant_declaration(
5928
pair: Pair<'_, Rule>,
6029
constants: &BTreeMap<String, usize>,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::collections::BTreeMap;
2+
3+
use pest::Parser;
4+
5+
use super::{
6+
LangParser, ParseError, Rule, parse_constant_declaration, parse_function, remove_comments,
7+
};
8+
use crate::parser::Program;
9+
10+
pub fn parse_program(input: &str) -> Result<Program, ParseError> {
11+
let input = remove_comments(input);
12+
let mut pairs = LangParser::parse(Rule::program, &input)?;
13+
let program_pair = pairs.next().unwrap();
14+
15+
let mut constants = BTreeMap::new();
16+
let mut functions = BTreeMap::new();
17+
let mut trash_var_count = 0;
18+
19+
for pair in program_pair.into_inner() {
20+
match pair.as_rule() {
21+
Rule::constant_declaration => {
22+
let (name, value) = parse_constant_declaration(pair, &constants)?;
23+
constants.insert(name, value);
24+
}
25+
Rule::function => {
26+
let function = parse_function(pair, &constants, &mut trash_var_count)?;
27+
functions.insert(function.name.clone(), function);
28+
}
29+
Rule::EOI => break,
30+
_ => {}
31+
}
32+
}
33+
34+
Ok(Program { functions })
35+
}

crates/leanVm/src/parser/utils.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub(crate) fn remove_comments(input: &str) -> String {
2+
input
3+
.lines()
4+
.map(|line| line.find("//").map_or(line, |pos| &line[..pos]))
5+
.collect::<Vec<_>>()
6+
.join("\n")
7+
}

0 commit comments

Comments
 (0)