antlr4 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +27 -0
- data/README.md +46 -0
- data/lib/antlr4.rb +262 -0
- data/lib/antlr4/BufferedTokenStream.rb +306 -0
- data/lib/antlr4/CommonTokenFactory.rb +53 -0
- data/lib/antlr4/CommonTokenStream.rb +56 -0
- data/lib/antlr4/FileStream.rb +14 -0
- data/lib/antlr4/InputStream.rb +82 -0
- data/lib/antlr4/IntervalSet.rb +341 -0
- data/lib/antlr4/LL1Analyzer.rb +177 -0
- data/lib/antlr4/Lexer.rb +335 -0
- data/lib/antlr4/ListTokenSource.rb +140 -0
- data/lib/antlr4/Parser.rb +562 -0
- data/lib/antlr4/ParserInterpreter.rb +149 -0
- data/lib/antlr4/ParserRuleContext.rb +162 -0
- data/lib/antlr4/PredictionContext.rb +690 -0
- data/lib/antlr4/Recognizer.rb +162 -0
- data/lib/antlr4/RuleContext.rb +226 -0
- data/lib/antlr4/Token.rb +124 -0
- data/lib/antlr4/TokenFactory.rb +3 -0
- data/lib/antlr4/TokenSource.rb +4 -0
- data/lib/antlr4/TokenStream.rb +3 -0
- data/lib/antlr4/TraceListener.rb +23 -0
- data/lib/antlr4/atn/ATN.rb +133 -0
- data/lib/antlr4/atn/ATNConfig.rb +146 -0
- data/lib/antlr4/atn/ATNConfigSet.rb +215 -0
- data/lib/antlr4/atn/ATNDeserializationOptions.rb +62 -0
- data/lib/antlr4/atn/ATNDeserializer.rb +604 -0
- data/lib/antlr4/atn/ATNSimulator.rb +43 -0
- data/lib/antlr4/atn/ATNState.rb +253 -0
- data/lib/antlr4/atn/ATNType.rb +22 -0
- data/lib/antlr4/atn/LexerATNSimulator.rb +612 -0
- data/lib/antlr4/atn/LexerAction.rb +311 -0
- data/lib/antlr4/atn/LexerActionExecutor.rb +134 -0
- data/lib/antlr4/atn/ParserATNSimulator.rb +1622 -0
- data/lib/antlr4/atn/PredictionMode.rb +525 -0
- data/lib/antlr4/atn/SemanticContext.rb +355 -0
- data/lib/antlr4/atn/Transition.rb +297 -0
- data/lib/antlr4/base.rb +60 -0
- data/lib/antlr4/dfa/DFA.rb +128 -0
- data/lib/antlr4/dfa/DFASerializer.rb +77 -0
- data/lib/antlr4/dfa/DFAState.rb +133 -0
- data/lib/antlr4/error.rb +151 -0
- data/lib/antlr4/error/DiagnosticErrorListener.rb +136 -0
- data/lib/antlr4/error/ErrorListener.rb +109 -0
- data/lib/antlr4/error/ErrorStrategy.rb +742 -0
- data/lib/antlr4/tree/Chunk.rb +31 -0
- data/lib/antlr4/tree/ParseTreeMatch.rb +105 -0
- data/lib/antlr4/tree/ParseTreePattern.rb +70 -0
- data/lib/antlr4/tree/ParseTreePatternMatcher.rb +334 -0
- data/lib/antlr4/tree/RuleTagToken.rb +39 -0
- data/lib/antlr4/tree/TokenTagToken.rb +38 -0
- data/lib/antlr4/tree/Tree.rb +204 -0
- data/lib/antlr4/tree/Trees.rb +111 -0
- data/lib/antlr4/version.rb +5 -0
- data/lib/antlr4/xpath/XPath.rb +354 -0
- data/lib/double_key_map.rb +78 -0
- data/lib/java_symbols.rb +24 -0
- data/lib/uuid.rb +87 -0
- data/test/test_intervalset.rb +664 -0
- data/test/test_tree.rb +140 -0
- data/test/test_uuid.rb +122 -0
- metadata +109 -0
@@ -0,0 +1,162 @@
|
|
1
|
+
|
2
|
+
class Recognizer
|
3
|
+
|
4
|
+
attr_accessor :listeners, :interp #, :stateNumber
|
5
|
+
attr_accessor :tokenTypeMapCache, :ruleIndexMapCache
|
6
|
+
attr_accessor :state
|
7
|
+
def initialize
|
8
|
+
@listeners = [ ConsoleErrorListener.INSTANCE ]
|
9
|
+
@interp = nil
|
10
|
+
@state = -1
|
11
|
+
@tokenTypeMapCache = Hash.new
|
12
|
+
@ruleIndexMapCache = Hash.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def getState
|
16
|
+
@state
|
17
|
+
end
|
18
|
+
def extractVersion(version)
|
19
|
+
pos = version.index(".")
|
20
|
+
major = version[0..pos-1]
|
21
|
+
version = version[pos+1..version.length]
|
22
|
+
pos = version.index(".")
|
23
|
+
if pos.nil?
|
24
|
+
pos = version.index("-")
|
25
|
+
end
|
26
|
+
if pos.nil?
|
27
|
+
pos = version.length
|
28
|
+
end
|
29
|
+
minor = version[0..pos-1]
|
30
|
+
return major, minor
|
31
|
+
end
|
32
|
+
|
33
|
+
def checkVersion(toolVersion)
|
34
|
+
runtimeVersion = "4.4.1"
|
35
|
+
rvmajor, rvminor = self.extractVersion(runtimeVersion)
|
36
|
+
tvmajor, tvminor = self.extractVersion(toolVersion)
|
37
|
+
if rvmajor!=tvmajor or rvminor!=tvminor
|
38
|
+
puts "ANTLR runtime and generated code versions disagree: #{runtimeVersion}!=#{toolVersion}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def addErrorListener(listener)
|
43
|
+
self.listeners.push(listener)
|
44
|
+
end
|
45
|
+
def getTokenTypeMap
|
46
|
+
tokenNames = self.getTokenNames()
|
47
|
+
if tokenNames.nil? then
|
48
|
+
raise UnsupportedOperationException.new("The current recognizer does not provide a list of token names.")
|
49
|
+
end
|
50
|
+
result = self.tokenTypeMapCache.get(tokenNames)
|
51
|
+
if result.nil?
|
52
|
+
result = tokenNames.zip(0..tokenNames.length)
|
53
|
+
result["EOF"] = Token::EOF
|
54
|
+
self.tokenTypeMapCache[tokenNames] = result
|
55
|
+
end
|
56
|
+
return result
|
57
|
+
end
|
58
|
+
# Get a map from rule names to rule indexes.
|
59
|
+
#
|
60
|
+
# <p>Used for XPath and tree pattern compilation.</p>
|
61
|
+
#
|
62
|
+
def getRuleIndexMap
|
63
|
+
ruleNames = self.ruleNames
|
64
|
+
if ruleNames.nil? then
|
65
|
+
raise UnsupportedOperationException.new("The current recognizer does not provide a list of rule names.")
|
66
|
+
end
|
67
|
+
result = self.ruleIndexMapCache.get(ruleNames)
|
68
|
+
if result.nil?
|
69
|
+
result = ruleNames.zip( 0..ruleNames.length)
|
70
|
+
self.ruleIndexMapCache[ruleNames] = result
|
71
|
+
end
|
72
|
+
return result
|
73
|
+
end
|
74
|
+
|
75
|
+
def getTokenType(tokenName)
|
76
|
+
ttype = self.getTokenTypeMap().get(tokenName)
|
77
|
+
if not ttype.nil? then
|
78
|
+
return ttype
|
79
|
+
else
|
80
|
+
return Token::INVALID_TYPE
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# What is the error header, normally line/character position information?#
|
85
|
+
def getErrorHeader(e) # :RecognitionException):
|
86
|
+
line = e.getOffendingToken().line
|
87
|
+
column = e.getOffendingToken().column
|
88
|
+
return "line #{line}:#{column}"
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
# How should a token be displayed in an error message? The default
|
93
|
+
# is to display just the text, but during development you might
|
94
|
+
# want to have a lot of information spit out. Override in that case
|
95
|
+
# to use t.toString() (which, for CommonToken, dumps everything about
|
96
|
+
# the token). This is better than forcing you to override a method in
|
97
|
+
# your token objects because you don't have to go modify your lexer
|
98
|
+
# so that it creates a new Java type.
|
99
|
+
#
|
100
|
+
# @deprecated This method is not called by the ANTLR 4 Runtime. Specific
|
101
|
+
# implementations of {@link ANTLRErrorStrategy} may provide a similar
|
102
|
+
# feature when necessary. For example, see
|
103
|
+
# {@link DefaultErrorStrategy#getTokenErrorDisplay}.
|
104
|
+
#
|
105
|
+
def getTokenErrorDisplay(t)
|
106
|
+
return "<no token>" if t.nil?
|
107
|
+
s = t.text
|
108
|
+
if s.nil?
|
109
|
+
if t.type==Token::EOF
|
110
|
+
s = "<EOF>"
|
111
|
+
else
|
112
|
+
s = "<" + str(t.type) + ">"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
s = s.gsub("\n","\\n")
|
116
|
+
s = s.gsub("\r","\\r")
|
117
|
+
s = s.gsub("\t","\\t")
|
118
|
+
return "'" + s + "'"
|
119
|
+
end
|
120
|
+
|
121
|
+
def getErrorListenerDispatch
|
122
|
+
return ProxyErrorListener.new(self.listeners)
|
123
|
+
end
|
124
|
+
|
125
|
+
# subclass needs to override these if there are sempreds or actions
|
126
|
+
# that the ATN interp needs to execute
|
127
|
+
def sempred(localctx, ruleIndex, actionIndex)
|
128
|
+
return true
|
129
|
+
end
|
130
|
+
|
131
|
+
def precpred(localctx, precedence)
|
132
|
+
return true
|
133
|
+
end
|
134
|
+
|
135
|
+
# def state
|
136
|
+
# @stateNumber
|
137
|
+
# end
|
138
|
+
|
139
|
+
# Indicate that the recognizer has changed internal state that is
|
140
|
+
# consistent with the ATN state passed in. This way we always know
|
141
|
+
# where we are in the ATN as the parser goes along. The rule
|
142
|
+
# context objects form a stack that lets us see the stack of
|
143
|
+
# invoking rules. Combine this and we have complete ATN
|
144
|
+
# configuration information.
|
145
|
+
|
146
|
+
# def state=(atnState)
|
147
|
+
# @stateNumber = atnState
|
148
|
+
# end
|
149
|
+
end
|
150
|
+
|
151
|
+
#import unittest
|
152
|
+
#class Test(unittest.TestCase):
|
153
|
+
# def testVersion(self):
|
154
|
+
# major, minor = Recognizer().extractVersion("1.2")
|
155
|
+
# self.assertEqual("1", major)
|
156
|
+
# self.assertEqual("2", minor)
|
157
|
+
# major, minor = Recognizer().extractVersion("1.2.3")
|
158
|
+
# self.assertEqual("1", major)
|
159
|
+
# self.assertEqual("2", minor)
|
160
|
+
# major, minor = Recognizer().extractVersion("1.2-snapshot")
|
161
|
+
# self.assertEqual("1", major)
|
162
|
+
# self.assertEqual("2", minor)
|
@@ -0,0 +1,226 @@
|
|
1
|
+
# A rule context is a record of a single rule invocation. It knows
|
2
|
+
# which context invoked it, if any. If there is no parent context, then
|
3
|
+
# naturally the invoking state is not valid. The parent link
|
4
|
+
# provides a chain upwards from the current rule invocation to the root
|
5
|
+
# of the invocation tree, forming a stack. We actually carry no
|
6
|
+
# information about the rule associated with this context (except
|
7
|
+
# when parsing). We keep only the state number of the invoking state from
|
8
|
+
# the ATN submachine that invoked this. Contrast this with the s
|
9
|
+
# pointer inside ParserRuleContext that tracks the current state
|
10
|
+
# being "executed" for the current rule.
|
11
|
+
#
|
12
|
+
# The parent contexts are useful for computing lookahead sets and
|
13
|
+
# getting error information.
|
14
|
+
#
|
15
|
+
# These objects are used during parsing and prediction.
|
16
|
+
# For the special case of parsers, we use the subclass
|
17
|
+
# ParserRuleContext.
|
18
|
+
#
|
19
|
+
# @see ParserRuleContext
|
20
|
+
|
21
|
+
class RuleContext < RuleNode
|
22
|
+
|
23
|
+
@@EMPTY = nil
|
24
|
+
def self.EMPTY
|
25
|
+
if @@EMPTY.nil? then
|
26
|
+
@@EMPTY = ParserRuleContext.new()
|
27
|
+
end
|
28
|
+
@@EMPTY
|
29
|
+
end
|
30
|
+
|
31
|
+
attr_accessor :parentCtx, :invokingState
|
32
|
+
def initialize(parent=nil, invoking_state=-1)
|
33
|
+
super()
|
34
|
+
# What context invoked this rule?
|
35
|
+
@parentCtx = parent
|
36
|
+
# What state invoked the rule associated with this context?
|
37
|
+
# The "return address" is the followState of invokingState
|
38
|
+
# If parent is null, this should be -1.
|
39
|
+
@invokingState = invoking_state
|
40
|
+
end
|
41
|
+
|
42
|
+
def depth
|
43
|
+
n = 0
|
44
|
+
p = self
|
45
|
+
while not p.nil? do
|
46
|
+
p = p.parentCtx
|
47
|
+
n = n + 1
|
48
|
+
end
|
49
|
+
return n
|
50
|
+
end
|
51
|
+
|
52
|
+
# A context is empty if there is no invoking state; meaning nobody call
|
53
|
+
# current context.
|
54
|
+
def isEmpty
|
55
|
+
return self.invokingState == -1
|
56
|
+
end
|
57
|
+
|
58
|
+
# satisfy the ParseTree / SyntaxTree interface
|
59
|
+
|
60
|
+
def getSourceInterval
|
61
|
+
return Antlr4::INVALID_INTERVAL
|
62
|
+
end
|
63
|
+
|
64
|
+
def getRuleContext
|
65
|
+
return self
|
66
|
+
end
|
67
|
+
|
68
|
+
def getPayload
|
69
|
+
return self
|
70
|
+
end
|
71
|
+
|
72
|
+
# Return the combined text of all child nodes. This method only considers
|
73
|
+
# tokens which have been added to the parse tree.
|
74
|
+
# <p>
|
75
|
+
# Since tokens on hidden channels (e.g. whitespace or comments) are not
|
76
|
+
# added to the parse trees, they will not appear in the output of this
|
77
|
+
# method.
|
78
|
+
#/
|
79
|
+
def getText
|
80
|
+
if self.getChildCount() == 0
|
81
|
+
return ""
|
82
|
+
end
|
83
|
+
StringIO.open do |builder|
|
84
|
+
self.getChildren().each {|child| builder.write(child.getText()) }
|
85
|
+
return builder.string()
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def getRuleIndex
|
90
|
+
return -1
|
91
|
+
end
|
92
|
+
|
93
|
+
def getChild(i)
|
94
|
+
return nil
|
95
|
+
end
|
96
|
+
|
97
|
+
def getChildCount
|
98
|
+
return 0
|
99
|
+
end
|
100
|
+
|
101
|
+
def getChildren
|
102
|
+
Array.new # [].map {|c| c }
|
103
|
+
end
|
104
|
+
def accept(visitor)
|
105
|
+
return visitor.visitChildren(self)
|
106
|
+
end
|
107
|
+
|
108
|
+
# # Call this method to view a parse tree in a dialog box visually.#/
|
109
|
+
# public Future<JDialog> inspect(@Nullable Parser parser) {
|
110
|
+
# List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
|
111
|
+
# return inspect(ruleNames);
|
112
|
+
# }
|
113
|
+
#
|
114
|
+
# public Future<JDialog> inspect(@Nullable List<String> ruleNames) {
|
115
|
+
# TreeViewer viewer = new TreeViewer(ruleNames, this);
|
116
|
+
# return viewer.open();
|
117
|
+
# }
|
118
|
+
#
|
119
|
+
# # Save this tree in a postscript file#/
|
120
|
+
# public void save(@Nullable Parser parser, String fileName)
|
121
|
+
# throws IOException, PrintException
|
122
|
+
# {
|
123
|
+
# List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
|
124
|
+
# save(ruleNames, fileName);
|
125
|
+
# }
|
126
|
+
#
|
127
|
+
# # Save this tree in a postscript file using a particular font name and size#/
|
128
|
+
# public void save(@Nullable Parser parser, String fileName,
|
129
|
+
# String fontName, int fontSize)
|
130
|
+
# throws IOException
|
131
|
+
# {
|
132
|
+
# List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
|
133
|
+
# save(ruleNames, fileName, fontName, fontSize);
|
134
|
+
# }
|
135
|
+
#
|
136
|
+
# # Save this tree in a postscript file#/
|
137
|
+
# public void save(@Nullable List<String> ruleNames, String fileName)
|
138
|
+
# throws IOException, PrintException
|
139
|
+
# {
|
140
|
+
# Trees.writePS(this, ruleNames, fileName);
|
141
|
+
# }
|
142
|
+
#
|
143
|
+
# # Save this tree in a postscript file using a particular font name and size#/
|
144
|
+
# public void save(@Nullable List<String> ruleNames, String fileName,
|
145
|
+
# String fontName, int fontSize)
|
146
|
+
# throws IOException
|
147
|
+
# {
|
148
|
+
# Trees.writePS(this, ruleNames, fileName, fontName, fontSize);
|
149
|
+
# }
|
150
|
+
#
|
151
|
+
# # Print out a whole tree, not just a node, in LISP format
|
152
|
+
# # (root child1 .. childN). Print just a node if this is a leaf.
|
153
|
+
# # We have to know the recognizer so we can get rule names.
|
154
|
+
# #/
|
155
|
+
# @Override
|
156
|
+
# public String toStringTree(@Nullable Parser recog) {
|
157
|
+
# return Trees.toStringTree(this, recog);
|
158
|
+
# }
|
159
|
+
#
|
160
|
+
# Print out a whole tree, not just a node, in LISP format
|
161
|
+
# (root child1 .. childN). Print just a node if this is a leaf.
|
162
|
+
#
|
163
|
+
def toStringTree(ruleNames=nil ,recog=nil)
|
164
|
+
return Trees.toStringTree(self, ruleNames, recog)
|
165
|
+
end
|
166
|
+
# }
|
167
|
+
#
|
168
|
+
# @Override
|
169
|
+
# public String toStringTree() {
|
170
|
+
# return toStringTree((List<String>)null);
|
171
|
+
# }
|
172
|
+
#
|
173
|
+
def to_s
|
174
|
+
return self.toString(nil, nil)
|
175
|
+
end
|
176
|
+
|
177
|
+
# @Override
|
178
|
+
# public String toString() {
|
179
|
+
# return toString((List<String>)null, (RuleContext)null);
|
180
|
+
# }
|
181
|
+
#
|
182
|
+
# public final String toString(@Nullable Recognizer<?,?> recog) {
|
183
|
+
# return toString(recog, ParserRuleContext.EMPTY);
|
184
|
+
# }
|
185
|
+
#
|
186
|
+
# public final String toString(@Nullable List<String> ruleNames) {
|
187
|
+
# return toString(ruleNames, null);
|
188
|
+
# }
|
189
|
+
#
|
190
|
+
# // recog null unless ParserRuleContext, in which case we use subclass toString(...)
|
191
|
+
# public String toString(@Nullable Recognizer<?,?> recog, @Nullable RuleContext stop) {
|
192
|
+
# String[] ruleNames = recog != null ? recog.getRuleNames() : null;
|
193
|
+
# List<String> ruleNamesList = ruleNames != null ? Arrays.asList(ruleNames) : null;
|
194
|
+
# return toString(ruleNamesList, stop);
|
195
|
+
# }
|
196
|
+
|
197
|
+
def toString(ruleNames, stop) #->str#ruleNames:list, stop:RuleContext)->str:
|
198
|
+
StringIO.open do |buf|
|
199
|
+
p = self
|
200
|
+
buf.write("[")
|
201
|
+
while (not p.nil?) and p != stop do
|
202
|
+
if ruleNames.nil? then
|
203
|
+
if not p.isEmpty()
|
204
|
+
buf.write(p.invokingState.to_s)
|
205
|
+
end
|
206
|
+
else
|
207
|
+
ri = p.getRuleIndex()
|
208
|
+
if ri >= 0 and ri < ruleNames.length
|
209
|
+
ruleName = ruleNames[ri]
|
210
|
+
else
|
211
|
+
ruleName = ri.to_s
|
212
|
+
end
|
213
|
+
# ruleName = ruleNames[ri] if ri >= 0 and ri < len(ruleNames) else str(ri)
|
214
|
+
buf.write(ruleName)
|
215
|
+
end
|
216
|
+
if p.parentCtx and (ruleNames or not p.parentCtx.isEmpty()) then
|
217
|
+
buf.write(" ")
|
218
|
+
end
|
219
|
+
p = p.parentCtx
|
220
|
+
end
|
221
|
+
buf.write("]")
|
222
|
+
return buf.string()
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
data/lib/antlr4/Token.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# A token has properties: text, type, line, character position in the line
|
2
|
+
# (so we can ignore tabs), token channel, index, and source from which
|
3
|
+
# we obtained this token.
|
4
|
+
|
5
|
+
class Token
|
6
|
+
|
7
|
+
INVALID_TYPE = 0
|
8
|
+
# During lookahead operations, this "token" signifies we hit rule end ATN state
|
9
|
+
# and did not follow it despite needing to.
|
10
|
+
EPSILON = -2
|
11
|
+
MIN_USER_TOKEN_TYPE = 1
|
12
|
+
EOF = -1
|
13
|
+
# All tokens go to the parser (unless skip() is called in that rule)
|
14
|
+
# on a particular "channel". The parser tunes to a particular channel
|
15
|
+
# so that whitespace etc... can go to the parser on a "hidden" channel.
|
16
|
+
DEFAULT_CHANNEL = 0
|
17
|
+
# Anything on different channel than DEFAULT_CHANNEL is not parsed
|
18
|
+
# by parser.
|
19
|
+
HIDDEN_CHANNEL = 1
|
20
|
+
|
21
|
+
attr_accessor :source, :type, :channel, :start, :stop, :tokenIndex
|
22
|
+
attr_accessor :line, :column , :text
|
23
|
+
# A token has properties: text, type, line, character position in the line
|
24
|
+
# (so we can ignore tabs), token channel, index, and source from which
|
25
|
+
# we obtained this token.
|
26
|
+
def initialize()
|
27
|
+
self.source = nil
|
28
|
+
self.type = nil # token type of the token
|
29
|
+
self.channel = nil # The parser ignores everything not on DEFAULT_CHANNEL
|
30
|
+
self.start = -1 # optional; return -1 if not implemented.
|
31
|
+
self.stop = -1 # optional; return -1 if not implemented.
|
32
|
+
self.tokenIndex = nil # from 0..n-1 of the token object in the input stream
|
33
|
+
self.line = nil # line=1..n of the 1st character
|
34
|
+
self.column = nil # beginning of the line at which it occurs, 0..n-1
|
35
|
+
end
|
36
|
+
# Explicitly set the text for this token. If {code text} is not
|
37
|
+
# {@code null}, then {@link #getText} will return this value rather than
|
38
|
+
# extracting the text from the input.
|
39
|
+
#
|
40
|
+
# @param text The explicit text of the token, or {@code null} if the text
|
41
|
+
# should be obtained from the input along with the start and stop indexes
|
42
|
+
# of the token.
|
43
|
+
|
44
|
+
def getTokenSource()
|
45
|
+
return self.source[0]
|
46
|
+
end
|
47
|
+
|
48
|
+
def getInputStream()
|
49
|
+
return self.source[1]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class CommonToken < Token
|
54
|
+
# An empty {@link Pair} which is used as the default value of
|
55
|
+
# {@link #source} for tokens that do not have a source.
|
56
|
+
EMPTY_SOURCE = [nil, nil]
|
57
|
+
|
58
|
+
def initialize(source = EMPTY_SOURCE, type = nil, channel=Token::DEFAULT_CHANNEL, start=-1, stop=-1)
|
59
|
+
super()
|
60
|
+
self.source = source
|
61
|
+
self.type = type
|
62
|
+
self.channel = channel
|
63
|
+
self.start = start
|
64
|
+
self.stop = stop
|
65
|
+
self.tokenIndex = -1
|
66
|
+
if source[0] then
|
67
|
+
self.line = source[0].line
|
68
|
+
self.column = source[0].column
|
69
|
+
else
|
70
|
+
self.column = -1
|
71
|
+
self.line = nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
# Constructs a new {@link CommonToken} as a copy of another {@link Token}.
|
75
|
+
#
|
76
|
+
# <p>
|
77
|
+
# If {@code oldToken} is also a {@link CommonToken} instance, the newly
|
78
|
+
# constructed token will share a reference to the {@link #text} field and
|
79
|
+
# the {@link Pair} stored in {@link #source}. Otherwise, {@link #text} will
|
80
|
+
# be assigned the result of calling {@link #getText}, and {@link #source}
|
81
|
+
# will be constructed from the result of {@link Token#getTokenSource} and
|
82
|
+
# {@link Token#getInputStream}.</p>
|
83
|
+
#
|
84
|
+
# @param oldToken The token to copy.
|
85
|
+
#
|
86
|
+
def clone()
|
87
|
+
# t = CommonToken(self.source, self.type, self.channel, self.start, self.stop)
|
88
|
+
# t.tokenIndex = self.tokenIndex
|
89
|
+
# t.line = self.line
|
90
|
+
# t.column = self.column
|
91
|
+
# t.text = self.text
|
92
|
+
# return t
|
93
|
+
raise NotImplementedError.new("Token.clone not implemented")
|
94
|
+
end
|
95
|
+
def text()
|
96
|
+
return @text if @text
|
97
|
+
input = self.getInputStream()
|
98
|
+
return nil if input.nil?
|
99
|
+
n = input.size
|
100
|
+
if self.start < n and self.stop < n then
|
101
|
+
return input.getText(self.start, self.stop)
|
102
|
+
else
|
103
|
+
return '<EOF>'
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def to_s
|
108
|
+
txt = self.text()
|
109
|
+
if txt.nil? then
|
110
|
+
txt = "<no text>"
|
111
|
+
else
|
112
|
+
txt = txt.gsub("\n","\\n").gsub("\r","\\r").gsub("\t","\\t")
|
113
|
+
end
|
114
|
+
if self.channel > 0 then
|
115
|
+
c = ",channel=#{channel}"
|
116
|
+
else
|
117
|
+
c = ""
|
118
|
+
end
|
119
|
+
"[@#{tokenIndex},#{start}:#{stop}='#{txt}',<#{type}>#{c},#{line}:#{column}]"
|
120
|
+
end
|
121
|
+
def inspect
|
122
|
+
to_s
|
123
|
+
end
|
124
|
+
end
|