add antlr

This commit is contained in:
2020-09-29 23:16:42 +05:30
parent 34754a7130
commit 2b36e74ff5
181 changed files with 14689 additions and 7 deletions

View File

@@ -0,0 +1,170 @@
/* 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.
*/
// A standard C++ class loosely modeled after boost::Any.
#pragma once
#include "antlr4-common.h"
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4521) // 'antlrcpp::Any': multiple copy constructors specified
#endif
namespace antlrcpp {
template<class T>
using StorageType = typename std::decay<T>::type;
struct ANTLR4CPP_PUBLIC Any
{
bool isNull() const { return _ptr == nullptr; }
bool isNotNull() const { return _ptr != nullptr; }
Any() : _ptr(nullptr) {
}
Any(Any& that) : _ptr(that.clone()) {
}
Any(Any&& that) : _ptr(that._ptr) {
that._ptr = nullptr;
}
Any(const Any& that) : _ptr(that.clone()) {
}
Any(const Any&& that) : _ptr(that.clone()) {
}
template<typename U>
Any(U&& value) : _ptr(new Derived<StorageType<U>>(std::forward<U>(value))) {
}
template<class U>
bool is() const {
auto derived = getDerived<U>(false);
return derived != nullptr;
}
template<class U>
StorageType<U>& as() {
auto derived = getDerived<U>(true);
return derived->value;
}
template<class U>
const StorageType<U>& as() const {
auto derived = getDerived<U>(true);
return derived->value;
}
template<class U>
operator U() {
return as<StorageType<U>>();
}
template<class U>
operator const U() const {
return as<const StorageType<U>>();
}
Any& operator = (const Any& a) {
if (_ptr == a._ptr)
return *this;
auto old_ptr = _ptr;
_ptr = a.clone();
if (old_ptr)
delete old_ptr;
return *this;
}
Any& operator = (Any&& a) {
if (_ptr == a._ptr)
return *this;
std::swap(_ptr, a._ptr);
return *this;
}
virtual ~Any();
virtual bool equals(Any other) const {
return _ptr == other._ptr;
}
private:
struct Base {
virtual ~Base() {};
virtual Base* clone() const = 0;
};
template<typename T>
struct Derived : Base
{
template<typename U> Derived(U&& value_) : value(std::forward<U>(value_)) {
}
T value;
Base* clone() const {
return clone<>();
}
private:
template<int N = 0, typename std::enable_if<N == N && std::is_nothrow_copy_constructible<T>::value, int>::type = 0>
Base* clone() const {
return new Derived<T>(value);
}
template<int N = 0, typename std::enable_if<N == N && !std::is_nothrow_copy_constructible<T>::value, int>::type = 0>
Base* clone() const {
return nullptr;
}
};
Base* clone() const
{
if (_ptr)
return _ptr->clone();
else
return nullptr;
}
template<class U>
Derived<StorageType<U>>* getDerived(bool checkCast) const {
typedef StorageType<U> T;
auto derived = dynamic_cast<Derived<T>*>(_ptr);
if (checkCast && !derived)
throw std::bad_cast();
return derived;
}
Base *_ptr;
};
template<> inline
Any::Any(std::nullptr_t&& ) : _ptr(nullptr) {
}
} // namespace antlrcpp
#ifdef _MSC_VER
#pragma warning(pop)
#endif

View File

@@ -0,0 +1,110 @@
/* 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 antlrcpp {
class ANTLR4CPP_PUBLIC Arrays {
public:
static std::string listToString(const std::vector<std::string> &list, const std::string &separator);
template <typename T>
static bool equals(const std::vector<T> &a, const std::vector<T> &b) {
if (a.size() != b.size())
return false;
for (size_t i = 0; i < a.size(); ++i)
if (!(a[i] == b[i]))
return false;
return true;
}
template <typename T>
static bool equals(const std::vector<T *> &a, const std::vector<T *> &b) {
if (a.size() != b.size())
return false;
for (size_t i = 0; i < a.size(); ++i) {
if (a[i] == b[i])
continue;
if (!(*a[i] == *b[i]))
return false;
}
return true;
}
template <typename T>
static bool equals(const std::vector<Ref<T>> &a, const std::vector<Ref<T>> &b) {
if (a.size() != b.size())
return false;
for (size_t i = 0; i < a.size(); ++i) {
if (!a[i] && !b[i])
continue;
if (!a[i] || !b[i])
return false;
if (a[i] == b[i])
continue;
if (!(*a[i] == *b[i]))
return false;
}
return true;
}
template <typename T>
static std::string toString(const std::vector<T> &source) {
std::string result = "[";
bool firstEntry = true;
for (auto &value : source) {
result += value.toString();
if (firstEntry) {
result += ", ";
firstEntry = false;
}
}
return result + "]";
}
template <typename T>
static std::string toString(const std::vector<Ref<T>> &source) {
std::string result = "[";
bool firstEntry = true;
for (auto &value : source) {
result += value->toString();
if (firstEntry) {
result += ", ";
firstEntry = false;
}
}
return result + "]";
}
template <typename T>
static std::string toString(const std::vector<T *> &source) {
std::string result = "[";
bool firstEntry = true;
for (auto value : source) {
result += value->toString();
if (firstEntry) {
result += ", ";
firstEntry = false;
}
}
return result + "]";
}
};
template <>
std::string Arrays::toString(const std::vector<antlr4::tree::ParseTree *> &source);
}

View File

@@ -0,0 +1,76 @@
/* 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 antlrcpp {
class ANTLR4CPP_PUBLIC BitSet : public std::bitset<2048> {
public:
size_t nextSetBit(size_t pos) const {
for (size_t i = pos; i < size(); i++){
if (test(i)) {
return i;
}
}
return INVALID_INDEX;
}
// Prints a list of every index for which the bitset contains a bit in true.
friend std::wostream& operator << (std::wostream& os, const BitSet& obj)
{
os << "{";
size_t total = obj.count();
for (size_t i = 0; i < obj.size(); i++){
if (obj.test(i)){
os << i;
--total;
if (total > 1){
os << ", ";
}
}
}
os << "}";
return os;
}
static std::string subStringRepresentation(const std::vector<BitSet>::iterator &begin,
const std::vector<BitSet>::iterator &end) {
std::string result;
std::vector<BitSet>::iterator vectorIterator;
for (vectorIterator = begin; vectorIterator != end; vectorIterator++) {
result += vectorIterator->toString();
}
// Grab the end
result += end->toString();
return result;
}
std::string toString(){
std::stringstream stream;
stream << "{";
bool valueAdded = false;
for (size_t i = 0; i < size(); ++i){
if (test(i)){
if (valueAdded) {
stream << ", ";
}
stream << i;
valueAdded = true;
}
}
stream << "}";
return stream.str();
}
};
}

View File

@@ -0,0 +1,78 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
#pragma once
#include "antlr4-common.h"
namespace antlrcpp {
std::string join(std::vector<std::string> strings, const std::string &separator);
std::map<std::string, size_t> toMap(const std::vector<std::string> &keys);
std::string escapeWhitespace(std::string str, bool escapeSpaces);
std::string toHexString(const int t);
std::string arrayToString(const std::vector<std::string> &data);
std::string replaceString(const std::string &s, const std::string &from, const std::string &to);
std::vector<std::string> split(const std::string &s, const std::string &sep, int count);
std::string indent(const std::string &s, const std::string &indentation, bool includingFirst = true);
// Using RAII + a lambda to implement a "finally" replacement.
struct FinalAction {
FinalAction(std::function<void ()> f) : _cleanUp { f } {}
FinalAction(FinalAction &&other) :
_cleanUp(std::move(other._cleanUp)), _enabled(other._enabled) {
other._enabled = false; // Don't trigger the lambda after ownership has moved.
}
~FinalAction() { if (_enabled) _cleanUp(); }
void disable() { _enabled = false; }
private:
std::function<void ()> _cleanUp;
bool _enabled {true};
};
ANTLR4CPP_PUBLIC FinalAction finally(std::function<void ()> f);
// Convenience functions to avoid lengthy dynamic_cast() != nullptr checks in many places.
template <typename T1, typename T2>
inline bool is(T2 *obj) { // For pointer types.
return dynamic_cast<typename std::add_const<T1>::type>(obj) != nullptr;
}
template <typename T1, typename T2>
inline bool is(Ref<T2> const& obj) { // For shared pointers.
return dynamic_cast<T1 *>(obj.get()) != nullptr;
}
template <typename T>
std::string toString(const T &o) {
std::stringstream ss;
// typeid gives the mangled class name, but that's all what's possible
// in a portable way.
ss << typeid(o).name() << "@" << std::hex << reinterpret_cast<uintptr_t>(&o);
return ss.str();
}
// Get the error text from an exception pointer or the current exception.
std::string what(std::exception_ptr eptr = std::current_exception());
class SingleWriteMultipleReadLock {
public:
void readLock();
void readUnlock();
void writeLock();
void writeUnlock();
private:
std::condition_variable _readerGate;
std::condition_variable _writerGate;
std::mutex _mutex;
size_t _activeReaders = 0;
size_t _waitingWriters = 0;
size_t _activeWriters = 0;
};
} // namespace antlrcpp

View File

@@ -0,0 +1,163 @@
/* 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
namespace antlr4 {
class ANTLRErrorListener;
class ANTLRErrorStrategy;
class ANTLRFileStream;
class ANTLRInputStream;
class BailErrorStrategy;
class BaseErrorListener;
class BufferedTokenStream;
class CharStream;
class CommonToken;
class CommonTokenFactory;
class CommonTokenStream;
class ConsoleErrorListener;
class DefaultErrorStrategy;
class DiagnosticErrorListener;
class EmptyStackException;
class FailedPredicateException;
class IllegalArgumentException;
class IllegalStateException;
class InputMismatchException;
class IntStream;
class InterpreterRuleContext;
class Lexer;
class LexerInterpreter;
class LexerNoViableAltException;
class ListTokenSource;
class NoSuchElementException;
class NoViableAltException;
class NullPointerException;
class ParseCancellationException;
class Parser;
class ParserInterpreter;
class ParserRuleContext;
class ProxyErrorListener;
class RecognitionException;
class Recognizer;
class RuleContext;
class Token;
template<typename Symbol> class TokenFactory;
class TokenSource;
class TokenStream;
class TokenStreamRewriter;
class UnbufferedCharStream;
class UnbufferedTokenStream;
class WritableToken;
namespace misc {
class InterpreterDataReader;
class Interval;
class IntervalSet;
class MurmurHash;
class Utils;
class Predicate;
}
namespace atn {
class ATN;
class ATNConfig;
class ATNConfigSet;
class ATNDeserializationOptions;
class ATNDeserializer;
class ATNSerializer;
class ATNSimulator;
class ATNState;
enum class ATNType;
class AbstractPredicateTransition;
class ActionTransition;
class ArrayPredictionContext;
class AtomTransition;
class BasicBlockStartState;
class BasicState;
class BlockEndState;
class BlockStartState;
class DecisionState;
class EmptyPredictionContext;
class EpsilonTransition;
class LL1Analyzer;
class LexerAction;
class LexerActionExecutor;
class LexerATNConfig;
class LexerATNSimulator;
class LexerMoreAction;
class LexerPopModeAction;
class LexerSkipAction;
class LookaheadEventInfo;
class LoopEndState;
class NotSetTransition;
class OrderedATNConfigSet;
class ParseInfo;
class ParserATNSimulator;
class PlusBlockStartState;
class PlusLoopbackState;
class PrecedencePredicateTransition;
class PredicateTransition;
class PredictionContext;
enum class PredictionMode;
class PredictionModeClass;
class RangeTransition;
class RuleStartState;
class RuleStopState;
class RuleTransition;
class SemanticContext;
class SetTransition;
class SingletonPredictionContext;
class StarBlockStartState;
class StarLoopEntryState;
class StarLoopbackState;
class TokensStartState;
class Transition;
class WildcardTransition;
}
namespace dfa {
class DFA;
class DFASerializer;
class DFAState;
class LexerDFASerializer;
class Vocabulary;
}
namespace tree {
class AbstractParseTreeVisitor;
class ErrorNode;
class ErrorNodeImpl;
class ParseTree;
class ParseTreeListener;
template<typename T> class ParseTreeProperty;
class ParseTreeVisitor;
class ParseTreeWalker;
class SyntaxTree;
class TerminalNode;
class TerminalNodeImpl;
class Tree;
class Trees;
namespace pattern {
class Chunk;
class ParseTreeMatch;
class ParseTreePattern;
class ParseTreePatternMatcher;
class RuleTagToken;
class TagChunk;
class TextChunk;
class TokenTagToken;
}
namespace xpath {
class XPath;
class XPathElement;
class XPathLexerErrorListener;
class XPathRuleAnywhereElement;
class XPathRuleElement;
class XPathTokenAnywhereElement;
class XPathTokenElement;
class XPathWildcardAnywhereElement;
class XPathWildcardElement;
}
}
}

View File

@@ -0,0 +1,54 @@
/* 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 antlrcpp {
// For all conversions utf8 <-> utf32.
// VS 2015 and VS 2017 have different bugs in std::codecvt_utf8<char32_t> (VS 2013 works fine).
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
typedef std::wstring_convert<std::codecvt_utf8<__int32>, __int32> UTF32Converter;
#else
typedef std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> UTF32Converter;
#endif
// The conversion functions fails in VS2017, so we explicitly use a workaround.
template<typename T>
inline std::string utf32_to_utf8(T const& data)
{
// Don't make the converter static or we have to serialize access to it.
thread_local UTF32Converter converter;
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
auto p = reinterpret_cast<const int32_t *>(data.data());
return converter.to_bytes(p, p + data.size());
#else
return converter.to_bytes(data);
#endif
}
inline UTF32String utf8_to_utf32(const char* first, const char* last)
{
thread_local UTF32Converter converter;
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
auto r = converter.from_bytes(first, last);
i32string s = reinterpret_cast<const int32_t *>(r.data());
#else
std::u32string s = converter.from_bytes(first, last);
#endif
return s;
}
void replaceAll(std::string &str, std::string const& from, std::string const& to);
// string <-> wstring conversion (UTF-16), e.g. for use with Window's wide APIs.
ANTLR4CPP_PUBLIC std::string ws2s(std::wstring const& wstr);
ANTLR4CPP_PUBLIC std::wstring s2ws(std::string const& str);
}

View File

@@ -0,0 +1,112 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Graeme Hill (http://graemehill.ca)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
#include <iomanip>
#include <stdint.h>
#ifdef GUID_ANDROID
#include <jni.h>
#endif
// Class to represent a GUID/UUID. Each instance acts as a wrapper around a
// 16 byte value that can be passed around by value. It also supports
// conversion to string (via the stream operator <<) and conversion from a
// string via constructor.
class Guid
{
public:
// create a guid from vector of bytes
Guid(const std::vector<unsigned char> &bytes);
// create a guid from array of bytes
Guid(const unsigned char *bytes);
// Create a guid from array of words.
Guid(const uint16_t *bytes, bool reverse);
// create a guid from string
Guid(const std::string &fromString);
// create empty guid
Guid();
// copy constructor
Guid(const Guid &other);
// overload assignment operator
Guid &operator=(const Guid &other);
// overload equality and inequality operator
bool operator==(const Guid &other) const;
bool operator!=(const Guid &other) const;
const std::string toString() const;
std::vector<unsigned char>::const_iterator begin() { return _bytes.begin(); }
std::vector<unsigned char>::const_iterator end() { return _bytes.end(); }
std::vector<unsigned char>::const_reverse_iterator rbegin() { return _bytes.rbegin(); }
std::vector<unsigned char>::const_reverse_iterator rend() { return _bytes.rend(); }
private:
// actual data
std::vector<unsigned char> _bytes;
// make the << operator a friend so it can access _bytes
friend std::ostream &operator<<(std::ostream &s, const Guid &guid);
};
// Class that can create new guids. The only reason this exists instead of
// just a global "newGuid" function is because some platforms will require
// that there is some attached context. In the case of android, we need to
// know what JNIEnv is being used to call back to Java, but the newGuid()
// function would no longer be cross-platform if we parameterized the android
// version. Instead, construction of the GuidGenerator may be different on
// each platform, but the use of newGuid is uniform.
class GuidGenerator
{
public:
#ifdef GUID_ANDROID
GuidGenerator(JNIEnv *env);
#else
GuidGenerator() { }
#endif
Guid newGuid();
#ifdef GUID_ANDROID
private:
JNIEnv *_env;
jclass _uuidClass;
jmethodID _newGuidMethod;
jmethodID _mostSignificantBitsMethod;
jmethodID _leastSignificantBitsMethod;
#endif
};