Skip to content

Commit 0b7c83b

Browse files
committed
Add skip_whitespace to peek() functions
This allows whitespace to be inspected in some special cases.
1 parent 77b4044 commit 0b7c83b

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/parse_stream.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,28 +294,34 @@ function _lookahead_index(stream::ParseStream, n::Integer, skip_newlines::Bool)
294294
end
295295

296296
"""
297-
peek(stream [, n=1])
297+
peek(stream [, n=1]; skip_newlines=false)
298298
299299
Look ahead in the stream `n` tokens, returning the token kind. Comments and
300300
non-newline whitespace are skipped automatically. Whitespace containing a
301301
single newline is returned as kind `K"NewlineWs"` unless `skip_newlines` is
302302
true.
303303
"""
304-
function Base.peek(stream::ParseStream, n::Integer=1; skip_newlines::Bool=false)
305-
kind(peek_token(stream, n; skip_newlines=skip_newlines))
304+
function Base.peek(stream::ParseStream, n::Integer=1;
305+
skip_newlines::Bool=false, skip_whitespace=true)
306+
kind(peek_token(stream, n; skip_newlines=skip_newlines, skip_whitespace=skip_whitespace))
306307
end
307308

308309
"""
309310
peek_token(stream [, n=1])
310311
311312
Like `peek`, but return the full token information rather than just the kind.
312313
"""
313-
function peek_token(stream::ParseStream, n::Integer=1; skip_newlines=false)
314+
function peek_token(stream::ParseStream, n::Integer=1;
315+
skip_newlines=false, skip_whitespace=true)
314316
stream.peek_count += 1
315317
if stream.peek_count > 100_000
316318
error("The parser seems stuck at byte $(stream.next_byte)")
317319
end
318-
stream.lookahead[_lookahead_index(stream, n, skip_newlines)]
320+
i = _lookahead_index(stream, n, skip_newlines)
321+
if !skip_whitespace
322+
i = 1
323+
end
324+
return stream.lookahead[i]
319325
end
320326

321327
function _peek_behind_fields(ranges, i)

0 commit comments

Comments
 (0)