Skip to content

Commit 59f15f8

Browse files
authored
Merge pull request #7 from distributed-lab/feature/circuit-v2.2.0
Add suport for Circom v2.2.0. Restructure package
2 parents 01620f8 + a5b434f commit 59f15f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+6960
-5684
lines changed

.gitmodules

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
[submodule "circom-g4-grammar"]
22
path = circom-g4-grammar
3-
branch = main
43
url = https://github.com/distributed-lab/circom-g4-grammar.git

.mocharc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"require": [ "ts-node/register" ],
3+
"file": ["test/setup.ts"],
34
"extensions": ["ts"],
45
"spec": [
56
"test/**/*.ts"

README.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,3 @@ if (errorListener.hasErrors()) {
4545
throw new ParserError(errorListener.getErrors());
4646
}
4747
```
48-
49-
## Reference: Built-in Visitors
50-
51-
You can use the built-in visitors provided in this package as a reference or starting point for your own implementations:
52-
- [CircomTemplateVisitor](./src/builtin/CircomTemplateVisitor.ts)
53-
- [CircomIncludeVisitor](./src/builtin/CircomIncludeVisitor.ts)
54-
- [CircomMainComponentVisitor](./src/builtin/CircomMainComponentVisitor.ts)
55-
- [CircomExpressionVisitor](./src/builtin/CircomExpressionVisitor.ts)
56-
57-
## Known limitations
58-
59-
1. Function calls inside the main component declaration or expressions are not supported.
60-
2. Currently, all 'simple' expressions are evaluated as-is, without accounting for the module.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@distributedlab/circom-parser",
33
"description": "Circom circuit parser built with ANTLR4",
4-
"version": "0.1.5",
4+
"version": "0.2.0",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"files": [

src/ExtendedCircomParser.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ import { CircomLexer, CircomParser } from "./generated";
77
import ErrorListener from "./errors/ErrorListener";
88

99
export class ExtendedCircomParser extends CircomParser {
10-
lexer: CircomLexer | null = null;
10+
lexer: CircomLexer;
1111

1212
parserErrorListener: ErrorListener<Token>;
13-
lexerErrorListener: ErrorListener<number> | null = null;
13+
lexerErrorListener: ErrorListener<number>;
1414

15-
constructor(tokens: antlr4.CommonTokenStream) {
15+
constructor(tokens: antlr4.CommonTokenStream, lexer: CircomLexer) {
1616
super(tokens);
1717

18-
this.removeErrorListeners();
18+
this.lexer = lexer;
19+
this.lexerErrorListener = new ErrorListener();
1920
this.parserErrorListener = new ErrorListener();
20-
this.addErrorListener(this.parserErrorListener);
21+
22+
this.initErrorListeners();
23+
24+
this.buildParseTrees = true;
2125
}
2226

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

35-
this.parserErrorListener = new ErrorListener();
36-
this.removeErrorListeners();
37-
this.addErrorListener(this.parserErrorListener);
39+
this.initErrorListeners();
3840

3941
return super.circuit();
4042
}
4143

42-
setLexer(lexer: CircomLexer) {
43-
this.lexer = lexer;
44-
}
45-
4644
initErrorListeners() {
4745
this.parserErrorListener = new ErrorListener();
4846
this.removeErrorListeners();
4947
this.addErrorListener(this.parserErrorListener);
5048

51-
if (this.lexer) {
52-
this.lexerErrorListener = new ErrorListener();
53-
this.lexer.removeErrorListeners();
54-
this.lexer.addErrorListener(this.lexerErrorListener);
55-
}
49+
this.lexerErrorListener = new ErrorListener();
50+
this.lexer.removeErrorListeners();
51+
this.lexer.addErrorListener(this.lexerErrorListener);
5652
}
5753

5854
hasAnyErrors(): boolean {
5955
return (
6056
this.parserErrorListener.hasErrors() ||
61-
(this.lexerErrorListener !== null && this.lexerErrorListener.hasErrors())
57+
this.lexerErrorListener.hasErrors()
6258
);
6359
}
6460

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

72-
if (this.lexerErrorListener) {
73-
this.lexerErrorListener.getErrors().forEach((error) => {
74-
errors.push(error);
75-
});
76-
}
68+
this.lexerErrorListener.getErrors().forEach((error) => {
69+
errors.push(error);
70+
});
7771

7872
return errors;
7973
}

src/ExtendedCircomVisitor.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { ParserRuleContext } from "antlr4";
2+
3+
import { CircomVisitor } from "./generated";
4+
5+
import { ParserErrorItem } from "./types";
6+
7+
export class ExtendedCircomVisitor<Result> extends CircomVisitor<Result> {
8+
errors: ParserErrorItem[];
9+
10+
constructor(public templateIdentifier: string) {
11+
super();
12+
13+
this.errors = [];
14+
}
15+
16+
protected addError(message: string, context: ParserRuleContext) {
17+
this.errors.push({
18+
templateName: this.templateIdentifier,
19+
message,
20+
line: context.start.line,
21+
column: context.start.column,
22+
context,
23+
});
24+
}
25+
26+
public getErrors(): ParserErrorItem[] {
27+
return this.errors;
28+
}
29+
}

0 commit comments

Comments
 (0)