Initial commit from sysy-main

This commit is contained in:
Lixuanwang
2025-02-27 23:14:53 +08:00
commit cc523fd30b
1125 changed files with 257793 additions and 0 deletions

View 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;
}

View 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

View 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;
}

View 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

View File

@@ -0,0 +1,182 @@
// Generated from XPathLexer.g4 by ANTLR 4.9.3
#include "XPathLexer.h"
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 xpathLexerOnceFlag;
XPathLexerStaticData *xpathLexerStaticData = nullptr;
void xpathLexerInitialize() {
assert(xpathLexerStaticData == nullptr);
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 serializedAT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26, 0xc, 0x1, 0x0, 0x0, 0x0, 0x27, 0x28, 0x7, 0x1,
0x0, 0x0, 0x28, 0xe, 0x1, 0x0, 0x0, 0x0, 0x29, 0x2d, 0x5, 0x27, 0x0,
0x0, 0x2a, 0x2c, 0x9, 0x0, 0x0, 0x0, 0x2b, 0x2a, 0x1, 0x0, 0x0, 0x0,
0x2c, 0x2f, 0x1, 0x0, 0x0, 0x0, 0x2d, 0x2e, 0x1, 0x0, 0x0, 0x0, 0x2d,
0x2b, 0x1, 0x0, 0x0, 0x0, 0x2e, 0x30, 0x1, 0x0, 0x0, 0x0, 0x2f, 0x2d,
0x1, 0x0, 0x0, 0x0, 0x30, 0x31, 0x5, 0x27, 0x0, 0x0, 0x31, 0x10,
0x1, 0x0, 0x0, 0x0, 0x4, 0x0, 0x1e, 0x25, 0x2d, 0x1, 0x1, 0x4, 0x0,
};
staticData->serializedATN = antlr4::atn::SerializedATNView(serializedATNSegment, sizeof(serializedATNSegment) / sizeof(serializedATNSegment[0]));
atn::ATNDeserializer deserializer;
staticData->atn = deserializer.deserialize(staticData->serializedATN);
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);
}
xpathLexerStaticData = staticData.release();
}
}
XPathLexer::XPathLexer(CharStream *input) : Lexer(input) {
XPathLexer::initialize();
_interpreter = new atn::LexerATNSimulator(this, *xpathLexerStaticData->atn, xpathLexerStaticData->decisionToDFA, xpathLexerStaticData->sharedContextCache);
}
XPathLexer::~XPathLexer() {
delete _interpreter;
}
std::string XPathLexer::getGrammarFileName() const {
return "XPathLexer.g4";
}
const std::vector<std::string>& XPathLexer::getRuleNames() const {
return xpathLexerStaticData->ruleNames;
}
const std::vector<std::string>& XPathLexer::getChannelNames() const {
return xpathLexerStaticData->channelNames;
}
const std::vector<std::string>& XPathLexer::getModeNames() const {
return xpathLexerStaticData->modeNames;
}
const dfa::Vocabulary& XPathLexer::getVocabulary() const {
return xpathLexerStaticData->vocabulary;
}
antlr4::atn::SerializedATNView XPathLexer::getSerializedATN() const {
return xpathLexerStaticData->serializedATN;
}
const atn::ATN& XPathLexer::getATN() const {
return *xpathLexerStaticData->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() {
::antlr4::internal::call_once(xpathLexerOnceFlag, xpathLexerInitialize);
}

View 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 ;

View File

@@ -0,0 +1,47 @@
// Generated from XPathLexer.g4 by ANTLR 4.9.3
#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;
virtual std::string getGrammarFileName() const override;
virtual const std::vector<std::string>& getRuleNames() const override;
virtual const std::vector<std::string>& getChannelNames() const override;
virtual const std::vector<std::string>& getModeNames() const override;
virtual const antlr4::dfa::Vocabulary& getVocabulary() const override;
virtual antlr4::atn::SerializedATNView getSerializedATN() const override;
virtual const antlr4::atn::ATN& getATN() const override;
virtual 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.
};

View File

@@ -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

View 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*/) {
}

View 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

View 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);
}

View 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

View 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;
}

View 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

View 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);
}

View 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

View 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;
}

View 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

View 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);
}

View 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

View 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;
}

View 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