How to use antlr4ts - 10 common examples

To help you get started, we’ve selected a few antlr4ts examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mike-lischke / antlr4-c3 / test / CPP14Lexer.ts View on Github external
"Throw", "True", "Try", "Typedef", "Typeid", "Typename", "Union", "Unsigned", 
		"Using", "Virtual", "Void", "Volatile", "Wchar", "While", "LeftParen", 
		"RightParen", "LeftBracket", "RightBracket", "LeftBrace", "RightBrace", 
		"Plus", "Minus", "Star", "Div", "Mod", "Caret", "And", "Or", "Tilde", 
		"Not", "Assign", "Less", "Greater", "PlusAssign", "MinusAssign", "StarAssign", 
		"DivAssign", "ModAssign", "XorAssign", "AndAssign", "OrAssign", "LeftShift", 
		"LeftShiftAssign", "Equal", "NotEqual", "LessEqual", "GreaterEqual", "AndAnd", 
		"OrOr", "PlusPlus", "MinusMinus", "Comma", "ArrowStar", "Arrow", "Question", 
		"Colon", "Doublecolon", "Semi", "Dot", "DotStar", "Ellipsis", "Identifier", 
		"Integerliteral", "Decimalliteral", "Octalliteral", "Hexadecimalliteral", 
		"Binaryliteral", "Integersuffix", "Characterliteral", "Floatingliteral", 
		"Stringliteral", "Userdefinedintegerliteral", "Userdefinedfloatingliteral", 
		"Userdefinedstringliteral", "Userdefinedcharacterliteral", "Whitespace", 
		"Newline", "BlockComment", "LineComment",
	];
	public static readonly VOCABULARY: Vocabulary = new VocabularyImpl(CPP14Lexer._LITERAL_NAMES, CPP14Lexer._SYMBOLIC_NAMES, []);

	// @Override
	// @NotNull
	public get vocabulary(): Vocabulary {
		return CPP14Lexer.VOCABULARY;
	}
	// tslint:enable:no-trailing-whitespace


	constructor(input: CharStream) {
		super(input);
		this._interp = new LexerATNSimulator(CPP14Lexer._ATN, this);
	}

	// @Override
	public get grammarFileName(): string { return "CPP14.g4"; }
github mike-lischke / antlr4-c3 / test / ExprLexer.ts View on Github external
];

	public static readonly ruleNames: string[] = [
		"VAR", "LET", "PLUS", "MINUS", "MULTIPLY", "DIVIDE", "EQUAL", "OPEN_PAR", 
		"CLOSE_PAR", "ID", "WS",
	];

	private static readonly _LITERAL_NAMES: Array = [
		undefined, undefined, undefined, "'+'", "'-'", "'*'", "'/'", "'='", "'('", 
		"')'",
	];
	private static readonly _SYMBOLIC_NAMES: Array = [
		undefined, "VAR", "LET", "PLUS", "MINUS", "MULTIPLY", "DIVIDE", "EQUAL", 
		"OPEN_PAR", "CLOSE_PAR", "ID", "WS",
	];
	public static readonly VOCABULARY: Vocabulary = new VocabularyImpl(ExprLexer._LITERAL_NAMES, ExprLexer._SYMBOLIC_NAMES, []);

	// @Override
	// @NotNull
	public get vocabulary(): Vocabulary {
		return ExprLexer.VOCABULARY;
	}
	// tslint:enable:no-trailing-whitespace


	constructor(input: CharStream) {
		super(input);
		this._interp = new LexerATNSimulator(ExprLexer._ATN, this);
	}

	// @Override
	public get grammarFileName(): string { return "Expr.g4"; }
github microsoft / botbuilder-js / libraries / botframework-expressions / src / commonRegex.ts View on Github external
private static antlrParse(pattern: string): ParseTree {
        const inputStream: ANTLRInputStream = new ANTLRInputStream(pattern);
        const lexer: CommonRegexLexer = new CommonRegexLexer(inputStream);
        const tokenStream: CommonTokenStream = new CommonTokenStream(lexer);
        const parser: CommonRegexParser = new CommonRegexParser(tokenStream);
        parser.removeErrorListeners();
        // tslint:disable-next-line: no-use-before-declare
        parser.addErrorListener(ErrorListener.Instance);
        parser.buildParseTree = true;

        return parser.parse();
    }
}
github zenclabs / codetree / src / antlr.ts View on Github external
export function parse(
  lexerConstructor: new (inputStream: antlr.ANTLRInputStream) => L,
  parserConstructor: new (tokenStream: antlr.TokenStream) => P,
  rootNode: (parser: P) => antlr.ParserRuleContext,
  code: string
): Node {
  let inputStream = new antlr.ANTLRInputStream(code);
  let lexer = new lexerConstructor(inputStream);
  lexer.removeErrorListeners();
  lexer.addErrorListener({
    syntaxError(
      // tslint:disable-next-line no-any
      recognizer: antlr.Recognizer,
      offendingSymbol: number | undefined,
      line: number,
      charPositionInLine: number,
      msg: string,
      e: antlr.RecognitionException | undefined
    ) {
      throw new Error(msg + " at " + line + ":" + charPositionInLine);
    }
  });
  let tokenStream = new antlr.CommonTokenStream(lexer);
github zenclabs / codetree / src / antlr.ts View on Github external
let lexer = new lexerConstructor(inputStream);
  lexer.removeErrorListeners();
  lexer.addErrorListener({
    syntaxError(
      // tslint:disable-next-line no-any
      recognizer: antlr.Recognizer,
      offendingSymbol: number | undefined,
      line: number,
      charPositionInLine: number,
      msg: string,
      e: antlr.RecognitionException | undefined
    ) {
      throw new Error(msg + " at " + line + ":" + charPositionInLine);
    }
  });
  let tokenStream = new antlr.CommonTokenStream(lexer);
  let parser = new parserConstructor(tokenStream);
  parser.removeErrorListeners();
  parser.addErrorListener({
    syntaxError(
      // tslint:disable-next-line no-any
      recognizer: antlr.Recognizer,
      offendingSymbol: antlr.Token | undefined,
      line: number,
      charPositionInLine: number,
      msg: string,
      e: antlr.RecognitionException | undefined
    ) {
      throw new Error(msg + " at " + line + ":" + charPositionInLine);
    }
  });
  let node = rootNode(parser);
github eirikpre / VSCode-SystemVerilog / src / compiling / ANTLRBackend.ts View on Github external
if (!isSystemVerilogDocument(document) && !isVerilogDocument(document)) {
                reject("The document is not a SystemVerilog/Verilog file.");
                return;
            }

            let visitedDocuments = new Map();
            let diagnosticCollection: Map = new Map();

            // Get document text
            let text = document.getText();
            // Perform macro replacements
            let new_text = this.macroReplace(text);
            // Create the lexer and parser
            let inputStream = new ANTLRInputStream(new_text);   
            let lexer = new SystemVerilogLexer(inputStream);
            let tokenStream = new CommonTokenStream(lexer);
            let parser = new SystemVerilogParser(tokenStream);

            //Use syntaxError to collect a list of errors found by the parser
            let syntaxError = new SyntaxErrorListener();
            parser.addErrorListener(syntaxError);

            // Parse the input
            let tree = parser.system_verilog_text();

            //place errors in the diagnostic list
            let diagnosticList = new Array();
            for (let i = 0; i < syntaxError.error_list.length; i++) {
                let range: Range = getLineRange(
                    syntaxError.error_list[i].line, 
                    syntaxError.error_list[i].offendingSymbol.text, 
                    syntaxError.error_list[i].charPositionInLine);
github eirikpre / VSCode-SystemVerilog / src / compiling / ANTLRCompiler.ts View on Github external
if (!isSystemVerilogDocument(document) && !isVerilogDocument(document)) {
                reject("The document is not a SystemVerilog/Verilog file.");
                return;
            }

            let visitedDocuments = new Map();
            let diagnosticCollection: Map = new Map();

            // Get document text
            let text = document.getText();
            // Perform macro replacements
            let new_text = this.macroReplace(text);
            // Create the lexer and parser
            let inputStream = new ANTLRInputStream(new_text);
            let lexer = new SystemVerilogLexer(inputStream);
            let tokenStream = new CommonTokenStream(lexer);
            let parser = new SystemVerilogParser(tokenStream);

            let syntaxError = new SyntaxErrorListener();
            parser.addErrorListener(syntaxError);

            // Parse the input, where `compilationUnit` is whatever entry point you defined
            let tree = parser.system_verilog_text();

            for (let i = 0; i < syntaxError.error_list.length; i++) {
                let diagnosticData: DiagnosticData = new DiagnosticData();

                diagnosticData.filePath = document.uri;
                diagnosticData.line = syntaxError.error_list[i].line;
                diagnosticData.diagnosticSeverity = DiagnosticSeverity.Error;
                diagnosticData.problem = this.getImprovedMessage(syntaxError.error_list[i],document.uri);
                diagnosticData.offendingSymbol = syntaxError.error_list[i].offendingSymbol.text;
github yifanwu / diel / src / compiler / compiler.ts View on Github external
export function ParsePlainDielAst(code: string) {
  // PrintCode(code);
  const inputStream = new ANTLRInputStream(code);
  const l = new lexer.DIELLexer(inputStream);
  const tokenStream = new CommonTokenStream(l);
  const p = new parser.DIELParser(tokenStream);
  const tree = p.queries();
  let visitor = new Visitor();
  let ast = visitor.visitQueries(tree);
  return ast;
}
github microsoft / botbuilder-js / libraries / botframework-expression / src / resources / ExpressionLexer.ts View on Github external
"T__17", "T__18", "T__19", "T__20", "LETTER", "DIGIT", "NUMBER", "WHITESPACE", 
		"IDENTIFIER", "NEWLINE", "STRING",
	];

	private static readonly _LITERAL_NAMES: Array = [
		undefined, "'!'", "'^'", "'*'", "'/'", "'+'", "'-'", "'=='", "'!='", "'<>'", 
		"'<'", "'<='", "'>'", "'>='", "'&&'", "'||'", "'('", "')'", "'.'", "'['", 
		"']'", "','",
	];
	private static readonly _SYMBOLIC_NAMES: Array = [
		undefined, undefined, undefined, undefined, undefined, undefined, undefined, 
		undefined, undefined, undefined, undefined, undefined, undefined, undefined, 
		undefined, undefined, undefined, undefined, undefined, undefined, undefined, 
		undefined, "NUMBER", "WHITESPACE", "IDENTIFIER", "NEWLINE", "STRING",
	];
	public static readonly VOCABULARY: Vocabulary = new VocabularyImpl(ExpressionLexer._LITERAL_NAMES, ExpressionLexer._SYMBOLIC_NAMES, []);

	// @Override
	// @NotNull
	public get vocabulary(): Vocabulary {
		return ExpressionLexer.VOCABULARY;
	}
	// tslint:enable:no-trailing-whitespace


	constructor(input: CharStream) {
		super(input);
		this._interp = new LexerATNSimulator(ExpressionLexer._ATN, this);
	}

	// @Override
	public get grammarFileName(): string { return "Expression.g4"; }
github microsoft / botbuilder-js / libraries / botbuilder-lg / src / generated / LGFileLexer.ts View on Github external
"ESCAPE_CHARACTER", "EXPRESSION", "TEMPLATE_REF", "TEXT_SEPARATOR", "TEXT",
	];

	private static readonly _LITERAL_NAMES: Array = [
		undefined, undefined, undefined, undefined, "'#'", undefined, undefined, 
		undefined, undefined, undefined, undefined, "'.'", "'('", "')'", "','",
	];
	private static readonly _SYMBOLIC_NAMES: Array = [
		undefined, "COMMENTS", "WS", "NEWLINE", "HASH", "DASH", "IMPORT_DESC", 
		"IMPORT_PATH", "INVALID_TOKEN_DEFAULT_MODE", "WS_IN_NAME", "IDENTIFIER", 
		"DOT", "OPEN_PARENTHESIS", "CLOSE_PARENTHESIS", "COMMA", "TEXT_IN_NAME", 
		"WS_IN_BODY_IGNORED", "IF", "ELSEIF", "ELSE", "SWITCH", "CASE", "DEFAULT", 
		"MULTI_LINE_TEXT", "ESCAPE_CHARACTER", "EXPRESSION", "TEMPLATE_REF", "TEXT_SEPARATOR", 
		"TEXT",
	];
	public static readonly VOCABULARY: Vocabulary = new VocabularyImpl(LGFileLexer._LITERAL_NAMES, LGFileLexer._SYMBOLIC_NAMES, []);

	// @Override
	// @NotNull
	public get vocabulary(): Vocabulary {
		return LGFileLexer.VOCABULARY;
	}
	// tslint:enable:no-trailing-whitespace


	  ignoreWS = true;             // usually we ignore whitespace, but inside template, whitespace is significant
	  expectKeywords = false;        // whether we are expecting IF/ELSEIF/ELSE or Switch/Case/Default keywords


	constructor(input: CharStream) {
		super(input);
		this._interp = new LexerATNSimulator(LGFileLexer._ATN, this);