-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRecognizer.js
147 lines (126 loc) · 4.51 KB
/
Recognizer.js
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
//
/* Copyright (c) 2012-2017 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.
*/
//
var Token = require('./Token').Token;
var ConsoleErrorListener = require('./error/ErrorListener').ConsoleErrorListener;
var ProxyErrorListener = require('./error/ErrorListener').ProxyErrorListener;
function Recognizer() {
this._listeners = [ ConsoleErrorListener.INSTANCE ];
this._interp = null;
this._stateNumber = -1;
return this;
}
Recognizer.tokenTypeMapCache = {};
Recognizer.ruleIndexMapCache = {};
Recognizer.prototype.checkVersion = function(toolVersion) {
var runtimeVersion = "4.7.2";
if (runtimeVersion!==toolVersion) {
console.log("ANTLR runtime and generated code versions disagree: "+runtimeVersion+"!="+toolVersion);
}
};
Recognizer.prototype.addErrorListener = function(listener) {
this._listeners.push(listener);
};
Recognizer.prototype.removeErrorListeners = function() {
this._listeners = [];
};
Recognizer.prototype.getTokenTypeMap = function() {
var tokenNames = this.getTokenNames();
if (tokenNames===null) {
throw("The current recognizer does not provide a list of token names.");
}
var 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>
//
Recognizer.prototype.getRuleIndexMap = function() {
var ruleNames = this.ruleNames;
if (ruleNames===null) {
throw("The current recognizer does not provide a list of rule names.");
}
var result = this.ruleIndexMapCache[ruleNames];
if(result===undefined) {
result = ruleNames.reduce(function(o, k, i) { o[k] = i; });
this.ruleIndexMapCache[ruleNames] = result;
}
return result;
};
Recognizer.prototype.getTokenType = function(tokenName) {
var ttype = this.getTokenTypeMap()[tokenName];
if (ttype !==undefined) {
return ttype;
} else {
return Token.INVALID_TYPE;
}
};
// What is the error header, normally line/character position information?//
Recognizer.prototype.getErrorHeader = function(e) {
var line = e.getOffendingToken().line;
var 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}.
//
Recognizer.prototype.getTokenErrorDisplay = function(t) {
if (t===null) {
return "<no token>";
}
var 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 + "'";
};
Recognizer.prototype.getErrorListenerDispatch = function() {
return new ProxyErrorListener(this._listeners);
};
// subclass needs to override these if there are sempreds or actions
// that the ATN interp needs to execute
Recognizer.prototype.sempred = function(localctx, ruleIndex, actionIndex) {
return true;
};
Recognizer.prototype.precpred = function(localctx , precedence) {
return true;
};
//Indicate that the recognizer has changed internal state that is
//consistent with the ATN state passed in. This way we always know
//where we are in the ATN as the parser goes along. The rule
//context objects form a stack that lets us see the stack of
//invoking rules. Combine this and we have complete ATN
//configuration information.
Object.defineProperty(Recognizer.prototype, "state", {
get : function() {
return this._stateNumber;
},
set : function(state) {
this._stateNumber = state;
}
});
exports.Recognizer = Recognizer;