|
1 | 1 | import { Parser, TokenStream, BufferedTokenStream, Token } from 'antlr4ng';
|
2 | 2 | import { GoLexer } from './GoLexer.js';
|
| 3 | +import { GoParser, ImportSpecContext } from './GoParser.js'; |
3 | 4 |
|
4 | 5 | export default abstract class GoParserBase extends Parser {
|
| 6 | + |
| 7 | + debug: boolean; |
| 8 | + table: Set<string>; |
| 9 | + |
5 | 10 | constructor(input: TokenStream) {
|
6 | 11 | super(input);
|
7 | 12 | }
|
8 | 13 |
|
| 14 | + protected myreset(): void { |
| 15 | + this.debug = false; |
| 16 | + this.table = new Set<string>(); |
| 17 | + } |
| 18 | + |
9 | 19 | protected closingBracket(): boolean {
|
10 | 20 | const stream = this.inputStream as BufferedTokenStream;
|
11 |
| - const la = stream.LA(1); |
12 |
| - return la === GoLexer.R_CURLY || la === GoLexer.R_PAREN || la === Token.EOF; |
| 21 | + const la = stream.LT(1); |
| 22 | + return la.type === GoParser.R_CURLY || la.type === GoParser.R_PAREN || la.type === Token.EOF; |
| 23 | + } |
| 24 | + |
| 25 | + public isNotReceive(): boolean |
| 26 | + { |
| 27 | + const stream = this.inputStream as BufferedTokenStream; |
| 28 | + const la = stream.LT(2); |
| 29 | + return la.type !== GoParser.RECEIVE; |
| 30 | + } |
| 31 | + |
| 32 | + public addImportSpec(): void |
| 33 | + { |
| 34 | + const ctx = this.context; |
| 35 | + const count = ctx.getChildCount(); |
| 36 | + if (!(ctx instanceof ImportSpecContext)) { |
| 37 | + return; |
| 38 | + } |
| 39 | + const importSpec = ctx; |
| 40 | + var packageName = importSpec.packageName(); |
| 41 | + if (packageName != null) |
| 42 | + { |
| 43 | + var name = packageName.getText(); |
| 44 | + if (this.debug) console.log("Entering " + name); |
| 45 | + this.table.add(name); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + var name = importSpec.importPath().getText(); |
| 50 | + name = name.replaceAll("\"", ""); |
| 51 | + name = name.replaceAll("\\", "/"); |
| 52 | + const pathArr = name.split('/'); |
| 53 | + const fileArr = pathArr.at(-1).split('.'); |
| 54 | + const fileName = fileArr.at(-1).toString(); |
| 55 | + if (this.debug) console.log("Entering " + fileName); |
| 56 | + this.table.add(fileName); |
| 57 | + } |
13 | 58 | }
|
14 | 59 |
|
15 | 60 | protected isType(): boolean {
|
16 | 61 | const stream = this.inputStream as BufferedTokenStream;
|
17 | 62 | const la = stream.LA(1);
|
18 |
| - return la !== GoLexer.IDENTIFIER; |
| 63 | + return la !== GoParser.IDENTIFIER; |
| 64 | + } |
| 65 | + |
| 66 | + public isOperand(): boolean |
| 67 | + { |
| 68 | + const stream = this.inputStream as BufferedTokenStream; |
| 69 | + const la = stream.LT(1); |
| 70 | + if (la.text === "err") return true; |
| 71 | + var result = true; |
| 72 | + if (la.type !== GoParser.IDENTIFIER) { |
| 73 | + if (this.debug) console.log("isOperand Returning " + result + " for " + la); |
| 74 | + return result; |
| 75 | + } |
| 76 | + result = this.table.has(la.text); |
| 77 | + var la2 = stream.LT(2); |
| 78 | + // If it's not followed by a '.', then it really should be |
| 79 | + // considered as operand. |
| 80 | + if (la2.type !== GoParser.DOT) { |
| 81 | + result = true; |
| 82 | + if (this.debug) console.log("isOperand Returning " + result + " for " + la); |
| 83 | + return result; |
| 84 | + } |
| 85 | + // If it's followed by '.', and then followed by '(', then |
| 86 | + // it is a typeAssertion, and so la must be an operand. |
| 87 | + var la3 = stream.LT(3); |
| 88 | + if (la3.type === GoParser.L_PAREN) |
| 89 | + { |
| 90 | + result = true; |
| 91 | + if (this.debug) console.log("isOperand Returning " + result + " for " + la); |
| 92 | + return result; |
| 93 | + } |
| 94 | + if (this.debug) console.log("isOperand Returning " + result + " for " + la); |
| 95 | + return result; |
19 | 96 | }
|
20 | 97 |
|
21 |
| - protected isNotReceive(): boolean { |
| 98 | + public isConversion(): boolean |
| 99 | + { |
22 | 100 | const stream = this.inputStream as BufferedTokenStream;
|
23 |
| - const la = stream.LA(2); |
24 |
| - return la !== GoLexer.RECEIVE; |
25 |
| - } |
| 101 | + const la = stream.LT(1); |
| 102 | + var result = la.type !== GoParser.IDENTIFIER; |
| 103 | + if (this.debug) console.log("isConversion Returning " + result + " for " + la); |
| 104 | + return result; |
| 105 | + } |
| 106 | + |
| 107 | + public isMethodExpr(): boolean |
| 108 | + { |
| 109 | + const stream = this.inputStream as BufferedTokenStream; |
| 110 | + const la = stream.LT(1); |
| 111 | + var result = true; |
| 112 | + // See if it looks like a method expr. |
| 113 | + if (la.type === GoParser.STAR) { |
| 114 | + if (this.debug) console.log("isMethodExpr Returning " + result + " for " + la); |
| 115 | + return result; |
| 116 | + } |
| 117 | + if (la.type !== GoParser.IDENTIFIER) { |
| 118 | + result = false; |
| 119 | + if (this.debug) console.log("isMethodExpr Returning " + result + " for " + la); |
| 120 | + return result; |
| 121 | + } |
| 122 | + result = ! this.table.has(la.text); |
| 123 | + if (this.debug) console.log("isMethodExpr Returning " + result + " for " + la); |
| 124 | + return result; |
| 125 | + } |
26 | 126 | }
|
0 commit comments