/* 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 "antlr4-common.h" namespace antlr4 { namespace tree { namespace pattern { /// Represents the result of matching a ParseTree against a tree pattern. class ANTLR4CPP_PUBLIC ParseTreeMatch { private: /// This is the backing field for getTree(). ParseTree *_tree; /// This is the backing field for getPattern(). const ParseTreePattern &_pattern; /// This is the backing field for getLabels(). std::map> _labels; /// This is the backing field for getMismatchedNode(). ParseTree *_mismatchedNode; public: /// /// Constructs a new instance of from the specified /// parse tree and pattern. /// /// The parse tree to match against the pattern. /// The parse tree pattern. /// A mapping from label names to collections of /// objects located by the tree pattern matching process. /// The first node which failed to match the tree /// pattern during the matching process. /// /// if {@code tree} is {@code null} /// if {@code pattern} is {@code null} /// if {@code labels} is {@code null} ParseTreeMatch(ParseTree *tree, ParseTreePattern const& pattern, const std::map> &labels, ParseTree *mismatchedNode); ParseTreeMatch(ParseTreeMatch const&) = default; virtual ~ParseTreeMatch(); /// /// Get the last node associated with a specific {@code label}. ///

/// For example, for pattern {@code }, {@code get("id")} returns the /// node matched for that {@code ID}. If more than one node /// matched the specified label, only the last is returned. If there is /// no node associated with the label, this returns {@code null}. ///

/// Pattern tags like {@code } and {@code } without labels are /// considered to be labeled with {@code ID} and {@code expr}, respectively. ///

/// The label to check. /// /// The last to match a tag with the specified /// label, or {@code null} if no parse tree matched a tag with the label. virtual ParseTree* get(const std::string &label); /// /// Return all nodes matching a rule or token tag with the specified label. ///

/// If the {@code label} is the name of a parser rule or token in the /// grammar, the resulting list will contain both the parse trees matching /// rule or tags explicitly labeled with the label and the complete set of /// parse trees matching the labeled and unlabeled tags in the pattern for /// the parser rule or token. For example, if {@code label} is {@code "foo"}, /// the result will contain all of the following. /// ///

    ///
  • Parse tree nodes matching tags of the form {@code } and /// {@code }.
  • ///
  • Parse tree nodes matching tags of the form {@code }.
  • ///
  • Parse tree nodes matching tags of the form {@code }.
  • ///
///
/// The label. /// /// A collection of all nodes matching tags with /// the specified {@code label}. If no nodes matched the label, an empty list /// is returned. virtual std::vector getAll(const std::string &label); /// /// Return a mapping from label → [list of nodes]. ///

/// The map includes special entries corresponding to the names of rules and /// tokens referenced in tags in the original pattern. For additional /// information, see the description of . ///

/// A mapping from labels to parse tree nodes. If the parse tree /// pattern did not contain any rule or token tags, this map will be empty. virtual std::map>& getLabels(); /// /// Get the node at which we first detected a mismatch. /// /// the node at which we first detected a mismatch, or {@code null} /// if the match was successful. virtual ParseTree* getMismatchedNode(); /// /// Gets a value indicating whether the match operation succeeded. /// /// {@code true} if the match operation succeeded; otherwise, /// {@code false}. virtual bool succeeded(); /// /// Get the tree pattern we are matching against. /// /// The tree pattern we are matching against. virtual const ParseTreePattern& getPattern(); /// /// Get the parse tree we are trying to match to a pattern. /// /// The we are trying to match to a pattern. virtual ParseTree* getTree(); virtual std::string toString(); }; } // namespace pattern } // namespace tree } // namespace antlr4