Skip to content

Commit

Permalink
Merge branch 'fix-parser' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
NTTVy03 authored Dec 30, 2024
2 parents 18ec997 + bb95297 commit 6949ca6
Show file tree
Hide file tree
Showing 30 changed files with 946 additions and 642 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ node_modules
.vscode-test
/target
Cargo.lock
assets
assets
*.new
*.pending-snap
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ syntax = {path = './crates/syntax', version = "0.1.0"}
circom-lsp = {path = './crates/lsp', version = "*"}
common = { path = './crates/common', version = "*"}
database = {path = "./crates/database", version = "*"}

[workspace.package]
rust-version = "1.71"
11 changes: 11 additions & 0 deletions SNAPSHOT_TEST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Snapshot Test

* Run all tests:
```
cargo test
```
* Review snapshot changes
```
cargo insta review
```
2 changes: 1 addition & 1 deletion crates/lsp/src/handler/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ template Y() {
let file = FileDB::create(&source, Url::from_file_path(Path::new("/tmp")).unwrap());

let syntax_node = SyntaxTreeBuilder::syntax_tree(&source);

if let Some(program_ast) = AstCircomProgram::cast(syntax_node) {
for template in program_ast.template_list() {
println!("{template:?}");
Expand Down
13 changes: 11 additions & 2 deletions crates/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ lsp-types = {version = "0.94.1", features = ["proposed"]}
rowan = "0.15.15"
num-traits = "0.2"
num-derive = "0.2"

serde = "1.0.216"

[profile.dev]
debug = 2
debug = 2

[dev-dependencies]
# for snapshot testing, yaml format
insta = { version = "1.41.1", features = ["yaml"] }

[profile.dev.package]
# compile slightly slower once, but use less memory, have faster diffs
insta.opt-level = 3
similar.opt-level = 3
3 changes: 2 additions & 1 deletion crates/parser/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::token_kind::TokenKind;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub enum Event {
Open { kind: TokenKind },
Close,
TokenPosition(usize),
ErrorReport(String),
}
10 changes: 9 additions & 1 deletion crates/parser/src/grammar/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use super::*;

/*
{
<declaration>/<statement>
<declaration>/<statement>
....
<declaration>/<statement>
}
*/
pub fn block(p: &mut Parser) {
p.inc_rcurly();

Expand Down Expand Up @@ -30,7 +38,7 @@ pub fn block(p: &mut Parser) {

p.close(stmt_marker, StatementList);

p.expect(RCurly);
p.eat(RCurly);

p.close(m, Block);

Expand Down
8 changes: 1 addition & 7 deletions crates/parser/src/grammar/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,4 @@ pub(super) fn declaration(p: &mut Parser) {
ComponentKw => component_declaration(p),
_ => unreachable!(),
}
}

#[cfg(test)]
mod declar_tests {
#[test]
fn signal_with_tag() {}
}
}
24 changes: 1 addition & 23 deletions crates/parser/src/grammar/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,4 @@ fn circom_expression(p: &mut Parser) {
p.close(m, TenaryConditional);
}
}
}
// #[cfg(test)]
// mod tests {

// use rowan::SyntaxNode;

// use crate::{syntax_node::CircomLang};

// use super::{entry::Scope, Parser};

// #[test]
// fn test_expression() {
// let source = r#"
// {
// a.tmp <== 100;
// b[1].c <== 10;
// }
// "#;
// let green = Parser::parse_scope(source, Scope::Block);
// let node = SyntaxNode::<CircomLang>::new_root(green);
// println!("{:#?}", node);
// }
// }
}
23 changes: 0 additions & 23 deletions crates/parser/src/grammar/pragma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,3 @@ pub fn pragma(p: &mut Parser) {
p.expect(Semicolon);
p.close(m, Pragma);
}

// #[cfg(test)]
// mod tests {
// #[test]
// fn pragam_test() {
// use crate::{
// ast::{AstNode, AstPragma},
// syntax_node::SyntaxNode,
// token_kind::TokenKind,
// };

// use super::{entry::Scope, Parser};

// let source: String = r#"pragma circom 2.0.1;"#.to_string();

// let green_node = Parser::parse_scope(&source, Scope::Pragma);
// let node = SyntaxNode::new_root(green_node);

// let pragma = AstPragma::cast(node.last_child().unwrap()).unwrap();

// assert!(pragma.version().unwrap().syntax().kind() == TokenKind::Version);
// }
// }
46 changes: 28 additions & 18 deletions crates/parser/src/grammar/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ pub(super) fn statement(p: &mut Parser) {
p.close(m, Statement);
}

/*
if (expr)
<statement>
else
<statement>
*/
fn if_statement(p: &mut Parser) {
let m = p.open();
p.expect(IfKw);
Expand All @@ -25,6 +31,7 @@ fn if_statement(p: &mut Parser) {

/**
* no if condition here.
* for/while/return/assert...
*/
fn statement_no_condition(p: &mut Parser) {
match p.current() {
Expand All @@ -50,6 +57,10 @@ fn statement_no_condition(p: &mut Parser) {
}
}

/*
for (<declaration>/<assignment>; <expression>; <assignment>)
<statement>
*/
fn for_statement(p: &mut Parser) {
let m = p.open();
p.expect(ForKw);
Expand All @@ -70,6 +81,10 @@ fn for_statement(p: &mut Parser) {
p.close(m, ForLoop);
}

/*
while (<expression>)
<statement>
*/
fn while_statement(p: &mut Parser) {
p.expect(WhileKw);
p.expect(LParen);
Expand All @@ -78,6 +93,9 @@ fn while_statement(p: &mut Parser) {
statement(p);
}

/*
assert(<expression>)
*/
fn assert_statement(p: &mut Parser) {
let m = p.open();
p.expect(AssertKw);
Expand All @@ -87,6 +105,9 @@ fn assert_statement(p: &mut Parser) {
p.close(m, AssertKw);
}

/*
log()
*/
fn log_statement(p: &mut Parser) {
let m = p.open();
p.expect(LogKw);
Expand All @@ -109,13 +130,19 @@ fn log_statement(p: &mut Parser) {
p.close(m, LogKw);
}

/*
return <expression>
*/
fn return_statement(p: &mut Parser) {
let m = p.open();
p.expect(ReturnKw);
expression(p);
p.close(m, ReturnKw);
}

/*
*/
fn assignment_statement(p: &mut Parser) {
let m = p.open();

Expand Down Expand Up @@ -154,21 +181,4 @@ fn assignment_statement(p: &mut Parser) {
} else {
p.close(m, Error);
}
}

#[cfg(test)]
mod tests {

#[test]
fn if_statement_test() {
let _source = r#"
assert(1 == 2);
"#;
// let mut parser = Parser::new(source);

// statement(&mut parser);
// let cst = parser.build_tree().ok().unwrap();

// println!("{:?}", cst);
}
}
}
41 changes: 1 addition & 40 deletions crates/parser/src/grammar/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,4 @@ pub fn template(p: &mut Parser) {
block::block(p);

p.close(m, TemplateDef);
}

// #[cfg(test)]
// mod tests {
// use crate::ast::AstTemplateDef;

// #[test]
// fn template_parse_test() {
// use crate::{ast::AstNode, syntax_node::SyntaxNode};

// use super::{entry::Scope, Parser};

// let source: String = r#"
// template Multiplier2 (a, b, c) {

// // Declaration of signals.
// signal input a;
// signal input b;
// signal output c;

// // Constraints.
// c <== a * b;
// }

// "#
// .to_string();

// let green_node = ::parse_scope(&source, Scope::Template);
// let node = SyntaxNode::new_root(green_node);

// let ast_template = AstTemplateDef::cast(node);

// if let Some(ast_internal) = ast_template {
// println!(
// "name {:?}",
// ast_internal.template_name().unwrap().syntax().text()
// );
// }
// }
// }
}
Loading

0 comments on commit 6949ca6

Please sign in to comment.