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 all 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
8 changes: 4 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"cSpell.words": [
"vuvoth"
]
{
"cSpell.words": [
"vuvoth"
]
}
366 changes: 195 additions & 171 deletions crates/lsp/src/global_state.rs
Original file line number Diff line number Diff line change
@@ -1,171 +1,195 @@
use std::{fs, path::PathBuf};

use crate::{
database::{FileDB, SemanticDB},
handler::goto_definition::lookup_node_wrap_token,
};
use anyhow::Result;
use dashmap::DashMap;
use lsp_server::{RequestId, Response};
use lsp_types::{
DidChangeTextDocumentParams, DidOpenTextDocumentParams, GotoDefinitionParams,
GotoDefinitionResponse, Location, Url,
};

use parser::token_kind::TokenKind;
use rowan::ast::AstNode;
use syntax::abstract_syntax_tree::AstCircomProgram;
use syntax::syntax::SyntaxTreeBuilder;
use syntax::syntax_node::SyntaxToken;

use crate::handler::goto_definition::{lookup_definition, lookup_token_at_postion};

#[derive(Debug)]
pub struct TextDocument {
text: String,
uri: Url,
}

impl From<DidOpenTextDocumentParams> for TextDocument {
fn from(value: DidOpenTextDocumentParams) -> Self {
Self {
text: value.text_document.text,
uri: value.text_document.uri,
}
}
}

impl From<DidChangeTextDocumentParams> for TextDocument {
fn from(value: DidChangeTextDocumentParams) -> Self {
Self {
text: value.content_changes[0].text.to_string(),
uri: value.text_document.uri,
}
}
}

pub struct GlobalState {
pub ast_map: DashMap<String, AstCircomProgram>,
pub file_map: DashMap<String, FileDB>,
pub db: SemanticDB,
}

impl Default for GlobalState {
fn default() -> Self {
Self::new()
}
}

impl GlobalState {
pub fn new() -> Self {
Self {
ast_map: DashMap::new(),
file_map: DashMap::new(),
db: SemanticDB::new(),
}
}

pub fn lookup_definition(
&self,
root: &FileDB,
ast: &AstCircomProgram,
token: &SyntaxToken,
) -> Vec<Location> {
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;
}

let p = root.get_path();

if lookup_node_wrap_token(TokenKind::ComponentDecl, token).is_some()
|| lookup_node_wrap_token(TokenKind::ComponentCall, token).is_some()
{
for lib in ast.libs() {
let lib_abs_path = PathBuf::from(lib.lib().unwrap().value());
let lib_path = p.parent().unwrap().join(lib_abs_path).clone();
let lib_url = Url::from_file_path(lib_path.clone()).unwrap();

if let Some(file_lib) = self.file_map.get(&lib_url.to_string()) {
let ast_lib = self.ast_map.get(&lib_url.to_string()).unwrap();
if let Some(semantic_data_lib) = self.db.semantic.get(&file_lib.file_id) {
let lib_result =
lookup_definition(&file_lib, &ast_lib, semantic_data_lib, token);
result.extend(lib_result);
}
}
}
}
result
}

pub fn goto_definition_handler(&self, id: RequestId, params: GotoDefinitionParams) -> Response {
let uri = params.text_document_position_params.text_document.uri;

let ast = self.ast_map.get(&uri.to_string()).unwrap();
let file = self.file_map.get(&uri.to_string()).unwrap();

let mut locations = Vec::new();

if let Some(token) =
lookup_token_at_postion(&file, &ast, params.text_document_position_params.position)
{
locations = self.lookup_definition(&file, &ast, &token);
};

let result: Option<GotoDefinitionResponse> = Some(GotoDefinitionResponse::Array(locations));

let result = serde_json::to_value(result).unwrap();

Response {
id,
result: Some(result),
error: None,
}
}

pub fn handle_update(&mut self, text_document: &TextDocument) -> Result<()> {
let text = &text_document.text;
let url = &text_document.uri.to_string();

let syntax = SyntaxTreeBuilder::syntax_tree(text);
let file_db = FileDB::create(text, text_document.uri.clone());
let file_id = file_db.file_id;

let p: PathBuf = file_db.get_path();
if let Some(ast) = AstCircomProgram::cast(syntax) {
self.db.semantic.remove(&file_id);
self.db.circom_program_semantic(&file_db, &ast);

for lib in ast.libs() {
if let Some(lib_abs_path) = lib.lib() {
let lib_path = p.parent().unwrap().join(lib_abs_path.value()).clone();
let lib_url = Url::from_file_path(lib_path.clone()).unwrap();
if let Ok(src) = fs::read_to_string(lib_path) {
let text_doc = TextDocument {
text: src,
uri: lib_url.clone(),
};
let lib_file = FileDB::create(&text_doc.text, lib_url.clone());
let syntax = SyntaxTreeBuilder::syntax_tree(&text_doc.text);

if let Some(lib_ast) = AstCircomProgram::cast(syntax) {
self.db.semantic.remove(&lib_file.file_id);
self.db.circom_program_semantic(&lib_file, &lib_ast);
self.ast_map.insert(lib_url.to_string(), lib_ast);
}

self.file_map.insert(lib_url.to_string(), lib_file);
}
}
}
self.ast_map.insert(url.to_string(), ast);
}

self.file_map.insert(url.to_string(), file_db);

Ok(())
}
}
use std::{fs, path::PathBuf};

use crate::{
database::{FileDB, SemanticDB},
handler::goto_definition::lookup_node_wrap_token,
};
use anyhow::Result;
use dashmap::DashMap;
use lsp_server::{RequestId, Response};
use lsp_types::{
DidChangeTextDocumentParams, DidOpenTextDocumentParams, GotoDefinitionParams,
GotoDefinitionResponse, Location, Url,
};

use parser::token_kind::TokenKind;
use rowan::ast::AstNode;
use syntax::abstract_syntax_tree::AstCircomProgram;
use syntax::syntax::SyntaxTreeBuilder;
use syntax::syntax_node::SyntaxToken;

use crate::handler::goto_definition::{lookup_definition, lookup_token_at_postion};

#[derive(Debug)]
pub struct TextDocument {
text: String,
uri: Url,
}

impl From<DidOpenTextDocumentParams> for TextDocument {
fn from(value: DidOpenTextDocumentParams) -> Self {
Self {
text: value.text_document.text,
uri: value.text_document.uri,
}
}
}

impl From<DidChangeTextDocumentParams> for TextDocument {
fn from(value: DidChangeTextDocumentParams) -> Self {
Self {
text: value.content_changes[0].text.to_string(),
uri: value.text_document.uri,
}
}
}

/// 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,
}

impl Default for GlobalState {
fn default() -> Self {
Self::new()
}
}

impl GlobalState {
pub fn new() -> Self {
Self {
ast_map: DashMap::new(),
file_map: DashMap::new(),
db: SemanticDB::new(),
}
}

pub fn lookup_definition(
&self,
root: &FileDB,
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()
|| lookup_node_wrap_token(TokenKind::ComponentCall, token).is_some()
{
for lib in ast.libs() {
let lib_abs_path = PathBuf::from(lib.lib().unwrap().value());
let lib_path = p.parent().unwrap().join(lib_abs_path).clone();
let lib_url = Url::from_file_path(lib_path.clone()).unwrap();

if let Some(file_lib) = self.file_map.get(&lib_url.to_string()) {
let ast_lib = self.ast_map.get(&lib_url.to_string()).unwrap();
if let Some(semantic_data_lib) = self.db.semantic.get(&file_lib.file_id) {
let lib_result =
lookup_definition(&file_lib, &ast_lib, semantic_data_lib, token);
result.extend(lib_result);
}
}
}
}

result
}

pub fn goto_definition_handler(&self, id: RequestId, params: GotoDefinitionParams) -> Response {
// 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)
{
locations = self.lookup_definition(&file, &ast, &token);
};

let result: Option<GotoDefinitionResponse> = Some(GotoDefinitionResponse::Array(locations));

let result = serde_json::to_value(result).unwrap();

Response {
id,
result: Some(result),
error: None,
}
}

/// 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();

let syntax = SyntaxTreeBuilder::syntax_tree(text);
let file_db = FileDB::create(text, text_document.uri.clone());
let file_id = file_db.file_id;

let p: PathBuf = file_db.get_path();
if let Some(ast) = AstCircomProgram::cast(syntax) {
self.db.semantic.remove(&file_id);
self.db.circom_program_semantic(&file_db, &ast);

for lib in ast.libs() {
if let Some(lib_abs_path) = lib.lib() {
let lib_path = p.parent().unwrap().join(lib_abs_path.value()).clone();
let lib_url = Url::from_file_path(lib_path.clone()).unwrap();
if let Ok(src) = fs::read_to_string(lib_path) {
let text_doc = TextDocument {
text: src,
uri: lib_url.clone(),
};
let lib_file = FileDB::create(&text_doc.text, lib_url.clone());
let syntax = SyntaxTreeBuilder::syntax_tree(&text_doc.text);

if let Some(lib_ast) = AstCircomProgram::cast(syntax) {
self.db.semantic.remove(&lib_file.file_id);
self.db.circom_program_semantic(&lib_file, &lib_ast);
self.ast_map.insert(lib_url.to_string(), lib_ast);
}

self.file_map.insert(lib_url.to_string(), lib_file);
}
}
}
self.ast_map.insert(url.to_string(), ast);
}

self.file_map.insert(url.to_string(), file_db);

Ok(())
}
}
Loading