@@ -39,7 +39,12 @@ template <typename T> class ParserResult {
39
39
IsCodeCompletion = 0x2 ,
40
40
};
41
41
42
- template <typename U> friend class ParserResult ;
42
+ template <typename U>
43
+ friend class ParserResult ;
44
+
45
+ template <typename U>
46
+ friend inline ParserResult<U> makeParserResult (ParserStatus Status,
47
+ U *Result);
43
48
44
49
public:
45
50
// / Construct a null result with error bit set.
@@ -78,21 +83,35 @@ template <typename T> class ParserResult {
78
83
// / Return the AST node or a null pointer.
79
84
T *getPtrOrNull () const { return PtrAndBits.getPointer (); }
80
85
81
- // / Return true if there was a parse error.
86
+ // / Return true if there was a parse error that the parser has not yet
87
+ // / recovered from.
82
88
// /
83
89
// / Note that we can still have an AST node which was constructed during
84
90
// / recovery.
85
91
bool isParseError () const { return PtrAndBits.getInt () & IsError; }
86
92
93
+ // / Return true if there was a parse error that the parser has not yet
94
+ // / recovered from, or if we found a code completion token while parsing.
95
+ // /
96
+ // / Note that we can still have an AST node which was constructed during
97
+ // / recovery.
98
+ bool isParseErrorOrHasCompletion () const {
99
+ return PtrAndBits.getInt () & (IsError | IsCodeCompletion);
100
+ }
101
+
87
102
// / Return true if we found a code completion token while parsing this.
88
103
bool hasCodeCompletion () const {
89
104
return PtrAndBits.getInt () & IsCodeCompletion;
90
105
}
91
106
92
107
void setIsParseError () { PtrAndBits.setInt (PtrAndBits.getInt () | IsError); }
108
+ void setHasCodeCompletionAndIsError () {
109
+ PtrAndBits.setInt (PtrAndBits.getInt () | IsError | IsCodeCompletion);
110
+ }
93
111
112
+ private:
94
113
void setHasCodeCompletion () {
95
- PtrAndBits.setInt (PtrAndBits.getInt () | IsError | IsCodeCompletion);
114
+ PtrAndBits.setInt (PtrAndBits.getInt () | IsCodeCompletion);
96
115
}
97
116
};
98
117
@@ -119,7 +138,7 @@ static inline ParserResult<T> makeParserCodeCompletionResult(T *Result =
119
138
ParserResult<T> PR;
120
139
if (Result)
121
140
PR = ParserResult<T>(Result);
122
- PR.setHasCodeCompletion ();
141
+ PR.setHasCodeCompletionAndIsError ();
123
142
return PR;
124
143
}
125
144
@@ -145,27 +164,32 @@ class ParserStatus {
145
164
if (Result.isParseError ())
146
165
setIsParseError ();
147
166
if (Result.hasCodeCompletion ())
148
- setHasCodeCompletion () ;
167
+ IsCodeCompletion = true ;
149
168
}
150
169
170
+ // / Return true if either 1) no errors were encountered while parsing this,
171
+ // / or 2) there were errors but the the parser already recovered from them.
151
172
bool isSuccess () const { return !isError (); }
152
- bool isError () const { return IsError; }
173
+ bool isErrorOrHasCompletion () const { return IsError || IsCodeCompletion ; }
153
174
154
175
// / Return true if we found a code completion token while parsing this.
155
176
bool hasCodeCompletion () const { return IsCodeCompletion; }
156
177
178
+ // / Return true if we encountered any errors while parsing this that the
179
+ // / parser hasn't yet recovered from.
180
+ bool isError () const { return IsError; }
181
+
157
182
void setIsParseError () {
158
183
IsError = true ;
159
184
}
160
185
161
- void setHasCodeCompletion () {
162
- IsError = true ;
163
- IsCodeCompletion = true ;
186
+ void clearIsError () {
187
+ IsError = false ;
164
188
}
165
189
166
- // / True if we should stop parsing for any reason.
167
- bool shouldStopParsing () const {
168
- return IsError || IsCodeCompletion ;
190
+ void setHasCodeCompletionAndIsError () {
191
+ IsError = true ;
192
+ IsCodeCompletion = true ;
169
193
}
170
194
171
195
ParserStatus &operator |=(ParserStatus RHS) {
@@ -196,19 +220,21 @@ static inline ParserStatus makeParserError() {
196
220
// / Create a status with error and code completion bits set.
197
221
static inline ParserStatus makeParserCodeCompletionStatus () {
198
222
ParserStatus Status;
199
- Status.setHasCodeCompletion ();
223
+ Status.setHasCodeCompletionAndIsError ();
200
224
return Status;
201
225
}
202
226
203
227
// / Create a parser result with specified bits.
204
228
template <typename T>
205
229
static inline ParserResult<T> makeParserResult (ParserStatus Status,
206
230
T *Result) {
207
- if (Status.isSuccess ())
208
- return makeParserResult (Result);
231
+ ParserResult<T> PR = Status.isError ()
232
+ ? makeParserErrorResult (Result)
233
+ : makeParserResult (Result);
234
+
209
235
if (Status.hasCodeCompletion ())
210
- return makeParserCodeCompletionResult (Result );
211
- return makeParserErrorResult (Result) ;
236
+ PR. setHasCodeCompletion ( );
237
+ return PR ;
212
238
}
213
239
214
240
template <typename T> ParserResult<T>::ParserResult(ParserStatus Status) {
0 commit comments