Skip to content

Commit 21c28a9

Browse files
committed
Add placehold kind K"None" and use K"nothing" more precisely
K"nothing" now means the literal Julia `nothing`, and K"None" is used as a placeholder in the code instead. This also allows us to remove the redundant NothingLiteral kind.
1 parent cf16ddc commit 21c28a9

File tree

6 files changed

+17
-18
lines changed

6 files changed

+17
-18
lines changed

Tokenize/src/token_kinds.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@enum(Kind,
2+
NONE, # Placeholder; never emitted by lexer
23
ENDMARKER, # EOF
34
ERROR,
45
COMMENT, # aadsdsa, #= fdsf #=
@@ -51,7 +52,7 @@
5152

5253
begin_cstparser,
5354
INVISIBLE_BRACKETS,
54-
NOTHING,
55+
NOTHING, # A literal `nothing`
5556
WS,
5657
SEMICOLON_WS,
5758
NEWLINE_WS,
@@ -835,7 +836,6 @@
835836
# width tokens to keep the parse tree more uniform.
836837
begin_parser_tokens,
837838
TOMBSTONE, # Empty placeholder for kind to be filled later
838-
NOTHING_LITERAL, # A literal Julia `nothing` in the AST
839839

840840
# Macro names are modelled as a special kind of identifier because the
841841
# @ may not be attached to the macro name in the source (or may not be

src/parse_stream.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ TODO: Optimize this data structure? It's very large at the moment.
121121
"""
122122
struct TaggedRange
123123
head::SyntaxHead # Kind,flags
124-
orig_kind::Kind # Kind of the original token for leaf tokens, or K"Nothing"
124+
orig_kind::Kind # Kind of the original token for leaf tokens, or K"None"
125125
first_byte::UInt32 # First byte in the input text
126126
last_byte::UInt32 # Last byte in the input text
127127
start_mark::UInt32 # Index of first emitted range which this range covers
@@ -362,7 +362,7 @@ end
362362

363363
# Bump the next `n` tokens
364364
# flags and remap_kind are applied to any non-trivia tokens
365-
function _bump_n(stream::ParseStream, n::Integer, flags, remap_kind=K"Nothing")
365+
function _bump_n(stream::ParseStream, n::Integer, flags, remap_kind=K"None")
366366
if n <= 0
367367
return
368368
end
@@ -375,7 +375,7 @@ function _bump_n(stream::ParseStream, n::Integer, flags, remap_kind=K"Nothing")
375375
is_trivia = k (K"Whitespace", K"Comment", K"NewlineWs")
376376
f = is_trivia ? TRIVIA_FLAG : flags
377377
is_dotted(tok) && (f |= DOTOP_FLAG)
378-
outk = (is_trivia || remap_kind == K"Nothing") ? k : remap_kind
378+
outk = (is_trivia || remap_kind == K"None") ? k : remap_kind
379379
range = TaggedRange(SyntaxHead(outk, f), k, first_byte(tok),
380380
last_byte(tok), lastindex(stream.ranges)+1)
381381
push!(stream.ranges, range)
@@ -393,7 +393,7 @@ end
393393
Shift the current token from the input to the output, adding the given flags.
394394
"""
395395
function bump(stream::ParseStream, flags=EMPTY_FLAGS; skip_newlines=false,
396-
error=nothing, remap_kind::Kind=K"Nothing")
396+
error=nothing, remap_kind::Kind=K"None")
397397
emark = position(stream)
398398
_bump_n(stream, _lookahead_index(stream, 1, skip_newlines), flags, remap_kind)
399399
if !isnothing(error)
@@ -438,7 +438,7 @@ lexing ambiguities. There's no special whitespace handling — bump any
438438
whitespace if necessary with bump_trivia.
439439
"""
440440
function bump_glue(stream::ParseStream, kind, flags, num_tokens)
441-
span = TaggedRange(SyntaxHead(kind, flags), K"Nothing",
441+
span = TaggedRange(SyntaxHead(kind, flags), K"None",
442442
first_byte(stream.lookahead[1]),
443443
last_byte(stream.lookahead[num_tokens]),
444444
lastindex(stream.ranges) + 1)
@@ -541,7 +541,7 @@ should be a previous return value of `position()`.
541541
"""
542542
function emit(stream::ParseStream, mark::ParseStreamPosition, kind::Kind,
543543
flags::RawFlags = EMPTY_FLAGS; error=nothing)
544-
range = TaggedRange(SyntaxHead(kind, flags), K"Nothing", mark.input_byte,
544+
range = TaggedRange(SyntaxHead(kind, flags), K"None", mark.input_byte,
545545
stream.next_byte-1, mark.output_index+1)
546546
if !isnothing(error)
547547
_emit_diagnostic(stream, first_byte(range), last_byte(range), error=error)

src/parser.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ function parse_resword(ps::ParseState)
17791779
if k == K"NewlineWs" || is_closing_token(ps, k)
17801780
# return\nx ==> (return nothing)
17811781
# return) ==> (return nothing)
1782-
bump_invisible(ps, K"NothingLiteral")
1782+
bump_invisible(ps, K"nothing")
17831783
else
17841784
# return x ==> (return x)
17851785
# return x,y ==> (return (tuple x y))
@@ -1889,7 +1889,7 @@ function parse_const_local_global(ps)
18891889
mark = position(ps)
18901890
scope_mark = mark
18911891
has_const = false
1892-
scope_k = K"Nothing"
1892+
scope_k = K"None"
18931893
k = peek(ps)
18941894
if k in KSet`global local`
18951895
# global x ==> (global x)
@@ -1947,7 +1947,7 @@ function parse_const_local_global(ps)
19471947
min_supported_version(v"1.8", ps, beforevar_mark,
19481948
"`const` struct field without assignment")
19491949
end
1950-
if scope_k != K"Nothing"
1950+
if scope_k != K"None"
19511951
emit(ps, scope_mark, scope_k)
19521952
end
19531953
if has_const

src/syntax_tree.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function SyntaxNode(source::SourceFile, raw::GreenNode{SyntaxHead}, position::In
5555
isempty(val_range) ?
5656
Symbol(untokenize(k)) : # synthetic invisible tokens
5757
Symbol(normalize_identifier(val_str))
58-
elseif k == K"NothingLiteral"
58+
elseif k == K"nothing"
5959
nothing
6060
elseif k == K"error"
6161
ErrorVal()

src/token_kinds.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
const _str_to_kind = let Ts = TzTokens
77
Dict([
8+
"None" => Ts.NONE
89
"EndMarker" => Ts.ENDMARKER
910
"error" => Ts.ERROR
1011
"Comment" => Ts.COMMENT
@@ -53,9 +54,8 @@ Dict([
5354
"var" => Ts.VAR
5455
"END_KEYWORDS" => Ts.end_keywords
5556

56-
# FIXME: Define precisely what Nothing means; integrate better with other tokens.
5757
"BEGIN_CSTPARSER" => Ts.begin_cstparser
58-
"Nothing" => Ts.NOTHING
58+
"nothing" => Ts.NOTHING
5959
"NewlineWs" => Ts.NEWLINE_WS
6060
"END_CSTPARSER" => Ts.end_cstparser
6161

@@ -819,7 +819,6 @@ Dict([
819819
"BEGIN_PARSER_TOKENS" => Ts.begin_parser_tokens
820820

821821
"TOMBSTONE" => Ts.TOMBSTONE
822-
"NothingLiteral" => Ts.NOTHING_LITERAL
823822

824823
# Macro names are modelled as a special kind of identifier because the
825824
# @ may not be attached to the macro name in the source (or may not be
@@ -891,7 +890,7 @@ for kw in split("""
891890
vcat ncat typed_hcat typed_vcat typed_ncat row nrow generator
892891
filter flatten comprehension typed_comprehension
893892
894-
error Nothing
893+
error nothing true false None
895894
""")
896895
_kind_to_str_unique[_str_to_kind[kw]] = kw
897896
end

test/parser.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
function test_parse(production, code; v=v"1.6")
22
stream = ParseStream(code, version=v)
33
production(JuliaSyntax.ParseState(stream))
4-
t = JuliaSyntax.build_tree(GreenNode, stream, wrap_toplevel_as_kind=K"Nothing")
4+
t = JuliaSyntax.build_tree(GreenNode, stream, wrap_toplevel_as_kind=K"None")
55
source = SourceFile(code)
66
s = SyntaxNode(source, t)
7-
if JuliaSyntax.kind(s) == K"Nothing"
7+
if JuliaSyntax.kind(s) == K"None"
88
join([sprint(show, MIME("text/x.sexpression"), c) for c in children(s)], ' ')
99
else
1010
sprint(show, MIME("text/x.sexpression"), s)

0 commit comments

Comments
 (0)