Created
May 11, 2020 21:27
-
-
Save amazzalel-habib/7a2468872e4ca225937889fc23ae3e36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { TodoLangGrammarParser, TodoExpressionsContext } from "../ANTLR/TodoLangGrammarParser"; | |
import { TodoLangGrammarLexer } from "../ANTLR/TodoLangGrammarLexer"; | |
import { ANTLRInputStream, CommonTokenStream } from "antlr4ts"; | |
import TodoLangErrorListener, { ITodoLangError } from "./TodoLangErrorListener"; | |
function parse(code: string): {ast:TodoExpressionsContext, errors: ITodoLangError[]} { | |
const inputStream = new ANTLRInputStream(code); | |
const lexer = new TodoLangGrammarLexer(inputStream); | |
lexer.removeErrorListeners() | |
const todoLangErrorsListner = new TodoLangErrorListener(); | |
lexer.addErrorListener(todoLangErrorsListner); | |
const tokenStream = new CommonTokenStream(lexer); | |
const parser = new TodoLangGrammarParser(tokenStream); | |
parser.removeErrorListeners(); | |
parser.addErrorListener(todoLangErrorsListner); | |
const ast = parser.todoExpressions(); | |
const errors: ITodoLangError[] = todoLangErrorsListner.getErrors(); | |
return {ast, errors}; | |
} | |
export function parseAndGetASTRoot(code: string): TodoExpressionsContext { | |
const {ast} = parse(code); | |
return ast; | |
} | |
export function parseAndGetSyntaxErrors(code: string): ITodoLangError[] { | |
const {errors} = parse(code); | |
return errors; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment