Skip to content

Commit

Permalink
Fix/ccls not working after jump to lib file (#52)
Browse files Browse the repository at this point in the history
* add block comment parser

* clippy and format
  • Loading branch information
vuvoth authored Feb 29, 2024
1 parent 07d7d06 commit 637bb22
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 34 deletions.
14 changes: 9 additions & 5 deletions crates/lsp/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use lsp_types::{

use parser::token_kind::TokenKind;
use rowan::ast::AstNode;
use syntax::abstract_syntax_tree::{AstCircomProgram, AstCircomString};
use syntax::abstract_syntax_tree::AstCircomProgram;
use syntax::syntax::SyntaxTreeBuilder;
use syntax::syntax_node::SyntaxToken;

Expand Down Expand Up @@ -68,6 +68,8 @@ impl GlobalState {
ast: &AstCircomProgram,
token: &SyntaxToken,
) -> Vec<Location> {
eprintln!("token {}", token.text());

let semantic_data = self.db.semantic.get(&root.file_id).unwrap();
let mut result = lookup_definition(root, ast, semantic_data, token);

Expand Down Expand Up @@ -99,11 +101,10 @@ impl GlobalState {
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)
{
eprintln!("lookup token at {}", token.text());

locations = self.lookup_definition(&file, &ast, &token);
};

Expand All @@ -119,6 +120,7 @@ impl GlobalState {
}

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

Expand All @@ -127,12 +129,13 @@ impl GlobalState {
let file_id = file_db.file_id;

let p: PathBuf = file_db.get_path();

eprintln!("syntax...");
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() {
eprintln!("{:?}", lib.syntax().text());
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();
Expand All @@ -141,9 +144,9 @@ impl GlobalState {
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);
eprintln!("{}", syntax.text());

if let Some(lib_ast) = AstCircomProgram::cast(syntax) {
self.db.semantic.remove(&lib_file.file_id);
Expand All @@ -160,6 +163,7 @@ impl GlobalState {

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

eprintln!("Finish {}", text_document.uri);
Ok(())
}
}
3 changes: 2 additions & 1 deletion crates/lsp/src/handler/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn lookup_definition(

let mut res = Vec::new();
let mut signal_outside = false;

eprintln!("{}", token.text());
if let Some(include_lib) = lookup_node_wrap_token(TokenKind::IncludeKw, token) {
if let Some(ast_include) = AstInclude::cast(include_lib) {
if let Some(abs_lib_ans) = ast_include.lib() {
Expand All @@ -86,6 +86,7 @@ pub fn lookup_definition(
}
}

eprintln!("{}", token.text());
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) {
Expand Down
50 changes: 28 additions & 22 deletions crates/parser/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,27 @@ impl<'a> Input<'a> {
let mut lex = Lexer::<TokenKind>::new(source);

while let Some(tk) = lex.next() {
input.kind.push(tk);
input.position.push(lex.span());
if tk == TokenKind::CommentBlockOpen {
let mut closed = false;
let mut join_span = lex.span();
while let Some(t) = lex.next() {
join_span.end = lex.span().end;
if t == TokenKind::CommentBlockClose {
closed = true;
break;
}
}

if closed {
input.kind.push(TokenKind::BlockComment);
} else {
input.kind.push(TokenKind::Error);
}
input.position.push(join_span);
} else {
input.kind.push(tk);
input.position.push(lex.span());
}
}

input
Expand Down Expand Up @@ -52,34 +71,21 @@ impl<'a> Input<'a> {

#[cfg(test)]
mod tests {
use std::cmp::min;

use super::Input;

#[test]
fn test_input() {
let source: String = r#"
pragma circom 2.0.0;
template X() {
component x = Multiplier2()
}
template Multiplier2 () {
// Declaration of signals.
signal input a;
signal input b;
signal output c;
signal output d;
// Constraints.
c <== a * b;
}
"#
let source = r#"
/*a + b == 10*/
a + 10
"#
.to_string();

let input = Input::new(&source);

for i in 0..10 {
for i in 0..min(input.size(), 10) {
println!("kind = {:?}", input.kind[i]);
println!("position {:?}", input.position[i]);
}
Expand Down
2 changes: 0 additions & 2 deletions crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ impl<'a> Parser<'a> {

pub fn current(&mut self) -> TokenKind {
let mut kind: TokenKind;

loop {
kind = self.input.kind_of(self.pos);

if !kind.is_travial() {
break;
}
Expand Down
10 changes: 9 additions & 1 deletion crates/parser/src/token_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub enum TokenKind {
Error = 0,
#[regex(r"//[^\n]*")]
CommentLine,
#[token("/*")]
CommentBlockOpen,
#[token("*/")]
CommentBlockClose,
#[regex("[ \t]+")]
WhiteSpace,
#[regex("[\n]")]
Expand Down Expand Up @@ -165,6 +169,7 @@ pub enum TokenKind {
SignalIdentifier,
ArrayQuery,
ParserError,
BlockComment,
EOF,
ROOT,
__LAST,
Expand Down Expand Up @@ -244,6 +249,9 @@ impl TokenKind {
matches!(self, Self::VarKw | Self::ComponentKw | Self::SignalKw)
}
pub fn is_travial(self) -> bool {
matches!(self, Self::WhiteSpace | Self::EndLine | Self::CommentLine)
matches!(
self,
Self::WhiteSpace | Self::EndLine | Self::CommentLine | Self::Error
)
}
}
107 changes: 107 additions & 0 deletions crates/syntax/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,111 @@ mod tests {

// find token
}

#[test]
fn parser_test() {
let source = r#"/*
Copyright 2018 0KIMS association.
This file is part of circom (Zero Knowledge Circuit Compiler).
circom is a free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
circom is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with circom. If not, see <https://www.gnu.org/licenses/>.
*/
/*
Binary Sum
==========
This component creates a binary sum componet of ops operands and n bits each operand.
e is Number of carries: Depends on the number of operands in the input.
Main Constraint:
in[0][0] * 2^0 + in[0][1] * 2^1 + ..... + in[0][n-1] * 2^(n-1) +
+ in[1][0] * 2^0 + in[1][1] * 2^1 + ..... + in[1][n-1] * 2^(n-1) +
+ ..
+ in[ops-1][0] * 2^0 + in[ops-1][1] * 2^1 + ..... + in[ops-1][n-1] * 2^(n-1) +
===
out[0] * 2^0 + out[1] * 2^1 + + out[n+e-1] *2(n+e-1)
To waranty binary outputs:
out[0] * (out[0] - 1) === 0
out[1] * (out[0] - 1) === 0
.
.
.
out[n+e-1] * (out[n+e-1] - 1) == 0
*/
/*
This function calculates the number of extra bits in the output to do the full sum.
*/
pragma circom 2.0.0;
function nbits(a) {
var n = 1;
var r = 0;
while (n-1<a) {
r++;
n *= 2;
}
return r;
}
template BinSum(n, ops) {
var nout = nbits((2**n -1)*ops);
signal input in[ops][n];
signal output out[nout];
var lin = 0;
var lout = 0;
var k;
var j;
var e2;
e2 = 1;
for (k=0; k<n; k++) {
for (j=0; j<ops; j++) {
lin += in[j][k] * e2;
}
e2 = e2 + e2;
}
e2 = 1;
for (k=0; k<nout; k++) {
out[k] <-- (lin >> k) & 1;
// Ensure out is binary
out[k] * (out[k] - 1) === 0;
lout += out[k] * e2;
e2 = e2+e2;
}
// Ensure the sum;
lin === lout;
}
"#;

let _syntax = SyntaxTreeBuilder::syntax_tree(source);
}
}
7 changes: 4 additions & 3 deletions editors/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ export async function activate(context: ExtensionContext) {
clientOptions
);

client.start();
await client.start();
const disposable = commands.registerCommand(
"circom-plus.restart",
async () => {
// The code you place here will be executed every time your command is executed

window.showInformationMessage("Restart server");
// Display a message box to the user
client.restart();
await client.restart();
}
);

Expand All @@ -57,5 +57,6 @@ export async function deactivate() {
if (!client) {
return undefined;
}
client.stop();

await client.stop();
}

0 comments on commit 637bb22

Please sign in to comment.