From 1413e6d72c49d6f481ef836e1e59f29e3618c0dc Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Fri, 15 Feb 2013 15:30:47 -0600 Subject: [PATCH 01/10] Add initial translation --- Antlr4.Runtime/ANTLRErrorListener.cs | 78 + Antlr4.Runtime/ANTLRErrorStrategy.cs | 157 ++ Antlr4.Runtime/ANTLRFileStream.cs | 97 + Antlr4.Runtime/ANTLRInputStream.cs | 308 +++ Antlr4.Runtime/Antlr4.Runtime.csproj | 126 + Antlr4.Runtime/Atn/ATN.cs | 223 ++ Antlr4.Runtime/Atn/ATNConfig.cs | 551 +++++ Antlr4.Runtime/Atn/ATNConfigSet.cs | 692 ++++++ Antlr4.Runtime/Atn/ATNSimulator.cs | 995 ++++++++ Antlr4.Runtime/Atn/ATNState.cs | 304 +++ .../Atn/AbstractPredicateTransition.cs | 42 + Antlr4.Runtime/Atn/ActionTransition.cs | 78 + Antlr4.Runtime/Atn/ArrayPredictionContext.cs | 274 +++ Antlr4.Runtime/Atn/AtomTransition.cs | 70 + Antlr4.Runtime/Atn/BasicBlockStartState.cs | 43 + Antlr4.Runtime/Atn/BasicState.cs | 43 + Antlr4.Runtime/Atn/BlockEndState.cs | 49 + Antlr4.Runtime/Atn/BlockStartState.cs | 44 + Antlr4.Runtime/Atn/DecisionState.cs | 43 + Antlr4.Runtime/Atn/EmptyPredictionContext.cs | 127 + Antlr4.Runtime/Atn/EpsilonTransition.cs | 63 + Antlr4.Runtime/Atn/LL1Analyzer.cs | 232 ++ Antlr4.Runtime/Atn/LexerATNSimulator.cs | 742 ++++++ Antlr4.Runtime/Atn/LoopEndState.cs | 46 + Antlr4.Runtime/Atn/NotSetTransition.cs | 58 + Antlr4.Runtime/Atn/OrderedATNConfigSet.cs | 69 + Antlr4.Runtime/Atn/ParserATNPathFinder.cs | 177 ++ Antlr4.Runtime/Atn/ParserATNSimulator.cs | 2182 +++++++++++++++++ Antlr4.Runtime/Atn/PlusBlockStartState.cs | 56 + Antlr4.Runtime/Atn/PlusLoopbackState.cs | 50 + .../Atn/PrecedencePredicateTransition.cs | 71 + Antlr4.Runtime/Atn/PredicateTransition.cs | 90 + Antlr4.Runtime/Atn/PredictionContext.cs | 542 ++++ Antlr4.Runtime/Atn/PredictionContextCache.cs | 212 ++ Antlr4.Runtime/Atn/RangeTransition.cs | 70 + Antlr4.Runtime/Atn/RuleStartState.cs | 48 + Antlr4.Runtime/Atn/RuleStopState.cs | 55 + Antlr4.Runtime/Atn/RuleTransition.cs | 75 + Antlr4.Runtime/Atn/SemanticContext.cs | 418 ++++ Antlr4.Runtime/Atn/SetTransition.cs | 76 + Antlr4.Runtime/Atn/SimulatorState.cs | 57 + .../Atn/SingletonPredictionContext.cs | 126 + Antlr4.Runtime/Atn/StarBlockStartState.cs | 44 + Antlr4.Runtime/Atn/StarLoopEntryState.cs | 44 + Antlr4.Runtime/Atn/StarLoopbackState.cs | 47 + Antlr4.Runtime/Atn/TokensStartState.cs | 43 + Antlr4.Runtime/Atn/Transition.cs | 134 + Antlr4.Runtime/Atn/WildcardTransition.cs | 58 + Antlr4.Runtime/BailErrorStrategy.cs | 93 + Antlr4.Runtime/BaseErrorListener.cs | 61 + Antlr4.Runtime/BufferedTokenStream.cs | 661 +++++ Antlr4.Runtime/CharStream.cs | 112 + Antlr4.Runtime/CommonToken.cs | 255 ++ Antlr4.Runtime/CommonTokenFactory.cs | 92 + Antlr4.Runtime/CommonTokenStream.cs | 156 ++ Antlr4.Runtime/ConsoleErrorListener.cs | 47 + Antlr4.Runtime/DefaultErrorStrategy.cs | 549 +++++ Antlr4.Runtime/Dependents.cs | 49 + Antlr4.Runtime/Dfa/AbstractEdgeMap.cs | 117 + Antlr4.Runtime/Dfa/ArrayEdgeMap.cs | 272 ++ Antlr4.Runtime/Dfa/DFA.cs | 144 ++ Antlr4.Runtime/Dfa/DFASerializer.cs | 222 ++ Antlr4.Runtime/Dfa/DFAState.cs | 349 +++ Antlr4.Runtime/Dfa/EdgeMap.cs | 67 + Antlr4.Runtime/Dfa/LexerDFASerializer.cs | 48 + Antlr4.Runtime/Dfa/SingletonEdgeMap.cs | 238 ++ Antlr4.Runtime/Dfa/SparseEdgeMap.cs | 293 +++ Antlr4.Runtime/DiagnosticErrorListener.cs | 73 + Antlr4.Runtime/FailedPredicateException.cs | 110 + Antlr4.Runtime/InputMismatchException.cs | 54 + Antlr4.Runtime/IntStream.cs | 362 +++ Antlr4.Runtime/Lexer.cs | 567 +++++ Antlr4.Runtime/LexerNoViableAltException.cs | 86 + .../Misc/AbstractEqualityComparator.cs | 50 + Antlr4.Runtime/Misc/Args.cs | 57 + Antlr4.Runtime/Misc/Array2DHashSet.cs | 692 ++++++ Antlr4.Runtime/Misc/DoubleKeyMap.cs | 108 + Antlr4.Runtime/Misc/EqualityComparator.cs | 74 + Antlr4.Runtime/Misc/FlexibleHashMap.cs | 397 +++ Antlr4.Runtime/Misc/Func0.cs | 40 + Antlr4.Runtime/Misc/Func1.cs | 40 + Antlr4.Runtime/Misc/IntSet.cs | 92 + Antlr4.Runtime/Misc/IntegerList.cs | 346 +++ Antlr4.Runtime/Misc/IntegerStack.cs | 65 + Antlr4.Runtime/Misc/Interval.cs | 226 ++ Antlr4.Runtime/Misc/IntervalSet.cs | 821 +++++++ Antlr4.Runtime/Misc/MultiMap.cs | 66 + .../Misc/ObjectEqualityComparator.cs | 97 + Antlr4.Runtime/Misc/OrderedHashSet.cs | 164 ++ Antlr4.Runtime/Misc/ParseCanceledException.cs | 71 + Antlr4.Runtime/Misc/Predicate.cs | 40 + Antlr4.Runtime/Misc/RuleDependencyChecker.cs | 327 +++ .../Misc/RuleDependencyProcessor.cs | 865 +++++++ Antlr4.Runtime/Misc/TestRig.cs | 337 +++ Antlr4.Runtime/Misc/Tuple.cs | 93 + Antlr4.Runtime/Misc/Utils.cs | 258 ++ Antlr4.Runtime/NoViableAltException.cs | 97 + Antlr4.Runtime/Parser.cs | 922 +++++++ Antlr4.Runtime/ParserErrorListener.cs | 71 + Antlr4.Runtime/ParserRuleContext.cs | 382 +++ Antlr4.Runtime/ProxyErrorListener.cs | 62 + Antlr4.Runtime/ProxyParserErrorListener.cs | 91 + Antlr4.Runtime/RecognitionException.cs | 155 ++ Antlr4.Runtime/Recognizer.cs | 219 ++ Antlr4.Runtime/RuleContext.cs | 324 +++ Antlr4.Runtime/Token.cs | 151 ++ Antlr4.Runtime/TokenFactory.cs | 59 + Antlr4.Runtime/TokenSource.cs | 96 + Antlr4.Runtime/TokenStream.cs | 245 ++ Antlr4.Runtime/TokenStreamRewriter.cs | 745 ++++++ .../Tree/AbstractParseTreeVisitor.cs | 231 ++ Antlr4.Runtime/Tree/ErrorNode.cs | 38 + Antlr4.Runtime/Tree/ErrorNodeImpl.cs | 58 + Antlr4.Runtime/Tree/ParseTree.cs | 82 + Antlr4.Runtime/Tree/ParseTreeListener.cs | 46 + Antlr4.Runtime/Tree/ParseTreeProperty.cs | 71 + Antlr4.Runtime/Tree/ParseTreeVisitor.cs | 97 + Antlr4.Runtime/Tree/ParseTreeWalker.cs | 88 + Antlr4.Runtime/Tree/RuleNode.cs | 42 + Antlr4.Runtime/Tree/SyntaxTree.cs | 64 + Antlr4.Runtime/Tree/TerminalNode.cs | 42 + Antlr4.Runtime/Tree/TerminalNodeImpl.cs | 123 + Antlr4.Runtime/Tree/Tree.cs | 88 + Antlr4.Runtime/Tree/Trees.cs | 215 ++ Antlr4.Runtime/UnbufferedCharStream.cs | 438 ++++ Antlr4.Runtime/UnbufferedTokenStream.cs | 422 ++++ Antlr4.Runtime/WritableToken.cs | 49 + 127 files changed, 26445 insertions(+) create mode 100644 Antlr4.Runtime/ANTLRErrorListener.cs create mode 100644 Antlr4.Runtime/ANTLRErrorStrategy.cs create mode 100644 Antlr4.Runtime/ANTLRFileStream.cs create mode 100644 Antlr4.Runtime/ANTLRInputStream.cs create mode 100644 Antlr4.Runtime/Atn/ATN.cs create mode 100644 Antlr4.Runtime/Atn/ATNConfig.cs create mode 100644 Antlr4.Runtime/Atn/ATNConfigSet.cs create mode 100644 Antlr4.Runtime/Atn/ATNSimulator.cs create mode 100644 Antlr4.Runtime/Atn/ATNState.cs create mode 100644 Antlr4.Runtime/Atn/AbstractPredicateTransition.cs create mode 100644 Antlr4.Runtime/Atn/ActionTransition.cs create mode 100644 Antlr4.Runtime/Atn/ArrayPredictionContext.cs create mode 100644 Antlr4.Runtime/Atn/AtomTransition.cs create mode 100644 Antlr4.Runtime/Atn/BasicBlockStartState.cs create mode 100644 Antlr4.Runtime/Atn/BasicState.cs create mode 100644 Antlr4.Runtime/Atn/BlockEndState.cs create mode 100644 Antlr4.Runtime/Atn/BlockStartState.cs create mode 100644 Antlr4.Runtime/Atn/DecisionState.cs create mode 100644 Antlr4.Runtime/Atn/EmptyPredictionContext.cs create mode 100644 Antlr4.Runtime/Atn/EpsilonTransition.cs create mode 100644 Antlr4.Runtime/Atn/LL1Analyzer.cs create mode 100644 Antlr4.Runtime/Atn/LexerATNSimulator.cs create mode 100644 Antlr4.Runtime/Atn/LoopEndState.cs create mode 100644 Antlr4.Runtime/Atn/NotSetTransition.cs create mode 100644 Antlr4.Runtime/Atn/OrderedATNConfigSet.cs create mode 100644 Antlr4.Runtime/Atn/ParserATNPathFinder.cs create mode 100644 Antlr4.Runtime/Atn/ParserATNSimulator.cs create mode 100644 Antlr4.Runtime/Atn/PlusBlockStartState.cs create mode 100644 Antlr4.Runtime/Atn/PlusLoopbackState.cs create mode 100644 Antlr4.Runtime/Atn/PrecedencePredicateTransition.cs create mode 100644 Antlr4.Runtime/Atn/PredicateTransition.cs create mode 100644 Antlr4.Runtime/Atn/PredictionContext.cs create mode 100644 Antlr4.Runtime/Atn/PredictionContextCache.cs create mode 100644 Antlr4.Runtime/Atn/RangeTransition.cs create mode 100644 Antlr4.Runtime/Atn/RuleStartState.cs create mode 100644 Antlr4.Runtime/Atn/RuleStopState.cs create mode 100644 Antlr4.Runtime/Atn/RuleTransition.cs create mode 100644 Antlr4.Runtime/Atn/SemanticContext.cs create mode 100644 Antlr4.Runtime/Atn/SetTransition.cs create mode 100644 Antlr4.Runtime/Atn/SimulatorState.cs create mode 100644 Antlr4.Runtime/Atn/SingletonPredictionContext.cs create mode 100644 Antlr4.Runtime/Atn/StarBlockStartState.cs create mode 100644 Antlr4.Runtime/Atn/StarLoopEntryState.cs create mode 100644 Antlr4.Runtime/Atn/StarLoopbackState.cs create mode 100644 Antlr4.Runtime/Atn/TokensStartState.cs create mode 100644 Antlr4.Runtime/Atn/Transition.cs create mode 100644 Antlr4.Runtime/Atn/WildcardTransition.cs create mode 100644 Antlr4.Runtime/BailErrorStrategy.cs create mode 100644 Antlr4.Runtime/BaseErrorListener.cs create mode 100644 Antlr4.Runtime/BufferedTokenStream.cs create mode 100644 Antlr4.Runtime/CharStream.cs create mode 100644 Antlr4.Runtime/CommonToken.cs create mode 100644 Antlr4.Runtime/CommonTokenFactory.cs create mode 100644 Antlr4.Runtime/CommonTokenStream.cs create mode 100644 Antlr4.Runtime/ConsoleErrorListener.cs create mode 100644 Antlr4.Runtime/DefaultErrorStrategy.cs create mode 100644 Antlr4.Runtime/Dependents.cs create mode 100644 Antlr4.Runtime/Dfa/AbstractEdgeMap.cs create mode 100644 Antlr4.Runtime/Dfa/ArrayEdgeMap.cs create mode 100644 Antlr4.Runtime/Dfa/DFA.cs create mode 100644 Antlr4.Runtime/Dfa/DFASerializer.cs create mode 100644 Antlr4.Runtime/Dfa/DFAState.cs create mode 100644 Antlr4.Runtime/Dfa/EdgeMap.cs create mode 100644 Antlr4.Runtime/Dfa/LexerDFASerializer.cs create mode 100644 Antlr4.Runtime/Dfa/SingletonEdgeMap.cs create mode 100644 Antlr4.Runtime/Dfa/SparseEdgeMap.cs create mode 100644 Antlr4.Runtime/DiagnosticErrorListener.cs create mode 100644 Antlr4.Runtime/FailedPredicateException.cs create mode 100644 Antlr4.Runtime/InputMismatchException.cs create mode 100644 Antlr4.Runtime/IntStream.cs create mode 100644 Antlr4.Runtime/Lexer.cs create mode 100644 Antlr4.Runtime/LexerNoViableAltException.cs create mode 100644 Antlr4.Runtime/Misc/AbstractEqualityComparator.cs create mode 100644 Antlr4.Runtime/Misc/Args.cs create mode 100644 Antlr4.Runtime/Misc/Array2DHashSet.cs create mode 100644 Antlr4.Runtime/Misc/DoubleKeyMap.cs create mode 100644 Antlr4.Runtime/Misc/EqualityComparator.cs create mode 100644 Antlr4.Runtime/Misc/FlexibleHashMap.cs create mode 100644 Antlr4.Runtime/Misc/Func0.cs create mode 100644 Antlr4.Runtime/Misc/Func1.cs create mode 100644 Antlr4.Runtime/Misc/IntSet.cs create mode 100644 Antlr4.Runtime/Misc/IntegerList.cs create mode 100644 Antlr4.Runtime/Misc/IntegerStack.cs create mode 100644 Antlr4.Runtime/Misc/Interval.cs create mode 100644 Antlr4.Runtime/Misc/IntervalSet.cs create mode 100644 Antlr4.Runtime/Misc/MultiMap.cs create mode 100644 Antlr4.Runtime/Misc/ObjectEqualityComparator.cs create mode 100644 Antlr4.Runtime/Misc/OrderedHashSet.cs create mode 100644 Antlr4.Runtime/Misc/ParseCanceledException.cs create mode 100644 Antlr4.Runtime/Misc/Predicate.cs create mode 100644 Antlr4.Runtime/Misc/RuleDependencyChecker.cs create mode 100644 Antlr4.Runtime/Misc/RuleDependencyProcessor.cs create mode 100644 Antlr4.Runtime/Misc/TestRig.cs create mode 100644 Antlr4.Runtime/Misc/Tuple.cs create mode 100644 Antlr4.Runtime/Misc/Utils.cs create mode 100644 Antlr4.Runtime/NoViableAltException.cs create mode 100644 Antlr4.Runtime/Parser.cs create mode 100644 Antlr4.Runtime/ParserErrorListener.cs create mode 100644 Antlr4.Runtime/ParserRuleContext.cs create mode 100644 Antlr4.Runtime/ProxyErrorListener.cs create mode 100644 Antlr4.Runtime/ProxyParserErrorListener.cs create mode 100644 Antlr4.Runtime/RecognitionException.cs create mode 100644 Antlr4.Runtime/Recognizer.cs create mode 100644 Antlr4.Runtime/RuleContext.cs create mode 100644 Antlr4.Runtime/Token.cs create mode 100644 Antlr4.Runtime/TokenFactory.cs create mode 100644 Antlr4.Runtime/TokenSource.cs create mode 100644 Antlr4.Runtime/TokenStream.cs create mode 100644 Antlr4.Runtime/TokenStreamRewriter.cs create mode 100644 Antlr4.Runtime/Tree/AbstractParseTreeVisitor.cs create mode 100644 Antlr4.Runtime/Tree/ErrorNode.cs create mode 100644 Antlr4.Runtime/Tree/ErrorNodeImpl.cs create mode 100644 Antlr4.Runtime/Tree/ParseTree.cs create mode 100644 Antlr4.Runtime/Tree/ParseTreeListener.cs create mode 100644 Antlr4.Runtime/Tree/ParseTreeProperty.cs create mode 100644 Antlr4.Runtime/Tree/ParseTreeVisitor.cs create mode 100644 Antlr4.Runtime/Tree/ParseTreeWalker.cs create mode 100644 Antlr4.Runtime/Tree/RuleNode.cs create mode 100644 Antlr4.Runtime/Tree/SyntaxTree.cs create mode 100644 Antlr4.Runtime/Tree/TerminalNode.cs create mode 100644 Antlr4.Runtime/Tree/TerminalNodeImpl.cs create mode 100644 Antlr4.Runtime/Tree/Tree.cs create mode 100644 Antlr4.Runtime/Tree/Trees.cs create mode 100644 Antlr4.Runtime/UnbufferedCharStream.cs create mode 100644 Antlr4.Runtime/UnbufferedTokenStream.cs create mode 100644 Antlr4.Runtime/WritableToken.cs diff --git a/Antlr4.Runtime/ANTLRErrorListener.cs b/Antlr4.Runtime/ANTLRErrorListener.cs new file mode 100644 index 000000000..c73bf3bbb --- /dev/null +++ b/Antlr4.Runtime/ANTLRErrorListener.cs @@ -0,0 +1,78 @@ +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +using Antlr4.Runtime; +using Sharpen; + +namespace Antlr4.Runtime +{ + /// How to emit recognition errors + public interface ANTLRErrorListener + { + /// Upon syntax error, notify any interested parties. + /// + /// Upon syntax error, notify any interested parties. This is not + /// how to recover from errors or compute error messages. The + /// parser 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. + /// The 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). + /// + /// + /// What parser got the error. From this + /// object, you can access the context as well + /// as the input stream. + /// + /// + /// The offending token in the input token + /// stream, unless recognizer is a lexer (then it's null) If + /// no viable alternative error, e has token at which we + /// started production for the decision. + /// + /// + /// At what line in input to the error occur? This always refers to + /// stopTokenIndex + /// + /// At what character position within that line did the error occur. + /// + /// The message to emit + /// + /// 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. + /// + void SyntaxError(Recognizer recognizer, T offendingSymbol, int line + , int charPositionInLine, string msg, RecognitionException e) where T:Symbol; + } +} diff --git a/Antlr4.Runtime/ANTLRErrorStrategy.cs b/Antlr4.Runtime/ANTLRErrorStrategy.cs new file mode 100644 index 000000000..669a0030d --- /dev/null +++ b/Antlr4.Runtime/ANTLRErrorStrategy.cs @@ -0,0 +1,157 @@ +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +using Antlr4.Runtime; +using Sharpen; + +namespace Antlr4.Runtime +{ + /// + /// The interface for defining strategies to deal with syntax errors + /// encountered during a parse by ANTLR-generated parsers and tree parsers. + /// + /// + /// The interface for defining strategies to deal with syntax errors + /// encountered during a parse by ANTLR-generated parsers and tree parsers. + /// We distinguish between three different kinds of errors: + /// o The parser could not figure out which path to take in the ATN + /// (none of the available alternatives could possibly match) + /// o The current input does not match what we were looking for. + /// o A predicate evaluated to false. + /// The default implementation of this interface reports errors to any + /// error listeners of the parser. It also handles single token insertion + /// and deletion for mismatched elements. + /// We pass in the parser to each function so that the same strategy + /// can be shared between multiple parsers running at the same time. + /// This is just for flexibility, not that we need it for the default system. + /// TODO: To bail out upon first error, simply rethrow e? + /// TODO: what to do about lexers + /// + public interface ANTLRErrorStrategy + { + /// + /// When matching elements within alternative, use this method + /// to recover. + /// + /// + /// When matching elements within alternative, use this method + /// to recover. The default implementation uses single token + /// insertion and deletion. If you want to change the way ANTLR + /// response to mismatched element errors within an alternative, + /// implement this method. + /// From the recognizer, we can get the input stream to get + /// the current input symbol and we can get the current context. + /// That context gives us the current state within the ATN. + /// From that state, we can look at its transition to figure out + /// what was expected. + /// Because we can recover from a single token deletions by + /// "inserting" tokens, we need to specify what that implicitly created + /// token is. We use object, because it could be a tree node. + /// + /// + Token RecoverInline(Parser recognizer); + + /// + /// Resynchronize the parser by consuming tokens until we find one + /// in the resynchronization set--loosely the set of tokens that can follow + /// the current rule. + /// + /// + /// Resynchronize the parser by consuming tokens until we find one + /// in the resynchronization set--loosely the set of tokens that can follow + /// the current rule. The exception contains info you might want to + /// use to recover better. + /// + void Recover(Parser recognizer, RecognitionException e); + + /// + /// Make sure that the current lookahead symbol is consistent with + /// what were expecting at this point in the ATN. + /// + /// + /// Make 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. + /// Implements Jim Idle's magic sync mechanism in closures and optional + /// subrules. E.g., + /// a : sync ( stuff sync )* ; + /// sync : {consume to what can follow sync} ; + /// 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 + /// classDef : 'class' ID '{' member* '}' + /// 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. + /// 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 { }. + /// + void Sync(Parser recognizer); + + /// Notify handler that parser has entered an error state. + /// + /// Notify handler that parser has entered an error state. The + /// parser currently doesn't call this--the handler itself calls this + /// in report error methods. But, for symmetry with endErrorCondition, + /// this method is in the interface. + /// + void BeginErrorCondition(Parser recognizer); + + /// + /// Is the parser in the process of recovering from an error? Upon + /// a syntax error, the parser enters recovery mode and stays there until + /// the next successful match of a token. + /// + /// + /// Is the parser in the process of recovering from an error? Upon + /// a syntax error, the parser enters recovery mode and stays there until + /// the next successful match of a token. In this way, we can + /// avoid sending out spurious error messages. We only want one error + /// message per syntax error + /// + bool InErrorRecoveryMode(Parser recognizer); + + /// Reset the error handler. + /// + /// Reset the error handler. Call this when the parser + /// matches a valid token (indicating no longer in recovery mode) + /// and from its own reset method. + /// + void EndErrorCondition(Parser recognizer); + + /// Report any kind of RecognitionException. + /// Report any kind of RecognitionException. + /// + void ReportError(Parser recognizer, RecognitionException e); + } +} diff --git a/Antlr4.Runtime/ANTLRFileStream.cs b/Antlr4.Runtime/ANTLRFileStream.cs new file mode 100644 index 000000000..1fab3edaa --- /dev/null +++ b/Antlr4.Runtime/ANTLRFileStream.cs @@ -0,0 +1,97 @@ +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +using System.IO; +using Antlr4.Runtime; +using Sharpen; + +namespace Antlr4.Runtime +{ + /// + /// This is an ANTLRInputStream that is loaded from a file + /// all at once when you construct the object. + /// + /// + /// This is an ANTLRInputStream that is loaded from a file + /// all at once when you construct the object. This is a special case + /// since we know the exact size of the object to load. We can avoid lots + /// of data copying. + /// + public class ANTLRFileStream : ANTLRInputStream + { + protected internal string fileName; + + /// + public ANTLRFileStream(string fileName) : this(fileName, null) + { + } + + /// + public ANTLRFileStream(string fileName, string encoding) + { + this.fileName = fileName; + Load(fileName, encoding); + } + + /// + public virtual void Load(string fileName, string encoding) + { + if (fileName == null) + { + return; + } + FilePath f = new FilePath(fileName); + int size = (int)f.Length(); + InputStreamReader isr; + FileInputStream fis = new FileInputStream(fileName); + if (encoding != null) + { + isr = new InputStreamReader(fis, encoding); + } + else + { + isr = new InputStreamReader(fis); + } + try + { + data = new char[size]; + base.n = isr.Read(data); + } + finally + { + isr.Close(); + } + } + + public override string GetSourceName() + { + return fileName; + } + } +} diff --git a/Antlr4.Runtime/ANTLRInputStream.cs b/Antlr4.Runtime/ANTLRInputStream.cs new file mode 100644 index 000000000..4f614b9af --- /dev/null +++ b/Antlr4.Runtime/ANTLRInputStream.cs @@ -0,0 +1,308 @@ +/* + * [The "BSD license"] + * Copyright (c) 2013 Terence Parr + * Copyright (c) 2013 Sam Harwell + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +using System; +using System.IO; +using Antlr4.Runtime; +using Antlr4.Runtime.Misc; +using Sharpen; + +namespace Antlr4.Runtime +{ + /// Vacuum all input from a Reader/InputStream and then treat it like a char[] buffer. + /// + /// + /// Vacuum all input from a Reader/InputStream and then treat it like a char[] buffer. + /// Can also pass in a string or char[] to use. + /// If you need encoding, pass in stream/reader with correct encoding. + /// + public class ANTLRInputStream : CharStream + { + public const int ReadBufferSize = 1024; + + public const int InitialBufferSize = 1024; + + /// The data being scanned + protected internal char[] data; + + /// How many characters are actually in the buffer + protected internal int n; + + /// 0..n-1 index into string of next char + protected internal int p = 0; + + /// What is name or source of this char stream? + public string name; + + public ANTLRInputStream() + { + } + + /// Copy data in string to a local char array + public ANTLRInputStream(string input) + { + this.data = input.ToCharArray(); + this.n = input.Length; + } + + /// This is the preferred constructor for strings as no data is copied + public ANTLRInputStream(char[] data, int numberOfActualCharsInArray) + { + this.data = data; + this.n = numberOfActualCharsInArray; + } + + /// + public ANTLRInputStream(StreamReader r) : this(r, InitialBufferSize, ReadBufferSize + ) + { + } + + /// + public ANTLRInputStream(StreamReader r, int initialSize) : this(r, initialSize, ReadBufferSize + ) + { + } + + /// + public ANTLRInputStream(StreamReader r, int initialSize, int readChunkSize) + { + Load(r, initialSize, readChunkSize); + } + + /// + public ANTLRInputStream(InputStream input) : this(new InputStreamReader(input), InitialBufferSize + ) + { + } + + /// + public ANTLRInputStream(InputStream input, int initialSize) : this(new InputStreamReader + (input), initialSize) + { + } + + /// + public ANTLRInputStream(InputStream input, int initialSize, int readChunkSize) : + this(new InputStreamReader(input), initialSize, readChunkSize) + { + } + + /// + public virtual void Load(StreamReader r, int size, int readChunkSize) + { + if (r == null) + { + return; + } + if (size <= 0) + { + size = InitialBufferSize; + } + if (readChunkSize <= 0) + { + readChunkSize = ReadBufferSize; + } + // System.out.println("load "+size+" in chunks of "+readChunkSize); + try + { + // alloc initial buffer size. + data = new char[size]; + // read all the data in chunks of readChunkSize + int numRead = 0; + int p = 0; + do + { + if (p + readChunkSize > data.Length) + { + // overflow? + // System.out.println("### overflow p="+p+", data.length="+data.length); + data = Arrays.CopyOf(data, data.Length * 2); + } + numRead = r.Read(data, p, readChunkSize); + // System.out.println("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; + } + finally + { + //System.out.println("n="+n); + 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. + /// + /// + /// 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 virtual void Reset() + { + p = 0; + } + + public virtual void Consume() + { + if (p >= n) + { + System.Diagnostics.Debug.Assert(La(1) == IntStream.Eof); + throw new InvalidOperationException("cannot consume EOF"); + } + //System.out.println("prev p="+p+", c="+(char)data[p]); + if (p < n) + { + p++; + } + } + + //System.out.println("p moves to "+p+" (c='"+(char)data[p]+"')"); + public virtual int La(int i) + { + if (i == 0) + { + return 0; + } + // undefined + if (i < 0) + { + i++; + // e.g., translate LA(-1) to use offset i=0; then data[p+0-1] + if ((p + i - 1) < 0) + { + return IntStream.Eof; + } + } + // invalid; no char before first char + if ((p + i - 1) >= n) + { + //System.out.println("char LA("+i+")=EOF; p="+p); + return IntStream.Eof; + } + //System.out.println("char LA("+i+")="+(char)data[p+i-1]+"; p="+p); + //System.out.println("LA("+i+"); p="+p+" n="+n+" data.length="+data.length); + return data[p + i - 1]; + } + + public virtual int Lt(int i) + { + return La(i); + } + + /// + /// Return the current input symbol index 0..n where n indicates the + /// last symbol has been read. + /// + /// + /// 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 virtual int Index() + { + return p; + } + + public virtual int Size() + { + return n; + } + + /// mark/release do nothing; we have entire buffer + public virtual int Mark() + { + return -1; + } + + public virtual void Release(int marker) + { + } + + /// + /// consume() ahead until p==index; can't just set p=index as we must + /// update line and charPositionInLine. + /// + /// + /// 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 virtual void Seek(int index) + { + if (index <= p) + { + p = index; + // just jump; don't update stream state (line, ...) + return; + } + // seek forward, consume until p hits index + while (p < index && index < n) + { + Consume(); + } + } + + public override string GetText(Interval interval) + { + int start = interval.a; + int stop = interval.b; + if (stop >= n) + { + stop = n - 1; + } + int count = stop - start + 1; + if (start >= n) + { + return string.Empty; + } + // System.err.println("data: "+Arrays.toString(data)+", n="+n+ + // ", start="+start+ + // ", stop="+stop); + return new string(data, start, count); + } + + public virtual string GetSourceName() + { + return name; + } + + public override string ToString() + { + return new string(data); + } + } +} diff --git a/Antlr4.Runtime/Antlr4.Runtime.csproj b/Antlr4.Runtime/Antlr4.Runtime.csproj index da813927f..b66dd38c9 100644 --- a/Antlr4.Runtime/Antlr4.Runtime.csproj +++ b/Antlr4.Runtime/Antlr4.Runtime.csproj @@ -39,7 +39,133 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +