@@ -527,20 +527,23 @@ Retroactively inspecting or modifying the parser's output can be confusing, so
527
527
using this function should be avoided where possible.
528
528
"""
529
529
function peek_behind (stream:: ParseStream , pos:: ParseStreamPosition )
530
- if token_is_last (stream, pos) && ! isempty (stream . tokens)
530
+ if token_is_last (stream, pos) && pos . token_index > 0
531
531
t = stream. tokens[pos. token_index]
532
532
return (kind= kind (t),
533
533
flags= flags (t),
534
534
orig_kind= t. orig_kind,
535
535
is_leaf= true )
536
- elseif ! isempty (stream. ranges)
536
+ elseif ! isempty (stream. ranges) && pos . range_index > 0
537
537
r = stream. ranges[pos. range_index]
538
538
return (kind= kind (r),
539
539
flags= flags (r),
540
540
orig_kind= K " None" ,
541
541
is_leaf= false )
542
542
else
543
- internal_error (" Can't peek behind at start of stream" )
543
+ return (kind= K " None" ,
544
+ flags= EMPTY_FLAGS,
545
+ orig_kind= K " None" ,
546
+ is_leaf= true )
544
547
end
545
548
end
546
549
@@ -585,7 +588,11 @@ function first_child_position(stream::ParseStream, pos::ParseStreamPosition)
585
588
end
586
589
end
587
590
588
- function peek_behind (stream:: ParseStream ; skip_trivia:: Bool = true )
591
+ # Get last position in stream "of interest", skipping
592
+ # * parens nodes
593
+ # * deleted tokens (TOMBSTONE)
594
+ # * whitespace (if skip_trivia=true)
595
+ function peek_behind_pos (stream:: ParseStream ; skip_trivia:: Bool = true )
589
596
token_index = lastindex (stream. tokens)
590
597
range_index = lastindex (stream. ranges)
591
598
while range_index >= firstindex (stream. ranges) &&
@@ -601,11 +608,11 @@ function peek_behind(stream::ParseStream; skip_trivia::Bool=true)
601
608
end
602
609
token_index -= 1
603
610
end
604
- if token_index > 0
605
- return peek_behind (stream, ParseStreamPosition (token_index, range_index))
606
- else
607
- internal_error ( " Can't peek behind at start of stream" )
608
- end
611
+ return ParseStreamPosition ( token_index, range_index)
612
+ end
613
+
614
+ function peek_behind ( stream:: ParseStream ; skip_trivia :: Bool = true )
615
+ peek_behind (stream, peek_behind_pos (stream; skip_trivia = skip_trivia))
609
616
end
610
617
611
618
# -------------------------------------------------------------------------------
0 commit comments