-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathRecognizer.js
More file actions
167 lines (146 loc) · 5.17 KB
/
Recognizer.js
File metadata and controls
167 lines (146 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Copyright (c) 2012-2022 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
import Token from './Token.js';
import ConsoleErrorListener from './error/ConsoleErrorListener.js';
import ProxyErrorListener from './error/ProxyErrorListener.js';
export default class Recognizer {
constructor() {
this._listeners = [ ConsoleErrorListener.INSTANCE ];
this._interp = null;
this._stateNumber = -1;
}
checkVersion(toolVersion) {
let runtimeVersion = "4.13.2";
if (runtimeVersion!==toolVersion) {
console.log("ANTLR runtime and generated code versions disagree: "+runtimeVersion+"!="+toolVersion);
}
}
addErrorListener(listener) {
this._listeners.push(listener);
}
removeErrorListeners() {
this._listeners = [];
}
getLiteralNames() {
return Object.getPrototypeOf(this).constructor.literalNames || [];
}
getSymbolicNames() {
return Object.getPrototypeOf(this).constructor.symbolicNames || [];
}
getTokenNames() {
if(!this.tokenNames) {
let literalNames = this.getLiteralNames();
let symbolicNames = this.getSymbolicNames();
let length = literalNames.length > symbolicNames.length ? literalNames.length : symbolicNames.length;
this.tokenNames = [];
for(let i=0; i<length; i++) {
this.tokenNames[i] = literalNames[i] || symbolicNames[i] || "<INVALID";
}
}
return this.tokenNames;
}
getTokenTypeMap() {
let tokenNames = this.getTokenNames();
if (tokenNames===null) {
throw("The current recognizer does not provide a list of token names.");
}
let result = this.tokenTypeMapCache[tokenNames];
if(result===undefined) {
result = tokenNames.reduce(function(o, k, i) { o[k] = i; });
result.EOF = Token.EOF;
this.tokenTypeMapCache[tokenNames] = result;
}
return result;
}
/**
* Get a map from rule names to rule indexes.
* <p>Used for XPath and tree pattern compilation.</p>
*/
getRuleIndexMap() {
let ruleNames = this.ruleNames;
if (ruleNames===null) {
throw("The current recognizer does not provide a list of rule names.");
}
let result = this.ruleIndexMapCache[ruleNames]; // todo: should it be Recognizer.ruleIndexMapCache ?
if(result===undefined) {
result = ruleNames.reduce(function(o, k, i) { o[k] = i; });
this.ruleIndexMapCache[ruleNames] = result;
}
return result;
}
getTokenType(tokenName) {
let ttype = this.getTokenTypeMap()[tokenName];
if (ttype !==undefined) {
return ttype;
} else {
return Token.INVALID_TYPE;
}
}
// What is the error header, normally line/character position information?
getErrorHeader(e) {
let line = e.getOffendingToken().line;
let column = e.getOffendingToken().column;
return "line " + line + ":" + column;
}
/**
* How should a token be displayed in an error message? The default
* is to display just the text, but during development you might
* want to have a lot of information spit out. Override in that case
* to use t.toString() (which, for CommonToken, dumps everything about
* the token). This is better than forcing you to override a method in
* your token objects because you don't have to go modify your lexer
* so that it creates a new Java type.
*
* @deprecated This method is not called by the ANTLR 4 Runtime. Specific
* implementations of {@link ANTLRErrorStrategy} may provide a similar
* feature when necessary. For example, see
* {@link DefaultErrorStrategy//getTokenErrorDisplay}.*/
getTokenErrorDisplay(t) {
if (t===null) {
return "<no token>";
}
let s = t.text;
if (s===null) {
if (t.type===Token.EOF) {
s = "<EOF>";
} else {
s = "<" + t.type + ">";
}
}
s = s.replace("\n","\\n").replace("\r","\\r").replace("\t","\\t");
return "'" + s + "'";
}
/**
* @deprecated since ANTLR 4.13.2; use getErrorListener instead
*/
getErrorListenerDispatch() {
console.warn("Calling deprecated method in Recognizer class: getErrorListenerDispatch()");
return this.getErrorListener();
}
getErrorListener() {
return new ProxyErrorListener(this._listeners);
}
/**
* subclass needs to override these if there are sempreds or actions
* that the ATN interp needs to execute
*/
sempred(localctx, ruleIndex, actionIndex) {
return true;
}
precpred(localctx , precedence) {
return true;
}
get atn() {
return this._interp.atn;
}
get state(){
return this._stateNumber;
}
set state(state) {
this._stateNumber = state;
}
}
Recognizer.tokenTypeMapCache = {};
Recognizer.ruleIndexMapCache = {};