add antlr
This commit is contained in:
86
lib/antlr4/include/tree/xpath/XPath.h
Normal file
86
lib/antlr4/include/tree/xpath/XPath.h
Normal 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
|
40
lib/antlr4/include/tree/xpath/XPathElement.h
Normal file
40
lib/antlr4/include/tree/xpath/XPathElement.h
Normal 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
|
56
lib/antlr4/include/tree/xpath/XPathLexer.h
Normal file
56
lib/antlr4/include/tree/xpath/XPathLexer.h
Normal file
@@ -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<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 std::vector<std::string>& getTokenNames() const override; // deprecated, use vocabulary instead
|
||||
virtual antlr4::dfa::Vocabulary& getVocabulary() const override;
|
||||
|
||||
virtual const std::vector<uint16_t> 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<antlr4::dfa::DFA> _decisionToDFA;
|
||||
static antlr4::atn::PredictionContextCache _sharedContextCache;
|
||||
static std::vector<std::string> _ruleNames;
|
||||
static std::vector<std::string> _tokenNames;
|
||||
static std::vector<std::string> _channelNames;
|
||||
static std::vector<std::string> _modeNames;
|
||||
|
||||
static std::vector<std::string> _literalNames;
|
||||
static std::vector<std::string> _symbolicNames;
|
||||
static antlr4::dfa::Vocabulary _vocabulary;
|
||||
static antlr4::atn::ATN _atn;
|
||||
static std::vector<uint16_t> _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;
|
||||
};
|
||||
|
22
lib/antlr4/include/tree/xpath/XPathLexerErrorListener.h
Normal file
22
lib/antlr4/include/tree/xpath/XPathLexerErrorListener.h
Normal 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
|
27
lib/antlr4/include/tree/xpath/XPathRuleAnywhereElement.h
Normal file
27
lib/antlr4/include/tree/xpath/XPathRuleAnywhereElement.h
Normal 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
|
26
lib/antlr4/include/tree/xpath/XPathRuleElement.h
Normal file
26
lib/antlr4/include/tree/xpath/XPathRuleElement.h
Normal 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
|
25
lib/antlr4/include/tree/xpath/XPathTokenAnywhereElement.h
Normal file
25
lib/antlr4/include/tree/xpath/XPathTokenAnywhereElement.h
Normal 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
|
26
lib/antlr4/include/tree/xpath/XPathTokenElement.h
Normal file
26
lib/antlr4/include/tree/xpath/XPathTokenElement.h
Normal 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
lib/antlr4/include/tree/xpath/XPathWildcardAnywhereElement.h
Normal file
23
lib/antlr4/include/tree/xpath/XPathWildcardAnywhereElement.h
Normal 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
|
23
lib/antlr4/include/tree/xpath/XPathWildcardElement.h
Normal file
23
lib/antlr4/include/tree/xpath/XPathWildcardElement.h
Normal 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