Skip to content

Commit

Permalink
Make it possible for RecognitionException to release the parser and
Browse files Browse the repository at this point in the history
token stream that triggered the error.

These are useful for error diagnostics, but if client code wants to throw
the RecognitionException but discard the parser and token stream, then
the fields in RecognitionException need to be cleared.

This adds RecognitionException.{clearRecognizer,clearInputStream} so that
client code can clear those fields if desired.  It also makes
RecognitionException.ctx weak, so it will go nil at the same time as
the parser is discarded.
  • Loading branch information
ewanmellor committed Oct 24, 2017
1 parent 7b5b785 commit da346c4
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions runtime/Swift/Sources/Antlr4/RecognitionException.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class RecognitionException<T:ATNSimulator> {
private final var recognizer: Recognizer<T>?
//Recognizer<AnyObject,ATNSimulator>? ;

private final let ctx: RuleContext?
private final weak var ctx: RuleContext?

private final let input: IntStream
private final var input: IntStream?

///
/// The current _org.antlr.v4.runtime.Token_ when an error occurred. Since not all streams
Expand Down Expand Up @@ -113,10 +113,13 @@ public class RecognitionException<T:ATNSimulator> {
/// where this exception was thrown, or `null` if the stream is not
/// available.
///
public func getInputStream() -> IntStream {
public func getInputStream() -> IntStream? {
return input
}

public func clearInputStream() {
input = nil
}

public func getOffendingToken() -> Token {
return offendingToken
Expand All @@ -137,4 +140,8 @@ public class RecognitionException<T:ATNSimulator> {
public func getRecognizer() -> Recognizer<T>? {
return recognizer
}

public func clearRecognizer() {
self.recognizer = nil
}
}

0 comments on commit da346c4

Please sign in to comment.