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

fix expression #66

Open
wants to merge 71 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 64 commits
Commits
Show all changes
71 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
ccc7226
comment statement in declaration test
NTTVy03 Jan 2, 2025
9a17705
fix format
NTTVy03 Jan 2, 2025
09bb4fa
add function parse test
NTTVy03 Jan 2, 2025
8089756
add comments
NTTVy03 Jan 6, 2025
a12d3bb
refactor expression
NTTVy03 Jan 6, 2025
e78ab17
extract parameter list, identifier list
NTTVy03 Jan 6, 2025
c516b18
fix statement kind
NTTVy03 Jan 6, 2025
49c4ac2
rearrange token kind
NTTVy03 Jan 6, 2025
ce223c8
extract list and tuple
NTTVy03 Jan 6, 2025
e5a8c45
rename tuple and list
NTTVy03 Jan 6, 2025
b081735
format
NTTVy03 Jan 6, 2025
783f2a7
remove eprint in goto_definition_handler
NTTVy03 Jan 18, 2025
15311ca
Merge master into fix-expression
NTTVy03 Jan 18, 2025
20a2c18
replace if with match for signal header check
NTTVy03 Jan 30, 2025
ccc0f8b
format
NTTVy03 Jan 30, 2025
cf96bb7
add example for var_init
NTTVy03 Jan 30, 2025
1798427
merge master after PR #68
NTTVy03 Feb 3, 2025
2b19a9b
remove unuse snapshots
NTTVy03 Feb 3, 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
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
```
25 changes: 25 additions & 0 deletions crates/lsp/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ impl From<DidChangeTextDocumentParams> for TextDocument {
}
}

/// state of all (circom) source file
pub struct GlobalState {
/// file id - ast from that file content
pub ast_map: DashMap<String, AstCircomProgram>,

/// file id - file content (+ end lines)
pub file_map: DashMap<String, FileDB>,

/// file id - database (template in4, function in4...)
pub db: SemanticDB,
}

Expand All @@ -71,13 +77,17 @@ impl GlobalState {
ast: &AstCircomProgram,
token: &SyntaxToken,
) -> Vec<Location> {
// look up token in current file
let semantic_data = self.db.semantic.get(&root.file_id).unwrap();
let mut result = lookup_definition(root, ast, semantic_data, token);

if token.kind() == TokenKind::CircomString {
return result;
}

// if can not find that token in current file,
// and if token in a component call / declaration
// continue looking up in libs
let p = root.get_path();

if lookup_node_wrap_token(TokenKind::ComponentDecl, token).is_some()
Expand All @@ -98,17 +108,27 @@ impl GlobalState {
}
}
}

result
}

pub fn goto_definition_handler(&self, id: RequestId, params: GotoDefinitionParams) -> Response {
eprint!("-------------------");
NTTVy03 marked this conversation as resolved.
Show resolved Hide resolved
// path to the element we want to get definition
// TODO eg: file/line/start column..end column
let uri = params.text_document_position_params.text_document.uri;

// abtract syntax tree for the element from that uri
// TODO eg:
let ast = self.ast_map.get(&uri.to_string()).unwrap();
// the file contains the element from that uri
// TODO eg:
let file = self.file_map.get(&uri.to_string()).unwrap();

let mut locations = Vec::new();

// extract token from ast at position (file, params position)
// TODO eg:
if let Some(token) =
lookup_token_at_postion(&file, &ast, params.text_document_position_params.position)
{
Expand All @@ -126,6 +146,11 @@ impl GlobalState {
}
}

/// update a file of (circom) source code
/// parse new code --> syntax tree
/// remove old data of that file in semantic database
/// add new data (circom_program_semantic) + related libs into database
/// update corresponding file-map and ast-map in global-state
pub fn handle_update(&mut self, text_document: &TextDocument) -> Result<()> {
let text = &text_document.text;
let url = &text_document.uri.to_string();
Expand Down
23 changes: 18 additions & 5 deletions crates/lsp/src/handler/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use syntax::syntax_node::SyntaxToken;

use crate::database::{FileDB, SemanticData, TokenId};

// find the first ancestor with given kind of a syntax token
pub fn lookup_node_wrap_token(ast_type: TokenKind, token: &SyntaxToken) -> Option<SyntaxNode> {
let mut p = token.parent();
while let Some(t) = p {
Expand All @@ -27,6 +28,7 @@ pub fn lookup_node_wrap_token(ast_type: TokenKind, token: &SyntaxToken) -> Optio
None
}

// return an Identifier/CircomString token at a position
pub fn lookup_token_at_postion(
file: &FileDB,
ast: &AstCircomProgram,
Expand All @@ -47,6 +49,7 @@ pub fn lookup_token_at_postion(
})
}

// find all template name (in component declaration) which are used inside a template
pub fn lookup_component(template: &AstTemplateDef, text: SyntaxText) -> Option<AstTemplateName> {
if let Some(statements) = template.statements() {
for component in statements.find_children::<AstComponentDecl>() {
Expand All @@ -60,6 +63,8 @@ pub fn lookup_component(template: &AstTemplateDef, text: SyntaxText) -> Option<A
None
}

// if token in an include statement
// add lib path (location of source code of that library) into result
pub fn jump_to_lib(file: &FileDB, token: &SyntaxToken) -> Vec<Location> {
if let Some(include_lib) = lookup_node_wrap_token(TokenKind::IncludeKw, token) {
if let Some(ast_include) = AstInclude::cast(include_lib) {
Expand All @@ -86,22 +91,27 @@ pub fn lookup_definition(
token: &SyntaxToken,
) -> Vec<Location> {
let template_list = ast.template_list();
// TODO: extract function list

let mut res = Vec::new();

if token.kind() == TokenKind::CircomString {
return jump_to_lib(file, token);
}

// signal from other template
// eg: in1, in2 from component call mul(in1, in2)
let mut signal_outside = false;

if let Some(component_call) = lookup_node_wrap_token(TokenKind::ComponentCall, token) {
// find template called.
if let Some(ast_component_call) = AstComponentCall::cast(component_call) {
if let Some(signal) = ast_component_call.signal() {
// if target token is the parameter of a component call
// TODO: go to params in template!!! (failed)
if signal.syntax().text() == token.text() {
signal_outside = true;
// lookup template of componenet
// lookup template of component
if let Some(current_template) =
lookup_node_wrap_token(TokenKind::TemplateDef, token)
{
Expand Down Expand Up @@ -131,6 +141,8 @@ pub fn lookup_definition(
}

if !signal_outside {
// look up token in template information
// (template name, signal/variable/component in template)
for template in template_list {
let template_name = template.name().unwrap();
if template_name.name().unwrap().syntax().text() == token.text() {
Expand Down Expand Up @@ -160,6 +172,9 @@ pub fn lookup_definition(
res.extend(component_decl);
}
}

// TODO: look up token in function information
// (function name, signal/variable/component in function)
}

res.into_iter()
Expand Down Expand Up @@ -189,7 +204,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 +245,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
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),
}
2 changes: 1 addition & 1 deletion crates/parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod declaration;
mod expression;
mod function;
mod include;
mod list_identity;
mod list;
mod main_component;
mod pragma;
mod statement;
Expand Down
13 changes: 11 additions & 2 deletions crates/parser/src/grammar/block.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
use super::*;

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

// TODO: why do not use expect for { and }
NTTVy03 marked this conversation as resolved.
Show resolved Hide resolved
if !p.at(LCurly) {
p.advance_with_error("Miss {");
} else {
let m = p.open();
p.eat(LCurly);
p.expect(LCurly);

let stmt_marker = p.open();
while !p.at(RCurly) && !p.eof() {
let kind = p.current();
Expand All @@ -31,7 +41,6 @@ pub fn block(p: &mut Parser) {
p.close(stmt_marker, StatementList);

p.expect(RCurly);

p.close(m, Block);

p.dec_rcurly();
Expand Down
Loading
Loading