Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
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
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "circom-g4-grammar"]
path = circom-g4-grammar
branch = main
branch = dev
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
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;
}
}
243 changes: 0 additions & 243 deletions src/builtin/CircomExpressionVisitor.ts

This file was deleted.

Loading
Loading