/* Copyright (c) 2012-2017 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. */ #pragma once #include "Recognizer.h" #include "tree/ParseTreeListener.h" #include "tree/ParseTree.h" #include "TokenStream.h" #include "TokenSource.h" #include "misc/Interval.h" namespace antlr4 { /// This is all the parsing support code essentially; most of it is error recovery stuff. class ANTLR4CPP_PUBLIC Parser : public Recognizer { public: class TraceListener : public tree::ParseTreeListener { public: TraceListener(Parser *outerInstance); virtual ~TraceListener(); virtual void enterEveryRule(ParserRuleContext *ctx) override; virtual void visitTerminal(tree::TerminalNode *node) override; virtual void visitErrorNode(tree::ErrorNode *node) override; virtual void exitEveryRule(ParserRuleContext *ctx) override; private: Parser *const outerInstance; }; class TrimToSizeListener : public tree::ParseTreeListener { public: static TrimToSizeListener INSTANCE; virtual ~TrimToSizeListener(); virtual void enterEveryRule(ParserRuleContext *ctx) override; virtual void visitTerminal(tree::TerminalNode *node) override; virtual void visitErrorNode(tree::ErrorNode *node) override; virtual void exitEveryRule(ParserRuleContext *ctx) override; }; Parser(TokenStream *input); virtual ~Parser(); /// reset the parser's state virtual void reset(); /// /// Match current input symbol against {@code ttype}. If the symbol type /// matches, and are /// called to complete the match process. /// /// If the symbol type does not match, /// is called on the current error /// strategy to attempt recovery. If is /// {@code true} and the token index of the symbol returned by /// is -1, the symbol is added to /// the parse tree by calling {@link #createErrorNode(ParserRuleContext, Token)} then /// {@link ParserRuleContext#addErrorNode(ErrorNode)}. /// /// the token type to match /// the matched symbol /// if the current input symbol did not match /// {@code ttype} and the error strategy could not recover from the /// mismatched symbol virtual Token* match(size_t ttype); /// /// Match current input symbol as a wildcard. If the symbol type matches /// (i.e. has a value greater than 0), /// and are called to complete the match process. ///

/// If the symbol type does not match, /// is called on the current error /// strategy to attempt recovery. If is /// {@code true} and the token index of the symbol returned by /// is -1, the symbol is added to /// the parse tree by calling . ///

/// the matched symbol /// if the current input symbol did not match /// a wildcard and the error strategy could not recover from the mismatched /// symbol virtual Token* matchWildcard(); /// /// Track the objects during the parse and hook /// them up using the list so that it /// forms a parse tree. The returned from the start /// rule represents the root of the parse tree. ///

/// Note that if we are not building parse trees, rule contexts only point /// upwards. When a rule exits, it returns the context but that gets garbage /// collected if nobody holds a reference. It points upwards but nobody /// points at it. ///

/// When we build parse trees, we are adding all of these contexts to /// list. Contexts are then not candidates /// for garbage collection. ///

virtual void setBuildParseTree(bool buildParseTrees); /// /// Gets whether or not a complete parse tree will be constructed while /// parsing. This property is {@code true} for a newly constructed parser. /// /// {@code true} if a complete parse tree will be constructed while /// parsing, otherwise {@code false} virtual bool getBuildParseTree(); /// /// Trim the internal lists of the parse tree during parsing to conserve memory. /// This property is set to {@code false} by default for a newly constructed parser. /// /// {@code true} to trim the capacity of the /// list to its size after a rule is parsed. virtual void setTrimParseTree(bool trimParseTrees); /// {@code true} if the list is trimmed /// using the default during the parse process. virtual bool getTrimParseTree(); virtual std::vector getParseListeners(); /// /// Registers {@code listener} to receive events during the parsing process. ///

/// To support output-preserving grammar transformations (including but not /// limited to left-recursion removal, automated left-factoring, and /// optimized code generation), calls to listener methods during the parse /// may differ substantially from calls made by /// used after the parse is complete. In /// particular, rule entry and exit events may occur in a different order /// during the parse than after the parser. In addition, calls to certain /// rule entry methods may be omitted. ///

/// With the following specific exceptions, calls to listener events are /// deterministic, i.e. for identical input the calls to listener /// methods will be the same. /// ///

    ///
  • Alterations to the grammar used to generate code may change the /// behavior of the listener calls.
  • ///
  • Alterations to the command line options passed to ANTLR 4 when /// generating the parser may change the behavior of the listener calls.
  • ///
  • Changing the version of the ANTLR Tool used to generate the parser /// may change the behavior of the listener calls.
  • ///
///
/// the listener to add /// /// if {@code} listener is {@code null} virtual void addParseListener(tree::ParseTreeListener *listener); /// /// Remove {@code listener} from the list of parse listeners. ///

/// If {@code listener} is {@code null} or has not been added as a parse /// listener, this method does nothing. ///

/// /// the listener to remove virtual void removeParseListener(tree::ParseTreeListener *listener); /// /// Remove all parse listeners. /// /// virtual void removeParseListeners(); /// /// Notify any parse listeners of an enter rule event. /// /// virtual void triggerEnterRuleEvent(); /// /// Notify any parse listeners of an exit rule event. /// /// virtual void triggerExitRuleEvent(); /// /// Gets the number of syntax errors reported during parsing. This value is /// incremented each time is called. /// /// virtual size_t getNumberOfSyntaxErrors(); virtual TokenFactory* getTokenFactory() override; /// /// Tell our token source and error strategy about a new way to create tokens. template void setTokenFactory(TokenFactory *factory) { _input->getTokenSource()->setTokenFactory(factory); } /// The ATN with bypass alternatives is expensive to create so we create it /// lazily. The ATN is owned by us. virtual const atn::ATN& getATNWithBypassAlts(); /// /// The preferred method of getting a tree pattern. For example, here's a /// sample use: /// ///
    /// ParseTree t = parser.expr();
    /// ParseTreePattern p = parser.compileParseTreePattern("+0", MyParser.RULE_expr);
    /// ParseTreeMatch m = p.match(t);
    /// String id = m.get("ID");
    /// 
///
virtual tree::pattern::ParseTreePattern compileParseTreePattern(const std::string &pattern, int patternRuleIndex); /// /// The same as but specify a /// rather than trying to deduce it from this parser. /// virtual tree::pattern::ParseTreePattern compileParseTreePattern(const std::string &pattern, int patternRuleIndex, Lexer *lexer); virtual Ref getErrorHandler(); virtual void setErrorHandler(Ref const& handler); virtual IntStream* getInputStream() override; void setInputStream(IntStream *input) override; virtual TokenStream* getTokenStream(); /// Set the token stream and reset the parser. virtual void setTokenStream(TokenStream *input); /// /// Match needs to return the current input symbol, which gets put /// into the label for the associated token ref; e.g., x=ID. /// virtual Token* getCurrentToken(); void notifyErrorListeners(const std::string &msg); virtual void notifyErrorListeners(Token *offendingToken, const std::string &msg, std::exception_ptr e); /// Consume and return the . ///

/// E.g., given the following input with {@code A} being the current /// lookahead symbol, this function moves the cursor to {@code B} and returns /// {@code A}. /// ///

    ///  A B
    ///  ^
    /// 
/// /// If the parser is not in error recovery mode, the consumed symbol is added /// to the parse tree using , and /// is called on any parse listeners. /// If the parser is in error recovery mode, the consumed symbol is /// added to the parse tree using {@link #createErrorNode(ParserRuleContext, Token)} then /// {@link ParserRuleContext#addErrorNode(ErrorNode)} and /// is called on any parse /// listeners. virtual Token* consume(); /// Always called by generated parsers upon entry to a rule. Access field /// get the current context. virtual void enterRule(ParserRuleContext *localctx, size_t state, size_t ruleIndex); void exitRule(); virtual void enterOuterAlt(ParserRuleContext *localctx, size_t altNum); /** * Get the precedence level for the top-most precedence rule. * * @return The precedence level for the top-most precedence rule, or -1 if * the parser context is not nested within a precedence rule. */ int getPrecedence() const; /// @deprecated Use /// instead. virtual void enterRecursionRule(ParserRuleContext *localctx, size_t ruleIndex); virtual void enterRecursionRule(ParserRuleContext *localctx, size_t state, size_t ruleIndex, int precedence); /** Like {@link #enterRule} but for recursive rules. * Make the current context the child of the incoming localctx. */ virtual void pushNewRecursionContext(ParserRuleContext *localctx, size_t state, size_t ruleIndex); virtual void unrollRecursionContexts(ParserRuleContext *parentctx); virtual ParserRuleContext* getInvokingContext(size_t ruleIndex); virtual ParserRuleContext* getContext(); virtual void setContext(ParserRuleContext *ctx); virtual bool precpred(RuleContext *localctx, int precedence) override; virtual bool inContext(const std::string &context); /// /// Checks whether or not {@code symbol} can follow the current state in the /// ATN. The behavior of this method is equivalent to the following, but is /// implemented such that the complete context-sensitive follow set does not /// need to be explicitly constructed. /// ///
    /// return getExpectedTokens().contains(symbol);
    /// 
///
/// the symbol type to check /// {@code true} if {@code symbol} can follow the current state in /// the ATN, otherwise {@code false}. virtual bool isExpectedToken(size_t symbol); bool isMatchedEOF() const; /// /// Computes the set of input symbols which could follow the current parser /// state and context, as given by and , /// respectively. /// /// virtual misc::IntervalSet getExpectedTokens(); virtual misc::IntervalSet getExpectedTokensWithinCurrentRule(); /// Get a rule's index (i.e., {@code RULE_ruleName} field) or INVALID_INDEX if not found. virtual size_t getRuleIndex(const std::string &ruleName); virtual ParserRuleContext* getRuleContext(); /// /// Return List<String> of the rule names in your parser instance /// leading up to a call to the current rule. You could override if /// you want more details such as the file/line info of where /// in the ATN a rule is invoked. /// /// This is very useful for error messages. /// virtual std::vector getRuleInvocationStack(); virtual std::vector getRuleInvocationStack(RuleContext *p); /// /// For debugging and other purposes. virtual std::vector getDFAStrings(); /// /// For debugging and other purposes. virtual void dumpDFA(); virtual std::string getSourceName(); atn::ParseInfo getParseInfo() const; /** * @since 4.3 */ void setProfile(bool profile); /// /// During a parse is sometimes useful to listen in on the rule entry and exit /// events as well as token matches. This is for quick and dirty debugging. /// virtual void setTrace(bool trace); /** * Gets whether a {@link TraceListener} is registered as a parse listener * for the parser. * * @see #setTrace(boolean) */ bool isTrace() const; tree::ParseTreeTracker& getTreeTracker() { return _tracker; } /** How to create a token leaf node associated with a parent. * Typically, the terminal node to create is not a function of the parent * but this method must still set the parent pointer of the terminal node * returned. I would prefer having {@link ParserRuleContext#addAnyChild(ParseTree)} * set the parent pointer, but the parent pointer is implementation dependent * and currently there is no setParent() in {@link TerminalNode} (and can't * add method in Java 1.7 without breaking backward compatibility). * * @since 4.7 */ tree::TerminalNode *createTerminalNode(Token *t); /** How to create an error node, given a token, associated with a parent. * Typically, the error node to create is not a function of the parent * but this method must still set the parent pointer of the terminal node * returned. I would prefer having {@link ParserRuleContext#addAnyChild(ParseTree)} * set the parent pointer, but the parent pointer is implementation dependent * and currently there is no setParent() in {@link ErrorNode} (and can't * add method in Java 1.7 without breaking backward compatibility). * * @since 4.7 */ tree::ErrorNode *createErrorNode(Token *t); protected: /// The ParserRuleContext object for the currently executing rule. /// This is always non-null during the parsing process. // ml: this is one of the contexts tracked in _allocatedContexts. ParserRuleContext *_ctx; /// The error handling strategy for the parser. The default is DefaultErrorStrategy. /// See also getErrorHandler. Ref _errHandler; /// /// The input stream. /// /// /// TokenStream *_input; std::vector _precedenceStack; /// /// Specifies whether or not the parser should construct a parse tree during /// the parsing process. The default value is {@code true}. /// /// /// bool _buildParseTrees; /// The list of listeners registered to receive /// events during the parse. /// std::vector _parseListeners; /// /// The number of syntax errors reported during parsing. This value is /// incremented each time is called. /// size_t _syntaxErrors; /** Indicates parser has match()ed EOF token. See {@link #exitRule()}. */ bool _matchedEOF; virtual void addContextToParseTree(); // All rule contexts created during a parse run. This is cleared when calling reset(). tree::ParseTreeTracker _tracker; private: /// This field maps from the serialized ATN string to the deserialized with /// bypass alternatives. /// /// static std::map, atn::ATN> bypassAltsAtnCache; /// When setTrace(true) is called, a reference to the /// TraceListener is stored here so it can be easily removed in a /// later call to setTrace(false). The listener itself is /// implemented as a parser listener so this field is not directly used by /// other parser methods. TraceListener *_tracer; void InitializeInstanceFields(); }; } // namespace antlr4