|
1 | 1 | import antlr4 from 'antlr4'; |
2 | 2 | import Lexer from 'antlr4/Lexer.js'; |
3 | 3 | import { TextDocument } from 'vscode-languageserver-textdocument'; |
| 4 | +import Recognizer from 'antlr4/Recognizer'; |
| 5 | +import Token from 'antlr4/Token'; |
4 | 6 | import { IAnalyzer } from '../analyzer.js'; |
5 | 7 | import BitloopsVisitor from './BitloopsVisitor/BitloopsVisitor.js'; |
6 | 8 | import BitloopsLexer from './grammar/BitloopsLexer.js'; |
7 | 9 | import BitloopsParser from './grammar/BitloopsParser.js'; |
8 | 10 |
|
9 | | -export class BitloopsIntermediateASTParser implements IAnalyzer { |
| 11 | +// import { ErrorListener } from 'antlr4/error'; |
| 12 | +class VerboseListener { |
| 13 | + syntaxError = ( |
| 14 | + _recognizer: Recognizer, |
| 15 | + _offendingSymbol: Token, |
| 16 | + line: number, |
| 17 | + column: number, |
| 18 | + _msg: string, |
| 19 | + _e: any |
| 20 | + ) => { |
| 21 | + // const stack: any = recognizer.getTokenErrorDisplay(offendingSymbol); |
| 22 | + // console.log("rule stack: "+stack); |
| 23 | + console.log(`line: ${line}:${column}, offendingSymbol : ${_offendingSymbol.text}, msg: ${_msg}`); |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +export class AntlrAnalyzer implements IAnalyzer { |
10 | 28 | analyze(document: TextDocument): any[] { |
11 | | - const textFile = document.getText(); |
12 | | - const chars = new antlr4.InputStream(textFile); |
13 | | - const lexer: Lexer = new BitloopsLexer(chars) as any; |
14 | | - const tokens = new antlr4.CommonTokenStream(lexer); |
15 | | - const parser = new BitloopsParser(tokens); |
16 | | - const tree = parser.program(); |
17 | | - const bitloopsVisitor = new BitloopsVisitor(); |
18 | | - const result = bitloopsVisitor.visit(tree); |
19 | | - console.log('result', result); |
20 | | - return []; |
| 29 | + try { |
| 30 | + const textFile = document.getText(); |
| 31 | + |
| 32 | + const chars = new antlr4.InputStream(textFile); |
| 33 | + const lexer: Lexer = new BitloopsLexer(chars) as any; |
| 34 | + const tokens = new antlr4.CommonTokenStream(lexer); |
| 35 | + const parser = new BitloopsParser(tokens) as any; |
| 36 | + |
| 37 | + parser.removeErrorListeners(); |
| 38 | + parser.addErrorListener(new VerboseListener() as any); |
| 39 | + const tree = parser.program(); |
| 40 | + const bitloopsVisitor = new BitloopsVisitor(); |
| 41 | + const result = bitloopsVisitor.visit(tree).catch((e) => {}); |
| 42 | + console.log('result', result); |
| 43 | + return []; |
| 44 | + } catch (e) { |
| 45 | + console.log('error', e); |
| 46 | + return []; |
| 47 | + } |
21 | 48 | } |
22 | 49 | } |
0 commit comments