build(antlr): 引入第三方 ANTLR4 runtime/tool 并接入构建
This commit is contained in:
154
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPath.cpp
vendored
Executable file
154
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPath.cpp
vendored
Executable file
@@ -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<std::unique_ptr<XPathElement>> 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<Token *> tokens = tokenStream.getTokens();
|
||||
std::vector<std::unique_ptr<XPathElement>> 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<XPathElement> 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<XPathElement> 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<XPathWildcardAnywhereElement>(new XPathWildcardAnywhereElement());
|
||||
return std::unique_ptr<XPathWildcardElement>(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<XPathTokenAnywhereElement>(new XPathTokenAnywhereElement(word, (int)ttype));
|
||||
return std::unique_ptr<XPathTokenElement>(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<XPathRuleAnywhereElement>(new XPathRuleAnywhereElement(word, (int)ruleIndex));
|
||||
return std::unique_ptr<XPathRuleElement>(new XPathRuleElement(word, (int)ruleIndex));
|
||||
}
|
||||
}
|
||||
|
||||
static ParserRuleContext dummyRoot;
|
||||
|
||||
std::vector<ParseTree *> XPath::findAll(ParseTree *tree, std::string const& xpath, Parser *parser) {
|
||||
XPath p(parser, xpath);
|
||||
return p.evaluate(tree);
|
||||
}
|
||||
|
||||
std::vector<ParseTree *> XPath::evaluate(ParseTree *t) {
|
||||
dummyRoot.children = { t }; // don't set t's parent.
|
||||
|
||||
std::vector<ParseTree *> work = { &dummyRoot };
|
||||
|
||||
size_t i = 0;
|
||||
std::vector<std::unique_ptr<XPathElement>> elements = split(_path);
|
||||
|
||||
while (i < elements.size()) {
|
||||
std::vector<ParseTree *> 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;
|
||||
}
|
||||
86
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPath.h
vendored
Executable file
86
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPath.h
vendored
Executable file
@@ -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.
|
||||
///
|
||||
/// <para>
|
||||
/// 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.</para>
|
||||
///
|
||||
/// <para>
|
||||
/// The basic interface is
|
||||
/// <seealso cref="XPath#findAll ParseTree.findAll"/>{@code (tree, pathString, parser)}.
|
||||
/// But that is just shorthand for:</para>
|
||||
///
|
||||
/// <pre>
|
||||
/// <seealso cref="XPath"/> p = new <seealso cref="XPath#XPath XPath"/>(parser, pathString);
|
||||
/// return p.<seealso cref="#evaluate evaluate"/>(tree);
|
||||
/// </pre>
|
||||
///
|
||||
/// <para>
|
||||
/// See {@code org.antlr.v4.test.TestXPath} for descriptions. In short, this
|
||||
/// allows operators:</para>
|
||||
///
|
||||
/// <dl>
|
||||
/// <dt>/</dt> <dd>root</dd>
|
||||
/// <dt>//</dt> <dd>anywhere</dd>
|
||||
/// <dt>!</dt> <dd>invert; this must appear directly after root or anywhere
|
||||
/// operator</dd>
|
||||
/// </dl>
|
||||
///
|
||||
/// <para>
|
||||
/// and path elements:</para>
|
||||
///
|
||||
/// <dl>
|
||||
/// <dt>ID</dt> <dd>token name</dd>
|
||||
/// <dt>'string'</dt> <dd>any string literal token from the grammar</dd>
|
||||
/// <dt>expr</dt> <dd>rule name</dd>
|
||||
/// <dt>*</dt> <dd>wildcard matching any node</dd>
|
||||
/// </dl>
|
||||
///
|
||||
/// <para>
|
||||
/// Whitespace is not allowed.</para>
|
||||
|
||||
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<std::unique_ptr<XPathElement>> split(const std::string &path);
|
||||
|
||||
static std::vector<ParseTree *> 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
|
||||
/// <seealso cref="#evaluate"/>.
|
||||
virtual std::vector<ParseTree *> 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<XPathElement> getXPathElement(Token *wordToken, bool anywhere);
|
||||
};
|
||||
|
||||
} // namespace xpath
|
||||
} // namespace tree
|
||||
} // namespace antlr4
|
||||
31
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathElement.cpp
vendored
Executable file
31
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathElement.cpp
vendored
Executable file
@@ -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<ParseTree *> 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;
|
||||
}
|
||||
40
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathElement.h
vendored
Executable file
40
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathElement.h
vendored
Executable file
@@ -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<ParseTree *> 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
|
||||
180
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathLexer.cpp
vendored
Normal file
180
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathLexer.cpp
vendored
Normal file
@@ -0,0 +1,180 @@
|
||||
|
||||
// Generated from XPathLexer.g4 by ANTLR 4.13.1
|
||||
|
||||
|
||||
#include "XPathLexer.h"
|
||||
|
||||
|
||||
using namespace antlr4;
|
||||
|
||||
|
||||
|
||||
using namespace antlr4;
|
||||
|
||||
namespace {
|
||||
|
||||
struct XPathLexerStaticData final {
|
||||
XPathLexerStaticData(std::vector<std::string> ruleNames,
|
||||
std::vector<std::string> channelNames,
|
||||
std::vector<std::string> modeNames,
|
||||
std::vector<std::string> literalNames,
|
||||
std::vector<std::string> symbolicNames)
|
||||
: ruleNames(std::move(ruleNames)), channelNames(std::move(channelNames)),
|
||||
modeNames(std::move(modeNames)), literalNames(std::move(literalNames)),
|
||||
symbolicNames(std::move(symbolicNames)),
|
||||
vocabulary(this->literalNames, this->symbolicNames) {}
|
||||
|
||||
XPathLexerStaticData(const XPathLexerStaticData&) = delete;
|
||||
XPathLexerStaticData(XPathLexerStaticData&&) = delete;
|
||||
XPathLexerStaticData& operator=(const XPathLexerStaticData&) = delete;
|
||||
XPathLexerStaticData& operator=(XPathLexerStaticData&&) = delete;
|
||||
|
||||
std::vector<antlr4::dfa::DFA> decisionToDFA;
|
||||
antlr4::atn::PredictionContextCache sharedContextCache;
|
||||
const std::vector<std::string> ruleNames;
|
||||
const std::vector<std::string> channelNames;
|
||||
const std::vector<std::string> modeNames;
|
||||
const std::vector<std::string> literalNames;
|
||||
const std::vector<std::string> symbolicNames;
|
||||
const antlr4::dfa::Vocabulary vocabulary;
|
||||
antlr4::atn::SerializedATNView serializedATN;
|
||||
std::unique_ptr<antlr4::atn::ATN> atn;
|
||||
};
|
||||
|
||||
::antlr4::internal::OnceFlag xpathlexerLexerOnceFlag;
|
||||
#if ANTLR4_USE_THREAD_LOCAL_CACHE
|
||||
static thread_local
|
||||
#endif
|
||||
std::unique_ptr<XPathLexerStaticData> xpathlexerLexerStaticData = nullptr;
|
||||
|
||||
void xpathlexerLexerInitialize() {
|
||||
#if ANTLR4_USE_THREAD_LOCAL_CACHE
|
||||
if (xpathlexerLexerStaticData != nullptr) {
|
||||
return;
|
||||
}
|
||||
#else
|
||||
assert(xpathlexerLexerStaticData == nullptr);
|
||||
#endif
|
||||
auto staticData = std::make_unique<XPathLexerStaticData>(
|
||||
std::vector<std::string>{
|
||||
"ANYWHERE", "ROOT", "WILDCARD", "BANG", "ID", "NameChar", "NameStartChar",
|
||||
"STRING"
|
||||
},
|
||||
std::vector<std::string>{
|
||||
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
|
||||
},
|
||||
std::vector<std::string>{
|
||||
"DEFAULT_MODE"
|
||||
},
|
||||
std::vector<std::string>{
|
||||
"", "", "", "'//'", "'/'", "'*'", "'!'"
|
||||
},
|
||||
std::vector<std::string>{
|
||||
"", "TOKEN_REF", "RULE_REF", "ANYWHERE", "ROOT", "WILDCARD", "BANG",
|
||||
"ID", "STRING"
|
||||
}
|
||||
);
|
||||
static const int32_t serializedATNSegment[] = {
|
||||
4,0,8,50,6,-1,2,0,7,0,2,1,7,1,2,2,7,2,2,3,7,3,2,4,7,4,2,5,7,5,2,6,7,6,
|
||||
2,7,7,7,1,0,1,0,1,0,1,1,1,1,1,2,1,2,1,3,1,3,1,4,1,4,5,4,29,8,4,10,4,12,
|
||||
4,32,9,4,1,4,1,4,1,5,1,5,3,5,38,8,5,1,6,1,6,1,7,1,7,5,7,44,8,7,10,7,12,
|
||||
7,47,9,7,1,7,1,7,1,45,0,8,1,3,3,4,5,5,7,6,9,7,11,0,13,0,15,8,1,0,2,5,
|
||||
0,48,57,95,95,183,183,768,879,8255,8256,13,0,65,90,97,122,192,214,216,
|
||||
246,248,767,880,893,895,8191,8204,8205,8304,8591,11264,12271,12289,55295,
|
||||
63744,64975,65008,65535,50,0,1,1,0,0,0,0,3,1,0,0,0,0,5,1,0,0,0,0,7,1,
|
||||
0,0,0,0,9,1,0,0,0,0,15,1,0,0,0,1,17,1,0,0,0,3,20,1,0,0,0,5,22,1,0,0,0,
|
||||
7,24,1,0,0,0,9,26,1,0,0,0,11,37,1,0,0,0,13,39,1,0,0,0,15,41,1,0,0,0,17,
|
||||
18,5,47,0,0,18,19,5,47,0,0,19,2,1,0,0,0,20,21,5,47,0,0,21,4,1,0,0,0,22,
|
||||
23,5,42,0,0,23,6,1,0,0,0,24,25,5,33,0,0,25,8,1,0,0,0,26,30,3,13,6,0,27,
|
||||
29,3,11,5,0,28,27,1,0,0,0,29,32,1,0,0,0,30,28,1,0,0,0,30,31,1,0,0,0,31,
|
||||
33,1,0,0,0,32,30,1,0,0,0,33,34,6,4,0,0,34,10,1,0,0,0,35,38,3,13,6,0,36,
|
||||
38,7,0,0,0,37,35,1,0,0,0,37,36,1,0,0,0,38,12,1,0,0,0,39,40,7,1,0,0,40,
|
||||
14,1,0,0,0,41,45,5,39,0,0,42,44,9,0,0,0,43,42,1,0,0,0,44,47,1,0,0,0,45,
|
||||
46,1,0,0,0,45,43,1,0,0,0,46,48,1,0,0,0,47,45,1,0,0,0,48,49,5,39,0,0,49,
|
||||
16,1,0,0,0,4,0,30,37,45,1,1,4,0
|
||||
};
|
||||
staticData->serializedATN = antlr4::atn::SerializedATNView(serializedATNSegment, sizeof(serializedATNSegment) / sizeof(serializedATNSegment[0]));
|
||||
|
||||
antlr4::atn::ATNDeserializer deserializer;
|
||||
staticData->atn = deserializer.deserialize(staticData->serializedATN);
|
||||
|
||||
const size_t count = staticData->atn->getNumberOfDecisions();
|
||||
staticData->decisionToDFA.reserve(count);
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
staticData->decisionToDFA.emplace_back(staticData->atn->getDecisionState(i), i);
|
||||
}
|
||||
xpathlexerLexerStaticData = std::move(staticData);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
XPathLexer::XPathLexer(CharStream *input) : Lexer(input) {
|
||||
XPathLexer::initialize();
|
||||
_interpreter = new atn::LexerATNSimulator(this, *xpathlexerLexerStaticData->atn, xpathlexerLexerStaticData->decisionToDFA, xpathlexerLexerStaticData->sharedContextCache);
|
||||
}
|
||||
|
||||
XPathLexer::~XPathLexer() {
|
||||
delete _interpreter;
|
||||
}
|
||||
|
||||
std::string XPathLexer::getGrammarFileName() const {
|
||||
return "XPathLexer.g4";
|
||||
}
|
||||
|
||||
const std::vector<std::string>& XPathLexer::getRuleNames() const {
|
||||
return xpathlexerLexerStaticData->ruleNames;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& XPathLexer::getChannelNames() const {
|
||||
return xpathlexerLexerStaticData->channelNames;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& XPathLexer::getModeNames() const {
|
||||
return xpathlexerLexerStaticData->modeNames;
|
||||
}
|
||||
|
||||
const dfa::Vocabulary& XPathLexer::getVocabulary() const {
|
||||
return xpathlexerLexerStaticData->vocabulary;
|
||||
}
|
||||
|
||||
antlr4::atn::SerializedATNView XPathLexer::getSerializedATN() const {
|
||||
return xpathlexerLexerStaticData->serializedATN;
|
||||
}
|
||||
|
||||
const atn::ATN& XPathLexer::getATN() const {
|
||||
return *xpathlexerLexerStaticData->atn;
|
||||
}
|
||||
|
||||
|
||||
void XPathLexer::action(RuleContext *context, size_t ruleIndex, size_t actionIndex) {
|
||||
switch (ruleIndex) {
|
||||
case 4: IDAction(antlrcpp::downCast<antlr4::RuleContext *>(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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void XPathLexer::initialize() {
|
||||
#if ANTLR4_USE_THREAD_LOCAL_CACHE
|
||||
xpathlexerLexerInitialize();
|
||||
#else
|
||||
::antlr4::internal::call_once(xpathlexerLexerOnceFlag, xpathlexerLexerInitialize);
|
||||
#endif
|
||||
}
|
||||
64
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathLexer.g4
vendored
Normal file
64
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathLexer.g4
vendored
Normal file
@@ -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 ;
|
||||
|
||||
53
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathLexer.h
vendored
Normal file
53
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathLexer.h
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
|
||||
// Generated from XPathLexer.g4 by ANTLR 4.13.1
|
||||
|
||||
#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
|
||||
};
|
||||
|
||||
explicit XPathLexer(antlr4::CharStream *input);
|
||||
|
||||
~XPathLexer() override;
|
||||
|
||||
|
||||
std::string getGrammarFileName() const override;
|
||||
|
||||
const std::vector<std::string>& getRuleNames() const override;
|
||||
|
||||
const std::vector<std::string>& getChannelNames() const override;
|
||||
|
||||
const std::vector<std::string>& getModeNames() const override;
|
||||
|
||||
const antlr4::dfa::Vocabulary& getVocabulary() const override;
|
||||
|
||||
antlr4::atn::SerializedATNView getSerializedATN() const override;
|
||||
|
||||
const antlr4::atn::ATN& getATN() const override;
|
||||
|
||||
void action(antlr4::RuleContext *context, size_t ruleIndex, size_t actionIndex) override;
|
||||
|
||||
// By default the static state used to implement the lexer is lazily initialized during the first
|
||||
// call to the constructor. You can call this function if you wish to initialize the static state
|
||||
// ahead of time.
|
||||
static void initialize();
|
||||
|
||||
private:
|
||||
|
||||
// Individual action functions triggered by action() above.
|
||||
void IDAction(antlr4::RuleContext *context, size_t actionIndex);
|
||||
|
||||
// Individual semantic predicate functions triggered by sempred() above.
|
||||
|
||||
};
|
||||
|
||||
13
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathLexerErrorListener.cpp
vendored
Executable file
13
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathLexerErrorListener.cpp
vendored
Executable file
@@ -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*/) {
|
||||
}
|
||||
22
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathLexerErrorListener.h
vendored
Executable file
22
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathLexerErrorListener.h
vendored
Executable file
@@ -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
|
||||
20
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathRuleAnywhereElement.cpp
vendored
Executable file
20
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathRuleAnywhereElement.cpp
vendored
Executable file
@@ -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<ParseTree *> XPathRuleAnywhereElement::evaluate(ParseTree *t) {
|
||||
return Trees::findAllRuleNodes(t, _ruleIndex);
|
||||
}
|
||||
27
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathRuleAnywhereElement.h
vendored
Executable file
27
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathRuleAnywhereElement.h
vendored
Executable file
@@ -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<ParseTree *> evaluate(ParseTree *t) override;
|
||||
|
||||
protected:
|
||||
int _ruleIndex = 0;
|
||||
};
|
||||
|
||||
} // namespace xpath
|
||||
} // namespace tree
|
||||
} // namespace antlr4
|
||||
30
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathRuleElement.cpp
vendored
Executable file
30
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathRuleElement.cpp
vendored
Executable file
@@ -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<ParseTree *> XPathRuleElement::evaluate(ParseTree *t) {
|
||||
// return all children of t that match nodeName
|
||||
std::vector<ParseTree *> nodes;
|
||||
for (auto *c : t->children) {
|
||||
if (antlrcpp::is<ParserRuleContext *>(c)) {
|
||||
ParserRuleContext *ctx = dynamic_cast<ParserRuleContext *>(c);
|
||||
if ((ctx->getRuleIndex() == _ruleIndex && !_invert) || (ctx->getRuleIndex() != _ruleIndex && _invert)) {
|
||||
nodes.push_back(ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
26
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathRuleElement.h
vendored
Executable file
26
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathRuleElement.h
vendored
Executable file
@@ -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<ParseTree *> evaluate(ParseTree *t) override;
|
||||
|
||||
protected:
|
||||
size_t _ruleIndex = 0;
|
||||
};
|
||||
|
||||
} // namespace xpath
|
||||
} // namespace tree
|
||||
} // namespace antlr4
|
||||
20
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathTokenAnywhereElement.cpp
vendored
Executable file
20
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathTokenAnywhereElement.cpp
vendored
Executable file
@@ -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<ParseTree *> XPathTokenAnywhereElement::evaluate(ParseTree *t) {
|
||||
return Trees::findAllTokenNodes(t, tokenType);
|
||||
}
|
||||
25
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathTokenAnywhereElement.h
vendored
Executable file
25
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathTokenAnywhereElement.h
vendored
Executable file
@@ -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<ParseTree *> evaluate(ParseTree *t) override;
|
||||
};
|
||||
|
||||
} // namespace xpath
|
||||
} // namespace tree
|
||||
} // namespace antlr4
|
||||
33
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathTokenElement.cpp
vendored
Executable file
33
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathTokenElement.cpp
vendored
Executable file
@@ -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<ParseTree *> XPathTokenElement::evaluate(ParseTree *t) {
|
||||
// return all children of t that match nodeName
|
||||
std::vector<ParseTree *> nodes;
|
||||
for (auto *c : t->children) {
|
||||
if (antlrcpp::is<TerminalNode *>(c)) {
|
||||
TerminalNode *tnode = dynamic_cast<TerminalNode *>(c);
|
||||
if ((tnode->getSymbol()->getType() == _tokenType && !_invert) || (tnode->getSymbol()->getType() != _tokenType && _invert)) {
|
||||
nodes.push_back(tnode);
|
||||
}
|
||||
}
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
26
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathTokenElement.h
vendored
Executable file
26
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathTokenElement.h
vendored
Executable file
@@ -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<ParseTree *> evaluate(ParseTree *t) override;
|
||||
|
||||
protected:
|
||||
size_t _tokenType = 0;
|
||||
};
|
||||
|
||||
} // namespace xpath
|
||||
} // namespace tree
|
||||
} // namespace antlr4
|
||||
23
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathWildcardAnywhereElement.cpp
vendored
Executable file
23
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathWildcardAnywhereElement.cpp
vendored
Executable file
@@ -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<ParseTree *> XPathWildcardAnywhereElement::evaluate(ParseTree *t) {
|
||||
if (_invert) {
|
||||
return {}; // !* is weird but valid (empty)
|
||||
}
|
||||
return Trees::getDescendants(t);
|
||||
}
|
||||
23
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathWildcardAnywhereElement.h
vendored
Executable file
23
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathWildcardAnywhereElement.h
vendored
Executable file
@@ -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<ParseTree *> evaluate(ParseTree *t) override;
|
||||
};
|
||||
|
||||
} // namespace xpath
|
||||
} // namespace tree
|
||||
} // namespace antlr4
|
||||
24
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathWildcardElement.cpp
vendored
Executable file
24
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathWildcardElement.cpp
vendored
Executable file
@@ -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<ParseTree *> XPathWildcardElement::evaluate(ParseTree *t) {
|
||||
if (_invert) {
|
||||
return {}; // !* is weird but valid (empty)
|
||||
}
|
||||
|
||||
return t->children;
|
||||
}
|
||||
23
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathWildcardElement.h
vendored
Executable file
23
third_party/antlr4-runtime-4.13.2/runtime/src/tree/xpath/XPathWildcardElement.h
vendored
Executable file
@@ -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<ParseTree *> evaluate(ParseTree *t) override;
|
||||
};
|
||||
|
||||
} // namespace xpath
|
||||
} // namespace tree
|
||||
} // namespace antlr4
|
||||
Reference in New Issue
Block a user