@@ -63,7 +63,7 @@ extension Parser on ParserState {
6363 bool get enableDriftExtensions => options.useDriftExtensions;
6464
6565 void _suggestHint (HintDescription description) {
66- autoComplete? .addHint (Hint (tokens.previous , description));
66+ autoComplete? .addHint (Hint (tokens.lastConsumedToken , description));
6767 }
6868
6969 void _suggestHintForTokens (Iterable <TokenType > types) {
@@ -81,7 +81,7 @@ extension Parser on ParserState {
8181 bool get _isAtEnd => _peek.type == TokenType .eof;
8282 Token get _peek => tokens.lookahead1 ();
8383
84- Token get _previous => tokens.previous ! ;
84+ Token get _previous => tokens.lastConsumedToken ! ;
8585
8686 bool _match (Iterable <TokenType > types) {
8787 if (_reportAutoComplete) _suggestHintForTokens (types);
@@ -114,14 +114,14 @@ extension Parser on ParserState {
114114 /// "NOT" followed by [type] . Does not consume any tokens.
115115 bool _checkWithNot (TokenType type) {
116116 if (_check (type)) return true ;
117- final (peek, next) = tokens.lookahead2 ();
117+ final (peek, next) = tokens.keywordLookahead2 ();
118118 return peek.type == TokenType .not && next? .type == type;
119119 }
120120
121121 /// Like [_checkWithNot] , but with more than one token type.
122122 bool _checkAnyWithNot (List <TokenType > types) {
123123 if (types.any (_check)) return true ;
124- final (peek, next) = tokens.lookahead2 ();
124+ final (peek, next) = tokens.keywordLookahead2 ();
125125 return peek.type == TokenType .not && types.contains (next? .type);
126126 }
127127
@@ -313,16 +313,20 @@ extension Parser on ParserState {
313313 }
314314
315315 DriftFile driftFile () {
316+ tokens.scanner.isInTopLevelDriftFile = true ;
316317 final first = _peek;
317- final foundComponents = < PartOfDriftFile ? > [];
318+ final foundComponents = < PartOfDriftFile > [];
318319
319- while (! _isAtEnd ) {
320- foundComponents. add ( _parseAsStatement (_partOfDriftFile)) ;
321- }
320+ while (true ) {
321+ tokens.scanner.isInTopLevelDriftFile = true ;
322+ if (_isAtEnd) break ;
322323
323- foundComponents.removeWhere ((c) => c == null );
324+ if (_parseAsStatement (_partOfDriftFile) case final component? ) {
325+ foundComponents.add (component);
326+ }
327+ }
324328
325- final file = DriftFile (foundComponents. cast () );
329+ final file = DriftFile (foundComponents);
326330 if (foundComponents.isNotEmpty) {
327331 file.setSpan (first, _previous);
328332 } else {
@@ -334,6 +338,9 @@ extension Parser on ParserState {
334338 }
335339
336340 PartOfDriftFile _partOfDriftFile () {
341+ _peek;
342+ tokens.scanner.isInTopLevelDriftFile = false ;
343+
337344 final found = _import () ?? _create () ?? _declaredStatement ();
338345
339346 if (found != null ) {
@@ -3070,7 +3077,7 @@ final class _ExpressionParser extends ParserState {
30703077
30713078 if (_peek is KeywordToken ) {
30723079 // Improve error messages for possible function calls, https://github.com/simolus3/drift/discussions/2277
3073- final (_, next) = tokens.lookahead2 ();
3080+ final (_, next) = tokens.keywordLookahead2 ();
30743081 if (next? .type == TokenType .leftParen) {
30753082 _error (
30763083 'Expected an expression here, but got a reserved keyword. Did you '
0 commit comments