Skip to content

Commit 2deac15

Browse files
authored
Remove DOTOP_FLAG from tokenizer and parser (#568)
This implements the first of a series of AST format changes described in #567. In particular, this removes the DOTOP_FLAG. Currently, we already do not emit the DOTOP_FLAG on terminals, they always get split into one dot token and one identifier token (although we did set it on the intermediate tokens that came out of the lexer). The only four kinds for which DOTOP_FLAG was ever set in the final AST were `=`, `op=`, `&&` and `||`. This introduces separate head kinds for each of these (similar to how there are already separate head calls for `dotcall` and `dot). Otherwise the AST structure should be unchanged.
1 parent 71320ea commit 2deac15

File tree

10 files changed

+238
-273
lines changed

10 files changed

+238
-273
lines changed

LICENSE.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
The JuliaSyntax.jl package is licensed under the MIT "Expat" License:
22

33
> Copyright (c) 2021 Julia Computing and contributors
4-
>
4+
>
55
> Permission is hereby granted, free of charge, to any person obtaining a copy
66
> of this software and associated documentation files (the "Software"), to deal
77
> in the Software without restriction, including without limitation the rights
88
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99
> copies of the Software, and to permit persons to whom the Software is
1010
> furnished to do so, subject to the following conditions:
11-
>
11+
>
1212
> The above copyright notice and this permission notice shall be included in all
1313
> copies or substantial portions of the Software.
14-
>
14+
>
1515
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1717
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

src/core/parse_stream.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,6 @@ function _buffer_lookahead_tokens(lexer, lookahead)
376376
was_whitespace = is_whitespace(k)
377377
had_whitespace |= was_whitespace
378378
f = EMPTY_FLAGS
379-
raw.dotop && (f |= DOTOP_FLAG)
380379
raw.suffix && (f |= SUFFIXED_FLAG)
381380
push!(lookahead, SyntaxToken(SyntaxHead(k, f), k,
382381
had_whitespace, raw.endbyte + 2))

src/integration/expr.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,13 @@ end
313313
op = args[2]
314314
rhs = args[3]
315315
headstr = string(args[2], '=')
316-
if is_dotted(nodehead)
317-
headstr = '.'*headstr
318-
end
316+
retexpr.head = Symbol(headstr)
317+
retexpr.args = Any[lhs, rhs]
318+
elseif k == K".op=" && length(args) == 3
319+
lhs = args[1]
320+
op = args[2]
321+
rhs = args[3]
322+
headstr = '.' * string(args[2], '=')
319323
retexpr.head = Symbol(headstr)
320324
retexpr.args = Any[lhs, rhs]
321325
elseif k == K"macrocall"

src/julia/julia_parse_stream.jl

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# Token flags - may be set for operator kinded tokens
2-
# Operator is dotted
3-
const DOTOP_FLAG = RawFlags(1<<1)
42
# Operator has a suffix
53
const SUFFIXED_FLAG = RawFlags(1<<2)
64

@@ -112,12 +110,6 @@ Return true for postfix operator calls such as the `'ᵀ` call node parsed from
112110
"""
113111
is_postfix_op_call(x) = call_type_flags(x) == POSTFIX_OP_FLAG
114112

115-
"""
116-
is_dotted(x)
117-
118-
Return true for dotted syntax tokens
119-
"""
120-
is_dotted(x) = has_flags(x, DOTOP_FLAG)
121113

122114
"""
123115
is_suffixed(x)
@@ -126,12 +118,6 @@ Return true for operators which have suffixes, such as `+₁`
126118
"""
127119
is_suffixed(x) = has_flags(x, SUFFIXED_FLAG)
128120

129-
"""
130-
is_decorated(x)
131-
132-
Return true for operators which are decorated with a dot or suffix.
133-
"""
134-
is_decorated(x) = is_dotted(x) || is_suffixed(x)
135121

136122
"""
137123
numeric_flags(x)
@@ -144,11 +130,7 @@ numeric_flags(x) = numeric_flags(flags(x))
144130
function untokenize(head::SyntaxHead; unique=true, include_flag_suff=true)
145131
str = (is_error(kind(head)) ? untokenize(kind(head); unique=false) :
146132
untokenize(kind(head); unique=unique))::String
147-
if is_dotted(head)
148-
str = "."*str
149-
end
150133
if include_flag_suff
151-
# Ignore DOTOP_FLAG - it's represented above with . prefix
152134
is_trivia(head) && (str = str*"-t")
153135
is_infix_op_call(head) && (str = str*"-i")
154136
is_prefix_op_call(head) && (str = str*"-pre")
@@ -313,3 +295,32 @@ function bump_split(stream::ParseStream, split_spec::Vararg{Any, N}) where {N}
313295
stream.peek_count = 0
314296
return position(stream)
315297
end
298+
299+
function peek_dotted_op_token(ps, allow_whitespace=false)
300+
# Peek the next token, but if it is a dot, peek the next one as well
301+
t = peek_token(ps)
302+
isdotted = kind(t) == K"."
303+
if isdotted
304+
t2 = peek_token(ps, 2)
305+
if !is_operator(t2) || (!allow_whitespace && preceding_whitespace(t2))
306+
isdotted = false
307+
else
308+
t = t2
309+
end
310+
end
311+
return (isdotted, t)
312+
end
313+
314+
function bump_dotted(ps, isdot, flags=EMPTY_FLAGS; emit_dot_node=false, remap_kind=K"None")
315+
if isdot
316+
if emit_dot_node
317+
dotmark = position(ps)
318+
bump(ps, TRIVIA_FLAG) # TODO: NOTATION_FLAG
319+
else
320+
bump(ps, TRIVIA_FLAG) # TODO: NOTATION_FLAG
321+
end
322+
end
323+
pos = bump(ps, flags, remap_kind=remap_kind)
324+
isdot && emit_dot_node && (pos = emit(ps, dotmark, K"."))
325+
return pos
326+
end

src/julia/kinds.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ register_kinds!(JuliaSyntax, 0, [
293293
"BEGIN_ASSIGNMENTS"
294294
"BEGIN_SYNTACTIC_ASSIGNMENTS"
295295
"="
296+
".="
296297
"op=" # Updating assignment operator ( $= %= &= *= += -= //= /= <<= >>= >>>= \= ^= |= ÷= ⊻= )
298+
".op="
297299
":="
298300
"END_SYNTACTIC_ASSIGNMENTS"
299301
"~"
@@ -470,11 +472,13 @@ register_kinds!(JuliaSyntax, 0, [
470472
# Level 4
471473
"BEGIN_LAZYOR"
472474
"||"
475+
".||"
473476
"END_LAZYOR"
474477

475478
# Level 5
476479
"BEGIN_LAZYAND"
477480
"&&"
481+
".&&"
478482
"END_LAZYAND"
479483

480484
# Level 6

0 commit comments

Comments
 (0)