1
- /* Copyright (c) 2012-2016 The ANTLR Project. All rights reserved.
2
- * Use of this file is governed by the BSD 3-clause license that
3
- * can be found in the LICENSE.txt file in the project root.
4
- */
5
-
6
-
7
- /** This is all the parsing support code essentially; most of it is error recovery stuff. */
8
- //public abstract class Parser : Recognizer<Token, ParserATNSimulator> {
1
+ ///
2
+ /// Copyright (c) 2012-2016 The ANTLR Project. All rights reserved.
3
+ /// Use of this file is governed by the BSD 3-clause license that
4
+ /// can be found in the LICENSE.txt file in the project root.
5
+ ///
9
6
10
7
import Foundation
11
8
9
+ /// This is all the parsing support code essentially; most of it is error recovery stuff.
12
10
open class Parser : Recognizer < ParserATNSimulator > {
13
11
public static let EOF : Int = - 1
14
12
public static var ConsoleError = true
15
- //false
16
13
17
14
public class TraceListener : ParseTreeListener {
18
15
var host : Parser
@@ -62,10 +59,7 @@ open class Parser: Recognizer<ParserATNSimulator> {
62
59
63
60
64
61
public func exitEveryRule( _ ctx: ParserRuleContext ) {
65
- //TODO: check necessary
66
- // if (ctx.children is ArrayList) {
67
- // (ctx.children as ArrayList<?>).trimToSize();
68
- // }
62
+ // TODO: Print exit info.
69
63
}
70
64
}
71
65
@@ -75,18 +69,15 @@ open class Parser: Recognizer<ParserATNSimulator> {
75
69
*
76
70
* @see org.antlr.v4.runtime.atn.ATNDeserializationOptions#isGenerateRuleBypassTransitions()
77
71
*/
78
- //private let bypassAltsAtnCache : Dictionary<String, ATN> =
79
- // WeakHashMap<String, ATN>(); MapTable<NSString, ATN>
80
-
81
72
private let bypassAltsAtnCache : HashMap < String , ATN > = HashMap < String , ATN > ( )
73
+
82
74
/**
83
75
* The error handling strategy for the parser. The default value is a new
84
76
* instance of {@link org.antlr.v4.runtime.DefaultErrorStrategy}.
85
77
*
86
78
* @see #getErrorHandler
87
79
* @see #setErrorHandler
88
80
*/
89
-
90
81
public var _errHandler : ANTLRErrorStrategy = DefaultErrorStrategy ( )
91
82
92
83
/**
@@ -177,14 +168,15 @@ open class Parser: Recognizer<ParserATNSimulator> {
177
168
* strategy to attempt recovery. If {@link #getBuildParseTree} is
178
169
* {@code true} and the token index of the symbol returned by
179
170
* {@link org.antlr.v4.runtime.ANTLRErrorStrategy#recoverInline} is -1, the symbol is added to
180
- * the parse tree by calling {@link org.antlr.v4.runtime.ParserRuleContext#addErrorNode}.</p>
171
+ * the parse tree by calling {@link #createErrorNode(ParserRuleContext, Token)} then
172
+ * {@link ParserRuleContext#addErrorNode(ErrorNode)}.</p>
181
173
*
182
174
* @param ttype the token type to match
183
175
* @return the matched symbol
184
176
* @throws org.antlr.v4.runtime.RecognitionException if the current input symbol did not match
185
177
* {@code ttype} and the error strategy could not recover from the
186
178
* mismatched symbol
187
- *///; RecognitionException
179
+ */
188
180
@discardableResult
189
181
public func match( _ ttype: Int ) throws -> Token {
190
182
var t : Token = try getCurrentToken ( )
@@ -196,7 +188,7 @@ open class Parser: Recognizer<ParserATNSimulator> {
196
188
if _buildParseTrees && t. getTokenIndex ( ) == - 1 {
197
189
// we must have conjured up a new token during single token insertion
198
190
// if it's not the current symbol
199
- _ctx!. addErrorNode ( t )
191
+ _ctx!. addErrorNode ( createErrorNode ( parent : _ctx! , t : t ) )
200
192
}
201
193
}
202
194
return t
@@ -212,7 +204,8 @@ open class Parser: Recognizer<ParserATNSimulator> {
212
204
* strategy to attempt recovery. If {@link #getBuildParseTree} is
213
205
* {@code true} and the token index of the symbol returned by
214
206
* {@link org.antlr.v4.runtime.ANTLRErrorStrategy#recoverInline} is -1, the symbol is added to
215
- * the parse tree by calling {@link org.antlr.v4.runtime.ParserRuleContext#addErrorNode}.</p>
207
+ * the parse tree by calling {@link #createErrorNode(ParserRuleContext, Token)} then
208
+ * {@link ParserRuleContext#addErrorNode(ErrorNode)}.</p>
216
209
*
217
210
* @return the matched symbol
218
211
* @throws org.antlr.v4.runtime.RecognitionException if the current input symbol did not match
@@ -230,7 +223,7 @@ open class Parser: Recognizer<ParserATNSimulator> {
230
223
if _buildParseTrees && t. getTokenIndex ( ) == - 1 {
231
224
// we must have conjured up a new token during single token insertion
232
225
// if it's not the current symbol
233
- _ctx!. addErrorNode ( t )
226
+ _ctx!. addErrorNode ( createErrorNode ( parent : _ctx! , t : t ) )
234
227
}
235
228
}
236
229
@@ -562,11 +555,11 @@ open class Parser: Recognizer<ParserATNSimulator> {
562
555
* </pre>
563
556
*
564
557
* If the parser is not in error recovery mode, the consumed symbol is added
565
- * to the parse tree using {@link org.antlr.v4.runtime. ParserRuleContext#addChild(org.antlr.v4.runtime.Token )}, and
558
+ * to the parse tree using {@link ParserRuleContext#addChild(TerminalNode )}, and
566
559
* {@link org.antlr.v4.runtime.tree.ParseTreeListener#visitTerminal} is called on any parse listeners.
567
560
* If the parser <em>is</em> in error recovery mode, the consumed symbol is
568
- * added to the parse tree using
569
- * {@link org.antlr.v4.runtime. ParserRuleContext#addErrorNode(org.antlr.v4.runtime.Token)}, and
561
+ * added to the parse tree using {@link #createErrorNode(ParserRuleContext, Token)} then
562
+ * {@link ParserRuleContext#addErrorNode(ErrorNode)} and
570
563
* {@link org.antlr.v4.runtime.tree.ParseTreeListener#visitErrorNode} is called on any parse
571
564
* listeners.
572
565
*/
@@ -583,14 +576,14 @@ open class Parser: Recognizer<ParserATNSimulator> {
583
576
584
577
if _buildParseTrees || hasListener {
585
578
if _errHandler. inErrorRecoveryMode ( self ) {
586
- let node : ErrorNode = _ctx. addErrorNode ( o )
579
+ let node : ErrorNode = _ctx. addErrorNode ( createErrorNode ( parent : _ctx , t : o ) )
587
580
if let _parseListeners = _parseListeners {
588
581
for listener : ParseTreeListener in _parseListeners {
589
582
listener. visitErrorNode ( node)
590
583
}
591
584
}
592
585
} else {
593
- let node : TerminalNode = _ctx. addChild ( o )
586
+ let node : TerminalNode = _ctx. addChild ( createTerminalNode ( parent : _ctx , t : o ) )
594
587
if let _parseListeners = _parseListeners {
595
588
for listener : ParseTreeListener in _parseListeners {
596
589
listener. visitTerminal ( node)
@@ -600,6 +593,38 @@ open class Parser: Recognizer<ParserATNSimulator> {
600
593
}
601
594
return o
602
595
}
596
+
597
+ /** How to create a token leaf node associated with a parent.
598
+ * Typically, the terminal node to create is not a function of the parent
599
+ * but this method must still set the parent pointer of the terminal node
600
+ * returned. I would prefer having {@link ParserRuleContext#addAnyChild(ParseTree)}
601
+ * set the parent pointer, but the parent pointer is implementation dependent
602
+ * and currently there is no setParent() in {@link TerminalNode} (and can't
603
+ * add method in Java 1.7 without breaking backward compatibility).
604
+ *
605
+ * @since 4.6.1
606
+ */
607
+ public func createTerminalNode( parent: ParserRuleContext , t: Token ) -> TerminalNode {
608
+ let node = TerminalNodeImpl ( t) ;
609
+ node. parent = parent;
610
+ return node;
611
+ }
612
+
613
+ /** How to create an error node, given a token, associated with a parent.
614
+ * Typically, the error node to create is not a function of the parent
615
+ * but this method must still set the parent pointer of the terminal node
616
+ * returned. I would prefer having {@link ParserRuleContext#addAnyChild(ParseTree)}
617
+ * set the parent pointer, but the parent pointer is implementation dependent
618
+ * and currently there is no setParent() in {@link ErrorNode} (and can't
619
+ * add method in Java 1.7 without breaking backward compatibility).
620
+ *
621
+ * @since 4.6.1
622
+ */
623
+ public func createErrorNode( parent: ParserRuleContext , t: Token ) -> ErrorNode {
624
+ let node = ErrorNode ( t) ;
625
+ node. parent = parent;
626
+ return node;
627
+ }
603
628
604
629
internal func addContextToParseTree( ) {
605
630
0 commit comments