Merge pull request #1606 from hanjoes/spm

Tweak Repo to use SwiftPackageManager.
This commit is contained in:
Terence Parr 2017-01-29 10:31:53 -08:00 committed by GitHub
commit bf4fa40ff9
239 changed files with 8044 additions and 9315 deletions

View File

@ -57,7 +57,7 @@ public class BaseSwiftTest implements RuntimeTestSupport {
//add antlr.swift
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
final URL swiftRuntime = loader.getResource("Swift/Antlr4");
final URL swiftRuntime = loader.getResource("Swift/Sources/Antlr4");
if (swiftRuntime == null) {
throw new RuntimeException("Swift runtime file not found at:" + swiftRuntime.getPath());
}

View File

@ -1,23 +0,0 @@
import Antlr4
var input = "hello world"
let lexer = HelloLexer(ANTLRInputStream(input))
let tokens = CommonTokenStream(lexer)
do {
let parser = try HelloParser(tokens)
let tree = try parser.r()
let walker = ParseTreeWalker()
try walker.walk(HelloWalker(), tree)
}
catch ANTLRException.cannotInvokeStartRule {
print("Error: cannot invoke start rule.")
}
catch ANTLRException.recognition(let e) {
print("Unrecoverable recognition error: \(e)")
}
catch {
print("Unknown error: \(error)")
}

View File

@ -1,5 +0,0 @@
// Define a grammar called Hello
grammar Hello;
r : 'hello' ID ; // match keyword hello followed by an identifier
ID : [a-z]+ ; // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

View File

@ -1,4 +0,0 @@
T__0=1
ID=2
WS=3
'hello'=1

View File

@ -1,20 +0,0 @@
// Generated from Hello.g4 by ANTLR 4.6
import Antlr4
/**
* This class provides an empty implementation of {@link HelloVisitor},
* which can be extended to create a visitor which only needs to handle a subset
* of the available methods.
*
* @param <T> The return type of the visit operation. Use {@link Void} for
* operations with no return type.
*/
open class HelloBaseVisitor<T>: AbstractParseTreeVisitor<T> {
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
open func visitR(_ ctx: HelloParser.RContext) -> T? { return visitChildren(ctx) }
}

View File

@ -1,4 +0,0 @@
T__0=1
ID=2
WS=3
'hello'=1

View File

@ -1,3 +0,0 @@
class HelloLexerATN {
let jsonString: String = "{\"version\":3,\"uuid\":\"aadb8d7e-aeef-4415-ad2b-8204d6cf042e\",\"grammarType\":0,\"maxTokenType\":3,\"states\":[{\"stateType\":6,\"ruleIndex\":-1},{\"stateType\":2,\"ruleIndex\":0},{\"stateType\":7,\"ruleIndex\":0},{\"stateType\":2,\"ruleIndex\":1},{\"stateType\":7,\"ruleIndex\":1},{\"stateType\":2,\"ruleIndex\":2},{\"stateType\":7,\"ruleIndex\":2},{\"stateType\":1,\"ruleIndex\":0},{\"stateType\":1,\"ruleIndex\":0},{\"stateType\":1,\"ruleIndex\":0},{\"stateType\":1,\"ruleIndex\":0},{\"stateType\":1,\"ruleIndex\":0},{\"stateType\":1,\"ruleIndex\":0},{\"stateType\":1,\"ruleIndex\":1},{\"stateType\":4,\"ruleIndex\":1,\"detailStateNumber\":15},{\"stateType\":8,\"ruleIndex\":1},{\"stateType\":11,\"ruleIndex\":1},{\"stateType\":12,\"ruleIndex\":1,\"detailStateNumber\":16},{\"stateType\":1,\"ruleIndex\":2},{\"stateType\":4,\"ruleIndex\":2,\"detailStateNumber\":20},{\"stateType\":8,\"ruleIndex\":2},{\"stateType\":11,\"ruleIndex\":2},{\"stateType\":12,\"ruleIndex\":2,\"detailStateNumber\":21},{\"stateType\":1,\"ruleIndex\":2},{\"stateType\":1,\"ruleIndex\":2}],\"nonGreedyStates\":[],\"precedenceStates\":[],\"ruleToStartState\":[{\"stateNumber\":1,\"ruleToTokenType\":1},{\"stateNumber\":3,\"ruleToTokenType\":2},{\"stateNumber\":5,\"ruleToTokenType\":3}],\"modeToStartState\":[0],\"nsets\":2,\"IntervalSet\":[{\"size\":1,\"containsEof\":0,\"Intervals\":[{\"a\":97,\"b\":122}]},{\"size\":3,\"containsEof\":0,\"Intervals\":[{\"a\":9,\"b\":10},{\"a\":13,\"b\":13},{\"a\":32,\"b\":32}]}],\"allTransitionsBuilder\":[[{\"src\":0,\"trg\":1,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0},{\"src\":0,\"trg\":3,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0},{\"src\":0,\"trg\":5,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":1,\"trg\":7,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":3,\"trg\":14,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":5,\"trg\":19,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":7,\"trg\":8,\"edgeType\":5,\"arg1\":104,\"arg2\":0,\"arg3\":0}],[{\"src\":8,\"trg\":9,\"edgeType\":5,\"arg1\":101,\"arg2\":0,\"arg3\":0}],[{\"src\":9,\"trg\":10,\"edgeType\":5,\"arg1\":108,\"arg2\":0,\"arg3\":0}],[{\"src\":10,\"trg\":11,\"edgeType\":5,\"arg1\":108,\"arg2\":0,\"arg3\":0}],[{\"src\":11,\"trg\":12,\"edgeType\":5,\"arg1\":111,\"arg2\":0,\"arg3\":0}],[{\"src\":12,\"trg\":2,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":13,\"trg\":15,\"edgeType\":7,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":14,\"trg\":13,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":15,\"trg\":16,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":16,\"trg\":14,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0},{\"src\":16,\"trg\":17,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":17,\"trg\":4,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":18,\"trg\":20,\"edgeType\":7,\"arg1\":1,\"arg2\":0,\"arg3\":0}],[{\"src\":19,\"trg\":18,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":20,\"trg\":21,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":21,\"trg\":19,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0},{\"src\":21,\"trg\":22,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":22,\"trg\":23,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}],[{\"src\":23,\"trg\":24,\"edgeType\":6,\"arg1\":2,\"arg2\":0,\"arg3\":0}],[{\"src\":24,\"trg\":6,\"edgeType\":1,\"arg1\":0,\"arg2\":0,\"arg3\":0}]],\"decisionToState\":[0,16,21],\"lexerActions\":[{\"actionType\":6,\"a\":0,\"b\":0}]}"
}

View File

@ -1,21 +0,0 @@
// Generated from Hello.g4 by ANTLR 4.6
import Antlr4
/**
* This interface defines a complete listener for a parse tree produced by
* {@link HelloParser}.
*/
public protocol HelloListener: ParseTreeListener {
/**
* Enter a parse tree produced by {@link HelloParser#r}.
- Parameters:
- ctx: the parse tree
*/
func enterR(_ ctx: HelloParser.RContext)
/**
* Exit a parse tree produced by {@link HelloParser#r}.
- Parameters:
- ctx: the parse tree
*/
func exitR(_ ctx: HelloParser.RContext)
}

View File

@ -1,22 +0,0 @@
// Generated from Hello.g4 by ANTLR 4.6
import Antlr4
/**
* This interface defines a complete generic visitor for a parse tree produced
* by {@link HelloParser}.
*
* @param <T> The return type of the visit operation. Use {@link Void} for
* operations with no return type.
*/
open class HelloVisitor<T>: ParseTreeVisitor<T> {
/**
* Visit a parse tree produced by {@link HelloParser#r}.
- Parameters:
- ctx: the parse tree
- returns: the visitor result
*/
open func visitR(_ ctx: HelloParser.RContext) -> T{
fatalError(#function + " must be overridden")
}
}

View File

@ -1,18 +0,0 @@
import Antlr4
public class HelloWalker: HelloBaseListener {
public override func enterR(_ ctx: HelloParser.RContext) {
print("enterR: \(ctx.IDText())")
}
public override func exitR(_ ctx: HelloParser.RContext) {
print("exitR: \(ctx.IDText())")
}
}
fileprivate extension HelloParser.RContext {
fileprivate func IDText() -> String {
return ID()?.getText() ?? ""
}
}

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos'>
<timeline fileName='timeline.xctimeline'/>
</playground>

View File

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:">
</FileRef>
</Workspace>

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0810"
version = "1.3">
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
@ -37,6 +37,10 @@
BlueprintName = "Antlr4Tests"
ReferencedContainer = "container:Antlr4.xcodeproj">
</BuildableReference>
<LocationScenarioReference
identifier = "com.apple.dt.IDEFoundation.CurrentLocationScenarioIdentifier"
referenceType = "1">
</LocationScenarioReference>
</TestableReference>
</Testables>
<MacroExpansion>

View File

@ -4,7 +4,4 @@
<FileRef
location = "container:Antlr4.xcodeproj">
</FileRef>
<FileRef
location = "container:Antlr4 playground.playground">
</FileRef>
</Workspace>

View File

@ -1,171 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/** How to emit recognition errors. */
public protocol ANTLRErrorListener: class {
/**
* Upon syntax error, notify any interested parties. This is not how to
* recover from errors or compute error messages. {@link org.antlr.v4.runtime.ANTLRErrorStrategy}
* specifies how to recover from syntax errors and how to compute error
* messages. This listener's job is simply to emit a computed message,
* though it has enough information to create its own message in many cases.
*
* <p>The {@link org.antlr.v4.runtime.RecognitionException} is non-null for all syntax errors except
* when we discover mismatched token errors that we can recover from
* in-line, without returning from the surrounding rule (via the single
* token insertion and deletion mechanism).</p>
*
* @param recognizer
* What parser got the error. From this
* object, you can access the context as well
* as the input stream.
* @param offendingSymbol
* The offending token in the input token
* stream, unless recognizer is a lexer (then it's null). If
* no viable alternative error, {@code e} has token at which we
* started production for the decision.
* @param line
* The line number in the input where the error occurred.
* @param charPositionInLine
* The character position within that line where the error occurred.
* @param msg
* The message to emit.
* @param e
* The exception generated by the parser that led to
* the reporting of an error. It is null in the case where
* the parser was able to recover in line without exiting the
* surrounding rule.
*/
func syntaxError<T:ATNSimulator>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int,
_ charPositionInLine: Int,
_ msg: String,
_ e: AnyObject?// RecognitionException?
)
/**
* This method is called by the parser when a full-context prediction
* results in an ambiguity.
*
* <p>Each full-context prediction which does not result in a syntax error
* will call either {@link #reportContextSensitivity} or
* {@link #reportAmbiguity}.</p>
*
* <p>When {@code ambigAlts} is not null, it contains the set of potentially
* viable alternatives identified by the prediction algorithm. When
* {@code ambigAlts} is null, use {@link org.antlr.v4.runtime.atn.ATNConfigSet#getAlts} to obtain the
* represented alternatives from the {@code configs} argument.</p>
*
* <p>When {@code exact} is {@code true}, <em>all</em> of the potentially
* viable alternatives are truly viable, i.e. this is reporting an exact
* ambiguity. When {@code exact} is {@code false}, <em>at least two</em> of
* the potentially viable alternatives are viable for the current input, but
* the prediction algorithm terminated as soon as it determined that at
* least the <em>minimum</em> potentially viable alternative is truly
* viable.</p>
*
* <p>When the {@link org.antlr.v4.runtime.atn.PredictionMode#LL_EXACT_AMBIG_DETECTION} prediction
* mode is used, the parser is required to identify exact ambiguities so
* {@code exact} will always be {@code true}.</p>
*
* <p>This method is not used by lexers.</p>
*
* @param recognizer the parser instance
* @param dfa the DFA for the current decision
* @param startIndex the input index where the decision started
* @param stopIndex the input input where the ambiguity was identified
* @param exact {@code true} if the ambiguity is exactly known, otherwise
* {@code false}. This is always {@code true} when
* {@link org.antlr.v4.runtime.atn.PredictionMode#LL_EXACT_AMBIG_DETECTION} is used.
* @param ambigAlts the potentially ambiguous alternatives, or {@code null}
* to indicate that the potentially ambiguous alternatives are the complete
* set of represented alternatives in {@code configs}
* @param configs the ATN configuration set where the ambiguity was
* identified
*/
func reportAmbiguity(_ recognizer: Parser,
_ dfa: DFA,
_ startIndex: Int,
_ stopIndex: Int,
_ exact: Bool,
_ ambigAlts: BitSet,
_ configs: ATNConfigSet) throws
/**
* This method is called when an SLL conflict occurs and the parser is about
* to use the full context information to make an LL decision.
*
* <p>If one or more configurations in {@code configs} contains a semantic
* predicate, the predicates are evaluated before this method is called. The
* subset of alternatives which are still viable after predicates are
* evaluated is reported in {@code conflictingAlts}.</p>
*
* <p>This method is not used by lexers.</p>
*
* @param recognizer the parser instance
* @param dfa the DFA for the current decision
* @param startIndex the input index where the decision started
* @param stopIndex the input index where the SLL conflict occurred
* @param conflictingAlts The specific conflicting alternatives. If this is
* {@code null}, the conflicting alternatives are all alternatives
* represented in {@code configs}. At the moment, conflictingAlts is non-null
* (for the reference implementation, but Sam's optimized version can see this
* as null).
* @param configs the ATN configuration set where the SLL conflict was
* detected
*/
func reportAttemptingFullContext(_ recognizer: Parser,
_ dfa: DFA,
_ startIndex: Int,
_ stopIndex: Int,
_ conflictingAlts: BitSet?,
_ configs: ATNConfigSet) throws
/**
* This method is called by the parser when a full-context prediction has a
* unique result.
*
* <p>Each full-context prediction which does not result in a syntax error
* will call either {@link #reportContextSensitivity} or
* {@link #reportAmbiguity}.</p>
*
* <p>For prediction implementations that only evaluate full-context
* predictions when an SLL conflict is found (including the default
* {@link org.antlr.v4.runtime.atn.ParserATNSimulator} implementation), this method reports cases
* where SLL conflicts were resolved to unique full-context predictions,
* i.e. the decision was context-sensitive. This report does not necessarily
* indicate a problem, and it may appear even in completely unambiguous
* grammars.</p>
*
* <p>{@code configs} may have more than one represented alternative if the
* full-context prediction algorithm does not evaluate predicates before
* beginning the full-context prediction. In all cases, the final prediction
* is passed as the {@code prediction} argument.</p>
*
* <p>Note that the definition of "context sensitivity" in this method
* differs from the concept in {@link org.antlr.v4.runtime.atn.DecisionInfo#contextSensitivities}.
* This method reports all instances where an SLL conflict occurred but LL
* parsing produced a unique result, whether or not that unique result
* matches the minimum alternative in the SLL conflicting set.</p>
*
* <p>This method is not used by lexers.</p>
*
* @param recognizer the parser instance
* @param dfa the DFA for the current decision
* @param startIndex the input index where the decision started
* @param stopIndex the input index where the context sensitivity was
* finally determined
* @param prediction the unambiguous result of the full-context prediction
* @param configs the ATN configuration set where the unambiguous prediction
* was determined
*/
func reportContextSensitivity(_ recognizer: Parser,
_ dfa: DFA,
_ startIndex: Int,
_ stopIndex: Int,
_ prediction: Int,
_ configs: ATNConfigSet) throws
}

View File

@ -1,115 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* The interface for defining strategies to deal with syntax errors encountered
* during a parse by ANTLR-generated parsers. We distinguish between three
* different kinds of errors:
*
* <ul>
* <li>The parser could not figure out which path to take in the ATN (none of
* the available alternatives could possibly match)</li>
* <li>The current input does not match what we were looking for</li>
* <li>A predicate evaluated to false</li>
* </ul>
*
* Implementations of this interface report syntax errors by calling
* {@link org.antlr.v4.runtime.Parser#notifyErrorListeners}.
*
* <p>TODO: what to do about lexers</p>
*/
public protocol ANTLRErrorStrategy {
/**
* Reset the error handler state for the specified {@code recognizer}.
* @param recognizer the parser instance
*/
func reset(_ recognizer: Parser)
/**
* This method is called when an unexpected symbol is encountered during an
* inline match operation, such as {@link org.antlr.v4.runtime.Parser#match}. If the error
* strategy successfully recovers from the match failure, this method
* returns the {@link org.antlr.v4.runtime.Token} instance which should be treated as the
* successful result of the match.
*
* <p>This method handles the consumption of any tokens - the caller should
* <b>not</b> call {@link org.antlr.v4.runtime.Parser#consume} after a successful recovery.</p>
*
* <p>Note that the calling code will not report an error if this method
* returns successfully. The error strategy implementation is responsible
* for calling {@link org.antlr.v4.runtime.Parser#notifyErrorListeners} as appropriate.</p>
*
* @param recognizer the parser instance
* @throws org.antlr.v4.runtime.RecognitionException if the error strategy was not able to
* recover from the unexpected input symbol
*/
@discardableResult
func recoverInline(_ recognizer: Parser) throws -> Token // RecognitionException;
/**
* This method is called to recover from exception {@code e}. This method is
* called after {@link #reportError} by the default exception handler
* generated for a rule method.
*
* @see #reportError
*
* @param recognizer the parser instance
* @param e the recognition exception to recover from
* @throws org.antlr.v4.runtime.RecognitionException if the error strategy could not recover from
* the recognition exception
*/
func recover(_ recognizer: Parser, _ e: AnyObject) throws // RecognitionException;
/**
* This method provides the error handler with an opportunity to handle
* syntactic or semantic errors in the input stream before they result in a
* {@link org.antlr.v4.runtime.RecognitionException}.
*
* <p>The generated code currently contains calls to {@link #sync} after
* entering the decision state of a closure block ({@code (...)*} or
* {@code (...)+}).</p>
*
* <p>For an implementation based on Jim Idle's "magic sync" mechanism, see
* {@link org.antlr.v4.runtime.DefaultErrorStrategy#sync}.</p>
*
* @see org.antlr.v4.runtime.DefaultErrorStrategy#sync
*
* @param recognizer the parser instance
* @throws org.antlr.v4.runtime.RecognitionException if an error is detected by the error
* strategy but cannot be automatically recovered at the current state in
* the parsing process
*/
func sync(_ recognizer: Parser) throws // RecognitionException;
/**
* Tests whether or not {@code recognizer} is in the process of recovering
* from an error. In error recovery mode, {@link org.antlr.v4.runtime.Parser#consume} adds
* symbols to the parse tree by calling
* {@link org.antlr.v4.runtime.ParserRuleContext#addErrorNode(org.antlr.v4.runtime.Token)} instead of
* {@link org.antlr.v4.runtime.ParserRuleContext#addChild(org.antlr.v4.runtime.Token)}.
*
* @param recognizer the parser instance
* @return {@code true} if the parser is currently recovering from a parse
* error, otherwise {@code false}
*/
func inErrorRecoveryMode(_ recognizer: Parser) -> Bool
/**
* This method is called by when the parser successfully matches an input
* symbol.
*
* @param recognizer the parser instance
*/
func reportMatch(_ recognizer: Parser)
/**
* Report any kind of {@link org.antlr.v4.runtime.RecognitionException}. This method is called by
* the default exception handler generated for a rule method.
*
* @param recognizer the parser instance
* @param e the recognition exception to report
*/
func reportError(_ recognizer: Parser, _ e: AnyObject)
}

View File

@ -1,224 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* Vacuum all input from a {@link java.io.Reader}/{@link java.io.InputStream} and then treat it
* like a {@code char[]} buffer. Can also pass in a {@link String} or
* {@code char[]} to use.
*
* <p>If you need encoding, pass in stream/reader with correct encoding.</p>
*/
public class ANTLRInputStream: CharStream {
public static let READ_BUFFER_SIZE: Int = 1024
public static let INITIAL_BUFFER_SIZE: Int = 1024
/** The data being scanned */
internal var data: [Character]
/** How many characters are actually in the buffer */
internal var n: Int
/** 0..n-1 index into string of next char */
internal var p: Int = 0
/** What is name or source of this char stream? */
public var name: String?
public init() {
n = 0
data = [Character]()
}
/** Copy data in string to a local char array */
public init(_ input: String) {
self.data = Array(input.characters) // input.toCharArray();
self.n = input.length
}
/** This is the preferred constructor for strings as no data is copied */
public init(_ data: [Character], _ numberOfActualCharsInArray: Int) {
self.data = data
self.n = numberOfActualCharsInArray
}
/*
public convenience init(_ r : Reader) throws; IOException {
self.init(r, INITIAL_BUFFER_SIZE, READ_BUFFER_SIZE);
}
public convenience init(_ r : Reader, _ initialSize : Int) throws; IOException {
self.init(r, initialSize, READ_BUFFER_SIZE);
}
public init(_ r : Reader, _ initialSize : Int, _ readChunkSize : Int) throws; IOException {
load(r, initialSize, readChunkSize);
}
public convenience init(_ input : InputStream) throws; IOException {
self.init(InputStreamReader(input), INITIAL_BUFFER_SIZE);
}
public convenience init(_ input : InputStream, _ initialSize : Int) throws; IOException {
self.init(InputStreamReader(input), initialSize);
}
public convenience init(_ input : InputStream, _ initialSize : Int, _ readChunkSize : Int) throws; IOException {
self.init(InputStreamReader(input), initialSize, readChunkSize);
}
public func load(r : Reader, _ size : Int, _ readChunkSize : Int)
throws; IOException
{
if ( r==nil ) {
return;
}
if ( size<=0 ) {
size = INITIAL_BUFFER_SIZE;
}
if ( readChunkSize<=0 ) {
readChunkSize = READ_BUFFER_SIZE;
}
// print("load "+size+" in chunks of "+readChunkSize);
try {
// alloc initial buffer size.
data = new char[size];
// read all the data in chunks of readChunkSize
var numRead : Int=0;
var p : Int = 0;
do {
if ( p+readChunkSize > data.length ) { // overflow?
// print("### overflow p="+p+", data.length="+data.length);
data = Arrays.copyOf(data, data.length * 2);
}
numRead = r.read(data, p, readChunkSize);
// print("read "+numRead+" chars; p was "+p+" is now "+(p+numRead));
p += numRead;
} while (numRead!=-1); // while not EOF
// set the actual size of the data available;
// EOF subtracted one above in p+=numRead; add one back
n = p+1;
//print("n="+n);
}
finally {
r.close();
}
}
*/
/** Reset the stream so that it's in the same state it was
* when the object was created *except* the data array is not
* touched.
*/
public func reset() {
p = 0
}
public func consume() throws {
if p >= n {
assert(LA(1) == ANTLRInputStream.EOF, "Expected: LA(1)==IntStream.EOF")
throw ANTLRError.illegalState(msg: "annot consume EOF")
}
// print("prev p="+p+", c="+(char)data[p]);
if p < n {
p += 1
//print("p moves to "+p+" (c='"+(char)data[p]+"')");
}
}
public func LA(_ i: Int) -> Int {
var i = i
if i == 0 {
return 0 // undefined
}
if i < 0 {
i += 1 // e.g., translate LA(-1) to use offset i=0; then data[p+0-1]
if (p + i - 1) < 0 {
return ANTLRInputStream.EOF// invalid; no char before first char
}
}
if (p + i - 1) >= n {
//print("char LA("+i+")=EOF; p="+p);
return ANTLRInputStream.EOF
}
//print("char LA("+i+")="+(char)data[p+i-1]+"; p="+p);
//print("LA("+i+"); p="+p+" n="+n+" data.length="+data.length);
return data[p + i - 1].unicodeValue
}
public func LT(_ i: Int) -> Int {
return LA(i)
}
/** Return the current input symbol index 0..n where n indicates the
* last symbol has been read. The index is the index of char to
* be returned from LA(1).
*/
public func index() -> Int {
return p
}
public func size() -> Int {
return n
}
/** mark/release do nothing; we have entire buffer */
public func mark() -> Int {
return -1
}
public func release(_ marker: Int) {
}
/** consume() ahead until p==index; can't just set p=index as we must
* update line and charPositionInLine. If we seek backwards, just set p
*/
public func seek(_ index: Int) throws {
var index = index
if index <= p {
p = index // just jump; don't update stream state (line, ...)
return
}
// seek forward, consume until p hits index or n (whichever comes first)
index = min(index, n)
while p < index {
try consume()
}
}
public func getText(_ interval: Interval) -> String {
let start: Int = interval.a
var stop: Int = interval.b
if stop >= n {
stop = n - 1
}
let count = stop - start + 1;
if start >= n {
return ""
}
return String(data[start ..< (start + count)])
}
public func getSourceName() -> String {
guard let name = name , !name.isEmpty else {
return ANTLRInputStream.UNKNOWN_SOURCE_NAME
}
return name
}
public func toString() -> String {
return String(data)
}
}

View File

@ -1,76 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This implementation of {@link org.antlr.v4.runtime.ANTLRErrorStrategy} responds to syntax errors
* by immediately canceling the parse operation with a
* {@link org.antlr.v4.runtime.misc.ParseCancellationException}. The implementation ensures that the
* {@link org.antlr.v4.runtime.ParserRuleContext#exception} field is set for all parse tree nodes
* that were not completed prior to encountering the error.
*
* <p>
* This error strategy is useful in the following scenarios.</p>
*
* <ul>
* <li><strong>Two-stage parsing:</strong> This error strategy allows the first
* stage of two-stage parsing to immediately terminate if an error is
* encountered, and immediately fall back to the second stage. In addition to
* avoiding wasted work by attempting to recover from errors here, the empty
* implementation of {@link org.antlr.v4.runtime.BailErrorStrategy#sync} improves the performance of
* the first stage.</li>
* <li><strong>Silent validation:</strong> When syntax errors are not being
* reported or logged, and the parse result is simply ignored if errors occur,
* the {@link org.antlr.v4.runtime.BailErrorStrategy} avoids wasting work on recovering from errors
* when the result will be ignored either way.</li>
* </ul>
*
* <p>
* {@code myparser.setErrorHandler(new BailErrorStrategy());}</p>
*
* @see org.antlr.v4.runtime.Parser#setErrorHandler(org.antlr.v4.runtime.ANTLRErrorStrategy)
*/
public class BailErrorStrategy: DefaultErrorStrategy {
public override init(){}
/** Instead of recovering from exception {@code e}, re-throw it wrapped
* in a {@link org.antlr.v4.runtime.misc.ParseCancellationException} so it is not caught by the
* rule function catches. Use {@link Exception#getCause()} to get the
* original {@link org.antlr.v4.runtime.RecognitionException}.
*/
override
public func recover(_ recognizer: Parser, _ e: AnyObject) throws {
var context: ParserRuleContext? = recognizer.getContext()
while let contextWrap = context{
contextWrap.exception = e
context = (contextWrap.getParent() as? ParserRuleContext)
}
throw ANTLRException.recognition(e: e)
}
/** Make sure we don't attempt to recover inline; if the parser
* successfully recovers, it won't throw an exception.
*/
override
public func recoverInline(_ recognizer: Parser) throws -> Token {
let e: InputMismatchException = try InputMismatchException(recognizer)
var context: ParserRuleContext? = recognizer.getContext()
while let contextWrap = context {
contextWrap.exception = e
context = (contextWrap.getParent() as? ParserRuleContext)
}
throw ANTLRException.recognition(e: e)
}
/** Make sure we don't attempt to recover from problems in subrules. */
override
public func sync(_ recognizer: Parser) {
}
}

View File

@ -1,27 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/** A source of characters for an ANTLR lexer. */
public protocol CharStream: IntStream {
/**
* This method returns the text for a range of characters within this input
* stream. This method is guaranteed to not throw an exception if the
* specified {@code interval} lies entirely within a marked range. For more
* information about marked ranges, see {@link org.antlr.v4.runtime.IntStream#mark}.
*
* @param interval an interval within the stream
* @return the text of the specified interval
*
* @throws NullPointerException if {@code interval} is {@code null}
* @throws IllegalArgumentException if {@code interval.a < 0}, or if
* {@code interval.b < interval.a - 1}, or if {@code interval.b} lies at or
* past the end of the stream
* @throws UnsupportedOperationException if the stream does not support
* getting the text of the specified interval
*/
func getText(_ interval: Interval) -> String
}

View File

@ -1,88 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This default implementation of {@link org.antlr.v4.runtime.TokenFactory} creates
* {@link org.antlr.v4.runtime.CommonToken} objects.
*/
public class CommonTokenFactory: TokenFactory {
/**
* The default {@link org.antlr.v4.runtime.CommonTokenFactory} instance.
*
* <p>
* This token factory does not explicitly copy token text when constructing
* tokens.</p>
*/
public static let DEFAULT: TokenFactory = CommonTokenFactory()
/**
* Indicates whether {@link org.antlr.v4.runtime.CommonToken#setText} should be called after
* constructing tokens to explicitly set the text. This is useful for cases
* where the input stream might not be able to provide arbitrary substrings
* of text from the input after the lexer creates a token (e.g. the
* implementation of {@link org.antlr.v4.runtime.CharStream#getText} in
* {@link org.antlr.v4.runtime.UnbufferedCharStream} throws an
* {@link UnsupportedOperationException}). Explicitly setting the token text
* allows {@link org.antlr.v4.runtime.Token#getText} to be called at any time regardless of the
* input stream implementation.
*
* <p>
* The default value is {@code false} to avoid the performance and memory
* overhead of copying text for every token unless explicitly requested.</p>
*/
internal final var copyText: Bool
/**
* Constructs a {@link org.antlr.v4.runtime.CommonTokenFactory} with the specified value for
* {@link #copyText}.
*
* <p>
* When {@code copyText} is {@code false}, the {@link #DEFAULT} instance
* should be used instead of constructing a new instance.</p>
*
* @param copyText The value for {@link #copyText}.
*/
public init(_ copyText: Bool) {
self.copyText = copyText
}
/**
* Constructs a {@link org.antlr.v4.runtime.CommonTokenFactory} with {@link #copyText} set to
* {@code false}.
*
* <p>
* The {@link #DEFAULT} instance should be used instead of calling this
* directly.</p>
*/
public convenience init() {
self.init(false)
}
public func create(_ source: (TokenSource?, CharStream?), _ type: Int, _ text: String?,
_ channel: Int, _ start: Int, _ stop: Int,
_ line: Int, _ charPositionInLine: Int) -> Token {
let t: CommonToken = CommonToken(source, type, channel, start, stop)
t.setLine(line)
t.setCharPositionInLine(charPositionInLine)
if text != nil {
t.setText(text!)
} else {
if let cStream = source.1 , copyText {
t.setText(cStream.getText(Interval.of(start, stop)))
}
}
return t
}
public func create(_ type: Int, _ text: String) -> Token {
return CommonToken(type, text)
}
}

View File

@ -1,133 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This class extends {@link org.antlr.v4.runtime.BufferedTokenStream} with functionality to filter
* token streams to tokens on a particular channel (tokens where
* {@link org.antlr.v4.runtime.Token#getChannel} returns a particular value).
*
* <p>
* This token stream provides access to all tokens by index or when calling
* methods like {@link #getText}. The channel filtering is only used for code
* accessing tokens via the lookahead methods {@link #LA}, {@link #LT}, and
* {@link #LB}.</p>
*
* <p>
* By default, tokens are placed on the default channel
* ({@link org.antlr.v4.runtime.Token#DEFAULT_CHANNEL}), but may be reassigned by using the
* {@code ->channel(HIDDEN)} lexer command, or by using an embedded action to
* call {@link org.antlr.v4.runtime.Lexer#setChannel}.
* </p>
*
* <p>
* Note: lexer rules which use the {@code ->skip} lexer command or call
* {@link org.antlr.v4.runtime.Lexer#skip} do not produce tokens at all, so input text matched by
* such a rule will not be available as part of the token stream, regardless of
* channel.</p>
*/
public class CommonTokenStream: BufferedTokenStream {
/**
* Specifies the channel to use for filtering tokens.
*
* <p>
* The default value is {@link org.antlr.v4.runtime.Token#DEFAULT_CHANNEL}, which matches the
* default channel assigned to tokens created by the lexer.</p>
*/
internal var channel: Int = CommonToken.DEFAULT_CHANNEL
/**
* Constructs a new {@link org.antlr.v4.runtime.CommonTokenStream} using the specified token
* source and the default token channel ({@link org.antlr.v4.runtime.Token#DEFAULT_CHANNEL}).
*
* @param tokenSource The token source.
*/
public override init(_ tokenSource: TokenSource) {
super.init(tokenSource)
}
/**
* Constructs a new {@link org.antlr.v4.runtime.CommonTokenStream} using the specified token
* source and filtering tokens to the specified channel. Only tokens whose
* {@link org.antlr.v4.runtime.Token#getChannel} matches {@code channel} or have the
* {@link org.antlr.v4.runtime.Token#getType} equal to {@link org.antlr.v4.runtime.Token#EOF} will be returned by the
* token stream lookahead methods.
*
* @param tokenSource The token source.
* @param channel The channel to use for filtering tokens.
*/
public convenience init(_ tokenSource: TokenSource, _ channel: Int) {
self.init(tokenSource)
self.channel = channel
}
override
internal func adjustSeekIndex(_ i: Int) throws -> Int {
return try nextTokenOnChannel(i, channel)
}
override
internal func LB(_ k: Int) throws -> Token? {
if k == 0 || (p - k) < 0 {
return nil
}
var i: Int = p
var n: Int = 1
// find k good tokens looking backwards
while n <= k {
// skip off-channel tokens
try i = previousTokenOnChannel(i - 1, channel)
n += 1
}
if i < 0 {
return nil
}
return tokens[i]
}
override
public func LT(_ k: Int) throws -> Token? {
//System.out.println("enter LT("+k+")");
try lazyInit()
if k == 0 {
return nil
}
if k < 0 {
return try LB(-k)
}
var i: Int = p
var n: Int = 1 // we know tokens[p] is a good one
// find k good tokens
while n < k {
// skip off-channel tokens, but make sure to not look past EOF
if try sync(i + 1) {
i = try nextTokenOnChannel(i + 1, channel)
}
n += 1
}
// if ( i>range ) range = i;
return tokens[i]
}
/** Count EOF just once. */
public func getNumberOfOnChannelTokens() throws -> Int {
var n: Int = 0
try fill()
let length = tokens.count
for i in 0..<length {
let t: Token = tokens[i]
if t.getChannel() == channel {
n += 1
}
if t.getType() == CommonToken.EOF {
break
}
}
return n
}
}

View File

@ -1,43 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
*
* @author Sam Harwell
*/
public class ConsoleErrorListener: BaseErrorListener {
/**
* Provides a default instance of {@link org.antlr.v4.runtime.ConsoleErrorListener}.
*/
public static let INSTANCE: ConsoleErrorListener = ConsoleErrorListener()
/**
* {@inheritDoc}
*
* <p>
* This implementation prints messages to {@link System#err} containing the
* values of {@code line}, {@code charPositionInLine}, and {@code msg} using
* the following format.</p>
*
* <pre>
* line <em>line</em>:<em>charPositionInLine</em> <em>msg</em>
* </pre>
*/
override
public func syntaxError<T:ATNSimulator>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int,
_ charPositionInLine: Int,
_ msg: String,
_ e: AnyObject?
) {
if Parser.ConsoleError {
errPrint("line \(line):\(charPositionInLine) \(msg)")
}
}
}

View File

@ -1,745 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This is the default implementation of {@link org.antlr.v4.runtime.ANTLRErrorStrategy} used for
* error reporting and recovery in ANTLR parsers.
*/
import Foundation
public class DefaultErrorStrategy: ANTLRErrorStrategy {
/**
* Indicates whether the error strategy is currently "recovering from an
* error". This is used to suppress reporting multiple error messages while
* attempting to recover from a detected syntax error.
*
* @see #inErrorRecoveryMode
*/
internal var errorRecoveryMode: Bool = false
/** The index into the input stream where the last error occurred.
* This is used to prevent infinite loops where an error is found
* but no token is consumed during recovery...another error is found,
* ad nauseum. This is a failsafe mechanism to guarantee that at least
* one token/tree node is consumed for two errors.
*/
internal var lastErrorIndex: Int = -1
internal var lastErrorStates: IntervalSet?
/**
* {@inheritDoc}
*
* <p>The default implementation simply calls {@link #endErrorCondition} to
* ensure that the handler is not in error recovery mode.</p>
*/
public func reset(_ recognizer: Parser) {
endErrorCondition(recognizer)
}
/**
* This method is called to enter error recovery mode when a recognition
* exception is reported.
*
* @param recognizer the parser instance
*/
internal func beginErrorCondition(_ recognizer: Parser) {
errorRecoveryMode = true
}
/**
* {@inheritDoc}
*/
public func inErrorRecoveryMode(_ recognizer: Parser) -> Bool {
return errorRecoveryMode
}
/**
* This method is called to leave error recovery mode after recovering from
* a recognition exception.
*
* @param recognizer
*/
internal func endErrorCondition(_ recognizer: Parser) {
errorRecoveryMode = false
lastErrorStates = nil
lastErrorIndex = -1
}
/**
* {@inheritDoc}
*
* <p>The default implementation simply calls {@link #endErrorCondition}.</p>
*/
public func reportMatch(_ recognizer: Parser) {
endErrorCondition(recognizer)
}
/**
* {@inheritDoc}
*
* <p>The default implementation returns immediately if the handler is already
* in error recovery mode. Otherwise, it calls {@link #beginErrorCondition}
* and dispatches the reporting task based on the runtime type of {@code e}
* according to the following table.</p>
*
* <ul>
* <li>{@link org.antlr.v4.runtime.NoViableAltException}: Dispatches the call to
* {@link #reportNoViableAlternative}</li>
* <li>{@link org.antlr.v4.runtime.InputMismatchException}: Dispatches the call to
* {@link #reportInputMismatch}</li>
* <li>{@link org.antlr.v4.runtime.FailedPredicateException}: Dispatches the call to
* {@link #reportFailedPredicate}</li>
* <li>All other types: calls {@link org.antlr.v4.runtime.Parser#notifyErrorListeners} to report
* the exception</li>
* </ul>
*/
public func reportError(_ recognizer: Parser,
_ e: AnyObject) {
// if we've already reported an error and have not matched a token
// yet successfully, don't report any errors.
if inErrorRecoveryMode(recognizer) {
return // don't report spurious errors
}
beginErrorCondition(recognizer)
//TODO: exception handler
if (e is NoViableAltException) {
try! reportNoViableAlternative(recognizer, e as! NoViableAltException);
} else {
if (e is InputMismatchException) {
reportInputMismatch(recognizer, e as! InputMismatchException);
} else {
if (e is FailedPredicateException) {
reportFailedPredicate(recognizer, e as! FailedPredicateException);
} else {
errPrint("unknown recognition error type: " + String(describing: type(of: e)));
let re = (e as! RecognitionException<ParserATNSimulator>)
recognizer.notifyErrorListeners(re.getOffendingToken(), re.message ?? "", e);
}
}
}
}
/**
* {@inheritDoc}
*
* <p>The default implementation resynchronizes the parser by consuming tokens
* until we find one in the resynchronization set--loosely the set of tokens
* that can follow the current rule.</p>
*/
public func recover(_ recognizer: Parser, _ e: AnyObject) throws {
// print("recover in "+recognizer.getRuleInvocationStack()+
// " index="+getTokenStream(recognizer).index()+
// ", lastErrorIndex="+
// lastErrorIndex+
// ", states="+lastErrorStates);
if let lastErrorStates = lastErrorStates ,
lastErrorIndex == getTokenStream(recognizer).index() &&
lastErrorStates.contains(recognizer.getState()) {
// uh oh, another error at same token index and previously-visited
// state in ATN; must be a case where LT(1) is in the recovery
// token set so nothing got consumed. Consume a single token
// at least to prevent an infinite loop; this is a failsafe.
// errPrint("seen error condition before index="+
// lastErrorIndex+", states="+lastErrorStates);
// errPrint("FAILSAFE consumes "+recognizer.getTokenNames()[getTokenStream(recognizer).LA(1)]);
try recognizer.consume()
}
lastErrorIndex = getTokenStream(recognizer).index()
if lastErrorStates == nil {
lastErrorStates = try IntervalSet()
}
try lastErrorStates!.add(recognizer.getState())
let followSet: IntervalSet = try getErrorRecoverySet(recognizer)
try consumeUntil(recognizer, followSet)
}
/**
* The default implementation of {@link org.antlr.v4.runtime.ANTLRErrorStrategy#sync} makes sure
* that the current lookahead symbol is consistent with what were expecting
* at this point in the ATN. You can call this anytime but ANTLR only
* generates code to check before subrules/loops and each iteration.
*
* <p>Implements Jim Idle's magic sync mechanism in closures and optional
* subrules. E.g.,</p>
*
* <pre>
* a : sync ( stuff sync )* ;
* sync : {consume to what can follow sync} ;
* </pre>
*
* At the start of a sub rule upon error, {@link #sync} performs single
* token deletion, if possible. If it can't do that, it bails on the current
* rule and uses the default error recovery, which consumes until the
* resynchronization set of the current rule.
*
* <p>If the sub rule is optional ({@code (...)?}, {@code (...)*}, or block
* with an empty alternative), then the expected set includes what follows
* the subrule.</p>
*
* <p>During loop iteration, it consumes until it sees a token that can start a
* sub rule or what follows loop. Yes, that is pretty aggressive. We opt to
* stay in the loop as long as possible.</p>
*
* <p><strong>ORIGINS</strong></p>
*
* <p>Previous versions of ANTLR did a poor job of their recovery within loops.
* A single mismatch token or missing token would force the parser to bail
* out of the entire rules surrounding the loop. So, for rule</p>
*
* <pre>
* classDef : 'class' ID '{' member* '}'
* </pre>
*
* input with an extra token between members would force the parser to
* consume until it found the next class definition rather than the next
* member definition of the current class.
*
* <p>This functionality cost a little bit of effort because the parser has to
* compare token set at the start of the loop and at each iteration. If for
* some reason speed is suffering for you, you can turn off this
* functionality by simply overriding this method as a blank { }.</p>
*/
public func sync(_ recognizer: Parser) throws {
let s: ATNState = recognizer.getInterpreter().atn.states[recognizer.getState()]!
// errPrint("sync @ "+s.stateNumber+"="+s.getClass().getSimpleName());
// If already recovering, don't try to sync
if inErrorRecoveryMode(recognizer) {
return
}
let tokens: TokenStream = getTokenStream(recognizer)
let la: Int = try tokens.LA(1)
// try cheaper subset first; might get lucky. seems to shave a wee bit off
//let set : IntervalSet = recognizer.getATN().nextTokens(s)
if try recognizer.getATN().nextTokens(s).contains(CommonToken.EPSILON) {
return
}
if try recognizer.getATN().nextTokens(s).contains(la) {
return
}
switch s.getStateType() {
case ATNState.BLOCK_START: fallthrough
case ATNState.STAR_BLOCK_START: fallthrough
case ATNState.PLUS_BLOCK_START: fallthrough
case ATNState.STAR_LOOP_ENTRY:
// report error and recover if possible
if try singleTokenDeletion(recognizer) != nil {
return
}
throw try ANTLRException.recognition(e: InputMismatchException(recognizer))
case ATNState.PLUS_LOOP_BACK: fallthrough
case ATNState.STAR_LOOP_BACK:
// errPrint("at loop back: "+s.getClass().getSimpleName());
try reportUnwantedToken(recognizer)
let expecting: IntervalSet = try recognizer.getExpectedTokens()
let whatFollowsLoopIterationOrRule: IntervalSet =
try expecting.or(try getErrorRecoverySet(recognizer)) as! IntervalSet
try consumeUntil(recognizer, whatFollowsLoopIterationOrRule)
break
default:
// do nothing if we can't identify the exact kind of ATN state
break
}
}
/**
* This is called by {@link #reportError} when the exception is a
* {@link org.antlr.v4.runtime.NoViableAltException}.
*
* @see #reportError
*
* @param recognizer the parser instance
* @param e the recognition exception
*/
internal func reportNoViableAlternative(_ recognizer: Parser,
_ e: NoViableAltException) throws {
let tokens: TokenStream? = getTokenStream(recognizer)
var input: String
if let tokens = tokens {
if e.getStartToken().getType() == CommonToken.EOF {
input = "<EOF>"
} else {
input = try tokens.getText(e.getStartToken(), e.getOffendingToken())
}
} else {
input = "<unknown input>"
}
let msg: String = "no viable alternative at input " + escapeWSAndQuote(input)
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e)
}
/**
* This is called by {@link #reportError} when the exception is an
* {@link org.antlr.v4.runtime.InputMismatchException}.
*
* @see #reportError
*
* @param recognizer the parser instance
* @param e the recognition exception
*/
internal func reportInputMismatch(_ recognizer: Parser,
_ e: InputMismatchException) {
let msg: String = "mismatched input " + getTokenErrorDisplay(e.getOffendingToken()) +
" expecting " + e.getExpectedTokens()!.toString(recognizer.getVocabulary())
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e)
}
/**
* This is called by {@link #reportError} when the exception is a
* {@link org.antlr.v4.runtime.FailedPredicateException}.
*
* @see #reportError
*
* @param recognizer the parser instance
* @param e the recognition exception
*/
internal func reportFailedPredicate(_ recognizer: Parser,
_ e: FailedPredicateException) {
let ruleName: String = recognizer.getRuleNames()[recognizer._ctx!.getRuleIndex()]
let msg: String = "rule " + ruleName + " " + e.message! // e.getMessage()
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e)
}
/**
* This method is called to report a syntax error which requires the removal
* of a token from the input stream. At the time this method is called, the
* erroneous symbol is current {@code LT(1)} symbol and has not yet been
* removed from the input stream. When this method returns,
* {@code recognizer} is in error recovery mode.
*
* <p>This method is called when {@link #singleTokenDeletion} identifies
* single-token deletion as a viable recovery strategy for a mismatched
* input error.</p>
*
* <p>The default implementation simply returns if the handler is already in
* error recovery mode. Otherwise, it calls {@link #beginErrorCondition} to
* enter error recovery mode, followed by calling
* {@link org.antlr.v4.runtime.Parser#notifyErrorListeners}.</p>
*
* @param recognizer the parser instance
*/
internal func reportUnwantedToken(_ recognizer: Parser) throws {
if inErrorRecoveryMode(recognizer) {
return
}
beginErrorCondition(recognizer)
let t: Token = try recognizer.getCurrentToken()
let tokenName: String = getTokenErrorDisplay(t)
let expecting: IntervalSet = try getExpectedTokens(recognizer)
let msg: String = "extraneous input " + tokenName + " expecting " +
expecting.toString(recognizer.getVocabulary())
recognizer.notifyErrorListeners(t, msg, nil)
}
/**
* This method is called to report a syntax error which requires the
* insertion of a missing token into the input stream. At the time this
* method is called, the missing token has not yet been inserted. When this
* method returns, {@code recognizer} is in error recovery mode.
*
* <p>This method is called when {@link #singleTokenInsertion} identifies
* single-token insertion as a viable recovery strategy for a mismatched
* input error.</p>
*
* <p>The default implementation simply returns if the handler is already in
* error recovery mode. Otherwise, it calls {@link #beginErrorCondition} to
* enter error recovery mode, followed by calling
* {@link org.antlr.v4.runtime.Parser#notifyErrorListeners}.</p>
*
* @param recognizer the parser instance
*/
internal func reportMissingToken(_ recognizer: Parser) throws {
if inErrorRecoveryMode(recognizer) {
return
}
beginErrorCondition(recognizer)
let t: Token = try recognizer.getCurrentToken()
let expecting: IntervalSet = try getExpectedTokens(recognizer)
let msg: String = "missing " + expecting.toString(recognizer.getVocabulary()) +
" at " + getTokenErrorDisplay(t)
recognizer.notifyErrorListeners(t, msg, nil)
}
/**
* {@inheritDoc}
*
* <p>The default implementation attempts to recover from the mismatched input
* by using single token insertion and deletion as described below. If the
* recovery attempt fails, this method throws an
* {@link org.antlr.v4.runtime.InputMismatchException}.</p>
*
* <p><strong>EXTRA TOKEN</strong> (single token deletion)</p>
*
* <p>{@code LA(1)} is not what we are looking for. If {@code LA(2)} has the
* right token, however, then assume {@code LA(1)} is some extra spurious
* token and delete it. Then consume and return the next token (which was
* the {@code LA(2)} token) as the successful result of the match operation.</p>
*
* <p>This recovery strategy is implemented by {@link #singleTokenDeletion}.</p>
*
* <p><strong>MISSING TOKEN</strong> (single token insertion)</p>
*
* <p>If current token (at {@code LA(1)}) is consistent with what could come
* after the expected {@code LA(1)} token, then assume the token is missing
* and use the parser's {@link org.antlr.v4.runtime.TokenFactory} to create it on the fly. The
* "insertion" is performed by returning the created token as the successful
* result of the match operation.</p>
*
* <p>This recovery strategy is implemented by {@link #singleTokenInsertion}.</p>
*
* <p><strong>EXAMPLE</strong></p>
*
* <p>For example, Input {@code i=(3;} is clearly missing the {@code ')'}. When
* the parser returns from the nested call to {@code expr}, it will have
* call chain:</p>
*
* <pre>
* stat &rarr; expr &rarr; atom
* </pre>
*
* and it will be trying to match the {@code ')'} at this point in the
* derivation:
*
* <pre>
* =&gt; ID '=' '(' INT ')' ('+' atom)* ';'
* ^
* </pre>
*
* The attempt to match {@code ')'} will fail when it sees {@code ';'} and
* call {@link #recoverInline}. To recover, it sees that {@code LA(1)==';'}
* is in the set of tokens that can follow the {@code ')'} token reference
* in rule {@code atom}. It can assume that you forgot the {@code ')'}.
*/
public func recoverInline(_ recognizer: Parser) throws -> Token {
// SINGLE TOKEN DELETION
let matchedSymbol: Token? = try singleTokenDeletion(recognizer)
if matchedSymbol != nil {
// we have deleted the extra token.
// now, move past ttype token as if all were ok
try recognizer.consume()
return matchedSymbol!
}
// SINGLE TOKEN INSERTION
if try singleTokenInsertion(recognizer) {
return try getMissingSymbol(recognizer)
}
throw try ANTLRException.recognition(e: InputMismatchException(recognizer))
// throw try ANTLRException.InputMismatch(e: InputMismatchException(recognizer) )
//RuntimeException("InputMismatchException")
// even that didn't work; must throw the exception
//throwException() /* throw InputMismatchException(recognizer); */
}
/**
* This method implements the single-token insertion inline error recovery
* strategy. It is called by {@link #recoverInline} if the single-token
* deletion strategy fails to recover from the mismatched input. If this
* method returns {@code true}, {@code recognizer} will be in error recovery
* mode.
*
* <p>This method determines whether or not single-token insertion is viable by
* checking if the {@code LA(1)} input symbol could be successfully matched
* if it were instead the {@code LA(2)} symbol. If this method returns
* {@code true}, the caller is responsible for creating and inserting a
* token with the correct type to produce this behavior.</p>
*
* @param recognizer the parser instance
* @return {@code true} if single-token insertion is a viable recovery
* strategy for the current mismatched input, otherwise {@code false}
*/
internal func singleTokenInsertion(_ recognizer: Parser) throws -> Bool {
let currentSymbolType: Int = try getTokenStream(recognizer).LA(1)
// if current token is consistent with what could come after current
// ATN state, then we know we're missing a token; error recovery
// is free to conjure up and insert the missing token
let currentState: ATNState = recognizer.getInterpreter().atn.states[recognizer.getState()]!
let next: ATNState = currentState.transition(0).target
let atn: ATN = recognizer.getInterpreter().atn
let expectingAtLL2: IntervalSet = try atn.nextTokens(next, recognizer._ctx)
// print("LT(2) set="+expectingAtLL2.toString(recognizer.getTokenNames()));
if expectingAtLL2.contains(currentSymbolType) {
try reportMissingToken(recognizer)
return true
}
return false
}
/**
* This method implements the single-token deletion inline error recovery
* strategy. It is called by {@link #recoverInline} to attempt to recover
* from mismatched input. If this method returns null, the parser and error
* handler state will not have changed. If this method returns non-null,
* {@code recognizer} will <em>not</em> be in error recovery mode since the
* returned token was a successful match.
*
* <p>If the single-token deletion is successful, this method calls
* {@link #reportUnwantedToken} to report the error, followed by
* {@link org.antlr.v4.runtime.Parser#consume} to actually "delete" the extraneous token. Then,
* before returning {@link #reportMatch} is called to signal a successful
* match.</p>
*
* @param recognizer the parser instance
* @return the successfully matched {@link org.antlr.v4.runtime.Token} instance if single-token
* deletion successfully recovers from the mismatched input, otherwise
* {@code null}
*/
internal func singleTokenDeletion(_ recognizer: Parser) throws -> Token? {
let nextTokenType: Int = try getTokenStream(recognizer).LA(2)
let expecting: IntervalSet = try getExpectedTokens(recognizer)
if expecting.contains(nextTokenType) {
try reportUnwantedToken(recognizer)
/*
errPrint("recoverFromMismatchedToken deleting "+
((TokenStream)getTokenStream(recognizer)).LT(1)+
" since "+((TokenStream)getTokenStream(recognizer)).LT(2)+
" is what we want");
*/
try recognizer.consume() // simply delete extra token
// we want to return the token we're actually matching
let matchedSymbol: Token = try recognizer.getCurrentToken()
reportMatch(recognizer) // we know current token is correct
return matchedSymbol
}
return nil
}
/** Conjure up a missing token during error recovery.
*
* The recognizer attempts to recover from single missing
* symbols. But, actions might refer to that missing symbol.
* For example, x=ID {f($x);}. The action clearly assumes
* that there has been an identifier matched previously and that
* $x points at that token. If that token is missing, but
* the next token in the stream is what we want we assume that
* this token is missing and we keep going. Because we
* have to return some token to replace the missing token,
* we have to conjure one up. This method gives the user control
* over the tokens returned for missing tokens. Mostly,
* you will want to create something special for identifier
* tokens. For literals such as '{' and ',', the default
* action in the parser or tree parser works. It simply creates
* a CommonToken of the appropriate type. The text will be the token.
* If you change what tokens must be created by the lexer,
* override this method to create the appropriate tokens.
*/
internal func getTokenStream(_ recognizer: Parser) -> TokenStream {
return recognizer.getInputStream() as! TokenStream
}
internal func getMissingSymbol(_ recognizer: Parser) throws -> Token {
let currentSymbol: Token = try recognizer.getCurrentToken()
let expecting: IntervalSet = try getExpectedTokens(recognizer)
let expectedTokenType: Int = expecting.getMinElement() // get any element
var tokenText: String
if expectedTokenType == CommonToken.EOF {
tokenText = "<missing EOF>"
} else {
tokenText = "<missing " + recognizer.getVocabulary().getDisplayName(expectedTokenType) + ">"
}
var current: Token = currentSymbol
let lookback: Token? = try getTokenStream(recognizer).LT(-1)
if current.getType() == CommonToken.EOF && lookback != nil {
current = lookback!
}
let token = recognizer.getTokenFactory().create((current.getTokenSource(), current.getTokenSource()!.getInputStream()), expectedTokenType, tokenText,
CommonToken.DEFAULT_CHANNEL,
-1, -1,
current.getLine(), current.getCharPositionInLine())
return token
}
internal func getExpectedTokens(_ recognizer: Parser) throws -> IntervalSet {
return try recognizer.getExpectedTokens()
}
/** 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.
*/
internal func getTokenErrorDisplay(_ t: Token?) -> String {
if t == nil {
return "<no token>"
}
var s: String? = getSymbolText(t!)
if s == nil {
if getSymbolType(t!) == CommonToken.EOF {
s = "<EOF>"
} else {
s = "<\(getSymbolType(t!))>"
}
}
return escapeWSAndQuote(s!)
}
internal func getSymbolText(_ symbol: Token) -> String {
return symbol.getText()!
}
internal func getSymbolType(_ symbol: Token) -> Int {
return symbol.getType()
}
internal func escapeWSAndQuote(_ s: String) -> String {
var s = s
s = s.replaceAll("\n", replacement: "\\n")
s = s.replaceAll("\r", replacement: "\\r")
s = s.replaceAll("\t", replacement: "\\t")
return "'" + s + "'"
}
/* Compute the error recovery set for the current rule. During
* rule invocation, the parser pushes the set of tokens that can
* follow that rule reference on the stack; this amounts to
* computing FIRST of what follows the rule reference in the
* enclosing rule. See LinearApproximator.FIRST().
* This local follow set only includes tokens
* from within the rule; i.e., the FIRST computation done by
* ANTLR stops at the end of a rule.
*
* EXAMPLE
*
* When you find a "no viable alt exception", the input is not
* consistent with any of the alternatives for rule r. The best
* thing to do is to consume tokens until you see something that
* can legally follow a call to r *or* any rule that called r.
* You don't want the exact set of viable next tokens because the
* input might just be missing a token--you might consume the
* rest of the input looking for one of the missing tokens.
*
* Consider grammar:
*
* a : '[' b ']'
* | '(' b ')'
* ;
* b : c '^' INT ;
* c : ID
* | INT
* ;
*
* At each rule invocation, the set of tokens that could follow
* that rule is pushed on a stack. Here are the various
* context-sensitive follow sets:
*
* FOLLOW(b1_in_a) = FIRST(']') = ']'
* FOLLOW(b2_in_a) = FIRST(')') = ')'
* FOLLOW(c_in_b) = FIRST('^') = '^'
*
* Upon erroneous input "[]", the call chain is
*
* a -> b -> c
*
* and, hence, the follow context stack is:
*
* depth follow set start of rule execution
* 0 <EOF> a (from main())
* 1 ']' b
* 2 '^' c
*
* Notice that ')' is not included, because b would have to have
* been called from a different context in rule a for ')' to be
* included.
*
* For error recovery, we cannot consider FOLLOW(c)
* (context-sensitive or otherwise). We need the combined set of
* all context-sensitive FOLLOW sets--the set of all tokens that
* could follow any reference in the call chain. We need to
* resync to one of those tokens. Note that FOLLOW(c)='^' and if
* we resync'd to that token, we'd consume until EOF. We need to
* sync to context-sensitive FOLLOWs for a, b, and c: {']','^'}.
* In this case, for input "[]", LA(1) is ']' and in the set, so we would
* not consume anything. After printing an error, rule c would
* return normally. Rule b would not find the required '^' though.
* At this point, it gets a mismatched token error and throws an
* exception (since LA(1) is not in the viable following token
* set). The rule exception handler tries to recover, but finds
* the same recovery set and doesn't consume anything. Rule b
* exits normally returning to rule a. Now it finds the ']' (and
* with the successful match exits errorRecovery mode).
*
* So, you can see that the parser walks up the call chain looking
* for the token that was a member of the recovery set.
*
* Errors are not generated in errorRecovery mode.
*
* ANTLR's error recovery mechanism is based upon original ideas:
*
* "Algorithms + Data Structures = Programs" by Niklaus Wirth
*
* and
*
* "A note on error recovery in recursive descent parsers":
* http://portal.acm.org/citation.cfm?id=947902.947905
*
* Later, Josef Grosch had some good ideas:
*
* "Efficient and Comfortable Error Recovery in Recursive Descent
* Parsers":
* ftp://www.cocolab.com/products/cocktail/doca4.ps/ell.ps.zip
*
* Like Grosch I implement context-sensitive FOLLOW sets that are combined
* at run-time upon error to avoid overhead during parsing.
*/
internal func getErrorRecoverySet(_ recognizer: Parser) throws -> IntervalSet {
let atn: ATN = recognizer.getInterpreter().atn
var ctx: RuleContext? = recognizer._ctx
let recoverSet: IntervalSet = try IntervalSet()
while let ctxWrap = ctx , ctxWrap.invokingState >= 0 {
// compute what follows who invoked us
let invokingState: ATNState = atn.states[ctxWrap.invokingState]!
let rt: RuleTransition = invokingState.transition(0) as! RuleTransition
let follow: IntervalSet = try atn.nextTokens(rt.followState)
try recoverSet.addAll(follow)
ctx = ctxWrap.parent
}
try recoverSet.remove(CommonToken.EPSILON)
// print("recover set "+recoverSet.toString(recognizer.getTokenNames()));
return recoverSet
}
/** Consume tokens until one matches the given token set. */
internal func consumeUntil(_ recognizer: Parser, _ set: IntervalSet) throws {
// errPrint("consumeUntil("+set.toString(recognizer.getTokenNames())+")");
var ttype: Int = try getTokenStream(recognizer).LA(1)
while ttype != CommonToken.EOF && !set.contains(ttype) {
//print("consume during recover LA(1)="+getTokenNames()[input.LA(1)]);
// getTokenStream(recognizer).consume();
try recognizer.consume()
ttype = try getTokenStream(recognizer).LA(1)
}
}
}

View File

@ -1,16 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/** This signifies any kind of mismatched input exceptions such as
* when the current input does not match the expected token.
*/
public class InputMismatchException: RecognitionException<ParserATNSimulator> {
public init(_ recognizer: Parser) throws {
super.init(recognizer, recognizer.getInputStream()!, recognizer._ctx)
self.setOffendingToken(try recognizer.getCurrentToken())
}
}

View File

@ -1,216 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* A simple stream of symbols whose values are represented as integers. This
* interface provides <em>marked ranges</em> with support for a minimum level
* of buffering necessary to implement arbitrary lookahead during prediction.
* For more information on marked ranges, see {@link #mark}.
*
* <p><strong>Initializing Methods:</strong> Some methods in this interface have
* unspecified behavior if no call to an initializing method has occurred after
* the stream was constructed. The following is a list of initializing methods:</p>
*
* <ul>
* <li>{@link #LA}</li>
* <li>{@link #consume}</li>
* <li>{@link #size}</li>
* </ul>
*/
public protocol IntStream: class {
/**
* The value returned by {@link #LA LA()} when the end of the stream is
* reached.
*/
//let EOF : Int = -1;
/**
* The value returned by {@link #getSourceName} when the actual name of the
* underlying source is not known.
*/
//let UNKNOWN_SOURCE_NAME : String = "<unknown>";
/**
* Consumes the current symbol in the stream. This method has the following
* effects:
*
* <ul>
* <li><strong>Forward movement:</strong> The value of {@link #index index()}
* before calling this method is less than the value of {@code index()}
* after calling this method.</li>
* <li><strong>Ordered lookahead:</strong> The value of {@code LA(1)} before
* calling this method becomes the value of {@code LA(-1)} after calling
* this method.</li>
* </ul>
*
* Note that calling this method does not guarantee that {@code index()} is
* incremented by exactly 1, as that would preclude the ability to implement
* filtering streams (e.g. {@link org.antlr.v4.runtime.CommonTokenStream} which distinguishes
* between "on-channel" and "off-channel" tokens).
*
* @throws IllegalStateException if an attempt is made to consume the the
* end of the stream (i.e. if {@code LA(1)==}{@link #EOF EOF} before calling
* {@code consume}).
*/
func consume() throws
/**
* Gets the value of the symbol at offset {@code i} from the current
* position. When {@code i==1}, this method returns the value of the current
* symbol in the stream (which is the next symbol to be consumed). When
* {@code i==-1}, this method returns the value of the previously read
* symbol in the stream. It is not valid to call this method with
* {@code i==0}, but the specific behavior is unspecified because this
* method is frequently called from performance-critical code.
*
* <p>This method is guaranteed to succeed if any of the following are true:</p>
*
* <ul>
* <li>{@code i>0}</li>
* <li>{@code i==-1} and {@link #index index()} returns a value greater
* than the value of {@code index()} after the stream was constructed
* and {@code LA(1)} was called in that order. Specifying the current
* {@code index()} relative to the index after the stream was created
* allows for filtering implementations that do not return every symbol
* from the underlying source. Specifying the call to {@code LA(1)}
* allows for lazily initialized streams.</li>
* <li>{@code LA(i)} refers to a symbol consumed within a marked region
* that has not yet been released.</li>
* </ul>
*
* <p>If {@code i} represents a position at or beyond the end of the stream,
* this method returns {@link #EOF}.</p>
*
* <p>The return value is unspecified if {@code i<0} and fewer than {@code -i}
* calls to {@link #consume consume()} have occurred from the beginning of
* the stream before calling this method.</p>
*
* @throws UnsupportedOperationException if the stream does not support
* retrieving the value of the specified symbol
*/
func LA(_ i: Int) throws -> Int
/**
* A mark provides a guarantee that {@link #seek seek()} operations will be
* valid over a "marked range" extending from the index where {@code mark()}
* was called to the current {@link #index index()}. This allows the use of
* streaming input sources by specifying the minimum buffering requirements
* to support arbitrary lookahead during prediction.
*
* <p>The returned mark is an opaque handle (type {@code int}) which is passed
* to {@link #release release()} when the guarantees provided by the marked
* range are no longer necessary. When calls to
* {@code mark()}/{@code release()} are nested, the marks must be released
* in reverse order of which they were obtained. Since marked regions are
* used during performance-critical sections of prediction, the specific
* behavior of invalid usage is unspecified (i.e. a mark is not released, or
* a mark is released twice, or marks are not released in reverse order from
* which they were created).</p>
*
* <p>The behavior of this method is unspecified if no call to an
* {@link org.antlr.v4.runtime.IntStream initializing method} has occurred after this stream was
* constructed.</p>
*
* <p>This method does not change the current position in the input stream.</p>
*
* <p>The following example shows the use of {@link #mark mark()},
* {@link #release release(mark)}, {@link #index index()}, and
* {@link #seek seek(index)} as part of an operation to safely work within a
* marked region, then restore the stream position to its original value and
* release the mark.</p>
* <pre>
* IntStream stream = ...;
* int index = -1;
* int mark = stream.mark();
* try {
* index = stream.index();
* // perform work here...
* } finally {
* if (index != -1) {
* stream.seek(index);
* }
* stream.release(mark);
* }
* </pre>
*
* @return An opaque marker which should be passed to
* {@link #release release()} when the marked range is no longer required.
*/
func mark() -> Int
/**
* This method releases a marked range created by a call to
* {@link #mark mark()}. Calls to {@code release()} must appear in the
* reverse order of the corresponding calls to {@code mark()}. If a mark is
* released twice, or if marks are not released in reverse order of the
* corresponding calls to {@code mark()}, the behavior is unspecified.
*
* <p>For more information and an example, see {@link #mark}.</p>
*
* @param marker A marker returned by a call to {@code mark()}.
* @see #mark
*/
func release(_ marker: Int) throws
/**
* Return the index into the stream of the input symbol referred to by
* {@code LA(1)}.
*
* <p>The behavior of this method is unspecified if no call to an
* {@link org.antlr.v4.runtime.IntStream initializing method} has occurred after this stream was
* constructed.</p>
*/
func index() -> Int
/**
* Set the input cursor to the position indicated by {@code index}. If the
* specified index lies past the end of the stream, the operation behaves as
* though {@code index} was the index of the EOF symbol. After this method
* returns without throwing an exception, then at least one of the following
* will be true.
*
* <ul>
* <li>{@link #index index()} will return the index of the first symbol
* appearing at or after the specified {@code index}. Specifically,
* implementations which filter their sources should automatically
* adjust {@code index} forward the minimum amount required for the
* operation to target a non-ignored symbol.</li>
* <li>{@code LA(1)} returns {@link #EOF}</li>
* </ul>
*
* This operation is guaranteed to not throw an exception if {@code index}
* lies within a marked region. For more information on marked regions, see
* {@link #mark}. The behavior of this method is unspecified if no call to
* an {@link org.antlr.v4.runtime.IntStream initializing method} has occurred after this stream
* was constructed.
*
* @param index The absolute index to seek to.
*
* @throws IllegalArgumentException if {@code index} is less than 0
* @throws UnsupportedOperationException if the stream does not support
* seeking to the specified index
*/
func seek(_ index: Int) throws
/**
* Returns the total number of symbols in the stream, including a single EOF
* symbol.
*
* @throws UnsupportedOperationException if the size of the stream is
* unknown.
*/
func size() -> Int
/**
* Gets the name of the underlying symbol source. This method returns a
* non-null, non-empty string. If such a name is not known, this method
* returns {@link #UNKNOWN_SOURCE_NAME}.
*/
func getSourceName() -> String
}

View File

@ -1,63 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This class extends {@link org.antlr.v4.runtime.ParserRuleContext} by allowing the value of
* {@link #getRuleIndex} to be explicitly set for the context.
*
* <p>
* {@link org.antlr.v4.runtime.ParserRuleContext} does not include field storage for the rule index
* since the context classes created by the code generator override the
* {@link #getRuleIndex} method to return the correct value for that context.
* Since the parser interpreter does not use the context classes generated for a
* parser, this class (with slightly more memory overhead per node) is used to
* provide equivalent functionality.</p>
*/
public class InterpreterRuleContext: ParserRuleContext {
/** This is the backing field for {@link #getRuleIndex}. */
private var ruleIndex: Int = -1
public override init() {
super.init()
}
/**
* Constructs a new {@link org.antlr.v4.runtime.InterpreterRuleContext} with the specified
* parent, invoking state, and rule index.
*
* @param parent The parent context.
* @param invokingStateNumber The invoking state number.
* @param ruleIndex The rule index for the current context.
*/
public init(_ parent: ParserRuleContext?,
_ invokingStateNumber: Int,
_ ruleIndex: Int) {
self.ruleIndex = ruleIndex
super.init(parent, invokingStateNumber)
}
override
public func getRuleIndex() -> Int {
return ruleIndex
}
/** Copy a {@link org.antlr.v4.runtime.ParserRuleContext} or {@link org.antlr.v4.runtime.InterpreterRuleContext}
* stack to a {@link org.antlr.v4.runtime.InterpreterRuleContext} tree.
* Return {@link null} if {@code ctx} is null.
*/
public static func fromParserRuleContext(_ ctx: ParserRuleContext?) -> InterpreterRuleContext? {
guard let ctx = ctx else {
return nil
}
let dup: InterpreterRuleContext = InterpreterRuleContext()
dup.copyFrom(ctx)
dup.ruleIndex = ctx.getRuleIndex()
dup.parent = fromParserRuleContext(ctx.getParent() as? ParserRuleContext)
return dup
}
}

View File

@ -1,26 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* Represents the type of recognizer an ATN applies to.
*
* @author Sam Harwell
*/
public enum ATNType: Int {
/**
* A lexer grammar.
*/
case lexer = 0
/**
* A parser grammar.
*/
case parser
}

View File

@ -1,19 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
*
* @author Sam Harwell
*/
public class AbstractPredicateTransition: Transition {
/*public override init(_ target : ATNState) {
super.init(target);
}*/
}

View File

@ -1,67 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This class represents profiling event information for an ambiguity.
* Ambiguities are decisions where a particular input resulted in an SLL
* conflict, followed by LL prediction also reaching a conflict state
* (indicating a true ambiguity in the grammar).
*
* <p>
* This event may be reported during SLL prediction in cases where the
* conflicting SLL configuration set provides sufficient information to
* determine that the SLL conflict is truly an ambiguity. For example, if none
* of the ATN configurations in the conflicting SLL configuration set have
* traversed a global follow transition (i.e.
* {@link org.antlr.v4.runtime.atn.ATNConfig#reachesIntoOuterContext} is 0 for all configurations), then
* the result of SLL prediction for that input is known to be equivalent to the
* result of LL prediction for that input.</p>
*
* <p>
* In some cases, the minimum represented alternative in the conflicting LL
* configuration set is not equal to the minimum represented alternative in the
* conflicting SLL configuration set. Grammars and inputs which result in this
* scenario are unable to use {@link org.antlr.v4.runtime.atn.PredictionMode#SLL}, which in turn means
* they cannot use the two-stage parsing strategy to improve parsing performance
* for that input.</p>
*
* @see org.antlr.v4.runtime.atn.ParserATNSimulator#reportAmbiguity
* @see org.antlr.v4.runtime.ANTLRErrorListener#reportAmbiguity
*
* @since 4.3
*/
public class AmbiguityInfo: DecisionEventInfo {
/** The set of alternative numbers for this decision event that lead to a valid parse. */
public var ambigAlts: BitSet
/**
* Constructs a new instance of the {@link org.antlr.v4.runtime.atn.AmbiguityInfo} class with the
* specified detailed ambiguity information.
*
* @param decision The decision number
* @param configs The final configuration set identifying the ambiguous
* alternatives for the current input
* @param ambigAlts The set of alternatives in the decision that lead to a valid parse.
* @param input The input token stream
* @param startIndex The start index for the current prediction
* @param stopIndex The index at which the ambiguity was identified during
* prediction
* @param fullCtx {@code true} if the ambiguity was identified during LL
* prediction; otherwise, {@code false} if the ambiguity was identified
* during SLL prediction
*/
public init(_ decision: Int,
_ configs: ATNConfigSet,
_ ambigAlts: BitSet,
_ input: TokenStream, _ startIndex: Int, _ stopIndex: Int,
_ fullCtx: Bool) {
self.ambigAlts = ambigAlts
super.init(decision, configs, input, startIndex, stopIndex, fullCtx)
}
}

View File

@ -1,18 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
*
* @author Sam Harwell
*/
public final class BasicBlockStartState: BlockStartState {
override
public func getStateType() -> Int {
return BlockStartState.BLOCK_START
}
}

View File

@ -1,20 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
*
* @author Sam Harwell
*/
public final class BasicState: ATNState {
override
public func getStateType() -> Int {
return ATNState.BASIC
}
}

View File

@ -1,17 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/** Terminal node of a simple {@code (a|b|c)} block. */
public final class BlockEndState: ATNState {
public var startState: BlockStartState?
override
public func getStateType() -> Int {
return ATNState.BLOCK_END
}
}

View File

@ -1,11 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/** The start of a regular {@code (...)} block. */
public class BlockStartState: DecisionState {
public var endState: BlockEndState?
}

View File

@ -1,44 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This class represents profiling event information for a context sensitivity.
* Context sensitivities are decisions where a particular input resulted in an
* SLL conflict, but LL prediction produced a single unique alternative.
*
* <p>
* In some cases, the unique alternative identified by LL prediction is not
* equal to the minimum represented alternative in the conflicting SLL
* configuration set. Grammars and inputs which result in this scenario are
* unable to use {@link org.antlr.v4.runtime.atn.PredictionMode#SLL}, which in turn means they cannot use
* the two-stage parsing strategy to improve parsing performance for that
* input.</p>
*
* @see org.antlr.v4.runtime.atn.ParserATNSimulator#reportContextSensitivity
* @see org.antlr.v4.runtime.ANTLRErrorListener#reportContextSensitivity
*
* @since 4.3
*/
public class ContextSensitivityInfo: DecisionEventInfo {
/**
* Constructs a new instance of the {@link org.antlr.v4.runtime.atn.ContextSensitivityInfo} class
* with the specified detailed context sensitivity information.
*
* @param decision The decision number
* @param configs The final configuration set containing the unique
* alternative identified by full-context prediction
* @param input The input token stream
* @param startIndex The start index for the current prediction
* @param stopIndex The index at which the context sensitivity was
* identified during full-context prediction
*/
public init(_ decision: Int,
_ configs: ATNConfigSet,
_ input: TokenStream, _ startIndex: Int, _ stopIndex: Int) {
super.init(decision, configs, input, startIndex, stopIndex, true)
}
}

View File

@ -1,74 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This is the base class for gathering detailed information about prediction
* events which occur during parsing.
*
* Note that we could record the parser call stack at the time this event
* occurred but in the presence of left recursive rules, the stack is kind of
* meaningless. It's better to look at the individual configurations for their
* individual stacks. Of course that is a {@link org.antlr.v4.runtime.atn.PredictionContext} object
* not a parse tree node and so it does not have information about the extent
* (start...stop) of the various subtrees. Examining the stack tops of all
* configurations provide the return states for the rule invocations.
* From there you can get the enclosing rule.
*
* @since 4.3
*/
public class DecisionEventInfo {
/**
* The invoked decision number which this event is related to.
*
* @see org.antlr.v4.runtime.atn.ATN#decisionToState
*/
public let decision: Int
/**
* The configuration set containing additional information relevant to the
* prediction state when the current event occurred, or {@code null} if no
* additional information is relevant or available.
*/
public let configs: ATNConfigSet?
/**
* The input token stream which is being parsed.
*/
public let input: TokenStream
/**
* The token index in the input stream at which the current prediction was
* originally invoked.
*/
public let startIndex: Int
/**
* The token index in the input stream at which the current event occurred.
*/
public let stopIndex: Int
/**
* {@code true} if the current event occurred during LL prediction;
* otherwise, {@code false} if the input occurred during SLL prediction.
*/
public let fullCtx: Bool
public init(_ decision: Int,
_ configs: ATNConfigSet?,
_ input: TokenStream,
_ startIndex: Int,
_ stopIndex: Int,
_ fullCtx: Bool) {
self.decision = decision
self.fullCtx = fullCtx
self.stopIndex = stopIndex
self.input = input
self.startIndex = startIndex
self.configs = configs
}
}

View File

@ -1,244 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This class contains profiling gathered for a particular decision.
*
* <p>
* Parsing performance in ANTLR 4 is heavily influenced by both static factors
* (e.g. the form of the rules in the grammar) and dynamic factors (e.g. the
* choice of input and the state of the DFA cache at the time profiling
* operations are started). For best results, gather and use aggregate
* statistics from a large sample of inputs representing the inputs expected in
* production before using the results to make changes in the grammar.</p>
*
* @since 4.3
*/
public class DecisionInfo: CustomStringConvertible {
/**
* The decision number, which is an index into {@link org.antlr.v4.runtime.atn.ATN#decisionToState}.
*/
public final var decision: Int
/**
* The total number of times {@link org.antlr.v4.runtime.atn.ParserATNSimulator#adaptivePredict} was
* invoked for this decision.
*/
public var invocations: Int64 = 0
/**
* The total time spent in {@link org.antlr.v4.runtime.atn.ParserATNSimulator#adaptivePredict} for
* this decision, in nanoseconds.
*
* <p>
* The value of this field contains the sum of differential results obtained
* by {@link System#nanoTime()}, and is not adjusted to compensate for JIT
* and/or garbage collection overhead. For best accuracy, use a modern JVM
* implementation that provides precise results from
* {@link System#nanoTime()}, and perform profiling in a separate process
* which is warmed up by parsing the input prior to profiling. If desired,
* call {@link org.antlr.v4.runtime.atn.ATNSimulator#clearDFA} to reset the DFA cache to its initial
* state before starting the profiling measurement pass.</p>
*/
public var timeInPrediction: Int64 = 0
/**
* The sum of the lookahead required for SLL prediction for this decision.
* Note that SLL prediction is used before LL prediction for performance
* reasons even when {@link org.antlr.v4.runtime.atn.PredictionMode#LL} or
* {@link org.antlr.v4.runtime.atn.PredictionMode#LL_EXACT_AMBIG_DETECTION} is used.
*/
public var SLL_TotalLook: Int64 = 0
/**
* Gets the minimum lookahead required for any single SLL prediction to
* complete for this decision, by reaching a unique prediction, reaching an
* SLL conflict state, or encountering a syntax error.
*/
public var SLL_MinLook: Int64 = 0
/**
* Gets the maximum lookahead required for any single SLL prediction to
* complete for this decision, by reaching a unique prediction, reaching an
* SLL conflict state, or encountering a syntax error.
*/
public var SLL_MaxLook: Int64 = 0
/**
* Gets the {@link org.antlr.v4.runtime.atn.LookaheadEventInfo} associated with the event where the
* {@link #SLL_MaxLook} value was set.
*/
public var SLL_MaxLookEvent: LookaheadEventInfo!
/**
* The sum of the lookahead required for LL prediction for this decision.
* Note that LL prediction is only used when SLL prediction reaches a
* conflict state.
*/
public var LL_TotalLook: Int64 = 0
/**
* Gets the minimum lookahead required for any single LL prediction to
* complete for this decision. An LL prediction completes when the algorithm
* reaches a unique prediction, a conflict state (for
* {@link org.antlr.v4.runtime.atn.PredictionMode#LL}, an ambiguity state (for
* {@link org.antlr.v4.runtime.atn.PredictionMode#LL_EXACT_AMBIG_DETECTION}, or a syntax error.
*/
public var LL_MinLook: Int64 = 0
/**
* Gets the maximum lookahead required for any single LL prediction to
* complete for this decision. An LL prediction completes when the algorithm
* reaches a unique prediction, a conflict state (for
* {@link org.antlr.v4.runtime.atn.PredictionMode#LL}, an ambiguity state (for
* {@link org.antlr.v4.runtime.atn.PredictionMode#LL_EXACT_AMBIG_DETECTION}, or a syntax error.
*/
public var LL_MaxLook: Int64 = 0
/**
* Gets the {@link org.antlr.v4.runtime.atn.LookaheadEventInfo} associated with the event where the
* {@link #LL_MaxLook} value was set.
*/
public var LL_MaxLookEvent: LookaheadEventInfo!
/**
* A collection of {@link org.antlr.v4.runtime.atn.ContextSensitivityInfo} instances describing the
* context sensitivities encountered during LL prediction for this decision.
*
* @see org.antlr.v4.runtime.atn.ContextSensitivityInfo
*/
public final var contextSensitivities: Array<ContextSensitivityInfo> = Array<ContextSensitivityInfo>()
/**
* A collection of {@link org.antlr.v4.runtime.atn.ErrorInfo} instances describing the parse errors
* identified during calls to {@link org.antlr.v4.runtime.atn.ParserATNSimulator#adaptivePredict} for
* this decision.
*
* @see org.antlr.v4.runtime.atn.ErrorInfo
*/
public final var errors: Array<ErrorInfo> = Array<ErrorInfo>()
/**
* A collection of {@link org.antlr.v4.runtime.atn.AmbiguityInfo} instances describing the
* ambiguities encountered during LL prediction for this decision.
*
* @see org.antlr.v4.runtime.atn.AmbiguityInfo
*/
public final var ambiguities: Array<AmbiguityInfo> = Array<AmbiguityInfo>()
/**
* A collection of {@link org.antlr.v4.runtime.atn.PredicateEvalInfo} instances describing the
* results of evaluating individual predicates during prediction for this
* decision.
*
* @see org.antlr.v4.runtime.atn.PredicateEvalInfo
*/
public final var predicateEvals: Array<PredicateEvalInfo> = Array<PredicateEvalInfo>()
/**
* The total number of ATN transitions required during SLL prediction for
* this decision. An ATN transition is determined by the number of times the
* DFA does not contain an edge that is required for prediction, resulting
* in on-the-fly computation of that edge.
*
* <p>
* If DFA caching of SLL transitions is employed by the implementation, ATN
* computation may cache the computed edge for efficient lookup during
* future parsing of this decision. Otherwise, the SLL parsing algorithm
* will use ATN transitions exclusively.</p>
*
* @see #SLL_ATNTransitions
* @see org.antlr.v4.runtime.atn.ParserATNSimulator#computeTargetState
* @see org.antlr.v4.runtime.atn.LexerATNSimulator#computeTargetState
*/
public var SLL_ATNTransitions: Int64 = 0
/**
* The total number of DFA transitions required during SLL prediction for
* this decision.
*
* <p>If the ATN simulator implementation does not use DFA caching for SLL
* transitions, this value will be 0.</p>
*
* @see org.antlr.v4.runtime.atn.ParserATNSimulator#getExistingTargetState
* @see org.antlr.v4.runtime.atn.LexerATNSimulator#getExistingTargetState
*/
public var SLL_DFATransitions: Int64 = 0
/**
* Gets the total number of times SLL prediction completed in a conflict
* state, resulting in fallback to LL prediction.
*
* <p>Note that this value is not related to whether or not
* {@link org.antlr.v4.runtime.atn.PredictionMode#SLL} may be used successfully with a particular
* grammar. If the ambiguity resolution algorithm applied to the SLL
* conflicts for this decision produce the same result as LL prediction for
* this decision, {@link org.antlr.v4.runtime.atn.PredictionMode#SLL} would produce the same overall
* parsing result as {@link org.antlr.v4.runtime.atn.PredictionMode#LL}.</p>
*/
public var LL_Fallback: Int64 = 0
/**
* The total number of ATN transitions required during LL prediction for
* this decision. An ATN transition is determined by the number of times the
* DFA does not contain an edge that is required for prediction, resulting
* in on-the-fly computation of that edge.
*
* <p>
* If DFA caching of LL transitions is employed by the implementation, ATN
* computation may cache the computed edge for efficient lookup during
* future parsing of this decision. Otherwise, the LL parsing algorithm will
* use ATN transitions exclusively.</p>
*
* @see #LL_DFATransitions
* @see org.antlr.v4.runtime.atn.ParserATNSimulator#computeTargetState
* @see org.antlr.v4.runtime.atn.LexerATNSimulator#computeTargetState
*/
public var LL_ATNTransitions: Int64 = 0
/**
* The total number of DFA transitions required during LL prediction for
* this decision.
*
* <p>If the ATN simulator implementation does not use DFA caching for LL
* transitions, this value will be 0.</p>
*
* @see org.antlr.v4.runtime.atn.ParserATNSimulator#getExistingTargetState
* @see org.antlr.v4.runtime.atn.LexerATNSimulator#getExistingTargetState
*/
public var LL_DFATransitions: Int64 = 0
/**
* Constructs a new instance of the {@link org.antlr.v4.runtime.atn.DecisionInfo} class to contain
* statistics for a particular decision.
*
* @param decision The decision number
*/
public init(_ decision: Int) {
self.decision = decision
}
public var description: String {
let desc: StringBuilder = StringBuilder()
desc.append("{")
desc.append("decision=\(decision)")
desc.append(", contextSensitivities=\(contextSensitivities.count)")
desc.append(", errors=\(errors.count)")
desc.append(", ambiguities=\(ambiguities.count)")
desc.append(", SLL_lookahead=\(SLL_TotalLook)")
desc.append(", SLL_ATNTransitions=\(SLL_ATNTransitions)")
desc.append(", SLL_DFATransitions=\(SLL_DFATransitions)")
desc.append(", LL_Fallback=\(LL_Fallback)")
desc.append(", LL_lookahead=\(LL_TotalLook)")
desc.append(", LL_ATNTransitions=\(LL_ATNTransitions)")
desc.append("}")
return desc.toString()
}
}

View File

@ -1,10 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
public class DecisionState: ATNState {
public var decision: Int = -1
public var nonGreedy: Bool = false
}

View File

@ -1,41 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This class represents profiling event information for a syntax error
* identified during prediction. Syntax errors occur when the prediction
* algorithm is unable to identify an alternative which would lead to a
* successful parse.
*
* @see org.antlr.v4.runtime.Parser#notifyErrorListeners(org.antlr.v4.runtime.Token, String, org.antlr.v4.runtime.RecognitionException)
* @see org.antlr.v4.runtime.ANTLRErrorListener#syntaxError
*
* @since 4.3
*/
public class ErrorInfo: DecisionEventInfo {
/**
* Constructs a new instance of the {@link org.antlr.v4.runtime.atn.ErrorInfo} class with the
* specified detailed syntax error information.
*
* @param decision The decision number
* @param configs The final configuration set reached during prediction
* prior to reaching the {@link org.antlr.v4.runtime.atn.ATNSimulator#ERROR} state
* @param input The input token stream
* @param startIndex The start index for the current prediction
* @param stopIndex The index at which the syntax error was identified
* @param fullCtx {@code true} if the syntax error was identified during LL
* prediction; otherwise, {@code false} if the syntax error was identified
* during SLL prediction
*/
public init(_ decision: Int,
_ configs: ATNConfigSet,
_ input: TokenStream, _ startIndex: Int, _ stopIndex: Int,
_ fullCtx: Bool) {
super.init(decision, configs, input, startIndex, stopIndex, fullCtx)
}
}

View File

@ -1,202 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* Represents an executor for a sequence of lexer actions which traversed during
* the matching operation of a lexer rule (token).
*
* <p>The executor tracks position information for position-dependent lexer actions
* efficiently, ensuring that actions appearing only at the end of the rule do
* not cause bloating of the {@link org.antlr.v4.runtime.dfa.DFA} created for the lexer.</p>
*
* @author Sam Harwell
* @since 4.2
*/
public class LexerActionExecutor: Hashable {
fileprivate final var lexerActions: [LexerAction]
/**
* Caches the result of {@link #hashCode} since the hash code is an element
* of the performance-critical {@link org.antlr.v4.runtime.atn.LexerATNConfig#hashCode} operation.
*/
fileprivate final var hashCode: Int
/**
* Constructs an executor for a sequence of {@link org.antlr.v4.runtime.atn.LexerAction} actions.
* @param lexerActions The lexer actions to execute.
*/
public init(_ lexerActions: [LexerAction]) {
self.lexerActions = lexerActions
var hash: Int = MurmurHash.initialize()
for lexerAction: LexerAction in lexerActions {
hash = MurmurHash.update(hash, lexerAction)
}
self.hashCode = MurmurHash.finish(hash, lexerActions.count)
}
/**
* Creates a {@link org.antlr.v4.runtime.atn.LexerActionExecutor} which executes the actions for
* the input {@code lexerActionExecutor} followed by a specified
* {@code lexerAction}.
*
* @param lexerActionExecutor The executor for actions already traversed by
* the lexer while matching a token within a particular
* {@link org.antlr.v4.runtime.atn.LexerATNConfig}. If this is {@code null}, the method behaves as
* though it were an empty executor.
* @param lexerAction The lexer action to execute after the actions
* specified in {@code lexerActionExecutor}.
*
* @return A {@link org.antlr.v4.runtime.atn.LexerActionExecutor} for executing the combine actions
* of {@code lexerActionExecutor} and {@code lexerAction}.
*/
public static func append(_ lexerActionExecutor: LexerActionExecutor?, _ lexerAction: LexerAction) -> LexerActionExecutor {
if lexerActionExecutor == nil {
return LexerActionExecutor([lexerAction])
}
//var lexerActions : [LexerAction] = lexerActionExecutor.lexerActions, //lexerActionExecutor.lexerActions.length + 1);
var lexerActions: [LexerAction] = lexerActionExecutor!.lexerActions
lexerActions.append(lexerAction)
//lexerActions[lexerActions.length - 1] = lexerAction;
return LexerActionExecutor(lexerActions)
}
/**
* Creates a {@link org.antlr.v4.runtime.atn.LexerActionExecutor} which encodes the current offset
* for position-dependent lexer actions.
*
* <p>Normally, when the executor encounters lexer actions where
* {@link org.antlr.v4.runtime.atn.LexerAction#isPositionDependent} returns {@code true}, it calls
* {@link org.antlr.v4.runtime.IntStream#seek} on the input {@link org.antlr.v4.runtime.CharStream} to set the input
* position to the <em>end</em> of the current token. This behavior provides
* for efficient DFA representation of lexer actions which appear at the end
* of a lexer rule, even when the lexer rule matches a variable number of
* characters.</p>
*
* <p>Prior to traversing a match transition in the ATN, the current offset
* from the token start index is assigned to all position-dependent lexer
* actions which have not already been assigned a fixed offset. By storing
* the offsets relative to the token start index, the DFA representation of
* lexer actions which appear in the middle of tokens remains efficient due
* to sharing among tokens of the same length, regardless of their absolute
* position in the input stream.</p>
*
* <p>If the current executor already has offsets assigned to all
* position-dependent lexer actions, the method returns {@code this}.</p>
*
* @param offset The current offset to assign to all position-dependent
* lexer actions which do not already have offsets assigned.
*
* @return A {@link org.antlr.v4.runtime.atn.LexerActionExecutor} which stores input stream offsets
* for all position-dependent lexer actions.
*/
public func fixOffsetBeforeMatch(_ offset: Int) -> LexerActionExecutor {
var updatedLexerActions: [LexerAction]? = nil
let length = lexerActions.count
for i in 0..<length {
if lexerActions[i].isPositionDependent() && !(lexerActions[i] is LexerIndexedCustomAction) {
if updatedLexerActions == nil {
updatedLexerActions = lexerActions //lexerActions.clone();
}
updatedLexerActions![i] = LexerIndexedCustomAction(offset, lexerActions[i])
}
}
if updatedLexerActions == nil {
return self
}
return LexerActionExecutor(updatedLexerActions!)
}
/**
* Gets the lexer actions to be executed by this executor.
* @return The lexer actions to be executed by this executor.
*/
public func getLexerActions() -> [LexerAction] {
return lexerActions
}
/**
* Execute the actions encapsulated by this executor within the context of a
* particular {@link org.antlr.v4.runtime.Lexer}.
*
* <p>This method calls {@link org.antlr.v4.runtime.IntStream#seek} to set the position of the
* {@code input} {@link org.antlr.v4.runtime.CharStream} prior to calling
* {@link org.antlr.v4.runtime.atn.LexerAction#execute} on a position-dependent action. Before the
* method returns, the input position will be restored to the same position
* it was in when the method was invoked.</p>
*
* @param lexer The lexer instance.
* @param input The input stream which is the source for the current token.
* When this method is called, the current {@link org.antlr.v4.runtime.IntStream#index} for
* {@code input} should be the start of the following token, i.e. 1
* character past the end of the current token.
* @param startIndex The token start index. This value may be passed to
* {@link org.antlr.v4.runtime.IntStream#seek} to set the {@code input} position to the beginning
* of the token.
*/
public func execute(_ lexer: Lexer, _ input: CharStream, _ startIndex: Int) throws {
var requiresSeek: Bool = false
var stopIndex: Int = input.index()
defer {
if requiresSeek {
try! input.seek(stopIndex)
}
}
//try {
for var lexerAction: LexerAction in self.lexerActions {
if let runLexerAction = lexerAction as? LexerIndexedCustomAction {
let offset: Int = runLexerAction.getOffset()
try input.seek(startIndex + offset)
lexerAction = runLexerAction.getAction()
requiresSeek = (startIndex + offset) != stopIndex
} else {
if lexerAction.isPositionDependent() {
try input.seek(stopIndex)
requiresSeek = false
}
}
try lexerAction.execute(lexer)
}
//}
}
public var hashValue: Int {
return self.hashCode
}
}
public func ==(lhs: LexerActionExecutor, rhs: LexerActionExecutor) -> Bool {
if lhs === rhs {
return true
}
if lhs.lexerActions.count != rhs.lexerActions.count {
return false
}
let length = lhs.lexerActions.count
for i in 0..<length {
if !(lhs.lexerActions[i] == rhs.lexerActions[i]) {
return false
}
}
return lhs.hashCode == rhs.hashCode
}

View File

@ -1,88 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* Implements the {@code channel} lexer action by calling
* {@link org.antlr.v4.runtime.Lexer#setChannel} with the assigned channel.
*
* @author Sam Harwell
* @since 4.2
*/
public final class LexerChannelAction: LexerAction, CustomStringConvertible {
fileprivate let channel: Int
/**
* Constructs a new {@code channel} action with the specified channel value.
* @param channel The channel value to pass to {@link org.antlr.v4.runtime.Lexer#setChannel}.
*/
public init(_ channel: Int) {
self.channel = channel
}
/**
* Gets the channel to use for the {@link org.antlr.v4.runtime.Token} created by the lexer.
*
* @return The channel to use for the {@link org.antlr.v4.runtime.Token} created by the lexer.
*/
public func getChannel() -> Int {
return channel
}
/**
* {@inheritDoc}
* @return This method returns {@link org.antlr.v4.runtime.atn.LexerActionType#CHANNEL}.
*/
public override func getActionType() -> LexerActionType {
return LexerActionType.channel
}
/**
* {@inheritDoc}
* @return This method returns {@code false}.
*/
public override func isPositionDependent() -> Bool {
return false
}
/**
* {@inheritDoc}
*
* <p>This action is implemented by calling {@link org.antlr.v4.runtime.Lexer#setChannel} with the
* value provided by {@link #getChannel}.</p>
*/
public override func execute(_ lexer: Lexer) {
lexer.setChannel(channel)
}
override
public var hashValue: Int {
var hash: Int = MurmurHash.initialize()
hash = MurmurHash.update(hash, getActionType().rawValue)
hash = MurmurHash.update(hash, channel)
return MurmurHash.finish(hash, 2)
}
public var description: String {
return "channel\(channel)"
}
}
public func ==(lhs: LexerChannelAction, rhs: LexerChannelAction) -> Bool {
if lhs === rhs {
return true
}
return lhs.channel == rhs.channel
}

View File

@ -1,114 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* Executes a custom lexer action by calling {@link org.antlr.v4.runtime.Recognizer#action} with the
* rule and action indexes assigned to the custom action. The implementation of
* a custom action is added to the generated code for the lexer in an override
* of {@link org.antlr.v4.runtime.Recognizer#action} when the grammar is compiled.
*
* <p>This class may represent embedded actions created with the <code>{...}</code>
* syntax in ANTLR 4, as well as actions created for lexer commands where the
* command argument could not be evaluated when the grammar was compiled.</p>
*
* @author Sam Harwell
* @since 4.2
*/
public final class LexerCustomAction: LexerAction {
fileprivate let ruleIndex: Int
fileprivate let actionIndex: Int
/**
* Constructs a custom lexer action with the specified rule and action
* indexes.
*
* @param ruleIndex The rule index to use for calls to
* {@link org.antlr.v4.runtime.Recognizer#action}.
* @param actionIndex The action index to use for calls to
* {@link org.antlr.v4.runtime.Recognizer#action}.
*/
public init(_ ruleIndex: Int, _ actionIndex: Int) {
self.ruleIndex = ruleIndex
self.actionIndex = actionIndex
}
/**
* Gets the rule index to use for calls to {@link org.antlr.v4.runtime.Recognizer#action}.
*
* @return The rule index for the custom action.
*/
public func getRuleIndex() -> Int {
return ruleIndex
}
/**
* Gets the action index to use for calls to {@link org.antlr.v4.runtime.Recognizer#action}.
*
* @return The action index for the custom action.
*/
public func getActionIndex() -> Int {
return actionIndex
}
/**
* {@inheritDoc}
*
* @return This method returns {@link org.antlr.v4.runtime.atn.LexerActionType#CUSTOM}.
*/
public override func getActionType() -> LexerActionType {
return LexerActionType.custom
}
/**
* Gets whether the lexer action is position-dependent. Position-dependent
* actions may have different semantics depending on the {@link org.antlr.v4.runtime.CharStream}
* index at the time the action is executed.
*
* <p>Custom actions are position-dependent since they may represent a
* user-defined embedded action which makes calls to methods like
* {@link org.antlr.v4.runtime.Lexer#getText}.</p>
*
* @return This method returns {@code true}.
*/
override
public func isPositionDependent() -> Bool {
return true
}
/**
* {@inheritDoc}
*
* <p>Custom actions are implemented by calling {@link org.antlr.v4.runtime.Lexer#action} with the
* appropriate rule and action indexes.</p>
*/
override
public func execute(_ lexer: Lexer) throws {
try lexer.action(nil, ruleIndex, actionIndex)
}
override
public var hashValue: Int {
var hash: Int = MurmurHash.initialize()
hash = MurmurHash.update(hash, getActionType().rawValue)
hash = MurmurHash.update(hash, ruleIndex)
hash = MurmurHash.update(hash, actionIndex)
return MurmurHash.finish(hash, 3)
}
}
public func ==(lhs: LexerCustomAction, rhs: LexerCustomAction) -> Bool {
if lhs === rhs {
return true
}
return lhs.ruleIndex == rhs.ruleIndex
&& lhs.actionIndex == rhs.actionIndex
}

View File

@ -1,118 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This implementation of {@link org.antlr.v4.runtime.atn.LexerAction} is used for tracking input offsets
* for position-dependent actions within a {@link org.antlr.v4.runtime.atn.LexerActionExecutor}.
*
* <p>This action is not serialized as part of the ATN, and is only required for
* position-dependent lexer actions which appear at a location other than the
* end of a rule. For more information about DFA optimizations employed for
* lexer actions, see {@link org.antlr.v4.runtime.atn.LexerActionExecutor#append} and
* {@link org.antlr.v4.runtime.atn.LexerActionExecutor#fixOffsetBeforeMatch}.</p>
*
* @author Sam Harwell
* @since 4.2
*/
public final class LexerIndexedCustomAction: LexerAction {
fileprivate let offset: Int
fileprivate let action: LexerAction
/**
* Constructs a new indexed custom action by associating a character offset
* with a {@link org.antlr.v4.runtime.atn.LexerAction}.
*
* <p>Note: This class is only required for lexer actions for which
* {@link org.antlr.v4.runtime.atn.LexerAction#isPositionDependent} returns {@code true}.</p>
*
* @param offset The offset into the input {@link org.antlr.v4.runtime.CharStream}, relative to
* the token start index, at which the specified lexer action should be
* executed.
* @param action The lexer action to execute at a particular offset in the
* input {@link org.antlr.v4.runtime.CharStream}.
*/
public init(_ offset: Int, _ action: LexerAction) {
self.offset = offset
self.action = action
}
/**
* Gets the location in the input {@link org.antlr.v4.runtime.CharStream} at which the lexer
* action should be executed. The value is interpreted as an offset relative
* to the token start index.
*
* @return The location in the input {@link org.antlr.v4.runtime.CharStream} at which the lexer
* action should be executed.
*/
public func getOffset() -> Int {
return offset
}
/**
* Gets the lexer action to execute.
*
* @return A {@link org.antlr.v4.runtime.atn.LexerAction} object which executes the lexer action.
*/
public func getAction() -> LexerAction {
return action
}
/**
* {@inheritDoc}
*
* @return This method returns the result of calling {@link #getActionType}
* on the {@link org.antlr.v4.runtime.atn.LexerAction} returned by {@link #getAction}.
*/
public override func getActionType() -> LexerActionType {
return action.getActionType()
}
/**
* {@inheritDoc}
* @return This method returns {@code true}.
*/
public override func isPositionDependent() -> Bool {
return true
}
/**
* {@inheritDoc}
*
* <p>This method calls {@link #execute} on the result of {@link #getAction}
* using the provided {@code lexer}.</p>
*/
public override func execute(_ lexer: Lexer) throws {
// assume the input stream position was properly set by the calling code
try action.execute(lexer)
}
public override var hashValue: Int {
var hash: Int = MurmurHash.initialize()
hash = MurmurHash.update(hash, offset)
hash = MurmurHash.update(hash, action)
return MurmurHash.finish(hash, 2)
}
}
public func ==(lhs: LexerIndexedCustomAction, rhs: LexerIndexedCustomAction) -> Bool {
if lhs === rhs {
return true
}
return lhs.offset == rhs.offset
&& lhs.action == rhs.action
}

View File

@ -1,85 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* Implements the {@code mode} lexer action by calling {@link org.antlr.v4.runtime.Lexer#mode} with
* the assigned mode.
*
* @author Sam Harwell
* @since 4.2
*/
public final class LexerModeAction: LexerAction, CustomStringConvertible {
fileprivate final var mode: Int
/**
* Constructs a new {@code mode} action with the specified mode value.
* @param mode The mode value to pass to {@link org.antlr.v4.runtime.Lexer#mode}.
*/
public init(_ mode: Int) {
self.mode = mode
}
/**
* Get the lexer mode this action should transition the lexer to.
*
* @return The lexer mode for this {@code mode} command.
*/
public func getMode() -> Int {
return mode
}
/**
* {@inheritDoc}
* @return This method returns {@link org.antlr.v4.runtime.atn.LexerActionType#MODE}.
*/
public override func getActionType() -> LexerActionType {
return LexerActionType.mode
}
/**
* {@inheritDoc}
* @return This method returns {@code false}.
*/
public override func isPositionDependent() -> Bool {
return false
}
/**
* {@inheritDoc}
*
* <p>This action is implemented by calling {@link org.antlr.v4.runtime.Lexer#mode} with the
* value provided by {@link #getMode}.</p>
*/
override
public func execute(_ lexer: Lexer) {
lexer.mode(mode)
}
override
public var hashValue: Int {
var hash: Int = MurmurHash.initialize()
hash = MurmurHash.update(hash, getActionType().rawValue)
hash = MurmurHash.update(hash, mode)
return MurmurHash.finish(hash, 2)
}
public var description: String {
return "mode(\(mode))"
}
}
public func ==(lhs: LexerModeAction, rhs: LexerModeAction) -> Bool {
if lhs === rhs {
return true
}
return lhs.mode == rhs.mode
}

View File

@ -1,77 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* Implements the {@code more} lexer action by calling {@link org.antlr.v4.runtime.Lexer#more}.
*
* <p>The {@code more} command does not have any parameters, so this action is
* implemented as a singleton instance exposed by {@link #INSTANCE}.</p>
*
* @author Sam Harwell
* @since 4.2
*/
public final class LexerMoreAction: LexerAction, CustomStringConvertible {
/**
* Provides a singleton instance of this parameterless lexer action.
*/
public static let INSTANCE: LexerMoreAction = LexerMoreAction()
/**
* Constructs the singleton instance of the lexer {@code more} command.
*/
private override init() {
}
/**
* {@inheritDoc}
* @return This method returns {@link org.antlr.v4.runtime.atn.LexerActionType#MORE}.
*/
override
public func getActionType() -> LexerActionType {
return LexerActionType.more
}
/**
* {@inheritDoc}
* @return This method returns {@code false}.
*/
override
public func isPositionDependent() -> Bool {
return false
}
/**
* {@inheritDoc}
*
* <p>This action is implemented by calling {@link org.antlr.v4.runtime.Lexer#more}.</p>
*/
override
public func execute(_ lexer: Lexer) {
lexer.more()
}
override
public var hashValue: Int {
var hash: Int = MurmurHash.initialize()
hash = MurmurHash.update(hash, getActionType().rawValue)
return MurmurHash.finish(hash, 1)
}
public var description: String {
return "more"
}
}
public func ==(lhs: LexerMoreAction, rhs: LexerMoreAction) -> Bool {
return lhs === rhs
}

View File

@ -1,76 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* Implements the {@code popMode} lexer action by calling {@link org.antlr.v4.runtime.Lexer#popMode}.
*
* <p>The {@code popMode} command does not have any parameters, so this action is
* implemented as a singleton instance exposed by {@link #INSTANCE}.</p>
*
* @author Sam Harwell
* @since 4.2
*/
public final class LexerPopModeAction: LexerAction, CustomStringConvertible {
/**
* Provides a singleton instance of this parameterless lexer action.
*/
public static let INSTANCE: LexerPopModeAction = LexerPopModeAction()
/**
* Constructs the singleton instance of the lexer {@code popMode} command.
*/
private override init() {
}
/**
* {@inheritDoc}
* @return This method returns {@link org.antlr.v4.runtime.atn.LexerActionType#popMode}.
*/
override
public func getActionType() -> LexerActionType {
return LexerActionType.popMode
}
/**
* {@inheritDoc}
* @return This method returns {@code false}.
*/
public override func isPositionDependent() -> Bool {
return false
}
/**
* {@inheritDoc}
*
* <p>This action is implemented by calling {@link org.antlr.v4.runtime.Lexer#popMode}.</p>
*/
public override func execute(_ lexer: Lexer) throws {
try lexer.popMode()
}
override
public var hashValue: Int {
var hash: Int = MurmurHash.initialize()
hash = MurmurHash.update(hash, getActionType().rawValue)
return MurmurHash.finish(hash, 1)
}
public var description: String {
return "popMode"
}
}
public func ==(lhs: LexerPopModeAction, rhs: LexerPopModeAction) -> Bool {
return lhs === rhs
}

View File

@ -1,87 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* Implements the {@code pushMode} lexer action by calling
* {@link org.antlr.v4.runtime.Lexer#pushMode} with the assigned mode.
*
* @author Sam Harwell
* @since 4.2
*/
public final class LexerPushModeAction: LexerAction, CustomStringConvertible {
fileprivate final var mode: Int
/**
* Constructs a new {@code pushMode} action with the specified mode value.
* @param mode The mode value to pass to {@link org.antlr.v4.runtime.Lexer#pushMode}.
*/
public init(_ mode: Int) {
self.mode = mode
}
/**
* Get the lexer mode this action should transition the lexer to.
*
* @return The lexer mode for this {@code pushMode} command.
*/
public func getMode() -> Int {
return mode
}
/**
* {@inheritDoc}
* @return This method returns {@link org.antlr.v4.runtime.atn.LexerActionType#pushMode}.
*/
public override func getActionType() -> LexerActionType {
return LexerActionType.pushMode
}
/**
* {@inheritDoc}
* @return This method returns {@code false}.
*/
public override func isPositionDependent() -> Bool {
return false
}
/**
* {@inheritDoc}
*
* <p>This action is implemented by calling {@link org.antlr.v4.runtime.Lexer#pushMode} with the
* value provided by {@link #getMode}.</p>
*/
override
public func execute(_ lexer: Lexer) {
lexer.pushMode(mode)
}
override
public var hashValue: Int {
var hash: Int = MurmurHash.initialize()
hash = MurmurHash.update(hash, getActionType().rawValue)
hash = MurmurHash.update(hash, mode)
return MurmurHash.finish(hash, 2)
}
public var description: String {
return "pushMode(\(mode))"
}
}
public func ==(lhs: LexerPushModeAction, rhs: LexerPushModeAction) -> Bool {
if lhs === rhs {
return true
}
return lhs.mode == rhs.mode
}

View File

@ -1,73 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* Implements the {@code skip} lexer action by calling {@link org.antlr.v4.runtime.Lexer#skip}.
*
* <p>The {@code skip} command does not have any parameters, so this action is
* implemented as a singleton instance exposed by {@link #INSTANCE}.</p>
*
* @author Sam Harwell
* @since 4.2
*/
public final class LexerSkipAction: LexerAction, CustomStringConvertible {
/**
* Provides a singleton instance of this parameterless lexer action.
*/
public static let INSTANCE: LexerSkipAction = LexerSkipAction()
/**
* Constructs the singleton instance of the lexer {@code skip} command.
*/
private override init() {
}
/**
* {@inheritDoc}
* @return This method returns {@link org.antlr.v4.runtime.atn.LexerActionType#SKIP}.
*/
override
public func getActionType() -> LexerActionType {
return LexerActionType.skip
}
/**
* {@inheritDoc}
* @return This method returns {@code false}.
*/
override
public func isPositionDependent() -> Bool {
return false
}
/**
* {@inheritDoc}
*
* <p>This action is implemented by calling {@link org.antlr.v4.runtime.Lexer#skip}.</p>
*/
override
public func execute(_ lexer: Lexer) {
lexer.skip()
}
override
public var hashValue: Int {
var hash: Int = MurmurHash.initialize()
hash = MurmurHash.update(hash, getActionType().rawValue)
return MurmurHash.finish(hash, 1)
}
public var description: String {
return "skip"
}
}
public func ==(lhs: LexerSkipAction, rhs: LexerSkipAction) -> Bool {
return lhs === rhs
}

View File

@ -1,84 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* Implements the {@code type} lexer action by calling {@link org.antlr.v4.runtime.Lexer#setType}
* with the assigned type.
*
* @author Sam Harwell
* @since 4.2
*/
public class LexerTypeAction: LexerAction, CustomStringConvertible {
fileprivate final var type: Int
/**
* Constructs a new {@code type} action with the specified token type value.
* @param type The type to assign to the token using {@link org.antlr.v4.runtime.Lexer#setType}.
*/
public init(_ type: Int) {
self.type = type
}
/**
* Gets the type to assign to a token created by the lexer.
* @return The type to assign to a token created by the lexer.
*/
public func getType() -> Int {
return type
}
/**
* {@inheritDoc}
* @return This method returns {@link org.antlr.v4.runtime.atn.LexerActionType#TYPE}.
*/
public override func getActionType() -> LexerActionType {
return LexerActionType.type
}
/**
* {@inheritDoc}
* @return This method returns {@code false}.
*/
override
public func isPositionDependent() -> Bool {
return false
}
/**
* {@inheritDoc}
*
* <p>This action is implemented by calling {@link org.antlr.v4.runtime.Lexer#setType} with the
* value provided by {@link #getType}.</p>
*/
public override func execute(_ lexer: Lexer) {
lexer.setType(type)
}
override
public var hashValue: Int {
var hash: Int = MurmurHash.initialize()
hash = MurmurHash.update(hash, getActionType().rawValue)
hash = MurmurHash.update(hash, type)
return MurmurHash.finish(hash, 2)
}
public var description: String {
return "type(\(type))"
}
}
public func ==(lhs: LexerTypeAction, rhs: LexerTypeAction) -> Bool {
if lhs === rhs {
return true
}
return lhs.type == rhs.type
}

View File

@ -1,36 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This class represents profiling event information for tracking the lookahead
* depth required in order to make a prediction.
*
* @since 4.3
*/
public class LookaheadEventInfo: DecisionEventInfo {
/**
* Constructs a new instance of the {@link org.antlr.v4.runtime.atn.LookaheadEventInfo} class with
* the specified detailed lookahead information.
*
* @param decision The decision number
* @param configs The final configuration set containing the necessary
* information to determine the result of a prediction, or {@code null} if
* the final configuration set is not available
* @param input The input token stream
* @param startIndex The start index for the current prediction
* @param stopIndex The index at which the prediction was finally made
* @param fullCtx {@code true} if the current lookahead is part of an LL
* prediction; otherwise, {@code false} if the current lookahead is part of
* an SLL prediction
*/
public override init(_ decision: Int,
_ configs: ATNConfigSet?,
_ input: TokenStream, _ startIndex: Int, _ stopIndex: Int,
_ fullCtx: Bool) {
super.init(decision, configs, input, startIndex, stopIndex, fullCtx)
}
}

View File

@ -1,17 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/** Mark the end of a * or + loop. */
public final class LoopEndState: ATNState {
public var loopBackState: ATNState?
override
public func getStateType() -> Int {
return ATNState.LOOP_END
}
}

View File

@ -1,21 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
*
* @author Sam Harwell
*/
public class OrderedATNConfigSet: ATNConfigSet {
public init() {
super.init(true)
self.configLookup = LookupDictionary(type: LookupDictionaryType.ordered)
}
}

View File

@ -1,21 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/** Start of {@code (A|B|...)+} loop. Technically a decision state, but
* we don't use for code generation; somebody might need it, so I'm defining
* it for completeness. In reality, the {@link org.antlr.v4.runtime.atn.PlusLoopbackState} node is the
* real decision-making note for {@code A+}.
*/
public final class PlusBlockStartState: BlockStartState {
public var loopBackState: PlusLoopbackState?
override
public func getStateType() -> Int {
return ATNState.PLUS_BLOCK_START
}
}

View File

@ -1,18 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/** Decision state for {@code A+} and {@code (A|B)+}. It has two transitions:
* one to the loop back to start of the block and one to exit.
*/
public final class PlusLoopbackState: DecisionState {
override
public func getStateType() -> Int {
return ATNState.PLUS_LOOP_BACK
}
}

View File

@ -1,70 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This class represents profiling event information for semantic predicate
* evaluations which occur during prediction.
*
* @see org.antlr.v4.runtime.atn.ParserATNSimulator#evalSemanticContext
*
* @since 4.3
*/
public class PredicateEvalInfo: DecisionEventInfo {
/**
* The semantic context which was evaluated.
*/
public private(set) var semctx: SemanticContext
/**
* The alternative number for the decision which is guarded by the semantic
* context {@link #semctx}. Note that other ATN
* configurations may predict the same alternative which are guarded by
* other semantic contexts and/or {@link org.antlr.v4.runtime.atn.SemanticContext#NONE}.
*/
public private(set) var predictedAlt: Int
/**
* The result of evaluating the semantic context {@link #semctx}.
*/
public private(set) var evalResult: Bool
/**
* Constructs a new instance of the {@link org.antlr.v4.runtime.atn.PredicateEvalInfo} class with the
* specified detailed predicate evaluation information.
*
* @param decision The decision number
* @param input The input token stream
* @param startIndex The start index for the current prediction
* @param stopIndex The index at which the predicate evaluation was
* triggered. Note that the input stream may be reset to other positions for
* the actual evaluation of individual predicates.
* @param semctx The semantic context which was evaluated
* @param evalResult The results of evaluating the semantic context
* @param predictedAlt The alternative number for the decision which is
* guarded by the semantic context {@code semctx}. See {@link #predictedAlt}
* for more information.
* @param fullCtx {@code true} if the semantic context was
* evaluated during LL prediction; otherwise, {@code false} if the semantic
* context was evaluated during SLL prediction
*
* @see org.antlr.v4.runtime.atn.ParserATNSimulator#evalSemanticContext(org.antlr.v4.runtime.atn.SemanticContext, org.antlr.v4.runtime.ParserRuleContext, int, boolean)
* @see org.antlr.v4.runtime.atn.SemanticContext#eval(org.antlr.v4.runtime.Recognizer, org.antlr.v4.runtime.RuleContext)
*/
public init(_ decision: Int,
_ input: TokenStream,
_ startIndex: Int,
_ stopIndex: Int,
_ semctx: SemanticContext,
_ evalResult: Bool,
_ predictedAlt: Int,
_ fullCtx: Bool) {
self.semctx = semctx
self.evalResult = evalResult
self.predictedAlt = predictedAlt
super.init(decision, ATNConfigSet(), input, startIndex, stopIndex, fullCtx)
}
}

View File

@ -1,532 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This enumeration defines the prediction modes available in ANTLR 4 along with
* utility methods for analyzing configuration sets for conflicts and/or
* ambiguities.
*/
public enum PredictionMode {
/**
* The SLL(*) prediction mode. This prediction mode ignores the current
* parser context when making predictions. This is the fastest prediction
* mode, and provides correct results for many grammars. This prediction
* mode is more powerful than the prediction mode provided by ANTLR 3, but
* may result in syntax errors for grammar and input combinations which are
* not SLL.
*
* <p>
* When using this prediction mode, the parser will either return a correct
* parse tree (i.e. the same parse tree that would be returned with the
* {@link #LL} prediction mode), or it will report a syntax error. If a
* syntax error is encountered when using the {@link #SLL} prediction mode,
* it may be due to either an actual syntax error in the input or indicate
* that the particular combination of grammar and input requires the more
* powerful {@link #LL} prediction abilities to complete successfully.</p>
*
* <p>
* This prediction mode does not provide any guarantees for prediction
* behavior for syntactically-incorrect inputs.</p>
*/
case SLL
/**
* The LL(*) prediction mode. This prediction mode allows the current parser
* context to be used for resolving SLL conflicts that occur during
* prediction. This is the fastest prediction mode that guarantees correct
* parse results for all combinations of grammars with syntactically correct
* inputs.
*
* <p>
* When using this prediction mode, the parser will make correct decisions
* for all syntactically-correct grammar and input combinations. However, in
* cases where the grammar is truly ambiguous this prediction mode might not
* report a precise answer for <em>exactly which</em> alternatives are
* ambiguous.</p>
*
* <p>
* This prediction mode does not provide any guarantees for prediction
* behavior for syntactically-incorrect inputs.</p>
*/
case LL
/**
* The LL(*) prediction mode with exact ambiguity detection. In addition to
* the correctness guarantees provided by the {@link #LL} prediction mode,
* this prediction mode instructs the prediction algorithm to determine the
* complete and exact set of ambiguous alternatives for every ambiguous
* decision encountered while parsing.
*
* <p>
* This prediction mode may be used for diagnosing ambiguities during
* grammar development. Due to the performance overhead of calculating sets
* of ambiguous alternatives, this prediction mode should be avoided when
* the exact results are not necessary.</p>
*
* <p>
* This prediction mode does not provide any guarantees for prediction
* behavior for syntactically-incorrect inputs.</p>
*/
case LL_EXACT_AMBIG_DETECTION
/**
* Computes the SLL prediction termination condition.
*
* <p>
* This method computes the SLL prediction termination condition for both of
* the following cases.</p>
*
* <ul>
* <li>The usual SLL+LL fallback upon SLL conflict</li>
* <li>Pure SLL without LL fallback</li>
* </ul>
*
* <p><strong>COMBINED SLL+LL PARSING</strong></p>
*
* <p>When LL-fallback is enabled upon SLL conflict, correct predictions are
* ensured regardless of how the termination condition is computed by this
* method. Due to the substantially higher cost of LL prediction, the
* prediction should only fall back to LL when the additional lookahead
* cannot lead to a unique SLL prediction.</p>
*
* <p>Assuming combined SLL+LL parsing, an SLL configuration set with only
* conflicting subsets should fall back to full LL, even if the
* configuration sets don't resolve to the same alternative (e.g.
* {@code {1,2}} and {@code {3,4}}. If there is at least one non-conflicting
* configuration, SLL could continue with the hopes that more lookahead will
* resolve via one of those non-conflicting configurations.</p>
*
* <p>Here's the prediction termination rule them: SLL (for SLL+LL parsing)
* stops when it sees only conflicting configuration subsets. In contrast,
* full LL keeps going when there is uncertainty.</p>
*
* <p><strong>HEURISTIC</strong></p>
*
* <p>As a heuristic, we stop prediction when we see any conflicting subset
* unless we see a state that only has one alternative associated with it.
* The single-alt-state thing lets prediction continue upon rules like
* (otherwise, it would admit defeat too soon):</p>
*
* <p>{@code [12|1|[], 6|2|[], 12|2|[]]. s : (ID | ID ID?) ';' ;}</p>
*
* <p>When the ATN simulation reaches the state before {@code ';'}, it has a
* DFA state that looks like: {@code [12|1|[], 6|2|[], 12|2|[]]}. Naturally
* {@code 12|1|[]} and {@code 12|2|[]} conflict, but we cannot stop
* processing this node because alternative to has another way to continue,
* via {@code [6|2|[]]}.</p>
*
* <p>It also let's us continue for this rule:</p>
*
* <p>{@code [1|1|[], 1|2|[], 8|3|[]] a : A | A | A B ;}</p>
*
* <p>After matching input A, we reach the stop state for rule A, state 1.
* State 8 is the state right before B. Clearly alternatives 1 and 2
* conflict and no amount of further lookahead will separate the two.
* However, alternative 3 will be able to continue and so we do not stop
* working on this state. In the previous example, we're concerned with
* states associated with the conflicting alternatives. Here alt 3 is not
* associated with the conflicting configs, but since we can continue
* looking for input reasonably, don't declare the state done.</p>
*
* <p><strong>PURE SLL PARSING</strong></p>
*
* <p>To handle pure SLL parsing, all we have to do is make sure that we
* combine stack contexts for configurations that differ only by semantic
* predicate. From there, we can do the usual SLL termination heuristic.</p>
*
* <p><strong>PREDICATES IN SLL+LL PARSING</strong></p>
*
* <p>SLL decisions don't evaluate predicates until after they reach DFA stop
* states because they need to create the DFA cache that works in all
* semantic situations. In contrast, full LL evaluates predicates collected
* during start state computation so it can ignore predicates thereafter.
* This means that SLL termination detection can totally ignore semantic
* predicates.</p>
*
* <p>Implementation-wise, {@link org.antlr.v4.runtime.atn.ATNConfigSet} combines stack contexts but not
* semantic predicate contexts so we might see two configurations like the
* following.</p>
*
* <p>{@code (s, 1, x, {}), (s, 1, x', {p})}</p>
*
* <p>Before testing these configurations against others, we have to merge
* {@code x} and {@code x'} (without modifying the existing configurations).
* For example, we test {@code (x+x')==x''} when looking for conflicts in
* the following configurations.</p>
*
* <p>{@code (s, 1, x, {}), (s, 1, x', {p}), (s, 2, x'', {})}</p>
*
* <p>If the configuration set has predicates (as indicated by
* {@link org.antlr.v4.runtime.atn.ATNConfigSet#hasSemanticContext}), this algorithm makes a copy of
* the configurations to strip out all of the predicates so that a standard
* {@link org.antlr.v4.runtime.atn.ATNConfigSet} will merge everything ignoring predicates.</p>
*/
public static func hasSLLConflictTerminatingPrediction(_ mode: PredictionMode,_ configs: ATNConfigSet) throws -> Bool {
var configs = configs
/* Configs in rule stop states indicate reaching the end of the decision
* rule (local context) or end of start rule (full context). If all
* configs meet this condition, then none of the configurations is able
* to match additional input so we terminate prediction.
*/
if allConfigsInRuleStopStates(configs) {
return true
}
// pure SLL mode parsing
if mode == PredictionMode.SLL {
// Don't bother with combining configs from different semantic
// contexts if we can fail over to full LL; costs more time
// since we'll often fail over anyway.
if configs.hasSemanticContext {
// dup configs, tossing out semantic predicates
configs = try configs.dupConfigsWithoutSemanticPredicates()
}
// now we have combined contexts for configs with dissimilar preds
}
// pure SLL or combined SLL+LL mode parsing
let altsets: Array<BitSet> = try getConflictingAltSubsets(configs)
let heuristic: Bool =
try hasConflictingAltSet(altsets) && !hasStateAssociatedWithOneAlt(configs)
return heuristic
}
/**
* Checks if any configuration in {@code configs} is in a
* {@link org.antlr.v4.runtime.atn.RuleStopState}. Configurations meeting this condition have reached
* the end of the decision rule (local context) or end of start rule (full
* context).
*
* @param configs the configuration set to test
* @return {@code true} if any configuration in {@code configs} is in a
* {@link org.antlr.v4.runtime.atn.RuleStopState}, otherwise {@code false}
*/
public static func hasConfigInRuleStopState(_ configs: ATNConfigSet) -> Bool {
return configs.hasConfigInRuleStopState
}
/**
* Checks if all configurations in {@code configs} are in a
* {@link org.antlr.v4.runtime.atn.RuleStopState}. Configurations meeting this condition have reached
* the end of the decision rule (local context) or end of start rule (full
* context).
*
* @param configs the configuration set to test
* @return {@code true} if all configurations in {@code configs} are in a
* {@link org.antlr.v4.runtime.atn.RuleStopState}, otherwise {@code false}
*/
public static func allConfigsInRuleStopStates(_ configs: ATNConfigSet) -> Bool {
return configs.allConfigsInRuleStopStates
}
/**
* Full LL prediction termination.
*
* <p>Can we stop looking ahead during ATN simulation or is there some
* uncertainty as to which alternative we will ultimately pick, after
* consuming more input? Even if there are partial conflicts, we might know
* that everything is going to resolve to the same minimum alternative. That
* means we can stop since no more lookahead will change that fact. On the
* other hand, there might be multiple conflicts that resolve to different
* minimums. That means we need more look ahead to decide which of those
* alternatives we should predict.</p>
*
* <p>The basic idea is to split the set of configurations {@code C}, into
* conflicting subsets {@code (s, _, ctx, _)} and singleton subsets with
* non-conflicting configurations. Two configurations conflict if they have
* identical {@link org.antlr.v4.runtime.atn.ATNConfig#state} and {@link org.antlr.v4.runtime.atn.ATNConfig#context} values
* but different {@link org.antlr.v4.runtime.atn.ATNConfig#alt} value, e.g. {@code (s, i, ctx, _)}
* and {@code (s, j, ctx, _)} for {@code i!=j}.</p>
*
* <p>Reduce these configuration subsets to the set of possible alternatives.
* You can compute the alternative subsets in one pass as follows:</p>
*
* <p>{@code A_s,ctx = {i | (s, i, ctx, _)}} for each configuration in
* {@code C} holding {@code s} and {@code ctx} fixed.</p>
*
* <p>Or in pseudo-code, for each configuration {@code c} in {@code C}:</p>
*
* <pre>
* map[c] U= c.{@link org.antlr.v4.runtime.atn.ATNConfig#alt alt} # map hash/equals uses s and x, not
* alt and not pred
* </pre>
*
* <p>The values in {@code map} are the set of {@code A_s,ctx} sets.</p>
*
* <p>If {@code |A_s,ctx|=1} then there is no conflict associated with
* {@code s} and {@code ctx}.</p>
*
* <p>Reduce the subsets to singletons by choosing a minimum of each subset. If
* the union of these alternative subsets is a singleton, then no amount of
* more lookahead will help us. We will always pick that alternative. If,
* however, there is more than one alternative, then we are uncertain which
* alternative to predict and must continue looking for resolution. We may
* or may not discover an ambiguity in the future, even if there are no
* conflicting subsets this round.</p>
*
* <p>The biggest sin is to terminate early because it means we've made a
* decision but were uncertain as to the eventual outcome. We haven't used
* enough lookahead. On the other hand, announcing a conflict too late is no
* big deal; you will still have the conflict. It's just inefficient. It
* might even look until the end of file.</p>
*
* <p>No special consideration for semantic predicates is required because
* predicates are evaluated on-the-fly for full LL prediction, ensuring that
* no configuration contains a semantic context during the termination
* check.</p>
*
* <p><strong>CONFLICTING CONFIGS</strong></p>
*
* <p>Two configurations {@code (s, i, x)} and {@code (s, j, x')}, conflict
* when {@code i!=j} but {@code x=x'}. Because we merge all
* {@code (s, i, _)} configurations together, that means that there are at
* most {@code n} configurations associated with state {@code s} for
* {@code n} possible alternatives in the decision. The merged stacks
* complicate the comparison of configuration contexts {@code x} and
* {@code x'}. Sam checks to see if one is a subset of the other by calling
* merge and checking to see if the merged result is either {@code x} or
* {@code x'}. If the {@code x} associated with lowest alternative {@code i}
* is the superset, then {@code i} is the only possible prediction since the
* others resolve to {@code min(i)} as well. However, if {@code x} is
* associated with {@code j>i} then at least one stack configuration for
* {@code j} is not in conflict with alternative {@code i}. The algorithm
* should keep going, looking for more lookahead due to the uncertainty.</p>
*
* <p>For simplicity, I'm doing a equality check between {@code x} and
* {@code x'} that lets the algorithm continue to consume lookahead longer
* than necessary. The reason I like the equality is of course the
* simplicity but also because that is the test you need to detect the
* alternatives that are actually in conflict.</p>
*
* <p><strong>CONTINUE/STOP RULE</strong></p>
*
* <p>Continue if union of resolved alternative sets from non-conflicting and
* conflicting alternative subsets has more than one alternative. We are
* uncertain about which alternative to predict.</p>
*
* <p>The complete set of alternatives, {@code [i for (_,i,_)]}, tells us which
* alternatives are still in the running for the amount of input we've
* consumed at this point. The conflicting sets let us to strip away
* configurations that won't lead to more states because we resolve
* conflicts to the configuration with a minimum alternate for the
* conflicting set.</p>
*
* <p><strong>CASES</strong></p>
*
* <ul>
*
* <li>no conflicts and more than 1 alternative in set =&gt; continue</li>
*
* <li> {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s, 3, z)},
* {@code (s', 1, y)}, {@code (s', 2, y)} yields non-conflicting set
* {@code {3}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} =
* {@code {1,3}} =&gt; continue
* </li>
*
* <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)},
* {@code (s', 2, y)}, {@code (s'', 1, z)} yields non-conflicting set
* {@code {1}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} =
* {@code {1}} =&gt; stop and predict 1</li>
*
* <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)},
* {@code (s', 2, y)} yields conflicting, reduced sets {@code {1}} U
* {@code {1}} = {@code {1}} =&gt; stop and predict 1, can announce
* ambiguity {@code {1,2}}</li>
*
* <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 2, y)},
* {@code (s', 3, y)} yields conflicting, reduced sets {@code {1}} U
* {@code {2}} = {@code {1,2}} =&gt; continue</li>
*
* <li>{@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 3, y)},
* {@code (s', 4, y)} yields conflicting, reduced sets {@code {1}} U
* {@code {3}} = {@code {1,3}} =&gt; continue</li>
*
* </ul>
*
* <p><strong>EXACT AMBIGUITY DETECTION</strong></p>
*
* <p>If all states report the same conflicting set of alternatives, then we
* know we have the exact ambiguity set.</p>
*
* <p><code>|A_<em>i</em>|&gt;1</code> and
* <code>A_<em>i</em> = A_<em>j</em></code> for all <em>i</em>, <em>j</em>.</p>
*
* <p>In other words, we continue examining lookahead until all {@code A_i}
* have more than one alternative and all {@code A_i} are the same. If
* {@code A={{1,2}, {1,3}}}, then regular LL prediction would terminate
* because the resolved set is {@code {1}}. To determine what the real
* ambiguity is, we have to know whether the ambiguity is between one and
* two or one and three so we keep going. We can only stop prediction when
* we need exact ambiguity detection when the sets look like
* {@code A={{1,2}}} or {@code {{1,2},{1,2}}}, etc...</p>
*/
public static func resolvesToJustOneViableAlt(_ altsets: Array<BitSet>) throws -> Int {
return try getSingleViableAlt(altsets)
}
/**
* Determines if every alternative subset in {@code altsets} contains more
* than one alternative.
*
* @param altsets a collection of alternative subsets
* @return {@code true} if every {@link java.util.BitSet} in {@code altsets} has
* {@link java.util.BitSet#cardinality cardinality} &gt; 1, otherwise {@code false}
*/
public static func allSubsetsConflict(_ altsets: Array<BitSet>) -> Bool {
return !hasNonConflictingAltSet(altsets)
}
/**
* Determines if any single alternative subset in {@code altsets} contains
* exactly one alternative.
*
* @param altsets a collection of alternative subsets
* @return {@code true} if {@code altsets} contains a {@link java.util.BitSet} with
* {@link java.util.BitSet#cardinality cardinality} 1, otherwise {@code false}
*/
public static func hasNonConflictingAltSet(_ altsets: Array<BitSet>) -> Bool {
for alts: BitSet in altsets {
if alts.cardinality() == 1 {
return true
}
}
return false
}
/**
* Determines if any single alternative subset in {@code altsets} contains
* more than one alternative.
*
* @param altsets a collection of alternative subsets
* @return {@code true} if {@code altsets} contains a {@link java.util.BitSet} with
* {@link java.util.BitSet#cardinality cardinality} &gt; 1, otherwise {@code false}
*/
public static func hasConflictingAltSet(_ altsets: Array<BitSet>) -> Bool {
for alts: BitSet in altsets {
if alts.cardinality() > 1 {
return true
}
}
return false
}
/**
* Determines if every alternative subset in {@code altsets} is equivalent.
*
* @param altsets a collection of alternative subsets
* @return {@code true} if every member of {@code altsets} is equal to the
* others, otherwise {@code false}
*/
public static func allSubsetsEqual(_ altsets: Array<BitSet>) -> Bool {
let first: BitSet = altsets[0]
for it in altsets {
if it != first {
return false
}
}
return true
}
/**
* Returns the unique alternative predicted by all alternative subsets in
* {@code altsets}. If no such alternative exists, this method returns
* {@link org.antlr.v4.runtime.atn.ATN#INVALID_ALT_NUMBER}.
*
* @param altsets a collection of alternative subsets
*/
public static func getUniqueAlt(_ altsets: Array<BitSet>) throws -> Int {
let all: BitSet = getAlts(altsets)
if all.cardinality() == 1 {
return try all.nextSetBit(0)
}
return ATN.INVALID_ALT_NUMBER
}
/**
* Gets the complete set of represented alternatives for a collection of
* alternative subsets. This method returns the union of each {@link java.util.BitSet}
* in {@code altsets}.
*
* @param altsets a collection of alternative subsets
* @return the set of represented alternatives in {@code altsets}
*/
public static func getAlts(_ altsets: Array<BitSet>) -> BitSet {
let all: BitSet = BitSet()
for alts: BitSet in altsets {
all.or(alts)
}
return all
}
/** Get union of all alts from configs. @since 4.5.1 */
public static func getAlts(_ configs: ATNConfigSet) throws -> BitSet {
return try configs.getAltBitSet()
}
/**
* This function gets the conflicting alt subsets from a configuration set.
* For each configuration {@code c} in {@code configs}:
*
* <pre>
* map[c] U= c.{@link org.antlr.v4.runtime.atn.ATNConfig#alt alt} # map hash/equals uses s and x, not
* alt and not pred
* </pre>
*/
public static func getConflictingAltSubsets(_ configs: ATNConfigSet) throws -> Array<BitSet> {
return try configs.getConflictingAltSubsets()
}
/**
* Get a map from state to alt subset from a configuration set. For each
* configuration {@code c} in {@code configs}:
*
* <pre>
* map[c.{@link org.antlr.v4.runtime.atn.ATNConfig#state state}] U= c.{@link org.antlr.v4.runtime.atn.ATNConfig#alt alt}
* </pre>
*/
public static func getStateToAltMap(_ configs: ATNConfigSet) throws -> HashMap<ATNState, BitSet> {
return try configs.getStateToAltMap()
}
public static func hasStateAssociatedWithOneAlt(_ configs: ATNConfigSet) throws -> Bool {
let x: HashMap<ATNState, BitSet> = try getStateToAltMap(configs)
let values = x.values
for alts: BitSet in values {
if alts.cardinality() == 1 {
return true
}
}
return false
}
public static func getSingleViableAlt(_ altsets: Array<BitSet>) throws -> Int {
let viableAlts: BitSet = BitSet()
for alts: BitSet in altsets {
let minAlt: Int = try alts.nextSetBit(0)
try viableAlts.set(minAlt)
if viableAlts.cardinality() > 1 {
// more than 1 viable alt
return ATN.INVALID_ALT_NUMBER
}
}
return try viableAlts.nextSetBit(0)
}
}

View File

@ -1,21 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/** The last node in the ATN for a rule, unless that rule is the start symbol.
* In that case, there is one transition to EOF. Later, we might encode
* references to all calls to this rule to compute FOLLOW sets for
* error handling.
*/
public final class RuleStopState: ATNState {
override
public func getStateType() -> Int {
return ATNState.RULE_STOP
}
}

View File

@ -1,16 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/** The block that begins a closure loop. */
public final class StarBlockStartState: BlockStartState {
override
public func getStateType() -> Int {
return ATNState.STAR_BLOCK_START
}
}

View File

@ -1,26 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
public final class StarLoopEntryState: DecisionState {
public var loopBackState: StarLoopbackState?
/**
* Indicates whether this state can benefit from a precedence DFA during SLL
* decision making.
*
* <p>This is a computed property that is calculated during ATN deserialization
* and stored for use in {@link org.antlr.v4.runtime.atn.ParserATNSimulator} and
* {@link org.antlr.v4.runtime.ParserInterpreter}.</p>
*
* @see org.antlr.v4.runtime.dfa.DFA#isPrecedenceDfa()
*/
public var precedenceRuleDecision: Bool = false
override
public func getStateType() -> Int {
return ATNState.STAR_LOOP_ENTRY
}
}

View File

@ -1,15 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/** The Tokens rule start state linking to each lexer rule start state */
public final class TokensStartState: DecisionState {
override
public func getStateType() -> Int {
return ATNState.TOKEN_START
}
}

View File

@ -1,164 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/** A DFA state represents a set of possible ATN configurations.
* As Aho, Sethi, Ullman p. 117 says "The DFA uses its state
* to keep track of all possible states the ATN can be in after
* reading each input symbol. That is to say, after reading
* input a1a2..an, the DFA is in a state that represents the
* subset T of the states of the ATN that are reachable from the
* ATN's start state along some path labeled a1a2..an."
* In conventional NFA&rarr;DFA conversion, therefore, the subset T
* would be a bitset representing the set of states the
* ATN could be in. We need to track the alt predicted by each
* state as well, however. More importantly, we need to maintain
* a stack of states, tracking the closure operations as they
* jump from rule to rule, emulating rule invocations (method calls).
* I have to add a stack to simulate the proper lookahead sequences for
* the underlying LL grammar from which the ATN was derived.
*
* <p>I use a set of ATNConfig objects not simple states. An ATNConfig
* is both a state (ala normal conversion) and a RuleContext describing
* the chain of rules (if any) followed to arrive at that state.</p>
*
* <p>A DFA state may have multiple references to a particular state,
* but with different ATN contexts (with same or different alts)
* meaning that state was reached via a different set of rule invocations.</p>
*/
public class DFAState: Hashable, CustomStringConvertible {
public var stateNumber: Int = -1
public var configs: ATNConfigSet = ATNConfigSet()
/** {@code edges[symbol]} points to target of symbol. Shift up by 1 so (-1)
* {@link org.antlr.v4.runtime.Token#EOF} maps to {@code edges[0]}.
*/
public var edges: [DFAState?]!
public var isAcceptState: Bool = false
/** if accept state, what ttype do we match or alt do we predict?
* This is set to {@link org.antlr.v4.runtime.atn.ATN#INVALID_ALT_NUMBER} when {@link #predicates}{@code !=null} or
* {@link #requiresFullContext}.
*/
public var prediction: Int! = 0
public var lexerActionExecutor: LexerActionExecutor!
/**
* Indicates that this state was created during SLL prediction that
* discovered a conflict between the configurations in the state. Future
* {@link org.antlr.v4.runtime.atn.ParserATNSimulator#execATN} invocations immediately jumped doing
* full context prediction if this field is true.
*/
public var requiresFullContext: Bool = false
/** During SLL parsing, this is a list of predicates associated with the
* ATN configurations of the DFA state. When we have predicates,
* {@link #requiresFullContext} is {@code false} since full context prediction evaluates predicates
* on-the-fly. If this is not null, then {@link #prediction} is
* {@link org.antlr.v4.runtime.atn.ATN#INVALID_ALT_NUMBER}.
*
* <p>We only use these for non-{@link #requiresFullContext} but conflicting states. That
* means we know from the context (it's $ or we don't dip into outer
* context) that it's an ambiguity not a conflict.</p>
*
* <p>This list is computed by {@link org.antlr.v4.runtime.atn.ParserATNSimulator#predicateDFAState}.</p>
*/
public var predicates: [PredPrediction]?
/** Map a predicate to a predicted alternative. */
public class PredPrediction: CustomStringConvertible {
public final var pred: SemanticContext
// never null; at least SemanticContext.NONE
public final var alt: Int
public init(_ pred: SemanticContext, _ alt: Int) {
self.alt = alt
self.pred = pred
}
public var description: String {
return "(\(pred),\(alt))"
}
}
public init() {
}
public init(_ stateNumber: Int) {
self.stateNumber = stateNumber
}
public init(_ configs: ATNConfigSet) {
self.configs = configs
}
/** Get the set of all alts mentioned by all ATN configurations in this
* DFA state.
*/
public func getAltSet() -> Set<Int>? {
let alts = configs.getAltSet()
return alts
}
public var hashValue: Int {
var hash: Int = MurmurHash.initialize(7)
hash = MurmurHash.update(hash, configs.hashValue)
hash = MurmurHash.finish(hash, 1)
return hash
}
/**
* Two {@link org.antlr.v4.runtime.dfa.DFAState} instances are equal if their ATN configuration sets
* are the same. This method is used to see if a state already exists.
*
* <p>Because the number of alternatives and number of ATN configurations are
* finite, there is a finite number of DFA states that can be processed.
* This is necessary to show that the algorithm terminates.</p>
*
* <p>Cannot test the DFA state numbers here because in
* {@link org.antlr.v4.runtime.atn.ParserATNSimulator#addDFAState} we need to know if any other state
* exists that has this exact set of ATN configurations. The
* {@link #stateNumber} is irrelevant.</p>
*/
public var description: String {
let buf: StringBuilder = StringBuilder()
buf.append(stateNumber).append(":").append(configs)
if isAcceptState {
buf.append("=>")
if let predicates = predicates {
buf.append(predicates.map({ $0.description }))
} else {
buf.append(prediction)
}
}
return buf.toString()
}
}
public func ==(lhs: DFAState, rhs: DFAState) -> Bool {
if lhs === rhs {
return true
}
let sameSet: Bool = lhs.configs == rhs.configs
return sameSet
}

View File

@ -1,170 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* A generic set of integers.
*
* @see org.antlr.v4.runtime.misc.IntervalSet
*/
public protocol IntSet {
/**
* Adds the specified value to the current set.
*
* @param el the value to add
*
* @exception IllegalStateException if the current set is read-only
*/
func add(_ el: Int) throws
/**
* Modify the current {@link org.antlr.v4.runtime.misc.IntSet} object to contain all elements that are
* present in itself, the specified {@code set}, or both.
*
* @param set The set to add to the current set. A {@code null} argument is
* treated as though it were an empty set.
* @return {@code this} (to support chained calls)
*
* @exception IllegalStateException if the current set is read-only
*/
func addAll(_ set: IntSet?) throws -> IntSet
/**
* Return a new {@link org.antlr.v4.runtime.misc.IntSet} object containing all elements that are
* present in both the current set and the specified set {@code a}.
*
* @param a The set to intersect with the current set. A {@code null}
* argument is treated as though it were an empty set.
* @return A new {@link org.antlr.v4.runtime.misc.IntSet} instance containing the intersection of the
* current set and {@code a}. The value {@code null} may be returned in
* place of an empty result set.
*/
func and(_ a: IntSet?) throws -> IntSet?
/**
* Return a new {@link org.antlr.v4.runtime.misc.IntSet} object containing all elements that are
* present in {@code elements} but not present in the current set. The
* following expressions are equivalent for input non-null {@link org.antlr.v4.runtime.misc.IntSet}
* instances {@code x} and {@code y}.
*
* <ul>
* <li>{@code x.complement(y)}</li>
* <li>{@code y.subtract(x)}</li>
* </ul>
*
* @param elements The set to compare with the current set. A {@code null}
* argument is treated as though it were an empty set.
* @return A new {@link org.antlr.v4.runtime.misc.IntSet} instance containing the elements present in
* {@code elements} but not present in the current set. The value
* {@code null} may be returned in place of an empty result set.
*/
func complement(_ elements: IntSet?) throws -> IntSet?
/**
* Return a new {@link org.antlr.v4.runtime.misc.IntSet} object containing all elements that are
* present in the current set, the specified set {@code a}, or both.
*
* <p>
* This method is similar to {@link #addAll(org.antlr.v4.runtime.misc.IntSet)}, but returns a new
* {@link org.antlr.v4.runtime.misc.IntSet} instance instead of modifying the current set.</p>
*
* @param a The set to union with the current set. A {@code null} argument
* is treated as though it were an empty set.
* @return A new {@link org.antlr.v4.runtime.misc.IntSet} instance containing the union of the current
* set and {@code a}. The value {@code null} may be returned in place of an
* empty result set.
*/
func or(_ a: IntSet) throws -> IntSet
/**
* Return a new {@link org.antlr.v4.runtime.misc.IntSet} object containing all elements that are
* present in the current set but not present in the input set {@code a}.
* The following expressions are equivalent for input non-null
* {@link org.antlr.v4.runtime.misc.IntSet} instances {@code x} and {@code y}.
*
* <ul>
* <li>{@code y.subtract(x)}</li>
* <li>{@code x.complement(y)}</li>
* </ul>
*
* @param a The set to compare with the current set. A {@code null}
* argument is treated as though it were an empty set.
* @return A new {@link org.antlr.v4.runtime.misc.IntSet} instance containing the elements present in
* {@code elements} but not present in the current set. The value
* {@code null} may be returned in place of an empty result set.
*/
func subtract(_ a: IntSet?) throws -> IntSet
/**
* Return the total number of elements represented by the current set.
*
* @return the total number of elements represented by the current set,
* regardless of the manner in which the elements are stored.
*/
func size() -> Int
/**
* Returns {@code true} if this set contains no elements.
*
* @return {@code true} if the current set contains no elements; otherwise,
* {@code false}.
*/
func isNil() -> Bool
/**
* {@inheritDoc}
*/
//func equals(obj : AnyObject) -> Bool;
/**
* Returns the single value contained in the set, if {@link #size} is 1;
* otherwise, returns {@link org.antlr.v4.runtime.Token#INVALID_TYPE}.
*
* @return the single value contained in the set, if {@link #size} is 1;
* otherwise, returns {@link org.antlr.v4.runtime.Token#INVALID_TYPE}.
*/
func getSingleElement() -> Int
/**
* Returns {@code true} if the set contains the specified element.
*
* @param el The element to check for.
* @return {@code true} if the set contains {@code el}; otherwise {@code false}.
*/
func contains(_ el: Int) -> Bool
/**
* Removes the specified value from the current set. If the current set does
* not contain the element, no changes are made.
*
* @param el the value to remove
*
* @exception IllegalStateException if the current set is read-only
*/
func remove(_ el: Int) throws
/**
* Return a list containing the elements represented by the current set. The
* list is returned in ascending numerical order.
*
* @return A list containing all element present in the current set, sorted
* in ascending numerical order.
*/
func toList() -> Array<Int>
/**
* {@inheritDoc}
*/
func toString() -> String
}

View File

@ -1,33 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
//
// IntStreamExtension.swift
// Antlr.swift
//
// Created by janyou on 15/9/3.
//
import Foundation
extension IntStream {
/**
* The value returned by {@link #LA LA()} when the end of the stream is
* reached.
*/
public static var EOF: Int {
return -1
}
/**
* The value returned by {@link #getSourceName} when the actual name of the
* underlying source is not known.
*/
public static var UNKNOWN_SOURCE_NAME: String {
return "<unknown>"
}
}

View File

@ -1,68 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
//
// TokenExtension.swift
// Antlr.swift
//
// Created by janyou on 15/9/4.
//
import Foundation
extension Token {
static public var INVALID_TYPE: Int {
return 0
}
/** During lookahead operations, this "token" signifies we hit rule end ATN state
* and did not follow it despite needing to.
*/
static public var EPSILON: Int {
return -2
}
static public var MIN_USER_TOKEN_TYPE: Int {
return 1
}
static public var EOF: Int {
return -1
}
//IntStream.EOF
/** All tokens go to the parser (unless skip() is called in that rule)
* on a particular "channel". The parser tunes to a particular channel
* so that whitespace etc... can go to the parser on a "hidden" channel.
*/
static public var DEFAULT_CHANNEL: Int {
return 0
}
/** Anything on different channel than DEFAULT_CHANNEL is not parsed
* by parser.
*/
static public var HIDDEN_CHANNEL: Int {
return 1
}
/**
* This is the minimum constant value which can be assigned to a
* user-defined token channel.
*
* <p>
* The non-negative numbers less than {@link #MIN_USER_CHANNEL_VALUE} are
* assigned to the predefined channels {@link #DEFAULT_CHANNEL} and
* {@link #HIDDEN_CHANNEL}.</p>
*
* @see org.antlr.v4.runtime.Token#getChannel()
*/
static public var MIN_USER_CHANNEL_VALUE: Int {
return 2
}
}

View File

@ -1,39 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
//
// Antlr4Tests.swift
// Antlr4Tests
//
// Created by janyou on 15/10/13.
//
import XCTest
class Antlr4Tests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}

View File

@ -1,40 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
//
// BaseTest.swift
// Antlr4
//
// Created by janyou on 15/10/13.
//
import XCTest
@testable import Antlr4
class BaseTest: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}

View File

@ -1,68 +0,0 @@
/* Copyright (c) 2012-2016 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.
*/
//
// HashMapTest.swift
// Antlr4
//
// Created by janyou on 16/4/8.
//
import XCTest
import Antlr4
class HashMapTest: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
let map = HashMap<String,Int>()
map["a"] = 1
map["b"] = 2
XCTAssert(map["a"] == 1)
XCTAssert(map["b"] == 2)
for (k,v) in map {
print("\(k) : \(v)")
}
for k in map.keys {
print("key:\(k) ")
}
for v in map.values {
print("value:\(v) ")
}
print("isEmpty:\(map.isEmpty) ")
XCTAssert(map.isEmpty == false)
print("\(map)")
map.remove("a")
print("\(map.count)")
map["b"] = nil
print("\(map.count)")
for (k,v) in map {
print("\(k) : \(v)")
}
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}

View File

@ -0,0 +1,9 @@
// Copyright (c) 2012-2016 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 PackageDescription
let package = Package(
name: "Antlr4"
)

View File

@ -0,0 +1,162 @@
/// Copyright (c) 2012-2016 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.
/// How to emit recognition errors.
public protocol ANTLRErrorListener: class {
/// Upon syntax error, notify any interested parties. This is not how to
/// recover from errors or compute error messages. {@link org.antlr.v4.runtime.ANTLRErrorStrategy}
/// specifies how to recover from syntax errors and how to compute error
/// messages. This listener's job is simply to emit a computed message,
/// though it has enough information to create its own message in many cases.
///
/// <p>The {@link org.antlr.v4.runtime.RecognitionException} is non-null for all syntax errors except
/// when we discover mismatched token errors that we can recover from
/// in-line, without returning from the surrounding rule (via the single
/// token insertion and deletion mechanism).</p>
///
/// - parameter recognizer:
/// What parser got the error. From this
/// object, you can access the context as well
/// as the input stream.
/// - parameter offendingSymbol:
/// The offending token in the input token
/// stream, unless recognizer is a lexer (then it's null). If
/// no viable alternative error, {@code e} has token at which we
/// started production for the decision.
/// - parameter line:
/// The line number in the input where the error occurred.
/// - parameter charPositionInLine:
/// The character position within that line where the error occurred.
/// - parameter msg:
/// The message to emit.
/// - parameter e:
/// The exception generated by the parser that led to
/// the reporting of an error. It is null in the case where
/// the parser was able to recover in line without exiting the
/// surrounding rule.
func syntaxError<T:ATNSimulator>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int,
_ charPositionInLine: Int,
_ msg: String,
_ e: AnyObject?// RecognitionException?
)
/// This method is called by the parser when a full-context prediction
/// results in an ambiguity.
///
/// <p>Each full-context prediction which does not result in a syntax error
/// will call either {@link #reportContextSensitivity} or
/// {@link #reportAmbiguity}.</p>
///
/// <p>When {@code ambigAlts} is not null, it contains the set of potentially
/// viable alternatives identified by the prediction algorithm. When
/// {@code ambigAlts} is null, use {@link org.antlr.v4.runtime.atn.ATNConfigSet#getAlts} to obtain the
/// represented alternatives from the {@code configs} argument.</p>
///
/// <p>When {@code exact} is {@code true}, <em>all</em> of the potentially
/// viable alternatives are truly viable, i.e. this is reporting an exact
/// ambiguity. When {@code exact} is {@code false}, <em>at least two</em> of
/// the potentially viable alternatives are viable for the current input, but
/// the prediction algorithm terminated as soon as it determined that at
/// least the <em>minimum</em> potentially viable alternative is truly
/// viable.</p>
///
/// <p>When the {@link org.antlr.v4.runtime.atn.PredictionMode#LL_EXACT_AMBIG_DETECTION} prediction
/// mode is used, the parser is required to identify exact ambiguities so
/// {@code exact} will always be {@code true}.</p>
///
/// <p>This method is not used by lexers.</p>
///
/// - parameter recognizer: the parser instance
/// - parameter dfa: the DFA for the current decision
/// - parameter startIndex: the input index where the decision started
/// - parameter stopIndex: the input input where the ambiguity was identified
/// - parameter exact: {@code true} if the ambiguity is exactly known, otherwise
/// {@code false}. This is always {@code true} when
/// {@link org.antlr.v4.runtime.atn.PredictionMode#LL_EXACT_AMBIG_DETECTION} is used.
/// - parameter ambigAlts: the potentially ambiguous alternatives, or {@code null}
/// to indicate that the potentially ambiguous alternatives are the complete
/// set of represented alternatives in {@code configs}
/// - parameter configs: the ATN configuration set where the ambiguity was
/// identified
func reportAmbiguity(_ recognizer: Parser,
_ dfa: DFA,
_ startIndex: Int,
_ stopIndex: Int,
_ exact: Bool,
_ ambigAlts: BitSet,
_ configs: ATNConfigSet) throws
/// This method is called when an SLL conflict occurs and the parser is about
/// to use the full context information to make an LL decision.
///
/// <p>If one or more configurations in {@code configs} contains a semantic
/// predicate, the predicates are evaluated before this method is called. The
/// subset of alternatives which are still viable after predicates are
/// evaluated is reported in {@code conflictingAlts}.</p>
///
/// <p>This method is not used by lexers.</p>
///
/// - parameter recognizer: the parser instance
/// - parameter dfa: the DFA for the current decision
/// - parameter startIndex: the input index where the decision started
/// - parameter stopIndex: the input index where the SLL conflict occurred
/// - parameter conflictingAlts: The specific conflicting alternatives. If this is
/// {@code null}, the conflicting alternatives are all alternatives
/// represented in {@code configs}. At the moment, conflictingAlts is non-null
/// (for the reference implementation, but Sam's optimized version can see this
/// as null).
/// - parameter configs: the ATN configuration set where the SLL conflict was
/// detected
func reportAttemptingFullContext(_ recognizer: Parser,
_ dfa: DFA,
_ startIndex: Int,
_ stopIndex: Int,
_ conflictingAlts: BitSet?,
_ configs: ATNConfigSet) throws
/// This method is called by the parser when a full-context prediction has a
/// unique result.
///
/// <p>Each full-context prediction which does not result in a syntax error
/// will call either {@link #reportContextSensitivity} or
/// {@link #reportAmbiguity}.</p>
///
/// <p>For prediction implementations that only evaluate full-context
/// predictions when an SLL conflict is found (including the default
/// {@link org.antlr.v4.runtime.atn.ParserATNSimulator} implementation), this method reports cases
/// where SLL conflicts were resolved to unique full-context predictions,
/// i.e. the decision was context-sensitive. This report does not necessarily
/// indicate a problem, and it may appear even in completely unambiguous
/// grammars.</p>
///
/// <p>{@code configs} may have more than one represented alternative if the
/// full-context prediction algorithm does not evaluate predicates before
/// beginning the full-context prediction. In all cases, the final prediction
/// is passed as the {@code prediction} argument.</p>
///
/// <p>Note that the definition of "context sensitivity" in this method
/// differs from the concept in {@link org.antlr.v4.runtime.atn.DecisionInfo#contextSensitivities}.
/// This method reports all instances where an SLL conflict occurred but LL
/// parsing produced a unique result, whether or not that unique result
/// matches the minimum alternative in the SLL conflicting set.</p>
///
/// <p>This method is not used by lexers.</p>
///
/// - parameter recognizer: the parser instance
/// - parameter dfa: the DFA for the current decision
/// - parameter startIndex: the input index where the decision started
/// - parameter stopIndex: the input index where the context sensitivity was
/// finally determined
/// - parameter prediction: the unambiguous result of the full-context prediction
/// - parameter configs: the ATN configuration set where the unambiguous prediction
/// was determined
func reportContextSensitivity(_ recognizer: Parser,
_ dfa: DFA,
_ startIndex: Int,
_ stopIndex: Int,
_ prediction: Int,
_ configs: ATNConfigSet) throws
}

View File

@ -0,0 +1,98 @@
/// Copyright (c) 2012-2016 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.
/// The interface for defining strategies to deal with syntax errors encountered
/// during a parse by ANTLR-generated parsers. We distinguish between three
/// different kinds of errors:
///
/// <ul>
/// <li>The parser could not figure out which path to take in the ATN (none of
/// the available alternatives could possibly match)</li>
/// <li>The current input does not match what we were looking for</li>
/// <li>A predicate evaluated to false</li>
/// </ul>
///
/// Implementations of this interface report syntax errors by calling
/// {@link org.antlr.v4.runtime.Parser#notifyErrorListeners}.
///
/// <p>TODO: what to do about lexers</p>
public protocol ANTLRErrorStrategy {
/// Reset the error handler state for the specified {@code recognizer}.
/// - parameter recognizer: the parser instance
func reset(_ recognizer: Parser)
/// This method is called when an unexpected symbol is encountered during an
/// inline match operation, such as {@link org.antlr.v4.runtime.Parser#match}. If the error
/// strategy successfully recovers from the match failure, this method
/// returns the {@link org.antlr.v4.runtime.Token} instance which should be treated as the
/// successful result of the match.
///
/// <p>This method handles the consumption of any tokens - the caller should
/// <b>not</b> call {@link org.antlr.v4.runtime.Parser#consume} after a successful recovery.</p>
///
/// <p>Note that the calling code will not report an error if this method
/// returns successfully. The error strategy implementation is responsible
/// for calling {@link org.antlr.v4.runtime.Parser#notifyErrorListeners} as appropriate.</p>
///
/// - parameter recognizer: the parser instance
/// - org.antlr.v4.runtime.RecognitionException if the error strategy was not able to
/// recover from the unexpected input symbol
@discardableResult
func recoverInline(_ recognizer: Parser) throws -> Token // RecognitionException;
/// This method is called to recover from exception {@code e}. This method is
/// called after {@link #reportError} by the default exception handler
/// generated for a rule method.
///
/// - seealso: #reportError
///
/// - parameter recognizer: the parser instance
/// - parameter e: the recognition exception to recover from
/// - org.antlr.v4.runtime.RecognitionException if the error strategy could not recover from
/// the recognition exception
func recover(_ recognizer: Parser, _ e: AnyObject) throws // RecognitionException;
/// This method provides the error handler with an opportunity to handle
/// syntactic or semantic errors in the input stream before they result in a
/// {@link org.antlr.v4.runtime.RecognitionException}.
///
/// <p>The generated code currently contains calls to {@link #sync} after
/// entering the decision state of a closure block ({@code (...)*} or
/// {@code (...)+}).</p>
///
/// <p>For an implementation based on Jim Idle's "magic sync" mechanism, see
/// {@link org.antlr.v4.runtime.DefaultErrorStrategy#sync}.</p>
///
/// - seealso: org.antlr.v4.runtime.DefaultErrorStrategy#sync
///
/// - parameter recognizer: the parser instance
/// - org.antlr.v4.runtime.RecognitionException if an error is detected by the error
/// strategy but cannot be automatically recovered at the current state in
/// the parsing process
func sync(_ recognizer: Parser) throws // RecognitionException;
/// Tests whether or not {@code recognizer} is in the process of recovering
/// from an error. In error recovery mode, {@link org.antlr.v4.runtime.Parser#consume} adds
/// symbols to the parse tree by calling
/// {@link org.antlr.v4.runtime.ParserRuleContext#addErrorNode(org.antlr.v4.runtime.Token)} instead of
/// {@link org.antlr.v4.runtime.ParserRuleContext#addChild(org.antlr.v4.runtime.Token)}.
///
/// - parameter recognizer: the parser instance
/// - returns: {@code true} if the parser is currently recovering from a parse
/// error, otherwise {@code false}
func inErrorRecoveryMode(_ recognizer: Parser) -> Bool
/// This method is called by when the parser successfully matches an input
/// symbol.
///
/// - parameter recognizer: the parser instance
func reportMatch(_ recognizer: Parser)
/// Report any kind of {@link org.antlr.v4.runtime.RecognitionException}. This method is called by
/// the default exception handler generated for a rule method.
///
/// - parameter recognizer: the parser instance
/// - parameter e: the recognition exception to report
func reportError(_ recognizer: Parser, _ e: AnyObject)
}

View File

@ -1,11 +1,8 @@
/* Copyright (c) 2012-2016 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.
*/
/**
* This is an {@link org.antlr.v4.runtime.ANTLRInputStream} that is loaded from a file all at once
* when you construct the object.
*/
/// Copyright (c) 2012-2016 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.
/// This is an {@link org.antlr.v4.runtime.ANTLRInputStream} that is loaded from a file all at once
/// when you construct the object.
import Foundation

View File

@ -0,0 +1,216 @@
/// Copyright (c) 2012-2016 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.
/// Vacuum all input from a {@link java.io.Reader}/{@link java.io.InputStream} and then treat it
/// like a {@code char[]} buffer. Can also pass in a {@link String} or
/// {@code char[]} to use.
///
/// <p>If you need encoding, pass in stream/reader with correct encoding.</p>
public class ANTLRInputStream: CharStream {
public static let READ_BUFFER_SIZE: Int = 1024
public static let INITIAL_BUFFER_SIZE: Int = 1024
/// The data being scanned
internal var data: [Character]
/// How many characters are actually in the buffer
internal var n: Int
/// 0..n-1 index into string of next char
internal var p: Int = 0
/// What is name or source of this char stream?
public var name: String?
public init() {
n = 0
data = [Character]()
}
/// Copy data in string to a local char array
public init(_ input: String) {
self.data = Array(input.characters) // input.toCharArray();
self.n = input.length
}
/// This is the preferred constructor for strings as no data is copied
public init(_ data: [Character], _ numberOfActualCharsInArray: Int) {
self.data = data
self.n = numberOfActualCharsInArray
}
/// public convenience init(_ r : Reader) throws; IOException {
/// self.init(r, INITIAL_BUFFER_SIZE, READ_BUFFER_SIZE);
/// }
///
/// public convenience init(_ r : Reader, _ initialSize : Int) throws; IOException {
/// self.init(r, initialSize, READ_BUFFER_SIZE);
/// }
///
/// public init(_ r : Reader, _ initialSize : Int, _ readChunkSize : Int) throws; IOException {
/// load(r, initialSize, readChunkSize);
/// }
///
/// public convenience init(_ input : InputStream) throws; IOException {
/// self.init(InputStreamReader(input), INITIAL_BUFFER_SIZE);
/// }
///
/// public convenience init(_ input : InputStream, _ initialSize : Int) throws; IOException {
/// self.init(InputStreamReader(input), initialSize);
/// }
///
/// public convenience init(_ input : InputStream, _ initialSize : Int, _ readChunkSize : Int) throws; IOException {
/// self.init(InputStreamReader(input), initialSize, readChunkSize);
/// }
///
/// public func load(r : Reader, _ size : Int, _ readChunkSize : Int)
/// throws; IOException
/// {
/// if ( r==nil ) {
/// return;
/// }
/// if ( size<=0 ) {
/// size = INITIAL_BUFFER_SIZE;
/// }
/// if ( readChunkSize<=0 ) {
/// readChunkSize = READ_BUFFER_SIZE;
/// }
/// // print("load "+size+" in chunks of "+readChunkSize);
/// try {
/// // alloc initial buffer size.
/// data = new char[size];
/// // read all the data in chunks of readChunkSize
/// var numRead : Int=0;
/// var p : Int = 0;
/// do {
/// if ( p+readChunkSize > data.length ) { // overflow?
/// // print("### overflow p="+p+", data.length="+data.length);
/// data = Arrays.copyOf(data, data.length * 2);
/// }
/// numRead = r.read(data, p, readChunkSize);
/// // print("read "+numRead+" chars; p was "+p+" is now "+(p+numRead));
/// p += numRead;
/// } while (numRead!=-1); // while not EOF
/// // set the actual size of the data available;
/// // EOF subtracted one above in p+=numRead; add one back
/// n = p+1;
/// //print("n="+n);
/// }
/// finally {
/// r.close();
/// }
/// }
/// Reset the stream so that it's in the same state it was
/// when the object was created *except* the data array is not
/// touched.
public func reset() {
p = 0
}
public func consume() throws {
if p >= n {
assert(LA(1) == ANTLRInputStream.EOF, "Expected: LA(1)==IntStream.EOF")
throw ANTLRError.illegalState(msg: "annot consume EOF")
}
// print("prev p="+p+", c="+(char)data[p]);
if p < n {
p += 1
//print("p moves to "+p+" (c='"+(char)data[p]+"')");
}
}
public func LA(_ i: Int) -> Int {
var i = i
if i == 0 {
return 0 // undefined
}
if i < 0 {
i += 1 // e.g., translate LA(-1) to use offset i=0; then data[p+0-1]
if (p + i - 1) < 0 {
return ANTLRInputStream.EOF// invalid; no char before first char
}
}
if (p + i - 1) >= n {
//print("char LA("+i+")=EOF; p="+p);
return ANTLRInputStream.EOF
}
//print("char LA("+i+")="+(char)data[p+i-1]+"; p="+p);
//print("LA("+i+"); p="+p+" n="+n+" data.length="+data.length);
return data[p + i - 1].unicodeValue
}
public func LT(_ i: Int) -> Int {
return LA(i)
}
/// Return the current input symbol index 0..n where n indicates the
/// last symbol has been read. The index is the index of char to
/// be returned from LA(1).
public func index() -> Int {
return p
}
public func size() -> Int {
return n
}
/// mark/release do nothing; we have entire buffer
public func mark() -> Int {
return -1
}
public func release(_ marker: Int) {
}
/// consume() ahead until p==index; can't just set p=index as we must
/// update line and charPositionInLine. If we seek backwards, just set p
public func seek(_ index: Int) throws {
var index = index
if index <= p {
p = index // just jump; don't update stream state (line, ...)
return
}
// seek forward, consume until p hits index or n (whichever comes first)
index = min(index, n)
while p < index {
try consume()
}
}
public func getText(_ interval: Interval) -> String {
let start: Int = interval.a
var stop: Int = interval.b
if stop >= n {
stop = n - 1
}
let count = stop - start + 1;
if start >= n {
return ""
}
return String(data[start ..< (start + count)])
}
public func getSourceName() -> String {
guard let name = name , !name.isEmpty else {
return ANTLRInputStream.UNKNOWN_SOURCE_NAME
}
return name
}
public func toString() -> String {
return String(data)
}
}

View File

@ -0,0 +1,71 @@
/// Copyright (c) 2012-2016 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.
/// This implementation of {@link org.antlr.v4.runtime.ANTLRErrorStrategy} responds to syntax errors
/// by immediately canceling the parse operation with a
/// {@link org.antlr.v4.runtime.misc.ParseCancellationException}. The implementation ensures that the
/// {@link org.antlr.v4.runtime.ParserRuleContext#exception} field is set for all parse tree nodes
/// that were not completed prior to encountering the error.
///
/// <p>
/// This error strategy is useful in the following scenarios.</p>
///
/// <ul>
/// <li><strong>Two-stage parsing:</strong> This error strategy allows the first
/// stage of two-stage parsing to immediately terminate if an error is
/// encountered, and immediately fall back to the second stage. In addition to
/// avoiding wasted work by attempting to recover from errors here, the empty
/// implementation of {@link org.antlr.v4.runtime.BailErrorStrategy#sync} improves the performance of
/// the first stage.</li>
/// <li><strong>Silent validation:</strong> When syntax errors are not being
/// reported or logged, and the parse result is simply ignored if errors occur,
/// the {@link org.antlr.v4.runtime.BailErrorStrategy} avoids wasting work on recovering from errors
/// when the result will be ignored either way.</li>
/// </ul>
///
/// <p>
/// {@code myparser.setErrorHandler(new BailErrorStrategy());}</p>
///
/// - seealso: org.antlr.v4.runtime.Parser#setErrorHandler(org.antlr.v4.runtime.ANTLRErrorStrategy)
public class BailErrorStrategy: DefaultErrorStrategy {
public override init(){}
/// Instead of recovering from exception {@code e}, re-throw it wrapped
/// in a {@link org.antlr.v4.runtime.misc.ParseCancellationException} so it is not caught by the
/// rule function catches. Use {@link Exception#getCause()} to get the
/// original {@link org.antlr.v4.runtime.RecognitionException}.
override
public func recover(_ recognizer: Parser, _ e: AnyObject) throws {
var context: ParserRuleContext? = recognizer.getContext()
while let contextWrap = context{
contextWrap.exception = e
context = (contextWrap.getParent() as? ParserRuleContext)
}
throw ANTLRException.recognition(e: e)
}
/// Make sure we don't attempt to recover inline; if the parser
/// successfully recovers, it won't throw an exception.
override
public func recoverInline(_ recognizer: Parser) throws -> Token {
let e: InputMismatchException = try InputMismatchException(recognizer)
var context: ParserRuleContext? = recognizer.getContext()
while let contextWrap = context {
contextWrap.exception = e
context = (contextWrap.getParent() as? ParserRuleContext)
}
throw ANTLRException.recognition(e: e)
}
/// Make sure we don't attempt to recover from problems in subrules.
override
public func sync(_ recognizer: Parser) {
}
}

View File

@ -1,16 +1,13 @@
/* Copyright (c) 2012-2016 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.
*/
/// Copyright (c) 2012-2016 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.
/**
* Provides an empty default implementation of {@link org.antlr.v4.runtime.ANTLRErrorListener}. The
* default implementation of each method does nothing, but can be overridden as
* necessary.
*
* @author Sam Harwell
*/
/// Provides an empty default implementation of {@link org.antlr.v4.runtime.ANTLRErrorListener}. The
/// default implementation of each method does nothing, but can be overridden as
/// necessary.
///
/// - Sam Harwell
public class BaseErrorListener: ANTLRErrorListener {

View File

@ -1,63 +1,52 @@
/* Copyright (c) 2012-2016 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.
*/
/// Copyright (c) 2012-2016 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.
/**
* This implementation of {@link org.antlr.v4.runtime.TokenStream} loads tokens from a
* {@link org.antlr.v4.runtime.TokenSource} on-demand, and places the tokens in a buffer to provide
* access to any previous token by index.
*
* <p>
* This token stream ignores the value of {@link org.antlr.v4.runtime.Token#getChannel}. If your
* parser requires the token stream filter tokens to only those on a particular
* channel, such as {@link org.antlr.v4.runtime.Token#DEFAULT_CHANNEL} or
* {@link org.antlr.v4.runtime.Token#HIDDEN_CHANNEL}, use a filtering token stream such a
* {@link org.antlr.v4.runtime.CommonTokenStream}.</p>
*/
/// This implementation of {@link org.antlr.v4.runtime.TokenStream} loads tokens from a
/// {@link org.antlr.v4.runtime.TokenSource} on-demand, and places the tokens in a buffer to provide
/// access to any previous token by index.
///
/// <p>
/// This token stream ignores the value of {@link org.antlr.v4.runtime.Token#getChannel}. If your
/// parser requires the token stream filter tokens to only those on a particular
/// channel, such as {@link org.antlr.v4.runtime.Token#DEFAULT_CHANNEL} or
/// {@link org.antlr.v4.runtime.Token#HIDDEN_CHANNEL}, use a filtering token stream such a
/// {@link org.antlr.v4.runtime.CommonTokenStream}.</p>
public class BufferedTokenStream: TokenStream {
/**
* The {@link org.antlr.v4.runtime.TokenSource} from which tokens for this stream are fetched.
*/
/// The {@link org.antlr.v4.runtime.TokenSource} from which tokens for this stream are fetched.
internal var tokenSource: TokenSource
/**
* A collection of all tokens fetched from the token source. The list is
* considered a complete view of the input once {@link #fetchedEOF} is set
* to {@code true}.
*/
/// A collection of all tokens fetched from the token source. The list is
/// considered a complete view of the input once {@link #fetchedEOF} is set
/// to {@code true}.
internal var tokens: Array<Token> = Array<Token>()
// Array<Token>(100
/**
* The index into {@link #tokens} of the current token (next token to
* {@link #consume}). {@link #tokens}{@code [}{@link #p}{@code ]} should be
* {@link #LT LT(1)}.
*
* <p>This field is set to -1 when the stream is first constructed or when
* {@link #setTokenSource} is called, indicating that the first token has
* not yet been fetched from the token source. For additional information,
* see the documentation of {@link org.antlr.v4.runtime.IntStream} for a description of
* Initializing Methods.</p>
*/
/// The index into {@link #tokens} of the current token (next token to
/// {@link #consume}). {@link #tokens}{@code [}{@link #p}{@code ]} should be
/// {@link #LT LT(1)}.
///
/// <p>This field is set to -1 when the stream is first constructed or when
/// {@link #setTokenSource} is called, indicating that the first token has
/// not yet been fetched from the token source. For additional information,
/// see the documentation of {@link org.antlr.v4.runtime.IntStream} for a description of
/// Initializing Methods.</p>
internal var p: Int = -1
/**
* Indicates whether the {@link org.antlr.v4.runtime.Token#EOF} token has been fetched from
* {@link #tokenSource} and added to {@link #tokens}. This field improves
* performance for the following cases:
*
* <ul>
* <li>{@link #consume}: The lookahead check in {@link #consume} to prevent
* consuming the EOF symbol is optimized by checking the values of
* {@link #fetchedEOF} and {@link #p} instead of calling {@link #LA}.</li>
* <li>{@link #fetch}: The check to prevent adding multiple EOF symbols into
* {@link #tokens} is trivial with this field.</li>
* <ul>
*/
/// Indicates whether the {@link org.antlr.v4.runtime.Token#EOF} token has been fetched from
/// {@link #tokenSource} and added to {@link #tokens}. This field improves
/// performance for the following cases:
///
/// <ul>
/// <li>{@link #consume}: The lookahead check in {@link #consume} to prevent
/// consuming the EOF symbol is optimized by checking the values of
/// {@link #fetchedEOF} and {@link #p} instead of calling {@link #LA}.</li>
/// <li>{@link #fetch}: The check to prevent adding multiple EOF symbols into
/// {@link #tokens} is trivial with this field.</li>
/// <ul>
internal var fetchedEOF: Bool = false
public init(_ tokenSource: TokenSource) {
@ -128,12 +117,11 @@ public class BufferedTokenStream: TokenStream {
}
}
/** Make sure index {@code i} in tokens has a token.
*
* @return {@code true} if a token is located at index {@code i}, otherwise
* {@code false}.
* @see #get(int i)
*/
/// Make sure index {@code i} in tokens has a token.
///
/// - returns: {@code true} if a token is located at index {@code i}, otherwise
/// {@code false}.
/// - seealso: #get(int i)
@discardableResult
internal func sync(_ i: Int) throws -> Bool {
assert(i >= 0, "Expected: i>=0")
@ -147,10 +135,9 @@ public class BufferedTokenStream: TokenStream {
return true
}
/** Add {@code n} elements to buffer.
*
* @return The actual number of elements added to the buffer.
*/
/// Add {@code n} elements to buffer.
///
/// - returns: The actual number of elements added to the buffer.
internal func fetch(_ n: Int) throws -> Int {
if fetchedEOF {
return 0
@ -181,7 +168,7 @@ public class BufferedTokenStream: TokenStream {
return tokens[i] //tokens[i]
}
/** Get all tokens from start..stop inclusively */
/// Get all tokens from start..stop inclusively
public func get(_ start: Int,_ stop: Int) throws -> Array<Token>? {
var stop = stop
if start < 0 || stop < 0 {
@ -235,19 +222,17 @@ public class BufferedTokenStream: TokenStream {
return tokens[i]
}
/**
* Allowed derived classes to modify the behavior of operations which change
* the current stream position by adjusting the target token index of a seek
* operation. The default implementation simply returns {@code i}. If an
* exception is thrown in this method, the current stream index should not be
* changed.
*
* <p>For example, {@link org.antlr.v4.runtime.CommonTokenStream} overrides this method to ensure that
* the seek target is always an on-channel token.</p>
*
* @param i The target token index.
* @return The adjusted target token index.
*/
/// Allowed derived classes to modify the behavior of operations which change
/// the current stream position by adjusting the target token index of a seek
/// operation. The default implementation simply returns {@code i}. If an
/// exception is thrown in this method, the current stream index should not be
/// changed.
///
/// <p>For example, {@link org.antlr.v4.runtime.CommonTokenStream} overrides this method to ensure that
/// the seek target is always an on-channel token.</p>
///
/// - parameter i: The target token index.
/// - returns: The adjusted target token index.
internal func adjustSeekIndex(_ i: Int) throws -> Int {
return i
}
@ -263,11 +248,12 @@ public class BufferedTokenStream: TokenStream {
p = try adjustSeekIndex(0)
}
/** Reset this token stream by setting its token source. */
/// Reset this token stream by setting its token source.
public func setTokenSource(_ tokenSource: TokenSource) {
self.tokenSource = tokenSource
tokens.removeAll()
p = -1
fetchedEOF = false
}
public func getTokens() -> Array<Token> {
@ -278,10 +264,9 @@ public class BufferedTokenStream: TokenStream {
return try getTokens(start, stop, nil)
}
/** Given a start and stop index, return a List of all tokens in
* the token type BitSet. Return null if no tokens were found. This
* method looks at both on and off channel tokens.
*/
/// Given a start and stop index, return a List of all tokens in
/// the token type BitSet. Return null if no tokens were found. This
/// method looks at both on and off channel tokens.
public func getTokens(_ start: Int, _ stop: Int, _ types: Set<Int>?) throws -> Array<Token>? {
try lazyInit()
if start < 0 || stop >= tokens.count ||
@ -318,12 +303,10 @@ public class BufferedTokenStream: TokenStream {
return try getTokens(start, stop, s)
}
/**
* Given a starting index, return the index of the next token on channel.
* Return {@code i} if {@code tokens[i]} is on channel. Return the index of
* the EOF token if there are no tokens on channel between {@code i} and
* EOF.
*/
/// Given a starting index, return the index of the next token on channel.
/// Return {@code i} if {@code tokens[i]} is on channel. Return the index of
/// the EOF token if there are no tokens on channel between {@code i} and
/// EOF.
internal func nextTokenOnChannel(_ i: Int, _ channel: Int) throws -> Int {
var i = i
try sync(i)
@ -345,16 +328,14 @@ public class BufferedTokenStream: TokenStream {
return i
}
/**
* Given a starting index, return the index of the previous token on
* channel. Return {@code i} if {@code tokens[i]} is on channel. Return -1
* if there are no tokens on channel between {@code i} and 0.
*
* <p>
* If {@code i} specifies an index at or after the EOF token, the EOF token
* index is returned. This is due to the fact that the EOF token is treated
* as though it were on every channel.</p>
*/
/// Given a starting index, return the index of the previous token on
/// channel. Return {@code i} if {@code tokens[i]} is on channel. Return -1
/// if there are no tokens on channel between {@code i} and 0.
///
/// <p>
/// If {@code i} specifies an index at or after the EOF token, the EOF token
/// index is returned. This is due to the fact that the EOF token is treated
/// as though it were on every channel.</p>
internal func previousTokenOnChannel(_ i: Int, _ channel: Int) throws -> Int {
var i = i
try sync(i)
@ -375,10 +356,9 @@ public class BufferedTokenStream: TokenStream {
return i
}
/** Collect all tokens on specified channel to the right of
* the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or
* EOF. If channel is -1, find any non default channel token.
*/
/// Collect all tokens on specified channel to the right of
/// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or
/// EOF. If channel is -1, find any non default channel token.
public func getHiddenTokensToRight(_ tokenIndex: Int, _ channel: Int) throws -> Array<Token>? {
try lazyInit()
if tokenIndex < 0 || tokenIndex >= tokens.count {
@ -400,18 +380,16 @@ public class BufferedTokenStream: TokenStream {
return filterForChannel(from, to, channel)
}
/** Collect all hidden tokens (any off-default channel) to the right of
* the current token up until we see a token on DEFAULT_TOKEN_CHANNEL
* or EOF.
*/
/// Collect all hidden tokens (any off-default channel) to the right of
/// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL
/// or EOF.
public func getHiddenTokensToRight(_ tokenIndex: Int) throws -> Array<Token>? {
return try getHiddenTokensToRight(tokenIndex, -1)
}
/** Collect all tokens on specified channel to the left of
* the current token up until we see a token on DEFAULT_TOKEN_CHANNEL.
* If channel is -1, find any non default channel token.
*/
/// Collect all tokens on specified channel to the left of
/// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL.
/// If channel is -1, find any non default channel token.
public func getHiddenTokensToLeft(_ tokenIndex: Int, _ channel: Int) throws -> Array<Token>? {
try lazyInit()
if tokenIndex < 0 || tokenIndex >= tokens.count {
@ -437,9 +415,8 @@ public class BufferedTokenStream: TokenStream {
return filterForChannel(from, to, channel)
}
/** Collect all hidden tokens (any off-default channel) to the left of
* the current token up until we see a token on DEFAULT_TOKEN_CHANNEL.
*/
/// Collect all hidden tokens (any off-default channel) to the left of
/// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL.
public func getHiddenTokensToLeft(_ tokenIndex: Int) throws -> Array<Token>? {
return try getHiddenTokensToLeft(tokenIndex, -1)
}
@ -469,7 +446,7 @@ public class BufferedTokenStream: TokenStream {
return tokenSource.getSourceName()
}
/** Get the text of all tokens in this buffer. */
/// Get the text of all tokens in this buffer.
public func getText() throws -> String {
@ -513,7 +490,7 @@ public class BufferedTokenStream: TokenStream {
return ""
}
/** Get all tokens from lexer until EOF */
/// Get all tokens from lexer until EOF
public func fill() throws {
try lazyInit()
let blockSize: Int = 1000

View File

@ -0,0 +1,24 @@
/// Copyright (c) 2012-2016 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.
/// A source of characters for an ANTLR lexer.
public protocol CharStream: IntStream {
/// This method returns the text for a range of characters within this input
/// stream. This method is guaranteed to not throw an exception if the
/// specified {@code interval} lies entirely within a marked range. For more
/// information about marked ranges, see {@link org.antlr.v4.runtime.IntStream#mark}.
///
/// - parameter interval: an interval within the stream
/// - returns: the text of the specified interval
///
/// - NullPointerException if {@code interval} is {@code null}
/// - IllegalArgumentException if {@code interval.a < 0}, or if
/// {@code interval.b < interval.a - 1}, or if {@code interval.b} lies at or
/// past the end of the stream
/// - UnsupportedOperationException if the stream does not support
/// getting the text of the specified interval
func getText(_ interval: Interval) -> String
}

View File

@ -1,84 +1,61 @@
/* Copyright (c) 2012-2016 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.
*/
/// Copyright (c) 2012-2016 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.
public class CommonToken: WritableToken {
/**
* An empty {@link org.antlr.v4.runtime.misc.Pair} which is used as the default value of
* {@link #source} for tokens that do not have a source.
*/
/// An empty {@link org.antlr.v4.runtime.misc.Pair} which is used as the default value of
/// {@link #source} for tokens that do not have a source.
internal static let EMPTY_SOURCE: (TokenSource?, CharStream?) = (nil, nil)
/**
* This is the backing field for {@link #getType} and {@link #setType}.
*/
/// This is the backing field for {@link #getType} and {@link #setType}.
internal var type: Int
/**
* This is the backing field for {@link #getLine} and {@link #setLine}.
*/
/// This is the backing field for {@link #getLine} and {@link #setLine}.
internal var line: Int = 0
/**
* This is the backing field for {@link #getCharPositionInLine} and
* {@link #setCharPositionInLine}.
*/
/// This is the backing field for {@link #getCharPositionInLine} and
/// {@link #setCharPositionInLine}.
internal var charPositionInLine: Int = -1
// set to invalid position
/**
* This is the backing field for {@link #getChannel} and
* {@link #setChannel}.
*/
/// This is the backing field for {@link #getChannel} and
/// {@link #setChannel}.
internal var channel: Int = DEFAULT_CHANNEL
/**
* This is the backing field for {@link #getTokenSource} and
* {@link #getInputStream}.
*
* <p>
* These properties share a field to reduce the memory footprint of
* {@link org.antlr.v4.runtime.CommonToken}. Tokens created by a {@link org.antlr.v4.runtime.CommonTokenFactory} from
* the same source and input stream share a reference to the same
* {@link org.antlr.v4.runtime.misc.Pair} containing these values.</p>
*/
/// This is the backing field for {@link #getTokenSource} and
/// {@link #getInputStream}.
///
/// <p>
/// These properties share a field to reduce the memory footprint of
/// {@link org.antlr.v4.runtime.CommonToken}. Tokens created by a {@link org.antlr.v4.runtime.CommonTokenFactory} from
/// the same source and input stream share a reference to the same
/// {@link org.antlr.v4.runtime.misc.Pair} containing these values.</p>
internal var source: (TokenSource?, CharStream?)
/**
* This is the backing field for {@link #getText} when the token text is
* explicitly set in the constructor or via {@link #setText}.
*
* @see #getText()
*/
/// This is the backing field for {@link #getText} when the token text is
/// explicitly set in the constructor or via {@link #setText}.
///
/// - seealso: #getText()
internal var text: String?
/**
* This is the backing field for {@link #getTokenIndex} and
* {@link #setTokenIndex}.
*/
/// This is the backing field for {@link #getTokenIndex} and
/// {@link #setTokenIndex}.
internal var index: Int = -1
/**
* This is the backing field for {@link #getStartIndex} and
* {@link #setStartIndex}.
*/
/// This is the backing field for {@link #getStartIndex} and
/// {@link #setStartIndex}.
internal var start: Int = 0
/**
* This is the backing field for {@link #getStopIndex} and
* {@link #setStopIndex}.
*/
/// This is the backing field for {@link #getStopIndex} and
/// {@link #setStopIndex}.
internal var stop: Int = 0
/**
* Constructs a new {@link org.antlr.v4.runtime.CommonToken} with the specified token type.
*
* @param type The token type.
*/
/// Constructs a new {@link org.antlr.v4.runtime.CommonToken} with the specified token type.
///
/// - parameter type: The token type.
private var _visited: Bool = false
@ -99,13 +76,11 @@ public class CommonToken: WritableToken {
}
}
/**
* Constructs a new {@link org.antlr.v4.runtime.CommonToken} with the specified token type and
* text.
*
* @param type The token type.
* @param text The text of the token.
*/
/// Constructs a new {@link org.antlr.v4.runtime.CommonToken} with the specified token type and
/// text.
///
/// - parameter type: The token type.
/// - parameter text: The text of the token.
public init(_ type: Int, _ text: String?) {
self.type = type
self.channel = CommonToken.DEFAULT_CHANNEL
@ -113,19 +88,17 @@ public class CommonToken: WritableToken {
self.source = CommonToken.EMPTY_SOURCE
}
/**
* Constructs a new {@link org.antlr.v4.runtime.CommonToken} as a copy of another {@link org.antlr.v4.runtime.Token}.
*
* <p>
* If {@code oldToken} is also a {@link org.antlr.v4.runtime.CommonToken} instance, the newly
* constructed token will share a reference to the {@link #text} field and
* the {@link org.antlr.v4.runtime.misc.Pair} stored in {@link #source}. Otherwise, {@link #text} will
* be assigned the result of calling {@link #getText}, and {@link #source}
* will be constructed from the result of {@link org.antlr.v4.runtime.Token#getTokenSource} and
* {@link org.antlr.v4.runtime.Token#getInputStream}.</p>
*
* @param oldToken The token to copy.
*/
/// Constructs a new {@link org.antlr.v4.runtime.CommonToken} as a copy of another {@link org.antlr.v4.runtime.Token}.
///
/// <p>
/// If {@code oldToken} is also a {@link org.antlr.v4.runtime.CommonToken} instance, the newly
/// constructed token will share a reference to the {@link #text} field and
/// the {@link org.antlr.v4.runtime.misc.Pair} stored in {@link #source}. Otherwise, {@link #text} will
/// be assigned the result of calling {@link #getText}, and {@link #source}
/// will be constructed from the result of {@link org.antlr.v4.runtime.Token#getTokenSource} and
/// {@link org.antlr.v4.runtime.Token#getInputStream}.</p>
///
/// - parameter oldToken: The token to copy.
public init(_ oldToken: Token) {
type = oldToken.getType()
line = oldToken.getLine()
@ -173,15 +146,13 @@ public class CommonToken: WritableToken {
}
/**
* Explicitly set the text for this token. If {code text} is not
* {@code null}, then {@link #getText} will return this value rather than
* extracting the text from the input.
*
* @param text The explicit text of the token, or {@code null} if the text
* should be obtained from the input along with the start and stop indexes
* of the token.
*/
/// Explicitly set the text for this token. If {code text} is not
/// {@code null}, then {@link #getText} will return this value rather than
/// extracting the text from the input.
///
/// - parameter text: The explicit text of the token, or {@code null} if the text
/// should be obtained from the input along with the start and stop indexes
/// of the token.
public func setText(_ text: String) {
self.text = text

View File

@ -0,0 +1,77 @@
/// Copyright (c) 2012-2016 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.
/// This default implementation of {@link org.antlr.v4.runtime.TokenFactory} creates
/// {@link org.antlr.v4.runtime.CommonToken} objects.
public class CommonTokenFactory: TokenFactory {
/// The default {@link org.antlr.v4.runtime.CommonTokenFactory} instance.
///
/// <p>
/// This token factory does not explicitly copy token text when constructing
/// tokens.</p>
public static let DEFAULT: TokenFactory = CommonTokenFactory()
/// Indicates whether {@link org.antlr.v4.runtime.CommonToken#setText} should be called after
/// constructing tokens to explicitly set the text. This is useful for cases
/// where the input stream might not be able to provide arbitrary substrings
/// of text from the input after the lexer creates a token (e.g. the
/// implementation of {@link org.antlr.v4.runtime.CharStream#getText} in
/// {@link org.antlr.v4.runtime.UnbufferedCharStream} throws an
/// {@link UnsupportedOperationException}). Explicitly setting the token text
/// allows {@link org.antlr.v4.runtime.Token#getText} to be called at any time regardless of the
/// input stream implementation.
///
/// <p>
/// The default value is {@code false} to avoid the performance and memory
/// overhead of copying text for every token unless explicitly requested.</p>
internal final var copyText: Bool
/// Constructs a {@link org.antlr.v4.runtime.CommonTokenFactory} with the specified value for
/// {@link #copyText}.
///
/// <p>
/// When {@code copyText} is {@code false}, the {@link #DEFAULT} instance
/// should be used instead of constructing a new instance.</p>
///
/// - parameter copyText: The value for {@link #copyText}.
public init(_ copyText: Bool) {
self.copyText = copyText
}
/// Constructs a {@link org.antlr.v4.runtime.CommonTokenFactory} with {@link #copyText} set to
/// {@code false}.
///
/// <p>
/// The {@link #DEFAULT} instance should be used instead of calling this
/// directly.</p>
public convenience init() {
self.init(false)
}
public func create(_ source: (TokenSource?, CharStream?), _ type: Int, _ text: String?,
_ channel: Int, _ start: Int, _ stop: Int,
_ line: Int, _ charPositionInLine: Int) -> Token {
let t: CommonToken = CommonToken(source, type, channel, start, stop)
t.setLine(line)
t.setCharPositionInLine(charPositionInLine)
if text != nil {
t.setText(text!)
} else {
if let cStream = source.1 , copyText {
t.setText(cStream.getText(Interval.of(start, stop)))
}
}
return t
}
public func create(_ type: Int, _ text: String) -> Token {
return CommonToken(type, text)
}
}

View File

@ -0,0 +1,124 @@
/// Copyright (c) 2012-2016 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.
/// This class extends {@link org.antlr.v4.runtime.BufferedTokenStream} with functionality to filter
/// token streams to tokens on a particular channel (tokens where
/// {@link org.antlr.v4.runtime.Token#getChannel} returns a particular value).
///
/// <p>
/// This token stream provides access to all tokens by index or when calling
/// methods like {@link #getText}. The channel filtering is only used for code
/// accessing tokens via the lookahead methods {@link #LA}, {@link #LT}, and
/// {@link #LB}.</p>
///
/// <p>
/// By default, tokens are placed on the default channel
/// ({@link org.antlr.v4.runtime.Token#DEFAULT_CHANNEL}), but may be reassigned by using the
/// {@code ->channel(HIDDEN)} lexer command, or by using an embedded action to
/// call {@link org.antlr.v4.runtime.Lexer#setChannel}.
/// </p>
///
/// <p>
/// Note: lexer rules which use the {@code ->skip} lexer command or call
/// {@link org.antlr.v4.runtime.Lexer#skip} do not produce tokens at all, so input text matched by
/// such a rule will not be available as part of the token stream, regardless of
/// channel.</p>
public class CommonTokenStream: BufferedTokenStream {
/// Specifies the channel to use for filtering tokens.
///
/// <p>
/// The default value is {@link org.antlr.v4.runtime.Token#DEFAULT_CHANNEL}, which matches the
/// default channel assigned to tokens created by the lexer.</p>
internal var channel: Int = CommonToken.DEFAULT_CHANNEL
/// Constructs a new {@link org.antlr.v4.runtime.CommonTokenStream} using the specified token
/// source and the default token channel ({@link org.antlr.v4.runtime.Token#DEFAULT_CHANNEL}).
///
/// - parameter tokenSource: The token source.
public override init(_ tokenSource: TokenSource) {
super.init(tokenSource)
}
/// Constructs a new {@link org.antlr.v4.runtime.CommonTokenStream} using the specified token
/// source and filtering tokens to the specified channel. Only tokens whose
/// {@link org.antlr.v4.runtime.Token#getChannel} matches {@code channel} or have the
/// {@link org.antlr.v4.runtime.Token#getType} equal to {@link org.antlr.v4.runtime.Token#EOF} will be returned by the
/// token stream lookahead methods.
///
/// - parameter tokenSource: The token source.
/// - parameter channel: The channel to use for filtering tokens.
public convenience init(_ tokenSource: TokenSource, _ channel: Int) {
self.init(tokenSource)
self.channel = channel
}
override
internal func adjustSeekIndex(_ i: Int) throws -> Int {
return try nextTokenOnChannel(i, channel)
}
override
internal func LB(_ k: Int) throws -> Token? {
if k == 0 || (p - k) < 0 {
return nil
}
var i: Int = p
var n: Int = 1
// find k good tokens looking backwards
while n <= k {
// skip off-channel tokens
try i = previousTokenOnChannel(i - 1, channel)
n += 1
}
if i < 0 {
return nil
}
return tokens[i]
}
override
public func LT(_ k: Int) throws -> Token? {
//System.out.println("enter LT("+k+")");
try lazyInit()
if k == 0 {
return nil
}
if k < 0 {
return try LB(-k)
}
var i: Int = p
var n: Int = 1 // we know tokens[p] is a good one
// find k good tokens
while n < k {
// skip off-channel tokens, but make sure to not look past EOF
if try sync(i + 1) {
i = try nextTokenOnChannel(i + 1, channel)
}
n += 1
}
// if ( i>range ) range = i;
return tokens[i]
}
/// Count EOF just once.
public func getNumberOfOnChannelTokens() throws -> Int {
var n: Int = 0
try fill()
let length = tokens.count
for i in 0..<length {
let t: Token = tokens[i]
if t.getChannel() == channel {
n += 1
}
if t.getType() == CommonToken.EOF {
break
}
}
return n
}
}

View File

@ -0,0 +1,36 @@
/// Copyright (c) 2012-2016 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.
///
/// - Sam Harwell
public class ConsoleErrorListener: BaseErrorListener {
/// Provides a default instance of {@link org.antlr.v4.runtime.ConsoleErrorListener}.
public static let INSTANCE: ConsoleErrorListener = ConsoleErrorListener()
/// {@inheritDoc}
///
/// <p>
/// This implementation prints messages to {@link System#err} containing the
/// values of {@code line}, {@code charPositionInLine}, and {@code msg} using
/// the following format.</p>
///
/// <pre>
/// line <em>line</em>:<em>charPositionInLine</em> <em>msg</em>
/// </pre>
override
public func syntaxError<T:ATNSimulator>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int,
_ charPositionInLine: Int,
_ msg: String,
_ e: AnyObject?
) {
if Parser.ConsoleError {
errPrint("line \(line):\(charPositionInLine) \(msg)")
}
}
}

View File

@ -0,0 +1,702 @@
/// Copyright (c) 2012-2016 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.
/// This is the default implementation of {@link org.antlr.v4.runtime.ANTLRErrorStrategy} used for
/// error reporting and recovery in ANTLR parsers.
import Foundation
public class DefaultErrorStrategy: ANTLRErrorStrategy {
/// Indicates whether the error strategy is currently "recovering from an
/// error". This is used to suppress reporting multiple error messages while
/// attempting to recover from a detected syntax error.
///
/// - seealso: #inErrorRecoveryMode
internal var errorRecoveryMode: Bool = false
/// The index into the input stream where the last error occurred.
/// This is used to prevent infinite loops where an error is found
/// but no token is consumed during recovery...another error is found,
/// ad nauseum. This is a failsafe mechanism to guarantee that at least
/// one token/tree node is consumed for two errors.
internal var lastErrorIndex: Int = -1
internal var lastErrorStates: IntervalSet?
/// {@inheritDoc}
///
/// <p>The default implementation simply calls {@link #endErrorCondition} to
/// ensure that the handler is not in error recovery mode.</p>
public func reset(_ recognizer: Parser) {
endErrorCondition(recognizer)
}
/// This method is called to enter error recovery mode when a recognition
/// exception is reported.
///
/// - parameter recognizer: the parser instance
internal func beginErrorCondition(_ recognizer: Parser) {
errorRecoveryMode = true
}
/// {@inheritDoc}
public func inErrorRecoveryMode(_ recognizer: Parser) -> Bool {
return errorRecoveryMode
}
/// This method is called to leave error recovery mode after recovering from
/// a recognition exception.
///
/// - parameter recognizer:
internal func endErrorCondition(_ recognizer: Parser) {
errorRecoveryMode = false
lastErrorStates = nil
lastErrorIndex = -1
}
/// {@inheritDoc}
///
/// <p>The default implementation simply calls {@link #endErrorCondition}.</p>
public func reportMatch(_ recognizer: Parser) {
endErrorCondition(recognizer)
}
/// {@inheritDoc}
///
/// <p>The default implementation returns immediately if the handler is already
/// in error recovery mode. Otherwise, it calls {@link #beginErrorCondition}
/// and dispatches the reporting task based on the runtime type of {@code e}
/// according to the following table.</p>
///
/// <ul>
/// <li>{@link org.antlr.v4.runtime.NoViableAltException}: Dispatches the call to
/// {@link #reportNoViableAlternative}</li>
/// <li>{@link org.antlr.v4.runtime.InputMismatchException}: Dispatches the call to
/// {@link #reportInputMismatch}</li>
/// <li>{@link org.antlr.v4.runtime.FailedPredicateException}: Dispatches the call to
/// {@link #reportFailedPredicate}</li>
/// <li>All other types: calls {@link org.antlr.v4.runtime.Parser#notifyErrorListeners} to report
/// the exception</li>
/// </ul>
public func reportError(_ recognizer: Parser,
_ e: AnyObject) {
// if we've already reported an error and have not matched a token
// yet successfully, don't report any errors.
if inErrorRecoveryMode(recognizer) {
return // don't report spurious errors
}
beginErrorCondition(recognizer)
//TODO: exception handler
if (e is NoViableAltException) {
try! reportNoViableAlternative(recognizer, e as! NoViableAltException);
} else {
if (e is InputMismatchException) {
reportInputMismatch(recognizer, e as! InputMismatchException);
} else {
if (e is FailedPredicateException) {
reportFailedPredicate(recognizer, e as! FailedPredicateException);
} else {
errPrint("unknown recognition error type: " + String(describing: type(of: e)));
let re = (e as! RecognitionException<ParserATNSimulator>)
recognizer.notifyErrorListeners(re.getOffendingToken(), re.message ?? "", e);
}
}
}
}
/// {@inheritDoc}
///
/// <p>The default implementation resynchronizes the parser by consuming tokens
/// until we find one in the resynchronization set--loosely the set of tokens
/// that can follow the current rule.</p>
public func recover(_ recognizer: Parser, _ e: AnyObject) throws {
// print("recover in "+recognizer.getRuleInvocationStack()+
// " index="+getTokenStream(recognizer).index()+
// ", lastErrorIndex="+
// lastErrorIndex+
// ", states="+lastErrorStates);
if let lastErrorStates = lastErrorStates ,
lastErrorIndex == getTokenStream(recognizer).index() &&
lastErrorStates.contains(recognizer.getState()) {
// uh oh, another error at same token index and previously-visited
// state in ATN; must be a case where LT(1) is in the recovery
// token set so nothing got consumed. Consume a single token
// at least to prevent an infinite loop; this is a failsafe.
// errPrint("seen error condition before index="+
// lastErrorIndex+", states="+lastErrorStates);
// errPrint("FAILSAFE consumes "+recognizer.getTokenNames()[getTokenStream(recognizer).LA(1)]);
try recognizer.consume()
}
lastErrorIndex = getTokenStream(recognizer).index()
if lastErrorStates == nil {
lastErrorStates = try IntervalSet()
}
try lastErrorStates!.add(recognizer.getState())
let followSet: IntervalSet = try getErrorRecoverySet(recognizer)
try consumeUntil(recognizer, followSet)
}
/// The default implementation of {@link org.antlr.v4.runtime.ANTLRErrorStrategy#sync} makes sure
/// that the current lookahead symbol is consistent with what were expecting
/// at this point in the ATN. You can call this anytime but ANTLR only
/// generates code to check before subrules/loops and each iteration.
///
/// <p>Implements Jim Idle's magic sync mechanism in closures and optional
/// subrules. E.g.,</p>
///
/// <pre>
/// a : sync ( stuff sync )* ;
/// sync : {consume to what can follow sync} ;
/// </pre>
///
/// At the start of a sub rule upon error, {@link #sync} performs single
/// token deletion, if possible. If it can't do that, it bails on the current
/// rule and uses the default error recovery, which consumes until the
/// resynchronization set of the current rule.
///
/// <p>If the sub rule is optional ({@code (...)?}, {@code (...)*}, or block
/// with an empty alternative), then the expected set includes what follows
/// the subrule.</p>
///
/// <p>During loop iteration, it consumes until it sees a token that can start a
/// sub rule or what follows loop. Yes, that is pretty aggressive. We opt to
/// stay in the loop as long as possible.</p>
///
/// <p><strong>ORIGINS</strong></p>
///
/// <p>Previous versions of ANTLR did a poor job of their recovery within loops.
/// A single mismatch token or missing token would force the parser to bail
/// out of the entire rules surrounding the loop. So, for rule</p>
///
/// <pre>
/// classDef : 'class' ID '{' member* '}'
/// </pre>
///
/// input with an extra token between members would force the parser to
/// consume until it found the next class definition rather than the next
/// member definition of the current class.
///
/// <p>This functionality cost a little bit of effort because the parser has to
/// compare token set at the start of the loop and at each iteration. If for
/// some reason speed is suffering for you, you can turn off this
/// functionality by simply overriding this method as a blank { }.</p>
public func sync(_ recognizer: Parser) throws {
let s: ATNState = recognizer.getInterpreter().atn.states[recognizer.getState()]!
// errPrint("sync @ "+s.stateNumber+"="+s.getClass().getSimpleName());
// If already recovering, don't try to sync
if inErrorRecoveryMode(recognizer) {
return
}
let tokens: TokenStream = getTokenStream(recognizer)
let la: Int = try tokens.LA(1)
// try cheaper subset first; might get lucky. seems to shave a wee bit off
//let set : IntervalSet = recognizer.getATN().nextTokens(s)
if try recognizer.getATN().nextTokens(s).contains(CommonToken.EPSILON) {
return
}
if try recognizer.getATN().nextTokens(s).contains(la) {
return
}
switch s.getStateType() {
case ATNState.BLOCK_START: fallthrough
case ATNState.STAR_BLOCK_START: fallthrough
case ATNState.PLUS_BLOCK_START: fallthrough
case ATNState.STAR_LOOP_ENTRY:
// report error and recover if possible
if try singleTokenDeletion(recognizer) != nil {
return
}
throw try ANTLRException.recognition(e: InputMismatchException(recognizer))
case ATNState.PLUS_LOOP_BACK: fallthrough
case ATNState.STAR_LOOP_BACK:
// errPrint("at loop back: "+s.getClass().getSimpleName());
try reportUnwantedToken(recognizer)
let expecting: IntervalSet = try recognizer.getExpectedTokens()
let whatFollowsLoopIterationOrRule: IntervalSet =
try expecting.or(try getErrorRecoverySet(recognizer)) as! IntervalSet
try consumeUntil(recognizer, whatFollowsLoopIterationOrRule)
break
default:
// do nothing if we can't identify the exact kind of ATN state
break
}
}
/// This is called by {@link #reportError} when the exception is a
/// {@link org.antlr.v4.runtime.NoViableAltException}.
///
/// - seealso: #reportError
///
/// - parameter recognizer: the parser instance
/// - parameter e: the recognition exception
internal func reportNoViableAlternative(_ recognizer: Parser,
_ e: NoViableAltException) throws {
let tokens: TokenStream? = getTokenStream(recognizer)
var input: String
if let tokens = tokens {
if e.getStartToken().getType() == CommonToken.EOF {
input = "<EOF>"
} else {
input = try tokens.getText(e.getStartToken(), e.getOffendingToken())
}
} else {
input = "<unknown input>"
}
let msg: String = "no viable alternative at input " + escapeWSAndQuote(input)
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e)
}
/// This is called by {@link #reportError} when the exception is an
/// {@link org.antlr.v4.runtime.InputMismatchException}.
///
/// - seealso: #reportError
///
/// - parameter recognizer: the parser instance
/// - parameter e: the recognition exception
internal func reportInputMismatch(_ recognizer: Parser,
_ e: InputMismatchException) {
let msg: String = "mismatched input " + getTokenErrorDisplay(e.getOffendingToken()) +
" expecting " + e.getExpectedTokens()!.toString(recognizer.getVocabulary())
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e)
}
/// This is called by {@link #reportError} when the exception is a
/// {@link org.antlr.v4.runtime.FailedPredicateException}.
///
/// - seealso: #reportError
///
/// - parameter recognizer: the parser instance
/// - parameter e: the recognition exception
internal func reportFailedPredicate(_ recognizer: Parser,
_ e: FailedPredicateException) {
let ruleName: String = recognizer.getRuleNames()[recognizer._ctx!.getRuleIndex()]
let msg: String = "rule " + ruleName + " " + e.message! // e.getMessage()
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e)
}
/// This method is called to report a syntax error which requires the removal
/// of a token from the input stream. At the time this method is called, the
/// erroneous symbol is current {@code LT(1)} symbol and has not yet been
/// removed from the input stream. When this method returns,
/// {@code recognizer} is in error recovery mode.
///
/// <p>This method is called when {@link #singleTokenDeletion} identifies
/// single-token deletion as a viable recovery strategy for a mismatched
/// input error.</p>
///
/// <p>The default implementation simply returns if the handler is already in
/// error recovery mode. Otherwise, it calls {@link #beginErrorCondition} to
/// enter error recovery mode, followed by calling
/// {@link org.antlr.v4.runtime.Parser#notifyErrorListeners}.</p>
///
/// - parameter recognizer: the parser instance
internal func reportUnwantedToken(_ recognizer: Parser) throws {
if inErrorRecoveryMode(recognizer) {
return
}
beginErrorCondition(recognizer)
let t: Token = try recognizer.getCurrentToken()
let tokenName: String = getTokenErrorDisplay(t)
let expecting: IntervalSet = try getExpectedTokens(recognizer)
let msg: String = "extraneous input " + tokenName + " expecting " +
expecting.toString(recognizer.getVocabulary())
recognizer.notifyErrorListeners(t, msg, nil)
}
/// This method is called to report a syntax error which requires the
/// insertion of a missing token into the input stream. At the time this
/// method is called, the missing token has not yet been inserted. When this
/// method returns, {@code recognizer} is in error recovery mode.
///
/// <p>This method is called when {@link #singleTokenInsertion} identifies
/// single-token insertion as a viable recovery strategy for a mismatched
/// input error.</p>
///
/// <p>The default implementation simply returns if the handler is already in
/// error recovery mode. Otherwise, it calls {@link #beginErrorCondition} to
/// enter error recovery mode, followed by calling
/// {@link org.antlr.v4.runtime.Parser#notifyErrorListeners}.</p>
///
/// - parameter recognizer: the parser instance
internal func reportMissingToken(_ recognizer: Parser) throws {
if inErrorRecoveryMode(recognizer) {
return
}
beginErrorCondition(recognizer)
let t: Token = try recognizer.getCurrentToken()
let expecting: IntervalSet = try getExpectedTokens(recognizer)
let msg: String = "missing " + expecting.toString(recognizer.getVocabulary()) +
" at " + getTokenErrorDisplay(t)
recognizer.notifyErrorListeners(t, msg, nil)
}
/// {@inheritDoc}
///
/// <p>The default implementation attempts to recover from the mismatched input
/// by using single token insertion and deletion as described below. If the
/// recovery attempt fails, this method throws an
/// {@link org.antlr.v4.runtime.InputMismatchException}.</p>
///
/// <p><strong>EXTRA TOKEN</strong> (single token deletion)</p>
///
/// <p>{@code LA(1)} is not what we are looking for. If {@code LA(2)} has the
/// right token, however, then assume {@code LA(1)} is some extra spurious
/// token and delete it. Then consume and return the next token (which was
/// the {@code LA(2)} token) as the successful result of the match operation.</p>
///
/// <p>This recovery strategy is implemented by {@link #singleTokenDeletion}.</p>
///
/// <p><strong>MISSING TOKEN</strong> (single token insertion)</p>
///
/// <p>If current token (at {@code LA(1)}) is consistent with what could come
/// after the expected {@code LA(1)} token, then assume the token is missing
/// and use the parser's {@link org.antlr.v4.runtime.TokenFactory} to create it on the fly. The
/// "insertion" is performed by returning the created token as the successful
/// result of the match operation.</p>
///
/// <p>This recovery strategy is implemented by {@link #singleTokenInsertion}.</p>
///
/// <p><strong>EXAMPLE</strong></p>
///
/// <p>For example, Input {@code i=(3;} is clearly missing the {@code ')'}. When
/// the parser returns from the nested call to {@code expr}, it will have
/// call chain:</p>
///
/// <pre>
/// stat &rarr; expr &rarr; atom
/// </pre>
///
/// and it will be trying to match the {@code ')'} at this point in the
/// derivation:
///
/// <pre>
/// =&gt; ID '=' '(' INT ')' ('+' atom)* ';'
/// ^
/// </pre>
///
/// The attempt to match {@code ')'} will fail when it sees {@code ';'} and
/// call {@link #recoverInline}. To recover, it sees that {@code LA(1)==';'}
/// is in the set of tokens that can follow the {@code ')'} token reference
/// in rule {@code atom}. It can assume that you forgot the {@code ')'}.
public func recoverInline(_ recognizer: Parser) throws -> Token {
// SINGLE TOKEN DELETION
let matchedSymbol: Token? = try singleTokenDeletion(recognizer)
if matchedSymbol != nil {
// we have deleted the extra token.
// now, move past ttype token as if all were ok
try recognizer.consume()
return matchedSymbol!
}
// SINGLE TOKEN INSERTION
if try singleTokenInsertion(recognizer) {
return try getMissingSymbol(recognizer)
}
throw try ANTLRException.recognition(e: InputMismatchException(recognizer))
// throw try ANTLRException.InputMismatch(e: InputMismatchException(recognizer) )
//RuntimeException("InputMismatchException")
// even that didn't work; must throw the exception
//throwException() /* throw InputMismatchException(recognizer); */
}
/// This method implements the single-token insertion inline error recovery
/// strategy. It is called by {@link #recoverInline} if the single-token
/// deletion strategy fails to recover from the mismatched input. If this
/// method returns {@code true}, {@code recognizer} will be in error recovery
/// mode.
///
/// <p>This method determines whether or not single-token insertion is viable by
/// checking if the {@code LA(1)} input symbol could be successfully matched
/// if it were instead the {@code LA(2)} symbol. If this method returns
/// {@code true}, the caller is responsible for creating and inserting a
/// token with the correct type to produce this behavior.</p>
///
/// - parameter recognizer: the parser instance
/// - returns: {@code true} if single-token insertion is a viable recovery
/// strategy for the current mismatched input, otherwise {@code false}
internal func singleTokenInsertion(_ recognizer: Parser) throws -> Bool {
let currentSymbolType: Int = try getTokenStream(recognizer).LA(1)
// if current token is consistent with what could come after current
// ATN state, then we know we're missing a token; error recovery
// is free to conjure up and insert the missing token
let currentState: ATNState = recognizer.getInterpreter().atn.states[recognizer.getState()]!
let next: ATNState = currentState.transition(0).target
let atn: ATN = recognizer.getInterpreter().atn
let expectingAtLL2: IntervalSet = try atn.nextTokens(next, recognizer._ctx)
// print("LT(2) set="+expectingAtLL2.toString(recognizer.getTokenNames()));
if expectingAtLL2.contains(currentSymbolType) {
try reportMissingToken(recognizer)
return true
}
return false
}
/// This method implements the single-token deletion inline error recovery
/// strategy. It is called by {@link #recoverInline} to attempt to recover
/// from mismatched input. If this method returns null, the parser and error
/// handler state will not have changed. If this method returns non-null,
/// {@code recognizer} will <em>not</em> be in error recovery mode since the
/// returned token was a successful match.
///
/// <p>If the single-token deletion is successful, this method calls
/// {@link #reportUnwantedToken} to report the error, followed by
/// {@link org.antlr.v4.runtime.Parser#consume} to actually "delete" the extraneous token. Then,
/// before returning {@link #reportMatch} is called to signal a successful
/// match.</p>
///
/// - parameter recognizer: the parser instance
/// - returns: the successfully matched {@link org.antlr.v4.runtime.Token} instance if single-token
/// deletion successfully recovers from the mismatched input, otherwise
/// {@code null}
internal func singleTokenDeletion(_ recognizer: Parser) throws -> Token? {
let nextTokenType: Int = try getTokenStream(recognizer).LA(2)
let expecting: IntervalSet = try getExpectedTokens(recognizer)
if expecting.contains(nextTokenType) {
try reportUnwantedToken(recognizer)
/// errPrint("recoverFromMismatchedToken deleting "+
/// ((TokenStream)getTokenStream(recognizer)).LT(1)+
/// " since "+((TokenStream)getTokenStream(recognizer)).LT(2)+
/// " is what we want");
try recognizer.consume() // simply delete extra token
// we want to return the token we're actually matching
let matchedSymbol: Token = try recognizer.getCurrentToken()
reportMatch(recognizer) // we know current token is correct
return matchedSymbol
}
return nil
}
/// Conjure up a missing token during error recovery.
///
/// The recognizer attempts to recover from single missing
/// symbols. But, actions might refer to that missing symbol.
/// For example, x=ID {f($x);}. The action clearly assumes
/// that there has been an identifier matched previously and that
/// $x points at that token. If that token is missing, but
/// the next token in the stream is what we want we assume that
/// this token is missing and we keep going. Because we
/// have to return some token to replace the missing token,
/// we have to conjure one up. This method gives the user control
/// over the tokens returned for missing tokens. Mostly,
/// you will want to create something special for identifier
/// tokens. For literals such as '{' and ',', the default
/// action in the parser or tree parser works. It simply creates
/// a CommonToken of the appropriate type. The text will be the token.
/// If you change what tokens must be created by the lexer,
/// override this method to create the appropriate tokens.
internal func getTokenStream(_ recognizer: Parser) -> TokenStream {
return recognizer.getInputStream() as! TokenStream
}
internal func getMissingSymbol(_ recognizer: Parser) throws -> Token {
let currentSymbol: Token = try recognizer.getCurrentToken()
let expecting: IntervalSet = try getExpectedTokens(recognizer)
let expectedTokenType: Int = expecting.getMinElement() // get any element
var tokenText: String
if expectedTokenType == CommonToken.EOF {
tokenText = "<missing EOF>"
} else {
tokenText = "<missing " + recognizer.getVocabulary().getDisplayName(expectedTokenType) + ">"
}
var current: Token = currentSymbol
let lookback: Token? = try getTokenStream(recognizer).LT(-1)
if current.getType() == CommonToken.EOF && lookback != nil {
current = lookback!
}
let token = recognizer.getTokenFactory().create((current.getTokenSource(), current.getTokenSource()!.getInputStream()), expectedTokenType, tokenText,
CommonToken.DEFAULT_CHANNEL,
-1, -1,
current.getLine(), current.getCharPositionInLine())
return token
}
internal func getExpectedTokens(_ recognizer: Parser) throws -> IntervalSet {
return try recognizer.getExpectedTokens()
}
/// 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.
internal func getTokenErrorDisplay(_ t: Token?) -> String {
if t == nil {
return "<no token>"
}
var s: String? = getSymbolText(t!)
if s == nil {
if getSymbolType(t!) == CommonToken.EOF {
s = "<EOF>"
} else {
s = "<\(getSymbolType(t!))>"
}
}
return escapeWSAndQuote(s!)
}
internal func getSymbolText(_ symbol: Token) -> String {
return symbol.getText()!
}
internal func getSymbolType(_ symbol: Token) -> Int {
return symbol.getType()
}
internal func escapeWSAndQuote(_ s: String) -> String {
var s = s
s = s.replaceAll("\n", replacement: "\\n")
s = s.replaceAll("\r", replacement: "\\r")
s = s.replaceAll("\t", replacement: "\\t")
return "'" + s + "'"
}
/// Compute the error recovery set for the current rule. During
/// rule invocation, the parser pushes the set of tokens that can
/// follow that rule reference on the stack; this amounts to
/// computing FIRST of what follows the rule reference in the
/// enclosing rule. See LinearApproximator.FIRST().
/// This local follow set only includes tokens
/// from within the rule; i.e., the FIRST computation done by
/// ANTLR stops at the end of a rule.
///
/// EXAMPLE
///
/// When you find a "no viable alt exception", the input is not
/// consistent with any of the alternatives for rule r. The best
/// thing to do is to consume tokens until you see something that
/// can legally follow a call to r *or* any rule that called r.
/// You don't want the exact set of viable next tokens because the
/// input might just be missing a token--you might consume the
/// rest of the input looking for one of the missing tokens.
///
/// Consider grammar:
///
/// a : '[' b ']'
/// | '(' b ')'
/// ;
/// b : c '^' INT ;
/// c : ID
/// | INT
/// ;
///
/// At each rule invocation, the set of tokens that could follow
/// that rule is pushed on a stack. Here are the various
/// context-sensitive follow sets:
///
/// FOLLOW(b1_in_a) = FIRST(']') = ']'
/// FOLLOW(b2_in_a) = FIRST(')') = ')'
/// FOLLOW(c_in_b) = FIRST('^') = '^'
///
/// Upon erroneous input "[]", the call chain is
///
/// a -> b -> c
///
/// and, hence, the follow context stack is:
///
/// depth follow set start of rule execution
/// 0 <EOF> a (from main())
/// 1 ']' b
/// 2 '^' c
///
/// Notice that ')' is not included, because b would have to have
/// been called from a different context in rule a for ')' to be
/// included.
///
/// For error recovery, we cannot consider FOLLOW(c)
/// (context-sensitive or otherwise). We need the combined set of
/// all context-sensitive FOLLOW sets--the set of all tokens that
/// could follow any reference in the call chain. We need to
/// resync to one of those tokens. Note that FOLLOW(c)='^' and if
/// we resync'd to that token, we'd consume until EOF. We need to
/// sync to context-sensitive FOLLOWs for a, b, and c: {']','^'}.
/// In this case, for input "[]", LA(1) is ']' and in the set, so we would
/// not consume anything. After printing an error, rule c would
/// return normally. Rule b would not find the required '^' though.
/// At this point, it gets a mismatched token error and throws an
/// exception (since LA(1) is not in the viable following token
/// set). The rule exception handler tries to recover, but finds
/// the same recovery set and doesn't consume anything. Rule b
/// exits normally returning to rule a. Now it finds the ']' (and
/// with the successful match exits errorRecovery mode).
///
/// So, you can see that the parser walks up the call chain looking
/// for the token that was a member of the recovery set.
///
/// Errors are not generated in errorRecovery mode.
///
/// ANTLR's error recovery mechanism is based upon original ideas:
///
/// "Algorithms + Data Structures = Programs" by Niklaus Wirth
///
/// and
///
/// "A note on error recovery in recursive descent parsers":
/// http://portal.acm.org/citation.cfm?id=947902.947905
///
/// Later, Josef Grosch had some good ideas:
///
/// "Efficient and Comfortable Error Recovery in Recursive Descent
/// Parsers":
/// ftp://www.cocolab.com/products/cocktail/doca4.ps/ell.ps.zip
///
/// Like Grosch I implement context-sensitive FOLLOW sets that are combined
/// at run-time upon error to avoid overhead during parsing.
internal func getErrorRecoverySet(_ recognizer: Parser) throws -> IntervalSet {
let atn: ATN = recognizer.getInterpreter().atn
var ctx: RuleContext? = recognizer._ctx
let recoverSet: IntervalSet = try IntervalSet()
while let ctxWrap = ctx , ctxWrap.invokingState >= 0 {
// compute what follows who invoked us
let invokingState: ATNState = atn.states[ctxWrap.invokingState]!
let rt: RuleTransition = invokingState.transition(0) as! RuleTransition
let follow: IntervalSet = try atn.nextTokens(rt.followState)
try recoverSet.addAll(follow)
ctx = ctxWrap.parent
}
try recoverSet.remove(CommonToken.EPSILON)
// print("recover set "+recoverSet.toString(recognizer.getTokenNames()));
return recoverSet
}
/// Consume tokens until one matches the given token set.
internal func consumeUntil(_ recognizer: Parser, _ set: IntervalSet) throws {
// errPrint("consumeUntil("+set.toString(recognizer.getTokenNames())+")");
var ttype: Int = try getTokenStream(recognizer).LA(1)
while ttype != CommonToken.EOF && !set.contains(ttype) {
//print("consume during recover LA(1)="+getTokenNames()[input.LA(1)]);
// getTokenStream(recognizer).consume();
try recognizer.consume()
ttype = try getTokenStream(recognizer).LA(1)
}
}
}

View File

@ -1,54 +1,45 @@
/* Copyright (c) 2012-2016 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.
*/
/// Copyright (c) 2012-2016 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.
/**
* This implementation of {@link org.antlr.v4.runtime.ANTLRErrorListener} can be used to identify
* certain potential correctness and performance problems in grammars. "Reports"
* are made by calling {@link org.antlr.v4.runtime.Parser#notifyErrorListeners} with the appropriate
* message.
*
* <ul>
* <li><b>Ambiguities</b>: These are cases where more than one path through the
* grammar can match the input.</li>
* <li><b>Weak context sensitivity</b>: These are cases where full-context
* prediction resolved an SLL conflict to a unique alternative which equaled the
* minimum alternative of the SLL conflict.</li>
* <li><b>Strong (forced) context sensitivity</b>: These are cases where the
* full-context prediction resolved an SLL conflict to a unique alternative,
* <em>and</em> the minimum alternative of the SLL conflict was found to not be
* a truly viable alternative. Two-stage parsing cannot be used for inputs where
* this situation occurs.</li>
* </ul>
*
* @author Sam Harwell
*/
/// This implementation of {@link org.antlr.v4.runtime.ANTLRErrorListener} can be used to identify
/// certain potential correctness and performance problems in grammars. "Reports"
/// are made by calling {@link org.antlr.v4.runtime.Parser#notifyErrorListeners} with the appropriate
/// message.
///
/// <ul>
/// <li><b>Ambiguities</b>: These are cases where more than one path through the
/// grammar can match the input.</li>
/// <li><b>Weak context sensitivity</b>: These are cases where full-context
/// prediction resolved an SLL conflict to a unique alternative which equaled the
/// minimum alternative of the SLL conflict.</li>
/// <li><b>Strong (forced) context sensitivity</b>: These are cases where the
/// full-context prediction resolved an SLL conflict to a unique alternative,
/// <em>and</em> the minimum alternative of the SLL conflict was found to not be
/// a truly viable alternative. Two-stage parsing cannot be used for inputs where
/// this situation occurs.</li>
/// </ul>
///
/// - Sam Harwell
import Foundation
public class DiagnosticErrorListener: BaseErrorListener {
/**
* When {@code true}, only exactly known ambiguities are reported.
*/
/// When {@code true}, only exactly known ambiguities are reported.
internal final var exactOnly: Bool
/**
* Initializes a new instance of {@link org.antlr.v4.runtime.DiagnosticErrorListener} which only
* reports exact ambiguities.
*/
/// Initializes a new instance of {@link org.antlr.v4.runtime.DiagnosticErrorListener} which only
/// reports exact ambiguities.
public convenience override init() {
self.init(true)
}
/**
* Initializes a new instance of {@link org.antlr.v4.runtime.DiagnosticErrorListener}, specifying
* whether all ambiguities or only exact ambiguities are reported.
*
* @param exactOnly {@code true} to report only exact ambiguities, otherwise
* {@code false} to report all ambiguities.
*/
/// Initializes a new instance of {@link org.antlr.v4.runtime.DiagnosticErrorListener}, specifying
/// whether all ambiguities or only exact ambiguities are reported.
///
/// - parameter exactOnly: {@code true} to report only exact ambiguities, otherwise
/// {@code false} to report all ambiguities.
public init(_ exactOnly: Bool) {
self.exactOnly = exactOnly
}
@ -120,17 +111,15 @@ public class DiagnosticErrorListener: BaseErrorListener {
return NSString(format: "%d (%@)", decision, ruleName) as String
}
/**
* Computes the set of conflicting or ambiguous alternatives from a
* configuration set, if that information was not already provided by the
* parser.
*
* @param reportedAlts The set of conflicting or ambiguous alternatives, as
* reported by the parser.
* @param configs The conflicting or ambiguous configuration set.
* @return Returns {@code reportedAlts} if it is not {@code null}, otherwise
* returns the set of alternatives represented in {@code configs}.
*/
/// Computes the set of conflicting or ambiguous alternatives from a
/// configuration set, if that information was not already provided by the
/// parser.
///
/// - parameter reportedAlts: The set of conflicting or ambiguous alternatives, as
/// reported by the parser.
/// - parameter configs: The conflicting or ambiguous configuration set.
/// - returns: Returns {@code reportedAlts} if it is not {@code null}, otherwise
/// returns the set of alternatives represented in {@code configs}.
internal func getConflictingAlts(_ reportedAlts: BitSet?, _ configs: ATNConfigSet) throws -> BitSet {
if reportedAlts != nil {
return reportedAlts!

View File

@ -1,14 +1,12 @@
/* Copyright (c) 2012-2016 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.
*/
/// Copyright (c) 2012-2016 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.
/** A semantic predicate failed during validation. Validation of predicates
* occurs when normally parsing the alternative just like matching a token.
* Disambiguating predicate evaluation occurs when we test a predicate during
* prediction.
*/
/// A semantic predicate failed during validation. Validation of predicates
/// occurs when normally parsing the alternative just like matching a token.
/// Disambiguating predicate evaluation occurs when we test a predicate during
/// prediction.
public class FailedPredicateException: RecognitionException<ParserATNSimulator> {
private final var ruleIndex: Int
private final var predicateIndex: Int

View File

@ -0,0 +1,14 @@
/// Copyright (c) 2012-2016 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.
/// This signifies any kind of mismatched input exceptions such as
/// when the current input does not match the expected token.
public class InputMismatchException: RecognitionException<ParserATNSimulator> {
public init(_ recognizer: Parser) throws {
super.init(recognizer, recognizer.getInputStream()!, recognizer._ctx)
self.setOffendingToken(try recognizer.getCurrentToken())
}
}

View File

@ -0,0 +1,193 @@
/// Copyright (c) 2012-2016 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.
/// A simple stream of symbols whose values are represented as integers. This
/// interface provides <em>marked ranges</em> with support for a minimum level
/// of buffering necessary to implement arbitrary lookahead during prediction.
/// For more information on marked ranges, see {@link #mark}.
///
/// <p><strong>Initializing Methods:</strong> Some methods in this interface have
/// unspecified behavior if no call to an initializing method has occurred after
/// the stream was constructed. The following is a list of initializing methods:</p>
///
/// <ul>
/// <li>{@link #LA}</li>
/// <li>{@link #consume}</li>
/// <li>{@link #size}</li>
/// </ul>
public protocol IntStream: class {
/// The value returned by {@link #LA LA()} when the end of the stream is
/// reached.
//let EOF : Int = -1;
/// The value returned by {@link #getSourceName} when the actual name of the
/// underlying source is not known.
//let UNKNOWN_SOURCE_NAME : String = "<unknown>";
/// Consumes the current symbol in the stream. This method has the following
/// effects:
///
/// <ul>
/// <li><strong>Forward movement:</strong> The value of {@link #index index()}
/// before calling this method is less than the value of {@code index()}
/// after calling this method.</li>
/// <li><strong>Ordered lookahead:</strong> The value of {@code LA(1)} before
/// calling this method becomes the value of {@code LA(-1)} after calling
/// this method.</li>
/// </ul>
///
/// Note that calling this method does not guarantee that {@code index()} is
/// incremented by exactly 1, as that would preclude the ability to implement
/// filtering streams (e.g. {@link org.antlr.v4.runtime.CommonTokenStream} which distinguishes
/// between "on-channel" and "off-channel" tokens).
///
/// - IllegalStateException if an attempt is made to consume the the
/// end of the stream (i.e. if {@code LA(1)==}{@link #EOF EOF} before calling
/// {@code consume}).
func consume() throws
/// Gets the value of the symbol at offset {@code i} from the current
/// position. When {@code i==1}, this method returns the value of the current
/// symbol in the stream (which is the next symbol to be consumed). When
/// {@code i==-1}, this method returns the value of the previously read
/// symbol in the stream. It is not valid to call this method with
/// {@code i==0}, but the specific behavior is unspecified because this
/// method is frequently called from performance-critical code.
///
/// <p>This method is guaranteed to succeed if any of the following are true:</p>
///
/// <ul>
/// <li>{@code i>0}</li>
/// <li>{@code i==-1} and {@link #index index()} returns a value greater
/// than the value of {@code index()} after the stream was constructed
/// and {@code LA(1)} was called in that order. Specifying the current
/// {@code index()} relative to the index after the stream was created
/// allows for filtering implementations that do not return every symbol
/// from the underlying source. Specifying the call to {@code LA(1)}
/// allows for lazily initialized streams.</li>
/// <li>{@code LA(i)} refers to a symbol consumed within a marked region
/// that has not yet been released.</li>
/// </ul>
///
/// <p>If {@code i} represents a position at or beyond the end of the stream,
/// this method returns {@link #EOF}.</p>
///
/// <p>The return value is unspecified if {@code i<0} and fewer than {@code -i}
/// calls to {@link #consume consume()} have occurred from the beginning of
/// the stream before calling this method.</p>
///
/// - UnsupportedOperationException if the stream does not support
/// retrieving the value of the specified symbol
func LA(_ i: Int) throws -> Int
/// A mark provides a guarantee that {@link #seek seek()} operations will be
/// valid over a "marked range" extending from the index where {@code mark()}
/// was called to the current {@link #index index()}. This allows the use of
/// streaming input sources by specifying the minimum buffering requirements
/// to support arbitrary lookahead during prediction.
///
/// <p>The returned mark is an opaque handle (type {@code int}) which is passed
/// to {@link #release release()} when the guarantees provided by the marked
/// range are no longer necessary. When calls to
/// {@code mark()}/{@code release()} are nested, the marks must be released
/// in reverse order of which they were obtained. Since marked regions are
/// used during performance-critical sections of prediction, the specific
/// behavior of invalid usage is unspecified (i.e. a mark is not released, or
/// a mark is released twice, or marks are not released in reverse order from
/// which they were created).</p>
///
/// <p>The behavior of this method is unspecified if no call to an
/// {@link org.antlr.v4.runtime.IntStream initializing method} has occurred after this stream was
/// constructed.</p>
///
/// <p>This method does not change the current position in the input stream.</p>
///
/// <p>The following example shows the use of {@link #mark mark()},
/// {@link #release release(mark)}, {@link #index index()}, and
/// {@link #seek seek(index)} as part of an operation to safely work within a
/// marked region, then restore the stream position to its original value and
/// release the mark.</p>
/// <pre>
/// IntStream stream = ...;
/// int index = -1;
/// int mark = stream.mark();
/// try {
/// index = stream.index();
/// // perform work here...
/// } finally {
/// if (index != -1) {
/// stream.seek(index);
/// }
/// stream.release(mark);
/// }
/// </pre>
///
/// - returns: An opaque marker which should be passed to
/// {@link #release release()} when the marked range is no longer required.
func mark() -> Int
/// This method releases a marked range created by a call to
/// {@link #mark mark()}. Calls to {@code release()} must appear in the
/// reverse order of the corresponding calls to {@code mark()}. If a mark is
/// released twice, or if marks are not released in reverse order of the
/// corresponding calls to {@code mark()}, the behavior is unspecified.
///
/// <p>For more information and an example, see {@link #mark}.</p>
///
/// - parameter marker: A marker returned by a call to {@code mark()}.
/// - seealso: #mark
func release(_ marker: Int) throws
/// Return the index into the stream of the input symbol referred to by
/// {@code LA(1)}.
///
/// <p>The behavior of this method is unspecified if no call to an
/// {@link org.antlr.v4.runtime.IntStream initializing method} has occurred after this stream was
/// constructed.</p>
func index() -> Int
/// Set the input cursor to the position indicated by {@code index}. If the
/// specified index lies past the end of the stream, the operation behaves as
/// though {@code index} was the index of the EOF symbol. After this method
/// returns without throwing an exception, then at least one of the following
/// will be true.
///
/// <ul>
/// <li>{@link #index index()} will return the index of the first symbol
/// appearing at or after the specified {@code index}. Specifically,
/// implementations which filter their sources should automatically
/// adjust {@code index} forward the minimum amount required for the
/// operation to target a non-ignored symbol.</li>
/// <li>{@code LA(1)} returns {@link #EOF}</li>
/// </ul>
///
/// This operation is guaranteed to not throw an exception if {@code index}
/// lies within a marked region. For more information on marked regions, see
/// {@link #mark}. The behavior of this method is unspecified if no call to
/// an {@link org.antlr.v4.runtime.IntStream initializing method} has occurred after this stream
/// was constructed.
///
/// - parameter index: The absolute index to seek to.
///
/// - IllegalArgumentException if {@code index} is less than 0
/// - UnsupportedOperationException if the stream does not support
/// seeking to the specified index
func seek(_ index: Int) throws
/// Returns the total number of symbols in the stream, including a single EOF
/// symbol.
///
/// - UnsupportedOperationException if the size of the stream is
/// unknown.
func size() -> Int
/// Gets the name of the underlying symbol source. This method returns a
/// non-null, non-empty string. If such a name is not known, this method
/// returns {@link #UNKNOWN_SOURCE_NAME}.
func getSourceName() -> String
}

View File

@ -0,0 +1,57 @@
/// Copyright (c) 2012-2016 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.
/// This class extends {@link org.antlr.v4.runtime.ParserRuleContext} by allowing the value of
/// {@link #getRuleIndex} to be explicitly set for the context.
///
/// <p>
/// {@link org.antlr.v4.runtime.ParserRuleContext} does not include field storage for the rule index
/// since the context classes created by the code generator override the
/// {@link #getRuleIndex} method to return the correct value for that context.
/// Since the parser interpreter does not use the context classes generated for a
/// parser, this class (with slightly more memory overhead per node) is used to
/// provide equivalent functionality.</p>
public class InterpreterRuleContext: ParserRuleContext {
/// This is the backing field for {@link #getRuleIndex}.
private var ruleIndex: Int = -1
public override init() {
super.init()
}
/// Constructs a new {@link org.antlr.v4.runtime.InterpreterRuleContext} with the specified
/// parent, invoking state, and rule index.
///
/// - parameter parent: The parent context.
/// - parameter invokingStateNumber: The invoking state number.
/// - parameter ruleIndex: The rule index for the current context.
public init(_ parent: ParserRuleContext?,
_ invokingStateNumber: Int,
_ ruleIndex: Int) {
self.ruleIndex = ruleIndex
super.init(parent, invokingStateNumber)
}
override
public func getRuleIndex() -> Int {
return ruleIndex
}
/// Copy a {@link org.antlr.v4.runtime.ParserRuleContext} or {@link org.antlr.v4.runtime.InterpreterRuleContext}
/// stack to a {@link org.antlr.v4.runtime.InterpreterRuleContext} tree.
/// Return {@link null} if {@code ctx} is null.
public static func fromParserRuleContext(_ ctx: ParserRuleContext?) -> InterpreterRuleContext? {
guard let ctx = ctx else {
return nil
}
let dup: InterpreterRuleContext = InterpreterRuleContext()
dup.copyFrom(ctx)
dup.ruleIndex = ctx.getRuleIndex()
dup.parent = fromParserRuleContext(ctx.getParent() as? ParserRuleContext)
return dup
}
}

View File

@ -1,14 +1,12 @@
/* Copyright (c) 2012-2016 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.
*/
/// Copyright (c) 2012-2016 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.
/** A lexer is recognizer that draws input symbols from a character stream.
* lexer grammars result in a subclass of this object. A Lexer object
* uses simplified match() and error recovery mechanisms in the interest
* of speed.
*/
/// A lexer is recognizer that draws input symbols from a character stream.
/// lexer grammars result in a subclass of this object. A Lexer object
/// uses simplified match() and error recovery mechanisms in the interest
/// of speed.
import Foundation
@ -29,48 +27,44 @@ open class Lexer: Recognizer<LexerATNSimulator>
public var _input: CharStream?
internal var _tokenFactorySourcePair: (TokenSource?, CharStream?)
/** How to create token objects */
/// How to create token objects
internal var _factory: TokenFactory = CommonTokenFactory.DEFAULT
/** The goal of all lexer rules/methods is to create a token object.
* This is an instance variable as multiple rules may collaborate to
* create a single token. nextToken will return this object after
* matching lexer rule(s). If you subclass to allow multiple token
* emissions, then set this to the last token to be matched or
* something nonnull so that the auto token emit mechanism will not
* emit another token.
*/
/// The goal of all lexer rules/methods is to create a token object.
/// This is an instance variable as multiple rules may collaborate to
/// create a single token. nextToken will return this object after
/// matching lexer rule(s). If you subclass to allow multiple token
/// emissions, then set this to the last token to be matched or
/// something nonnull so that the auto token emit mechanism will not
/// emit another token.
public var _token: Token?
/** What character index in the stream did the current token start at?
* Needed, for example, to get the text for current token. Set at
* the start of nextToken.
*/
/// What character index in the stream did the current token start at?
/// Needed, for example, to get the text for current token. Set at
/// the start of nextToken.
public var _tokenStartCharIndex: Int = -1
/** The line on which the first character of the token resides */
/// The line on which the first character of the token resides
public var _tokenStartLine: Int = 0
/** The character position of first character within the line */
/// The character position of first character within the line
public var _tokenStartCharPositionInLine: Int = 0
/** Once we see EOF on char stream, next token will be EOF.
* If you have DONE : EOF ; then you see DONE EOF.
*/
/// Once we see EOF on char stream, next token will be EOF.
/// If you have DONE : EOF ; then you see DONE EOF.
public var _hitEOF: Bool = false
/** The channel number for the current token */
/// The channel number for the current token
public var _channel: Int = 0
/** The token type for the current token */
/// The token type for the current token
public var _type: Int = 0
public final var _modeStack: Stack<Int> = Stack<Int>()
public var _mode: Int = Lexer.DEFAULT_MODE
/** You can set the text for the current token to override what is in
* the input char buffer. Use setText() or can set this instance var.
*/
/// You can set the text for the current token to override what is in
/// the input char buffer. Use setText() or can set this instance var.
public var _text: String?
public override init() {
@ -103,9 +97,8 @@ open class Lexer: Recognizer<LexerATNSimulator>
getInterpreter().reset()
}
/** Return a token from this source; i.e., match a token on the char
* stream.
*/
/// Return a token from this source; i.e., match a token on the char
/// stream.
open func nextToken() throws -> Token {
guard let _input = _input else {
@ -168,12 +161,11 @@ open class Lexer: Recognizer<LexerATNSimulator>
}
/** Instruct the lexer to skip creating a token for current lexer rule
* and look for another token. nextToken() knows to keep looking when
* a lexer rule finishes with token set to SKIP_TOKEN. Recall that
* if token==null at end of any token rule, it creates one for you
* and emits it.
*/
/// Instruct the lexer to skip creating a token for current lexer rule
/// and look for another token. nextToken() knows to keep looking when
/// a lexer rule finishes with token set to SKIP_TOKEN. Recall that
/// if token==null at end of any token rule, it creates one for you
/// and emits it.
open func skip() {
_type = Lexer.SKIP
}
@ -218,7 +210,7 @@ open class Lexer: Recognizer<LexerATNSimulator>
return _factory
}
/** Set the char stream and reset the lexer */
/// Set the char stream and reset the lexer
open override func setInputStream(_ input: IntStream) throws {
self._input = nil
@ -238,22 +230,20 @@ open class Lexer: Recognizer<LexerATNSimulator>
return _input
}
/** By default does not support multiple emits per nextToken invocation
* for efficiency reasons. Subclass and override this method, nextToken,
* and getToken (to push tokens into a list and pull from that list
* rather than a single variable as this implementation does).
*/
/// By default does not support multiple emits per nextToken invocation
/// for efficiency reasons. Subclass and override this method, nextToken,
/// and getToken (to push tokens into a list and pull from that list
/// rather than a single variable as this implementation does).
open func emit(_ token: Token) {
//System.err.println("emit "+token);
self._token = token
}
/** The standard method called to automatically emit a token at the
* outermost lexical rule. The token object should point into the
* char buffer start..stop. If there is a text override in 'text',
* use that to set the token's text. Override this method to emit
* custom Token objects or provide a new factory.
*/
/// The standard method called to automatically emit a token at the
/// outermost lexical rule. The token object should point into the
/// char buffer start..stop. If there is a text override in 'text',
/// use that to set the token's text. Override this method to emit
/// custom Token objects or provide a new factory.
@discardableResult
open func emit() -> Token {
let t: Token = _factory.create(_tokenFactorySourcePair, _type, _text, _channel, _tokenStartCharIndex, getCharIndex() - 1,
@ -296,14 +286,13 @@ open class Lexer: Recognizer<LexerATNSimulator>
getInterpreter().setCharPositionInLine(charPositionInLine)
}
/** What is the index of the current character of lookahead? */
/// What is the index of the current character of lookahead?
open func getCharIndex() -> Int {
return _input!.index()
}
/** Return the text matched so far for the current token or any
* text override.
*/
/// Return the text matched so far for the current token or any
/// text override.
open func getText() -> String {
if _text != nil {
return _text!
@ -311,14 +300,13 @@ open class Lexer: Recognizer<LexerATNSimulator>
return getInterpreter().getText(_input!)
}
/** Set the complete text of this token; it wipes any previous
* changes to the text.
*/
/// Set the complete text of this token; it wipes any previous
/// changes to the text.
open func setText(_ text: String) {
self._text = text
}
/** Override if emitting multiple tokens. */
/// Override if emitting multiple tokens.
open func getToken() -> Token {
return _token!
}
@ -347,18 +335,16 @@ open class Lexer: Recognizer<LexerATNSimulator>
return nil
}
/** Used to print out token names like ID during debugging and
* error reporting. The generated parsers implement a method
* that overrides this to point to their String[] tokenNames.
*/
/// Used to print out token names like ID during debugging and
/// error reporting. The generated parsers implement a method
/// that overrides this to point to their String[] tokenNames.
override
open func getTokenNames() -> [String?]? {
return nil
}
/** Return a list of all Token objects in input char stream.
* Forces load of all tokens. Does not include EOF token.
*/
/// Return a list of all Token objects in input char stream.
/// Forces load of all tokens. Does not include EOF token.
open func getAllTokens() throws -> Array<Token> {
var tokens: Array<Token> = Array<Token>()
var t: Token = try nextToken()
@ -419,11 +405,10 @@ open class Lexer: Recognizer<LexerATNSimulator>
return "'\(s)'"
}
/** Lexers can normally match any char in it's vocabulary after matching
* a token, so do the easy thing and just kill a character and hope
* it all works out. You can instead use the rule invocation stack
* to do sophisticated error recovery if you are in a fragment rule.
*/
/// Lexers can normally match any char in it's vocabulary after matching
/// a token, so do the easy thing and just kill a character and hope
/// it all works out. You can instead use the rule invocation stack
/// to do sophisticated error recovery if you are in a fragment rule.
//public func recover(re : RecognitionException) {
open func recover(_ re: AnyObject) throws {

View File

@ -1,7 +1,6 @@
/* Copyright (c) 2012-2016 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.
*/
/// Copyright (c) 2012-2016 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.
public class LexerInterpreter: Lexer {

View File

@ -1,14 +1,13 @@
/* Copyright (c) 2012-2016 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.
*/
/// Copyright (c) 2012-2016 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.
public class LexerNoViableAltException: RecognitionException<LexerATNSimulator>, CustomStringConvertible {
/** Matching attempted at what input index? */
/// Matching attempted at what input index?
private final var startIndex: Int
/** Which configurations did we try at input.index() that couldn't match input.LA(1)? */
/// Which configurations did we try at input.index() that couldn't match input.LA(1)?
private final var deadEndConfigs: ATNConfigSet
public init(_ lexer: Lexer?,

View File

@ -1,83 +1,64 @@
/* Copyright (c) 2012-2016 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.
*/
/// Copyright (c) 2012-2016 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.
/**
* Provides an implementation of {@link org.antlr.v4.runtime.TokenSource} as a wrapper around a list
* of {@link org.antlr.v4.runtime.Token} objects.
*
* <p>If the final token in the list is an {@link org.antlr.v4.runtime.Token#EOF} token, it will be used
* as the EOF token for every call to {@link #nextToken} after the end of the
* list is reached. Otherwise, an EOF token will be created.</p>
*/
/// Provides an implementation of {@link org.antlr.v4.runtime.TokenSource} as a wrapper around a list
/// of {@link org.antlr.v4.runtime.Token} objects.
///
/// <p>If the final token in the list is an {@link org.antlr.v4.runtime.Token#EOF} token, it will be used
/// as the EOF token for every call to {@link #nextToken} after the end of the
/// list is reached. Otherwise, an EOF token will be created.</p>
public class ListTokenSource: TokenSource {
/**
* The wrapped collection of {@link org.antlr.v4.runtime.Token} objects to return.
*/
/// The wrapped collection of {@link org.antlr.v4.runtime.Token} objects to return.
internal final var tokens: Array<Token>
/**
* The name of the input source. If this value is {@code null}, a call to
* {@link #getSourceName} should return the source name used to create the
* the next token in {@link #tokens} (or the previous token if the end of
* the input has been reached).
*/
/// The name of the input source. If this value is {@code null}, a call to
/// {@link #getSourceName} should return the source name used to create the
/// the next token in {@link #tokens} (or the previous token if the end of
/// the input has been reached).
private final var sourceName: String?
/**
* The index into {@link #tokens} of token to return by the next call to
* {@link #nextToken}. The end of the input is indicated by this value
* being greater than or equal to the number of items in {@link #tokens}.
*/
/// The index into {@link #tokens} of token to return by the next call to
/// {@link #nextToken}. The end of the input is indicated by this value
/// being greater than or equal to the number of items in {@link #tokens}.
internal var i: Int = 0
/**
* This field caches the EOF token for the token source.
*/
/// This field caches the EOF token for the token source.
internal var eofToken: Token?
/**
* This is the backing field for {@link #getTokenFactory} and
* {@link setTokenFactory}.
*/
/// This is the backing field for {@link #getTokenFactory} and
/// {@link setTokenFactory}.
private var _factory: TokenFactory = CommonTokenFactory.DEFAULT
/**
* Constructs a new {@link org.antlr.v4.runtime.ListTokenSource} instance from the specified
* collection of {@link org.antlr.v4.runtime.Token} objects.
*
* @param tokens The collection of {@link org.antlr.v4.runtime.Token} objects to provide as a
* {@link org.antlr.v4.runtime.TokenSource}.
* @exception NullPointerException if {@code tokens} is {@code null}
*/
/// Constructs a new {@link org.antlr.v4.runtime.ListTokenSource} instance from the specified
/// collection of {@link org.antlr.v4.runtime.Token} objects.
///
/// - parameter tokens: The collection of {@link org.antlr.v4.runtime.Token} objects to provide as a
/// {@link org.antlr.v4.runtime.TokenSource}.
/// - NullPointerException if {@code tokens} is {@code null}
public convenience init(_ tokens: Array<Token>) {
self.init(tokens, nil)
}
/**
* Constructs a new {@link org.antlr.v4.runtime.ListTokenSource} instance from the specified
* collection of {@link org.antlr.v4.runtime.Token} objects and source name.
*
* @param tokens The collection of {@link org.antlr.v4.runtime.Token} objects to provide as a
* {@link org.antlr.v4.runtime.TokenSource}.
* @param sourceName The name of the {@link org.antlr.v4.runtime.TokenSource}. If this value is
* {@code null}, {@link #getSourceName} will attempt to infer the name from
* the next {@link org.antlr.v4.runtime.Token} (or the previous token if the end of the input has
* been reached).
*
* @exception NullPointerException if {@code tokens} is {@code null}
*/
/// Constructs a new {@link org.antlr.v4.runtime.ListTokenSource} instance from the specified
/// collection of {@link org.antlr.v4.runtime.Token} objects and source name.
///
/// - parameter tokens: The collection of {@link org.antlr.v4.runtime.Token} objects to provide as a
/// {@link org.antlr.v4.runtime.TokenSource}.
/// - parameter sourceName: The name of the {@link org.antlr.v4.runtime.TokenSource}. If this value is
/// {@code null}, {@link #getSourceName} will attempt to infer the name from
/// the next {@link org.antlr.v4.runtime.Token} (or the previous token if the end of the input has
/// been reached).
///
/// - NullPointerException if {@code tokens} is {@code null}
public init(_ tokens: Array<Token>, _ sourceName: String?) {
self.tokens = tokens
self.sourceName = sourceName
}
/**
* {@inheritDoc}
*/
/// {@inheritDoc}
public func getCharPositionInLine() -> Int {
if i < tokens.count {
@ -111,9 +92,7 @@ public class ListTokenSource: TokenSource {
return 0
}
/**
* {@inheritDoc}
*/
/// {@inheritDoc}
public func nextToken() -> Token {
if i >= tokens.count {
@ -142,9 +121,7 @@ public class ListTokenSource: TokenSource {
return t
}
/**
* {@inheritDoc}
*/
/// {@inheritDoc}
public func getLine() -> Int {
if i < tokens.count {
@ -179,9 +156,7 @@ public class ListTokenSource: TokenSource {
return 1
}
/**
* {@inheritDoc}
*/
/// {@inheritDoc}
public func getInputStream() -> CharStream? {
if i < tokens.count {
@ -200,9 +175,7 @@ public class ListTokenSource: TokenSource {
return nil
}
/**
* {@inheritDoc}
*/
/// {@inheritDoc}
public func getSourceName() -> String {
if sourceName != nil {
@ -216,17 +189,13 @@ public class ListTokenSource: TokenSource {
return "List"
}
/**
* {@inheritDoc}
*/
/// {@inheritDoc}
public func setTokenFactory(_ factory: TokenFactory) {
self._factory = factory
}
/**
* {@inheritDoc}
*/
/// {@inheritDoc}
public func getTokenFactory() -> TokenFactory {
return _factory

Some files were not shown because too many files have changed in this diff Show More