From c6ad2948bb98d42f8e0883ef82cd14cd2d5eda60 Mon Sep 17 00:00:00 2001 From: Patrick Schönberger Date: Sat, 14 Aug 2021 14:56:12 +0200 Subject: add antlr source code and ReadMe --- .../runtime/src/tree/AbstractParseTreeVisitor.h | 128 +++++++ .../runtime/src/tree/ErrorNode.cpp | 9 + .../runtime/src/tree/ErrorNode.h | 19 ++ .../runtime/src/tree/ErrorNodeImpl.cpp | 23 ++ .../runtime/src/tree/ErrorNodeImpl.h | 33 ++ .../runtime/src/tree/IterativeParseTreeWalker.cpp | 71 ++++ .../runtime/src/tree/IterativeParseTreeWalker.h | 53 +++ .../runtime/src/tree/ParseTree.cpp | 15 + .../runtime/src/tree/ParseTree.h | 102 ++++++ .../runtime/src/tree/ParseTreeListener.cpp | 9 + .../runtime/src/tree/ParseTreeListener.h | 39 +++ .../runtime/src/tree/ParseTreeProperty.h | 50 +++ .../runtime/src/tree/ParseTreeVisitor.cpp | 9 + .../runtime/src/tree/ParseTreeVisitor.h | 57 ++++ .../runtime/src/tree/ParseTreeWalker.cpp | 49 +++ .../runtime/src/tree/ParseTreeWalker.h | 55 +++ .../runtime/src/tree/TerminalNode.cpp | 9 + .../runtime/src/tree/TerminalNode.h | 32 ++ .../runtime/src/tree/TerminalNodeImpl.cpp | 57 ++++ .../runtime/src/tree/TerminalNodeImpl.h | 33 ++ .../runtime/src/tree/Trees.cpp | 241 +++++++++++++ .../runtime/src/tree/Trees.h | 78 +++++ .../runtime/src/tree/pattern/Chunk.cpp | 9 + .../runtime/src/tree/pattern/Chunk.h | 44 +++ .../runtime/src/tree/pattern/ParseTreeMatch.cpp | 69 ++++ .../runtime/src/tree/pattern/ParseTreeMatch.h | 132 ++++++++ .../runtime/src/tree/pattern/ParseTreePattern.cpp | 64 ++++ .../runtime/src/tree/pattern/ParseTreePattern.h | 105 ++++++ .../src/tree/pattern/ParseTreePatternMatcher.cpp | 371 +++++++++++++++++++++ .../src/tree/pattern/ParseTreePatternMatcher.h | 185 ++++++++++ .../runtime/src/tree/pattern/RuleTagToken.cpp | 77 +++++ .../runtime/src/tree/pattern/RuleTagToken.h | 117 +++++++ .../runtime/src/tree/pattern/TagChunk.cpp | 39 +++ .../runtime/src/tree/pattern/TagChunk.h | 86 +++++ .../runtime/src/tree/pattern/TextChunk.cpp | 28 ++ .../runtime/src/tree/pattern/TextChunk.h | 51 +++ .../runtime/src/tree/pattern/TokenTagToken.cpp | 36 ++ .../runtime/src/tree/pattern/TokenTagToken.h | 80 +++++ .../runtime/src/tree/xpath/XPath.cpp | 154 +++++++++ .../runtime/src/tree/xpath/XPath.h | 86 +++++ .../runtime/src/tree/xpath/XPathElement.cpp | 31 ++ .../runtime/src/tree/xpath/XPathElement.h | 40 +++ .../runtime/src/tree/xpath/XPathLexer.cpp | 173 ++++++++++ .../runtime/src/tree/xpath/XPathLexer.g4 | 64 ++++ .../runtime/src/tree/xpath/XPathLexer.h | 56 ++++ .../runtime/src/tree/xpath/XPathLexer.tokens | 12 + .../src/tree/xpath/XPathLexerErrorListener.cpp | 13 + .../src/tree/xpath/XPathLexerErrorListener.h | 22 ++ .../src/tree/xpath/XPathRuleAnywhereElement.cpp | 20 ++ .../src/tree/xpath/XPathRuleAnywhereElement.h | 27 ++ .../runtime/src/tree/xpath/XPathRuleElement.cpp | 30 ++ .../runtime/src/tree/xpath/XPathRuleElement.h | 26 ++ .../src/tree/xpath/XPathTokenAnywhereElement.cpp | 20 ++ .../src/tree/xpath/XPathTokenAnywhereElement.h | 25 ++ .../runtime/src/tree/xpath/XPathTokenElement.cpp | 33 ++ .../runtime/src/tree/xpath/XPathTokenElement.h | 26 ++ .../tree/xpath/XPathWildcardAnywhereElement.cpp | 23 ++ .../src/tree/xpath/XPathWildcardAnywhereElement.h | 23 ++ .../src/tree/xpath/XPathWildcardElement.cpp | 24 ++ .../runtime/src/tree/xpath/XPathWildcardElement.h | 23 ++ 60 files changed, 3615 insertions(+) create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/AbstractParseTreeVisitor.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNode.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNode.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNodeImpl.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNodeImpl.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/IterativeParseTreeWalker.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/IterativeParseTreeWalker.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTree.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTree.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeListener.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeListener.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeProperty.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeVisitor.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeVisitor.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeWalker.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeWalker.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNode.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNode.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNodeImpl.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNodeImpl.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/Trees.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/Trees.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/Chunk.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/Chunk.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreeMatch.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreeMatch.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePattern.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePattern.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePatternMatcher.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePatternMatcher.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/RuleTagToken.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/RuleTagToken.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TagChunk.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TagChunk.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TextChunk.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TextChunk.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TokenTagToken.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TokenTagToken.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPath.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPath.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathElement.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathElement.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.g4 create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.tokens create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexerErrorListener.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexerErrorListener.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleAnywhereElement.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleAnywhereElement.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleElement.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleElement.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenAnywhereElement.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenAnywhereElement.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenElement.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenElement.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardAnywhereElement.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardAnywhereElement.h create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardElement.cpp create mode 100644 antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardElement.h (limited to 'antlr4-cpp-runtime-4.9.2-source/runtime/src/tree') diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/AbstractParseTreeVisitor.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/AbstractParseTreeVisitor.h new file mode 100644 index 0000000..d21795b --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/AbstractParseTreeVisitor.h @@ -0,0 +1,128 @@ +/* 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 "tree/ParseTreeVisitor.h" + +namespace antlr4 { +namespace tree { + + class ANTLR4CPP_PUBLIC AbstractParseTreeVisitor : public ParseTreeVisitor { + public: + /// The default implementation calls on the + /// specified tree. + virtual antlrcpp::Any visit(ParseTree *tree) override { + return tree->accept(this); + } + + /** + *

The default implementation initializes the aggregate result to + * {@link #defaultResult defaultResult()}. Before visiting each child, it + * calls {@link #shouldVisitNextChild shouldVisitNextChild}; if the result + * is {@code false} no more children are visited and the current aggregate + * result is returned. After visiting a child, the aggregate result is + * updated by calling {@link #aggregateResult aggregateResult} with the + * previous aggregate result and the result of visiting the child.

+ * + *

The default implementation is not safe for use in visitors that modify + * the tree structure. Visitors that modify the tree should override this + * method to behave properly in respect to the specific algorithm in use.

+ */ + virtual antlrcpp::Any visitChildren(ParseTree *node) override { + antlrcpp::Any result = defaultResult(); + size_t n = node->children.size(); + for (size_t i = 0; i < n; i++) { + if (!shouldVisitNextChild(node, result)) { + break; + } + + antlrcpp::Any childResult = node->children[i]->accept(this); + result = aggregateResult(result, childResult); + } + + return result; + } + + /// The default implementation returns the result of + /// . + virtual antlrcpp::Any visitTerminal(TerminalNode * /*node*/) override { + return defaultResult(); + } + + /// The default implementation returns the result of + /// . + virtual antlrcpp::Any visitErrorNode(ErrorNode * /*node*/) override { + return defaultResult(); + } + + protected: + /// + /// Gets the default value returned by visitor methods. This value is + /// returned by the default implementations of + /// , . + /// The default implementation of + /// initializes its aggregate result to this value. + ///

+ /// The base implementation returns {@code null}. + ///

+ /// The default value returned by visitor methods. + virtual antlrcpp::Any defaultResult() { + return nullptr; // support isNotNull + } + + /// + /// Aggregates the results of visiting multiple children of a node. After + /// either all children are visited or returns + /// {@code false}, the aggregate value is returned as the result of + /// . + ///

+ /// The default implementation returns {@code nextResult}, meaning + /// will return the result of the last child visited + /// (or return the initial value if the node has no children). + ///

+ /// The previous aggregate value. In the default + /// implementation, the aggregate value is initialized to + /// , which is passed as the {@code aggregate} argument + /// to this method after the first child node is visited. + /// The result of the immediately preceeding call to visit + /// a child node. + /// + /// The updated aggregate result. + virtual antlrcpp::Any aggregateResult(antlrcpp::Any /*aggregate*/, const antlrcpp::Any &nextResult) { + return nextResult; + } + + /// + /// This method is called after visiting each child in + /// . This method is first called before the first + /// child is visited; at that point {@code currentResult} will be the initial + /// value (in the default implementation, the initial value is returned by a + /// call to . This method is not called after the last + /// child is visited. + ///

+ /// The default implementation always returns {@code true}, indicating that + /// {@code visitChildren} should only return after all children are visited. + /// One reason to override this method is to provide a "short circuit" + /// evaluation option for situations where the result of visiting a single + /// child has the potential to determine the result of the visit operation as + /// a whole. + ///

+ /// The whose children are currently being + /// visited. + /// The current aggregate result of the children visited + /// to the current point. + /// + /// {@code true} to continue visiting children. Otherwise return + /// {@code false} to stop visiting children and immediately return the + /// current aggregate result from . + virtual bool shouldVisitNextChild(ParseTree * /*node*/, const antlrcpp::Any &/*currentResult*/) { + return true; + } + + }; + +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNode.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNode.cpp new file mode 100644 index 0000000..ade2539 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNode.cpp @@ -0,0 +1,9 @@ +/* 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. + */ + +#include "tree/ErrorNode.h" + +antlr4::tree::ErrorNode::~ErrorNode() { +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNode.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNode.h new file mode 100644 index 0000000..619f44d --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNode.h @@ -0,0 +1,19 @@ +/* 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 "tree/TerminalNode.h" + +namespace antlr4 { +namespace tree { + + class ANTLR4CPP_PUBLIC ErrorNode : public virtual TerminalNode { + public: + ~ErrorNode() override; + }; + +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNodeImpl.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNodeImpl.cpp new file mode 100644 index 0000000..fde942d --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNodeImpl.cpp @@ -0,0 +1,23 @@ +/* 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. + */ + +#include "Exceptions.h" +#include "tree/ParseTreeVisitor.h" + +#include "tree/ErrorNodeImpl.h" + +using namespace antlr4; +using namespace antlr4::misc; +using namespace antlr4::tree; + +ErrorNodeImpl::ErrorNodeImpl(Token *token) : TerminalNodeImpl(token) { +} + +ErrorNodeImpl::~ErrorNodeImpl() { +} + +antlrcpp::Any ErrorNodeImpl::accept(ParseTreeVisitor *visitor) { + return visitor->visitErrorNode(this); +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNodeImpl.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNodeImpl.h new file mode 100644 index 0000000..b64b6f9 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ErrorNodeImpl.h @@ -0,0 +1,33 @@ +/* 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 "tree/ErrorNode.h" +#include "tree/TerminalNodeImpl.h" +#include "misc/Interval.h" + +#include "support/Any.h" + +namespace antlr4 { +namespace tree { + + /// + /// Represents a token that was consumed during resynchronization + /// rather than during a valid match operation. For example, + /// we will create this kind of a node during single token insertion + /// and deletion as well as during "consume until error recovery set" + /// upon no viable alternative exceptions. + /// + class ANTLR4CPP_PUBLIC ErrorNodeImpl : public virtual TerminalNodeImpl, public virtual ErrorNode { + public: + ErrorNodeImpl(Token *token); + ~ErrorNodeImpl() override; + + virtual antlrcpp::Any accept(ParseTreeVisitor *visitor) override; + }; + +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/IterativeParseTreeWalker.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/IterativeParseTreeWalker.cpp new file mode 100644 index 0000000..a4b3efd --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/IterativeParseTreeWalker.cpp @@ -0,0 +1,71 @@ +/* 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. + */ + +#include "support/CPPUtils.h" + +#include "tree/ParseTreeListener.h" +#include "tree/ParseTree.h" +#include "tree/ErrorNode.h" + +#include "IterativeParseTreeWalker.h" + +using namespace antlr4::tree; + +void IterativeParseTreeWalker::walk(ParseTreeListener *listener, ParseTree *t) const { + + std::vector nodeStack; + std::vector indexStack; + + ParseTree *currentNode = t; + size_t currentIndex = 0; + + while (currentNode != nullptr) { + // pre-order visit + if (antlrcpp::is(currentNode)) { + listener->visitErrorNode(dynamic_cast(currentNode)); + } else if (antlrcpp::is(currentNode)) { + listener->visitTerminal((TerminalNode *)currentNode); + } else { + enterRule(listener, currentNode); + } + + // Move down to first child, if it exists. + if (!currentNode->children.empty()) { + nodeStack.push_back(currentNode); + indexStack.push_back(currentIndex); + currentIndex = 0; + currentNode = currentNode->children[0]; + continue; + } + + // No child nodes, so walk tree. + do { + // post-order visit + if (!antlrcpp::is(currentNode)) { + exitRule(listener, currentNode); + } + + // No parent, so no siblings. + if (nodeStack.empty()) { + currentNode = nullptr; + currentIndex = 0; + break; + } + + // Move to next sibling if possible. + if (nodeStack.back()->children.size() > ++currentIndex) { + currentNode = nodeStack.back()->children[currentIndex]; + break; + } + + // No next sibling, so move up. + currentNode = nodeStack.back(); + nodeStack.pop_back(); + currentIndex = indexStack.back(); + indexStack.pop_back(); + + } while (currentNode != nullptr); + } +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/IterativeParseTreeWalker.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/IterativeParseTreeWalker.h new file mode 100644 index 0000000..8957d87 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/IterativeParseTreeWalker.h @@ -0,0 +1,53 @@ +/* + * [The "BSD license"] + * Copyright (c) 2012 Terence Parr + * Copyright (c) 2012 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. + */ + +#pragma once + +#include "antlr4-common.h" + +#include "tree/ParseTreeWalker.h" + +namespace antlr4 { +namespace tree { + + class ParseTreeListener; + + /** + * An iterative (read: non-recursive) pre-order and post-order tree walker that + * doesn't use the thread stack but heap-based stacks. Makes it possible to + * process deeply nested parse trees. + */ + class ANTLR4CPP_PUBLIC IterativeParseTreeWalker : public ParseTreeWalker { + public: + virtual void walk(ParseTreeListener *listener, ParseTree *t) const override; + }; + +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTree.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTree.cpp new file mode 100644 index 0000000..b975a40 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTree.cpp @@ -0,0 +1,15 @@ +/* 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. + */ + +#include "tree/ParseTree.h" + +using namespace antlr4::tree; + +ParseTree::ParseTree() : parent(nullptr) { +} + +bool ParseTree::operator == (const ParseTree &other) const { + return &other == this; +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTree.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTree.h new file mode 100644 index 0000000..3b91be8 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTree.h @@ -0,0 +1,102 @@ +/* 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 "support/Any.h" + +namespace antlr4 { +namespace tree { + + /// An interface to access the tree of objects created + /// during a parse that makes the data structure look like a simple parse tree. + /// This node represents both internal nodes, rule invocations, + /// and leaf nodes, token matches. + /// + /// The payload is either a or a object. + // ml: This class unites 4 Java classes: RuleNode, ParseTree, SyntaxTree and Tree. + class ANTLR4CPP_PUBLIC ParseTree { + public: + ParseTree(); + ParseTree(ParseTree const&) = delete; + virtual ~ParseTree() {} + + ParseTree& operator=(ParseTree const&) = delete; + + /// The parent of this node. If the return value is null, then this + /// node is the root of the tree. + ParseTree *parent; + + /// If we are debugging or building a parse tree for a visitor, + /// we need to track all of the tokens and rule invocations associated + /// with this rule's context. This is empty for parsing w/o tree constr. + /// operation because we don't the need to track the details about + /// how we parse this rule. + // ml: memory is not managed here, but by the owning class. This is just for the structure. + std::vector children; + + /// Print out a whole tree, not just a node, in LISP format + /// {@code (root child1 .. childN)}. Print just a node if this is a leaf. + virtual std::string toStringTree(bool pretty = false) = 0; + virtual std::string toString() = 0; + + /// Specialize toStringTree so that it can print out more information + /// based upon the parser. + virtual std::string toStringTree(Parser *parser, bool pretty = false) = 0; + + virtual bool operator == (const ParseTree &other) const; + + /// The needs a double dispatch method. + // ml: This has been changed to use Any instead of a template parameter, to avoid the need of a virtual template function. + virtual antlrcpp::Any accept(ParseTreeVisitor *visitor) = 0; + + /// Return the combined text of all leaf nodes. Does not get any + /// off-channel tokens (if any) so won't return whitespace and + /// comments if they are sent to parser on hidden channel. + virtual std::string getText() = 0; + + /** + * Return an {@link Interval} indicating the index in the + * {@link TokenStream} of the first and last token associated with this + * subtree. If this node is a leaf, then the interval represents a single + * token and has interval i..i for token index i. + * + *

An interval of i..i-1 indicates an empty interval at position + * i in the input stream, where 0 <= i <= the size of the input + * token stream. Currently, the code base can only have i=0..n-1 but + * in concept one could have an empty interval after EOF.

+ * + *

If source interval is unknown, this returns {@link Interval#INVALID}.

+ * + *

As a weird special case, the source interval for rules matched after + * EOF is unspecified.

+ */ + virtual misc::Interval getSourceInterval() = 0; + }; + + // A class to help managing ParseTree instances without the need of a shared_ptr. + class ANTLR4CPP_PUBLIC ParseTreeTracker { + public: + template + T* createInstance(Args&& ... args) { + static_assert(std::is_base_of::value, "Argument must be a parse tree type"); + T* result = new T(args...); + _allocated.push_back(result); + return result; + } + + void reset() { + for (auto * entry : _allocated) + delete entry; + _allocated.clear(); + } + + private: + std::vector _allocated; + }; + + +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeListener.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeListener.cpp new file mode 100644 index 0000000..ce12297 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeListener.cpp @@ -0,0 +1,9 @@ +/* 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. + */ + +#include "ParseTreeListener.h" + +antlr4::tree::ParseTreeListener::~ParseTreeListener() { +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeListener.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeListener.h new file mode 100644 index 0000000..6a7f96a --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeListener.h @@ -0,0 +1,39 @@ +/* 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 { + + /** This interface describes the minimal core of methods triggered + * by {@link ParseTreeWalker}. E.g., + * + * ParseTreeWalker walker = new ParseTreeWalker(); + * walker.walk(myParseTreeListener, myParseTree); <-- triggers events in your listener + * + * If you want to trigger events in multiple listeners during a single + * tree walk, you can use the ParseTreeDispatcher object available at + * + * https://github.com/antlr/antlr4/issues/841 + */ + class ANTLR4CPP_PUBLIC ParseTreeListener { + public: + virtual ~ParseTreeListener(); + + virtual void visitTerminal(TerminalNode *node) = 0; + virtual void visitErrorNode(ErrorNode *node) = 0; + virtual void enterEveryRule(ParserRuleContext *ctx) = 0; + virtual void exitEveryRule(ParserRuleContext *ctx) = 0; + + bool operator == (const ParseTreeListener &other) { + return this == &other; + } + }; + +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeProperty.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeProperty.h new file mode 100644 index 0000000..8669a10 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeProperty.h @@ -0,0 +1,50 @@ +/* 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 { + + /// + /// Associate a property with a parse tree node. Useful with parse tree listeners + /// that need to associate values with particular tree nodes, kind of like + /// specifying a return value for the listener event method that visited a + /// particular node. Example: + /// + ///
+  /// ParseTreeProperty<Integer> values = new ParseTreeProperty<Integer>();
+  /// values.put(tree, 36);
+  /// int x = values.get(tree);
+  /// values.removeFrom(tree);
+  /// 
+ /// + /// You would make one decl (values here) in the listener and use lots of times + /// in your event methods. + ///
+ template + class ANTLR4CPP_PUBLIC ParseTreeProperty { + public: + virtual ~ParseTreeProperty() {} + virtual V get(ParseTree *node) { + return _annotations[node]; + } + virtual void put(ParseTree *node, V value) { + _annotations[node] = value; + } + virtual V removeFrom(ParseTree *node) { + auto value = _annotations[node]; + _annotations.erase(node); + return value; + } + + protected: + std::map _annotations; + }; + +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeVisitor.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeVisitor.cpp new file mode 100644 index 0000000..a329919 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeVisitor.cpp @@ -0,0 +1,9 @@ +/* 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. + */ + +#include "ParseTreeVisitor.h" + +antlr4::tree::ParseTreeVisitor::~ParseTreeVisitor() { +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeVisitor.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeVisitor.h new file mode 100644 index 0000000..5a08599 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeVisitor.h @@ -0,0 +1,57 @@ +/* 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 "support/Any.h" + +namespace antlr4 { +namespace tree { + + /// + /// This interface defines the basic notion of a parse tree visitor. Generated + /// visitors implement this interface and the {@code XVisitor} interface for + /// grammar {@code X}. + /// + /// @param The return type of the visit operation. Use for + /// operations with no return type. + // ml: no template parameter here, to avoid the need for virtual template functions. Instead we have our Any class. + class ANTLR4CPP_PUBLIC ParseTreeVisitor { + public: + virtual ~ParseTreeVisitor(); + + /// + /// Visit a parse tree, and return a user-defined result of the operation. + /// + /// The to visit. + /// The result of visiting the parse tree. + virtual antlrcpp::Any visit(ParseTree *tree) = 0; + + /// + /// Visit the children of a node, and return a user-defined result of the + /// operation. + /// + /// The whose children should be visited. + /// The result of visiting the children of the node. + virtual antlrcpp::Any visitChildren(ParseTree *node) = 0; + + /// + /// Visit a terminal node, and return a user-defined result of the operation. + /// + /// The to visit. + /// The result of visiting the node. + virtual antlrcpp::Any visitTerminal(TerminalNode *node) = 0; + + /// + /// Visit an error node, and return a user-defined result of the operation. + /// + /// The to visit. + /// The result of visiting the node. + virtual antlrcpp::Any visitErrorNode(ErrorNode *node) = 0; + + }; + +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeWalker.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeWalker.cpp new file mode 100644 index 0000000..998c9ed --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeWalker.cpp @@ -0,0 +1,49 @@ +/* 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. + */ + +#include "tree/ErrorNode.h" +#include "ParserRuleContext.h" +#include "tree/ParseTreeListener.h" +#include "support/CPPUtils.h" + +#include "tree/IterativeParseTreeWalker.h" +#include "tree/ParseTreeWalker.h" + +using namespace antlr4::tree; +using namespace antlrcpp; + +static IterativeParseTreeWalker defaultWalker; +ParseTreeWalker &ParseTreeWalker::DEFAULT = defaultWalker; + +ParseTreeWalker::~ParseTreeWalker() { +} + +void ParseTreeWalker::walk(ParseTreeListener *listener, ParseTree *t) const { + if (is(t)) { + listener->visitErrorNode(dynamic_cast(t)); + return; + } else if (is(t)) { + listener->visitTerminal(dynamic_cast(t)); + return; + } + + enterRule(listener, t); + for (auto &child : t->children) { + walk(listener, child); + } + exitRule(listener, t); +} + +void ParseTreeWalker::enterRule(ParseTreeListener *listener, ParseTree *r) const { + ParserRuleContext *ctx = dynamic_cast(r); + listener->enterEveryRule(ctx); + ctx->enterRule(listener); +} + +void ParseTreeWalker::exitRule(ParseTreeListener *listener, ParseTree *r) const { + ParserRuleContext *ctx = dynamic_cast(r); + ctx->exitRule(listener); + listener->exitEveryRule(ctx); +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeWalker.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeWalker.h new file mode 100644 index 0000000..166ad80 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/ParseTreeWalker.h @@ -0,0 +1,55 @@ +/* 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 { + + class ANTLR4CPP_PUBLIC ParseTreeWalker { + public: + static ParseTreeWalker &DEFAULT; + + virtual ~ParseTreeWalker(); + + /** + * + * Performs a walk on the given parse tree starting at the root and going down recursively + * with depth-first search. On each node, is called before + * recursively walking down into child nodes, then + * is called after the recursive call to wind up. + * + * The listener used by the walker to process grammar rules + * The parse tree to be walked on + */ + virtual void walk(ParseTreeListener *listener, ParseTree *t) const; + + protected: + + /** + * + * Enters a grammar rule by first triggering the generic event + * then by triggering the event specific to the given parse tree node + * + * The listener responding to the trigger events + * The grammar rule containing the rule context + */ + virtual void enterRule(ParseTreeListener *listener, ParseTree *r) const; + + /** + * + * Exits a grammar rule by first triggering the event specific to the given parse tree node + * then by triggering the generic event + * + * The listener responding to the trigger events + * The grammar rule containing the rule context + */ + virtual void exitRule(ParseTreeListener *listener, ParseTree *r) const; + }; + +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNode.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNode.cpp new file mode 100644 index 0000000..d630469 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNode.cpp @@ -0,0 +1,9 @@ +/* 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. + */ + +#include "tree/TerminalNode.h" + +antlr4::tree::TerminalNode::~TerminalNode() { +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNode.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNode.h new file mode 100644 index 0000000..7108f70 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNode.h @@ -0,0 +1,32 @@ +/* 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 "tree/ParseTree.h" + +namespace antlr4 { +namespace tree { + + class ANTLR4CPP_PUBLIC TerminalNode : public ParseTree { + public: + ~TerminalNode() override; + + virtual Token* getSymbol() = 0; + + /** Set the parent for this leaf node. + * + * Technically, this is not backward compatible as it changes + * the interface but no one was able to create custom + * TerminalNodes anyway so I'm adding as it improves internal + * code quality. + * + * @since 4.7 + */ + virtual void setParent(RuleContext *parent) = 0; + }; + +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNodeImpl.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNodeImpl.cpp new file mode 100644 index 0000000..7ab121b --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNodeImpl.cpp @@ -0,0 +1,57 @@ +/* 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. + */ + +#include "misc/Interval.h" +#include "Token.h" +#include "RuleContext.h" +#include "tree/ParseTreeVisitor.h" + +#include "tree/TerminalNodeImpl.h" + +using namespace antlr4; +using namespace antlr4::tree; + +TerminalNodeImpl::TerminalNodeImpl(Token *symbol_) : symbol(symbol_) { +} + +Token* TerminalNodeImpl::getSymbol() { + return symbol; +} + +void TerminalNodeImpl::setParent(RuleContext *parent_) { + this->parent = parent_; +} + +misc::Interval TerminalNodeImpl::getSourceInterval() { + if (symbol == nullptr) { + return misc::Interval::INVALID; + } + + size_t tokenIndex = symbol->getTokenIndex(); + return misc::Interval(tokenIndex, tokenIndex); +} + +antlrcpp::Any TerminalNodeImpl::accept(ParseTreeVisitor *visitor) { + return visitor->visitTerminal(this); +} + +std::string TerminalNodeImpl::getText() { + return symbol->getText(); +} + +std::string TerminalNodeImpl::toStringTree(Parser * /*parser*/, bool /*pretty*/) { + return toString(); +} + +std::string TerminalNodeImpl::toString() { + if (symbol->getType() == Token::EOF) { + return ""; + } + return symbol->getText(); +} + +std::string TerminalNodeImpl::toStringTree(bool /*pretty*/) { + return toString(); +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNodeImpl.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNodeImpl.h new file mode 100644 index 0000000..6f65d82 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/TerminalNodeImpl.h @@ -0,0 +1,33 @@ +/* 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 "tree/TerminalNode.h" + +namespace antlr4 { +namespace tree { + + class ANTLR4CPP_PUBLIC TerminalNodeImpl : public virtual TerminalNode { + public: + Token *symbol; + + TerminalNodeImpl(Token *symbol); + + virtual Token* getSymbol() override; + virtual void setParent(RuleContext *parent) override; + virtual misc::Interval getSourceInterval() override; + + virtual antlrcpp::Any accept(ParseTreeVisitor *visitor) override; + + virtual std::string getText() override; + virtual std::string toStringTree(Parser *parser, bool pretty = false) override; + virtual std::string toString() override; + virtual std::string toStringTree(bool pretty = false) override; + + }; + +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/Trees.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/Trees.cpp new file mode 100644 index 0000000..34cfb74 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/Trees.cpp @@ -0,0 +1,241 @@ +/* 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. + */ + +#include "tree/ErrorNode.h" +#include "Parser.h" +#include "ParserRuleContext.h" +#include "support/CPPUtils.h" +#include "tree/TerminalNodeImpl.h" +#include "atn/ATN.h" +#include "misc/Interval.h" +#include "Token.h" +#include "CommonToken.h" +#include "misc/Predicate.h" + +#include "tree/Trees.h" + +using namespace antlr4; +using namespace antlr4::misc; +using namespace antlr4::tree; + +using namespace antlrcpp; + +Trees::Trees() { +} + +std::string Trees::toStringTree(ParseTree *t, bool pretty) { + return toStringTree(t, nullptr, pretty); +} + +std::string Trees::toStringTree(ParseTree *t, Parser *recog, bool pretty) { + if (recog == nullptr) + return toStringTree(t, std::vector(), pretty); + return toStringTree(t, recog->getRuleNames(), pretty); +} + +std::string Trees::toStringTree(ParseTree *t, const std::vector &ruleNames, bool pretty) { + std::string temp = antlrcpp::escapeWhitespace(Trees::getNodeText(t, ruleNames), false); + if (t->children.empty()) { + return temp; + } + + std::stringstream ss; + ss << "(" << temp << ' '; + + // Implement the recursive walk as iteration to avoid trouble with deep nesting. + std::stack stack; + size_t childIndex = 0; + ParseTree *run = t; + size_t indentationLevel = 1; + while (childIndex < run->children.size()) { + if (childIndex > 0) { + ss << ' '; + } + ParseTree *child = run->children[childIndex]; + temp = antlrcpp::escapeWhitespace(Trees::getNodeText(child, ruleNames), false); + if (!child->children.empty()) { + // Go deeper one level. + stack.push(childIndex); + run = child; + childIndex = 0; + if (pretty) { + ++indentationLevel; + ss << std::endl; + for (size_t i = 0; i < indentationLevel; ++i) { + ss << " "; + } + } + ss << "(" << temp << " "; + } else { + ss << temp; + while (++childIndex == run->children.size()) { + if (stack.size() > 0) { + // Reached the end of the current level. See if we can step up from here. + childIndex = stack.top(); + stack.pop(); + run = run->parent; + if (pretty) { + --indentationLevel; + } + ss << ")"; + } else { + break; + } + } + } + } + + ss << ")"; + return ss.str(); +} + +std::string Trees::getNodeText(ParseTree *t, Parser *recog) { + return getNodeText(t, recog->getRuleNames()); +} + +std::string Trees::getNodeText(ParseTree *t, const std::vector &ruleNames) { + if (ruleNames.size() > 0) { + if (is(t)) { + size_t ruleIndex = dynamic_cast(t)->getRuleIndex(); + std::string ruleName = ruleNames[ruleIndex]; + size_t altNumber = dynamic_cast(t)->getAltNumber(); + if (altNumber != atn::ATN::INVALID_ALT_NUMBER) { + return ruleName + ":" + std::to_string(altNumber); + } + return ruleName; + } else if (is(t)) { + return t->toString(); + } else if (is(t)) { + Token *symbol = dynamic_cast(t)->getSymbol(); + if (symbol != nullptr) { + std::string s = symbol->getText(); + return s; + } + } + } + // no recog for rule names + if (is(t)) { + return dynamic_cast(t)->getText(); + } + + if (is(t)) { + return dynamic_cast(t)->getSymbol()->getText(); + } + + return ""; +} + +std::vector Trees::getAncestors(ParseTree *t) { + std::vector ancestors; + ParseTree *parent = t->parent; + while (parent != nullptr) { + ancestors.insert(ancestors.begin(), parent); // insert at start + parent = parent->parent; + } + return ancestors; +} + +template +static void _findAllNodes(ParseTree *t, size_t index, bool findTokens, std::vector &nodes) { + // check this node (the root) first + if (findTokens && is(t)) { + TerminalNode *tnode = dynamic_cast(t); + if (tnode->getSymbol()->getType() == index) { + nodes.push_back(t); + } + } else if (!findTokens && is(t)) { + ParserRuleContext *ctx = dynamic_cast(t); + if (ctx->getRuleIndex() == index) { + nodes.push_back(t); + } + } + // check children + for (size_t i = 0; i < t->children.size(); i++) { + _findAllNodes(t->children[i], index, findTokens, nodes); + } +} + +bool Trees::isAncestorOf(ParseTree *t, ParseTree *u) { + if (t == nullptr || u == nullptr || t->parent == nullptr) { + return false; + } + + ParseTree *p = u->parent; + while (p != nullptr) { + if (t == p) { + return true; + } + p = p->parent; + } + return false; +} + +std::vector Trees::findAllTokenNodes(ParseTree *t, size_t ttype) { + return findAllNodes(t, ttype, true); +} + +std::vector Trees::findAllRuleNodes(ParseTree *t, size_t ruleIndex) { + return findAllNodes(t, ruleIndex, false); +} + +std::vector Trees::findAllNodes(ParseTree *t, size_t index, bool findTokens) { + std::vector nodes; + _findAllNodes(t, index, findTokens, nodes); + return nodes; +} + +std::vector Trees::getDescendants(ParseTree *t) { + std::vector nodes; + nodes.push_back(t); + std::size_t n = t->children.size(); + for (size_t i = 0 ; i < n ; i++) { + auto descentants = getDescendants(t->children[i]); + for (auto *entry: descentants) { + nodes.push_back(entry); + } + } + return nodes; +} + +std::vector Trees::descendants(ParseTree *t) { + return getDescendants(t); +} + +ParserRuleContext* Trees::getRootOfSubtreeEnclosingRegion(ParseTree *t, size_t startTokenIndex, size_t stopTokenIndex) { + size_t n = t->children.size(); + for (size_t i = 0; i < n; i++) { + ParserRuleContext *r = getRootOfSubtreeEnclosingRegion(t->children[i], startTokenIndex, stopTokenIndex); + if (r != nullptr) { + return r; + } + } + + if (is(t)) { + ParserRuleContext *r = dynamic_cast(t); + if (startTokenIndex >= r->getStart()->getTokenIndex() && // is range fully contained in t? + (r->getStop() == nullptr || stopTokenIndex <= r->getStop()->getTokenIndex())) { + // note: r.getStop()==null likely implies that we bailed out of parser and there's nothing to the right + return r; + } + } + return nullptr; +} + +ParseTree * Trees::findNodeSuchThat(ParseTree *t, Ref const& pred) { + if (pred->test(t)) { + return t; + } + + size_t n = t->children.size(); + for (size_t i = 0 ; i < n ; ++i) { + ParseTree *u = findNodeSuchThat(t->children[i], pred); + if (u != nullptr) { + return u; + } + } + + return nullptr; +} + diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/Trees.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/Trees.h new file mode 100644 index 0000000..d9d0462 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/Trees.h @@ -0,0 +1,78 @@ +/* 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 "tree/TerminalNode.h" +#include "ParserRuleContext.h" +#include "Recognizer.h" + +namespace antlr4 { +namespace tree { + + /// A set of utility routines useful for all kinds of ANTLR trees. + class ANTLR4CPP_PUBLIC Trees { + public: + /// Print out a whole tree in LISP form. getNodeText is used on the + /// node payloads to get the text for the nodes. Detect + /// parse trees and extract data appropriately. + static std::string toStringTree(ParseTree *t, bool pretty = false); + + /// Print out a whole tree in LISP form. getNodeText is used on the + /// node payloads to get the text for the nodes. Detect + /// parse trees and extract data appropriately. + static std::string toStringTree(ParseTree *t, Parser *recog, bool pretty = false); + + /// Print out a whole tree in LISP form. getNodeText is used on the + /// node payloads to get the text for the nodes. Detect + /// parse trees and extract data appropriately. + static std::string toStringTree(ParseTree *t, const std::vector &ruleNames, bool pretty = false); + static std::string getNodeText(ParseTree *t, Parser *recog); + static std::string getNodeText(ParseTree *t, const std::vector &ruleNames); + + /// Return a list of all ancestors of this node. The first node of + /// list is the root and the last is the parent of this node. + static std::vector getAncestors(ParseTree *t); + + /** Return true if t is u's parent or a node on path to root from u. + * Use == not equals(). + * + * @since 4.5.1 + */ + static bool isAncestorOf(ParseTree *t, ParseTree *u); + static std::vector findAllTokenNodes(ParseTree *t, size_t ttype); + static std::vector findAllRuleNodes(ParseTree *t, size_t ruleIndex); + static std::vector findAllNodes(ParseTree *t, size_t index, bool findTokens); + + /** Get all descendents; includes t itself. + * + * @since 4.5.1 + */ + static std::vector getDescendants(ParseTree *t); + + /** @deprecated */ + static std::vector descendants(ParseTree *t); + + /** Find smallest subtree of t enclosing range startTokenIndex..stopTokenIndex + * inclusively using postorder traversal. Recursive depth-first-search. + * + * @since 4.5.1 + */ + static ParserRuleContext* getRootOfSubtreeEnclosingRegion(ParseTree *t, + size_t startTokenIndex, // inclusive + size_t stopTokenIndex); // inclusive + + /** Return first node satisfying the pred + * + * @since 4.5.1 + */ + static ParseTree* findNodeSuchThat(ParseTree *t, Ref const& pred); + + private: + Trees(); + }; + +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/Chunk.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/Chunk.cpp new file mode 100644 index 0000000..5320f91 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/Chunk.cpp @@ -0,0 +1,9 @@ +/* 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. + */ + +#include "tree/pattern/Chunk.h" + +antlr4::tree::pattern::Chunk::~Chunk() { +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/Chunk.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/Chunk.h new file mode 100644 index 0000000..42e7838 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/Chunk.h @@ -0,0 +1,44 @@ +/* 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 { + + /// + /// A chunk is either a token tag, a rule tag, or a span of literal text within a + /// tree pattern. + ///

+ /// The method returns a list of + /// chunks in preparation for creating a token stream by + /// . From there, we get a parse + /// tree from with . These + /// chunks are converted to , , or the + /// regular tokens of the text surrounding the tags. + ///

+ class ANTLR4CPP_PUBLIC Chunk { + public: + Chunk() = default; + Chunk(Chunk const&) = default; + virtual ~Chunk(); + + Chunk& operator=(Chunk const&) = default; + + /// This method returns a text representation of the tag chunk. Labeled tags + /// are returned in the form {@code label:tag}, and unlabeled tags are + /// returned as just the tag name. + virtual std::string toString() { + std::string str; + return str; + } + }; + +} // namespace pattern +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreeMatch.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreeMatch.cpp new file mode 100644 index 0000000..ce34b3f --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreeMatch.cpp @@ -0,0 +1,69 @@ +/* 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. + */ + +#include "Exceptions.h" + +#include "tree/pattern/ParseTreeMatch.h" + +using namespace antlr4::tree; +using namespace antlr4::tree::pattern; + +ParseTreeMatch::ParseTreeMatch(ParseTree *tree, const ParseTreePattern &pattern, + const std::map> &labels, + ParseTree *mismatchedNode) + : _tree(tree), _pattern(pattern), _labels(labels), _mismatchedNode(mismatchedNode) { + if (tree == nullptr) { + throw IllegalArgumentException("tree cannot be nul"); + } +} + +ParseTreeMatch::~ParseTreeMatch() { +} + +ParseTree* ParseTreeMatch::get(const std::string &label) { + auto iterator = _labels.find(label); + if (iterator == _labels.end() || iterator->second.empty()) { + return nullptr; + } + + return iterator->second.back(); // return last if multiple +} + +std::vector ParseTreeMatch::getAll(const std::string &label) { + auto iterator = _labels.find(label); + if (iterator == _labels.end()) { + return {}; + } + + return iterator->second; +} + +std::map>& ParseTreeMatch::getLabels() { + return _labels; +} + +ParseTree *ParseTreeMatch::getMismatchedNode() { + return _mismatchedNode; +} + +bool ParseTreeMatch::succeeded() { + return _mismatchedNode == nullptr; +} + +const ParseTreePattern& ParseTreeMatch::getPattern() { + return _pattern; +} + +ParseTree * ParseTreeMatch::getTree() { + return _tree; +} + +std::string ParseTreeMatch::toString() { + if (succeeded()) { + return "Match succeeded; found " + std::to_string(_labels.size()) + " labels"; + } else { + return "Match failed; found " + std::to_string(_labels.size()) + " labels"; + } +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreeMatch.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreeMatch.h new file mode 100644 index 0000000..eefde46 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreeMatch.h @@ -0,0 +1,132 @@ +/* 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 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePattern.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePattern.cpp new file mode 100644 index 0000000..50f44c8 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePattern.cpp @@ -0,0 +1,64 @@ +/* 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. + */ + +#include "tree/ParseTree.h" +#include "tree/pattern/ParseTreePatternMatcher.h" +#include "tree/pattern/ParseTreeMatch.h" + +#include "tree/xpath/XPath.h" +#include "tree/xpath/XPathElement.h" + +#include "tree/pattern/ParseTreePattern.h" + +using namespace antlr4::tree; +using namespace antlr4::tree::pattern; + +using namespace antlrcpp; + +ParseTreePattern::ParseTreePattern(ParseTreePatternMatcher *matcher, const std::string &pattern, int patternRuleIndex_, + ParseTree *patternTree) + : patternRuleIndex(patternRuleIndex_), _pattern(pattern), _patternTree(patternTree), _matcher(matcher) { +} + +ParseTreePattern::~ParseTreePattern() { +} + +ParseTreeMatch ParseTreePattern::match(ParseTree *tree) { + return _matcher->match(tree, *this); +} + +bool ParseTreePattern::matches(ParseTree *tree) { + return _matcher->match(tree, *this).succeeded(); +} + +std::vector ParseTreePattern::findAll(ParseTree *tree, const std::string &xpath) { + xpath::XPath finder(_matcher->getParser(), xpath); + std::vector subtrees = finder.evaluate(tree); + std::vector matches; + for (auto *t : subtrees) { + ParseTreeMatch aMatch = match(t); + if (aMatch.succeeded()) { + matches.push_back(aMatch); + } + } + return matches; +} + + +ParseTreePatternMatcher *ParseTreePattern::getMatcher() const { + return _matcher; +} + +std::string ParseTreePattern::getPattern() const { + return _pattern; +} + +int ParseTreePattern::getPatternRuleIndex() const { + return patternRuleIndex; +} + +ParseTree* ParseTreePattern::getPatternTree() const { + return _patternTree; +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePattern.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePattern.h new file mode 100644 index 0000000..d5b86ff --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePattern.h @@ -0,0 +1,105 @@ +/* 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 { + + /// + /// A pattern like {@code = ;} converted to a by + /// . + /// + class ANTLR4CPP_PUBLIC ParseTreePattern { + public: + /// + /// Construct a new instance of the class. + /// + /// The which created this + /// tree pattern. + /// The tree pattern in concrete syntax form. + /// The parser rule which serves as the root of the + /// tree pattern. + /// The tree pattern in form. + ParseTreePattern(ParseTreePatternMatcher *matcher, const std::string &pattern, int patternRuleIndex, + ParseTree *patternTree); + ParseTreePattern(ParseTreePattern const&) = default; + virtual ~ParseTreePattern(); + + /// + /// Match a specific parse tree against this tree pattern. + /// + /// The parse tree to match against this tree pattern. + /// A object describing the result of the + /// match operation. The method can be + /// used to determine whether or not the match was successful. + virtual ParseTreeMatch match(ParseTree *tree); + + /// + /// Determine whether or not a parse tree matches this tree pattern. + /// + /// The parse tree to match against this tree pattern. + /// {@code true} if {@code tree} is a match for the current tree + /// pattern; otherwise, {@code false}. + virtual bool matches(ParseTree *tree); + + /// Find all nodes using XPath and then try to match those subtrees against + /// this tree pattern. + /// @param tree The ParseTree to match against this pattern. + /// @param xpath An expression matching the nodes + /// + /// @returns A collection of ParseTreeMatch objects describing the + /// successful matches. Unsuccessful matches are omitted from the result, + /// regardless of the reason for the failure. + virtual std::vector findAll(ParseTree *tree, const std::string &xpath); + + /// + /// Get the which created this tree pattern. + /// + /// The which created this tree + /// pattern. + virtual ParseTreePatternMatcher *getMatcher() const; + + /// + /// Get the tree pattern in concrete syntax form. + /// + /// The tree pattern in concrete syntax form. + virtual std::string getPattern() const; + + /// + /// Get the parser rule which serves as the outermost rule for the tree + /// pattern. + /// + /// The parser rule which serves as the outermost rule for the tree + /// pattern. + virtual int getPatternRuleIndex() const; + + /// + /// Get the tree pattern as a . The rule and token tags from + /// the pattern are present in the parse tree as terminal nodes with a symbol + /// of type or . + /// + /// The tree pattern as a . + virtual ParseTree* getPatternTree() const; + + private: + const int patternRuleIndex; + + /// This is the backing field for . + const std::string _pattern; + + /// This is the backing field for . + ParseTree *_patternTree; + + /// This is the backing field for . + ParseTreePatternMatcher *const _matcher; + }; + +} // namespace pattern +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePatternMatcher.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePatternMatcher.cpp new file mode 100644 index 0000000..2e58a96 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePatternMatcher.cpp @@ -0,0 +1,371 @@ +/* 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. + */ + +#include "tree/pattern/ParseTreePattern.h" +#include "tree/pattern/ParseTreeMatch.h" +#include "tree/TerminalNode.h" +#include "CommonTokenStream.h" +#include "ParserInterpreter.h" +#include "tree/pattern/TokenTagToken.h" +#include "ParserRuleContext.h" +#include "tree/pattern/RuleTagToken.h" +#include "tree/pattern/TagChunk.h" +#include "atn/ATN.h" +#include "Lexer.h" +#include "BailErrorStrategy.h" + +#include "ListTokenSource.h" +#include "tree/pattern/TextChunk.h" +#include "ANTLRInputStream.h" +#include "support/Arrays.h" +#include "Exceptions.h" +#include "support/StringUtils.h" +#include "support/CPPUtils.h" + +#include "tree/pattern/ParseTreePatternMatcher.h" + +using namespace antlr4; +using namespace antlr4::tree; +using namespace antlr4::tree::pattern; +using namespace antlrcpp; + +ParseTreePatternMatcher::CannotInvokeStartRule::CannotInvokeStartRule(const RuntimeException &e) : RuntimeException(e.what()) { +} + +ParseTreePatternMatcher::CannotInvokeStartRule::~CannotInvokeStartRule() { +} + +ParseTreePatternMatcher::StartRuleDoesNotConsumeFullPattern::~StartRuleDoesNotConsumeFullPattern() { +} + +ParseTreePatternMatcher::ParseTreePatternMatcher(Lexer *lexer, Parser *parser) : _lexer(lexer), _parser(parser) { + InitializeInstanceFields(); +} + +ParseTreePatternMatcher::~ParseTreePatternMatcher() { +} + +void ParseTreePatternMatcher::setDelimiters(const std::string &start, const std::string &stop, const std::string &escapeLeft) { + if (start.empty()) { + throw IllegalArgumentException("start cannot be null or empty"); + } + + if (stop.empty()) { + throw IllegalArgumentException("stop cannot be null or empty"); + } + + _start = start; + _stop = stop; + _escape = escapeLeft; +} + +bool ParseTreePatternMatcher::matches(ParseTree *tree, const std::string &pattern, int patternRuleIndex) { + ParseTreePattern p = compile(pattern, patternRuleIndex); + return matches(tree, p); +} + +bool ParseTreePatternMatcher::matches(ParseTree *tree, const ParseTreePattern &pattern) { + std::map> labels; + ParseTree *mismatchedNode = matchImpl(tree, pattern.getPatternTree(), labels); + return mismatchedNode == nullptr; +} + +ParseTreeMatch ParseTreePatternMatcher::match(ParseTree *tree, const std::string &pattern, int patternRuleIndex) { + ParseTreePattern p = compile(pattern, patternRuleIndex); + return match(tree, p); +} + +ParseTreeMatch ParseTreePatternMatcher::match(ParseTree *tree, const ParseTreePattern &pattern) { + std::map> labels; + tree::ParseTree *mismatchedNode = matchImpl(tree, pattern.getPatternTree(), labels); + return ParseTreeMatch(tree, pattern, labels, mismatchedNode); +} + +ParseTreePattern ParseTreePatternMatcher::compile(const std::string &pattern, int patternRuleIndex) { + ListTokenSource tokenSrc(tokenize(pattern)); + CommonTokenStream tokens(&tokenSrc); + + ParserInterpreter parserInterp(_parser->getGrammarFileName(), _parser->getVocabulary(), + _parser->getRuleNames(), _parser->getATNWithBypassAlts(), &tokens); + + ParserRuleContext *tree = nullptr; + try { + parserInterp.setErrorHandler(std::make_shared()); + tree = parserInterp.parse(patternRuleIndex); + } catch (ParseCancellationException &e) { +#if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 190023026 + // rethrow_if_nested is not available before VS 2015. + throw e; +#else + std::rethrow_if_nested(e); // Unwrap the nested exception. +#endif + } catch (RecognitionException &re) { + throw re; +#if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 190023026 + } catch (std::exception &e) { + // throw_with_nested is not available before VS 2015. + throw e; +#else + } catch (std::exception & /*e*/) { + std::throw_with_nested((const char*)"Cannot invoke start rule"); // Wrap any other exception. We should however probably use one of the ANTLR exceptions here. +#endif + } + + // Make sure tree pattern compilation checks for a complete parse + if (tokens.LA(1) != Token::EOF) { + throw StartRuleDoesNotConsumeFullPattern(); + } + + return ParseTreePattern(this, pattern, patternRuleIndex, tree); +} + +Lexer* ParseTreePatternMatcher::getLexer() { + return _lexer; +} + +Parser* ParseTreePatternMatcher::getParser() { + return _parser; +} + +ParseTree* ParseTreePatternMatcher::matchImpl(ParseTree *tree, ParseTree *patternTree, + std::map> &labels) { + if (tree == nullptr) { + throw IllegalArgumentException("tree cannot be nul"); + } + + if (patternTree == nullptr) { + throw IllegalArgumentException("patternTree cannot be nul"); + } + + // x and , x and y, or x and x; or could be mismatched types + if (is(tree) && is(patternTree)) { + TerminalNode *t1 = dynamic_cast(tree); + TerminalNode *t2 = dynamic_cast(patternTree); + + ParseTree *mismatchedNode = nullptr; + // both are tokens and they have same type + if (t1->getSymbol()->getType() == t2->getSymbol()->getType()) { + if (is(t2->getSymbol())) { // x and + TokenTagToken *tokenTagToken = dynamic_cast(t2->getSymbol()); + + // track label->list-of-nodes for both token name and label (if any) + labels[tokenTagToken->getTokenName()].push_back(tree); + if (tokenTagToken->getLabel() != "") { + labels[tokenTagToken->getLabel()].push_back(tree); + } + } else if (t1->getText() == t2->getText()) { + // x and x + } else { + // x and y + if (mismatchedNode == nullptr) { + mismatchedNode = t1; + } + } + } else { + if (mismatchedNode == nullptr) { + mismatchedNode = t1; + } + } + + return mismatchedNode; + } + + if (is(tree) && is(patternTree)) { + ParserRuleContext *r1 = dynamic_cast(tree); + ParserRuleContext *r2 = dynamic_cast(patternTree); + ParseTree *mismatchedNode = nullptr; + + // (expr ...) and + RuleTagToken *ruleTagToken = getRuleTagToken(r2); + if (ruleTagToken != nullptr) { + //ParseTreeMatch *m = nullptr; // unused? + if (r1->getRuleIndex() == r2->getRuleIndex()) { + // track label->list-of-nodes for both rule name and label (if any) + labels[ruleTagToken->getRuleName()].push_back(tree); + if (ruleTagToken->getLabel() != "") { + labels[ruleTagToken->getLabel()].push_back(tree); + } + } else { + if (!mismatchedNode) { + mismatchedNode = r1; + } + } + + return mismatchedNode; + } + + // (expr ...) and (expr ...) + if (r1->children.size() != r2->children.size()) { + if (mismatchedNode == nullptr) { + mismatchedNode = r1; + } + + return mismatchedNode; + } + + std::size_t n = r1->children.size(); + for (size_t i = 0; i < n; i++) { + ParseTree *childMatch = matchImpl(r1->children[i], patternTree->children[i], labels); + if (childMatch) { + return childMatch; + } + } + + return mismatchedNode; + } + + // if nodes aren't both tokens or both rule nodes, can't match + return tree; +} + +RuleTagToken* ParseTreePatternMatcher::getRuleTagToken(ParseTree *t) { + if (t->children.size() == 1 && is(t->children[0])) { + TerminalNode *c = dynamic_cast(t->children[0]); + if (is(c->getSymbol())) { + return dynamic_cast(c->getSymbol()); + } + } + return nullptr; +} + +std::vector> ParseTreePatternMatcher::tokenize(const std::string &pattern) { + // split pattern into chunks: sea (raw input) and islands (, ) + std::vector chunks = split(pattern); + + // create token stream from text and tags + std::vector> tokens; + for (auto chunk : chunks) { + if (is(&chunk)) { + TagChunk &tagChunk = (TagChunk&)chunk; + // add special rule token or conjure up new token from name + if (isupper(tagChunk.getTag()[0])) { + size_t ttype = _parser->getTokenType(tagChunk.getTag()); + if (ttype == Token::INVALID_TYPE) { + throw IllegalArgumentException("Unknown token " + tagChunk.getTag() + " in pattern: " + pattern); + } + tokens.emplace_back(new TokenTagToken(tagChunk.getTag(), (int)ttype, tagChunk.getLabel())); + } else if (islower(tagChunk.getTag()[0])) { + size_t ruleIndex = _parser->getRuleIndex(tagChunk.getTag()); + if (ruleIndex == INVALID_INDEX) { + throw IllegalArgumentException("Unknown rule " + tagChunk.getTag() + " in pattern: " + pattern); + } + size_t ruleImaginaryTokenType = _parser->getATNWithBypassAlts().ruleToTokenType[ruleIndex]; + tokens.emplace_back(new RuleTagToken(tagChunk.getTag(), ruleImaginaryTokenType, tagChunk.getLabel())); + } else { + throw IllegalArgumentException("invalid tag: " + tagChunk.getTag() + " in pattern: " + pattern); + } + } else { + TextChunk &textChunk = (TextChunk&)chunk; + ANTLRInputStream input(textChunk.getText()); + _lexer->setInputStream(&input); + std::unique_ptr t(_lexer->nextToken()); + while (t->getType() != Token::EOF) { + tokens.push_back(std::move(t)); + t = _lexer->nextToken(); + } + _lexer->setInputStream(nullptr); + } + } + + return tokens; +} + +std::vector ParseTreePatternMatcher::split(const std::string &pattern) { + size_t p = 0; + size_t n = pattern.length(); + std::vector chunks; + + // find all start and stop indexes first, then collect + std::vector starts; + std::vector stops; + while (p < n) { + if (p == pattern.find(_escape + _start,p)) { + p += _escape.length() + _start.length(); + } else if (p == pattern.find(_escape + _stop,p)) { + p += _escape.length() + _stop.length(); + } else if (p == pattern.find(_start,p)) { + starts.push_back(p); + p += _start.length(); + } else if (p == pattern.find(_stop,p)) { + stops.push_back(p); + p += _stop.length(); + } else { + p++; + } + } + + if (starts.size() > stops.size()) { + throw IllegalArgumentException("unterminated tag in pattern: " + pattern); + } + + if (starts.size() < stops.size()) { + throw IllegalArgumentException("missing start tag in pattern: " + pattern); + } + + size_t ntags = starts.size(); + for (size_t i = 0; i < ntags; i++) { + if (starts[i] >= stops[i]) { + throw IllegalArgumentException("tag delimiters out of order in pattern: " + pattern); + } + } + + // collect into chunks now + if (ntags == 0) { + std::string text = pattern.substr(0, n); + chunks.push_back(TextChunk(text)); + } + + if (ntags > 0 && starts[0] > 0) { // copy text up to first tag into chunks + std::string text = pattern.substr(0, starts[0]); + chunks.push_back(TextChunk(text)); + } + + for (size_t i = 0; i < ntags; i++) { + // copy inside of + std::string tag = pattern.substr(starts[i] + _start.length(), stops[i] - (starts[i] + _start.length())); + std::string ruleOrToken = tag; + std::string label = ""; + size_t colon = tag.find(':'); + if (colon != std::string::npos) { + label = tag.substr(0,colon); + ruleOrToken = tag.substr(colon + 1, tag.length() - (colon + 1)); + } + chunks.push_back(TagChunk(label, ruleOrToken)); + if (i + 1 < ntags) { + // copy from end of to start of next + std::string text = pattern.substr(stops[i] + _stop.length(), starts[i + 1] - (stops[i] + _stop.length())); + chunks.push_back(TextChunk(text)); + } + } + + if (ntags > 0) { + size_t afterLastTag = stops[ntags - 1] + _stop.length(); + if (afterLastTag < n) { // copy text from end of last tag to end + std::string text = pattern.substr(afterLastTag, n - afterLastTag); + chunks.push_back(TextChunk(text)); + } + } + + // strip out all backslashes from text chunks but not tags + for (size_t i = 0; i < chunks.size(); i++) { + Chunk &c = chunks[i]; + if (is(&c)) { + TextChunk &tc = (TextChunk&)c; + std::string unescaped = tc.getText(); + unescaped.erase(std::remove(unescaped.begin(), unescaped.end(), '\\'), unescaped.end()); + if (unescaped.length() < tc.getText().length()) { + chunks[i] = TextChunk(unescaped); + } + } + } + + return chunks; +} + +void ParseTreePatternMatcher::InitializeInstanceFields() { + _start = "<"; + _stop = ">"; + _escape = "\\"; +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePatternMatcher.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePatternMatcher.h new file mode 100644 index 0000000..e77c7bc --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePatternMatcher.h @@ -0,0 +1,185 @@ +/* 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 "Exceptions.h" + +namespace antlr4 { +namespace tree { +namespace pattern { + + /// + /// A tree pattern matching mechanism for ANTLR s. + ///

+ /// Patterns are strings of source input text with special tags representing + /// token or rule references such as: + ///

+ /// {@code = ;} + ///

+ /// Given a pattern start rule such as {@code statement}, this object constructs + /// a with placeholders for the {@code ID} and {@code expr} + /// subtree. Then the routines can compare an actual + /// from a parse with this pattern. Tag {@code } matches + /// any {@code ID} token and tag {@code } references the result of the + /// {@code expr} rule (generally an instance of {@code ExprContext}. + ///

+ /// Pattern {@code x = 0;} is a similar pattern that matches the same pattern + /// except that it requires the identifier to be {@code x} and the expression to + /// be {@code 0}. + ///

+ /// The routines return {@code true} or {@code false} based + /// upon a match for the tree rooted at the parameter sent in. The + /// routines return a object that + /// contains the parse tree, the parse tree pattern, and a map from tag name to + /// matched nodes (more below). A subtree that fails to match, returns with + /// set to the first tree node that did not + /// match. + ///

+ /// For efficiency, you can compile a tree pattern in string form to a + /// object. + ///

+ /// See {@code TestParseTreeMatcher} for lots of examples. + /// has two static helper methods: + /// and that + /// are easy to use but not super efficient because they create new + /// objects each time and have to compile the + /// pattern in string form before using it. + ///

+ /// The lexer and parser that you pass into the + /// constructor are used to parse the pattern in string form. The lexer converts + /// the {@code = ;} into a sequence of four tokens (assuming lexer + /// throws out whitespace or puts it on a hidden channel). Be aware that the + /// input stream is reset for the lexer (but not the parser; a + /// is created to parse the input.). Any user-defined + /// fields you have put into the lexer might get changed when this mechanism asks + /// it to scan the pattern string. + ///

+ /// Normally a parser does not accept token {@code } as a valid + /// {@code expr} but, from the parser passed in, we create a special version of + /// the underlying grammar representation (an ) that allows imaginary + /// tokens representing rules ({@code }) to match entire rules. We call + /// these bypass alternatives. + ///

+ /// Delimiters are {@code <} and {@code >}, with {@code \} as the escape string + /// by default, but you can set them to whatever you want using + /// . You must escape both start and stop strings + /// {@code \<} and {@code \>}. + ///

+ class ANTLR4CPP_PUBLIC ParseTreePatternMatcher { + public: + class CannotInvokeStartRule : public RuntimeException { + public: + CannotInvokeStartRule(const RuntimeException &e); + ~CannotInvokeStartRule(); + }; + + // Fixes https://github.com/antlr/antlr4/issues/413 + // "Tree pattern compilation doesn't check for a complete parse" + class StartRuleDoesNotConsumeFullPattern : public RuntimeException { + public: + StartRuleDoesNotConsumeFullPattern() = default; + StartRuleDoesNotConsumeFullPattern(StartRuleDoesNotConsumeFullPattern const&) = default; + ~StartRuleDoesNotConsumeFullPattern(); + + StartRuleDoesNotConsumeFullPattern& operator=(StartRuleDoesNotConsumeFullPattern const&) = default; + }; + + /// Constructs a or from a and + /// object. The lexer input stream is altered for tokenizing + /// the tree patterns. The parser is used as a convenient mechanism to get + /// the grammar name, plus token, rule names. + ParseTreePatternMatcher(Lexer *lexer, Parser *parser); + virtual ~ParseTreePatternMatcher(); + + /// + /// Set the delimiters used for marking rule and token tags within concrete + /// syntax used by the tree pattern parser. + /// + /// The start delimiter. + /// The stop delimiter. + /// The escape sequence to use for escaping a start or stop delimiter. + /// + /// if {@code start} is {@code null} or empty. + /// if {@code stop} is {@code null} or empty. + virtual void setDelimiters(const std::string &start, const std::string &stop, const std::string &escapeLeft); + + /// + /// Does {@code pattern} matched as rule {@code patternRuleIndex} match {@code tree}? + virtual bool matches(ParseTree *tree, const std::string &pattern, int patternRuleIndex); + + /// + /// Does {@code pattern} matched as rule patternRuleIndex match tree? Pass in a + /// compiled pattern instead of a string representation of a tree pattern. + /// + virtual bool matches(ParseTree *tree, const ParseTreePattern &pattern); + + /// + /// Compare {@code pattern} matched as rule {@code patternRuleIndex} against + /// {@code tree} and return a object that contains the + /// matched elements, or the node at which the match failed. + /// + virtual ParseTreeMatch match(ParseTree *tree, const std::string &pattern, int patternRuleIndex); + + /// + /// Compare {@code pattern} matched against {@code tree} and return a + /// object that contains the matched elements, or the + /// node at which the match failed. Pass in a compiled pattern instead of a + /// string representation of a tree pattern. + /// + virtual ParseTreeMatch match(ParseTree *tree, const ParseTreePattern &pattern); + + /// + /// For repeated use of a tree pattern, compile it to a + /// using this method. + /// + virtual ParseTreePattern compile(const std::string &pattern, int patternRuleIndex); + + /// + /// Used to convert the tree pattern string into a series of tokens. The + /// input stream is reset. + /// + virtual Lexer* getLexer(); + + /// + /// Used to collect to the grammar file name, token names, rule names for + /// used to parse the pattern into a parse tree. + /// + virtual Parser* getParser(); + + // ---- SUPPORT CODE ---- + + virtual std::vector> tokenize(const std::string &pattern); + + /// Split " = ;" into 4 chunks for tokenizing by tokenize(). + virtual std::vector split(const std::string &pattern); + + protected: + std::string _start; + std::string _stop; + std::string _escape; // e.g., \< and \> must escape BOTH! + + /// Recursively walk {@code tree} against {@code patternTree}, filling + /// {@code match.}. + /// + /// the first node encountered in {@code tree} which does not match + /// a corresponding node in {@code patternTree}, or {@code null} if the match + /// was successful. The specific node returned depends on the matching + /// algorithm used by the implementation, and may be overridden. + virtual ParseTree* matchImpl(ParseTree *tree, ParseTree *patternTree, std::map> &labels); + + /// Is t subtree? + virtual RuleTagToken* getRuleTagToken(ParseTree *t); + + private: + Lexer *_lexer; + Parser *_parser; + + void InitializeInstanceFields(); + }; + +} // namespace pattern +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/RuleTagToken.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/RuleTagToken.cpp new file mode 100644 index 0000000..4e33f98 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/RuleTagToken.cpp @@ -0,0 +1,77 @@ +/* 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. + */ + +#include "Exceptions.h" + +#include "tree/pattern/RuleTagToken.h" + +using namespace antlr4::tree::pattern; + +RuleTagToken::RuleTagToken(const std::string &/*ruleName*/, int _bypassTokenType) : bypassTokenType(_bypassTokenType) { +} + +RuleTagToken::RuleTagToken(const std::string &ruleName, size_t bypassTokenType, const std::string &label) + : ruleName(ruleName), bypassTokenType(bypassTokenType), label(label) { + if (ruleName.empty()) { + throw IllegalArgumentException("ruleName cannot be null or empty."); + } + +} + +std::string RuleTagToken::getRuleName() const { + return ruleName; +} + +std::string RuleTagToken::getLabel() const { + return label; +} + +size_t RuleTagToken::getChannel() const { + return DEFAULT_CHANNEL; +} + +std::string RuleTagToken::getText() const { + if (label != "") { + return std::string("<") + label + std::string(":") + ruleName + std::string(">"); + } + + return std::string("<") + ruleName + std::string(">"); +} + +size_t RuleTagToken::getType() const { + return bypassTokenType; +} + +size_t RuleTagToken::getLine() const { + return 0; +} + +size_t RuleTagToken::getCharPositionInLine() const { + return INVALID_INDEX; +} + +size_t RuleTagToken::getTokenIndex() const { + return INVALID_INDEX; +} + +size_t RuleTagToken::getStartIndex() const { + return INVALID_INDEX; +} + +size_t RuleTagToken::getStopIndex() const { + return INVALID_INDEX; +} + +antlr4::TokenSource *RuleTagToken::getTokenSource() const { + return nullptr; +} + +antlr4::CharStream *RuleTagToken::getInputStream() const { + return nullptr; +} + +std::string RuleTagToken::toString() const { + return ruleName + ":" + std::to_string(bypassTokenType); +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/RuleTagToken.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/RuleTagToken.h new file mode 100644 index 0000000..368ae41 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/RuleTagToken.h @@ -0,0 +1,117 @@ +/* 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 "Token.h" + +namespace antlr4 { +namespace tree { +namespace pattern { + + /// + /// A object representing an entire subtree matched by a parser + /// rule; e.g., {@code }. These tokens are created for + /// chunks where the tag corresponds to a parser rule. + /// + class ANTLR4CPP_PUBLIC RuleTagToken : public Token { + /// + /// This is the backing field for . + /// + private: + const std::string ruleName; + + /// The token type for the current token. This is the token type assigned to + /// the bypass alternative for the rule during ATN deserialization. + const size_t bypassTokenType; + + /// This is the backing field for . + const std::string label; + + public: + /// + /// Constructs a new instance of with the specified rule + /// name and bypass token type and no label. + /// + /// The name of the parser rule this rule tag matches. + /// The bypass token type assigned to the parser rule. + /// + /// if {@code ruleName} is {@code null} + /// or empty. + RuleTagToken(const std::string &ruleName, int bypassTokenType); //this(ruleName, bypassTokenType, nullptr); + + /// + /// Constructs a new instance of with the specified rule + /// name, bypass token type, and label. + /// + /// The name of the parser rule this rule tag matches. + /// The bypass token type assigned to the parser rule. + /// The label associated with the rule tag, or {@code null} if + /// the rule tag is unlabeled. + /// + /// if {@code ruleName} is {@code null} + /// or empty. + RuleTagToken(const std::string &ruleName, size_t bypassTokenType, const std::string &label); + + /// + /// Gets the name of the rule associated with this rule tag. + /// + /// The name of the parser rule associated with this rule tag. + std::string getRuleName() const; + + /// + /// Gets the label associated with the rule tag. + /// + /// The name of the label associated with the rule tag, or + /// {@code null} if this is an unlabeled rule tag. + std::string getLabel() const; + + /// + /// {@inheritDoc} + ///

+ /// Rule tag tokens are always placed on the . + ///

+ virtual size_t getChannel() const override; + + /// + /// {@inheritDoc} + ///

+ /// This method returns the rule tag formatted with {@code <} and {@code >} + /// delimiters. + ///

+ virtual std::string getText() const override; + + /// Rule tag tokens have types assigned according to the rule bypass + /// transitions created during ATN deserialization. + virtual size_t getType() const override; + + /// The implementation for always returns 0. + virtual size_t getLine() const override; + + /// The implementation for always returns INVALID_INDEX. + virtual size_t getCharPositionInLine() const override; + + /// The implementation for always returns INVALID_INDEX. + virtual size_t getTokenIndex() const override; + + /// The implementation for always returns INVALID_INDEX. + virtual size_t getStartIndex() const override; + + /// The implementation for always returns INVALID_INDEX. + virtual size_t getStopIndex() const override; + + /// The implementation for always returns {@code null}. + virtual TokenSource *getTokenSource() const override; + + /// The implementation for always returns {@code null}. + virtual CharStream *getInputStream() const override; + + /// The implementation for returns a string of the form {@code ruleName:bypassTokenType}. + virtual std::string toString() const override; + }; + +} // namespace pattern +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TagChunk.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TagChunk.cpp new file mode 100644 index 0000000..77f2b4c --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TagChunk.cpp @@ -0,0 +1,39 @@ +/* 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. + */ + +#include "Exceptions.h" + +#include "tree/pattern/TagChunk.h" + +using namespace antlr4::tree::pattern; + +TagChunk::TagChunk(const std::string &tag) : TagChunk("", tag) { +} + +TagChunk::TagChunk(const std::string &label, const std::string &tag) : _tag(tag), _label(label) { + if (tag.empty()) { + throw IllegalArgumentException("tag cannot be null or empty"); + } + +} + +TagChunk::~TagChunk() { +} + +std::string TagChunk::getTag() { + return _tag; +} + +std::string TagChunk::getLabel() { + return _label; +} + +std::string TagChunk::toString() { + if (!_label.empty()) { + return _label + ":" + _tag; + } + + return _tag; +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TagChunk.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TagChunk.h new file mode 100644 index 0000000..3d0c9f8 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TagChunk.h @@ -0,0 +1,86 @@ +/* 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 "Chunk.h" + +namespace antlr4 { +namespace tree { +namespace pattern { + + /// + /// Represents a placeholder tag in a tree pattern. A tag can have any of the + /// following forms. + /// + ///
    + ///
  • {@code expr}: An unlabeled placeholder for a parser rule {@code expr}.
  • + ///
  • {@code ID}: An unlabeled placeholder for a token of type {@code ID}.
  • + ///
  • {@code e:expr}: A labeled placeholder for a parser rule {@code expr}.
  • + ///
  • {@code id:ID}: A labeled placeholder for a token of type {@code ID}.
  • + ///
+ /// + /// This class does not perform any validation on the tag or label names aside + /// from ensuring that the tag is a non-null, non-empty string. + ///
+ class ANTLR4CPP_PUBLIC TagChunk : public Chunk { + public: + /// + /// Construct a new instance of using the specified tag and + /// no label. + /// + /// The tag, which should be the name of a parser rule or token + /// type. + /// + /// if {@code tag} is {@code null} or + /// empty. + TagChunk(const std::string &tag); + virtual ~TagChunk(); + + /// + /// Construct a new instance of using the specified label + /// and tag. + /// + /// The label for the tag. If this is {@code null}, the + /// represents an unlabeled tag. + /// The tag, which should be the name of a parser rule or token + /// type. + /// + /// if {@code tag} is {@code null} or + /// empty. + TagChunk(const std::string &label, const std::string &tag); + + /// + /// Get the tag for this chunk. + /// + /// The tag for the chunk. + std::string getTag(); + + /// + /// Get the label, if any, assigned to this chunk. + /// + /// The label assigned to this chunk, or {@code null} if no label is + /// assigned to the chunk. + std::string getLabel(); + + /// + /// This method returns a text representation of the tag chunk. Labeled tags + /// are returned in the form {@code label:tag}, and unlabeled tags are + /// returned as just the tag name. + /// + virtual std::string toString() override; + + private: + /// This is the backing field for . + const std::string _tag; + /// + /// This is the backing field for . + /// + const std::string _label; + }; + +} // namespace pattern +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TextChunk.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TextChunk.cpp new file mode 100644 index 0000000..f8dcfb0 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TextChunk.cpp @@ -0,0 +1,28 @@ +/* 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. + */ + +#include "Exceptions.h" + +#include "tree/pattern/TextChunk.h" + +using namespace antlr4::tree::pattern; + +TextChunk::TextChunk(const std::string &text) : text(text) { + if (text == "") { + throw IllegalArgumentException("text cannot be nul"); + } + +} + +TextChunk::~TextChunk() { +} + +std::string TextChunk::getText() { + return text; +} + +std::string TextChunk::toString() { + return std::string("'") + text + std::string("'"); +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TextChunk.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TextChunk.h new file mode 100644 index 0000000..1cbc0dd --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TextChunk.h @@ -0,0 +1,51 @@ +/* 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 "Chunk.h" + +namespace antlr4 { +namespace tree { +namespace pattern { + + /// + /// Represents a span of raw text (concrete syntax) between tags in a tree + /// pattern string. + /// + class ANTLR4CPP_PUBLIC TextChunk : public Chunk { + private: + /// + /// This is the backing field for . + /// + const std::string text; + + /// + /// Constructs a new instance of with the specified text. + /// + /// The text of this chunk. + /// if {@code text} is {@code null}. + public: + TextChunk(const std::string &text); + virtual ~TextChunk(); + + /// + /// Gets the raw text of this chunk. + /// + /// The text of the chunk. + std::string getText(); + + /// + /// {@inheritDoc} + ///

+ /// The implementation for returns the result of + /// in single quotes. + ///

+ virtual std::string toString() override; + }; + +} // namespace pattern +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TokenTagToken.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TokenTagToken.cpp new file mode 100644 index 0000000..7d6cc9a --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TokenTagToken.cpp @@ -0,0 +1,36 @@ +/* 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. + */ + +#include "tree/pattern/TokenTagToken.h" + +using namespace antlr4::tree::pattern; + +TokenTagToken::TokenTagToken(const std::string &/*tokenName*/, int type) + : CommonToken(type), tokenName(""), label("") { +} + +TokenTagToken::TokenTagToken(const std::string &tokenName, int type, const std::string &label) + : CommonToken(type), tokenName(tokenName), label(label) { +} + +std::string TokenTagToken::getTokenName() const { + return tokenName; +} + +std::string TokenTagToken::getLabel() const { + return label; +} + +std::string TokenTagToken::getText() const { + if (!label.empty()) { + return "<" + label + ":" + tokenName + ">"; + } + + return "<" + tokenName + ">"; +} + +std::string TokenTagToken::toString() const { + return tokenName + ":" + std::to_string(_type); +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TokenTagToken.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TokenTagToken.h new file mode 100644 index 0000000..9013fb8 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/TokenTagToken.h @@ -0,0 +1,80 @@ +/* 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 "CommonToken.h" + +namespace antlr4 { +namespace tree { +namespace pattern { + + /// + /// A object representing a token of a particular type; e.g., + /// {@code }. These tokens are created for chunks where the + /// tag corresponds to a lexer rule or token type. + /// + class ANTLR4CPP_PUBLIC TokenTagToken : public CommonToken { + /// + /// This is the backing field for . + /// + private: + const std::string tokenName; + /// + /// This is the backing field for . + /// + const std::string label; + + /// + /// Constructs a new instance of for an unlabeled tag + /// with the specified token name and type. + /// + /// The token name. + /// The token type. + public: + TokenTagToken(const std::string &tokenName, int type); //this(tokenName, type, nullptr); + + /// + /// Constructs a new instance of with the specified + /// token name, type, and label. + /// + /// The token name. + /// The token type. + /// The label associated with the token tag, or {@code null} if + /// the token tag is unlabeled. + TokenTagToken(const std::string &tokenName, int type, const std::string &label); + + /// + /// Gets the token name. + /// The token name. + std::string getTokenName() const; + + /// + /// Gets the label associated with the rule tag. + /// + /// The name of the label associated with the rule tag, or + /// {@code null} if this is an unlabeled rule tag. + std::string getLabel() const; + + /// + /// {@inheritDoc} + ///

+ /// The implementation for returns the token tag + /// formatted with {@code <} and {@code >} delimiters. + ///

+ virtual std::string getText() const override; + + /// + /// {@inheritDoc} + ///

+ /// The implementation for returns a string of the form + /// {@code tokenName:type}. + ///

+ virtual std::string toString() const override; + }; + +} // namespace pattern +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPath.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPath.cpp new file mode 100644 index 0000000..c039896 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPath.cpp @@ -0,0 +1,154 @@ +/* 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. + */ + +#include "XPathLexer.h" +#include "XPathLexerErrorListener.h" +#include "XPathElement.h" +#include "XPathWildcardAnywhereElement.h" +#include "XPathWildcardElement.h" +#include "XPathTokenAnywhereElement.h" +#include "XPathTokenElement.h" +#include "XPathRuleAnywhereElement.h" +#include "XPathRuleElement.h" + +#include "XPath.h" + +using namespace antlr4; +using namespace antlr4::tree; +using namespace antlr4::tree::xpath; + +const std::string XPath::WILDCARD = "*"; +const std::string XPath::NOT = "!"; + +XPath::XPath(Parser *parser, const std::string &path) { + _parser = parser; + _path = path; +} + +std::vector> XPath::split(const std::string &path) { + ANTLRInputStream in(path); + XPathLexer lexer(&in); + lexer.removeErrorListeners(); + XPathLexerErrorListener listener; + lexer.addErrorListener(&listener); + CommonTokenStream tokenStream(&lexer); + try { + tokenStream.fill(); + } catch (LexerNoViableAltException &) { + size_t pos = lexer.getCharPositionInLine(); + std::string msg = "Invalid tokens or characters at index " + std::to_string(pos) + " in path '" + path + "'"; + throw IllegalArgumentException(msg); + } + + std::vector tokens = tokenStream.getTokens(); + std::vector> elements; + size_t n = tokens.size(); + size_t i = 0; + bool done = false; + while (!done && i < n) { + Token *el = tokens[i]; + Token *next = nullptr; + switch (el->getType()) { + case XPathLexer::ROOT: + case XPathLexer::ANYWHERE: { + bool anywhere = el->getType() == XPathLexer::ANYWHERE; + i++; + next = tokens[i]; + bool invert = next->getType() == XPathLexer::BANG; + if (invert) { + i++; + next = tokens[i]; + } + std::unique_ptr pathElement = getXPathElement(next, anywhere); + pathElement->setInvert(invert); + elements.push_back(std::move(pathElement)); + i++; + break; + + } + case XPathLexer::TOKEN_REF: + case XPathLexer::RULE_REF: + case XPathLexer::WILDCARD: + elements.push_back(getXPathElement(el, false)); + i++; + break; + + case Token::EOF: + done = true; + break; + + default : + throw IllegalArgumentException("Unknown path element " + el->toString()); + } + } + + return elements; +} + +std::unique_ptr XPath::getXPathElement(Token *wordToken, bool anywhere) { + if (wordToken->getType() == Token::EOF) { + throw IllegalArgumentException("Missing path element at end of path"); + } + + std::string word = wordToken->getText(); + size_t ttype = _parser->getTokenType(word); + ssize_t ruleIndex = _parser->getRuleIndex(word); + switch (wordToken->getType()) { + case XPathLexer::WILDCARD : + if (anywhere) + return std::unique_ptr(new XPathWildcardAnywhereElement()); + return std::unique_ptr(new XPathWildcardElement()); + + case XPathLexer::TOKEN_REF: + case XPathLexer::STRING : + if (ttype == Token::INVALID_TYPE) { + throw IllegalArgumentException(word + " at index " + std::to_string(wordToken->getStartIndex()) + " isn't a valid token name"); + } + if (anywhere) + return std::unique_ptr(new XPathTokenAnywhereElement(word, (int)ttype)); + return std::unique_ptr(new XPathTokenElement(word, (int)ttype)); + + default : + if (ruleIndex == -1) { + throw IllegalArgumentException(word + " at index " + std::to_string(wordToken->getStartIndex()) + " isn't a valid rule name"); + } + if (anywhere) + return std::unique_ptr(new XPathRuleAnywhereElement(word, (int)ruleIndex)); + return std::unique_ptr(new XPathRuleElement(word, (int)ruleIndex)); + } +} + +static ParserRuleContext dummyRoot; + +std::vector XPath::findAll(ParseTree *tree, std::string const& xpath, Parser *parser) { + XPath p(parser, xpath); + return p.evaluate(tree); +} + +std::vector XPath::evaluate(ParseTree *t) { + dummyRoot.children = { t }; // don't set t's parent. + + std::vector work = { &dummyRoot }; + + size_t i = 0; + std::vector> elements = split(_path); + + while (i < elements.size()) { + std::vector next; + for (auto *node : work) { + if (!node->children.empty()) { + // only try to match next element if it has children + // e.g., //func/*/stat might have a token node for which + // we can't go looking for stat nodes. + auto matching = elements[i]->evaluate(node); + next.insert(next.end(), matching.begin(), matching.end()); + } + } + i++; + work = next; + } + + return work; +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPath.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPath.h new file mode 100644 index 0000000..e38d482 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPath.h @@ -0,0 +1,86 @@ +/* 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 xpath { + + /// Represent a subset of XPath XML path syntax for use in identifying nodes in + /// parse trees. + /// + /// + /// Split path into words and separators {@code /} and {@code //} via ANTLR + /// itself then walk path elements from left to right. At each separator-word + /// pair, find set of nodes. Next stage uses those as work list. + /// + /// + /// The basic interface is + /// {@code (tree, pathString, parser)}. + /// But that is just shorthand for: + /// + ///
+  ///  p = new (parser, pathString);
+  /// return p.(tree);
+  /// 
+ /// + /// + /// See {@code org.antlr.v4.test.TestXPath} for descriptions. In short, this + /// allows operators: + /// + ///
+ ///
/
root
+ ///
//
anywhere
+ ///
!
invert; this must appear directly after root or anywhere + /// operator
+ ///
+ /// + /// + /// and path elements: + /// + ///
+ ///
ID
token name
+ ///
'string'
any string literal token from the grammar
+ ///
expr
rule name
+ ///
*
wildcard matching any node
+ ///
+ /// + /// + /// Whitespace is not allowed. + + class ANTLR4CPP_PUBLIC XPath { + public: + static const std::string WILDCARD; // word not operator/separator + static const std::string NOT; // word for invert operator + + XPath(Parser *parser, const std::string &path); + virtual ~XPath() {} + + // TODO: check for invalid token/rule names, bad syntax + virtual std::vector> split(const std::string &path); + + static std::vector findAll(ParseTree *tree, std::string const& xpath, Parser *parser); + + /// Return a list of all nodes starting at {@code t} as root that satisfy the + /// path. The root {@code /} is relative to the node passed to + /// . + virtual std::vector evaluate(ParseTree *t); + + protected: + std::string _path; + Parser *_parser; + + /// Convert word like {@code *} or {@code ID} or {@code expr} to a path + /// element. {@code anywhere} is {@code true} if {@code //} precedes the + /// word. + virtual std::unique_ptr getXPathElement(Token *wordToken, bool anywhere); + }; + +} // namespace xpath +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathElement.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathElement.cpp new file mode 100644 index 0000000..64b122d --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathElement.cpp @@ -0,0 +1,31 @@ +/* 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. + */ + +#include "support/CPPUtils.h" + +#include "XPathElement.h" + +using namespace antlr4::tree; +using namespace antlr4::tree::xpath; + +XPathElement::XPathElement(const std::string &nodeName) { + _nodeName = nodeName; +} + +XPathElement::~XPathElement() { +} + +std::vector XPathElement::evaluate(ParseTree * /*t*/) { + return {}; +} + +std::string XPathElement::toString() const { + std::string inv = _invert ? "!" : ""; + return antlrcpp::toString(*this) + "[" + inv + _nodeName + "]"; +} + +void XPathElement::setInvert(bool value) { + _invert = value; +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathElement.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathElement.h new file mode 100644 index 0000000..f339117 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathElement.h @@ -0,0 +1,40 @@ +/* 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 { + class ParseTree; + +namespace xpath { + + class ANTLR4CPP_PUBLIC XPathElement { + public: + /// Construct element like {@code /ID} or {@code ID} or {@code /*} etc... + /// op is null if just node + XPathElement(const std::string &nodeName); + XPathElement(XPathElement const&) = default; + virtual ~XPathElement(); + + XPathElement& operator=(XPathElement const&) = default; + + /// Given tree rooted at {@code t} return all nodes matched by this path + /// element. + virtual std::vector evaluate(ParseTree *t); + virtual std::string toString() const; + + void setInvert(bool value); + + protected: + std::string _nodeName; + bool _invert = false; + }; + +} // namespace xpath +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.cpp new file mode 100644 index 0000000..fb18788 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.cpp @@ -0,0 +1,173 @@ +#include "XPathLexer.h" + + +using namespace antlr4; + + +XPathLexer::XPathLexer(CharStream *input) : Lexer(input) { + _interpreter = new atn::LexerATNSimulator(this, _atn, _decisionToDFA, _sharedContextCache); +} + +XPathLexer::~XPathLexer() { + delete _interpreter; +} + +std::string XPathLexer::getGrammarFileName() const { + return "XPathLexer.g4"; +} + +const std::vector& XPathLexer::getRuleNames() const { + return _ruleNames; +} + +const std::vector& XPathLexer::getChannelNames() const { + return _channelNames; +} + +const std::vector& XPathLexer::getModeNames() const { + return _modeNames; +} + +const std::vector& XPathLexer::getTokenNames() const { + return _tokenNames; +} + +dfa::Vocabulary& XPathLexer::getVocabulary() const { + return _vocabulary; +} + +const std::vector XPathLexer::getSerializedATN() const { + return _serializedATN; +} + +const atn::ATN& XPathLexer::getATN() const { + return _atn; +} + + +void XPathLexer::action(RuleContext *context, size_t ruleIndex, size_t actionIndex) { + switch (ruleIndex) { + case 4: IDAction(dynamic_cast(context), actionIndex); break; + + default: + break; + } +} + +void XPathLexer::IDAction(antlr4::RuleContext * /*context*/, size_t actionIndex) { + switch (actionIndex) { + case 0: + if (isupper(getText()[0])) + setType(TOKEN_REF); + else + setType(RULE_REF); + break; + + default: + break; + } +} + + + +// Static vars and initialization. +std::vector XPathLexer::_decisionToDFA; +atn::PredictionContextCache XPathLexer::_sharedContextCache; + +// We own the ATN which in turn owns the ATN states. +atn::ATN XPathLexer::_atn; +std::vector XPathLexer::_serializedATN; + +std::vector XPathLexer::_ruleNames = { + "ANYWHERE", "ROOT", "WILDCARD", "BANG", "ID", "NameChar", "NameStartChar", + "STRING" +}; + +std::vector XPathLexer::_channelNames = { + "DEFAULT_TOKEN_CHANNEL", "HIDDEN" +}; + +std::vector XPathLexer::_modeNames = { + "DEFAULT_MODE" +}; + +std::vector XPathLexer::_literalNames = { + "", "", "", "'//'", "'/'", "'*'", "'!'" +}; + +std::vector XPathLexer::_symbolicNames = { + "", "TOKEN_REF", "RULE_REF", "ANYWHERE", "ROOT", "WILDCARD", "BANG", "ID", + "STRING" +}; + +dfa::Vocabulary XPathLexer::_vocabulary(_literalNames, _symbolicNames); + +std::vector XPathLexer::_tokenNames; + +XPathLexer::Initializer::Initializer() { + // This code could be in a static initializer lambda, but VS doesn't allow access to private class members from there. + for (size_t i = 0; i < _symbolicNames.size(); ++i) { + std::string name = _vocabulary.getLiteralName(i); + if (name.empty()) { + name = _vocabulary.getSymbolicName(i); + } + + if (name.empty()) { + _tokenNames.push_back(""); + } else { + _tokenNames.push_back(name); + } + } + + _serializedATN = { + 0x3, 0x430, 0xd6d1, 0x8206, 0xad2d, 0x4417, 0xaef1, 0x8d80, 0xaadd, + 0x2, 0xa, 0x34, 0x8, 0x1, 0x4, 0x2, 0x9, 0x2, 0x4, 0x3, 0x9, 0x3, 0x4, + 0x4, 0x9, 0x4, 0x4, 0x5, 0x9, 0x5, 0x4, 0x6, 0x9, 0x6, 0x4, 0x7, 0x9, + 0x7, 0x4, 0x8, 0x9, 0x8, 0x4, 0x9, 0x9, 0x9, 0x3, 0x2, 0x3, 0x2, 0x3, + 0x2, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x4, 0x3, 0x5, 0x3, 0x5, 0x3, + 0x6, 0x3, 0x6, 0x7, 0x6, 0x1f, 0xa, 0x6, 0xc, 0x6, 0xe, 0x6, 0x22, 0xb, + 0x6, 0x3, 0x6, 0x3, 0x6, 0x3, 0x7, 0x3, 0x7, 0x5, 0x7, 0x28, 0xa, 0x7, + 0x3, 0x8, 0x3, 0x8, 0x3, 0x9, 0x3, 0x9, 0x7, 0x9, 0x2e, 0xa, 0x9, 0xc, + 0x9, 0xe, 0x9, 0x31, 0xb, 0x9, 0x3, 0x9, 0x3, 0x9, 0x3, 0x2f, 0x2, 0xa, + 0x3, 0x5, 0x5, 0x6, 0x7, 0x7, 0x9, 0x8, 0xb, 0x9, 0xd, 0x2, 0xf, 0x2, + 0x11, 0xa, 0x3, 0x2, 0x4, 0x7, 0x2, 0x32, 0x3b, 0x61, 0x61, 0xb9, 0xb9, + 0x302, 0x371, 0x2041, 0x2042, 0xf, 0x2, 0x43, 0x5c, 0x63, 0x7c, 0xc2, + 0xd8, 0xda, 0xf8, 0xfa, 0x301, 0x372, 0x37f, 0x381, 0x2001, 0x200e, + 0x200f, 0x2072, 0x2191, 0x2c02, 0x2ff1, 0x3003, 0xd801, 0xf902, 0xfdd1, + 0xfdf2, 0x1, 0x34, 0x2, 0x3, 0x3, 0x2, 0x2, 0x2, 0x2, 0x5, 0x3, 0x2, + 0x2, 0x2, 0x2, 0x7, 0x3, 0x2, 0x2, 0x2, 0x2, 0x9, 0x3, 0x2, 0x2, 0x2, + 0x2, 0xb, 0x3, 0x2, 0x2, 0x2, 0x2, 0x11, 0x3, 0x2, 0x2, 0x2, 0x3, 0x13, + 0x3, 0x2, 0x2, 0x2, 0x5, 0x16, 0x3, 0x2, 0x2, 0x2, 0x7, 0x18, 0x3, 0x2, + 0x2, 0x2, 0x9, 0x1a, 0x3, 0x2, 0x2, 0x2, 0xb, 0x1c, 0x3, 0x2, 0x2, 0x2, + 0xd, 0x27, 0x3, 0x2, 0x2, 0x2, 0xf, 0x29, 0x3, 0x2, 0x2, 0x2, 0x11, + 0x2b, 0x3, 0x2, 0x2, 0x2, 0x13, 0x14, 0x7, 0x31, 0x2, 0x2, 0x14, 0x15, + 0x7, 0x31, 0x2, 0x2, 0x15, 0x4, 0x3, 0x2, 0x2, 0x2, 0x16, 0x17, 0x7, + 0x31, 0x2, 0x2, 0x17, 0x6, 0x3, 0x2, 0x2, 0x2, 0x18, 0x19, 0x7, 0x2c, + 0x2, 0x2, 0x19, 0x8, 0x3, 0x2, 0x2, 0x2, 0x1a, 0x1b, 0x7, 0x23, 0x2, + 0x2, 0x1b, 0xa, 0x3, 0x2, 0x2, 0x2, 0x1c, 0x20, 0x5, 0xf, 0x8, 0x2, + 0x1d, 0x1f, 0x5, 0xd, 0x7, 0x2, 0x1e, 0x1d, 0x3, 0x2, 0x2, 0x2, 0x1f, + 0x22, 0x3, 0x2, 0x2, 0x2, 0x20, 0x1e, 0x3, 0x2, 0x2, 0x2, 0x20, 0x21, + 0x3, 0x2, 0x2, 0x2, 0x21, 0x23, 0x3, 0x2, 0x2, 0x2, 0x22, 0x20, 0x3, + 0x2, 0x2, 0x2, 0x23, 0x24, 0x8, 0x6, 0x2, 0x2, 0x24, 0xc, 0x3, 0x2, + 0x2, 0x2, 0x25, 0x28, 0x5, 0xf, 0x8, 0x2, 0x26, 0x28, 0x9, 0x2, 0x2, + 0x2, 0x27, 0x25, 0x3, 0x2, 0x2, 0x2, 0x27, 0x26, 0x3, 0x2, 0x2, 0x2, + 0x28, 0xe, 0x3, 0x2, 0x2, 0x2, 0x29, 0x2a, 0x9, 0x3, 0x2, 0x2, 0x2a, + 0x10, 0x3, 0x2, 0x2, 0x2, 0x2b, 0x2f, 0x7, 0x29, 0x2, 0x2, 0x2c, 0x2e, + 0xb, 0x2, 0x2, 0x2, 0x2d, 0x2c, 0x3, 0x2, 0x2, 0x2, 0x2e, 0x31, 0x3, + 0x2, 0x2, 0x2, 0x2f, 0x30, 0x3, 0x2, 0x2, 0x2, 0x2f, 0x2d, 0x3, 0x2, + 0x2, 0x2, 0x30, 0x32, 0x3, 0x2, 0x2, 0x2, 0x31, 0x2f, 0x3, 0x2, 0x2, + 0x2, 0x32, 0x33, 0x7, 0x29, 0x2, 0x2, 0x33, 0x12, 0x3, 0x2, 0x2, 0x2, + 0x6, 0x2, 0x20, 0x27, 0x2f, 0x3, 0x3, 0x6, 0x2, + }; + + atn::ATNDeserializer deserializer; + _atn = deserializer.deserialize(_serializedATN); + + size_t count = _atn.getNumberOfDecisions(); + _decisionToDFA.reserve(count); + for (size_t i = 0; i < count; i++) { + _decisionToDFA.emplace_back(_atn.getDecisionState(i), i); + } +} + +XPathLexer::Initializer XPathLexer::_init; diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.g4 b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.g4 new file mode 100644 index 0000000..14bcf5a --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.g4 @@ -0,0 +1,64 @@ +lexer grammar XPathLexer; + +tokens { TOKEN_REF, RULE_REF } + +/* +path : separator? word (separator word)* EOF ; + +separator + : '/' '!' + | '//' '!' + | '/' + | '//' + ; + +word: TOKEN_REF + | RULE_REF + | STRING + | '*' + ; +*/ + +ANYWHERE : '//' ; +ROOT : '/' ; +WILDCARD : '*' ; +BANG : '!' ; + +ID : NameStartChar NameChar* + { + if (isupper(getText()[0])) + setType(TOKEN_REF); + else + setType(RULE_REF); + } + ; + +fragment +NameChar : NameStartChar + | '0'..'9' + | '_' + | '\u00B7' + | '\u0300'..'\u036F' + | '\u203F'..'\u2040' + ; + +fragment +NameStartChar + : 'A'..'Z' | 'a'..'z' + | '\u00C0'..'\u00D6' + | '\u00D8'..'\u00F6' + | '\u00F8'..'\u02FF' + | '\u0370'..'\u037D' + | '\u037F'..'\u1FFF' + | '\u200C'..'\u200D' + | '\u2070'..'\u218F' + | '\u2C00'..'\u2FEF' + | '\u3001'..'\uD7FF' + | '\uF900'..'\uFDCF' + | '\uFDF0'..'\uFFFF' // implicitly includes ['\u10000-'\uEFFFF] + ; + +STRING : '\'' .*? '\''; + +//WS : [ \t\r\n]+ -> skip ; + diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.h new file mode 100644 index 0000000..ca471c9 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.h @@ -0,0 +1,56 @@ +#pragma once + + +#include "antlr4-runtime.h" + + + + +class XPathLexer : public antlr4::Lexer { +public: + enum { + TOKEN_REF = 1, RULE_REF = 2, ANYWHERE = 3, ROOT = 4, WILDCARD = 5, BANG = 6, + ID = 7, STRING = 8 + }; + + XPathLexer(antlr4::CharStream *input); + ~XPathLexer(); + + virtual std::string getGrammarFileName() const override; + virtual const std::vector& getRuleNames() const override; + + virtual const std::vector& getChannelNames() const override; + virtual const std::vector& getModeNames() const override; + virtual const std::vector& getTokenNames() const override; // deprecated, use vocabulary instead + virtual antlr4::dfa::Vocabulary& getVocabulary() const override; + + virtual const std::vector getSerializedATN() const override; + virtual const antlr4::atn::ATN& getATN() const override; + + virtual void action(antlr4::RuleContext *context, size_t ruleIndex, size_t actionIndex) override; +private: + static std::vector _decisionToDFA; + static antlr4::atn::PredictionContextCache _sharedContextCache; + static std::vector _ruleNames; + static std::vector _tokenNames; + static std::vector _channelNames; + static std::vector _modeNames; + + static std::vector _literalNames; + static std::vector _symbolicNames; + static antlr4::dfa::Vocabulary _vocabulary; + static antlr4::atn::ATN _atn; + static std::vector _serializedATN; + + + // Individual action functions triggered by action() above. + void IDAction(antlr4::RuleContext *context, size_t actionIndex); + + // Individual semantic predicate functions triggered by sempred() above. + + struct Initializer { + Initializer(); + }; + static Initializer _init; +}; + diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.tokens b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.tokens new file mode 100644 index 0000000..5bf699e --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexer.tokens @@ -0,0 +1,12 @@ +TOKEN_REF=1 +RULE_REF=2 +ANYWHERE=3 +ROOT=4 +WILDCARD=5 +BANG=6 +ID=7 +STRING=8 +'//'=3 +'/'=4 +'*'=5 +'!'=6 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexerErrorListener.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexerErrorListener.cpp new file mode 100644 index 0000000..2804c8e --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexerErrorListener.cpp @@ -0,0 +1,13 @@ +/* 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. + */ + +#include "XPathLexerErrorListener.h" + +using namespace antlr4; +using namespace antlr4::tree::xpath; + +void XPathLexerErrorListener::syntaxError(Recognizer * /*recognizer*/, Token * /*offendingSymbol*/, + size_t /*line*/, size_t /*charPositionInLine*/, const std::string &/*msg*/, std::exception_ptr /*e*/) { +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexerErrorListener.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexerErrorListener.h new file mode 100644 index 0000000..c0c3eaa --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathLexerErrorListener.h @@ -0,0 +1,22 @@ +/* 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 "BaseErrorListener.h" + +namespace antlr4 { +namespace tree { +namespace xpath { + + class ANTLR4CPP_PUBLIC XPathLexerErrorListener : public BaseErrorListener { + public: + virtual void syntaxError(Recognizer *recognizer, Token *offendingSymbol, size_t line, + size_t charPositionInLine, const std::string &msg, std::exception_ptr e) override; + }; + +} // namespace xpath +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleAnywhereElement.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleAnywhereElement.cpp new file mode 100644 index 0000000..9ca910d --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleAnywhereElement.cpp @@ -0,0 +1,20 @@ +/* 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. + */ + +#include "tree/ParseTree.h" +#include "tree/Trees.h" + +#include "tree/xpath/XPathRuleAnywhereElement.h" + +using namespace antlr4::tree; +using namespace antlr4::tree::xpath; + +XPathRuleAnywhereElement::XPathRuleAnywhereElement(const std::string &ruleName, int ruleIndex) : XPathElement(ruleName) { + _ruleIndex = ruleIndex; +} + +std::vector XPathRuleAnywhereElement::evaluate(ParseTree *t) { + return Trees::findAllRuleNodes(t, _ruleIndex); +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleAnywhereElement.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleAnywhereElement.h new file mode 100644 index 0000000..2ceb75c --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleAnywhereElement.h @@ -0,0 +1,27 @@ +/* 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 "XPathElement.h" + +namespace antlr4 { +namespace tree { +namespace xpath { + + /// Either {@code ID} at start of path or {@code ...//ID} in middle of path. + class ANTLR4CPP_PUBLIC XPathRuleAnywhereElement : public XPathElement { + public: + XPathRuleAnywhereElement(const std::string &ruleName, int ruleIndex); + + virtual std::vector evaluate(ParseTree *t) override; + + protected: + int _ruleIndex = 0; + }; + +} // namespace xpath +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleElement.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleElement.cpp new file mode 100644 index 0000000..1d145fb --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleElement.cpp @@ -0,0 +1,30 @@ +/* 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. + */ + +#include "tree/ParseTree.h" +#include "tree/Trees.h" + +#include "XPathRuleElement.h" + +using namespace antlr4::tree; +using namespace antlr4::tree::xpath; + +XPathRuleElement::XPathRuleElement(const std::string &ruleName, size_t ruleIndex) : XPathElement(ruleName) { + _ruleIndex = ruleIndex; +} + +std::vector XPathRuleElement::evaluate(ParseTree *t) { + // return all children of t that match nodeName + std::vector nodes; + for (auto *c : t->children) { + if (antlrcpp::is(c)) { + ParserRuleContext *ctx = dynamic_cast(c); + if ((ctx->getRuleIndex() == _ruleIndex && !_invert) || (ctx->getRuleIndex() != _ruleIndex && _invert)) { + nodes.push_back(ctx); + } + } + } + return nodes; +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleElement.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleElement.h new file mode 100644 index 0000000..b57276f --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleElement.h @@ -0,0 +1,26 @@ +/* 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 "XPathElement.h" + +namespace antlr4 { +namespace tree { +namespace xpath { + + class ANTLR4CPP_PUBLIC XPathRuleElement : public XPathElement { + public: + XPathRuleElement(const std::string &ruleName, size_t ruleIndex); + + virtual std::vector evaluate(ParseTree *t) override; + + protected: + size_t _ruleIndex = 0; + }; + +} // namespace xpath +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenAnywhereElement.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenAnywhereElement.cpp new file mode 100644 index 0000000..c557c9d --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenAnywhereElement.cpp @@ -0,0 +1,20 @@ +/* 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. + */ + +#include "tree/ParseTree.h" +#include "tree/Trees.h" + +#include "XPathTokenAnywhereElement.h" + +using namespace antlr4::tree; +using namespace antlr4::tree::xpath; + +XPathTokenAnywhereElement::XPathTokenAnywhereElement(const std::string &tokenName, int tokenType) : XPathElement(tokenName) { + this->tokenType = tokenType; +} + +std::vector XPathTokenAnywhereElement::evaluate(ParseTree *t) { + return Trees::findAllTokenNodes(t, tokenType); +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenAnywhereElement.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenAnywhereElement.h new file mode 100644 index 0000000..2045d91 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenAnywhereElement.h @@ -0,0 +1,25 @@ +/* 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 "XPathElement.h" + +namespace antlr4 { +namespace tree { +namespace xpath { + + class ANTLR4CPP_PUBLIC XPathTokenAnywhereElement : public XPathElement { + protected: + int tokenType = 0; + public: + XPathTokenAnywhereElement(const std::string &tokenName, int tokenType); + + virtual std::vector evaluate(ParseTree *t) override; + }; + +} // namespace xpath +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenElement.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenElement.cpp new file mode 100644 index 0000000..d52fc26 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenElement.cpp @@ -0,0 +1,33 @@ +/* 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. + */ + +#include "tree/ParseTree.h" +#include "tree/Trees.h" +#include "support/CPPUtils.h" +#include "Token.h" + +#include "XPathTokenElement.h" + +using namespace antlr4; +using namespace antlr4::tree; +using namespace antlr4::tree::xpath; + +XPathTokenElement::XPathTokenElement(const std::string &tokenName, size_t tokenType) : XPathElement(tokenName) { + _tokenType = tokenType; +} + +std::vector XPathTokenElement::evaluate(ParseTree *t) { + // return all children of t that match nodeName + std::vector nodes; + for (auto *c : t->children) { + if (antlrcpp::is(c)) { + TerminalNode *tnode = dynamic_cast(c); + if ((tnode->getSymbol()->getType() == _tokenType && !_invert) || (tnode->getSymbol()->getType() != _tokenType && _invert)) { + nodes.push_back(tnode); + } + } + } + return nodes; +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenElement.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenElement.h new file mode 100644 index 0000000..7221530 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathTokenElement.h @@ -0,0 +1,26 @@ +/* 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 "XPathElement.h" + +namespace antlr4 { +namespace tree { +namespace xpath { + + class ANTLR4CPP_PUBLIC XPathTokenElement : public XPathElement { + public: + XPathTokenElement(const std::string &tokenName, size_t tokenType); + + virtual std::vector evaluate(ParseTree *t) override; + + protected: + size_t _tokenType = 0; + }; + +} // namespace xpath +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardAnywhereElement.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardAnywhereElement.cpp new file mode 100644 index 0000000..4ff424f --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardAnywhereElement.cpp @@ -0,0 +1,23 @@ +/* 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. + */ + +#include "XPath.h" +#include "tree/ParseTree.h" +#include "tree/Trees.h" + +#include "XPathWildcardAnywhereElement.h" + +using namespace antlr4::tree; +using namespace antlr4::tree::xpath; + +XPathWildcardAnywhereElement::XPathWildcardAnywhereElement() : XPathElement(XPath::WILDCARD) { +} + +std::vector XPathWildcardAnywhereElement::evaluate(ParseTree *t) { + if (_invert) { + return {}; // !* is weird but valid (empty) + } + return Trees::getDescendants(t); +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardAnywhereElement.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardAnywhereElement.h new file mode 100644 index 0000000..dc5d1e5 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardAnywhereElement.h @@ -0,0 +1,23 @@ +/* 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 "XPathElement.h" + +namespace antlr4 { +namespace tree { +namespace xpath { + + class ANTLR4CPP_PUBLIC XPathWildcardAnywhereElement : public XPathElement { + public: + XPathWildcardAnywhereElement(); + + virtual std::vector evaluate(ParseTree *t) override; + }; + +} // namespace xpath +} // namespace tree +} // namespace antlr4 diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardElement.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardElement.cpp new file mode 100644 index 0000000..aabda5a --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardElement.cpp @@ -0,0 +1,24 @@ +/* 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. + */ + +#include "XPath.h" +#include "tree/ParseTree.h" +#include "tree/Trees.h" + +#include "XPathWildcardElement.h" + +using namespace antlr4::tree; +using namespace antlr4::tree::xpath; + +XPathWildcardElement::XPathWildcardElement() : XPathElement(XPath::WILDCARD) { +} + +std::vector XPathWildcardElement::evaluate(ParseTree *t) { + if (_invert) { + return {}; // !* is weird but valid (empty) + } + + return t->children; +} diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardElement.h b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardElement.h new file mode 100644 index 0000000..accb461 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathWildcardElement.h @@ -0,0 +1,23 @@ +/* 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 "XPathElement.h" + +namespace antlr4 { +namespace tree { +namespace xpath { + + class ANTLR4CPP_PUBLIC XPathWildcardElement : public XPathElement { + public: + XPathWildcardElement(); + + virtual std::vector evaluate(ParseTree *t) override; + }; + +} // namespace xpath +} // namespace tree +} // namespace antlr4 -- cgit v1.2.3