A TypeScript package for parsing Circom code using ANTLR (ANother Tool for Language Recognition) grammar.
It includes built-in visitors and utilities that allow you to parse, traverse, and manipulate Circom circuits.
npm install @distributedlab/circom-parser
To create your own visitor, simply extend the CircomVisitor class and override the visit methods you need.
import { CircomVisitor, AnyRuleContext } from '@distributed-lab/circom-parser';
export default class MyCircomVisitor extends CircomVisitor {
visitAnyRule = (ctx: AnyRuleContext) => {
// override the visit logic here
}
}
After creating your custom visitor, you can use it with the CircomParser to traverse and manipulate the parse tree.
import { getCircomParser, ParserError } from '@distributed-lab/circom-parser';
import { MyCircomVisitor } from './MyCircomVisitor';
const { parser, errorListener } = getCircomParser(source);
const templateVisitor = new MyCircomVisitor();
templateVisitor.visit(parser.circuit());
if (errorListener.hasErrors()) {
throw new ParserError(errorListener.getErrors());
}