Skip to content

Commit

Permalink
Merge pull request #7 from distributed-lab/feature/circuit-v2.2.0
Browse files Browse the repository at this point in the history
Add suport for Circom v2.2.0. Restructure package
  • Loading branch information
Arvolear authored Oct 31, 2024
2 parents 01620f8 + a5b434f commit 59f15f8
Show file tree
Hide file tree
Showing 44 changed files with 6,960 additions and 5,684 deletions.
1 change: 0 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[submodule "circom-g4-grammar"]
path = circom-g4-grammar
branch = main
url = https://github.com/distributed-lab/circom-g4-grammar.git
1 change: 1 addition & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"require": [ "ts-node/register" ],
"file": ["test/setup.ts"],
"extensions": ["ts"],
"spec": [
"test/**/*.ts"
Expand Down
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,3 @@ if (errorListener.hasErrors()) {
throw new ParserError(errorListener.getErrors());
}
```

## Reference: Built-in Visitors

You can use the built-in visitors provided in this package as a reference or starting point for your own implementations:
- [CircomTemplateVisitor](./src/builtin/CircomTemplateVisitor.ts)
- [CircomIncludeVisitor](./src/builtin/CircomIncludeVisitor.ts)
- [CircomMainComponentVisitor](./src/builtin/CircomMainComponentVisitor.ts)
- [CircomExpressionVisitor](./src/builtin/CircomExpressionVisitor.ts)

## Known limitations

1. Function calls inside the main component declaration or expressions are not supported.
2. Currently, all 'simple' expressions are evaluated as-is, without accounting for the module.
2 changes: 1 addition & 1 deletion circom-g4-grammar
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@distributedlab/circom-parser",
"description": "Circom circuit parser built with ANTLR4",
"version": "0.1.5",
"version": "0.2.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
Expand Down
40 changes: 17 additions & 23 deletions src/ExtendedCircomParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ import { CircomLexer, CircomParser } from "./generated";
import ErrorListener from "./errors/ErrorListener";

export class ExtendedCircomParser extends CircomParser {
lexer: CircomLexer | null = null;
lexer: CircomLexer;

parserErrorListener: ErrorListener<Token>;
lexerErrorListener: ErrorListener<number> | null = null;
lexerErrorListener: ErrorListener<number>;

constructor(tokens: antlr4.CommonTokenStream) {
constructor(tokens: antlr4.CommonTokenStream, lexer: CircomLexer) {
super(tokens);

this.removeErrorListeners();
this.lexer = lexer;
this.lexerErrorListener = new ErrorListener();
this.parserErrorListener = new ErrorListener();
this.addErrorListener(this.parserErrorListener);

this.initErrorListeners();

this.buildParseTrees = true;
}

circuit() {
Expand All @@ -32,33 +36,25 @@ export class ExtendedCircomParser extends CircomParser {
this._interp.predictionMode = antlr4.PredictionMode.LL;
this.reset();

this.parserErrorListener = new ErrorListener();
this.removeErrorListeners();
this.addErrorListener(this.parserErrorListener);
this.initErrorListeners();

return super.circuit();
}

setLexer(lexer: CircomLexer) {
this.lexer = lexer;
}

initErrorListeners() {
this.parserErrorListener = new ErrorListener();
this.removeErrorListeners();
this.addErrorListener(this.parserErrorListener);

if (this.lexer) {
this.lexerErrorListener = new ErrorListener();
this.lexer.removeErrorListeners();
this.lexer.addErrorListener(this.lexerErrorListener);
}
this.lexerErrorListener = new ErrorListener();
this.lexer.removeErrorListeners();
this.lexer.addErrorListener(this.lexerErrorListener);
}

hasAnyErrors(): boolean {
return (
this.parserErrorListener.hasErrors() ||
(this.lexerErrorListener !== null && this.lexerErrorListener.hasErrors())
this.lexerErrorListener.hasErrors()
);
}

Expand All @@ -69,11 +65,9 @@ export class ExtendedCircomParser extends CircomParser {
errors.push(error);
});

if (this.lexerErrorListener) {
this.lexerErrorListener.getErrors().forEach((error) => {
errors.push(error);
});
}
this.lexerErrorListener.getErrors().forEach((error) => {
errors.push(error);
});

return errors;
}
Expand Down
29 changes: 29 additions & 0 deletions src/ExtendedCircomVisitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ParserRuleContext } from "antlr4";

import { CircomVisitor } from "./generated";

import { ParserErrorItem } from "./types";

export class ExtendedCircomVisitor<Result> extends CircomVisitor<Result> {
errors: ParserErrorItem[];

constructor(public templateIdentifier: string) {
super();

this.errors = [];
}

protected addError(message: string, context: ParserRuleContext) {
this.errors.push({
templateName: this.templateIdentifier,
message,
line: context.start.line,
column: context.start.column,
context,
});
}

public getErrors(): ParserErrorItem[] {
return this.errors;
}
}
Loading

0 comments on commit 59f15f8

Please sign in to comment.