Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor declaration grammar #65

Open
wants to merge 59 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
5f66905
skip trivial first tokens in parsing
NTTVy03 Nov 9, 2024
6913c55
remove comment in input test
NTTVy03 Nov 13, 2024
b2a750f
Merge branch 'master' of https://github.com/vuvoth/ccls
NTTVy03 Nov 18, 2024
caac481
manage out-of-bound case, update input test
NTTVy03 Nov 18, 2024
8efa4f7
update input test
NTTVy03 Nov 18, 2024
c78761e
update input test
NTTVy03 Dec 6, 2024
bfd1f15
make test programs private
NTTVy03 Dec 8, 2024
e054b57
remove comments in syntax tests
NTTVy03 Dec 8, 2024
15c9e5f
format
NTTVy03 Dec 8, 2024
c76246b
make Pragma optional, remove ROOT
NTTVy03 Dec 8, 2024
cd125ab
rename close() params, use advance() in eat(), fix typo scope
NTTVy03 Dec 8, 2024
15470b1
parse params using list_identity
NTTVy03 Dec 8, 2024
6dbb51a
return Option in token_value() and position_of()
NTTVy03 Dec 10, 2024
6d5f0c6
do not allow <--, <== in var declaration
NTTVy03 Dec 10, 2024
fbdc5b9
make public signal optional in main component
NTTVy03 Dec 10, 2024
07c4171
fix format before merge
NTTVy03 Dec 13, 2024
0bf3a04
remove empty test
NTTVy03 Dec 17, 2024
f1b0c39
replace expect by eat in block
NTTVy03 Dec 17, 2024
a10be25
add scope parsing test
NTTVy03 Dec 17, 2024
47df964
fix space in scope parsing test
NTTVy03 Dec 17, 2024
5c6cb56
update snapshot test for syntax
NTTVy03 Dec 19, 2024
372efd3
refactor syntax test
NTTVy03 Dec 19, 2024
46929c2
update input tests with snapshot
NTTVy03 Dec 20, 2024
d811037
change param type in find signal into str
NTTVy03 Dec 25, 2024
cafff02
add snapshot guild
NTTVy03 Dec 27, 2024
03da99a
add some combine arithmetic operators
NTTVy03 Dec 27, 2024
7792a96
error report in expect and expect_any
NTTVy03 Dec 27, 2024
6ac9ac9
update snapshot for combine operators
NTTVy03 Dec 27, 2024
ec73df8
update snapshot test
NTTVy03 Dec 28, 2024
5bdda2f
fix wrap trivial tokens before open a tree
NTTVy03 Dec 28, 2024
8e4853d
update template happy test
NTTVy03 Dec 28, 2024
bb95297
add operators into token kinds
NTTVy03 Dec 28, 2024
6949ca6
Merge branch 'fix-parser' into master
NTTVy03 Dec 30, 2024
7753149
Merge pull request #1 from vuvoth/master
NTTVy03 Dec 30, 2024
97bb689
add operators test
NTTVy03 Dec 30, 2024
179f6bc
re-priority token kinds
NTTVy03 Dec 30, 2024
40268bf
fix clippy check
NTTVy03 Dec 30, 2024
156b872
fix clippy check
NTTVy03 Dec 30, 2024
d18fba0
rebase add-token-kinds
NTTVy03 Dec 30, 2024
84e960b
fix clippy check
NTTVy03 Dec 30, 2024
d9f369e
Merge branch 'master' of https://github.com/NTTVy03/ccls
NTTVy03 Dec 30, 2024
a1bd25e
fix clippy
NTTVy03 Dec 30, 2024
3a08797
remove duplicate declaration in grammar tests
NTTVy03 Dec 30, 2024
39f1e12
add missing signal assign tokens
NTTVy03 Dec 30, 2024
f4db959
fix grammar of for statement
NTTVy03 Dec 30, 2024
a9edb76
add statements test
NTTVy03 Dec 30, 2024
c5a17c3
fix declaration, support matrix
NTTVy03 Dec 30, 2024
605c16b
fix format
NTTVy03 Dec 30, 2024
c9d93f7
refactor declaration
NTTVy03 Dec 31, 2024
8e92b83
use list identity parser in tuple
NTTVy03 Dec 31, 2024
6f6d678
fix right curly count in block
NTTVy03 Dec 31, 2024
df3113d
extract function params
NTTVy03 Dec 31, 2024
f68d36a
add declaration test
NTTVy03 Dec 31, 2024
db81682
remove duplicate else in comment
NTTVy03 Jan 1, 2025
e61edb2
replace eat by expect for curly in block
NTTVy03 Jan 1, 2025
ccc7226
comment statement in declaration test
NTTVy03 Jan 2, 2025
9a17705
fix format
NTTVy03 Jan 2, 2025
9352813
merge master into signal-inline-init
NTTVy03 Jan 18, 2025
82ecfe2
merge master into signal-inline-init
NTTVy03 Jan 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions crates/lsp/src/handler/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ mod tests {
pragma circom 2.0.0;

template X() {
signal x = 10;
signal x[100];
signal input x = 10;
component x = Multiplier2();
component y = X();
Expand Down Expand Up @@ -230,9 +230,7 @@ template Y() {
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:?}");
}
println!("program: {}", program_ast.syntax().text().to_string());

let inputs = program_ast.template_list()[0]
.func_body()
Expand Down
2 changes: 2 additions & 0 deletions crates/parser/src/grammar/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ use super::*;
pub fn block(p: &mut Parser) {
p.inc_rcurly();

// TODO: why do not use expect for { and }
if !p.at(LCurly) {
p.advance_with_error("Miss {");
} else {
let m = p.open();
p.expect(LCurly);

let stmt_marker = p.open();
while !p.at(RCurly) && !p.eof() {
let kind = p.current();
Expand Down
181 changes: 124 additions & 57 deletions crates/parser/src/grammar/declaration.rs
Original file line number Diff line number Diff line change
@@ -1,121 +1,188 @@
use super::{
expression::{tuple, tuple_init},
expression::{expression, tuple, tuple_init},
*,
};

// "signal" --> None
// "signal input" --> Some(true)
// "signal output" --> Some(false)
// [N][M-1]
fn array(p: &mut Parser) -> bool {
let is_array = p.at(LBracket);

while p.at(LBracket) {
p.expect(LBracket);
expression(p);
p.expect(RBracket);
}

is_array
}

/*
"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();
p.expect(SignalKw);
if p.at_any(&[InputKw, OutputKw]) {
if p.at(InputKw) {
res = Some(true);
} else {
res = Some(false);
}

let res = if p.at(InputKw) {
Some(true)
} else if p.at(OutputKw) {
Some(false)
} else {
None
};

if res.is_some() {
p.advance();
}

if p.at(LCurly) {
p.expect(Identifier);
p.expect(RCurly);
}
// signal tags
// {tag1, tag2, tag2}
// TODO: support list of tags
if p.at(LCurly) {
p.expect(Identifier);
p.expect(RCurly);
}

p.close(m, SignalHeader);
res
}

pub(crate) fn var_init(p: &mut Parser) {
// name of variable
p.expect(Identifier);

// eg: [N - 1][M]
array(p);

// assign for variable
// eg: = 10
if p.at_var_assign() {
p.advance();
expression(p);
}
}

// eg: in[N - 1] <== c.in;
pub(crate) fn signal_init(p: &mut Parser, assign_able: bool) {
// name of signal
p.expect(Identifier);

// eg: [N][M-1]
array(p);

// assign for intermediate and outputs signals
// eg: <== Multiplier2().out
if assign_able && p.at_inline_assign_signal() {
p.advance();
expression(p);
}
}

/**
* Declaration := "var" (SimpleSymbol, ..., SimpleSymbol) TupleInitialization |
*
*
* "var" iden1 = init1, iden2 = init2, iden3
*/
pub(super) fn var_declaration(p: &mut Parser) {
let m = p.open();
p.expect(VarKw);

// tuple of variables
// eg: var (in1, in2, in3) = (1, 2, 3);
if p.at(LParen) {
tuple(p);
if p.at(Assign) {
if p.at_var_assign() {
tuple_init(p);
}
} else {
p.expect(Identifier);
if p.at(Assign) {
p.expect(Assign);
expression::expression(p);
}
// list of var
// list of variables
// var in1[N], in2 = 5;
var_init(p);
while p.at(Comma) && !p.eof() {
p.expect(Comma);
p.expect(Identifier);
if p.at(Assign) {
p.expect(Assign);
expression::expression(p);
}
p.skip();
var_init(p);
}
}

p.close(m, VarDecl);
}

/*
* signal are immutable (can not modify after init value)
* can not initialize value for input signal
* since circom 2.0.4, it is also allowed to initialize
intermediate and outputs signals right after their declaration
*/
pub(super) fn signal_declaration(p: &mut Parser) {
// TODO: can we remove that?
if !p.at(SignalKw) {
p.advance_with_error("Signal error");
return;
}

let m = p.open();
let io_signal = signal_header(p);
let assign_able = io_signal != Some(true);

// tuple of signal
// eg: signal (in1, in2, in3) <== tuple_value;
if p.at(LParen) {
tuple(p);
if p.at_any(&[Assign, RAssignSignal, RAssignConstraintSignal]) {
// can not assign for input signal
if assign_able && p.at_inline_assign_signal() {
tuple_init(p);
}
} else {
p.expect(Identifier);
// list of var
// list of signals
// signal in1[N], in2 <== signal_value;
signal_init(p, assign_able);
while p.at(Comma) && !p.eof() {
p.skip();
p.expect(Identifier);
signal_init(p, assign_able);
}
}

if let Some(is_input) = io_signal {
if is_input {
p.close(m, InputSignalDecl);
} else {
p.close(m, OutputSignalDecl);
}
} else {
p.close(m, SignalDecl);
}
let close_kind = match io_signal {
Some(true) => InputSignalDecl,
Some(false) => OutputSignalDecl,
None => SignalDecl,
};

p.close(m, close_kind);
}

/*
* initialization in the definition of arrays of components is not allowed
*/
pub(super) fn component_declaration(p: &mut Parser) {
let m = p.open();
p.expect(ComponentKw);

// TODO: why do we need `ComponentIdentifier` kind here?
let m_c = p.open();
p.expect(Identifier);
p.close(m_c, ComponentIdentifier);

p.expect(Assign);
let m_c = p.open();
p.expect(Identifier);
p.close(m_c, TemplateName);
p.expect(LParen);

if p.at(Identifier) {
expression::expression(p);
while !p.at(RParen) && !p.eof() {
p.expect(Comma);
expression::expression(p);
}
}
// support array component
// eg: comp[N - 1][10]
let is_array = array(p);

// do not assign for array components
if !is_array && p.at(Assign) {
p.expect(Assign);

p.expect(RParen);
// TODO: support `parallel` tag
// eg: component comp = parallel NameTemplate(...){...}

// template name
let m_c = p.open();
p.expect(Identifier);
p.close(m_c, TemplateName);

// template params
tuple(p);
}

p.close(m, ComponentDecl);
}
Expand Down
35 changes: 29 additions & 6 deletions crates/parser/src/grammar/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,39 @@ pub(super) fn expression(p: &mut Parser) {
p.close(m, Expression);
}

/**
* grammar: "(param1, param2,..., paramn)"
* can be an empty ()
*/
pub(super) fn function_params(p: &mut Parser) {
let m = p.open();
p.expect(LParen);

while !p.at(RParen) && !p.eof() {
expression(p);
if p.at(Comma) {
p.expect(Comma)
} else {
break;
}
}

p.expect(RParen);
// TODO: what kind of it?
p.close(m, Tuple);
}

/**
* grammar: "(Symbol_1, Symbol_2,..., Symbol_n)"
* can be an empty tuple (for function cal: Mul())
*/
pub(super) fn tuple(p: &mut Parser) {
let m = p.open();
p.expect(LParen);
p.expect(Identifier);
while p.at(Comma) && !p.eof() {
p.expect(Comma);
p.expect(Identifier);
}

// iden1, iden2, iden3
list_identity::parse(p);

p.expect(RParen);
p.close(m, Tuple);
}
Expand Down Expand Up @@ -85,7 +107,8 @@ pub fn expression_rec(p: &mut Parser, pb: u16) -> Option<Marker> {
// TODO: function call
if p.at(LParen) {
let m = p.open_before(lhs);
tuple(p);
// tuple(p);
function_params(p);
lhs = p.close(m, Call);
}

Expand Down
Loading
Loading