BailErrorStrategy is supposed to throw an error that's different from
the ordinary recognition error, specifically so that it can be handled
differently by client code. This was not ported over from Java correctly.
Fix this by moving parseCancellation from ANTLRError to ANTLRException,
adding its RecognitionException argument, and throwing it from the
two handlers in BailErrorStrategy.
Also remove ANTLRException.cannotInvokeStartRule, which is unused.
(The Java runtime uses it when ParseTreePatternMatcher throws a generic
exception, but we don't have that.)
Remove pointless do block from LexerATNSimulator. This is a translation
from Java of a try/finally block, but we have the finally clause in a
defer block so we don't need the do block.
Change the initializer to ANTLRFileStream so that it throws any errors that
occur while reading the file. Previously, it was just dropping any errors on
the floor (inside Utils.readFile).
Remove Utils.readFile, it's not used anywhere else.
Fix initialization of {Lexer,Parser}Interpreter.decisionToDFA. These
were always being created as empty arrays, which would never work.
I don't know if anyone's using this code; presumably not.
Remove unused ATN.modeNameToStartState. In the Java runtime this is
only used by LexerATNFactory (i.e. during lexer generation) and we don't
have the equivalent in the Swift runtime at all.
Remove Recognizer.tokenTypeMapCache and .ruleIndexMapCache. These
were easily replaced in Swift with lazy vars. The input to these
two caches are fixed fields on the Recognizer (the Vocabulary and
rule names respectively) so a lazy var suffices.
Note that these differed compared with the Java runtime -- they are
declared as static in Java and therefore the caches are shared across
all recognizer instances, but the Swift runtime had them as Recognizer
instance variables, which meant that at most we had a cache with one
entry which got destroyed along with the parser. Regardless, using
lazy vars is still simpler.
This removes the only usage of ArrayWrapper in the Swift runtime, so
delete that too.
Make DFA.precedenceDfa be a "let" rather than a "var", and remove
setPrecedenceDfa. This field never varies after construction. The
code in setPrecedenceDfa was carried over from the Java runtime, but
it only threw an exception, and was deprecated. There's no need for
that in the Swift runtime.
Replace IntervalSet.setReadonly(Bool) with makeReadonly(). This
operation only ever works in one direction, and would throw an exception
if a caller attempted to make a read-only IntervalSet read-write again.
By changing the interface we remove the need to check this, and so we
don't need to declare the exception. Unlike in the Java runtime, we
need to declare the possibility of the exception at the callsite, so this
was pointlessly cluttering.
Make ParseTree, RuleNode, and TerminalNode be protocols rather than
classes. These had no useful functionality (which is not surprising,
since they are interfaces in the Java implementation) so there is
no need for them to be classes. This reduces the depth of the inheritance
tree.
Add a subscript getter to ParseTree (and corresponding implementations in
the concrete classes). This has two advantages over Tree.getChild(_: Int):
it can be declared to return ParseTree rather than Tree, and it can fault
on index-out-of-range rather than returning nil. Note that covariant
specialization of the return type is not supported through protocols in Swift
yet (https://bugs.swift.org/browse/SR-522). This means that ParseTree
cannot specialize Tree.getChild()'s return type in the way that the Java
implementation does.
Remove the return value from addChild / addErrorNode / addAnyChild.
This kind of chaining where a function returns its parameter does not fit
well with Swift's generics / protocols model.
Change ParserRuleContext.exception to be RecognitionException?
rather than AnyObject!. I don't know why it was declared that
way because the Java code uses RecognitionException.
Remove ParserRuleContext.addChild(Token) and addErrorNode(Token).
These are deprecated in the Java code and there was no need to
bring them over to the Swift runtime.
Fix ParserRuleContext.toInfoString, which was mangled when it was
ported from Java.
Various other tidyups: removal of useless type annotations, use of
if let, etc.