Skip to content

Commit 95d53d6

Browse files
committed
finish a basic build of antlr
1 parent 02a09a5 commit 95d53d6

File tree

173 files changed

+14592
-16
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+14592
-16
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2+
* Use of this file is governed by the BSD 3-clause license that
3+
* can be found in the LICENSE.txt file in the project root.
4+
*/
5+
6+
#pragma once
7+
8+
#include "RecognitionException.h"
9+
10+
namespace antlrcpp {
11+
class BitSet;
12+
}
13+
14+
namespace antlr4 {
15+
16+
/// How to emit recognition errors (an interface in Java).
17+
class ANTLR4CPP_PUBLIC ANTLRErrorListener {
18+
public:
19+
virtual ~ANTLRErrorListener();
20+
21+
/// <summary>
22+
/// Upon syntax error, notify any interested parties. This is not how to
23+
/// recover from errors or compute error messages. <seealso cref="ANTLRErrorStrategy"/>
24+
/// specifies how to recover from syntax errors and how to compute error
25+
/// messages. This listener's job is simply to emit a computed message,
26+
/// though it has enough information to create its own message in many cases.
27+
/// <p/>
28+
/// The <seealso cref="RecognitionException"/> is non-null for all syntax errors except
29+
/// when we discover mismatched token errors that we can recover from
30+
/// in-line, without returning from the surrounding rule (via the single
31+
/// token insertion and deletion mechanism).
32+
/// </summary>
33+
/// <param name="recognizer">
34+
/// What parser got the error. From this
35+
/// object, you can access the context as well
36+
/// as the input stream. </param>
37+
/// <param name="offendingSymbol">
38+
/// The offending token in the input token
39+
/// stream, unless recognizer is a lexer (then it's null). If
40+
/// no viable alternative error, {@code e} has token at which we
41+
/// started production for the decision. </param>
42+
/// <param name="line">
43+
/// The line number in the input where the error occurred. </param>
44+
/// <param name="charPositionInLine">
45+
/// The character position within that line where the error occurred. </param>
46+
/// <param name="msg">
47+
/// The message to emit. </param>
48+
/// <param name="e">
49+
/// The exception generated by the parser that led to
50+
/// the reporting of an error. It is null in the case where
51+
/// the parser was able to recover in line without exiting the
52+
/// surrounding rule. </param>
53+
virtual void syntaxError(Recognizer *recognizer, Token *offendingSymbol, size_t line,
54+
size_t charPositionInLine, const std::string &msg, std::exception_ptr e) = 0;
55+
56+
/**
57+
* This method is called by the parser when a full-context prediction
58+
* results in an ambiguity.
59+
*
60+
* <p>Each full-context prediction which does not result in a syntax error
61+
* will call either {@link #reportContextSensitivity} or
62+
* {@link #reportAmbiguity}.</p>
63+
*
64+
* <p>When {@code ambigAlts} is not null, it contains the set of potentially
65+
* viable alternatives identified by the prediction algorithm. When
66+
* {@code ambigAlts} is null, use {@link ATNConfigSet#getAlts} to obtain the
67+
* represented alternatives from the {@code configs} argument.</p>
68+
*
69+
* <p>When {@code exact} is {@code true}, <em>all</em> of the potentially
70+
* viable alternatives are truly viable, i.e. this is reporting an exact
71+
* ambiguity. When {@code exact} is {@code false}, <em>at least two</em> of
72+
* the potentially viable alternatives are viable for the current input, but
73+
* the prediction algorithm terminated as soon as it determined that at
74+
* least the <em>minimum</em> potentially viable alternative is truly
75+
* viable.</p>
76+
*
77+
* <p>When the {@link PredictionMode#LL_EXACT_AMBIG_DETECTION} prediction
78+
* mode is used, the parser is required to identify exact ambiguities so
79+
* {@code exact} will always be {@code true}.</p>
80+
*
81+
* <p>This method is not used by lexers.</p>
82+
*
83+
* @param recognizer the parser instance
84+
* @param dfa the DFA for the current decision
85+
* @param startIndex the input index where the decision started
86+
* @param stopIndex the input input where the ambiguity was identified
87+
* @param exact {@code true} if the ambiguity is exactly known, otherwise
88+
* {@code false}. This is always {@code true} when
89+
* {@link PredictionMode#LL_EXACT_AMBIG_DETECTION} is used.
90+
* @param ambigAlts the potentially ambiguous alternatives, or {@code null}
91+
* to indicate that the potentially ambiguous alternatives are the complete
92+
* set of represented alternatives in {@code configs}
93+
* @param configs the ATN configuration set where the ambiguity was
94+
* identified
95+
*/
96+
virtual void reportAmbiguity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex, bool exact,
97+
const antlrcpp::BitSet &ambigAlts, atn::ATNConfigSet *configs) = 0;
98+
99+
/**
100+
* This method is called when an SLL conflict occurs and the parser is about
101+
* to use the full context information to make an LL decision.
102+
*
103+
* <p>If one or more configurations in {@code configs} contains a semantic
104+
* predicate, the predicates are evaluated before this method is called. The
105+
* subset of alternatives which are still viable after predicates are
106+
* evaluated is reported in {@code conflictingAlts}.</p>
107+
*
108+
* <p>This method is not used by lexers.</p>
109+
*
110+
* @param recognizer the parser instance
111+
* @param dfa the DFA for the current decision
112+
* @param startIndex the input index where the decision started
113+
* @param stopIndex the input index where the SLL conflict occurred
114+
* @param conflictingAlts The specific conflicting alternatives. If this is
115+
* {@code null}, the conflicting alternatives are all alternatives
116+
* represented in {@code configs}. At the moment, conflictingAlts is non-null
117+
* (for the reference implementation, but Sam's optimized version can see this
118+
* as null).
119+
* @param configs the ATN configuration set where the SLL conflict was
120+
* detected
121+
*/
122+
virtual void reportAttemptingFullContext(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
123+
const antlrcpp::BitSet &conflictingAlts, atn::ATNConfigSet *configs) = 0;
124+
125+
/**
126+
* This method is called by the parser when a full-context prediction has a
127+
* unique result.
128+
*
129+
* <p>Each full-context prediction which does not result in a syntax error
130+
* will call either {@link #reportContextSensitivity} or
131+
* {@link #reportAmbiguity}.</p>
132+
*
133+
* <p>For prediction implementations that only evaluate full-context
134+
* predictions when an SLL conflict is found (including the default
135+
* {@link ParserATNSimulator} implementation), this method reports cases
136+
* where SLL conflicts were resolved to unique full-context predictions,
137+
* i.e. the decision was context-sensitive. This report does not necessarily
138+
* indicate a problem, and it may appear even in completely unambiguous
139+
* grammars.</p>
140+
*
141+
* <p>{@code configs} may have more than one represented alternative if the
142+
* full-context prediction algorithm does not evaluate predicates before
143+
* beginning the full-context prediction. In all cases, the final prediction
144+
* is passed as the {@code prediction} argument.</p>
145+
*
146+
* <p>Note that the definition of "context sensitivity" in this method
147+
* differs from the concept in {@link DecisionInfo#contextSensitivities}.
148+
* This method reports all instances where an SLL conflict occurred but LL
149+
* parsing produced a unique result, whether or not that unique result
150+
* matches the minimum alternative in the SLL conflicting set.</p>
151+
*
152+
* <p>This method is not used by lexers.</p>
153+
*
154+
* @param recognizer the parser instance
155+
* @param dfa the DFA for the current decision
156+
* @param startIndex the input index where the decision started
157+
* @param stopIndex the input index where the context sensitivity was
158+
* finally determined
159+
* @param prediction the unambiguous result of the full-context prediction
160+
* @param configs the ATN configuration set where the unambiguous prediction
161+
* was determined
162+
*/
163+
virtual void reportContextSensitivity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
164+
size_t prediction, atn::ATNConfigSet *configs) = 0;
165+
};
166+
167+
} // namespace antlr4
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2+
* Use of this file is governed by the BSD 3-clause license that
3+
* can be found in the LICENSE.txt file in the project root.
4+
*/
5+
6+
#pragma once
7+
8+
#include "Token.h"
9+
10+
namespace antlr4 {
11+
12+
/// <summary>
13+
/// The interface for defining strategies to deal with syntax errors encountered
14+
/// during a parse by ANTLR-generated parsers. We distinguish between three
15+
/// different kinds of errors:
16+
///
17+
/// <ul>
18+
/// <li>The parser could not figure out which path to take in the ATN (none of
19+
/// the available alternatives could possibly match)</li>
20+
/// <li>The current input does not match what we were looking for</li>
21+
/// <li>A predicate evaluated to false</li>
22+
/// </ul>
23+
///
24+
/// Implementations of this interface report syntax errors by calling
25+
/// <seealso cref="Parser#notifyErrorListeners"/>.
26+
/// <p/>
27+
/// TODO: what to do about lexers
28+
/// </summary>
29+
class ANTLR4CPP_PUBLIC ANTLRErrorStrategy {
30+
public:
31+
32+
/// <summary>
33+
/// Reset the error handler state for the specified {@code recognizer}. </summary>
34+
/// <param name="recognizer"> the parser instance </param>
35+
virtual ~ANTLRErrorStrategy();
36+
37+
virtual void reset(Parser *recognizer) = 0;
38+
39+
/**
40+
* This method is called when an unexpected symbol is encountered during an
41+
* inline match operation, such as {@link Parser#match}. If the error
42+
* strategy successfully recovers from the match failure, this method
43+
* returns the {@link Token} instance which should be treated as the
44+
* successful result of the match.
45+
*
46+
* <p>This method handles the consumption of any tokens - the caller should
47+
* <b>not</b> call {@link Parser#consume} after a successful recovery.</p>
48+
*
49+
* <p>Note that the calling code will not report an error if this method
50+
* returns successfully. The error strategy implementation is responsible
51+
* for calling {@link Parser#notifyErrorListeners} as appropriate.</p>
52+
*
53+
* @param recognizer the parser instance
54+
* @throws RecognitionException if the error strategy was not able to
55+
* recover from the unexpected input symbol
56+
*/
57+
virtual Token* recoverInline(Parser *recognizer) = 0;
58+
59+
/// <summary>
60+
/// This method is called to recover from exception {@code e}. This method is
61+
/// called after <seealso cref="#reportError"/> by the default exception handler
62+
/// generated for a rule method.
63+
/// </summary>
64+
/// <seealso cref= #reportError
65+
/// </seealso>
66+
/// <param name="recognizer"> the parser instance </param>
67+
/// <param name="e"> the recognition exception to recover from </param>
68+
/// <exception cref="RecognitionException"> if the error strategy could not recover from
69+
/// the recognition exception </exception>
70+
virtual void recover(Parser *recognizer, std::exception_ptr e) = 0;
71+
72+
/// <summary>
73+
/// This method provides the error handler with an opportunity to handle
74+
/// syntactic or semantic errors in the input stream before they result in a
75+
/// <seealso cref="RecognitionException"/>.
76+
/// <p/>
77+
/// The generated code currently contains calls to <seealso cref="#sync"/> after
78+
/// entering the decision state of a closure block ({@code (...)*} or
79+
/// {@code (...)+}).
80+
/// <p/>
81+
/// For an implementation based on Jim Idle's "magic sync" mechanism, see
82+
/// <seealso cref="DefaultErrorStrategy#sync"/>.
83+
/// </summary>
84+
/// <seealso cref= DefaultErrorStrategy#sync
85+
/// </seealso>
86+
/// <param name="recognizer"> the parser instance </param>
87+
/// <exception cref="RecognitionException"> if an error is detected by the error
88+
/// strategy but cannot be automatically recovered at the current state in
89+
/// the parsing process </exception>
90+
virtual void sync(Parser *recognizer) = 0;
91+
92+
/// <summary>
93+
/// Tests whether or not {@code recognizer} is in the process of recovering
94+
/// from an error. In error recovery mode, <seealso cref="Parser#consume"/> adds
95+
/// symbols to the parse tree by calling
96+
/// {@link Parser#createErrorNode(ParserRuleContext, Token)} then
97+
/// {@link ParserRuleContext#addErrorNode(ErrorNode)} instead of
98+
/// {@link Parser#createTerminalNode(ParserRuleContext, Token)}.
99+
/// </summary>
100+
/// <param name="recognizer"> the parser instance </param>
101+
/// <returns> {@code true} if the parser is currently recovering from a parse
102+
/// error, otherwise {@code false} </returns>
103+
virtual bool inErrorRecoveryMode(Parser *recognizer) = 0;
104+
105+
/// <summary>
106+
/// This method is called by when the parser successfully matches an input
107+
/// symbol.
108+
/// </summary>
109+
/// <param name="recognizer"> the parser instance </param>
110+
virtual void reportMatch(Parser *recognizer) = 0;
111+
112+
/// <summary>
113+
/// Report any kind of <seealso cref="RecognitionException"/>. This method is called by
114+
/// the default exception handler generated for a rule method.
115+
/// </summary>
116+
/// <param name="recognizer"> the parser instance </param>
117+
/// <param name="e"> the recognition exception to report </param>
118+
virtual void reportError(Parser *recognizer, const RecognitionException &e) = 0;
119+
};
120+
121+
} // namespace antlr4
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2+
* Use of this file is governed by the BSD 3-clause license that
3+
* can be found in the LICENSE.txt file in the project root.
4+
*/
5+
6+
#pragma once
7+
8+
#include "ANTLRInputStream.h"
9+
10+
namespace antlr4 {
11+
12+
/// This is an ANTLRInputStream that is loaded from a file all at once
13+
/// when you construct the object (or call load()).
14+
// TODO: this class needs testing.
15+
class ANTLR4CPP_PUBLIC ANTLRFileStream : public ANTLRInputStream {
16+
public:
17+
ANTLRFileStream() = default;
18+
ANTLRFileStream(const std::string &) = delete;
19+
ANTLRFileStream(const char *data, size_t length) = delete;
20+
ANTLRFileStream(std::istream &stream) = delete;
21+
22+
// Assumes a file name encoded in UTF-8 and file content in the same encoding (with or w/o BOM).
23+
virtual void loadFromFile(const std::string &fileName);
24+
virtual std::string getSourceName() const override;
25+
26+
private:
27+
std::string _fileName; // UTF-8 encoded file name.
28+
};
29+
30+
} // namespace antlr4

0 commit comments

Comments
 (0)