Skip to content

Commit

Permalink
Fix some parser bugs (#62)
Browse files Browse the repository at this point in the history
* skip trivial first tokens in parsing

* remove comment in input test

* manage out-of-bound case, update input test

* update input test

* update input test

* make test programs private

* remove comments in syntax tests

* format

* make Pragma optional, remove ROOT

* rename close() params, use advance() in eat(), fix typo scope

* parse params using list_identity

* return Option in token_value() and position_of()

* do not allow <--, <== in var declaration

* make public signal optional in main component

* fix format before merge
  • Loading branch information
NTTVy03 authored Dec 13, 2024
1 parent c1ab941 commit 18ec997
Show file tree
Hide file tree
Showing 15 changed files with 452 additions and 201 deletions.
30 changes: 7 additions & 23 deletions crates/parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,14 @@ pub mod entry {
p.skip();
}

pragma::pragma(p);
while !p.eof() {
match p.current() {
TemplateKw => {
template::template(p);
}
IncludeKw => {
include::include(p);
}
Pragma => pragma::pragma(p),
TemplateKw => template::template(p),
IncludeKw => include::include(p),
ComponentKw => main_component::main_component(p),
FunctionKw => function::function_parse(p),
_ => {
p.advance_with_error("invalid token");
}
_ => p.advance_with_error("invalid token"),
}
}
p.close(m, CircomProgram);
Expand All @@ -63,20 +57,10 @@ pub mod entry {
impl Scope {
pub fn parse(self, p: &mut Parser) {
match self {
Self::Block => {
let m = p.open();
block::block(p);
p.close(m, ROOT);
}
Self::Block => block::block(p),
Self::CircomProgram => circom_program(p),
Self::Pragma => {
let m = p.open();
pragma::pragma(p);
p.close(m, ROOT);
}
Self::Template => {
template::template(p);
}
Self::Pragma => pragma::pragma(p),
Self::Template => template::template(p),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/parser/src/grammar/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::*;

pub fn block(p: &mut Parser) {
p.inc_rcurly();

if !p.at(LCurly) {
p.advance_with_error("Miss {");
} else {
Expand Down
5 changes: 4 additions & 1 deletion crates/parser/src/grammar/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use super::{
*,
};

// "signal" --> None
// "signal input" --> Some(true)
// "signal output" --> Some(false)
fn signal_header(p: &mut Parser) -> Option<bool> {
let mut res = None;
let m = p.open();
Expand Down Expand Up @@ -35,7 +38,7 @@ pub(super) fn var_declaration(p: &mut Parser) {

if p.at(LParen) {
tuple(p);
if p.at_any(&[Assign, RAssignSignal, RAssignConstraintSignal]) {
if p.at(Assign) {
tuple_init(p);
}
} else {
Expand Down
1 change: 1 addition & 0 deletions crates/parser/src/grammar/expression.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::parser::Marker;

use super::*;

pub(super) fn expression(p: &mut Parser) {
let m = p.open();
circom_expression(p);
Expand Down
15 changes: 6 additions & 9 deletions crates/parser/src/grammar/function.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
use crate::grammar::*;

// fucntion name()
pub fn function_parse(p: &mut Parser) {
let m = p.open();

p.expect(FunctionKw);
let fn_name_marker = p.open();

let fn_name_marker = p.open();
p.expect(Identifier);
p.close(fn_name_marker, FunctionName);

p.expect(LParen);
let arg_marker = p.open();
while !p.at(RParen) && !p.eof() {
p.expect(Identifier);
if p.at(Comma) {
p.expect(Comma);
}
}

list_identity::parse(p);
p.close(arg_marker, ParameterList);

p.expect(RParen);

block::block(p);

p.close(m, FunctionDef);
}
1 change: 1 addition & 0 deletions crates/parser/src/grammar/list_identity.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;

// a, b, c, d
pub fn parse(p: &mut Parser) {
while p.at(Identifier) && !p.eof() {
p.expect(Identifier);
Expand Down
19 changes: 14 additions & 5 deletions crates/parser/src/grammar/main_component.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
use super::*;

/*
component main {public [signal_list]} = tempid(v1,...,vn);
{public [signal_list]} is optional
*/
pub fn main_component(p: &mut Parser) {
p.expect(ComponentKw);
p.expect(MainKw);
p.expect(LCurly);
p.expect(PublicKw);
p.expect(LBracket);
list_identity::parse(p);
p.expect(RBracket);

if p.at(LCurly) {
p.expect(LCurly);
p.expect(PublicKw);
p.expect(LBracket);
list_identity::parse(p);
p.expect(RBracket);
}

p.expect(Assign);
expression::expression(p);
}
14 changes: 6 additions & 8 deletions crates/parser/src/grammar/template.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
use crate::grammar::*;
/**
* template Identifier() {content}
*
* template Identifier( param_1, ... , param_n ) { content }
*/
pub fn template(p: &mut Parser) {
// assert!(p.at(TemplateKw));
let m = p.open();

p.expect(TemplateKw);

let name_marker = p.open();
p.expect(Identifier);
p.close(name_marker, TemplateName);

p.expect(LParen);
let arg_marker = p.open();
while !p.at(RParen) && !p.eof() {
p.expect(Identifier);
if p.at(Comma) {
p.expect(Comma);
}
}

list_identity::parse(p);
p.close(arg_marker, ParameterList);
p.expect(RParen);

block::block(p);

p.close(m, TemplateDef);
}

Expand Down
Loading

0 comments on commit 18ec997

Please sign in to comment.