add antlr
This commit is contained in:
31
lib/antlr4/include/misc/InterpreterDataReader.h
Normal file
31
lib/antlr4/include/misc/InterpreterDataReader.h
Normal 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "antlr4-common.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace misc {
|
||||
|
||||
struct InterpreterData {
|
||||
atn::ATN atn;
|
||||
dfa::Vocabulary vocabulary;
|
||||
std::vector<std::string> ruleNames;
|
||||
std::vector<std::string> channels; // Only valid for lexer grammars.
|
||||
std::vector<std::string> modes; // ditto
|
||||
|
||||
InterpreterData() {}; // For invalid content.
|
||||
InterpreterData(std::vector<std::string> const& literalNames, std::vector<std::string> const& symbolicNames);
|
||||
};
|
||||
|
||||
// A class to read plain text interpreter data produced by ANTLR.
|
||||
class ANTLR4CPP_PUBLIC InterpreterDataReader {
|
||||
public:
|
||||
static InterpreterData parseFile(std::string const& fileName);
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
84
lib/antlr4/include/misc/Interval.h
Normal file
84
lib/antlr4/include/misc/Interval.h
Normal file
@@ -0,0 +1,84 @@
|
||||
/* 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 misc {
|
||||
|
||||
// Helpers to convert certain unsigned symbols (e.g. Token::EOF) to their original numeric value (e.g. -1)
|
||||
// and vice versa. This is needed mostly for intervals to keep their original order and for toString()
|
||||
// methods to print the original numeric value (e.g. for tests).
|
||||
size_t numericToSymbol(ssize_t v);
|
||||
ssize_t symbolToNumeric(size_t v);
|
||||
|
||||
/// An immutable inclusive interval a..b
|
||||
class ANTLR4CPP_PUBLIC Interval {
|
||||
public:
|
||||
static const Interval INVALID;
|
||||
|
||||
// Must stay signed to guarantee the correct sort order.
|
||||
ssize_t a;
|
||||
ssize_t b;
|
||||
|
||||
Interval();
|
||||
explicit Interval(size_t a_, size_t b_); // For unsigned -> signed mappings.
|
||||
Interval(ssize_t a_, ssize_t b_);
|
||||
|
||||
/// return number of elements between a and b inclusively. x..x is length 1.
|
||||
/// if b < a, then length is 0. 9..10 has length 2.
|
||||
size_t length() const;
|
||||
|
||||
bool operator == (const Interval &other) const;
|
||||
|
||||
size_t hashCode() const;
|
||||
|
||||
/// <summary>
|
||||
/// Does this start completely before other? Disjoint </summary>
|
||||
bool startsBeforeDisjoint(const Interval &other) const;
|
||||
|
||||
/// <summary>
|
||||
/// Does this start at or before other? Nondisjoint </summary>
|
||||
bool startsBeforeNonDisjoint(const Interval &other) const;
|
||||
|
||||
/// <summary>
|
||||
/// Does this.a start after other.b? May or may not be disjoint </summary>
|
||||
bool startsAfter(const Interval &other) const;
|
||||
|
||||
/// <summary>
|
||||
/// Does this start completely after other? Disjoint </summary>
|
||||
bool startsAfterDisjoint(const Interval &other) const;
|
||||
|
||||
/// <summary>
|
||||
/// Does this start after other? NonDisjoint </summary>
|
||||
bool startsAfterNonDisjoint(const Interval &other) const;
|
||||
|
||||
/// <summary>
|
||||
/// Are both ranges disjoint? I.e., no overlap? </summary>
|
||||
bool disjoint(const Interval &other) const;
|
||||
|
||||
/// <summary>
|
||||
/// Are two intervals adjacent such as 0..41 and 42..42? </summary>
|
||||
bool adjacent(const Interval &other) const;
|
||||
|
||||
bool properlyContains(const Interval &other) const;
|
||||
|
||||
/// <summary>
|
||||
/// Return the interval computed from combining this and other </summary>
|
||||
Interval Union(const Interval &other) const;
|
||||
|
||||
/// <summary>
|
||||
/// Return the interval in common between this and o </summary>
|
||||
Interval intersection(const Interval &other) const;
|
||||
|
||||
std::string toString() const;
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
198
lib/antlr4/include/misc/IntervalSet.h
Normal file
198
lib/antlr4/include/misc/IntervalSet.h
Normal file
@@ -0,0 +1,198 @@
|
||||
/* 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 "misc/Interval.h"
|
||||
#include "Exceptions.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace misc {
|
||||
|
||||
/**
|
||||
* This class implements the {@link IntSet} backed by a sorted array of
|
||||
* non-overlapping intervals. It is particularly efficient for representing
|
||||
* large collections of numbers, where the majority of elements appear as part
|
||||
* of a sequential range of numbers that are all part of the set. For example,
|
||||
* the set { 1, 2, 3, 4, 7, 8 } may be represented as { [1, 4], [7, 8] }.
|
||||
*
|
||||
* <p>
|
||||
* This class is able to represent sets containing any combination of values in
|
||||
* the range {@link Integer#MIN_VALUE} to {@link Integer#MAX_VALUE}
|
||||
* (inclusive).</p>
|
||||
*/
|
||||
class ANTLR4CPP_PUBLIC IntervalSet {
|
||||
public:
|
||||
static IntervalSet const COMPLETE_CHAR_SET;
|
||||
static IntervalSet const EMPTY_SET;
|
||||
|
||||
private:
|
||||
/// The list of sorted, disjoint intervals.
|
||||
std::vector<Interval> _intervals;
|
||||
|
||||
explicit IntervalSet(std::vector<Interval>&& intervals);
|
||||
|
||||
public:
|
||||
IntervalSet();
|
||||
IntervalSet(IntervalSet const& set);
|
||||
IntervalSet(IntervalSet&& set);
|
||||
|
||||
template<typename T1, typename... T_NEXT>
|
||||
IntervalSet(int, T1 t1, T_NEXT&&... next) : IntervalSet() {
|
||||
// The first int argument is an ignored count for compatibility
|
||||
// with the previous varargs based interface.
|
||||
addItems(t1, std::forward<T_NEXT>(next)...);
|
||||
}
|
||||
|
||||
IntervalSet& operator=(IntervalSet const& set);
|
||||
IntervalSet& operator=(IntervalSet&& set);
|
||||
|
||||
/// Create a set with a single element, el.
|
||||
static IntervalSet of(ssize_t a);
|
||||
|
||||
/// Create a set with all ints within range [a..b] (inclusive)
|
||||
static IntervalSet of(ssize_t a, ssize_t b);
|
||||
|
||||
void clear();
|
||||
|
||||
/// Add a single element to the set. An isolated element is stored
|
||||
/// as a range el..el.
|
||||
void add(ssize_t el);
|
||||
|
||||
/// Add interval; i.e., add all integers from a to b to set.
|
||||
/// If b<a, do nothing.
|
||||
/// Keep list in sorted order (by left range value).
|
||||
/// If overlap, combine ranges. For example,
|
||||
/// If this is {1..5, 10..20}, adding 6..7 yields
|
||||
/// {1..5, 6..7, 10..20}. Adding 4..8 yields {1..8, 10..20}.
|
||||
void add(ssize_t a, ssize_t b);
|
||||
|
||||
/// combine all sets in the array returned the or'd value
|
||||
static IntervalSet Or(const std::vector<IntervalSet> &sets);
|
||||
|
||||
// Copy on write so we can cache a..a intervals and sets of that.
|
||||
void add(const Interval &addition);
|
||||
IntervalSet& addAll(const IntervalSet &set);
|
||||
|
||||
template<typename T1, typename... T_NEXT>
|
||||
void addItems(T1 t1, T_NEXT&&... next) {
|
||||
add(t1);
|
||||
addItems(std::forward<T_NEXT>(next)...);
|
||||
}
|
||||
|
||||
IntervalSet complement(ssize_t minElement, ssize_t maxElement) const;
|
||||
|
||||
/// Given the set of possible values (rather than, say UNICODE or MAXINT),
|
||||
/// return a new set containing all elements in vocabulary, but not in
|
||||
/// this. The computation is (vocabulary - this).
|
||||
///
|
||||
/// 'this' is assumed to be either a subset or equal to vocabulary.
|
||||
IntervalSet complement(const IntervalSet &vocabulary) const;
|
||||
|
||||
/// Compute this-other via this&~other.
|
||||
/// Return a new set containing all elements in this but not in other.
|
||||
/// other is assumed to be a subset of this;
|
||||
/// anything that is in other but not in this will be ignored.
|
||||
IntervalSet subtract(const IntervalSet &other) const;
|
||||
|
||||
/**
|
||||
* Compute the set difference between two interval sets. The specific
|
||||
* operation is {@code left - right}. If either of the input sets is
|
||||
* {@code null}, it is treated as though it was an empty set.
|
||||
*/
|
||||
static IntervalSet subtract(const IntervalSet &left, const IntervalSet &right);
|
||||
|
||||
IntervalSet Or(const IntervalSet &a) const;
|
||||
|
||||
/// Return a new set with the intersection of this set with other. Because
|
||||
/// the intervals are sorted, we can use an iterator for each list and
|
||||
/// just walk them together. This is roughly O(min(n,m)) for interval
|
||||
/// list lengths n and m.
|
||||
IntervalSet And(const IntervalSet &other) const;
|
||||
|
||||
/// Is el in any range of this set?
|
||||
bool contains(size_t el) const; // For mapping of e.g. Token::EOF to -1 etc.
|
||||
bool contains(ssize_t el) const;
|
||||
|
||||
/// return true if this set has no members
|
||||
bool isEmpty() const;
|
||||
|
||||
/// If this set is a single integer, return it otherwise Token.INVALID_TYPE.
|
||||
ssize_t getSingleElement() const;
|
||||
|
||||
/**
|
||||
* Returns the maximum value contained in the set.
|
||||
*
|
||||
* @return the maximum value contained in the set. If the set is empty, this
|
||||
* method returns {@link Token#INVALID_TYPE}.
|
||||
*/
|
||||
ssize_t getMaxElement() const;
|
||||
|
||||
/**
|
||||
* Returns the minimum value contained in the set.
|
||||
*
|
||||
* @return the minimum value contained in the set. If the set is empty, this
|
||||
* method returns {@link Token#INVALID_TYPE}.
|
||||
*/
|
||||
ssize_t getMinElement() const;
|
||||
|
||||
/// <summary>
|
||||
/// Return a list of Interval objects. </summary>
|
||||
std::vector<Interval> const& getIntervals() const;
|
||||
|
||||
size_t hashCode() const;
|
||||
|
||||
/// Are two IntervalSets equal? Because all intervals are sorted
|
||||
/// and disjoint, equals is a simple linear walk over both lists
|
||||
/// to make sure they are the same.
|
||||
bool operator == (const IntervalSet &other) const;
|
||||
std::string toString() const;
|
||||
std::string toString(bool elemAreChar) const;
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #toString(Vocabulary)} instead.
|
||||
*/
|
||||
std::string toString(const std::vector<std::string> &tokenNames) const;
|
||||
std::string toString(const dfa::Vocabulary &vocabulary) const;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @deprecated Use {@link #elementName(Vocabulary, int)} instead.
|
||||
*/
|
||||
std::string elementName(const std::vector<std::string> &tokenNames, ssize_t a) const;
|
||||
std::string elementName(const dfa::Vocabulary &vocabulary, ssize_t a) const;
|
||||
|
||||
public:
|
||||
size_t size() const;
|
||||
std::vector<ssize_t> toList() const;
|
||||
std::set<ssize_t> toSet() const;
|
||||
|
||||
/// Get the ith element of ordered set. Used only by RandomPhrase so
|
||||
/// don't bother to implement if you're not doing that for a new
|
||||
/// ANTLR code gen target.
|
||||
ssize_t get(size_t i) const;
|
||||
void remove(size_t el); // For mapping of e.g. Token::EOF to -1 etc.
|
||||
void remove(ssize_t el);
|
||||
|
||||
private:
|
||||
void addItems() { /* No-op */ }
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
|
||||
// Hash function for IntervalSet.
|
||||
|
||||
namespace std {
|
||||
using antlr4::misc::IntervalSet;
|
||||
|
||||
template <> struct hash<IntervalSet>
|
||||
{
|
||||
size_t operator() (const IntervalSet &x) const
|
||||
{
|
||||
return x.hashCode();
|
||||
}
|
||||
};
|
||||
}
|
76
lib/antlr4/include/misc/MurmurHash.h
Normal file
76
lib/antlr4/include/misc/MurmurHash.h
Normal 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 antlr4 {
|
||||
namespace misc {
|
||||
|
||||
class ANTLR4CPP_PUBLIC MurmurHash {
|
||||
|
||||
private:
|
||||
static const size_t DEFAULT_SEED = 0;
|
||||
|
||||
/// Initialize the hash using the default seed value.
|
||||
/// Returns the intermediate hash value.
|
||||
public:
|
||||
static size_t initialize();
|
||||
|
||||
/// Initialize the hash using the specified seed.
|
||||
static size_t initialize(size_t seed);
|
||||
|
||||
/// Update the intermediate hash value for the next input {@code value}.
|
||||
/// <param name="hash"> the intermediate hash value </param>
|
||||
/// <param name="value"> the value to add to the current hash </param>
|
||||
/// Returns the updated intermediate hash value.
|
||||
static size_t update(size_t hash, size_t value);
|
||||
|
||||
/**
|
||||
* Update the intermediate hash value for the next input {@code value}.
|
||||
*
|
||||
* @param hash the intermediate hash value
|
||||
* @param value the value to add to the current hash
|
||||
* @return the updated intermediate hash value
|
||||
*/
|
||||
template <class T>
|
||||
static size_t update(size_t hash, Ref<T> const& value) {
|
||||
return update(hash, value != nullptr ? value->hashCode() : 0);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static size_t update(size_t hash, T *value) {
|
||||
return update(hash, value != nullptr ? value->hashCode() : 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Apply the final computation steps to the intermediate value {@code hash}
|
||||
/// to form the final result of the MurmurHash 3 hash function.
|
||||
/// </summary>
|
||||
/// <param name="hash"> the intermediate hash value </param>
|
||||
/// <param name="entryCount"> the number of calls to update() before calling finish() </param>
|
||||
/// <returns> the final hash result </returns>
|
||||
static size_t finish(size_t hash, size_t entryCount);
|
||||
|
||||
/// Utility function to compute the hash code of an array using the MurmurHash3 algorithm.
|
||||
///
|
||||
/// @param <T> the array element type </param>
|
||||
/// <param name="data"> the array data </param>
|
||||
/// <param name="seed"> the seed for the MurmurHash algorithm </param>
|
||||
/// <returns> the hash code of the data </returns>
|
||||
template<typename T> // where T is C array type
|
||||
static size_t hashCode(const std::vector<Ref<T>> &data, size_t seed) {
|
||||
size_t hash = initialize(seed);
|
||||
for (auto entry : data) {
|
||||
hash = update(hash, entry->hashCode());
|
||||
}
|
||||
|
||||
return finish(hash, data.size());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
21
lib/antlr4/include/misc/Predicate.h
Normal file
21
lib/antlr4/include/misc/Predicate.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/* 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 misc {
|
||||
|
||||
class ANTLR4CPP_PUBLIC Predicate {
|
||||
public:
|
||||
virtual ~Predicate();
|
||||
|
||||
virtual bool test(tree::ParseTree *t) = 0;
|
||||
};
|
||||
|
||||
} // namespace tree
|
||||
} // namespace antlr4
|
Reference in New Issue
Block a user