forked from NUDT-compiler/nudt-compiler-cpp
parent
af5657dac7
commit
8e4cbbfd87
@ -1,10 +0,0 @@
|
||||
/* 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 "ANTLRErrorListener.h"
|
||||
|
||||
antlr4::ANTLRErrorListener::~ANTLRErrorListener()
|
||||
{
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
/* 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 "ANTLRErrorStrategy.h"
|
||||
|
||||
antlr4::ANTLRErrorStrategy::~ANTLRErrorStrategy()
|
||||
{
|
||||
}
|
||||
@ -1,121 +0,0 @@
|
||||
/* 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 "Token.h"
|
||||
|
||||
namespace antlr4 {
|
||||
|
||||
/// <summary>
|
||||
/// The interface for defining strategies to deal with syntax errors encountered
|
||||
/// during a parse by ANTLR-generated parsers. We distinguish between three
|
||||
/// different kinds of errors:
|
||||
///
|
||||
/// <ul>
|
||||
/// <li>The parser could not figure out which path to take in the ATN (none of
|
||||
/// the available alternatives could possibly match)</li>
|
||||
/// <li>The current input does not match what we were looking for</li>
|
||||
/// <li>A predicate evaluated to false</li>
|
||||
/// </ul>
|
||||
///
|
||||
/// Implementations of this interface report syntax errors by calling
|
||||
/// <seealso cref="Parser#notifyErrorListeners"/>.
|
||||
/// <p/>
|
||||
/// TODO: what to do about lexers
|
||||
/// </summary>
|
||||
class ANTLR4CPP_PUBLIC ANTLRErrorStrategy {
|
||||
public:
|
||||
|
||||
/// <summary>
|
||||
/// Reset the error handler state for the specified {@code recognizer}. </summary>
|
||||
/// <param name="recognizer"> the parser instance </param>
|
||||
virtual ~ANTLRErrorStrategy();
|
||||
|
||||
virtual void reset(Parser *recognizer) = 0;
|
||||
|
||||
/**
|
||||
* This method is called when an unexpected symbol is encountered during an
|
||||
* inline match operation, such as {@link Parser#match}. If the error
|
||||
* strategy successfully recovers from the match failure, this method
|
||||
* returns the {@link Token} instance which should be treated as the
|
||||
* successful result of the match.
|
||||
*
|
||||
* <p>This method handles the consumption of any tokens - the caller should
|
||||
* <b>not</b> call {@link Parser#consume} after a successful recovery.</p>
|
||||
*
|
||||
* <p>Note that the calling code will not report an error if this method
|
||||
* returns successfully. The error strategy implementation is responsible
|
||||
* for calling {@link Parser#notifyErrorListeners} as appropriate.</p>
|
||||
*
|
||||
* @param recognizer the parser instance
|
||||
* @throws RecognitionException if the error strategy was not able to
|
||||
* recover from the unexpected input symbol
|
||||
*/
|
||||
virtual Token* recoverInline(Parser *recognizer) = 0;
|
||||
|
||||
/// <summary>
|
||||
/// This method is called to recover from exception {@code e}. This method is
|
||||
/// called after <seealso cref="#reportError"/> by the default exception handler
|
||||
/// generated for a rule method.
|
||||
/// </summary>
|
||||
/// <seealso cref= #reportError
|
||||
/// </seealso>
|
||||
/// <param name="recognizer"> the parser instance </param>
|
||||
/// <param name="e"> the recognition exception to recover from </param>
|
||||
/// <exception cref="RecognitionException"> if the error strategy could not recover from
|
||||
/// the recognition exception </exception>
|
||||
virtual void recover(Parser *recognizer, std::exception_ptr e) = 0;
|
||||
|
||||
/// <summary>
|
||||
/// This method provides the error handler with an opportunity to handle
|
||||
/// syntactic or semantic errors in the input stream before they result in a
|
||||
/// <seealso cref="RecognitionException"/>.
|
||||
/// <p/>
|
||||
/// The generated code currently contains calls to <seealso cref="#sync"/> after
|
||||
/// entering the decision state of a closure block ({@code (...)*} or
|
||||
/// {@code (...)+}).
|
||||
/// <p/>
|
||||
/// For an implementation based on Jim Idle's "magic sync" mechanism, see
|
||||
/// <seealso cref="DefaultErrorStrategy#sync"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref= DefaultErrorStrategy#sync
|
||||
/// </seealso>
|
||||
/// <param name="recognizer"> the parser instance </param>
|
||||
/// <exception cref="RecognitionException"> if an error is detected by the error
|
||||
/// strategy but cannot be automatically recovered at the current state in
|
||||
/// the parsing process </exception>
|
||||
virtual void sync(Parser *recognizer) = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether or not {@code recognizer} is in the process of recovering
|
||||
/// from an error. In error recovery mode, <seealso cref="Parser#consume"/> adds
|
||||
/// symbols to the parse tree by calling
|
||||
/// {@link Parser#createErrorNode(ParserRuleContext, Token)} then
|
||||
/// {@link ParserRuleContext#addErrorNode(ErrorNode)} instead of
|
||||
/// {@link Parser#createTerminalNode(ParserRuleContext, Token)}.
|
||||
/// </summary>
|
||||
/// <param name="recognizer"> the parser instance </param>
|
||||
/// <returns> {@code true} if the parser is currently recovering from a parse
|
||||
/// error, otherwise {@code false} </returns>
|
||||
virtual bool inErrorRecoveryMode(Parser *recognizer) = 0;
|
||||
|
||||
/// <summary>
|
||||
/// This method is called by when the parser successfully matches an input
|
||||
/// symbol.
|
||||
/// </summary>
|
||||
/// <param name="recognizer"> the parser instance </param>
|
||||
virtual void reportMatch(Parser *recognizer) = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Report any kind of <seealso cref="RecognitionException"/>. This method is called by
|
||||
/// the default exception handler generated for a rule method.
|
||||
/// </summary>
|
||||
/// <param name="recognizer"> the parser instance </param>
|
||||
/// <param name="e"> the recognition exception to report </param>
|
||||
virtual void reportError(Parser *recognizer, const RecognitionException &e) = 0;
|
||||
};
|
||||
|
||||
} // namespace antlr4
|
||||
@ -1,23 +0,0 @@
|
||||
/* 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 "ANTLRFileStream.h"
|
||||
|
||||
using namespace antlr4;
|
||||
|
||||
void ANTLRFileStream::loadFromFile(const std::string &fileName) {
|
||||
_fileName = fileName;
|
||||
if (_fileName.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::ifstream stream(fileName, std::ios::binary);
|
||||
|
||||
ANTLRInputStream::load(stream);
|
||||
}
|
||||
|
||||
std::string ANTLRFileStream::getSourceName() const {
|
||||
return _fileName;
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
/* 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 "ANTLRInputStream.h"
|
||||
|
||||
namespace antlr4 {
|
||||
|
||||
/// This is an ANTLRInputStream that is loaded from a file all at once
|
||||
/// when you construct the object (or call load()).
|
||||
// TODO: this class needs testing.
|
||||
class ANTLR4CPP_PUBLIC ANTLRFileStream : public ANTLRInputStream {
|
||||
public:
|
||||
ANTLRFileStream() = default;
|
||||
ANTLRFileStream(const std::string &) = delete;
|
||||
ANTLRFileStream(const char *data, size_t length) = delete;
|
||||
ANTLRFileStream(std::istream &stream) = delete;
|
||||
|
||||
// Assumes a file name encoded in UTF-8 and file content in the same encoding (with or w/o BOM).
|
||||
virtual void loadFromFile(const std::string &fileName);
|
||||
virtual std::string getSourceName() const override;
|
||||
|
||||
private:
|
||||
std::string _fileName; // UTF-8 encoded file name.
|
||||
};
|
||||
|
||||
} // namespace antlr4
|
||||
@ -1,180 +0,0 @@
|
||||
/* 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 <string.h>
|
||||
|
||||
#include "Exceptions.h"
|
||||
#include "misc/Interval.h"
|
||||
#include "IntStream.h"
|
||||
|
||||
#include "support/Utf8.h"
|
||||
#include "support/CPPUtils.h"
|
||||
|
||||
#include "ANTLRInputStream.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlrcpp;
|
||||
|
||||
using misc::Interval;
|
||||
|
||||
ANTLRInputStream::ANTLRInputStream() {
|
||||
InitializeInstanceFields();
|
||||
}
|
||||
|
||||
ANTLRInputStream::ANTLRInputStream(std::string_view input): ANTLRInputStream() {
|
||||
load(input.data(), input.length());
|
||||
}
|
||||
|
||||
ANTLRInputStream::ANTLRInputStream(const char *data, size_t length) {
|
||||
load(data, length);
|
||||
}
|
||||
|
||||
ANTLRInputStream::ANTLRInputStream(std::istream &stream): ANTLRInputStream() {
|
||||
load(stream);
|
||||
}
|
||||
|
||||
void ANTLRInputStream::load(const std::string &input, bool lenient) {
|
||||
load(input.data(), input.size(), lenient);
|
||||
}
|
||||
|
||||
void ANTLRInputStream::load(const char *data, size_t length, bool lenient) {
|
||||
// Remove the UTF-8 BOM if present.
|
||||
const char *bom = "\xef\xbb\xbf";
|
||||
if (length >= 3 && strncmp(data, bom, 3) == 0) {
|
||||
data += 3;
|
||||
length -= 3;
|
||||
}
|
||||
if (lenient) {
|
||||
_data = Utf8::lenientDecode(std::string_view(data, length));
|
||||
} else {
|
||||
auto maybe_utf32 = Utf8::strictDecode(std::string_view(data, length));
|
||||
if (!maybe_utf32.has_value()) {
|
||||
throw IllegalArgumentException("UTF-8 string contains an illegal byte sequence");
|
||||
}
|
||||
_data = std::move(maybe_utf32).value();
|
||||
}
|
||||
p = 0;
|
||||
}
|
||||
|
||||
void ANTLRInputStream::load(std::istream &stream, bool lenient) {
|
||||
if (!stream.good() || stream.eof()) // No fail, bad or EOF.
|
||||
return;
|
||||
|
||||
_data.clear();
|
||||
|
||||
std::string s((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
|
||||
load(s.data(), s.length(), lenient);
|
||||
}
|
||||
|
||||
void ANTLRInputStream::reset() {
|
||||
p = 0;
|
||||
}
|
||||
|
||||
void ANTLRInputStream::consume() {
|
||||
if (p >= _data.size()) {
|
||||
assert(LA(1) == IntStream::EOF);
|
||||
throw IllegalStateException("cannot consume EOF");
|
||||
}
|
||||
|
||||
if (p < _data.size()) {
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
size_t ANTLRInputStream::LA(ssize_t i) {
|
||||
if (i == 0) {
|
||||
return 0; // undefined
|
||||
}
|
||||
|
||||
ssize_t position = static_cast<ssize_t>(p);
|
||||
if (i < 0) {
|
||||
i++; // e.g., translate LA(-1) to use offset i=0; then _data[p+0-1]
|
||||
if ((position + i - 1) < 0) {
|
||||
return IntStream::EOF; // invalid; no char before first char
|
||||
}
|
||||
}
|
||||
|
||||
if ((position + i - 1) >= static_cast<ssize_t>(_data.size())) {
|
||||
return IntStream::EOF;
|
||||
}
|
||||
|
||||
return _data[static_cast<size_t>((position + i - 1))];
|
||||
}
|
||||
|
||||
size_t ANTLRInputStream::LT(ssize_t i) {
|
||||
return LA(i);
|
||||
}
|
||||
|
||||
size_t ANTLRInputStream::index() {
|
||||
return p;
|
||||
}
|
||||
|
||||
size_t ANTLRInputStream::size() {
|
||||
return _data.size();
|
||||
}
|
||||
|
||||
// Mark/release do nothing. We have entire buffer.
|
||||
ssize_t ANTLRInputStream::mark() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
void ANTLRInputStream::release(ssize_t /* marker */) {
|
||||
}
|
||||
|
||||
void ANTLRInputStream::seek(size_t index) {
|
||||
if (index <= p) {
|
||||
p = index; // just jump; don't update stream state (line, ...)
|
||||
return;
|
||||
}
|
||||
// seek forward, consume until p hits index or n (whichever comes first)
|
||||
index = std::min(index, _data.size());
|
||||
while (p < index) {
|
||||
consume();
|
||||
}
|
||||
}
|
||||
|
||||
std::string ANTLRInputStream::getText(const Interval &interval) {
|
||||
if (interval.a < 0 || interval.b < 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
size_t start = static_cast<size_t>(interval.a);
|
||||
size_t stop = static_cast<size_t>(interval.b);
|
||||
|
||||
|
||||
if (stop >= _data.size()) {
|
||||
stop = _data.size() - 1;
|
||||
}
|
||||
|
||||
size_t count = stop - start + 1;
|
||||
if (start >= _data.size()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
auto maybeUtf8 = Utf8::strictEncode(std::u32string_view(_data).substr(start, count));
|
||||
if (!maybeUtf8.has_value()) {
|
||||
throw IllegalArgumentException("Input stream contains invalid Unicode code points");
|
||||
}
|
||||
return std::move(maybeUtf8).value();
|
||||
}
|
||||
|
||||
std::string ANTLRInputStream::getSourceName() const {
|
||||
if (name.empty()) {
|
||||
return IntStream::UNKNOWN_SOURCE_NAME;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
std::string ANTLRInputStream::toString() const {
|
||||
auto maybeUtf8 = Utf8::strictEncode(_data);
|
||||
if (!maybeUtf8.has_value()) {
|
||||
throw IllegalArgumentException("Input stream contains invalid Unicode code points");
|
||||
}
|
||||
return std::move(maybeUtf8).value();
|
||||
}
|
||||
|
||||
void ANTLRInputStream::InitializeInstanceFields() {
|
||||
p = 0;
|
||||
}
|
||||
@ -1,79 +0,0 @@
|
||||
/* 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 <string_view>
|
||||
|
||||
#include "CharStream.h"
|
||||
|
||||
namespace antlr4 {
|
||||
|
||||
// Vacuum all input from a stream and then treat it
|
||||
// like a string. Can also pass in a string or char[] to use.
|
||||
// Input is expected to be encoded in UTF-8 and converted to UTF-32 internally.
|
||||
class ANTLR4CPP_PUBLIC ANTLRInputStream : public CharStream {
|
||||
protected:
|
||||
/// The data being scanned.
|
||||
// UTF-32
|
||||
std::u32string _data;
|
||||
|
||||
/// 0..n-1 index into string of next char </summary>
|
||||
size_t p;
|
||||
|
||||
public:
|
||||
/// What is name or source of this char stream?
|
||||
std::string name;
|
||||
|
||||
ANTLRInputStream();
|
||||
|
||||
ANTLRInputStream(std::string_view input);
|
||||
|
||||
ANTLRInputStream(const char *data, size_t length);
|
||||
ANTLRInputStream(std::istream &stream);
|
||||
|
||||
virtual void load(const std::string &input, bool lenient);
|
||||
virtual void load(const char *data, size_t length, bool lenient);
|
||||
virtual void load(std::istream &stream, bool lenient);
|
||||
|
||||
virtual void load(const std::string &input) { load(input, false); }
|
||||
virtual void load(const char *data, size_t length) { load(data, length, false); }
|
||||
virtual void load(std::istream &stream) { load(stream, false); }
|
||||
|
||||
/// Reset the stream so that it's in the same state it was
|
||||
/// when the object was created *except* the data array is not
|
||||
/// touched.
|
||||
virtual void reset();
|
||||
virtual void consume() override;
|
||||
virtual size_t LA(ssize_t i) override;
|
||||
virtual size_t LT(ssize_t i);
|
||||
|
||||
/// <summary>
|
||||
/// Return the current input symbol index 0..n where n indicates the
|
||||
/// last symbol has been read. The index is the index of char to
|
||||
/// be returned from LA(1).
|
||||
/// </summary>
|
||||
virtual size_t index() override;
|
||||
virtual size_t size() override;
|
||||
|
||||
/// <summary>
|
||||
/// mark/release do nothing; we have entire buffer </summary>
|
||||
virtual ssize_t mark() override;
|
||||
virtual void release(ssize_t marker) override;
|
||||
|
||||
/// <summary>
|
||||
/// consume() ahead until p==index; can't just set p=index as we must
|
||||
/// update line and charPositionInLine. If we seek backwards, just set p
|
||||
/// </summary>
|
||||
virtual void seek(size_t index) override;
|
||||
virtual std::string getText(const misc::Interval &interval) override;
|
||||
virtual std::string getSourceName() const override;
|
||||
virtual std::string toString() const override;
|
||||
|
||||
private:
|
||||
void InitializeInstanceFields();
|
||||
};
|
||||
|
||||
} // namespace antlr4
|
||||
@ -1,101 +0,0 @@
|
||||
/* 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 <algorithm>
|
||||
#include <any>
|
||||
#include <atomic>
|
||||
#include <bitset>
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <exception>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <typeinfo>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
// Defines for the Guid class and other platform dependent stuff.
|
||||
#ifdef _WIN32
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning (disable: 4250) // Class inherits by dominance.
|
||||
#pragma warning (disable: 4512) // assignment operator could not be generated
|
||||
|
||||
#if _MSC_VER < 1900
|
||||
// Before VS 2015 code like "while (true)" will create a (useless) warning in level 4.
|
||||
#pragma warning (disable: 4127) // conditional expression is constant
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef _WIN64
|
||||
typedef __int64 ssize_t;
|
||||
#else
|
||||
typedef __int32 ssize_t;
|
||||
#endif
|
||||
|
||||
#ifdef ANTLR4CPP_EXPORTS
|
||||
#define ANTLR4CPP_PUBLIC __declspec(dllexport)
|
||||
#else
|
||||
#ifdef ANTLR4CPP_STATIC
|
||||
#define ANTLR4CPP_PUBLIC
|
||||
#else
|
||||
#define ANTLR4CPP_PUBLIC __declspec(dllimport)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#elif defined(__APPLE__)
|
||||
#if __GNUC__ >= 4
|
||||
#define ANTLR4CPP_PUBLIC __attribute__ ((visibility ("default")))
|
||||
#else
|
||||
#define ANTLR4CPP_PUBLIC
|
||||
#endif
|
||||
#else
|
||||
#if __GNUC__ >= 6
|
||||
#define ANTLR4CPP_PUBLIC __attribute__ ((visibility ("default")))
|
||||
#else
|
||||
#define ANTLR4CPP_PUBLIC
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __has_builtin
|
||||
#define ANTLR4CPP_HAVE_BUILTIN(x) __has_builtin(x)
|
||||
#else
|
||||
#define ANTLR4CPP_HAVE_BUILTIN(x) 0
|
||||
#endif
|
||||
|
||||
#define ANTLR4CPP_INTERNAL_STRINGIFY(x) #x
|
||||
#define ANTLR4CPP_STRINGIFY(x) ANTLR4CPP_INTERNAL_STRINGIFY(x)
|
||||
|
||||
// We use everything from the C++ standard library by default.
|
||||
#ifndef ANTLR4CPP_USING_ABSEIL
|
||||
#define ANTLR4CPP_USING_ABSEIL 0
|
||||
#endif
|
||||
|
||||
#include "support/Declarations.h"
|
||||
|
||||
// We have to undefine this symbol as ANTLR will use this name for own members and even
|
||||
// generated functions. Because EOF is a global macro we cannot use e.g. a namespace scope to disambiguate.
|
||||
#ifdef EOF
|
||||
#undef EOF
|
||||
#endif
|
||||
|
||||
#define INVALID_INDEX std::numeric_limits<size_t>::max()
|
||||
template<class T> using Ref = std::shared_ptr<T>;
|
||||
@ -1,168 +0,0 @@
|
||||
/* 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
|
||||
|
||||
// This is the umbrella header for all ANTLR4 C++ runtime headers.
|
||||
|
||||
#include "antlr4-common.h"
|
||||
|
||||
#include "ANTLRErrorListener.h"
|
||||
#include "ANTLRErrorStrategy.h"
|
||||
#include "ANTLRFileStream.h"
|
||||
#include "ANTLRInputStream.h"
|
||||
#include "BailErrorStrategy.h"
|
||||
#include "BaseErrorListener.h"
|
||||
#include "BufferedTokenStream.h"
|
||||
#include "CharStream.h"
|
||||
#include "CommonToken.h"
|
||||
#include "CommonTokenFactory.h"
|
||||
#include "CommonTokenStream.h"
|
||||
#include "ConsoleErrorListener.h"
|
||||
#include "DefaultErrorStrategy.h"
|
||||
#include "DiagnosticErrorListener.h"
|
||||
#include "Exceptions.h"
|
||||
#include "FailedPredicateException.h"
|
||||
#include "InputMismatchException.h"
|
||||
#include "IntStream.h"
|
||||
#include "InterpreterRuleContext.h"
|
||||
#include "Lexer.h"
|
||||
#include "LexerInterpreter.h"
|
||||
#include "LexerNoViableAltException.h"
|
||||
#include "ListTokenSource.h"
|
||||
#include "NoViableAltException.h"
|
||||
#include "Parser.h"
|
||||
#include "ParserInterpreter.h"
|
||||
#include "ParserRuleContext.h"
|
||||
#include "ProxyErrorListener.h"
|
||||
#include "RecognitionException.h"
|
||||
#include "Recognizer.h"
|
||||
#include "RuleContext.h"
|
||||
#include "RuleContextWithAltNum.h"
|
||||
#include "RuntimeMetaData.h"
|
||||
#include "Token.h"
|
||||
#include "TokenFactory.h"
|
||||
#include "TokenSource.h"
|
||||
#include "TokenStream.h"
|
||||
#include "TokenStreamRewriter.h"
|
||||
#include "UnbufferedCharStream.h"
|
||||
#include "UnbufferedTokenStream.h"
|
||||
#include "Version.h"
|
||||
#include "Vocabulary.h"
|
||||
#include "Vocabulary.h"
|
||||
#include "WritableToken.h"
|
||||
#include "atn/ATN.h"
|
||||
#include "atn/ATNConfig.h"
|
||||
#include "atn/ATNConfigSet.h"
|
||||
#include "atn/ATNDeserializationOptions.h"
|
||||
#include "atn/ATNDeserializer.h"
|
||||
#include "atn/ATNSimulator.h"
|
||||
#include "atn/ATNState.h"
|
||||
#include "atn/ATNType.h"
|
||||
#include "atn/ActionTransition.h"
|
||||
#include "atn/AmbiguityInfo.h"
|
||||
#include "atn/ArrayPredictionContext.h"
|
||||
#include "atn/AtomTransition.h"
|
||||
#include "atn/BasicBlockStartState.h"
|
||||
#include "atn/BasicState.h"
|
||||
#include "atn/BlockEndState.h"
|
||||
#include "atn/BlockStartState.h"
|
||||
#include "atn/ContextSensitivityInfo.h"
|
||||
#include "atn/DecisionEventInfo.h"
|
||||
#include "atn/DecisionInfo.h"
|
||||
#include "atn/DecisionState.h"
|
||||
#include "atn/EpsilonTransition.h"
|
||||
#include "atn/ErrorInfo.h"
|
||||
#include "atn/LL1Analyzer.h"
|
||||
#include "atn/LexerATNConfig.h"
|
||||
#include "atn/LexerATNSimulator.h"
|
||||
#include "atn/LexerAction.h"
|
||||
#include "atn/LexerActionExecutor.h"
|
||||
#include "atn/LexerActionType.h"
|
||||
#include "atn/LexerChannelAction.h"
|
||||
#include "atn/LexerCustomAction.h"
|
||||
#include "atn/LexerIndexedCustomAction.h"
|
||||
#include "atn/LexerModeAction.h"
|
||||
#include "atn/LexerMoreAction.h"
|
||||
#include "atn/LexerPopModeAction.h"
|
||||
#include "atn/LexerPushModeAction.h"
|
||||
#include "atn/LexerSkipAction.h"
|
||||
#include "atn/LexerTypeAction.h"
|
||||
#include "atn/LookaheadEventInfo.h"
|
||||
#include "atn/LoopEndState.h"
|
||||
#include "atn/NotSetTransition.h"
|
||||
#include "atn/OrderedATNConfigSet.h"
|
||||
#include "atn/ParseInfo.h"
|
||||
#include "atn/ParserATNSimulator.h"
|
||||
#include "atn/ParserATNSimulatorOptions.h"
|
||||
#include "atn/PlusBlockStartState.h"
|
||||
#include "atn/PlusLoopbackState.h"
|
||||
#include "atn/PrecedencePredicateTransition.h"
|
||||
#include "atn/PredicateEvalInfo.h"
|
||||
#include "atn/PredicateTransition.h"
|
||||
#include "atn/PredictionContext.h"
|
||||
#include "atn/PredictionContextCache.h"
|
||||
#include "atn/PredictionContextMergeCache.h"
|
||||
#include "atn/PredictionContextMergeCacheOptions.h"
|
||||
#include "atn/PredictionMode.h"
|
||||
#include "atn/ProfilingATNSimulator.h"
|
||||
#include "atn/RangeTransition.h"
|
||||
#include "atn/RuleStartState.h"
|
||||
#include "atn/RuleStopState.h"
|
||||
#include "atn/RuleTransition.h"
|
||||
#include "atn/SemanticContext.h"
|
||||
#include "atn/SerializedATNView.h"
|
||||
#include "atn/SetTransition.h"
|
||||
#include "atn/SingletonPredictionContext.h"
|
||||
#include "atn/StarBlockStartState.h"
|
||||
#include "atn/StarLoopEntryState.h"
|
||||
#include "atn/StarLoopbackState.h"
|
||||
#include "atn/TokensStartState.h"
|
||||
#include "atn/Transition.h"
|
||||
#include "atn/WildcardTransition.h"
|
||||
#include "dfa/DFA.h"
|
||||
#include "dfa/DFASerializer.h"
|
||||
#include "dfa/DFAState.h"
|
||||
#include "dfa/LexerDFASerializer.h"
|
||||
#include "misc/InterpreterDataReader.h"
|
||||
#include "misc/Interval.h"
|
||||
#include "misc/IntervalSet.h"
|
||||
#include "misc/MurmurHash.h"
|
||||
#include "misc/Predicate.h"
|
||||
#include "support/Any.h"
|
||||
#include "support/Arrays.h"
|
||||
#include "support/BitSet.h"
|
||||
#include "support/Casts.h"
|
||||
#include "support/CPPUtils.h"
|
||||
#include "tree/AbstractParseTreeVisitor.h"
|
||||
#include "tree/ErrorNode.h"
|
||||
#include "tree/ErrorNodeImpl.h"
|
||||
#include "tree/ParseTree.h"
|
||||
#include "tree/ParseTreeListener.h"
|
||||
#include "tree/ParseTreeProperty.h"
|
||||
#include "tree/ParseTreeVisitor.h"
|
||||
#include "tree/ParseTreeWalker.h"
|
||||
#include "tree/TerminalNode.h"
|
||||
#include "tree/TerminalNodeImpl.h"
|
||||
#include "tree/Trees.h"
|
||||
#include "tree/pattern/Chunk.h"
|
||||
#include "tree/pattern/ParseTreeMatch.h"
|
||||
#include "tree/pattern/ParseTreePattern.h"
|
||||
#include "tree/pattern/ParseTreePatternMatcher.h"
|
||||
#include "tree/pattern/RuleTagToken.h"
|
||||
#include "tree/pattern/TagChunk.h"
|
||||
#include "tree/pattern/TextChunk.h"
|
||||
#include "tree/pattern/TokenTagToken.h"
|
||||
#include "tree/xpath/XPath.h"
|
||||
#include "tree/xpath/XPathElement.h"
|
||||
#include "tree/xpath/XPathLexer.h"
|
||||
#include "tree/xpath/XPathLexerErrorListener.h"
|
||||
#include "tree/xpath/XPathRuleAnywhereElement.h"
|
||||
#include "tree/xpath/XPathRuleElement.h"
|
||||
#include "tree/xpath/XPathTokenAnywhereElement.h"
|
||||
#include "tree/xpath/XPathTokenElement.h"
|
||||
#include "tree/xpath/XPathWildcardAnywhereElement.h"
|
||||
#include "tree/xpath/XPathWildcardElement.h"
|
||||
#include "internal/Synchronization.h"
|
||||
@ -1,106 +0,0 @@
|
||||
/* 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 "misc/MurmurHash.h"
|
||||
#include "atn/PredictionContext.h"
|
||||
#include "SemanticContext.h"
|
||||
|
||||
#include "atn/ATNConfig.h"
|
||||
|
||||
using namespace antlr4::atn;
|
||||
|
||||
namespace {
|
||||
|
||||
/**
|
||||
* This field stores the bit mask for implementing the
|
||||
* {@link #isPrecedenceFilterSuppressed} property as a bit within the
|
||||
* existing {@link #reachesIntoOuterContext} field.
|
||||
*/
|
||||
inline constexpr size_t SUPPRESS_PRECEDENCE_FILTER = 0x40000000;
|
||||
|
||||
}
|
||||
|
||||
ATNConfig::ATNConfig(ATNState *state, size_t alt, Ref<const PredictionContext> context)
|
||||
: ATNConfig(state, alt, std::move(context), 0, SemanticContext::Empty::Instance) {}
|
||||
|
||||
ATNConfig::ATNConfig(ATNState *state, size_t alt, Ref<const PredictionContext> context, Ref<const SemanticContext> semanticContext)
|
||||
: ATNConfig(state, alt, std::move(context), 0, std::move(semanticContext)) {}
|
||||
|
||||
ATNConfig::ATNConfig(ATNConfig const& other, Ref<const SemanticContext> semanticContext)
|
||||
: ATNConfig(other.state, other.alt, other.context, other.reachesIntoOuterContext, std::move(semanticContext)) {}
|
||||
|
||||
ATNConfig::ATNConfig(ATNConfig const& other, ATNState *state)
|
||||
: ATNConfig(state, other.alt, other.context, other.reachesIntoOuterContext, other.semanticContext) {}
|
||||
|
||||
ATNConfig::ATNConfig(ATNConfig const& other, ATNState *state, Ref<const SemanticContext> semanticContext)
|
||||
: ATNConfig(state, other.alt, other.context, other.reachesIntoOuterContext, std::move(semanticContext)) {}
|
||||
|
||||
ATNConfig::ATNConfig(ATNConfig const& other, ATNState *state, Ref<const PredictionContext> context)
|
||||
: ATNConfig(state, other.alt, std::move(context), other.reachesIntoOuterContext, other.semanticContext) {}
|
||||
|
||||
ATNConfig::ATNConfig(ATNConfig const& other, ATNState *state, Ref<const PredictionContext> context, Ref<const SemanticContext> semanticContext)
|
||||
: ATNConfig(state, other.alt, std::move(context), other.reachesIntoOuterContext, std::move(semanticContext)) {}
|
||||
|
||||
ATNConfig::ATNConfig(ATNState *state, size_t alt, Ref<const PredictionContext> context, size_t reachesIntoOuterContext, Ref<const SemanticContext> semanticContext)
|
||||
: state(state), alt(alt), context(std::move(context)), reachesIntoOuterContext(reachesIntoOuterContext), semanticContext(std::move(semanticContext)) {}
|
||||
|
||||
size_t ATNConfig::hashCode() const {
|
||||
size_t hashCode = misc::MurmurHash::initialize(7);
|
||||
hashCode = misc::MurmurHash::update(hashCode, state->stateNumber);
|
||||
hashCode = misc::MurmurHash::update(hashCode, alt);
|
||||
hashCode = misc::MurmurHash::update(hashCode, context);
|
||||
hashCode = misc::MurmurHash::update(hashCode, semanticContext);
|
||||
hashCode = misc::MurmurHash::finish(hashCode, 4);
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
size_t ATNConfig::getOuterContextDepth() const {
|
||||
return reachesIntoOuterContext & ~SUPPRESS_PRECEDENCE_FILTER;
|
||||
}
|
||||
|
||||
bool ATNConfig::isPrecedenceFilterSuppressed() const {
|
||||
return (reachesIntoOuterContext & SUPPRESS_PRECEDENCE_FILTER) != 0;
|
||||
}
|
||||
|
||||
void ATNConfig::setPrecedenceFilterSuppressed(bool value) {
|
||||
if (value) {
|
||||
reachesIntoOuterContext |= SUPPRESS_PRECEDENCE_FILTER;
|
||||
} else {
|
||||
reachesIntoOuterContext &= ~SUPPRESS_PRECEDENCE_FILTER;
|
||||
}
|
||||
}
|
||||
|
||||
bool ATNConfig::operator==(const ATNConfig &other) const {
|
||||
return state->stateNumber == other.state->stateNumber && alt == other.alt &&
|
||||
((context == other.context) || (*context == *other.context)) &&
|
||||
*semanticContext == *other.semanticContext &&
|
||||
isPrecedenceFilterSuppressed() == other.isPrecedenceFilterSuppressed();
|
||||
}
|
||||
|
||||
std::string ATNConfig::toString() const {
|
||||
return toString(true);
|
||||
}
|
||||
|
||||
std::string ATNConfig::toString(bool showAlt) const {
|
||||
std::stringstream ss;
|
||||
ss << "(";
|
||||
|
||||
ss << state->toString();
|
||||
if (showAlt) {
|
||||
ss << "," << alt;
|
||||
}
|
||||
if (context) {
|
||||
ss << ",[" << context->toString() << "]";
|
||||
}
|
||||
if (semanticContext != nullptr && semanticContext != SemanticContext::Empty::Instance) {
|
||||
ss << "," << semanticContext->toString();
|
||||
}
|
||||
if (getOuterContextDepth() > 0) {
|
||||
ss << ",up=" << getOuterContextDepth();
|
||||
}
|
||||
ss << ")";
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
@ -1,157 +0,0 @@
|
||||
/* 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 <cassert>
|
||||
|
||||
#include "antlr4-common.h"
|
||||
#include "atn/SemanticContext.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// <summary>
|
||||
/// A tuple: (ATN state, predicted alt, syntactic, semantic context).
|
||||
/// The syntactic context is a graph-structured stack node whose
|
||||
/// path(s) to the root is the rule invocation(s)
|
||||
/// chain used to arrive at the state. The semantic context is
|
||||
/// the tree of semantic predicates encountered before reaching
|
||||
/// an ATN state.
|
||||
/// </summary>
|
||||
class ANTLR4CPP_PUBLIC ATNConfig {
|
||||
public:
|
||||
struct Hasher
|
||||
{
|
||||
size_t operator()(Ref<ATNConfig> const& k) const {
|
||||
return k->hashCode();
|
||||
}
|
||||
|
||||
size_t operator()(ATNConfig const& k) const {
|
||||
return k.hashCode();
|
||||
}
|
||||
};
|
||||
|
||||
struct Comparer {
|
||||
bool operator()(Ref<ATNConfig> const& lhs, Ref<ATNConfig> const& rhs) const {
|
||||
return (lhs == rhs) || (*lhs == *rhs);
|
||||
}
|
||||
|
||||
bool operator()(ATNConfig const& lhs, ATNConfig const& rhs) const {
|
||||
return (&lhs == &rhs) || (lhs == rhs);
|
||||
}
|
||||
};
|
||||
|
||||
using Set = std::unordered_set<Ref<ATNConfig>, Hasher, Comparer>;
|
||||
|
||||
/// The ATN state associated with this configuration.
|
||||
ATNState *state = nullptr;
|
||||
|
||||
/// What alt (or lexer rule) is predicted by this configuration.
|
||||
const size_t alt = 0;
|
||||
|
||||
/// The stack of invoking states leading to the rule/states associated
|
||||
/// with this config. We track only those contexts pushed during
|
||||
/// execution of the ATN simulator.
|
||||
///
|
||||
/// Can be shared between multiple ANTConfig instances.
|
||||
Ref<const PredictionContext> context;
|
||||
|
||||
/**
|
||||
* We cannot execute predicates dependent upon local context unless
|
||||
* we know for sure we are in the correct context. Because there is
|
||||
* no way to do this efficiently, we simply cannot evaluate
|
||||
* dependent predicates unless we are in the rule that initially
|
||||
* invokes the ATN simulator.
|
||||
*
|
||||
* <p>
|
||||
* closure() tracks the depth of how far we dip into the outer context:
|
||||
* depth > 0. Note that it may not be totally accurate depth since I
|
||||
* don't ever decrement. TODO: make it a boolean then</p>
|
||||
*
|
||||
* <p>
|
||||
* For memory efficiency, the {@link #isPrecedenceFilterSuppressed} method
|
||||
* is also backed by this field. Since the field is publicly accessible, the
|
||||
* highest bit which would not cause the value to become negative is used to
|
||||
* store this field. This choice minimizes the risk that code which only
|
||||
* compares this value to 0 would be affected by the new purpose of the
|
||||
* flag. It also ensures the performance of the existing {@link ATNConfig}
|
||||
* constructors as well as certain operations like
|
||||
* {@link ATNConfigSet#add(ATNConfig, DoubleKeyMap)} method are
|
||||
* <em>completely</em> unaffected by the change.</p>
|
||||
*/
|
||||
size_t reachesIntoOuterContext = 0;
|
||||
|
||||
/// Can be shared between multiple ATNConfig instances.
|
||||
Ref<const SemanticContext> semanticContext;
|
||||
|
||||
ATNConfig(ATNState *state, size_t alt, Ref<const PredictionContext> context);
|
||||
ATNConfig(ATNState *state, size_t alt, Ref<const PredictionContext> context, Ref<const SemanticContext> semanticContext);
|
||||
|
||||
ATNConfig(ATNConfig const& other, Ref<const SemanticContext> semanticContext);
|
||||
ATNConfig(ATNConfig const& other, ATNState *state);
|
||||
ATNConfig(ATNConfig const& other, ATNState *state, Ref<const SemanticContext> semanticContext);
|
||||
ATNConfig(ATNConfig const& other, ATNState *state, Ref<const PredictionContext> context);
|
||||
ATNConfig(ATNConfig const& other, ATNState *state, Ref<const PredictionContext> context, Ref<const SemanticContext> semanticContext);
|
||||
|
||||
ATNConfig(ATNConfig const&) = default;
|
||||
|
||||
ATNConfig(ATNConfig&&) = default;
|
||||
|
||||
virtual ~ATNConfig() = default;
|
||||
|
||||
virtual size_t hashCode() const;
|
||||
|
||||
/**
|
||||
* This method gets the value of the {@link #reachesIntoOuterContext} field
|
||||
* as it existed prior to the introduction of the
|
||||
* {@link #isPrecedenceFilterSuppressed} method.
|
||||
*/
|
||||
size_t getOuterContextDepth() const;
|
||||
bool isPrecedenceFilterSuppressed() const;
|
||||
void setPrecedenceFilterSuppressed(bool value);
|
||||
|
||||
/// An ATN configuration is equal to another if both have
|
||||
/// the same state, they predict the same alternative, and
|
||||
/// syntactic/semantic contexts are the same.
|
||||
bool operator==(const ATNConfig &other) const;
|
||||
bool operator!=(const ATNConfig &other) const;
|
||||
|
||||
virtual std::string toString() const;
|
||||
std::string toString(bool showAlt) const;
|
||||
|
||||
private:
|
||||
ATNConfig(ATNState *state, size_t alt, Ref<const PredictionContext> context, size_t reachesIntoOuterContext, Ref<const SemanticContext> semanticContext);
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
|
||||
|
||||
// Hash function for ATNConfig.
|
||||
|
||||
namespace std {
|
||||
using antlr4::atn::ATNConfig;
|
||||
|
||||
template <> struct hash<ATNConfig>
|
||||
{
|
||||
size_t operator() (const ATNConfig &x) const
|
||||
{
|
||||
return x.hashCode();
|
||||
}
|
||||
};
|
||||
|
||||
template <> struct hash<std::vector<Ref<ATNConfig>>>
|
||||
{
|
||||
size_t operator() (const std::vector<Ref<ATNConfig>> &vector) const
|
||||
{
|
||||
std::size_t seed = 0;
|
||||
for (const auto &config : vector) {
|
||||
seed ^= config->hashCode() + 0x9e3779b9 + (seed << 6) + (seed >> 2);
|
||||
}
|
||||
return seed;
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -1,233 +0,0 @@
|
||||
/* 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 "atn/PredictionContext.h"
|
||||
#include "atn/ATNConfig.h"
|
||||
#include "atn/ATNSimulator.h"
|
||||
#include "Exceptions.h"
|
||||
#include "atn/SemanticContext.h"
|
||||
#include "support/Arrays.h"
|
||||
|
||||
#include "atn/ATNConfigSet.h"
|
||||
|
||||
using namespace antlr4::atn;
|
||||
using namespace antlrcpp;
|
||||
|
||||
namespace {
|
||||
|
||||
}
|
||||
|
||||
ATNConfigSet::ATNConfigSet() : ATNConfigSet(true) {}
|
||||
|
||||
ATNConfigSet::ATNConfigSet(const ATNConfigSet &other)
|
||||
: fullCtx(other.fullCtx), _configLookup(other._configLookup.bucket_count(), ATNConfigHasher{this}, ATNConfigComparer{this}) {
|
||||
addAll(other);
|
||||
uniqueAlt = other.uniqueAlt;
|
||||
conflictingAlts = other.conflictingAlts;
|
||||
hasSemanticContext = other.hasSemanticContext;
|
||||
dipsIntoOuterContext = other.dipsIntoOuterContext;
|
||||
}
|
||||
|
||||
ATNConfigSet::ATNConfigSet(bool fullCtx)
|
||||
: fullCtx(fullCtx), _configLookup(0, ATNConfigHasher{this}, ATNConfigComparer{this}) {}
|
||||
|
||||
bool ATNConfigSet::add(const Ref<ATNConfig> &config) {
|
||||
return add(config, nullptr);
|
||||
}
|
||||
|
||||
bool ATNConfigSet::add(const Ref<ATNConfig> &config, PredictionContextMergeCache *mergeCache) {
|
||||
assert(config);
|
||||
|
||||
if (_readonly) {
|
||||
throw IllegalStateException("This set is readonly");
|
||||
}
|
||||
if (config->semanticContext != SemanticContext::Empty::Instance) {
|
||||
hasSemanticContext = true;
|
||||
}
|
||||
if (config->getOuterContextDepth() > 0) {
|
||||
dipsIntoOuterContext = true;
|
||||
}
|
||||
|
||||
auto existing = _configLookup.find(config.get());
|
||||
if (existing == _configLookup.end()) {
|
||||
_configLookup.insert(config.get());
|
||||
_cachedHashCode = 0;
|
||||
configs.push_back(config); // track order here
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// a previous (s,i,pi,_), merge with it and save result
|
||||
bool rootIsWildcard = !fullCtx;
|
||||
Ref<const PredictionContext> merged = PredictionContext::merge((*existing)->context, config->context, rootIsWildcard, mergeCache);
|
||||
// no need to check for existing.context, config.context in cache
|
||||
// since only way to create new graphs is "call rule" and here. We
|
||||
// cache at both places.
|
||||
(*existing)->reachesIntoOuterContext = std::max((*existing)->reachesIntoOuterContext, config->reachesIntoOuterContext);
|
||||
|
||||
// make sure to preserve the precedence filter suppression during the merge
|
||||
if (config->isPrecedenceFilterSuppressed()) {
|
||||
(*existing)->setPrecedenceFilterSuppressed(true);
|
||||
}
|
||||
|
||||
(*existing)->context = std::move(merged); // replace context; no need to alt mapping
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ATNConfigSet::addAll(const ATNConfigSet &other) {
|
||||
for (const auto &c : other.configs) {
|
||||
add(c);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<ATNState*> ATNConfigSet::getStates() const {
|
||||
std::vector<ATNState*> states;
|
||||
states.reserve(configs.size());
|
||||
for (const auto &c : configs) {
|
||||
states.push_back(c->state);
|
||||
}
|
||||
return states;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the complete set of represented alternatives for the configuration
|
||||
* set.
|
||||
*
|
||||
* @return the set of represented alternatives in this configuration set
|
||||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
|
||||
BitSet ATNConfigSet::getAlts() const {
|
||||
BitSet alts;
|
||||
for (const auto &config : configs) {
|
||||
alts.set(config->alt);
|
||||
}
|
||||
return alts;
|
||||
}
|
||||
|
||||
std::vector<Ref<const SemanticContext>> ATNConfigSet::getPredicates() const {
|
||||
std::vector<Ref<const SemanticContext>> preds;
|
||||
preds.reserve(configs.size());
|
||||
for (const auto &c : configs) {
|
||||
if (c->semanticContext != SemanticContext::Empty::Instance) {
|
||||
preds.push_back(c->semanticContext);
|
||||
}
|
||||
}
|
||||
return preds;
|
||||
}
|
||||
|
||||
const Ref<ATNConfig>& ATNConfigSet::get(size_t i) const {
|
||||
return configs[i];
|
||||
}
|
||||
|
||||
void ATNConfigSet::optimizeConfigs(ATNSimulator *interpreter) {
|
||||
assert(interpreter);
|
||||
|
||||
if (_readonly) {
|
||||
throw IllegalStateException("This set is readonly");
|
||||
}
|
||||
if (_configLookup.empty())
|
||||
return;
|
||||
|
||||
for (const auto &config : configs) {
|
||||
config->context = interpreter->getCachedContext(config->context);
|
||||
}
|
||||
}
|
||||
|
||||
bool ATNConfigSet::equals(const ATNConfigSet &other) const {
|
||||
if (&other == this) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (configs.size() != other.configs.size())
|
||||
return false;
|
||||
|
||||
if (fullCtx != other.fullCtx || uniqueAlt != other.uniqueAlt ||
|
||||
conflictingAlts != other.conflictingAlts || hasSemanticContext != other.hasSemanticContext ||
|
||||
dipsIntoOuterContext != other.dipsIntoOuterContext) // includes stack context
|
||||
return false;
|
||||
|
||||
return Arrays::equals(configs, other.configs);
|
||||
}
|
||||
|
||||
size_t ATNConfigSet::hashCode() const {
|
||||
size_t cachedHashCode = _cachedHashCode.load(std::memory_order_relaxed);
|
||||
if (!isReadonly() || cachedHashCode == 0) {
|
||||
cachedHashCode = 1;
|
||||
for (const auto &i : configs) {
|
||||
cachedHashCode = 31 * cachedHashCode + i->hashCode(); // Same as Java's list hashCode impl.
|
||||
}
|
||||
_cachedHashCode.store(cachedHashCode, std::memory_order_relaxed);
|
||||
}
|
||||
return cachedHashCode;
|
||||
}
|
||||
|
||||
size_t ATNConfigSet::size() const {
|
||||
return configs.size();
|
||||
}
|
||||
|
||||
bool ATNConfigSet::isEmpty() const {
|
||||
return configs.empty();
|
||||
}
|
||||
|
||||
void ATNConfigSet::clear() {
|
||||
if (_readonly) {
|
||||
throw IllegalStateException("This set is readonly");
|
||||
}
|
||||
configs.clear();
|
||||
_cachedHashCode = 0;
|
||||
_configLookup.clear();
|
||||
}
|
||||
|
||||
bool ATNConfigSet::isReadonly() const {
|
||||
return _readonly;
|
||||
}
|
||||
|
||||
void ATNConfigSet::setReadonly(bool readonly) {
|
||||
_readonly = readonly;
|
||||
LookupContainer(0, ATNConfigHasher{this}, ATNConfigComparer{this}).swap(_configLookup);
|
||||
}
|
||||
|
||||
std::string ATNConfigSet::toString() const {
|
||||
std::stringstream ss;
|
||||
ss << "[";
|
||||
for (size_t i = 0; i < configs.size(); i++) {
|
||||
if ( i>0 ) ss << ", ";
|
||||
ss << configs[i]->toString();
|
||||
}
|
||||
ss << "]";
|
||||
|
||||
if (hasSemanticContext) {
|
||||
ss << ",hasSemanticContext=" << (hasSemanticContext?"true":"false");
|
||||
}
|
||||
if (uniqueAlt != ATN::INVALID_ALT_NUMBER) {
|
||||
ss << ",uniqueAlt=" << uniqueAlt;
|
||||
}
|
||||
|
||||
if (conflictingAlts.count() > 0) {
|
||||
ss << ",conflictingAlts=";
|
||||
ss << conflictingAlts.toString();
|
||||
}
|
||||
|
||||
if (dipsIntoOuterContext) {
|
||||
ss << ",dipsIntoOuterContext";
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
size_t ATNConfigSet::hashCode(const ATNConfig &other) const {
|
||||
size_t hashCode = 7;
|
||||
hashCode = 31 * hashCode + other.state->stateNumber;
|
||||
hashCode = 31 * hashCode + other.alt;
|
||||
hashCode = 31 * hashCode + other.semanticContext->hashCode();
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
bool ATNConfigSet::equals(const ATNConfig &lhs, const ATNConfig &rhs) const {
|
||||
return lhs.state->stateNumber == rhs.state->stateNumber && lhs.alt == rhs.alt && *lhs.semanticContext == *rhs.semanticContext;
|
||||
}
|
||||
@ -1,157 +0,0 @@
|
||||
/* 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 <cassert>
|
||||
|
||||
#include "support/BitSet.h"
|
||||
#include "atn/PredictionContext.h"
|
||||
#include "atn/ATNConfig.h"
|
||||
#include "FlatHashSet.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// Specialized set that can track info about the set, with support for combining similar configurations using a
|
||||
/// graph-structured stack.
|
||||
class ANTLR4CPP_PUBLIC ATNConfigSet {
|
||||
public:
|
||||
/// Track the elements as they are added to the set; supports get(i)
|
||||
std::vector<Ref<ATNConfig>> configs;
|
||||
|
||||
// TODO: these fields make me pretty uncomfortable but nice to pack up info together, saves recomputation
|
||||
// TODO: can we track conflicts as they are added to save scanning configs later?
|
||||
size_t uniqueAlt = 0;
|
||||
|
||||
/** Currently this is only used when we detect SLL conflict; this does
|
||||
* not necessarily represent the ambiguous alternatives. In fact,
|
||||
* I should also point out that this seems to include predicated alternatives
|
||||
* that have predicates that evaluate to false. Computed in computeTargetState().
|
||||
*/
|
||||
antlrcpp::BitSet conflictingAlts;
|
||||
|
||||
// Used in parser and lexer. In lexer, it indicates we hit a pred
|
||||
// while computing a closure operation. Don't make a DFA state from this.
|
||||
bool hasSemanticContext = false;
|
||||
bool dipsIntoOuterContext = false;
|
||||
|
||||
/// Indicates that this configuration set is part of a full context
|
||||
/// LL prediction. It will be used to determine how to merge $. With SLL
|
||||
/// it's a wildcard whereas it is not for LL context merge.
|
||||
const bool fullCtx = true;
|
||||
|
||||
ATNConfigSet();
|
||||
|
||||
ATNConfigSet(const ATNConfigSet &other);
|
||||
|
||||
ATNConfigSet(ATNConfigSet&&) = delete;
|
||||
|
||||
explicit ATNConfigSet(bool fullCtx);
|
||||
|
||||
virtual ~ATNConfigSet() = default;
|
||||
|
||||
bool add(const Ref<ATNConfig> &config);
|
||||
|
||||
/// <summary>
|
||||
/// Adding a new config means merging contexts with existing configs for
|
||||
/// {@code (s, i, pi, _)}, where {@code s} is the
|
||||
/// <seealso cref="ATNConfig#state"/>, {@code i} is the <seealso cref="ATNConfig#alt"/>, and
|
||||
/// {@code pi} is the <seealso cref="ATNConfig#semanticContext"/>. We use
|
||||
/// {@code (s,i,pi)} as key.
|
||||
/// <p/>
|
||||
/// This method updates <seealso cref="#dipsIntoOuterContext"/> and
|
||||
/// <seealso cref="#hasSemanticContext"/> when necessary.
|
||||
/// </summary>
|
||||
bool add(const Ref<ATNConfig> &config, PredictionContextMergeCache *mergeCache);
|
||||
|
||||
bool addAll(const ATNConfigSet &other);
|
||||
|
||||
std::vector<ATNState*> getStates() const;
|
||||
|
||||
/**
|
||||
* Gets the complete set of represented alternatives for the configuration
|
||||
* set.
|
||||
*
|
||||
* @return the set of represented alternatives in this configuration set
|
||||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
antlrcpp::BitSet getAlts() const;
|
||||
std::vector<Ref<const SemanticContext>> getPredicates() const;
|
||||
|
||||
const Ref<ATNConfig>& get(size_t i) const;
|
||||
|
||||
void optimizeConfigs(ATNSimulator *interpreter);
|
||||
|
||||
size_t size() const;
|
||||
bool isEmpty() const;
|
||||
void clear();
|
||||
bool isReadonly() const;
|
||||
void setReadonly(bool readonly);
|
||||
|
||||
virtual size_t hashCode() const;
|
||||
|
||||
virtual bool equals(const ATNConfigSet &other) const;
|
||||
|
||||
virtual std::string toString() const;
|
||||
|
||||
private:
|
||||
struct ATNConfigHasher final {
|
||||
const ATNConfigSet* atnConfigSet;
|
||||
|
||||
size_t operator()(const ATNConfig *other) const {
|
||||
assert(other != nullptr);
|
||||
return atnConfigSet->hashCode(*other);
|
||||
}
|
||||
};
|
||||
|
||||
struct ATNConfigComparer final {
|
||||
const ATNConfigSet* atnConfigSet;
|
||||
|
||||
bool operator()(const ATNConfig *lhs, const ATNConfig *rhs) const {
|
||||
assert(lhs != nullptr);
|
||||
assert(rhs != nullptr);
|
||||
return atnConfigSet->equals(*lhs, *rhs);
|
||||
}
|
||||
};
|
||||
|
||||
mutable std::atomic<size_t> _cachedHashCode = 0;
|
||||
|
||||
/// Indicates that the set of configurations is read-only. Do not
|
||||
/// allow any code to manipulate the set; DFA states will point at
|
||||
/// the sets and they must not change. This does not protect the other
|
||||
/// fields; in particular, conflictingAlts is set after
|
||||
/// we've made this readonly.
|
||||
bool _readonly = false;
|
||||
|
||||
virtual size_t hashCode(const ATNConfig &atnConfig) const;
|
||||
|
||||
virtual bool equals(const ATNConfig &lhs, const ATNConfig &rhs) const;
|
||||
|
||||
using LookupContainer = FlatHashSet<ATNConfig*, ATNConfigHasher, ATNConfigComparer>;
|
||||
|
||||
/// All configs but hashed by (s, i, _, pi) not including context. Wiped out
|
||||
/// when we go readonly as this set becomes a DFA state.
|
||||
LookupContainer _configLookup;
|
||||
};
|
||||
|
||||
inline bool operator==(const ATNConfigSet &lhs, const ATNConfigSet &rhs) { return lhs.equals(rhs); }
|
||||
|
||||
inline bool operator!=(const ATNConfigSet &lhs, const ATNConfigSet &rhs) { return !operator==(lhs, rhs); }
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
|
||||
namespace std {
|
||||
|
||||
template <>
|
||||
struct hash<::antlr4::atn::ATNConfigSet> {
|
||||
size_t operator()(const ::antlr4::atn::ATNConfigSet &atnConfigSet) const {
|
||||
return atnConfigSet.hashCode();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
@ -1,33 +0,0 @@
|
||||
#include "atn/ATNStateType.h"
|
||||
|
||||
std::string antlr4::atn::atnStateTypeName(ATNStateType atnStateType) {
|
||||
switch (atnStateType) {
|
||||
case ATNStateType::INVALID:
|
||||
return "INVALID";
|
||||
case ATNStateType::BASIC:
|
||||
return "BASIC";
|
||||
case ATNStateType::RULE_START:
|
||||
return "RULE_START";
|
||||
case ATNStateType::BLOCK_START:
|
||||
return "BLOCK_START";
|
||||
case ATNStateType::PLUS_BLOCK_START:
|
||||
return "PLUS_BLOCK_START";
|
||||
case ATNStateType::STAR_BLOCK_START:
|
||||
return "STAR_BLOCK_START";
|
||||
case ATNStateType::TOKEN_START:
|
||||
return "TOKEN_START";
|
||||
case ATNStateType::RULE_STOP:
|
||||
return "RULE_STOP";
|
||||
case ATNStateType::BLOCK_END:
|
||||
return "BLOCK_END";
|
||||
case ATNStateType::STAR_LOOP_BACK:
|
||||
return "STAR_LOOP_BACK";
|
||||
case ATNStateType::STAR_LOOP_ENTRY:
|
||||
return "STAR_LOOP_ENTRY";
|
||||
case ATNStateType::PLUS_LOOP_BACK:
|
||||
return "PLUS_LOOP_BACK";
|
||||
case ATNStateType::LOOP_END:
|
||||
return "LOOP_END";
|
||||
}
|
||||
return "UNKNOWN";
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
/* 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 <cstddef>
|
||||
#include <string>
|
||||
|
||||
#include "antlr4-common.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
// Constants for ATNState serialization.
|
||||
enum class ATNStateType : size_t {
|
||||
INVALID = 0,
|
||||
BASIC = 1,
|
||||
RULE_START = 2,
|
||||
BLOCK_START = 3,
|
||||
PLUS_BLOCK_START = 4,
|
||||
STAR_BLOCK_START = 5,
|
||||
TOKEN_START = 6,
|
||||
RULE_STOP = 7,
|
||||
BLOCK_END = 8,
|
||||
STAR_LOOP_BACK = 9,
|
||||
STAR_LOOP_ENTRY = 10,
|
||||
PLUS_LOOP_BACK = 11,
|
||||
LOOP_END = 12,
|
||||
};
|
||||
|
||||
ANTLR4CPP_PUBLIC std::string atnStateTypeName(ATNStateType atnStateType);
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,16 +0,0 @@
|
||||
/* 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 "atn/AmbiguityInfo.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
|
||||
AmbiguityInfo::AmbiguityInfo(size_t decision, ATNConfigSet *configs, const antlrcpp::BitSet &ambigAlts,
|
||||
TokenStream *input, size_t startIndex, size_t stopIndex, bool fullCtx)
|
||||
: DecisionEventInfo(decision, configs, input, startIndex, stopIndex, fullCtx) {
|
||||
|
||||
this->ambigAlts = ambigAlts;
|
||||
}
|
||||
@ -1,68 +0,0 @@
|
||||
/* 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 "atn/DecisionEventInfo.h"
|
||||
#include "support/BitSet.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// <summary>
|
||||
/// This class represents profiling event information for an ambiguity.
|
||||
/// Ambiguities are decisions where a particular input resulted in an SLL
|
||||
/// conflict, followed by LL prediction also reaching a conflict state
|
||||
/// (indicating a true ambiguity in the grammar).
|
||||
///
|
||||
/// <para>
|
||||
/// This event may be reported during SLL prediction in cases where the
|
||||
/// conflicting SLL configuration set provides sufficient information to
|
||||
/// determine that the SLL conflict is truly an ambiguity. For example, if none
|
||||
/// of the ATN configurations in the conflicting SLL configuration set have
|
||||
/// traversed a global follow transition (i.e.
|
||||
/// <seealso cref="ATNConfig#reachesIntoOuterContext"/> is 0 for all configurations), then
|
||||
/// the result of SLL prediction for that input is known to be equivalent to the
|
||||
/// result of LL prediction for that input.</para>
|
||||
///
|
||||
/// <para>
|
||||
/// In some cases, the minimum represented alternative in the conflicting LL
|
||||
/// configuration set is not equal to the minimum represented alternative in the
|
||||
/// conflicting SLL configuration set. Grammars and inputs which result in this
|
||||
/// scenario are unable to use <seealso cref="PredictionMode#SLL"/>, which in turn means
|
||||
/// they cannot use the two-stage parsing strategy to improve parsing performance
|
||||
/// for that input.</para>
|
||||
/// </summary>
|
||||
/// <seealso cref= ParserATNSimulator#reportAmbiguity </seealso>
|
||||
/// <seealso cref= ANTLRErrorListener#reportAmbiguity
|
||||
///
|
||||
/// @since 4.3 </seealso>
|
||||
class ANTLR4CPP_PUBLIC AmbiguityInfo : public DecisionEventInfo {
|
||||
public:
|
||||
/// The set of alternative numbers for this decision event that lead to a valid parse.
|
||||
antlrcpp::BitSet ambigAlts;
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new instance of the <seealso cref="AmbiguityInfo"/> class with the
|
||||
/// specified detailed ambiguity information.
|
||||
/// </summary>
|
||||
/// <param name="decision"> The decision number </param>
|
||||
/// <param name="configs"> The final configuration set identifying the ambiguous
|
||||
/// alternatives for the current input </param>
|
||||
/// <param name="ambigAlts"> The set of alternatives in the decision that lead to a valid parse.
|
||||
/// The predicted alt is the min(ambigAlts) </param>
|
||||
/// <param name="input"> The input token stream </param>
|
||||
/// <param name="startIndex"> The start index for the current prediction </param>
|
||||
/// <param name="stopIndex"> The index at which the ambiguity was identified during
|
||||
/// prediction </param>
|
||||
/// <param name="fullCtx"> {@code true} if the ambiguity was identified during LL
|
||||
/// prediction; otherwise, {@code false} if the ambiguity was identified
|
||||
/// during SLL prediction </param>
|
||||
AmbiguityInfo(size_t decision, ATNConfigSet *configs, const antlrcpp::BitSet &ambigAlts, TokenStream *input,
|
||||
size_t startIndex, size_t stopIndex, bool fullCtx);
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,33 +0,0 @@
|
||||
/* 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 "atn/Transition.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// TODO: make all transitions sets? no, should remove set edges.
|
||||
class ANTLR4CPP_PUBLIC AtomTransition final : public Transition {
|
||||
public:
|
||||
static bool is(const Transition &transition) { return transition.getTransitionType() == TransitionType::ATOM; }
|
||||
|
||||
static bool is(const Transition *transition) { return transition != nullptr && is(*transition); }
|
||||
|
||||
/// The token type or character value; or, signifies special label.
|
||||
/// TODO: rename this to label
|
||||
const size_t _label;
|
||||
|
||||
AtomTransition(ATNState *target, size_t label);
|
||||
|
||||
virtual misc::IntervalSet label() const override;
|
||||
virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override;
|
||||
|
||||
virtual std::string toString() const override;
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,14 +0,0 @@
|
||||
/* 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 "atn/ContextSensitivityInfo.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
|
||||
ContextSensitivityInfo::ContextSensitivityInfo(size_t decision, ATNConfigSet *configs, TokenStream *input,
|
||||
size_t startIndex, size_t stopIndex)
|
||||
: DecisionEventInfo(decision, configs, input, startIndex, stopIndex, true) {
|
||||
}
|
||||
@ -1,47 +0,0 @@
|
||||
/* 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 "atn/DecisionEventInfo.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// <summary>
|
||||
/// This class represents profiling event information for a context sensitivity.
|
||||
/// Context sensitivities are decisions where a particular input resulted in an
|
||||
/// SLL conflict, but LL prediction produced a single unique alternative.
|
||||
///
|
||||
/// <para>
|
||||
/// In some cases, the unique alternative identified by LL prediction is not
|
||||
/// equal to the minimum represented alternative in the conflicting SLL
|
||||
/// configuration set. Grammars and inputs which result in this scenario are
|
||||
/// unable to use <seealso cref="PredictionMode#SLL"/>, which in turn means they cannot use
|
||||
/// the two-stage parsing strategy to improve parsing performance for that
|
||||
/// input.</para>
|
||||
/// </summary>
|
||||
/// <seealso cref= ParserATNSimulator#reportContextSensitivity </seealso>
|
||||
/// <seealso cref= ANTLRErrorListener#reportContextSensitivity
|
||||
///
|
||||
/// @since 4.3 </seealso>
|
||||
class ANTLR4CPP_PUBLIC ContextSensitivityInfo : public DecisionEventInfo {
|
||||
public:
|
||||
/// <summary>
|
||||
/// Constructs a new instance of the <seealso cref="ContextSensitivityInfo"/> class
|
||||
/// with the specified detailed context sensitivity information.
|
||||
/// </summary>
|
||||
/// <param name="decision"> The decision number </param>
|
||||
/// <param name="configs"> The final configuration set containing the unique
|
||||
/// alternative identified by full-context prediction </param>
|
||||
/// <param name="input"> The input token stream </param>
|
||||
/// <param name="startIndex"> The start index for the current prediction </param>
|
||||
/// <param name="stopIndex"> The index at which the context sensitivity was
|
||||
/// identified during full-context prediction </param>
|
||||
ContextSensitivityInfo(size_t decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex, size_t stopIndex);
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,14 +0,0 @@
|
||||
/* 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 "atn/DecisionEventInfo.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
|
||||
DecisionEventInfo::DecisionEventInfo(size_t decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex,
|
||||
size_t stopIndex, bool fullCtx)
|
||||
: decision(decision), configs(configs), input(input), startIndex(startIndex), stopIndex(stopIndex), fullCtx(fullCtx) {
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
/* 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 atn {
|
||||
|
||||
/// <summary>
|
||||
/// This is the base class for gathering detailed information about prediction
|
||||
/// events which occur during parsing.
|
||||
///
|
||||
/// Note that we could record the parser call stack at the time this event
|
||||
/// occurred but in the presence of left recursive rules, the stack is kind of
|
||||
/// meaningless. It's better to look at the individual configurations for their
|
||||
/// individual stacks. Of course that is a <seealso cref="PredictionContext"/> object
|
||||
/// not a parse tree node and so it does not have information about the extent
|
||||
/// (start...stop) of the various subtrees. Examining the stack tops of all
|
||||
/// configurations provide the return states for the rule invocations.
|
||||
/// From there you can get the enclosing rule.
|
||||
///
|
||||
/// @since 4.3
|
||||
/// </summary>
|
||||
class ANTLR4CPP_PUBLIC DecisionEventInfo {
|
||||
public:
|
||||
/// <summary>
|
||||
/// The invoked decision number which this event is related to.
|
||||
/// </summary>
|
||||
/// <seealso cref= ATN#decisionToState </seealso>
|
||||
const size_t decision;
|
||||
|
||||
/// <summary>
|
||||
/// The configuration set containing additional information relevant to the
|
||||
/// prediction state when the current event occurred, or {@code null} if no
|
||||
/// additional information is relevant or available.
|
||||
/// </summary>
|
||||
const ATNConfigSet *configs;
|
||||
|
||||
/// <summary>
|
||||
/// The input token stream which is being parsed.
|
||||
/// </summary>
|
||||
const TokenStream *input;
|
||||
|
||||
/// <summary>
|
||||
/// The token index in the input stream at which the current prediction was
|
||||
/// originally invoked.
|
||||
/// </summary>
|
||||
const size_t startIndex;
|
||||
|
||||
/// <summary>
|
||||
/// The token index in the input stream at which the current event occurred.
|
||||
/// </summary>
|
||||
const size_t stopIndex;
|
||||
|
||||
/// <summary>
|
||||
/// {@code true} if the current event occurred during LL prediction;
|
||||
/// otherwise, {@code false} if the input occurred during SLL prediction.
|
||||
/// </summary>
|
||||
const bool fullCtx;
|
||||
|
||||
DecisionEventInfo(size_t decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex,
|
||||
size_t stopIndex, bool fullCtx);
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,25 +0,0 @@
|
||||
/* 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 "atn/ErrorInfo.h"
|
||||
#include "atn/LookaheadEventInfo.h"
|
||||
|
||||
#include "atn/DecisionInfo.h"
|
||||
|
||||
using namespace antlr4::atn;
|
||||
|
||||
DecisionInfo::DecisionInfo(size_t decision) : decision(decision) {
|
||||
}
|
||||
|
||||
std::string DecisionInfo::toString() const {
|
||||
std::stringstream ss;
|
||||
|
||||
ss << "{decision=" << decision << ", contextSensitivities=" << contextSensitivities.size() << ", errors=";
|
||||
ss << errors.size() << ", ambiguities=" << ambiguities.size() << ", SLL_lookahead=" << SLL_TotalLook;
|
||||
ss << ", SLL_ATNTransitions=" << SLL_ATNTransitions << ", SLL_DFATransitions=" << SLL_DFATransitions;
|
||||
ss << ", LL_Fallback=" << LL_Fallback << ", LL_lookahead=" << LL_TotalLook << ", LL_ATNTransitions=" << LL_ATNTransitions << '}';
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
@ -1,227 +0,0 @@
|
||||
/* 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 "atn/ContextSensitivityInfo.h"
|
||||
#include "atn/AmbiguityInfo.h"
|
||||
#include "atn/PredicateEvalInfo.h"
|
||||
#include "atn/ErrorInfo.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
class LookaheadEventInfo;
|
||||
|
||||
/// <summary>
|
||||
/// This class contains profiling gathered for a particular decision.
|
||||
///
|
||||
/// <para>
|
||||
/// Parsing performance in ANTLR 4 is heavily influenced by both static factors
|
||||
/// (e.g. the form of the rules in the grammar) and dynamic factors (e.g. the
|
||||
/// choice of input and the state of the DFA cache at the time profiling
|
||||
/// operations are started). For best results, gather and use aggregate
|
||||
/// statistics from a large sample of inputs representing the inputs expected in
|
||||
/// production before using the results to make changes in the grammar.</para>
|
||||
///
|
||||
/// @since 4.3
|
||||
/// </summary>
|
||||
class ANTLR4CPP_PUBLIC DecisionInfo {
|
||||
public:
|
||||
/// <summary>
|
||||
/// The decision number, which is an index into <seealso cref="ATN#decisionToState"/>.
|
||||
/// </summary>
|
||||
const size_t decision;
|
||||
|
||||
/// <summary>
|
||||
/// The total number of times <seealso cref="ParserATNSimulator#adaptivePredict"/> was
|
||||
/// invoked for this decision.
|
||||
/// </summary>
|
||||
long long invocations = 0;
|
||||
|
||||
/// <summary>
|
||||
/// The total time spent in <seealso cref="ParserATNSimulator#adaptivePredict"/> for
|
||||
/// this decision, in nanoseconds.
|
||||
///
|
||||
/// <para>
|
||||
/// The value of this field contains the sum of differential results obtained
|
||||
/// by <seealso cref="System#nanoTime()"/>, and is not adjusted to compensate for JIT
|
||||
/// and/or garbage collection overhead. For best accuracy, use a modern JVM
|
||||
/// implementation that provides precise results from
|
||||
/// <seealso cref="System#nanoTime()"/>, and perform profiling in a separate process
|
||||
/// which is warmed up by parsing the input prior to profiling. If desired,
|
||||
/// call <seealso cref="ATNSimulator#clearDFA"/> to reset the DFA cache to its initial
|
||||
/// state before starting the profiling measurement pass.</para>
|
||||
/// </summary>
|
||||
long long timeInPrediction = 0;
|
||||
|
||||
/// <summary>
|
||||
/// The sum of the lookahead required for SLL prediction for this decision.
|
||||
/// Note that SLL prediction is used before LL prediction for performance
|
||||
/// reasons even when <seealso cref="PredictionMode#LL"/> or
|
||||
/// <seealso cref="PredictionMode#LL_EXACT_AMBIG_DETECTION"/> is used.
|
||||
/// </summary>
|
||||
long long SLL_TotalLook = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the minimum lookahead required for any single SLL prediction to
|
||||
/// complete for this decision, by reaching a unique prediction, reaching an
|
||||
/// SLL conflict state, or encountering a syntax error.
|
||||
/// </summary>
|
||||
long long SLL_MinLook = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the maximum lookahead required for any single SLL prediction to
|
||||
/// complete for this decision, by reaching a unique prediction, reaching an
|
||||
/// SLL conflict state, or encountering a syntax error.
|
||||
/// </summary>
|
||||
long long SLL_MaxLook = 0;
|
||||
|
||||
/// Gets the <seealso cref="LookaheadEventInfo"/> associated with the event where the
|
||||
/// <seealso cref="#SLL_MaxLook"/> value was set.
|
||||
Ref<LookaheadEventInfo> SLL_MaxLookEvent;
|
||||
|
||||
/// <summary>
|
||||
/// The sum of the lookahead required for LL prediction for this decision.
|
||||
/// Note that LL prediction is only used when SLL prediction reaches a
|
||||
/// conflict state.
|
||||
/// </summary>
|
||||
long long LL_TotalLook = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the minimum lookahead required for any single LL prediction to
|
||||
/// complete for this decision. An LL prediction completes when the algorithm
|
||||
/// reaches a unique prediction, a conflict state (for
|
||||
/// <seealso cref="PredictionMode#LL"/>, an ambiguity state (for
|
||||
/// <seealso cref="PredictionMode#LL_EXACT_AMBIG_DETECTION"/>, or a syntax error.
|
||||
/// </summary>
|
||||
long long LL_MinLook = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the maximum lookahead required for any single LL prediction to
|
||||
/// complete for this decision. An LL prediction completes when the algorithm
|
||||
/// reaches a unique prediction, a conflict state (for
|
||||
/// <seealso cref="PredictionMode#LL"/>, an ambiguity state (for
|
||||
/// <seealso cref="PredictionMode#LL_EXACT_AMBIG_DETECTION"/>, or a syntax error.
|
||||
/// </summary>
|
||||
long long LL_MaxLook = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <seealso cref="LookaheadEventInfo"/> associated with the event where the
|
||||
/// <seealso cref="#LL_MaxLook"/> value was set.
|
||||
/// </summary>
|
||||
Ref<LookaheadEventInfo> LL_MaxLookEvent;
|
||||
|
||||
/// <summary>
|
||||
/// A collection of <seealso cref="ContextSensitivityInfo"/> instances describing the
|
||||
/// context sensitivities encountered during LL prediction for this decision.
|
||||
/// </summary>
|
||||
/// <seealso cref= ContextSensitivityInfo </seealso>
|
||||
std::vector<ContextSensitivityInfo> contextSensitivities;
|
||||
|
||||
/// <summary>
|
||||
/// A collection of <seealso cref="ErrorInfo"/> instances describing the parse errors
|
||||
/// identified during calls to <seealso cref="ParserATNSimulator#adaptivePredict"/> for
|
||||
/// this decision.
|
||||
/// </summary>
|
||||
/// <seealso cref= ErrorInfo </seealso>
|
||||
std::vector<ErrorInfo> errors;
|
||||
|
||||
/// <summary>
|
||||
/// A collection of <seealso cref="AmbiguityInfo"/> instances describing the
|
||||
/// ambiguities encountered during LL prediction for this decision.
|
||||
/// </summary>
|
||||
/// <seealso cref= AmbiguityInfo </seealso>
|
||||
std::vector<AmbiguityInfo> ambiguities;
|
||||
|
||||
/// <summary>
|
||||
/// A collection of <seealso cref="PredicateEvalInfo"/> instances describing the
|
||||
/// results of evaluating individual predicates during prediction for this
|
||||
/// decision.
|
||||
/// </summary>
|
||||
/// <seealso cref= PredicateEvalInfo </seealso>
|
||||
std::vector<PredicateEvalInfo> predicateEvals;
|
||||
|
||||
/// <summary>
|
||||
/// The total number of ATN transitions required during SLL prediction for
|
||||
/// this decision. An ATN transition is determined by the number of times the
|
||||
/// DFA does not contain an edge that is required for prediction, resulting
|
||||
/// in on-the-fly computation of that edge.
|
||||
///
|
||||
/// <para>
|
||||
/// If DFA caching of SLL transitions is employed by the implementation, ATN
|
||||
/// computation may cache the computed edge for efficient lookup during
|
||||
/// future parsing of this decision. Otherwise, the SLL parsing algorithm
|
||||
/// will use ATN transitions exclusively.</para>
|
||||
/// </summary>
|
||||
/// <seealso cref= #SLL_ATNTransitions </seealso>
|
||||
/// <seealso cref= ParserATNSimulator#computeTargetState </seealso>
|
||||
/// <seealso cref= LexerATNSimulator#computeTargetState </seealso>
|
||||
long long SLL_ATNTransitions = 0;
|
||||
|
||||
/// <summary>
|
||||
/// The total number of DFA transitions required during SLL prediction for
|
||||
/// this decision.
|
||||
///
|
||||
/// <para>If the ATN simulator implementation does not use DFA caching for SLL
|
||||
/// transitions, this value will be 0.</para>
|
||||
/// </summary>
|
||||
/// <seealso cref= ParserATNSimulator#getExistingTargetState </seealso>
|
||||
/// <seealso cref= LexerATNSimulator#getExistingTargetState </seealso>
|
||||
long long SLL_DFATransitions = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of times SLL prediction completed in a conflict
|
||||
/// state, resulting in fallback to LL prediction.
|
||||
///
|
||||
/// <para>Note that this value is not related to whether or not
|
||||
/// <seealso cref="PredictionMode#SLL"/> may be used successfully with a particular
|
||||
/// grammar. If the ambiguity resolution algorithm applied to the SLL
|
||||
/// conflicts for this decision produce the same result as LL prediction for
|
||||
/// this decision, <seealso cref="PredictionMode#SLL"/> would produce the same overall
|
||||
/// parsing result as <seealso cref="PredictionMode#LL"/>.</para>
|
||||
/// </summary>
|
||||
long long LL_Fallback = 0;
|
||||
|
||||
/// <summary>
|
||||
/// The total number of ATN transitions required during LL prediction for
|
||||
/// this decision. An ATN transition is determined by the number of times the
|
||||
/// DFA does not contain an edge that is required for prediction, resulting
|
||||
/// in on-the-fly computation of that edge.
|
||||
///
|
||||
/// <para>
|
||||
/// If DFA caching of LL transitions is employed by the implementation, ATN
|
||||
/// computation may cache the computed edge for efficient lookup during
|
||||
/// future parsing of this decision. Otherwise, the LL parsing algorithm will
|
||||
/// use ATN transitions exclusively.</para>
|
||||
/// </summary>
|
||||
/// <seealso cref= #LL_DFATransitions </seealso>
|
||||
/// <seealso cref= ParserATNSimulator#computeTargetState </seealso>
|
||||
/// <seealso cref= LexerATNSimulator#computeTargetState </seealso>
|
||||
long long LL_ATNTransitions = 0;
|
||||
|
||||
/// <summary>
|
||||
/// The total number of DFA transitions required during LL prediction for
|
||||
/// this decision.
|
||||
///
|
||||
/// <para>If the ATN simulator implementation does not use DFA caching for LL
|
||||
/// transitions, this value will be 0.</para>
|
||||
/// </summary>
|
||||
/// <seealso cref= ParserATNSimulator#getExistingTargetState </seealso>
|
||||
/// <seealso cref= LexerATNSimulator#getExistingTargetState </seealso>
|
||||
long long LL_DFATransitions = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new instance of the <seealso cref="DecisionInfo"/> class to contain
|
||||
/// statistics for a particular decision.
|
||||
/// </summary>
|
||||
/// <param name="decision"> The decision number </param>
|
||||
explicit DecisionInfo(size_t decision);
|
||||
|
||||
std::string toString() const;
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,15 +0,0 @@
|
||||
/* 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 "atn/ATNConfigSet.h"
|
||||
|
||||
#include "atn/ErrorInfo.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
|
||||
ErrorInfo::ErrorInfo(size_t decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex, size_t stopIndex, bool fullCtx)
|
||||
: DecisionEventInfo(decision, configs, input, startIndex, stopIndex, fullCtx) {
|
||||
}
|
||||
@ -1,43 +0,0 @@
|
||||
/* 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 "atn/DecisionEventInfo.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// <summary>
|
||||
/// This class represents profiling event information for a syntax error
|
||||
/// identified during prediction. Syntax errors occur when the prediction
|
||||
/// algorithm is unable to identify an alternative which would lead to a
|
||||
/// successful parse.
|
||||
/// </summary>
|
||||
/// <seealso cref= Parser#notifyErrorListeners(Token, String, RecognitionException) </seealso>
|
||||
/// <seealso cref= ANTLRErrorListener#syntaxError
|
||||
///
|
||||
/// @since 4.3 </seealso>
|
||||
class ANTLR4CPP_PUBLIC ErrorInfo : public DecisionEventInfo {
|
||||
public:
|
||||
/// <summary>
|
||||
/// Constructs a new instance of the <seealso cref="ErrorInfo"/> class with the
|
||||
/// specified detailed syntax error information.
|
||||
/// </summary>
|
||||
/// <param name="decision"> The decision number </param>
|
||||
/// <param name="configs"> The final configuration set reached during prediction
|
||||
/// prior to reaching the <seealso cref="ATNSimulator#ERROR"/> state </param>
|
||||
/// <param name="input"> The input token stream </param>
|
||||
/// <param name="startIndex"> The start index for the current prediction </param>
|
||||
/// <param name="stopIndex"> The index at which the syntax error was identified </param>
|
||||
/// <param name="fullCtx"> {@code true} if the syntax error was identified during LL
|
||||
/// prediction; otherwise, {@code false} if the syntax error was identified
|
||||
/// during SLL prediction </param>
|
||||
ErrorInfo(size_t decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex, size_t stopIndex,
|
||||
bool fullCtx);
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,18 +0,0 @@
|
||||
/* Copyright (c) 2022 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 <cstddef>
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
inline bool cachedHashCodeEqual(size_t lhs, size_t rhs) {
|
||||
return lhs == rhs || lhs == 0 || rhs == 0;
|
||||
}
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,621 +0,0 @@
|
||||
/* 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 "IntStream.h"
|
||||
#include "atn/OrderedATNConfigSet.h"
|
||||
#include "Token.h"
|
||||
#include "LexerNoViableAltException.h"
|
||||
#include "atn/RuleStopState.h"
|
||||
#include "atn/RuleTransition.h"
|
||||
#include "atn/SingletonPredictionContext.h"
|
||||
#include "atn/PredicateTransition.h"
|
||||
#include "atn/ActionTransition.h"
|
||||
#include "atn/TokensStartState.h"
|
||||
#include "misc/Interval.h"
|
||||
#include "dfa/DFA.h"
|
||||
#include "Lexer.h"
|
||||
#include "internal/Synchronization.h"
|
||||
|
||||
#include "dfa/DFAState.h"
|
||||
#include "atn/LexerATNConfig.h"
|
||||
#include "atn/LexerActionExecutor.h"
|
||||
|
||||
#include "atn/LexerATNSimulator.h"
|
||||
|
||||
#ifndef LEXER_DEBUG_ATN
|
||||
#define LEXER_DEBUG_ATN 0
|
||||
#endif
|
||||
#ifndef LEXER_DEBUG_DFA
|
||||
#define LEXER_DEBUG_DFA 0
|
||||
#endif
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
using namespace antlr4::internal;
|
||||
using namespace antlrcpp;
|
||||
|
||||
void LexerATNSimulator::SimState::reset() {
|
||||
*this = SimState();
|
||||
}
|
||||
|
||||
LexerATNSimulator::LexerATNSimulator(const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
|
||||
PredictionContextCache &sharedContextCache)
|
||||
: LexerATNSimulator(nullptr, atn, decisionToDFA, sharedContextCache) {
|
||||
}
|
||||
|
||||
LexerATNSimulator::LexerATNSimulator(Lexer *recog, const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
|
||||
PredictionContextCache &sharedContextCache)
|
||||
: ATNSimulator(atn, sharedContextCache), _recog(recog), _decisionToDFA(decisionToDFA) {
|
||||
InitializeInstanceFields();
|
||||
}
|
||||
|
||||
void LexerATNSimulator::copyState(LexerATNSimulator *simulator) {
|
||||
_charPositionInLine = simulator->_charPositionInLine;
|
||||
_line = simulator->_line;
|
||||
_mode = simulator->_mode;
|
||||
_startIndex = simulator->_startIndex;
|
||||
}
|
||||
|
||||
size_t LexerATNSimulator::match(CharStream *input, size_t mode) {
|
||||
_mode = mode;
|
||||
ssize_t mark = input->mark();
|
||||
|
||||
auto onExit = finally([input, mark] {
|
||||
input->release(mark);
|
||||
});
|
||||
|
||||
_startIndex = input->index();
|
||||
_prevAccept.reset();
|
||||
const dfa::DFA &dfa = _decisionToDFA[mode];
|
||||
dfa::DFAState* s0;
|
||||
{
|
||||
SharedLock<SharedMutex> stateLock(atn._stateMutex);
|
||||
s0 = dfa.s0;
|
||||
}
|
||||
if (s0 == nullptr) {
|
||||
return matchATN(input);
|
||||
} else {
|
||||
return execATN(input, s0);
|
||||
}
|
||||
}
|
||||
|
||||
void LexerATNSimulator::reset() {
|
||||
_prevAccept.reset();
|
||||
_startIndex = 0;
|
||||
_line = 1;
|
||||
_charPositionInLine = 0;
|
||||
_mode = Lexer::DEFAULT_MODE;
|
||||
}
|
||||
|
||||
void LexerATNSimulator::clearDFA() {
|
||||
size_t size = _decisionToDFA.size();
|
||||
_decisionToDFA.clear();
|
||||
for (size_t d = 0; d < size; ++d) {
|
||||
_decisionToDFA.emplace_back(atn.getDecisionState(d), d);
|
||||
}
|
||||
}
|
||||
|
||||
size_t LexerATNSimulator::matchATN(CharStream *input) {
|
||||
ATNState *startState = atn.modeToStartState[_mode];
|
||||
|
||||
std::unique_ptr<ATNConfigSet> s0_closure = computeStartState(input, startState);
|
||||
|
||||
bool suppressEdge = s0_closure->hasSemanticContext;
|
||||
s0_closure->hasSemanticContext = false;
|
||||
|
||||
dfa::DFAState *next = addDFAState(s0_closure.release(), suppressEdge);
|
||||
|
||||
size_t predict = execATN(input, next);
|
||||
|
||||
return predict;
|
||||
}
|
||||
|
||||
size_t LexerATNSimulator::execATN(CharStream *input, dfa::DFAState *ds0) {
|
||||
if (ds0->isAcceptState) {
|
||||
// allow zero-length tokens
|
||||
// ml: in Java code this method uses 3 params. The first is a member var of the class anyway (_prevAccept), so why pass it here?
|
||||
captureSimState(input, ds0);
|
||||
}
|
||||
|
||||
size_t t = input->LA(1);
|
||||
dfa::DFAState *s = ds0; // s is current/from DFA state
|
||||
|
||||
while (true) { // while more work
|
||||
// As we move src->trg, src->trg, we keep track of the previous trg to
|
||||
// avoid looking up the DFA state again, which is expensive.
|
||||
// If the previous target was already part of the DFA, we might
|
||||
// be able to avoid doing a reach operation upon t. If s!=null,
|
||||
// it means that semantic predicates didn't prevent us from
|
||||
// creating a DFA state. Once we know s!=null, we check to see if
|
||||
// the DFA state has an edge already for t. If so, we can just reuse
|
||||
// it's configuration set; there's no point in re-computing it.
|
||||
// This is kind of like doing DFA simulation within the ATN
|
||||
// simulation because DFA simulation is really just a way to avoid
|
||||
// computing reach/closure sets. Technically, once we know that
|
||||
// we have a previously added DFA state, we could jump over to
|
||||
// the DFA simulator. But, that would mean popping back and forth
|
||||
// a lot and making things more complicated algorithmically.
|
||||
// This optimization makes a lot of sense for loops within DFA.
|
||||
// A character will take us back to an existing DFA state
|
||||
// that already has lots of edges out of it. e.g., .* in comments.
|
||||
dfa::DFAState *target = getExistingTargetState(s, t);
|
||||
if (target == nullptr) {
|
||||
target = computeTargetState(input, s, t);
|
||||
}
|
||||
|
||||
if (target == ERROR.get()) {
|
||||
break;
|
||||
}
|
||||
|
||||
// If this is a consumable input element, make sure to consume before
|
||||
// capturing the accept state so the input index, line, and char
|
||||
// position accurately reflect the state of the interpreter at the
|
||||
// end of the token.
|
||||
if (t != Token::EOF) {
|
||||
consume(input);
|
||||
}
|
||||
|
||||
if (target->isAcceptState) {
|
||||
captureSimState(input, target);
|
||||
if (t == Token::EOF) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
t = input->LA(1);
|
||||
s = target; // flip; current DFA target becomes new src/from state
|
||||
}
|
||||
|
||||
return failOrAccept(input, s->configs.get(), t);
|
||||
}
|
||||
|
||||
dfa::DFAState *LexerATNSimulator::getExistingTargetState(dfa::DFAState *s, size_t t) {
|
||||
dfa::DFAState* retval = nullptr;
|
||||
SharedLock<SharedMutex> edgeLock(atn._edgeMutex);
|
||||
if (t <= MAX_DFA_EDGE) {
|
||||
auto iterator = s->edges.find(t - MIN_DFA_EDGE);
|
||||
#if LEXER_DEBUG_ATN == 1
|
||||
if (iterator != s->edges.end()) {
|
||||
std::cout << std::string("reuse state ") << s->stateNumber << std::string(" edge to ") << iterator->second->stateNumber << std::endl;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (iterator != s->edges.end())
|
||||
retval = iterator->second;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
dfa::DFAState *LexerATNSimulator::computeTargetState(CharStream *input, dfa::DFAState *s, size_t t) {
|
||||
OrderedATNConfigSet *reach = new OrderedATNConfigSet(); /* mem-check: deleted on error or managed by new DFA state. */
|
||||
|
||||
// if we don't find an existing DFA state
|
||||
// Fill reach starting from closure, following t transitions
|
||||
getReachableConfigSet(input, s->configs.get(), reach, t);
|
||||
|
||||
if (reach->isEmpty()) { // we got nowhere on t from s
|
||||
if (!reach->hasSemanticContext) {
|
||||
// we got nowhere on t, don't throw out this knowledge; it'd
|
||||
// cause a failover from DFA later.
|
||||
addDFAEdge(s, t, ERROR.get());
|
||||
}
|
||||
delete reach;
|
||||
|
||||
// stop when we can't match any more char
|
||||
return ERROR.get();
|
||||
}
|
||||
|
||||
// Add an edge from s to target DFA found/created for reach
|
||||
return addDFAEdge(s, t, reach);
|
||||
}
|
||||
|
||||
size_t LexerATNSimulator::failOrAccept(CharStream *input, ATNConfigSet *reach, size_t t) {
|
||||
if (_prevAccept.dfaState != nullptr) {
|
||||
accept(input, _prevAccept.dfaState->lexerActionExecutor, _startIndex, _prevAccept.index, _prevAccept.line, _prevAccept.charPos);
|
||||
return _prevAccept.dfaState->prediction;
|
||||
} else {
|
||||
// if no accept and EOF is first char, return EOF
|
||||
if (t == Token::EOF && input->index() == _startIndex) {
|
||||
return Token::EOF;
|
||||
}
|
||||
|
||||
throw LexerNoViableAltException(_recog, input, _startIndex, reach);
|
||||
}
|
||||
}
|
||||
|
||||
void LexerATNSimulator::getReachableConfigSet(CharStream *input, ATNConfigSet *closure_, ATNConfigSet *reach, size_t t) {
|
||||
// this is used to skip processing for configs which have a lower priority
|
||||
// than a config that already reached an accept state for the same rule
|
||||
size_t skipAlt = ATN::INVALID_ALT_NUMBER;
|
||||
|
||||
for (const auto &c : closure_->configs) {
|
||||
bool currentAltReachedAcceptState = c->alt == skipAlt;
|
||||
if (currentAltReachedAcceptState && (std::static_pointer_cast<LexerATNConfig>(c))->hasPassedThroughNonGreedyDecision()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
#if LEXER_DEBUG_ATN == 1
|
||||
std::cout << "testing " << getTokenName((int)t) << " at " << c->toString(true) << std::endl;
|
||||
#endif
|
||||
|
||||
size_t n = c->state->transitions.size();
|
||||
for (size_t ti = 0; ti < n; ti++) { // for each transition
|
||||
const Transition *trans = c->state->transitions[ti].get();
|
||||
ATNState *target = getReachableTarget(trans, (int)t);
|
||||
if (target != nullptr) {
|
||||
auto lexerActionExecutor = downCast<const LexerATNConfig&>(*c).getLexerActionExecutor();
|
||||
if (lexerActionExecutor != nullptr) {
|
||||
lexerActionExecutor = lexerActionExecutor->fixOffsetBeforeMatch((int)input->index() - (int)_startIndex);
|
||||
}
|
||||
|
||||
bool treatEofAsEpsilon = t == Token::EOF;
|
||||
Ref<LexerATNConfig> config = std::make_shared<LexerATNConfig>(downCast<const LexerATNConfig&>(*c),
|
||||
target, std::move(lexerActionExecutor));
|
||||
|
||||
if (closure(input, config, reach, currentAltReachedAcceptState, true, treatEofAsEpsilon)) {
|
||||
// any remaining configs for this alt have a lower priority than
|
||||
// the one that just reached an accept state.
|
||||
skipAlt = c->alt;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LexerATNSimulator::accept(CharStream *input, const Ref<const LexerActionExecutor> &lexerActionExecutor, size_t /*startIndex*/,
|
||||
size_t index, size_t line, size_t charPos) {
|
||||
#if LEXER_DEBUG_ATN == 1
|
||||
std::cout << "ACTION ";
|
||||
std::cout << toString(lexerActionExecutor) << std::endl;
|
||||
#endif
|
||||
|
||||
// seek to after last char in token
|
||||
input->seek(index);
|
||||
_line = line;
|
||||
_charPositionInLine = (int)charPos;
|
||||
|
||||
if (lexerActionExecutor != nullptr && _recog != nullptr) {
|
||||
lexerActionExecutor->execute(_recog, input, _startIndex);
|
||||
}
|
||||
}
|
||||
|
||||
atn::ATNState *LexerATNSimulator::getReachableTarget(const Transition *trans, size_t t) {
|
||||
if (trans->matches(t, Lexer::MIN_CHAR_VALUE, Lexer::MAX_CHAR_VALUE)) {
|
||||
return trans->target;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<ATNConfigSet> LexerATNSimulator::computeStartState(CharStream *input, ATNState *p) {
|
||||
Ref<const PredictionContext> initialContext = PredictionContext::EMPTY; // ml: the purpose of this assignment is unclear
|
||||
std::unique_ptr<ATNConfigSet> configs(new OrderedATNConfigSet());
|
||||
for (size_t i = 0; i < p->transitions.size(); i++) {
|
||||
ATNState *target = p->transitions[i]->target;
|
||||
Ref<LexerATNConfig> c = std::make_shared<LexerATNConfig>(target, (int)(i + 1), initialContext);
|
||||
closure(input, c, configs.get(), false, false, false);
|
||||
}
|
||||
|
||||
return configs;
|
||||
}
|
||||
|
||||
bool LexerATNSimulator::closure(CharStream *input, const Ref<LexerATNConfig> &config, ATNConfigSet *configs,
|
||||
bool currentAltReachedAcceptState, bool speculative, bool treatEofAsEpsilon) {
|
||||
#if LEXER_DEBUG_ATN == 1
|
||||
std::cout << "closure(" << config->toString(true) << ")" << std::endl;
|
||||
#endif
|
||||
|
||||
if (config->state != nullptr && config->state->getStateType() == ATNStateType::RULE_STOP) {
|
||||
#if LEXER_DEBUG_ATN == 1
|
||||
if (_recog != nullptr) {
|
||||
std::cout << "closure at " << _recog->getRuleNames()[config->state->ruleIndex] << " rule stop " << config << std::endl;
|
||||
} else {
|
||||
std::cout << "closure at rule stop " << config << std::endl;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (config->context == nullptr || config->context->hasEmptyPath()) {
|
||||
if (config->context == nullptr || config->context->isEmpty()) {
|
||||
configs->add(config);
|
||||
return true;
|
||||
} else {
|
||||
configs->add(std::make_shared<LexerATNConfig>(*config, config->state, PredictionContext::EMPTY));
|
||||
currentAltReachedAcceptState = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (config->context != nullptr && !config->context->isEmpty()) {
|
||||
for (size_t i = 0; i < config->context->size(); i++) {
|
||||
if (config->context->getReturnState(i) != PredictionContext::EMPTY_RETURN_STATE) {
|
||||
Ref<const PredictionContext> newContext = config->context->getParent(i); // "pop" return state
|
||||
ATNState *returnState = atn.states[config->context->getReturnState(i)];
|
||||
Ref<LexerATNConfig> c = std::make_shared<LexerATNConfig>(*config, returnState, newContext);
|
||||
currentAltReachedAcceptState = closure(input, c, configs, currentAltReachedAcceptState, speculative, treatEofAsEpsilon);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return currentAltReachedAcceptState;
|
||||
}
|
||||
|
||||
// optimization
|
||||
if (!config->state->epsilonOnlyTransitions) {
|
||||
if (!currentAltReachedAcceptState || !config->hasPassedThroughNonGreedyDecision()) {
|
||||
configs->add(config);
|
||||
}
|
||||
}
|
||||
|
||||
ATNState *p = config->state;
|
||||
for (size_t i = 0; i < p->transitions.size(); i++) {
|
||||
const Transition *t = p->transitions[i].get();
|
||||
Ref<LexerATNConfig> c = getEpsilonTarget(input, config, t, configs, speculative, treatEofAsEpsilon);
|
||||
if (c != nullptr) {
|
||||
currentAltReachedAcceptState = closure(input, c, configs, currentAltReachedAcceptState, speculative, treatEofAsEpsilon);
|
||||
}
|
||||
}
|
||||
|
||||
return currentAltReachedAcceptState;
|
||||
}
|
||||
|
||||
Ref<LexerATNConfig> LexerATNSimulator::getEpsilonTarget(CharStream *input, const Ref<LexerATNConfig> &config, const Transition *t,
|
||||
ATNConfigSet *configs, bool speculative, bool treatEofAsEpsilon) {
|
||||
|
||||
Ref<LexerATNConfig> c = nullptr;
|
||||
switch (t->getTransitionType()) {
|
||||
case TransitionType::RULE: {
|
||||
const RuleTransition *ruleTransition = static_cast<const RuleTransition*>(t);
|
||||
Ref<const PredictionContext> newContext = SingletonPredictionContext::create(config->context, ruleTransition->followState->stateNumber);
|
||||
c = std::make_shared<LexerATNConfig>(*config, t->target, newContext);
|
||||
break;
|
||||
}
|
||||
|
||||
case TransitionType::PRECEDENCE:
|
||||
throw UnsupportedOperationException("Precedence predicates are not supported in lexers.");
|
||||
|
||||
case TransitionType::PREDICATE: {
|
||||
/* Track traversing semantic predicates. If we traverse,
|
||||
we cannot add a DFA state for this "reach" computation
|
||||
because the DFA would not test the predicate again in the
|
||||
future. Rather than creating collections of semantic predicates
|
||||
like v3 and testing them on prediction, v4 will test them on the
|
||||
fly all the time using the ATN not the DFA. This is slower but
|
||||
semantically it's not used that often. One of the key elements to
|
||||
this predicate mechanism is not adding DFA states that see
|
||||
predicates immediately afterwards in the ATN. For example,
|
||||
|
||||
a : ID {p1}? | ID {p2}? ;
|
||||
|
||||
should create the start state for rule 'a' (to save start state
|
||||
competition), but should not create target of ID state. The
|
||||
collection of ATN states the following ID references includes
|
||||
states reached by traversing predicates. Since this is when we
|
||||
test them, we cannot cash the DFA state target of ID.
|
||||
*/
|
||||
const PredicateTransition *pt = static_cast<const PredicateTransition*>(t);
|
||||
|
||||
#if LEXER_DEBUG_ATN == 1
|
||||
std::cout << "EVAL rule " << pt->getRuleIndex() << ":" << pt->getPredIndex() << std::endl;
|
||||
#endif
|
||||
|
||||
configs->hasSemanticContext = true;
|
||||
if (evaluatePredicate(input, pt->getRuleIndex(), pt->getPredIndex(), speculative)) {
|
||||
c = std::make_shared<LexerATNConfig>(*config, t->target);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case TransitionType::ACTION:
|
||||
if (config->context == nullptr|| config->context->hasEmptyPath()) {
|
||||
// execute actions anywhere in the start rule for a token.
|
||||
//
|
||||
// TODO: if the entry rule is invoked recursively, some
|
||||
// actions may be executed during the recursive call. The
|
||||
// problem can appear when hasEmptyPath() is true but
|
||||
// isEmpty() is false. In this case, the config needs to be
|
||||
// split into two contexts - one with just the empty path
|
||||
// and another with everything but the empty path.
|
||||
// Unfortunately, the current algorithm does not allow
|
||||
// getEpsilonTarget to return two configurations, so
|
||||
// additional modifications are needed before we can support
|
||||
// the split operation.
|
||||
auto lexerActionExecutor = LexerActionExecutor::append(config->getLexerActionExecutor(),
|
||||
atn.lexerActions[static_cast<const ActionTransition *>(t)->actionIndex]);
|
||||
c = std::make_shared<LexerATNConfig>(*config, t->target, std::move(lexerActionExecutor));
|
||||
break;
|
||||
}
|
||||
else {
|
||||
// ignore actions in referenced rules
|
||||
c = std::make_shared<LexerATNConfig>(*config, t->target);
|
||||
break;
|
||||
}
|
||||
|
||||
case TransitionType::EPSILON:
|
||||
c = std::make_shared<LexerATNConfig>(*config, t->target);
|
||||
break;
|
||||
|
||||
case TransitionType::ATOM:
|
||||
case TransitionType::RANGE:
|
||||
case TransitionType::SET:
|
||||
if (treatEofAsEpsilon) {
|
||||
if (t->matches(Token::EOF, Lexer::MIN_CHAR_VALUE, Lexer::MAX_CHAR_VALUE)) {
|
||||
c = std::make_shared<LexerATNConfig>(*config, t->target);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default: // To silence the compiler. Other transition types are not used here.
|
||||
break;
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
bool LexerATNSimulator::evaluatePredicate(CharStream *input, size_t ruleIndex, size_t predIndex, bool speculative) {
|
||||
// assume true if no recognizer was provided
|
||||
if (_recog == nullptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!speculative) {
|
||||
return _recog->sempred(nullptr, ruleIndex, predIndex);
|
||||
}
|
||||
|
||||
size_t savedCharPositionInLine = _charPositionInLine;
|
||||
size_t savedLine = _line;
|
||||
size_t index = input->index();
|
||||
ssize_t marker = input->mark();
|
||||
|
||||
auto onExit = finally([this, input, savedCharPositionInLine, savedLine, index, marker] {
|
||||
_charPositionInLine = savedCharPositionInLine;
|
||||
_line = savedLine;
|
||||
input->seek(index);
|
||||
input->release(marker);
|
||||
});
|
||||
|
||||
consume(input);
|
||||
return _recog->sempred(nullptr, ruleIndex, predIndex);
|
||||
}
|
||||
|
||||
void LexerATNSimulator::captureSimState(CharStream *input, dfa::DFAState *dfaState) {
|
||||
_prevAccept.index = input->index();
|
||||
_prevAccept.line = _line;
|
||||
_prevAccept.charPos = _charPositionInLine;
|
||||
_prevAccept.dfaState = dfaState;
|
||||
}
|
||||
|
||||
dfa::DFAState *LexerATNSimulator::addDFAEdge(dfa::DFAState *from, size_t t, ATNConfigSet *q) {
|
||||
/* leading to this call, ATNConfigSet.hasSemanticContext is used as a
|
||||
* marker indicating dynamic predicate evaluation makes this edge
|
||||
* dependent on the specific input sequence, so the static edge in the
|
||||
* DFA should be omitted. The target DFAState is still created since
|
||||
* execATN has the ability to resynchronize with the DFA state cache
|
||||
* following the predicate evaluation step.
|
||||
*
|
||||
* TJP notes: next time through the DFA, we see a pred again and eval.
|
||||
* If that gets us to a previously created (but dangling) DFA
|
||||
* state, we can continue in pure DFA mode from there.
|
||||
*/
|
||||
bool suppressEdge = q->hasSemanticContext;
|
||||
q->hasSemanticContext = false;
|
||||
|
||||
dfa::DFAState *to = addDFAState(q);
|
||||
|
||||
if (suppressEdge) {
|
||||
return to;
|
||||
}
|
||||
|
||||
addDFAEdge(from, t, to);
|
||||
return to;
|
||||
}
|
||||
|
||||
void LexerATNSimulator::addDFAEdge(dfa::DFAState *p, size_t t, dfa::DFAState *q) {
|
||||
if (/*t < MIN_DFA_EDGE ||*/ t > MAX_DFA_EDGE) { // MIN_DFA_EDGE is 0
|
||||
// Only track edges within the DFA bounds
|
||||
return;
|
||||
}
|
||||
|
||||
UniqueLock<SharedMutex> edgeLock(atn._edgeMutex);
|
||||
p->edges[t - MIN_DFA_EDGE] = q; // connect
|
||||
}
|
||||
|
||||
dfa::DFAState *LexerATNSimulator::addDFAState(ATNConfigSet *configs) {
|
||||
return addDFAState(configs, true);
|
||||
}
|
||||
|
||||
dfa::DFAState *LexerATNSimulator::addDFAState(ATNConfigSet *configs, bool suppressEdge) {
|
||||
/* the lexer evaluates predicates on-the-fly; by this point configs
|
||||
* should not contain any configurations with unevaluated predicates.
|
||||
*/
|
||||
assert(!configs->hasSemanticContext);
|
||||
|
||||
dfa::DFAState *proposed = new dfa::DFAState(std::unique_ptr<ATNConfigSet>(configs)); /* mem-check: managed by the DFA or deleted below */
|
||||
Ref<ATNConfig> firstConfigWithRuleStopState = nullptr;
|
||||
for (const auto &c : configs->configs) {
|
||||
if (RuleStopState::is(c->state)) {
|
||||
firstConfigWithRuleStopState = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (firstConfigWithRuleStopState != nullptr) {
|
||||
proposed->isAcceptState = true;
|
||||
proposed->lexerActionExecutor = downCast<const LexerATNConfig&>(*firstConfigWithRuleStopState).getLexerActionExecutor();
|
||||
proposed->prediction = atn.ruleToTokenType[firstConfigWithRuleStopState->state->ruleIndex];
|
||||
}
|
||||
|
||||
dfa::DFA &dfa = _decisionToDFA[_mode];
|
||||
|
||||
{
|
||||
UniqueLock<SharedMutex> stateLock(atn._stateMutex);
|
||||
auto [existing, inserted] = dfa.states.insert(proposed);
|
||||
if (!inserted) {
|
||||
delete proposed;
|
||||
proposed = *existing;
|
||||
} else {
|
||||
// Previously we did a lookup, then set fields, then inserted. It was `dfa.states.size()`,
|
||||
// since we already inserted we need to subtract one.
|
||||
proposed->stateNumber = static_cast<int>(dfa.states.size() - 1);
|
||||
proposed->configs->setReadonly(true);
|
||||
}
|
||||
if (!suppressEdge) {
|
||||
dfa.s0 = proposed;
|
||||
}
|
||||
}
|
||||
|
||||
return proposed;
|
||||
}
|
||||
|
||||
dfa::DFA& LexerATNSimulator::getDFA(size_t mode) {
|
||||
return _decisionToDFA[mode];
|
||||
}
|
||||
|
||||
std::string LexerATNSimulator::getText(CharStream *input) {
|
||||
// index is first lookahead char, don't include.
|
||||
return input->getText(misc::Interval(_startIndex, input->index() - 1));
|
||||
}
|
||||
|
||||
size_t LexerATNSimulator::getLine() const {
|
||||
return _line;
|
||||
}
|
||||
|
||||
void LexerATNSimulator::setLine(size_t line) {
|
||||
_line = line;
|
||||
}
|
||||
|
||||
size_t LexerATNSimulator::getCharPositionInLine() {
|
||||
return _charPositionInLine;
|
||||
}
|
||||
|
||||
void LexerATNSimulator::setCharPositionInLine(size_t charPositionInLine) {
|
||||
_charPositionInLine = charPositionInLine;
|
||||
}
|
||||
|
||||
void LexerATNSimulator::consume(CharStream *input) {
|
||||
size_t curChar = input->LA(1);
|
||||
if (curChar == '\n') {
|
||||
_line++;
|
||||
_charPositionInLine = 0;
|
||||
} else {
|
||||
_charPositionInLine++;
|
||||
}
|
||||
input->consume();
|
||||
}
|
||||
|
||||
std::string LexerATNSimulator::getTokenName(size_t t) {
|
||||
if (t == Token::EOF) {
|
||||
return "EOF";
|
||||
}
|
||||
return std::string("'") + static_cast<char>(t) + std::string("'");
|
||||
}
|
||||
|
||||
void LexerATNSimulator::InitializeInstanceFields() {
|
||||
_startIndex = 0;
|
||||
_line = 1;
|
||||
_charPositionInLine = 0;
|
||||
_mode = antlr4::Lexer::DEFAULT_MODE;
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
#include "LexerAction.h"
|
||||
|
||||
using namespace antlr4::atn;
|
||||
|
||||
size_t LexerAction::hashCode() const {
|
||||
auto hash = cachedHashCode();
|
||||
if (hash == 0) {
|
||||
hash = hashCodeImpl();
|
||||
if (hash == 0) {
|
||||
hash = std::numeric_limits<size_t>::max();
|
||||
}
|
||||
_hashCode.store(hash, std::memory_order_relaxed);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
@ -1,100 +0,0 @@
|
||||
/* 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 "atn/LexerActionType.h"
|
||||
#include "antlr4-common.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// <summary>
|
||||
/// Represents a single action which can be executed following the successful
|
||||
/// match of a lexer rule. Lexer actions are used for both embedded action syntax
|
||||
/// and ANTLR 4's new lexer command syntax.
|
||||
///
|
||||
/// @author Sam Harwell
|
||||
/// @since 4.2
|
||||
/// </summary>
|
||||
class ANTLR4CPP_PUBLIC LexerAction {
|
||||
public:
|
||||
virtual ~LexerAction() = default;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the serialization type of the lexer action.
|
||||
/// </summary>
|
||||
/// <returns> The serialization type of the lexer action. </returns>
|
||||
///
|
||||
/// IMPORTANT: Unlike Java, this returns LexerActionType::INDEXED_CUSTOM for instances of
|
||||
/// LexerIndexedCustomAction. If you need the wrapped action type, use
|
||||
/// LexerIndexedCustomAction::getAction()->getActionType().
|
||||
LexerActionType getActionType() const { return _actionType; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the lexer action is position-dependent. Position-dependent
|
||||
/// actions may have different semantics depending on the <seealso cref="CharStream"/>
|
||||
/// index at the time the action is executed.
|
||||
///
|
||||
/// <para>Many lexer commands, including {@code type}, {@code skip}, and
|
||||
/// {@code more}, do not check the input index during their execution.
|
||||
/// Actions like this are position-independent, and may be stored more
|
||||
/// efficiently as part of the <seealso cref="LexerATNConfig#lexerActionExecutor"/>.</para>
|
||||
/// </summary>
|
||||
/// <returns> {@code true} if the lexer action semantics can be affected by the
|
||||
/// position of the input <seealso cref="CharStream"/> at the time it is executed;
|
||||
/// otherwise, {@code false}. </returns>
|
||||
bool isPositionDependent() const { return _positionDependent; }
|
||||
|
||||
/// <summary>
|
||||
/// Execute the lexer action in the context of the specified <seealso cref="Lexer"/>.
|
||||
///
|
||||
/// <para>For position-dependent actions, the input stream must already be
|
||||
/// positioned correctly prior to calling this method.</para>
|
||||
/// </summary>
|
||||
/// <param name="lexer"> The lexer instance. </param>
|
||||
virtual void execute(Lexer *lexer) const = 0;
|
||||
|
||||
size_t hashCode() const;
|
||||
|
||||
virtual bool equals(const LexerAction &other) const = 0;
|
||||
|
||||
virtual std::string toString() const = 0;
|
||||
|
||||
protected:
|
||||
LexerAction(LexerActionType actionType, bool positionDependent)
|
||||
: _actionType(actionType), _hashCode(0), _positionDependent(positionDependent) {}
|
||||
|
||||
virtual size_t hashCodeImpl() const = 0;
|
||||
|
||||
size_t cachedHashCode() const { return _hashCode.load(std::memory_order_relaxed); }
|
||||
|
||||
private:
|
||||
const LexerActionType _actionType;
|
||||
mutable std::atomic<size_t> _hashCode;
|
||||
const bool _positionDependent;
|
||||
};
|
||||
|
||||
inline bool operator==(const LexerAction &lhs, const LexerAction &rhs) {
|
||||
return lhs.equals(rhs);
|
||||
}
|
||||
|
||||
inline bool operator!=(const LexerAction &lhs, const LexerAction &rhs) {
|
||||
return !operator==(lhs, rhs);
|
||||
}
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
|
||||
namespace std {
|
||||
|
||||
template <>
|
||||
struct hash<::antlr4::atn::LexerAction> {
|
||||
size_t operator()(const ::antlr4::atn::LexerAction &lexerAction) const {
|
||||
return lexerAction.hashCode();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
@ -1,108 +0,0 @@
|
||||
/* 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 "misc/MurmurHash.h"
|
||||
#include "atn/LexerIndexedCustomAction.h"
|
||||
#include "atn/HashUtils.h"
|
||||
#include "support/CPPUtils.h"
|
||||
#include "support/Arrays.h"
|
||||
#include "support/Casts.h"
|
||||
|
||||
#include "atn/LexerActionExecutor.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
using namespace antlr4::misc;
|
||||
using namespace antlrcpp;
|
||||
|
||||
namespace {
|
||||
|
||||
bool lexerActionEqual(const Ref<const LexerAction> &lhs, const Ref<const LexerAction> &rhs) {
|
||||
return *lhs == *rhs;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
LexerActionExecutor::LexerActionExecutor(std::vector<Ref<const LexerAction>> lexerActions)
|
||||
: _lexerActions(std::move(lexerActions)), _hashCode(0) {}
|
||||
|
||||
Ref<const LexerActionExecutor> LexerActionExecutor::append(const Ref<const LexerActionExecutor> &lexerActionExecutor,
|
||||
Ref<const LexerAction> lexerAction) {
|
||||
if (lexerActionExecutor == nullptr) {
|
||||
return std::make_shared<LexerActionExecutor>(std::vector<Ref<const LexerAction>>{ std::move(lexerAction) });
|
||||
}
|
||||
std::vector<Ref<const LexerAction>> lexerActions;
|
||||
lexerActions.reserve(lexerActionExecutor->_lexerActions.size() + 1);
|
||||
lexerActions.insert(lexerActions.begin(), lexerActionExecutor->_lexerActions.begin(), lexerActionExecutor->_lexerActions.end());
|
||||
lexerActions.push_back(std::move(lexerAction));
|
||||
return std::make_shared<LexerActionExecutor>(std::move(lexerActions));
|
||||
}
|
||||
|
||||
Ref<const LexerActionExecutor> LexerActionExecutor::fixOffsetBeforeMatch(int offset) const {
|
||||
std::vector<Ref<const LexerAction>> updatedLexerActions;
|
||||
for (size_t i = 0; i < _lexerActions.size(); i++) {
|
||||
if (_lexerActions[i]->isPositionDependent() && !LexerIndexedCustomAction::is(*_lexerActions[i])) {
|
||||
if (updatedLexerActions.empty()) {
|
||||
updatedLexerActions = _lexerActions; // Make a copy.
|
||||
}
|
||||
updatedLexerActions[i] = std::make_shared<LexerIndexedCustomAction>(offset, _lexerActions[i]);
|
||||
}
|
||||
}
|
||||
if (updatedLexerActions.empty()) {
|
||||
return shared_from_this();
|
||||
}
|
||||
return std::make_shared<LexerActionExecutor>(std::move(updatedLexerActions));
|
||||
}
|
||||
|
||||
const std::vector<Ref<const LexerAction>>& LexerActionExecutor::getLexerActions() const {
|
||||
return _lexerActions;
|
||||
}
|
||||
|
||||
void LexerActionExecutor::execute(Lexer *lexer, CharStream *input, size_t startIndex) const {
|
||||
bool requiresSeek = false;
|
||||
size_t stopIndex = input->index();
|
||||
|
||||
auto onExit = finally([requiresSeek, input, stopIndex]() {
|
||||
if (requiresSeek) {
|
||||
input->seek(stopIndex);
|
||||
}
|
||||
});
|
||||
for (const auto &lexerAction : _lexerActions) {
|
||||
if (LexerIndexedCustomAction::is(*lexerAction)) {
|
||||
int offset = downCast<const LexerIndexedCustomAction&>(*lexerAction).getOffset();
|
||||
input->seek(startIndex + offset);
|
||||
requiresSeek = (startIndex + offset) != stopIndex;
|
||||
} else if (lexerAction->isPositionDependent()) {
|
||||
input->seek(stopIndex);
|
||||
requiresSeek = false;
|
||||
}
|
||||
lexerAction->execute(lexer);
|
||||
}
|
||||
}
|
||||
|
||||
size_t LexerActionExecutor::hashCode() const {
|
||||
auto hash = _hashCode.load(std::memory_order_relaxed);
|
||||
if (hash == 0) {
|
||||
hash = MurmurHash::initialize();
|
||||
for (const auto &lexerAction : _lexerActions) {
|
||||
hash = MurmurHash::update(hash, lexerAction);
|
||||
}
|
||||
hash = MurmurHash::finish(hash, _lexerActions.size());
|
||||
if (hash == 0) {
|
||||
hash = std::numeric_limits<size_t>::max();
|
||||
}
|
||||
_hashCode.store(hash, std::memory_order_relaxed);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
bool LexerActionExecutor::equals(const LexerActionExecutor &other) const {
|
||||
if (this == std::addressof(other)) {
|
||||
return true;
|
||||
}
|
||||
return cachedHashCodeEqual(_hashCode.load(std::memory_order_relaxed), other._hashCode.load(std::memory_order_relaxed)) &&
|
||||
_lexerActions.size() == other._lexerActions.size() &&
|
||||
std::equal(_lexerActions.begin(), _lexerActions.end(), other._lexerActions.begin(), lexerActionEqual);
|
||||
}
|
||||
@ -1,128 +0,0 @@
|
||||
/* 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 "CharStream.h"
|
||||
#include "atn/LexerAction.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// Represents an executor for a sequence of lexer actions which traversed during
|
||||
/// the matching operation of a lexer rule (token).
|
||||
///
|
||||
/// <para>The executor tracks position information for position-dependent lexer actions
|
||||
/// efficiently, ensuring that actions appearing only at the end of the rule do
|
||||
/// not cause bloating of the <seealso cref="DFA"/> created for the lexer.</para>
|
||||
class ANTLR4CPP_PUBLIC LexerActionExecutor final : public std::enable_shared_from_this<LexerActionExecutor> {
|
||||
public:
|
||||
/// <summary>
|
||||
/// Constructs an executor for a sequence of <seealso cref="LexerAction"/> actions. </summary>
|
||||
/// <param name="lexerActions"> The lexer actions to execute. </param>
|
||||
explicit LexerActionExecutor(std::vector<Ref<const LexerAction>> lexerActions);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <seealso cref="LexerActionExecutor"/> which executes the actions for
|
||||
/// the input {@code lexerActionExecutor} followed by a specified
|
||||
/// {@code lexerAction}.
|
||||
/// </summary>
|
||||
/// <param name="lexerActionExecutor"> The executor for actions already traversed by
|
||||
/// the lexer while matching a token within a particular
|
||||
/// <seealso cref="LexerATNConfig"/>. If this is {@code null}, the method behaves as
|
||||
/// though it were an empty executor. </param>
|
||||
/// <param name="lexerAction"> The lexer action to execute after the actions
|
||||
/// specified in {@code lexerActionExecutor}.
|
||||
/// </param>
|
||||
/// <returns> A <seealso cref="LexerActionExecutor"/> for executing the combine actions
|
||||
/// of {@code lexerActionExecutor} and {@code lexerAction}. </returns>
|
||||
static Ref<const LexerActionExecutor> append(const Ref<const LexerActionExecutor> &lexerActionExecutor,
|
||||
Ref<const LexerAction> lexerAction);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <seealso cref="LexerActionExecutor"/> which encodes the current offset
|
||||
/// for position-dependent lexer actions.
|
||||
///
|
||||
/// <para>Normally, when the executor encounters lexer actions where
|
||||
/// <seealso cref="LexerAction#isPositionDependent"/> returns {@code true}, it calls
|
||||
/// <seealso cref="IntStream#seek"/> on the input <seealso cref="CharStream"/> to set the input
|
||||
/// position to the <em>end</em> of the current token. This behavior provides
|
||||
/// for efficient DFA representation of lexer actions which appear at the end
|
||||
/// of a lexer rule, even when the lexer rule matches a variable number of
|
||||
/// characters.</para>
|
||||
///
|
||||
/// <para>Prior to traversing a match transition in the ATN, the current offset
|
||||
/// from the token start index is assigned to all position-dependent lexer
|
||||
/// actions which have not already been assigned a fixed offset. By storing
|
||||
/// the offsets relative to the token start index, the DFA representation of
|
||||
/// lexer actions which appear in the middle of tokens remains efficient due
|
||||
/// to sharing among tokens of the same length, regardless of their absolute
|
||||
/// position in the input stream.</para>
|
||||
///
|
||||
/// <para>If the current executor already has offsets assigned to all
|
||||
/// position-dependent lexer actions, the method returns {@code this}.</para>
|
||||
/// </summary>
|
||||
/// <param name="offset"> The current offset to assign to all position-dependent
|
||||
/// lexer actions which do not already have offsets assigned.
|
||||
/// </param>
|
||||
/// <returns> A <seealso cref="LexerActionExecutor"/> which stores input stream offsets
|
||||
/// for all position-dependent lexer actions. </returns>
|
||||
Ref<const LexerActionExecutor> fixOffsetBeforeMatch(int offset) const;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the lexer actions to be executed by this executor. </summary>
|
||||
/// <returns> The lexer actions to be executed by this executor. </returns>
|
||||
const std::vector<Ref<const LexerAction>>& getLexerActions() const;
|
||||
|
||||
/// <summary>
|
||||
/// Execute the actions encapsulated by this executor within the context of a
|
||||
/// particular <seealso cref="Lexer"/>.
|
||||
///
|
||||
/// <para>This method calls <seealso cref="IntStream#seek"/> to set the position of the
|
||||
/// {@code input} <seealso cref="CharStream"/> prior to calling
|
||||
/// <seealso cref="LexerAction#execute"/> on a position-dependent action. Before the
|
||||
/// method returns, the input position will be restored to the same position
|
||||
/// it was in when the method was invoked.</para>
|
||||
/// </summary>
|
||||
/// <param name="lexer"> The lexer instance. </param>
|
||||
/// <param name="input"> The input stream which is the source for the current token.
|
||||
/// When this method is called, the current <seealso cref="IntStream#index"/> for
|
||||
/// {@code input} should be the start of the following token, i.e. 1
|
||||
/// character past the end of the current token. </param>
|
||||
/// <param name="startIndex"> The token start index. This value may be passed to
|
||||
/// <seealso cref="IntStream#seek"/> to set the {@code input} position to the beginning
|
||||
/// of the token. </param>
|
||||
void execute(Lexer *lexer, CharStream *input, size_t startIndex) const;
|
||||
|
||||
size_t hashCode() const;
|
||||
|
||||
bool equals(const LexerActionExecutor &other) const;
|
||||
|
||||
private:
|
||||
const std::vector<Ref<const LexerAction>> _lexerActions;
|
||||
mutable std::atomic<size_t> _hashCode;
|
||||
};
|
||||
|
||||
inline bool operator==(const LexerActionExecutor &lhs, const LexerActionExecutor &rhs) {
|
||||
return lhs.equals(rhs);
|
||||
}
|
||||
|
||||
inline bool operator!=(const LexerActionExecutor &lhs, const LexerActionExecutor &rhs) {
|
||||
return !operator==(lhs, rhs);
|
||||
}
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
|
||||
namespace std {
|
||||
|
||||
template <>
|
||||
struct hash<::antlr4::atn::LexerActionExecutor> {
|
||||
size_t operator()(const ::antlr4::atn::LexerActionExecutor &lexerActionExecutor) const {
|
||||
return lexerActionExecutor.hashCode();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
@ -1,57 +0,0 @@
|
||||
/* 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 atn {
|
||||
|
||||
/// <summary>
|
||||
/// Represents the serialization type of a <seealso cref="LexerAction"/>.
|
||||
///
|
||||
/// @author Sam Harwell
|
||||
/// @since 4.2
|
||||
/// </summary>
|
||||
enum class LexerActionType : size_t {
|
||||
/// <summary>
|
||||
/// The type of a <seealso cref="LexerChannelAction"/> action.
|
||||
/// </summary>
|
||||
CHANNEL = 0,
|
||||
/// <summary>
|
||||
/// The type of a <seealso cref="LexerCustomAction"/> action.
|
||||
/// </summary>
|
||||
CUSTOM,
|
||||
/// <summary>
|
||||
/// The type of a <seealso cref="LexerModeAction"/> action.
|
||||
/// </summary>
|
||||
MODE,
|
||||
/// <summary>
|
||||
/// The type of a <seealso cref="LexerMoreAction"/> action.
|
||||
/// </summary>
|
||||
MORE,
|
||||
/// <summary>
|
||||
/// The type of a <seealso cref="LexerPopModeAction"/> action.
|
||||
/// </summary>
|
||||
POP_MODE,
|
||||
/// <summary>
|
||||
/// The type of a <seealso cref="LexerPushModeAction"/> action.
|
||||
/// </summary>
|
||||
PUSH_MODE,
|
||||
/// <summary>
|
||||
/// The type of a <seealso cref="LexerSkipAction"/> action.
|
||||
/// </summary>
|
||||
SKIP,
|
||||
/// <summary>
|
||||
/// The type of a <seealso cref="LexerTypeAction"/> action.
|
||||
/// </summary>
|
||||
TYPE,
|
||||
|
||||
INDEXED_CUSTOM,
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,43 +0,0 @@
|
||||
/* 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 "misc/MurmurHash.h"
|
||||
#include "Lexer.h"
|
||||
#include "support/Casts.h"
|
||||
|
||||
#include "atn/LexerChannelAction.h"
|
||||
|
||||
using namespace antlr4::atn;
|
||||
using namespace antlr4::misc;
|
||||
using namespace antlrcpp;
|
||||
|
||||
LexerChannelAction::LexerChannelAction(int channel)
|
||||
: LexerAction(LexerActionType::CHANNEL, false), _channel(channel) {}
|
||||
|
||||
void LexerChannelAction::execute(Lexer *lexer) const {
|
||||
lexer->setChannel(getChannel());
|
||||
}
|
||||
|
||||
size_t LexerChannelAction::hashCodeImpl() const {
|
||||
size_t hash = MurmurHash::initialize();
|
||||
hash = MurmurHash::update(hash, static_cast<size_t>(getActionType()));
|
||||
hash = MurmurHash::update(hash, getChannel());
|
||||
return MurmurHash::finish(hash, 2);
|
||||
}
|
||||
|
||||
bool LexerChannelAction::equals(const LexerAction &other) const {
|
||||
if (this == std::addressof(other)) {
|
||||
return true;
|
||||
}
|
||||
if (getActionType() != other.getActionType()) {
|
||||
return false;
|
||||
}
|
||||
const auto &lexerAction = downCast<const LexerChannelAction&>(other);
|
||||
return getChannel() == lexerAction.getChannel();
|
||||
}
|
||||
|
||||
std::string LexerChannelAction::toString() const {
|
||||
return "channel(" + std::to_string(getChannel()) + ")";
|
||||
}
|
||||
@ -1,59 +0,0 @@
|
||||
/* 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 "atn/LexerAction.h"
|
||||
#include "atn/LexerActionType.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
using antlr4::Lexer;
|
||||
|
||||
/// <summary>
|
||||
/// Implements the {@code channel} lexer action by calling
|
||||
/// <seealso cref="Lexer#setChannel"/> with the assigned channel.
|
||||
///
|
||||
/// @author Sam Harwell
|
||||
/// @since 4.2
|
||||
/// </summary>
|
||||
class ANTLR4CPP_PUBLIC LexerChannelAction final : public LexerAction {
|
||||
public:
|
||||
static bool is(const LexerAction &lexerAction) { return lexerAction.getActionType() == LexerActionType::CHANNEL; }
|
||||
|
||||
static bool is(const LexerAction *lexerAction) { return lexerAction != nullptr && is(*lexerAction); }
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new {@code channel} action with the specified channel value. </summary>
|
||||
/// <param name="channel"> The channel value to pass to <seealso cref="Lexer#setChannel"/>. </param>
|
||||
explicit LexerChannelAction(int channel);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the channel to use for the <seealso cref="Token"/> created by the lexer.
|
||||
/// </summary>
|
||||
/// <returns> The channel to use for the <seealso cref="Token"/> created by the lexer. </returns>
|
||||
int getChannel() const { return _channel; }
|
||||
|
||||
/// <summary>
|
||||
/// {@inheritDoc}
|
||||
///
|
||||
/// <para>This action is implemented by calling <seealso cref="Lexer#setChannel"/> with the
|
||||
/// value provided by <seealso cref="#getChannel"/>.</para>
|
||||
/// </summary>
|
||||
void execute(Lexer *lexer) const override;
|
||||
|
||||
bool equals(const LexerAction &other) const override;
|
||||
std::string toString() const override;
|
||||
|
||||
protected:
|
||||
size_t hashCodeImpl() const override;
|
||||
|
||||
private:
|
||||
const int _channel;
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,45 +0,0 @@
|
||||
/* 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 "misc/MurmurHash.h"
|
||||
#include "Lexer.h"
|
||||
#include "support/Casts.h"
|
||||
|
||||
#include "atn/LexerCustomAction.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
using namespace antlr4::misc;
|
||||
using namespace antlrcpp;
|
||||
|
||||
LexerCustomAction::LexerCustomAction(size_t ruleIndex, size_t actionIndex)
|
||||
: LexerAction(LexerActionType::CUSTOM, true), _ruleIndex(ruleIndex), _actionIndex(actionIndex) {}
|
||||
|
||||
void LexerCustomAction::execute(Lexer *lexer) const {
|
||||
lexer->action(nullptr, getRuleIndex(), getActionIndex());
|
||||
}
|
||||
|
||||
size_t LexerCustomAction::hashCodeImpl() const {
|
||||
size_t hash = MurmurHash::initialize();
|
||||
hash = MurmurHash::update(hash, static_cast<size_t>(getActionType()));
|
||||
hash = MurmurHash::update(hash, getRuleIndex());
|
||||
hash = MurmurHash::update(hash, getActionIndex());
|
||||
return MurmurHash::finish(hash, 3);
|
||||
}
|
||||
|
||||
bool LexerCustomAction::equals(const LexerAction &other) const {
|
||||
if (this == std::addressof(other)) {
|
||||
return true;
|
||||
}
|
||||
if (getActionType() != other.getActionType()) {
|
||||
return false;
|
||||
}
|
||||
const auto &lexerAction = downCast<const LexerCustomAction&>(other);
|
||||
return getRuleIndex() == lexerAction.getRuleIndex() && getActionIndex() == lexerAction.getActionIndex();
|
||||
}
|
||||
|
||||
std::string LexerCustomAction::toString() const {
|
||||
return "custom(" + std::to_string(getRuleIndex()) + ", " + std::to_string(getActionIndex()) + ")";
|
||||
}
|
||||
@ -1,75 +0,0 @@
|
||||
/* 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 "atn/LexerAction.h"
|
||||
#include "atn/LexerActionType.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// <summary>
|
||||
/// Executes a custom lexer action by calling <seealso cref="Recognizer#action"/> with the
|
||||
/// rule and action indexes assigned to the custom action. The implementation of
|
||||
/// a custom action is added to the generated code for the lexer in an override
|
||||
/// of <seealso cref="Recognizer#action"/> when the grammar is compiled.
|
||||
///
|
||||
/// <para>This class may represent embedded actions created with the <code>{...}</code>
|
||||
/// syntax in ANTLR 4, as well as actions created for lexer commands where the
|
||||
/// command argument could not be evaluated when the grammar was compiled.</para>
|
||||
///
|
||||
/// @author Sam Harwell
|
||||
/// @since 4.2
|
||||
/// </summary>
|
||||
class ANTLR4CPP_PUBLIC LexerCustomAction final : public LexerAction {
|
||||
public:
|
||||
static bool is(const LexerAction &lexerAction) { return lexerAction.getActionType() == LexerActionType::CUSTOM; }
|
||||
|
||||
static bool is(const LexerAction *lexerAction) { return lexerAction != nullptr && is(*lexerAction); }
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a custom lexer action with the specified rule and action
|
||||
/// indexes.
|
||||
/// </summary>
|
||||
/// <param name="ruleIndex"> The rule index to use for calls to
|
||||
/// <seealso cref="Recognizer#action"/>. </param>
|
||||
/// <param name="actionIndex"> The action index to use for calls to
|
||||
/// <seealso cref="Recognizer#action"/>. </param>
|
||||
LexerCustomAction(size_t ruleIndex, size_t actionIndex);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the rule index to use for calls to <seealso cref="Recognizer#action"/>.
|
||||
/// </summary>
|
||||
/// <returns> The rule index for the custom action. </returns>
|
||||
size_t getRuleIndex() const { return _ruleIndex; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the action index to use for calls to <seealso cref="Recognizer#action"/>.
|
||||
/// </summary>
|
||||
/// <returns> The action index for the custom action. </returns>
|
||||
size_t getActionIndex() const { return _actionIndex; }
|
||||
|
||||
/// <summary>
|
||||
/// {@inheritDoc}
|
||||
///
|
||||
/// <para>Custom actions are implemented by calling <seealso cref="Lexer#action"/> with the
|
||||
/// appropriate rule and action indexes.</para>
|
||||
/// </summary>
|
||||
void execute(Lexer *lexer) const override;
|
||||
|
||||
bool equals(const LexerAction &other) const override;
|
||||
std::string toString() const override;
|
||||
|
||||
protected:
|
||||
size_t hashCodeImpl() const override;
|
||||
|
||||
private:
|
||||
const size_t _ruleIndex;
|
||||
const size_t _actionIndex;
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,50 +0,0 @@
|
||||
/* 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 "atn/HashUtils.h"
|
||||
#include "misc/MurmurHash.h"
|
||||
#include "Lexer.h"
|
||||
#include "support/CPPUtils.h"
|
||||
#include "support/Casts.h"
|
||||
|
||||
#include "atn/LexerIndexedCustomAction.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
using namespace antlr4::misc;
|
||||
using namespace antlrcpp;
|
||||
|
||||
LexerIndexedCustomAction::LexerIndexedCustomAction(int offset, Ref<const LexerAction> action)
|
||||
: LexerAction(LexerActionType::INDEXED_CUSTOM, true), _action(std::move(action)), _offset(offset) {}
|
||||
|
||||
void LexerIndexedCustomAction::execute(Lexer *lexer) const {
|
||||
// assume the input stream position was properly set by the calling code
|
||||
getAction()->execute(lexer);
|
||||
}
|
||||
|
||||
size_t LexerIndexedCustomAction::hashCodeImpl() const {
|
||||
size_t hash = MurmurHash::initialize();
|
||||
hash = MurmurHash::update(hash, static_cast<size_t>(getActionType()));
|
||||
hash = MurmurHash::update(hash, getOffset());
|
||||
hash = MurmurHash::update(hash, getAction());
|
||||
return MurmurHash::finish(hash, 3);
|
||||
}
|
||||
|
||||
bool LexerIndexedCustomAction::equals(const LexerAction &other) const {
|
||||
if (this == std::addressof(other)) {
|
||||
return true;
|
||||
}
|
||||
if (getActionType() != other.getActionType()) {
|
||||
return false;
|
||||
}
|
||||
const auto &lexerAction = downCast<const LexerIndexedCustomAction&>(other);
|
||||
return getOffset() == lexerAction.getOffset() &&
|
||||
cachedHashCodeEqual(cachedHashCode(), lexerAction.cachedHashCode()) &&
|
||||
*getAction() == *lexerAction.getAction();
|
||||
}
|
||||
|
||||
std::string LexerIndexedCustomAction::toString() const {
|
||||
return "indexedCustom(" + std::to_string(getOffset()) + ", " + getAction()->toString() + ")";
|
||||
}
|
||||
@ -1,76 +0,0 @@
|
||||
/* 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 "RuleContext.h"
|
||||
#include "atn/LexerAction.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// <summary>
|
||||
/// This implementation of <seealso cref="LexerAction"/> is used for tracking input offsets
|
||||
/// for position-dependent actions within a <seealso cref="LexerActionExecutor"/>.
|
||||
///
|
||||
/// <para>This action is not serialized as part of the ATN, and is only required for
|
||||
/// position-dependent lexer actions which appear at a location other than the
|
||||
/// end of a rule. For more information about DFA optimizations employed for
|
||||
/// lexer actions, see <seealso cref="LexerActionExecutor#append"/> and
|
||||
/// <seealso cref="LexerActionExecutor#fixOffsetBeforeMatch"/>.</para>
|
||||
///
|
||||
/// @author Sam Harwell
|
||||
/// @since 4.2
|
||||
/// </summary>
|
||||
class ANTLR4CPP_PUBLIC LexerIndexedCustomAction final : public LexerAction {
|
||||
public:
|
||||
static bool is(const LexerAction &lexerAction) { return lexerAction.getActionType() == LexerActionType::INDEXED_CUSTOM; }
|
||||
|
||||
static bool is(const LexerAction *lexerAction) { return lexerAction != nullptr && is(*lexerAction); }
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new indexed custom action by associating a character offset
|
||||
/// with a <seealso cref="LexerAction"/>.
|
||||
///
|
||||
/// <para>Note: This class is only required for lexer actions for which
|
||||
/// <seealso cref="LexerAction#isPositionDependent"/> returns {@code true}.</para>
|
||||
/// </summary>
|
||||
/// <param name="offset"> The offset into the input <seealso cref="CharStream"/>, relative to
|
||||
/// the token start index, at which the specified lexer action should be
|
||||
/// executed. </param>
|
||||
/// <param name="action"> The lexer action to execute at a particular offset in the
|
||||
/// input <seealso cref="CharStream"/>. </param>
|
||||
LexerIndexedCustomAction(int offset, Ref<const LexerAction> action);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the location in the input <seealso cref="CharStream"/> at which the lexer
|
||||
/// action should be executed. The value is interpreted as an offset relative
|
||||
/// to the token start index.
|
||||
/// </summary>
|
||||
/// <returns> The location in the input <seealso cref="CharStream"/> at which the lexer
|
||||
/// action should be executed. </returns>
|
||||
int getOffset() const { return _offset; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the lexer action to execute.
|
||||
/// </summary>
|
||||
/// <returns> A <seealso cref="LexerAction"/> object which executes the lexer action. </returns>
|
||||
const Ref<const LexerAction>& getAction() const { return _action; }
|
||||
|
||||
void execute(Lexer *lexer) const override;
|
||||
bool equals(const LexerAction &other) const override;
|
||||
std::string toString() const override;
|
||||
|
||||
protected:
|
||||
size_t hashCodeImpl() const override;
|
||||
|
||||
private:
|
||||
const Ref<const LexerAction> _action;
|
||||
const int _offset;
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
|
||||
@ -1,43 +0,0 @@
|
||||
/* 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 "misc/MurmurHash.h"
|
||||
#include "Lexer.h"
|
||||
#include "support/Casts.h"
|
||||
|
||||
#include "atn/LexerModeAction.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
using namespace antlr4::misc;
|
||||
using namespace antlrcpp;
|
||||
|
||||
LexerModeAction::LexerModeAction(int mode) : LexerAction(LexerActionType::MODE, false), _mode(mode) {}
|
||||
|
||||
void LexerModeAction::execute(Lexer *lexer) const {
|
||||
lexer->setMode(getMode());
|
||||
}
|
||||
|
||||
size_t LexerModeAction::hashCodeImpl() const {
|
||||
size_t hash = MurmurHash::initialize();
|
||||
hash = MurmurHash::update(hash, static_cast<size_t>(getActionType()));
|
||||
hash = MurmurHash::update(hash, getMode());
|
||||
return MurmurHash::finish(hash, 2);
|
||||
}
|
||||
|
||||
bool LexerModeAction::equals(const LexerAction &other) const {
|
||||
if (this == std::addressof(other)) {
|
||||
return true;
|
||||
}
|
||||
if (getActionType() != other.getActionType()) {
|
||||
return false;
|
||||
}
|
||||
const auto &lexerAction = downCast<const LexerModeAction&>(other);
|
||||
return getMode() == lexerAction.getMode();
|
||||
}
|
||||
|
||||
std::string LexerModeAction::toString() const {
|
||||
return "mode(" + std::to_string(getMode()) + ")";
|
||||
}
|
||||
@ -1,57 +0,0 @@
|
||||
/* 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 "atn/LexerAction.h"
|
||||
#include "atn/LexerActionType.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// <summary>
|
||||
/// Implements the {@code mode} lexer action by calling <seealso cref="Lexer#mode"/> with
|
||||
/// the assigned mode.
|
||||
///
|
||||
/// @author Sam Harwell
|
||||
/// @since 4.2
|
||||
/// </summary>
|
||||
class ANTLR4CPP_PUBLIC LexerModeAction final : public LexerAction {
|
||||
public:
|
||||
static bool is(const LexerAction &lexerAction) { return lexerAction.getActionType() == LexerActionType::MODE; }
|
||||
|
||||
static bool is(const LexerAction *lexerAction) { return lexerAction != nullptr && is(*lexerAction); }
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new {@code mode} action with the specified mode value. </summary>
|
||||
/// <param name="mode"> The mode value to pass to <seealso cref="Lexer#mode"/>. </param>
|
||||
explicit LexerModeAction(int mode);
|
||||
|
||||
/// <summary>
|
||||
/// Get the lexer mode this action should transition the lexer to.
|
||||
/// </summary>
|
||||
/// <returns> The lexer mode for this {@code mode} command. </returns>
|
||||
int getMode() const { return _mode; }
|
||||
|
||||
/// <summary>
|
||||
/// {@inheritDoc}
|
||||
///
|
||||
/// <para>This action is implemented by calling <seealso cref="Lexer#mode"/> with the
|
||||
/// value provided by <seealso cref="#getMode"/>.</para>
|
||||
/// </summary>
|
||||
void execute(Lexer *lexer) const override;
|
||||
|
||||
bool equals(const LexerAction &obj) const override;
|
||||
std::string toString() const override;
|
||||
|
||||
protected:
|
||||
size_t hashCodeImpl() const override;
|
||||
|
||||
private:
|
||||
const int _mode;
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,36 +0,0 @@
|
||||
/* 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 "misc/MurmurHash.h"
|
||||
#include "Lexer.h"
|
||||
|
||||
#include "atn/LexerMoreAction.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
using namespace antlr4::misc;
|
||||
|
||||
const Ref<const LexerMoreAction>& LexerMoreAction::getInstance() {
|
||||
static const Ref<const LexerMoreAction> instance(new LexerMoreAction());
|
||||
return instance;
|
||||
}
|
||||
|
||||
void LexerMoreAction::execute(Lexer *lexer) const {
|
||||
lexer->more();
|
||||
}
|
||||
|
||||
size_t LexerMoreAction::hashCodeImpl() const {
|
||||
size_t hash = MurmurHash::initialize();
|
||||
hash = MurmurHash::update(hash, static_cast<size_t>(getActionType()));
|
||||
return MurmurHash::finish(hash, 1);
|
||||
}
|
||||
|
||||
bool LexerMoreAction::equals(const LexerAction &other) const {
|
||||
return this == std::addressof(other);
|
||||
}
|
||||
|
||||
std::string LexerMoreAction::toString() const {
|
||||
return "more";
|
||||
}
|
||||
@ -1,53 +0,0 @@
|
||||
/* 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 "atn/LexerAction.h"
|
||||
#include "atn/LexerActionType.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// <summary>
|
||||
/// Implements the {@code more} lexer action by calling <seealso cref="Lexer#more"/>.
|
||||
///
|
||||
/// <para>The {@code more} command does not have any parameters, so this action is
|
||||
/// implemented as a singleton instance exposed by <seealso cref="#INSTANCE"/>.</para>
|
||||
///
|
||||
/// @author Sam Harwell
|
||||
/// @since 4.2
|
||||
/// </summary>
|
||||
class ANTLR4CPP_PUBLIC LexerMoreAction final : public LexerAction {
|
||||
public:
|
||||
static bool is(const LexerAction &lexerAction) { return lexerAction.getActionType() == LexerActionType::MORE; }
|
||||
|
||||
static bool is(const LexerAction *lexerAction) { return lexerAction != nullptr && is(*lexerAction); }
|
||||
|
||||
/// <summary>
|
||||
/// Provides a singleton instance of this parameterless lexer action.
|
||||
/// </summary>
|
||||
static const Ref<const LexerMoreAction>& getInstance();
|
||||
|
||||
/// <summary>
|
||||
/// {@inheritDoc}
|
||||
///
|
||||
/// <para>This action is implemented by calling <seealso cref="Lexer#more"/>.</para>
|
||||
/// </summary>
|
||||
void execute(Lexer *lexer) const override;
|
||||
|
||||
bool equals(const LexerAction &obj) const override;
|
||||
std::string toString() const override;
|
||||
|
||||
protected:
|
||||
size_t hashCodeImpl() const override;
|
||||
|
||||
private:
|
||||
/// Constructs the singleton instance of the lexer {@code more} command.
|
||||
LexerMoreAction() : LexerAction(LexerActionType::MORE, false) {}
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,36 +0,0 @@
|
||||
/* 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 "misc/MurmurHash.h"
|
||||
#include "Lexer.h"
|
||||
|
||||
#include "atn/LexerPopModeAction.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
using namespace antlr4::misc;
|
||||
|
||||
const Ref<const LexerPopModeAction>& LexerPopModeAction::getInstance() {
|
||||
static const Ref<const LexerPopModeAction> instance(new LexerPopModeAction());
|
||||
return instance;
|
||||
}
|
||||
|
||||
void LexerPopModeAction::execute(Lexer *lexer) const {
|
||||
lexer->popMode();
|
||||
}
|
||||
|
||||
size_t LexerPopModeAction::hashCodeImpl() const {
|
||||
size_t hash = MurmurHash::initialize();
|
||||
hash = MurmurHash::update(hash, static_cast<size_t>(getActionType()));
|
||||
return MurmurHash::finish(hash, 1);
|
||||
}
|
||||
|
||||
bool LexerPopModeAction::equals(const LexerAction &other) const {
|
||||
return this == std::addressof(other);
|
||||
}
|
||||
|
||||
std::string LexerPopModeAction::toString() const {
|
||||
return "popMode";
|
||||
}
|
||||
@ -1,53 +0,0 @@
|
||||
/* 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 "atn/LexerAction.h"
|
||||
#include "atn/LexerActionType.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// <summary>
|
||||
/// Implements the {@code popMode} lexer action by calling <seealso cref="Lexer#popMode"/>.
|
||||
///
|
||||
/// <para>The {@code popMode} command does not have any parameters, so this action is
|
||||
/// implemented as a singleton instance exposed by <seealso cref="#INSTANCE"/>.</para>
|
||||
///
|
||||
/// @author Sam Harwell
|
||||
/// @since 4.2
|
||||
/// </summary>
|
||||
class ANTLR4CPP_PUBLIC LexerPopModeAction final : public LexerAction {
|
||||
public:
|
||||
static bool is(const LexerAction &lexerAction) { return lexerAction.getActionType() == LexerActionType::POP_MODE; }
|
||||
|
||||
static bool is(const LexerAction *lexerAction) { return lexerAction != nullptr && is(*lexerAction); }
|
||||
|
||||
/// <summary>
|
||||
/// Provides a singleton instance of this parameterless lexer action.
|
||||
/// </summary>
|
||||
static const Ref<const LexerPopModeAction>& getInstance();
|
||||
|
||||
/// <summary>
|
||||
/// {@inheritDoc}
|
||||
///
|
||||
/// <para>This action is implemented by calling <seealso cref="Lexer#popMode"/>.</para>
|
||||
/// </summary>
|
||||
void execute(Lexer *lexer) const override;
|
||||
|
||||
bool equals(const LexerAction &other) const override;
|
||||
std::string toString() const override;
|
||||
|
||||
protected:
|
||||
size_t hashCodeImpl() const override;
|
||||
|
||||
private:
|
||||
/// Constructs the singleton instance of the lexer {@code popMode} command.
|
||||
LexerPopModeAction() : LexerAction(LexerActionType::POP_MODE, false) {}
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,43 +0,0 @@
|
||||
/* 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 "misc/MurmurHash.h"
|
||||
#include "Lexer.h"
|
||||
#include "support/Casts.h"
|
||||
|
||||
#include "atn/LexerPushModeAction.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
using namespace antlr4::misc;
|
||||
using namespace antlrcpp;
|
||||
|
||||
LexerPushModeAction::LexerPushModeAction(int mode) : LexerAction(LexerActionType::PUSH_MODE, false), _mode(mode) {}
|
||||
|
||||
void LexerPushModeAction::execute(Lexer *lexer) const {
|
||||
lexer->pushMode(getMode());
|
||||
}
|
||||
|
||||
size_t LexerPushModeAction::hashCodeImpl() const {
|
||||
size_t hash = MurmurHash::initialize();
|
||||
hash = MurmurHash::update(hash, static_cast<size_t>(getActionType()));
|
||||
hash = MurmurHash::update(hash, getMode());
|
||||
return MurmurHash::finish(hash, 2);
|
||||
}
|
||||
|
||||
bool LexerPushModeAction::equals(const LexerAction &other) const {
|
||||
if (this == std::addressof(other)) {
|
||||
return true;
|
||||
}
|
||||
if (getActionType() != other.getActionType()) {
|
||||
return false;
|
||||
}
|
||||
const auto &lexerAction = downCast<const LexerPushModeAction&>(other);
|
||||
return getMode() == lexerAction.getMode();
|
||||
}
|
||||
|
||||
std::string LexerPushModeAction::toString() const {
|
||||
return "pushMode(" + std::to_string(getMode()) + ")";
|
||||
}
|
||||
@ -1,57 +0,0 @@
|
||||
/* 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 "atn/LexerAction.h"
|
||||
#include "atn/LexerActionType.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// <summary>
|
||||
/// Implements the {@code pushMode} lexer action by calling
|
||||
/// <seealso cref="Lexer#pushMode"/> with the assigned mode.
|
||||
///
|
||||
/// @author Sam Harwell
|
||||
/// @since 4.2
|
||||
/// </summary>
|
||||
class ANTLR4CPP_PUBLIC LexerPushModeAction final : public LexerAction {
|
||||
public:
|
||||
static bool is(const LexerAction &lexerAction) { return lexerAction.getActionType() == LexerActionType::PUSH_MODE; }
|
||||
|
||||
static bool is(const LexerAction *lexerAction) { return lexerAction != nullptr && is(*lexerAction); }
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new {@code pushMode} action with the specified mode value. </summary>
|
||||
/// <param name="mode"> The mode value to pass to <seealso cref="Lexer#pushMode"/>. </param>
|
||||
explicit LexerPushModeAction(int mode);
|
||||
|
||||
/// <summary>
|
||||
/// Get the lexer mode this action should transition the lexer to.
|
||||
/// </summary>
|
||||
/// <returns> The lexer mode for this {@code pushMode} command. </returns>
|
||||
int getMode() const { return _mode; }
|
||||
|
||||
/// <summary>
|
||||
/// {@inheritDoc}
|
||||
///
|
||||
/// <para>This action is implemented by calling <seealso cref="Lexer#pushMode"/> with the
|
||||
/// value provided by <seealso cref="#getMode"/>.</para>
|
||||
/// </summary>
|
||||
void execute(Lexer *lexer) const override;
|
||||
|
||||
bool equals(const LexerAction &obj) const override;
|
||||
std::string toString() const override;
|
||||
|
||||
protected:
|
||||
size_t hashCodeImpl() const override;
|
||||
|
||||
private:
|
||||
const int _mode;
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,36 +0,0 @@
|
||||
/* 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 "misc/MurmurHash.h"
|
||||
#include "Lexer.h"
|
||||
|
||||
#include "atn/LexerSkipAction.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
using namespace antlr4::misc;
|
||||
|
||||
const Ref<const LexerSkipAction>& LexerSkipAction::getInstance() {
|
||||
static const Ref<const LexerSkipAction> instance(new LexerSkipAction());
|
||||
return instance;
|
||||
}
|
||||
|
||||
void LexerSkipAction::execute(Lexer *lexer) const {
|
||||
lexer->skip();
|
||||
}
|
||||
|
||||
size_t LexerSkipAction::hashCodeImpl() const {
|
||||
size_t hash = MurmurHash::initialize();
|
||||
hash = MurmurHash::update(hash, static_cast<size_t>(getActionType()));
|
||||
return MurmurHash::finish(hash, 1);
|
||||
}
|
||||
|
||||
bool LexerSkipAction::equals(const LexerAction &other) const {
|
||||
return this == std::addressof(other);
|
||||
}
|
||||
|
||||
std::string LexerSkipAction::toString() const {
|
||||
return "skip";
|
||||
}
|
||||
@ -1,51 +0,0 @@
|
||||
/* 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 "atn/LexerAction.h"
|
||||
#include "atn/LexerActionType.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// <summary>
|
||||
/// Implements the {@code skip} lexer action by calling <seealso cref="Lexer#skip"/>.
|
||||
///
|
||||
/// <para>The {@code skip} command does not have any parameters, so this action is
|
||||
/// implemented as a singleton instance exposed by <seealso cref="#INSTANCE"/>.</para>
|
||||
///
|
||||
/// @author Sam Harwell
|
||||
/// @since 4.2
|
||||
/// </summary>
|
||||
class ANTLR4CPP_PUBLIC LexerSkipAction final : public LexerAction {
|
||||
public:
|
||||
static bool is(const LexerAction &lexerAction) { return lexerAction.getActionType() == LexerActionType::SKIP; }
|
||||
|
||||
static bool is(const LexerAction *lexerAction) { return lexerAction != nullptr && is(*lexerAction); }
|
||||
|
||||
/// Provides a singleton instance of this parameterless lexer action.
|
||||
static const Ref<const LexerSkipAction>& getInstance();
|
||||
|
||||
/// <summary>
|
||||
/// {@inheritDoc}
|
||||
///
|
||||
/// <para>This action is implemented by calling <seealso cref="Lexer#skip"/>.</para>
|
||||
/// </summary>
|
||||
void execute(Lexer *lexer) const override;
|
||||
|
||||
bool equals(const LexerAction &obj) const override;
|
||||
std::string toString() const override;
|
||||
|
||||
protected:
|
||||
size_t hashCodeImpl() const override;
|
||||
|
||||
private:
|
||||
/// Constructs the singleton instance of the lexer {@code skip} command.
|
||||
LexerSkipAction() : LexerAction(LexerActionType::SKIP, false) {}
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,43 +0,0 @@
|
||||
/* 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 "misc/MurmurHash.h"
|
||||
#include "Lexer.h"
|
||||
#include "support/Casts.h"
|
||||
|
||||
#include "atn/LexerTypeAction.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
using namespace antlr4::misc;
|
||||
using namespace antlrcpp;
|
||||
|
||||
LexerTypeAction::LexerTypeAction(int type) : LexerAction(LexerActionType::TYPE, false), _type(type) {}
|
||||
|
||||
void LexerTypeAction::execute(Lexer *lexer) const {
|
||||
lexer->setType(getType());
|
||||
}
|
||||
|
||||
size_t LexerTypeAction::hashCodeImpl() const {
|
||||
size_t hash = MurmurHash::initialize();
|
||||
hash = MurmurHash::update(hash, static_cast<size_t>(getActionType()));
|
||||
hash = MurmurHash::update(hash, getType());
|
||||
return MurmurHash::finish(hash, 2);
|
||||
}
|
||||
|
||||
bool LexerTypeAction::equals(const LexerAction &other) const {
|
||||
if (this == std::addressof(other)) {
|
||||
return true;
|
||||
}
|
||||
if (getActionType() != other.getActionType()) {
|
||||
return false;
|
||||
}
|
||||
const auto &lexerAction = downCast<const LexerTypeAction&>(other);
|
||||
return getType() == lexerAction.getType();
|
||||
}
|
||||
|
||||
std::string LexerTypeAction::toString() const {
|
||||
return "type(" + std::to_string(getType()) + ")";
|
||||
}
|
||||
@ -1,51 +0,0 @@
|
||||
/* 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 "atn/LexerActionType.h"
|
||||
#include "atn/LexerAction.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// Implements the {@code type} lexer action by calling <seealso cref="Lexer#setType"/>
|
||||
/// with the assigned type.
|
||||
class ANTLR4CPP_PUBLIC LexerTypeAction final : public LexerAction {
|
||||
public:
|
||||
static bool is(const LexerAction &lexerAction) { return lexerAction.getActionType() == LexerActionType::TYPE; }
|
||||
|
||||
static bool is(const LexerAction *lexerAction) { return lexerAction != nullptr && is(*lexerAction); }
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new {@code type} action with the specified token type value. </summary>
|
||||
/// <param name="type"> The type to assign to the token using <seealso cref="Lexer#setType"/>. </param>
|
||||
explicit LexerTypeAction(int type);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type to assign to a token created by the lexer. </summary>
|
||||
/// <returns> The type to assign to a token created by the lexer. </returns>
|
||||
int getType() const { return _type; }
|
||||
|
||||
/// <summary>
|
||||
/// {@inheritDoc}
|
||||
///
|
||||
/// <para>This action is implemented by calling <seealso cref="Lexer#setType"/> with the
|
||||
/// value provided by <seealso cref="#getType"/>.</para>
|
||||
/// </summary>
|
||||
void execute(Lexer *lexer) const override;
|
||||
|
||||
bool equals(const LexerAction &obj) const override;
|
||||
std::string toString() const override;
|
||||
|
||||
protected:
|
||||
size_t hashCodeImpl() const override;
|
||||
|
||||
private:
|
||||
const int _type;
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,16 +0,0 @@
|
||||
/* 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 "atn/LookaheadEventInfo.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
|
||||
LookaheadEventInfo::LookaheadEventInfo(size_t decision, ATNConfigSet *configs, size_t predictedAlt,
|
||||
TokenStream *input, size_t startIndex, size_t stopIndex, bool fullCtx)
|
||||
: DecisionEventInfo(decision, configs, input, startIndex, stopIndex, fullCtx) {
|
||||
|
||||
this->predictedAlt = predictedAlt;
|
||||
}
|
||||
@ -1,42 +0,0 @@
|
||||
/* 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 "atn/DecisionEventInfo.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// This class represents profiling event information for tracking the lookahead
|
||||
/// depth required in order to make a prediction.
|
||||
class ANTLR4CPP_PUBLIC LookaheadEventInfo : public DecisionEventInfo {
|
||||
public:
|
||||
/// The alternative chosen by adaptivePredict(), not necessarily
|
||||
/// the outermost alt shown for a rule; left-recursive rules have
|
||||
/// user-level alts that differ from the rewritten rule with a (...) block
|
||||
/// and a (..)* loop.
|
||||
size_t predictedAlt = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new instance of the <seealso cref="LookaheadEventInfo"/> class with
|
||||
/// the specified detailed lookahead information.
|
||||
/// </summary>
|
||||
/// <param name="decision"> The decision number </param>
|
||||
/// <param name="configs"> The final configuration set containing the necessary
|
||||
/// information to determine the result of a prediction, or {@code null} if
|
||||
/// the final configuration set is not available </param>
|
||||
/// <param name="input"> The input token stream </param>
|
||||
/// <param name="startIndex"> The start index for the current prediction </param>
|
||||
/// <param name="stopIndex"> The index at which the prediction was finally made </param>
|
||||
/// <param name="fullCtx"> {@code true} if the current lookahead is part of an LL
|
||||
/// prediction; otherwise, {@code false} if the current lookahead is part of
|
||||
/// an SLL prediction </param>
|
||||
LookaheadEventInfo(size_t decision, ATNConfigSet *configs, size_t predictedAlt, TokenStream *input, size_t startIndex,
|
||||
size_t stopIndex, bool fullCtx);
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,102 +0,0 @@
|
||||
/* 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 "atn/ProfilingATNSimulator.h"
|
||||
#include "dfa/DFA.h"
|
||||
|
||||
#include "atn/ParseInfo.h"
|
||||
|
||||
using namespace antlr4::atn;
|
||||
|
||||
ParseInfo::ParseInfo(ProfilingATNSimulator *atnSimulator) : _atnSimulator(atnSimulator) {
|
||||
}
|
||||
|
||||
ParseInfo::~ParseInfo() {
|
||||
}
|
||||
|
||||
std::vector<DecisionInfo> ParseInfo::getDecisionInfo() {
|
||||
return _atnSimulator->getDecisionInfo();
|
||||
}
|
||||
|
||||
std::vector<size_t> ParseInfo::getLLDecisions() {
|
||||
std::vector<DecisionInfo> decisions = _atnSimulator->getDecisionInfo();
|
||||
std::vector<size_t> LL;
|
||||
for (size_t i = 0; i < decisions.size(); ++i) {
|
||||
long long fallBack = decisions[i].LL_Fallback;
|
||||
if (fallBack > 0) {
|
||||
LL.push_back(i);
|
||||
}
|
||||
}
|
||||
return LL;
|
||||
}
|
||||
|
||||
long long ParseInfo::getTotalTimeInPrediction() {
|
||||
std::vector<DecisionInfo> decisions = _atnSimulator->getDecisionInfo();
|
||||
long long t = 0;
|
||||
for (size_t i = 0; i < decisions.size(); ++i) {
|
||||
t += decisions[i].timeInPrediction;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
long long ParseInfo::getTotalSLLLookaheadOps() {
|
||||
std::vector<DecisionInfo> decisions = _atnSimulator->getDecisionInfo();
|
||||
long long k = 0;
|
||||
for (size_t i = 0; i < decisions.size(); ++i) {
|
||||
k += decisions[i].SLL_TotalLook;
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
long long ParseInfo::getTotalLLLookaheadOps() {
|
||||
std::vector<DecisionInfo> decisions = _atnSimulator->getDecisionInfo();
|
||||
long long k = 0;
|
||||
for (size_t i = 0; i < decisions.size(); i++) {
|
||||
k += decisions[i].LL_TotalLook;
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
long long ParseInfo::getTotalSLLATNLookaheadOps() {
|
||||
std::vector<DecisionInfo> decisions = _atnSimulator->getDecisionInfo();
|
||||
long long k = 0;
|
||||
for (size_t i = 0; i < decisions.size(); ++i) {
|
||||
k += decisions[i].SLL_ATNTransitions;
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
long long ParseInfo::getTotalLLATNLookaheadOps() {
|
||||
std::vector<DecisionInfo> decisions = _atnSimulator->getDecisionInfo();
|
||||
long long k = 0;
|
||||
for (size_t i = 0; i < decisions.size(); ++i) {
|
||||
k += decisions[i].LL_ATNTransitions;
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
long long ParseInfo::getTotalATNLookaheadOps() {
|
||||
std::vector<DecisionInfo> decisions = _atnSimulator->getDecisionInfo();
|
||||
long long k = 0;
|
||||
for (size_t i = 0; i < decisions.size(); ++i) {
|
||||
k += decisions[i].SLL_ATNTransitions;
|
||||
k += decisions[i].LL_ATNTransitions;
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
size_t ParseInfo::getDFASize() {
|
||||
size_t n = 0;
|
||||
std::vector<dfa::DFA> &decisionToDFA = _atnSimulator->decisionToDFA;
|
||||
for (size_t i = 0; i < decisionToDFA.size(); ++i) {
|
||||
n += getDFASize(i);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t ParseInfo::getDFASize(size_t decision) {
|
||||
dfa::DFA &decisionToDFA = _atnSimulator->decisionToDFA[decision];
|
||||
return decisionToDFA.states.size();
|
||||
}
|
||||
@ -1,102 +0,0 @@
|
||||
/* 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 "atn/DecisionInfo.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
class ProfilingATNSimulator;
|
||||
|
||||
/// This class provides access to specific and aggregate statistics gathered
|
||||
/// during profiling of a parser.
|
||||
class ANTLR4CPP_PUBLIC ParseInfo {
|
||||
public:
|
||||
ParseInfo(ProfilingATNSimulator *atnSimulator);
|
||||
ParseInfo(ParseInfo const&) = default;
|
||||
virtual ~ParseInfo();
|
||||
|
||||
ParseInfo& operator=(ParseInfo const&) = default;
|
||||
|
||||
/// <summary>
|
||||
/// Gets an array of <seealso cref="DecisionInfo"/> instances containing the profiling
|
||||
/// information gathered for each decision in the ATN.
|
||||
/// </summary>
|
||||
/// <returns> An array of <seealso cref="DecisionInfo"/> instances, indexed by decision
|
||||
/// number. </returns>
|
||||
virtual std::vector<DecisionInfo> getDecisionInfo();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the decision numbers for decisions that required one or more
|
||||
/// full-context predictions during parsing. These are decisions for which
|
||||
/// <seealso cref="DecisionInfo#LL_Fallback"/> is non-zero.
|
||||
/// </summary>
|
||||
/// <returns> A list of decision numbers which required one or more
|
||||
/// full-context predictions during parsing. </returns>
|
||||
virtual std::vector<size_t> getLLDecisions();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total time spent during prediction across all decisions made
|
||||
/// during parsing. This value is the sum of
|
||||
/// <seealso cref="DecisionInfo#timeInPrediction"/> for all decisions.
|
||||
/// </summary>
|
||||
virtual long long getTotalTimeInPrediction();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of SLL lookahead operations across all decisions
|
||||
/// made during parsing. This value is the sum of
|
||||
/// <seealso cref="DecisionInfo#SLL_TotalLook"/> for all decisions.
|
||||
/// </summary>
|
||||
virtual long long getTotalSLLLookaheadOps();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of LL lookahead operations across all decisions
|
||||
/// made during parsing. This value is the sum of
|
||||
/// <seealso cref="DecisionInfo#LL_TotalLook"/> for all decisions.
|
||||
/// </summary>
|
||||
virtual long long getTotalLLLookaheadOps();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of ATN lookahead operations for SLL prediction
|
||||
/// across all decisions made during parsing.
|
||||
/// </summary>
|
||||
virtual long long getTotalSLLATNLookaheadOps();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of ATN lookahead operations for LL prediction
|
||||
/// across all decisions made during parsing.
|
||||
/// </summary>
|
||||
virtual long long getTotalLLATNLookaheadOps();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of ATN lookahead operations for SLL and LL
|
||||
/// prediction across all decisions made during parsing.
|
||||
///
|
||||
/// <para>
|
||||
/// This value is the sum of <seealso cref="#getTotalSLLATNLookaheadOps"/> and
|
||||
/// <seealso cref="#getTotalLLATNLookaheadOps"/>.</para>
|
||||
/// </summary>
|
||||
virtual long long getTotalATNLookaheadOps();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of DFA states stored in the DFA cache for all
|
||||
/// decisions in the ATN.
|
||||
/// </summary>
|
||||
virtual size_t getDFASize();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of DFA states stored in the DFA cache for a
|
||||
/// particular decision.
|
||||
/// </summary>
|
||||
virtual size_t getDFASize(size_t decision);
|
||||
|
||||
protected:
|
||||
const ProfilingATNSimulator *_atnSimulator; // non-owning, we are created by this simulator.
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,911 +0,0 @@
|
||||
/* 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 "PredictionMode.h"
|
||||
#include "dfa/DFAState.h"
|
||||
#include "atn/ATNSimulator.h"
|
||||
#include "atn/PredictionContext.h"
|
||||
#include "atn/PredictionContextMergeCache.h"
|
||||
#include "atn/ParserATNSimulatorOptions.h"
|
||||
#include "SemanticContext.h"
|
||||
#include "atn/ATNConfig.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/**
|
||||
* The embodiment of the adaptive LL(*), ALL(*), parsing strategy.
|
||||
*
|
||||
* <p>
|
||||
* The basic complexity of the adaptive strategy makes it harder to understand.
|
||||
* We begin with ATN simulation to build paths in a DFA. Subsequent prediction
|
||||
* requests go through the DFA first. If they reach a state without an edge for
|
||||
* the current symbol, the algorithm fails over to the ATN simulation to
|
||||
* complete the DFA path for the current input (until it finds a conflict state
|
||||
* or uniquely predicting state).</p>
|
||||
*
|
||||
* <p>
|
||||
* All of that is done without using the outer context because we want to create
|
||||
* a DFA that is not dependent upon the rule invocation stack when we do a
|
||||
* prediction. One DFA works in all contexts. We avoid using context not
|
||||
* necessarily because it's slower, although it can be, but because of the DFA
|
||||
* caching problem. The closure routine only considers the rule invocation stack
|
||||
* created during prediction beginning in the decision rule. For example, if
|
||||
* prediction occurs without invoking another rule's ATN, there are no context
|
||||
* stacks in the configurations. When lack of context leads to a conflict, we
|
||||
* don't know if it's an ambiguity or a weakness in the strong LL(*) parsing
|
||||
* strategy (versus full LL(*)).</p>
|
||||
*
|
||||
* <p>
|
||||
* When SLL yields a configuration set with conflict, we rewind the input and
|
||||
* retry the ATN simulation, this time using full outer context without adding
|
||||
* to the DFA. Configuration context stacks will be the full invocation stacks
|
||||
* from the start rule. If we get a conflict using full context, then we can
|
||||
* definitively say we have a true ambiguity for that input sequence. If we
|
||||
* don't get a conflict, it implies that the decision is sensitive to the outer
|
||||
* context. (It is not context-sensitive in the sense of context-sensitive
|
||||
* grammars.)</p>
|
||||
*
|
||||
* <p>
|
||||
* The next time we reach this DFA state with an SLL conflict, through DFA
|
||||
* simulation, we will again retry the ATN simulation using full context mode.
|
||||
* This is slow because we can't save the results and have to "interpret" the
|
||||
* ATN each time we get that input.</p>
|
||||
*
|
||||
* <p>
|
||||
* <strong>CACHING FULL CONTEXT PREDICTIONS</strong></p>
|
||||
*
|
||||
* <p>
|
||||
* We could cache results from full context to predicted alternative easily and
|
||||
* that saves a lot of time but doesn't work in presence of predicates. The set
|
||||
* of visible predicates from the ATN start state changes depending on the
|
||||
* context, because closure can fall off the end of a rule. I tried to cache
|
||||
* tuples (stack context, semantic context, predicted alt) but it was slower
|
||||
* than interpreting and much more complicated. Also required a huge amount of
|
||||
* memory. The goal is not to create the world's fastest parser anyway. I'd like
|
||||
* to keep this algorithm simple. By launching multiple threads, we can improve
|
||||
* the speed of parsing across a large number of files.</p>
|
||||
*
|
||||
* <p>
|
||||
* There is no strict ordering between the amount of input used by SLL vs LL,
|
||||
* which makes it really hard to build a cache for full context. Let's say that
|
||||
* we have input A B C that leads to an SLL conflict with full context X. That
|
||||
* implies that using X we might only use A B but we could also use A B C D to
|
||||
* resolve conflict. Input A B C D could predict alternative 1 in one position
|
||||
* in the input and A B C E could predict alternative 2 in another position in
|
||||
* input. The conflicting SLL configurations could still be non-unique in the
|
||||
* full context prediction, which would lead us to requiring more input than the
|
||||
* original A B C. To make a prediction cache work, we have to track the exact
|
||||
* input used during the previous prediction. That amounts to a cache that maps
|
||||
* X to a specific DFA for that context.</p>
|
||||
*
|
||||
* <p>
|
||||
* Something should be done for left-recursive expression predictions. They are
|
||||
* likely LL(1) + pred eval. Easier to do the whole SLL unless error and retry
|
||||
* with full LL thing Sam does.</p>
|
||||
*
|
||||
* <p>
|
||||
* <strong>AVOIDING FULL CONTEXT PREDICTION</strong></p>
|
||||
*
|
||||
* <p>
|
||||
* We avoid doing full context retry when the outer context is empty, we did not
|
||||
* dip into the outer context by falling off the end of the decision state rule,
|
||||
* or when we force SLL mode.</p>
|
||||
*
|
||||
* <p>
|
||||
* As an example of the not dip into outer context case, consider as super
|
||||
* constructor calls versus function calls. One grammar might look like
|
||||
* this:</p>
|
||||
*
|
||||
* <pre>
|
||||
* ctorBody
|
||||
* : '{' superCall? stat* '}'
|
||||
* ;
|
||||
* </pre>
|
||||
*
|
||||
* <p>
|
||||
* Or, you might see something like</p>
|
||||
*
|
||||
* <pre>
|
||||
* stat
|
||||
* : superCall ';'
|
||||
* | expression ';'
|
||||
* | ...
|
||||
* ;
|
||||
* </pre>
|
||||
*
|
||||
* <p>
|
||||
* In both cases I believe that no closure operations will dip into the outer
|
||||
* context. In the first case ctorBody in the worst case will stop at the '}'.
|
||||
* In the 2nd case it should stop at the ';'. Both cases should stay within the
|
||||
* entry rule and not dip into the outer context.</p>
|
||||
*
|
||||
* <p>
|
||||
* <strong>PREDICATES</strong></p>
|
||||
*
|
||||
* <p>
|
||||
* Predicates are always evaluated if present in either SLL or LL both. SLL and
|
||||
* LL simulation deals with predicates differently. SLL collects predicates as
|
||||
* it performs closure operations like ANTLR v3 did. It delays predicate
|
||||
* evaluation until it reaches and accept state. This allows us to cache the SLL
|
||||
* ATN simulation whereas, if we had evaluated predicates on-the-fly during
|
||||
* closure, the DFA state configuration sets would be different and we couldn't
|
||||
* build up a suitable DFA.</p>
|
||||
*
|
||||
* <p>
|
||||
* When building a DFA accept state during ATN simulation, we evaluate any
|
||||
* predicates and return the sole semantically valid alternative. If there is
|
||||
* more than 1 alternative, we report an ambiguity. If there are 0 alternatives,
|
||||
* we throw an exception. Alternatives without predicates act like they have
|
||||
* true predicates. The simple way to think about it is to strip away all
|
||||
* alternatives with false predicates and choose the minimum alternative that
|
||||
* remains.</p>
|
||||
*
|
||||
* <p>
|
||||
* When we start in the DFA and reach an accept state that's predicated, we test
|
||||
* those and return the minimum semantically viable alternative. If no
|
||||
* alternatives are viable, we throw an exception.</p>
|
||||
*
|
||||
* <p>
|
||||
* During full LL ATN simulation, closure always evaluates predicates and
|
||||
* on-the-fly. This is crucial to reducing the configuration set size during
|
||||
* closure. It hits a landmine when parsing with the Java grammar, for example,
|
||||
* without this on-the-fly evaluation.</p>
|
||||
*
|
||||
* <p>
|
||||
* <strong>SHARING DFA</strong></p>
|
||||
*
|
||||
* <p>
|
||||
* All instances of the same parser share the same decision DFAs through a
|
||||
* static field. Each instance gets its own ATN simulator but they share the
|
||||
* same {@link #decisionToDFA} field. They also share a
|
||||
* {@link PredictionContextCache} object that makes sure that all
|
||||
* {@link PredictionContext} objects are shared among the DFA states. This makes
|
||||
* a big size difference.</p>
|
||||
*
|
||||
* <p>
|
||||
* <strong>THREAD SAFETY</strong></p>
|
||||
*
|
||||
* <p>
|
||||
* The {@link ParserATNSimulator} locks on the {@link #decisionToDFA} field when
|
||||
* it adds a new DFA object to that array. {@link #addDFAEdge}
|
||||
* locks on the DFA for the current decision when setting the
|
||||
* {@link DFAState#edges} field. {@link #addDFAState} locks on
|
||||
* the DFA for the current decision when looking up a DFA state to see if it
|
||||
* already exists. We must make sure that all requests to add DFA states that
|
||||
* are equivalent result in the same shared DFA object. This is because lots of
|
||||
* threads will be trying to update the DFA at once. The
|
||||
* {@link #addDFAState} method also locks inside the DFA lock
|
||||
* but this time on the shared context cache when it rebuilds the
|
||||
* configurations' {@link PredictionContext} objects using cached
|
||||
* subgraphs/nodes. No other locking occurs, even during DFA simulation. This is
|
||||
* safe as long as we can guarantee that all threads referencing
|
||||
* {@code s.edge[t]} get the same physical target {@link DFAState}, or
|
||||
* {@code null}. Once into the DFA, the DFA simulation does not reference the
|
||||
* {@link DFA#states} map. It follows the {@link DFAState#edges} field to new
|
||||
* targets. The DFA simulator will either find {@link DFAState#edges} to be
|
||||
* {@code null}, to be non-{@code null} and {@code dfa.edges[t]} null, or
|
||||
* {@code dfa.edges[t]} to be non-null. The
|
||||
* {@link #addDFAEdge} method could be racing to set the field
|
||||
* but in either case the DFA simulator works; if {@code null}, and requests ATN
|
||||
* simulation. It could also race trying to get {@code dfa.edges[t]}, but either
|
||||
* way it will work because it's not doing a test and set operation.</p>
|
||||
*
|
||||
* <p>
|
||||
* <strong>Starting with SLL then failing to combined SLL/LL (Two-Stage
|
||||
* Parsing)</strong></p>
|
||||
*
|
||||
* <p>
|
||||
* Sam pointed out that if SLL does not give a syntax error, then there is no
|
||||
* point in doing full LL, which is slower. We only have to try LL if we get a
|
||||
* syntax error. For maximum speed, Sam starts the parser set to pure SLL
|
||||
* mode with the {@link BailErrorStrategy}:</p>
|
||||
*
|
||||
* <pre>
|
||||
* parser.{@link Parser#getInterpreter() getInterpreter()}.{@link #setPredictionMode setPredictionMode}{@code (}{@link PredictionMode#SLL}{@code )};
|
||||
* parser.{@link Parser#setErrorHandler setErrorHandler}(new {@link BailErrorStrategy}());
|
||||
* </pre>
|
||||
*
|
||||
* <p>
|
||||
* If it does not get a syntax error, then we're done. If it does get a syntax
|
||||
* error, we need to retry with the combined SLL/LL strategy.</p>
|
||||
*
|
||||
* <p>
|
||||
* The reason this works is as follows. If there are no SLL conflicts, then the
|
||||
* grammar is SLL (at least for that input set). If there is an SLL conflict,
|
||||
* the full LL analysis must yield a set of viable alternatives which is a
|
||||
* subset of the alternatives reported by SLL. If the LL set is a singleton,
|
||||
* then the grammar is LL but not SLL. If the LL set is the same size as the SLL
|
||||
* set, the decision is SLL. If the LL set has size > 1, then that decision
|
||||
* is truly ambiguous on the current input. If the LL set is smaller, then the
|
||||
* SLL conflict resolution might choose an alternative that the full LL would
|
||||
* rule out as a possibility based upon better context information. If that's
|
||||
* the case, then the SLL parse will definitely get an error because the full LL
|
||||
* analysis says it's not viable. If SLL conflict resolution chooses an
|
||||
* alternative within the LL set, them both SLL and LL would choose the same
|
||||
* alternative because they both choose the minimum of multiple conflicting
|
||||
* alternatives.</p>
|
||||
*
|
||||
* <p>
|
||||
* Let's say we have a set of SLL conflicting alternatives {@code {1, 2, 3}} and
|
||||
* a smaller LL set called <em>s</em>. If <em>s</em> is {@code {2, 3}}, then SLL
|
||||
* parsing will get an error because SLL will pursue alternative 1. If
|
||||
* <em>s</em> is {@code {1, 2}} or {@code {1, 3}} then both SLL and LL will
|
||||
* choose the same alternative because alternative one is the minimum of either
|
||||
* set. If <em>s</em> is {@code {2}} or {@code {3}} then SLL will get a syntax
|
||||
* error. If <em>s</em> is {@code {1}} then SLL will succeed.</p>
|
||||
*
|
||||
* <p>
|
||||
* Of course, if the input is invalid, then we will get an error for sure in
|
||||
* both SLL and LL parsing. Erroneous input will therefore require 2 passes over
|
||||
* the input.</p>
|
||||
*/
|
||||
class ANTLR4CPP_PUBLIC ParserATNSimulator : public ATNSimulator {
|
||||
public:
|
||||
/// Testing only!
|
||||
ParserATNSimulator(const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
|
||||
PredictionContextCache &sharedContextCache);
|
||||
|
||||
ParserATNSimulator(Parser *parser, const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
|
||||
PredictionContextCache &sharedContextCache);
|
||||
|
||||
ParserATNSimulator(Parser *parser, const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
|
||||
PredictionContextCache &sharedContextCache,
|
||||
const ParserATNSimulatorOptions &options);
|
||||
|
||||
virtual void reset() override;
|
||||
virtual void clearDFA() override;
|
||||
virtual size_t adaptivePredict(TokenStream *input, size_t decision, ParserRuleContext *outerContext);
|
||||
|
||||
static const bool TURN_OFF_LR_LOOP_ENTRY_BRANCH_OPT;
|
||||
|
||||
std::vector<dfa::DFA> &decisionToDFA;
|
||||
|
||||
/** Implements first-edge (loop entry) elimination as an optimization
|
||||
* during closure operations. See antlr/antlr4#1398.
|
||||
*
|
||||
* The optimization is to avoid adding the loop entry config when
|
||||
* the exit path can only lead back to the same
|
||||
* StarLoopEntryState after popping context at the rule end state
|
||||
* (traversing only epsilon edges, so we're still in closure, in
|
||||
* this same rule).
|
||||
*
|
||||
* We need to detect any state that can reach loop entry on
|
||||
* epsilon w/o exiting rule. We don't have to look at FOLLOW
|
||||
* links, just ensure that all stack tops for config refer to key
|
||||
* states in LR rule.
|
||||
*
|
||||
* To verify we are in the right situation we must first check
|
||||
* closure is at a StarLoopEntryState generated during LR removal.
|
||||
* Then we check that each stack top of context is a return state
|
||||
* from one of these cases:
|
||||
*
|
||||
* 1. 'not' expr, '(' type ')' expr. The return state points at loop entry state
|
||||
* 2. expr op expr. The return state is the block end of internal block of (...)*
|
||||
* 3. 'between' expr 'and' expr. The return state of 2nd expr reference.
|
||||
* That state points at block end of internal block of (...)*.
|
||||
* 4. expr '?' expr ':' expr. The return state points at block end,
|
||||
* which points at loop entry state.
|
||||
*
|
||||
* If any is true for each stack top, then closure does not add a
|
||||
* config to the current config set for edge[0], the loop entry branch.
|
||||
*
|
||||
* Conditions fail if any context for the current config is:
|
||||
*
|
||||
* a. empty (we'd fall out of expr to do a global FOLLOW which could
|
||||
* even be to some weird spot in expr) or,
|
||||
* b. lies outside of expr or,
|
||||
* c. lies within expr but at a state not the BlockEndState
|
||||
* generated during LR removal
|
||||
*
|
||||
* Do we need to evaluate predicates ever in closure for this case?
|
||||
*
|
||||
* No. Predicates, including precedence predicates, are only
|
||||
* evaluated when computing a DFA start state. I.e., only before
|
||||
* the lookahead (but not parser) consumes a token.
|
||||
*
|
||||
* There are no epsilon edges allowed in LR rule alt blocks or in
|
||||
* the "primary" part (ID here). If closure is in
|
||||
* StarLoopEntryState any lookahead operation will have consumed a
|
||||
* token as there are no epsilon-paths that lead to
|
||||
* StarLoopEntryState. We do not have to evaluate predicates
|
||||
* therefore if we are in the generated StarLoopEntryState of a LR
|
||||
* rule. Note that when making a prediction starting at that
|
||||
* decision point, decision d=2, compute-start-state performs
|
||||
* closure starting at edges[0], edges[1] emanating from
|
||||
* StarLoopEntryState. That means it is not performing closure on
|
||||
* StarLoopEntryState during compute-start-state.
|
||||
*
|
||||
* How do we know this always gives same prediction answer?
|
||||
*
|
||||
* Without predicates, loop entry and exit paths are ambiguous
|
||||
* upon remaining input +b (in, say, a+b). Either paths lead to
|
||||
* valid parses. Closure can lead to consuming + immediately or by
|
||||
* falling out of this call to expr back into expr and loop back
|
||||
* again to StarLoopEntryState to match +b. In this special case,
|
||||
* we choose the more efficient path, which is to take the bypass
|
||||
* path.
|
||||
*
|
||||
* The lookahead language has not changed because closure chooses
|
||||
* one path over the other. Both paths lead to consuming the same
|
||||
* remaining input during a lookahead operation. If the next token
|
||||
* is an operator, lookahead will enter the choice block with
|
||||
* operators. If it is not, lookahead will exit expr. Same as if
|
||||
* closure had chosen to enter the choice block immediately.
|
||||
*
|
||||
* Closure is examining one config (some loopentrystate, some alt,
|
||||
* context) which means it is considering exactly one alt. Closure
|
||||
* always copies the same alt to any derived configs.
|
||||
*
|
||||
* How do we know this optimization doesn't mess up precedence in
|
||||
* our parse trees?
|
||||
*
|
||||
* Looking through expr from left edge of stat only has to confirm
|
||||
* that an input, say, a+b+c; begins with any valid interpretation
|
||||
* of an expression. The precedence actually doesn't matter when
|
||||
* making a decision in stat seeing through expr. It is only when
|
||||
* parsing rule expr that we must use the precedence to get the
|
||||
* right interpretation and, hence, parse tree.
|
||||
*/
|
||||
bool canDropLoopEntryEdgeInLeftRecursiveRule(ATNConfig *config) const;
|
||||
virtual std::string getRuleName(size_t index);
|
||||
|
||||
virtual Ref<ATNConfig> precedenceTransition(Ref<ATNConfig> const& config, const PrecedencePredicateTransition *pt,
|
||||
bool collectPredicates, bool inContext, bool fullCtx);
|
||||
|
||||
void setPredictionMode(PredictionMode newMode);
|
||||
PredictionMode getPredictionMode();
|
||||
|
||||
Parser* getParser();
|
||||
|
||||
virtual std::string getTokenName(size_t t);
|
||||
|
||||
virtual std::string getLookaheadName(TokenStream *input);
|
||||
|
||||
/// <summary>
|
||||
/// Used for debugging in adaptivePredict around execATN but I cut
|
||||
/// it out for clarity now that alg. works well. We can leave this
|
||||
/// "dead" code for a bit.
|
||||
/// </summary>
|
||||
virtual void dumpDeadEndConfigs(NoViableAltException &nvae);
|
||||
|
||||
protected:
|
||||
Parser *const parser;
|
||||
|
||||
/// <summary>
|
||||
/// Each prediction operation uses a cache for merge of prediction contexts.
|
||||
/// Don't keep around as it wastes huge amounts of memory. The merge cache
|
||||
/// isn't synchronized but we're ok since two threads shouldn't reuse same
|
||||
/// parser/atnsim object because it can only handle one input at a time.
|
||||
/// This maps graphs a and b to merged result c. (a,b)->c. We can avoid
|
||||
/// the merge if we ever see a and b again. Note that (b,a)->c should
|
||||
/// also be examined during cache lookup.
|
||||
/// </summary>
|
||||
PredictionContextMergeCache mergeCache;
|
||||
size_t _mergeCacheCounter = 0;
|
||||
|
||||
// LAME globals to avoid parameters!!!!! I need these down deep in predTransition
|
||||
TokenStream *_input;
|
||||
size_t _startIndex;
|
||||
ParserRuleContext *_outerContext;
|
||||
dfa::DFA *_dfa; // Reference into the decisionToDFA vector.
|
||||
|
||||
/// <summary>
|
||||
/// Performs ATN simulation to compute a predicted alternative based
|
||||
/// upon the remaining input, but also updates the DFA cache to avoid
|
||||
/// having to traverse the ATN again for the same input sequence.
|
||||
///
|
||||
/// There are some key conditions we're looking for after computing a new
|
||||
/// set of ATN configs (proposed DFA state):
|
||||
/// if the set is empty, there is no viable alternative for current symbol
|
||||
/// does the state uniquely predict an alternative?
|
||||
/// does the state have a conflict that would prevent us from
|
||||
/// putting it on the work list?
|
||||
///
|
||||
/// We also have some key operations to do:
|
||||
/// add an edge from previous DFA state to potentially new DFA state, D,
|
||||
/// upon current symbol but only if adding to work list, which means in all
|
||||
/// cases except no viable alternative (and possibly non-greedy decisions?)
|
||||
/// collecting predicates and adding semantic context to DFA accept states
|
||||
/// adding rule context to context-sensitive DFA accept states
|
||||
/// consuming an input symbol
|
||||
/// reporting a conflict
|
||||
/// reporting an ambiguity
|
||||
/// reporting a context sensitivity
|
||||
/// reporting insufficient predicates
|
||||
///
|
||||
/// cover these cases:
|
||||
/// dead end
|
||||
/// single alt
|
||||
/// single alt + preds
|
||||
/// conflict
|
||||
/// conflict + preds
|
||||
/// </summary>
|
||||
virtual size_t execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *input, size_t startIndex,
|
||||
ParserRuleContext *outerContext);
|
||||
|
||||
/// <summary>
|
||||
/// Get an existing target state for an edge in the DFA. If the target state
|
||||
/// for the edge has not yet been computed or is otherwise not available,
|
||||
/// this method returns {@code null}.
|
||||
/// </summary>
|
||||
/// <param name="previousD"> The current DFA state </param>
|
||||
/// <param name="t"> The next input symbol </param>
|
||||
/// <returns> The existing target DFA state for the given input symbol
|
||||
/// {@code t}, or {@code null} if the target state for this edge is not
|
||||
/// already cached </returns>
|
||||
virtual dfa::DFAState* getExistingTargetState(dfa::DFAState *previousD, size_t t);
|
||||
|
||||
/// <summary>
|
||||
/// Compute a target state for an edge in the DFA, and attempt to add the
|
||||
/// computed state and corresponding edge to the DFA.
|
||||
/// </summary>
|
||||
/// <param name="dfa"> The DFA </param>
|
||||
/// <param name="previousD"> The current DFA state </param>
|
||||
/// <param name="t"> The next input symbol
|
||||
/// </param>
|
||||
/// <returns> The computed target DFA state for the given input symbol
|
||||
/// {@code t}. If {@code t} does not lead to a valid DFA state, this method
|
||||
/// returns <seealso cref="#ERROR"/>. </returns>
|
||||
virtual dfa::DFAState *computeTargetState(dfa::DFA &dfa, dfa::DFAState *previousD, size_t t);
|
||||
|
||||
virtual void predicateDFAState(dfa::DFAState *dfaState, DecisionState *decisionState);
|
||||
|
||||
// comes back with reach.uniqueAlt set to a valid alt
|
||||
virtual size_t execATNWithFullContext(dfa::DFA &dfa, dfa::DFAState *D, ATNConfigSet *s0,
|
||||
TokenStream *input, size_t startIndex, ParserRuleContext *outerContext); // how far we got before failing over
|
||||
|
||||
virtual std::unique_ptr<ATNConfigSet> computeReachSet(ATNConfigSet *closure, size_t t, bool fullCtx);
|
||||
|
||||
/// <summary>
|
||||
/// Return a configuration set containing only the configurations from
|
||||
/// {@code configs} which are in a <seealso cref="RuleStopState"/>. If all
|
||||
/// configurations in {@code configs} are already in a rule stop state, this
|
||||
/// method simply returns {@code configs}.
|
||||
/// <p/>
|
||||
/// When {@code lookToEndOfRule} is true, this method uses
|
||||
/// <seealso cref="ATN#nextTokens"/> for each configuration in {@code configs} which is
|
||||
/// not already in a rule stop state to see if a rule stop state is reachable
|
||||
/// from the configuration via epsilon-only transitions.
|
||||
/// </summary>
|
||||
/// <param name="configs"> the configuration set to update </param>
|
||||
/// <param name="lookToEndOfRule"> when true, this method checks for rule stop states
|
||||
/// reachable by epsilon-only transitions from each configuration in
|
||||
/// {@code configs}.
|
||||
/// </param>
|
||||
/// <returns> {@code configs} if all configurations in {@code configs} are in a
|
||||
/// rule stop state, otherwise return a new configuration set containing only
|
||||
/// the configurations from {@code configs} which are in a rule stop state </returns>
|
||||
virtual ATNConfigSet* removeAllConfigsNotInRuleStopState(ATNConfigSet *configs, bool lookToEndOfRule);
|
||||
|
||||
virtual std::unique_ptr<ATNConfigSet> computeStartState(ATNState *p, RuleContext *ctx, bool fullCtx);
|
||||
|
||||
/* parrt internal source braindump that doesn't mess up
|
||||
* external API spec.
|
||||
|
||||
applyPrecedenceFilter is an optimization to avoid highly
|
||||
nonlinear prediction of expressions and other left recursive
|
||||
rules. The precedence predicates such as {3>=prec}? Are highly
|
||||
context-sensitive in that they can only be properly evaluated
|
||||
in the context of the proper prec argument. Without pruning,
|
||||
these predicates are normal predicates evaluated when we reach
|
||||
conflict state (or unique prediction). As we cannot evaluate
|
||||
these predicates out of context, the resulting conflict leads
|
||||
to full LL evaluation and nonlinear prediction which shows up
|
||||
very clearly with fairly large expressions.
|
||||
|
||||
Example grammar:
|
||||
|
||||
e : e '*' e
|
||||
| e '+' e
|
||||
| INT
|
||||
;
|
||||
|
||||
We convert that to the following:
|
||||
|
||||
e[int prec]
|
||||
: INT
|
||||
( {3>=prec}? '*' e[4]
|
||||
| {2>=prec}? '+' e[3]
|
||||
)*
|
||||
;
|
||||
|
||||
The (..)* loop has a decision for the inner block as well as
|
||||
an enter or exit decision, which is what concerns us here. At
|
||||
the 1st + of input 1+2+3, the loop entry sees both predicates
|
||||
and the loop exit also sees both predicates by falling off the
|
||||
edge of e. This is because we have no stack information with
|
||||
SLL and find the follow of e, which will hit the return states
|
||||
inside the loop after e[4] and e[3], which brings it back to
|
||||
the enter or exit decision. In this case, we know that we
|
||||
cannot evaluate those predicates because we have fallen off
|
||||
the edge of the stack and will in general not know which prec
|
||||
parameter is the right one to use in the predicate.
|
||||
|
||||
Because we have special information, that these are precedence
|
||||
predicates, we can resolve them without failing over to full
|
||||
LL despite their context sensitive nature. We make an
|
||||
assumption that prec[-1] <= prec[0], meaning that the current
|
||||
precedence level is greater than or equal to the precedence
|
||||
level of recursive invocations above us in the stack. For
|
||||
example, if predicate {3>=prec}? is true of the current prec,
|
||||
then one option is to enter the loop to match it now. The
|
||||
other option is to exit the loop and the left recursive rule
|
||||
to match the current operator in rule invocation further up
|
||||
the stack. But, we know that all of those prec are lower or
|
||||
the same value and so we can decide to enter the loop instead
|
||||
of matching it later. That means we can strip out the other
|
||||
configuration for the exit branch.
|
||||
|
||||
So imagine we have (14,1,$,{2>=prec}?) and then
|
||||
(14,2,$-dipsIntoOuterContext,{2>=prec}?). The optimization
|
||||
allows us to collapse these two configurations. We know that
|
||||
if {2>=prec}? is true for the current prec parameter, it will
|
||||
also be true for any prec from an invoking e call, indicated
|
||||
by dipsIntoOuterContext. As the predicates are both true, we
|
||||
have the option to evaluate them early in the decision start
|
||||
state. We do this by stripping both predicates and choosing to
|
||||
enter the loop as it is consistent with the notion of operator
|
||||
precedence. It's also how the full LL conflict resolution
|
||||
would work.
|
||||
|
||||
The solution requires a different DFA start state for each
|
||||
precedence level.
|
||||
|
||||
The basic filter mechanism is to remove configurations of the
|
||||
form (p, 2, pi) if (p, 1, pi) exists for the same p and pi. In
|
||||
other words, for the same ATN state and predicate context,
|
||||
remove any configuration associated with an exit branch if
|
||||
there is a configuration associated with the enter branch.
|
||||
|
||||
It's also the case that the filter evaluates precedence
|
||||
predicates and resolves conflicts according to precedence
|
||||
levels. For example, for input 1+2+3 at the first +, we see
|
||||
prediction filtering
|
||||
|
||||
[(11,1,[$],{3>=prec}?), (14,1,[$],{2>=prec}?), (5,2,[$],up=1),
|
||||
(11,2,[$],up=1), (14,2,[$],up=1)],hasSemanticContext=true,dipsIntoOuterContext
|
||||
|
||||
to
|
||||
|
||||
[(11,1,[$]), (14,1,[$]), (5,2,[$],up=1)],dipsIntoOuterContext
|
||||
|
||||
This filters because {3>=prec}? evals to true and collapses
|
||||
(11,1,[$],{3>=prec}?) and (11,2,[$],up=1) since early conflict
|
||||
resolution based upon rules of operator precedence fits with
|
||||
our usual match first alt upon conflict.
|
||||
|
||||
We noticed a problem where a recursive call resets precedence
|
||||
to 0. Sam's fix: each config has flag indicating if it has
|
||||
returned from an expr[0] call. then just don't filter any
|
||||
config with that flag set. flag is carried along in
|
||||
closure(). so to avoid adding field, set bit just under sign
|
||||
bit of dipsIntoOuterContext (SUPPRESS_PRECEDENCE_FILTER).
|
||||
With the change you filter "unless (p, 2, pi) was reached
|
||||
after leaving the rule stop state of the LR rule containing
|
||||
state p, corresponding to a rule invocation with precedence
|
||||
level 0"
|
||||
*/
|
||||
|
||||
/**
|
||||
* This method transforms the start state computed by
|
||||
* {@link #computeStartState} to the special start state used by a
|
||||
* precedence DFA for a particular precedence value. The transformation
|
||||
* process applies the following changes to the start state's configuration
|
||||
* set.
|
||||
*
|
||||
* <ol>
|
||||
* <li>Evaluate the precedence predicates for each configuration using
|
||||
* {@link SemanticContext#evalPrecedence}.</li>
|
||||
* <li>When {@link ATNConfig#isPrecedenceFilterSuppressed} is {@code false},
|
||||
* remove all configurations which predict an alternative greater than 1,
|
||||
* for which another configuration that predicts alternative 1 is in the
|
||||
* same ATN state with the same prediction context. This transformation is
|
||||
* valid for the following reasons:
|
||||
* <ul>
|
||||
* <li>The closure block cannot contain any epsilon transitions which bypass
|
||||
* the body of the closure, so all states reachable via alternative 1 are
|
||||
* part of the precedence alternatives of the transformed left-recursive
|
||||
* rule.</li>
|
||||
* <li>The "primary" portion of a left recursive rule cannot contain an
|
||||
* epsilon transition, so the only way an alternative other than 1 can exist
|
||||
* in a state that is also reachable via alternative 1 is by nesting calls
|
||||
* to the left-recursive rule, with the outer calls not being at the
|
||||
* preferred precedence level. The
|
||||
* {@link ATNConfig#isPrecedenceFilterSuppressed} property marks ATN
|
||||
* configurations which do not meet this condition, and therefore are not
|
||||
* eligible for elimination during the filtering process.</li>
|
||||
* </ul>
|
||||
* </li>
|
||||
* </ol>
|
||||
*
|
||||
* <p>
|
||||
* The prediction context must be considered by this filter to address
|
||||
* situations like the following.
|
||||
* </p>
|
||||
* <code>
|
||||
* <pre>
|
||||
* grammar TA;
|
||||
* prog: statement* EOF;
|
||||
* statement: letterA | statement letterA 'b' ;
|
||||
* letterA: 'a';
|
||||
* </pre>
|
||||
* </code>
|
||||
* <p>
|
||||
* If the above grammar, the ATN state immediately before the token
|
||||
* reference {@code 'a'} in {@code letterA} is reachable from the left edge
|
||||
* of both the primary and closure blocks of the left-recursive rule
|
||||
* {@code statement}. The prediction context associated with each of these
|
||||
* configurations distinguishes between them, and prevents the alternative
|
||||
* which stepped out to {@code prog} (and then back in to {@code statement}
|
||||
* from being eliminated by the filter.
|
||||
* </p>
|
||||
*
|
||||
* @param configs The configuration set computed by
|
||||
* {@link #computeStartState} as the start state for the DFA.
|
||||
* @return The transformed configuration set representing the start state
|
||||
* for a precedence DFA at a particular precedence level (determined by
|
||||
* calling {@link Parser#getPrecedence}).
|
||||
*/
|
||||
std::unique_ptr<ATNConfigSet> applyPrecedenceFilter(ATNConfigSet *configs);
|
||||
|
||||
virtual ATNState *getReachableTarget(const Transition *trans, size_t ttype);
|
||||
|
||||
virtual std::vector<Ref<const SemanticContext>> getPredsForAmbigAlts(const antlrcpp::BitSet &ambigAlts,
|
||||
ATNConfigSet *configs, size_t nalts);
|
||||
|
||||
std::vector<dfa::DFAState::PredPrediction> getPredicatePredictions(const antlrcpp::BitSet &ambigAlts,
|
||||
const std::vector<Ref<const SemanticContext>> &altToPred);
|
||||
|
||||
/**
|
||||
* This method is used to improve the localization of error messages by
|
||||
* choosing an alternative rather than throwing a
|
||||
* {@link NoViableAltException} in particular prediction scenarios where the
|
||||
* {@link #ERROR} state was reached during ATN simulation.
|
||||
*
|
||||
* <p>
|
||||
* The default implementation of this method uses the following
|
||||
* algorithm to identify an ATN configuration which successfully parsed the
|
||||
* decision entry rule. Choosing such an alternative ensures that the
|
||||
* {@link ParserRuleContext} returned by the calling rule will be complete
|
||||
* and valid, and the syntax error will be reported later at a more
|
||||
* localized location.</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li>If a syntactically valid path or paths reach the end of the decision rule and
|
||||
* they are semantically valid if predicated, return the min associated alt.</li>
|
||||
* <li>Else, if a semantically invalid but syntactically valid path exist
|
||||
* or paths exist, return the minimum associated alt.
|
||||
* </li>
|
||||
* <li>Otherwise, return {@link ATN#INVALID_ALT_NUMBER}.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>
|
||||
* In some scenarios, the algorithm described above could predict an
|
||||
* alternative which will result in a {@link FailedPredicateException} in
|
||||
* the parser. Specifically, this could occur if the <em>only</em> configuration
|
||||
* capable of successfully parsing to the end of the decision rule is
|
||||
* blocked by a semantic predicate. By choosing this alternative within
|
||||
* {@link #adaptivePredict} instead of throwing a
|
||||
* {@link NoViableAltException}, the resulting
|
||||
* {@link FailedPredicateException} in the parser will identify the specific
|
||||
* predicate which is preventing the parser from successfully parsing the
|
||||
* decision rule, which helps developers identify and correct logic errors
|
||||
* in semantic predicates.
|
||||
* </p>
|
||||
*
|
||||
* @param configs The ATN configurations which were valid immediately before
|
||||
* the {@link #ERROR} state was reached
|
||||
* @param outerContext The is the \gamma_0 initial parser context from the paper
|
||||
* or the parser stack at the instant before prediction commences.
|
||||
*
|
||||
* @return The value to return from {@link #adaptivePredict}, or
|
||||
* {@link ATN#INVALID_ALT_NUMBER} if a suitable alternative was not
|
||||
* identified and {@link #adaptivePredict} should report an error instead.
|
||||
*/
|
||||
size_t getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(ATNConfigSet *configs,
|
||||
ParserRuleContext *outerContext);
|
||||
|
||||
virtual size_t getAltThatFinishedDecisionEntryRule(ATNConfigSet *configs);
|
||||
|
||||
/** Walk the list of configurations and split them according to
|
||||
* those that have preds evaluating to true/false. If no pred, assume
|
||||
* true pred and include in succeeded set. Returns Pair of sets.
|
||||
*
|
||||
* Create a new set so as not to alter the incoming parameter.
|
||||
*
|
||||
* Assumption: the input stream has been restored to the starting point
|
||||
* prediction, which is where predicates need to evaluate.
|
||||
*/
|
||||
std::pair<ATNConfigSet *, ATNConfigSet *> splitAccordingToSemanticValidity(ATNConfigSet *configs,
|
||||
ParserRuleContext *outerContext);
|
||||
|
||||
/// <summary>
|
||||
/// Look through a list of predicate/alt pairs, returning alts for the
|
||||
/// pairs that win. A {@code NONE} predicate indicates an alt containing an
|
||||
/// unpredicated config which behaves as "always true." If !complete
|
||||
/// then we stop at the first predicate that evaluates to true. This
|
||||
/// includes pairs with null predicates.
|
||||
/// </summary>
|
||||
antlrcpp::BitSet evalSemanticContext(const std::vector<dfa::DFAState::PredPrediction> &predPredictions,
|
||||
ParserRuleContext *outerContext, bool complete);
|
||||
|
||||
/**
|
||||
* Evaluate a semantic context within a specific parser context.
|
||||
*
|
||||
* <p>
|
||||
* This method might not be called for every semantic context evaluated
|
||||
* during the prediction process. In particular, we currently do not
|
||||
* evaluate the following but it may change in the future:</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li>Precedence predicates (represented by
|
||||
* {@link SemanticContext.PrecedencePredicate}) are not currently evaluated
|
||||
* through this method.</li>
|
||||
* <li>Operator predicates (represented by {@link SemanticContext.AND} and
|
||||
* {@link SemanticContext.OR}) are evaluated as a single semantic
|
||||
* context, rather than evaluating the operands individually.
|
||||
* Implementations which require evaluation results from individual
|
||||
* predicates should override this method to explicitly handle evaluation of
|
||||
* the operands within operator predicates.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param pred The semantic context to evaluate
|
||||
* @param parserCallStack The parser context in which to evaluate the
|
||||
* semantic context
|
||||
* @param alt The alternative which is guarded by {@code pred}
|
||||
* @param fullCtx {@code true} if the evaluation is occurring during LL
|
||||
* prediction; otherwise, {@code false} if the evaluation is occurring
|
||||
* during SLL prediction
|
||||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
virtual bool evalSemanticContext(Ref<const SemanticContext> const& pred, ParserRuleContext *parserCallStack,
|
||||
size_t alt, bool fullCtx);
|
||||
|
||||
/* TODO: If we are doing predicates, there is no point in pursuing
|
||||
closure operations if we reach a DFA state that uniquely predicts
|
||||
alternative. We will not be caching that DFA state and it is a
|
||||
waste to pursue the closure. Might have to advance when we do
|
||||
ambig detection thought :(
|
||||
*/
|
||||
virtual void closure(Ref<ATNConfig> const& config, ATNConfigSet *configs, ATNConfig::Set &closureBusy,
|
||||
bool collectPredicates, bool fullCtx, bool treatEofAsEpsilon);
|
||||
|
||||
virtual void closureCheckingStopState(Ref<ATNConfig> const& config, ATNConfigSet *configs, ATNConfig::Set &closureBusy,
|
||||
bool collectPredicates, bool fullCtx, int depth, bool treatEofAsEpsilon);
|
||||
|
||||
/// Do the actual work of walking epsilon edges.
|
||||
virtual void closure_(Ref<ATNConfig> const& config, ATNConfigSet *configs, ATNConfig::Set &closureBusy,
|
||||
bool collectPredicates, bool fullCtx, int depth, bool treatEofAsEpsilon);
|
||||
|
||||
virtual Ref<ATNConfig> getEpsilonTarget(Ref<ATNConfig> const& config, const Transition *t, bool collectPredicates,
|
||||
bool inContext, bool fullCtx, bool treatEofAsEpsilon);
|
||||
virtual Ref<ATNConfig> actionTransition(Ref<ATNConfig> const& config, const ActionTransition *t);
|
||||
|
||||
virtual Ref<ATNConfig> predTransition(Ref<ATNConfig> const& config, const PredicateTransition *pt, bool collectPredicates,
|
||||
bool inContext, bool fullCtx);
|
||||
|
||||
virtual Ref<ATNConfig> ruleTransition(Ref<ATNConfig> const& config, const RuleTransition *t);
|
||||
|
||||
/**
|
||||
* Gets a {@link BitSet} containing the alternatives in {@code configs}
|
||||
* which are part of one or more conflicting alternative subsets.
|
||||
*
|
||||
* @param configs The {@link ATNConfigSet} to analyze.
|
||||
* @return The alternatives in {@code configs} which are part of one or more
|
||||
* conflicting alternative subsets. If {@code configs} does not contain any
|
||||
* conflicting subsets, this method returns an empty {@link BitSet}.
|
||||
*/
|
||||
virtual antlrcpp::BitSet getConflictingAlts(ATNConfigSet *configs);
|
||||
|
||||
/// <summary>
|
||||
/// Sam pointed out a problem with the previous definition, v3, of
|
||||
/// ambiguous states. If we have another state associated with conflicting
|
||||
/// alternatives, we should keep going. For example, the following grammar
|
||||
///
|
||||
/// s : (ID | ID ID?) ';' ;
|
||||
///
|
||||
/// When the ATN simulation reaches the state before ';', it has a DFA
|
||||
/// state that looks like: [12|1|[], 6|2|[], 12|2|[]]. Naturally
|
||||
/// 12|1|[] and 12|2|[] conflict, but we cannot stop processing this node
|
||||
/// because alternative to has another way to continue, via [6|2|[]].
|
||||
/// The key is that we have a single state that has config's only associated
|
||||
/// with a single alternative, 2, and crucially the state transitions
|
||||
/// among the configurations are all non-epsilon transitions. That means
|
||||
/// we don't consider any conflicts that include alternative 2. So, we
|
||||
/// ignore the conflict between alts 1 and 2. We ignore a set of
|
||||
/// conflicting alts when there is an intersection with an alternative
|
||||
/// associated with a single alt state in the state->config-list map.
|
||||
///
|
||||
/// It's also the case that we might have two conflicting configurations but
|
||||
/// also a 3rd nonconflicting configuration for a different alternative:
|
||||
/// [1|1|[], 1|2|[], 8|3|[]]. This can come about from grammar:
|
||||
///
|
||||
/// a : A | A | A B ;
|
||||
///
|
||||
/// After matching input A, we reach the stop state for rule A, state 1.
|
||||
/// State 8 is the state right before B. Clearly alternatives 1 and 2
|
||||
/// conflict and no amount of further lookahead will separate the two.
|
||||
/// However, alternative 3 will be able to continue and so we do not
|
||||
/// stop working on this state. In the previous example, we're concerned
|
||||
/// with states associated with the conflicting alternatives. Here alt
|
||||
/// 3 is not associated with the conflicting configs, but since we can continue
|
||||
/// looking for input reasonably, I don't declare the state done. We
|
||||
/// ignore a set of conflicting alts when we have an alternative
|
||||
/// that we still need to pursue.
|
||||
/// </summary>
|
||||
|
||||
virtual antlrcpp::BitSet getConflictingAltsOrUniqueAlt(ATNConfigSet *configs);
|
||||
|
||||
virtual NoViableAltException noViableAlt(TokenStream *input, ParserRuleContext *outerContext,
|
||||
ATNConfigSet *configs, size_t startIndex, bool deleteConfigs);
|
||||
|
||||
static size_t getUniqueAlt(ATNConfigSet *configs);
|
||||
|
||||
/// <summary>
|
||||
/// Add an edge to the DFA, if possible. This method calls
|
||||
/// <seealso cref="#addDFAState"/> to ensure the {@code to} state is present in the
|
||||
/// DFA. If {@code from} is {@code null}, or if {@code t} is outside the
|
||||
/// range of edges that can be represented in the DFA tables, this method
|
||||
/// returns without adding the edge to the DFA.
|
||||
/// <p/>
|
||||
/// If {@code to} is {@code null}, this method returns {@code null}.
|
||||
/// Otherwise, this method returns the <seealso cref="DFAState"/> returned by calling
|
||||
/// <seealso cref="#addDFAState"/> for the {@code to} state.
|
||||
/// </summary>
|
||||
/// <param name="dfa"> The DFA </param>
|
||||
/// <param name="from"> The source state for the edge </param>
|
||||
/// <param name="t"> The input symbol </param>
|
||||
/// <param name="to"> The target state for the edge
|
||||
/// </param>
|
||||
/// <returns> If {@code to} is {@code null}, this method returns {@code null};
|
||||
/// otherwise this method returns the result of calling <seealso cref="#addDFAState"/>
|
||||
/// on {@code to} </returns>
|
||||
virtual dfa::DFAState *addDFAEdge(dfa::DFA &dfa, dfa::DFAState *from, ssize_t t, dfa::DFAState *to);
|
||||
|
||||
/// <summary>
|
||||
/// Add state {@code D} to the DFA if it is not already present, and return
|
||||
/// the actual instance stored in the DFA. If a state equivalent to {@code D}
|
||||
/// is already in the DFA, the existing state is returned. Otherwise this
|
||||
/// method returns {@code D} after adding it to the DFA.
|
||||
/// <p/>
|
||||
/// If {@code D} is <seealso cref="#ERROR"/>, this method returns <seealso cref="#ERROR"/> and
|
||||
/// does not change the DFA.
|
||||
/// </summary>
|
||||
/// <param name="dfa"> The dfa </param>
|
||||
/// <param name="D"> The DFA state to add </param>
|
||||
/// <returns> The state stored in the DFA. This will be either the existing
|
||||
/// state if {@code D} is already in the DFA, or {@code D} itself if the
|
||||
/// state was not already present. </returns>
|
||||
virtual dfa::DFAState *addDFAState(dfa::DFA &dfa, dfa::DFAState *D);
|
||||
|
||||
virtual void reportAttemptingFullContext(dfa::DFA &dfa, const antlrcpp::BitSet &conflictingAlts,
|
||||
ATNConfigSet *configs, size_t startIndex, size_t stopIndex);
|
||||
|
||||
virtual void reportContextSensitivity(dfa::DFA &dfa, size_t prediction, ATNConfigSet *configs,
|
||||
size_t startIndex, size_t stopIndex);
|
||||
|
||||
/// If context sensitive parsing, we know it's ambiguity not conflict.
|
||||
virtual void reportAmbiguity(dfa::DFA &dfa,
|
||||
dfa::DFAState *D, // the DFA state from execATN() that had SLL conflicts
|
||||
size_t startIndex, size_t stopIndex,
|
||||
bool exact,
|
||||
const antlrcpp::BitSet &ambigAlts,
|
||||
ATNConfigSet *configs); // configs that LL not SLL considered conflicting
|
||||
|
||||
private:
|
||||
// SLL, LL, or LL + exact ambig detection?
|
||||
PredictionMode _mode;
|
||||
|
||||
static bool getLrLoopSetting();
|
||||
void InitializeInstanceFields();
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
|
||||
@ -1,50 +0,0 @@
|
||||
// Copyright 2012-2022 The ANTLR Project
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are permitted
|
||||
// provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
// and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of
|
||||
// conditions and the following disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to
|
||||
// endorse or promote products derived from this software without specific prior written
|
||||
// permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
||||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
|
||||
// WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "atn/PredictionContextMergeCacheOptions.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
class ANTLR4CPP_PUBLIC ParserATNSimulatorOptions final {
|
||||
public:
|
||||
ParserATNSimulatorOptions& setPredictionContextMergeCacheOptions(
|
||||
PredictionContextMergeCacheOptions predictionContextMergeCacheOptions) {
|
||||
_predictionContextMergeCacheOptions = std::move(predictionContextMergeCacheOptions);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const PredictionContextMergeCacheOptions& getPredictionContextMergeCacheOptions() const {
|
||||
return _predictionContextMergeCacheOptions;
|
||||
}
|
||||
|
||||
private:
|
||||
PredictionContextMergeCacheOptions _predictionContextMergeCacheOptions;
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
@ -1,17 +0,0 @@
|
||||
/* 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 "SemanticContext.h"
|
||||
|
||||
#include "atn/PredicateEvalInfo.h"
|
||||
|
||||
using namespace antlr4;
|
||||
using namespace antlr4::atn;
|
||||
|
||||
PredicateEvalInfo::PredicateEvalInfo(size_t decision, TokenStream *input, size_t startIndex, size_t stopIndex,
|
||||
Ref<const SemanticContext> semctx, bool evalResult, size_t predictedAlt, bool fullCtx)
|
||||
: DecisionEventInfo(decision, nullptr, input, startIndex, stopIndex, fullCtx),
|
||||
semctx(std::move(semctx)), predictedAlt(predictedAlt), evalResult(evalResult) {
|
||||
}
|
||||
@ -1,62 +0,0 @@
|
||||
/* 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 "atn/DecisionEventInfo.h"
|
||||
|
||||
namespace antlr4 {
|
||||
namespace atn {
|
||||
|
||||
/// <summary>
|
||||
/// This class represents profiling event information for semantic predicate
|
||||
/// evaluations which occur during prediction.
|
||||
/// </summary>
|
||||
/// <seealso cref= ParserATNSimulator#evalSemanticContext
|
||||
///
|
||||
/// @since 4.3 </seealso>
|
||||
class ANTLR4CPP_PUBLIC PredicateEvalInfo : public DecisionEventInfo {
|
||||
public:
|
||||
/// The semantic context which was evaluated.
|
||||
const Ref<const SemanticContext> semctx;
|
||||
|
||||
/// <summary>
|
||||
/// The alternative number for the decision which is guarded by the semantic
|
||||
/// context <seealso cref="#semctx"/>. Note that other ATN
|
||||
/// configurations may predict the same alternative which are guarded by
|
||||
/// other semantic contexts and/or <seealso cref="SemanticContext#NONE"/>.
|
||||
/// </summary>
|
||||
const size_t predictedAlt;
|
||||
|
||||
/// The result of evaluating the semantic context <seealso cref="#semctx"/>.
|
||||
const bool evalResult;
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new instance of the <seealso cref="PredicateEvalInfo"/> class with the
|
||||
/// specified detailed predicate evaluation information.
|
||||
/// </summary>
|
||||
/// <param name="decision"> The decision number </param>
|
||||
/// <param name="input"> The input token stream </param>
|
||||
/// <param name="startIndex"> The start index for the current prediction </param>
|
||||
/// <param name="stopIndex"> The index at which the predicate evaluation was
|
||||
/// triggered. Note that the input stream may be reset to other positions for
|
||||
/// the actual evaluation of individual predicates. </param>
|
||||
/// <param name="semctx"> The semantic context which was evaluated </param>
|
||||
/// <param name="evalResult"> The results of evaluating the semantic context </param>
|
||||
/// <param name="predictedAlt"> The alternative number for the decision which is
|
||||
/// guarded by the semantic context {@code semctx}. See <seealso cref="#predictedAlt"/>
|
||||
/// for more information. </param>
|
||||
/// <param name="fullCtx"> {@code true} if the semantic context was
|
||||
/// evaluated during LL prediction; otherwise, {@code false} if the semantic
|
||||
/// context was evaluated during SLL prediction
|
||||
/// </param>
|
||||
/// <seealso cref= ParserATNSimulator#evalSemanticContext(SemanticContext, ParserRuleContext, int, boolean) </seealso>
|
||||
/// <seealso cref= SemanticContext#eval(Recognizer, RuleContext) </seealso>
|
||||
PredicateEvalInfo(size_t decision, TokenStream *input, size_t startIndex, size_t stopIndex,
|
||||
Ref<const SemanticContext> semctx, bool evalResult, size_t predictedAlt, bool fullCtx);
|
||||
};
|
||||
|
||||
} // namespace atn
|
||||
} // namespace antlr4
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue