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/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 +++ 22 files changed, 951 insertions(+) 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/xpath') 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, 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 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