Skip to content

Commit 21c6774

Browse files
authored
Allow any of ≔ ⩴ ≕ as identifiers (#478)
These assignment-precedence operators shouldn't be special syntax and should instead be usable as normal identifiers just like `~`. Also add more test cases for the true syntactic operators. Fixes #405
1 parent 17b9285 commit 21c6774

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/kinds.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ register_kinds!(JuliaSyntax, 0, [
298298

299299
# Level 1
300300
"BEGIN_ASSIGNMENTS"
301+
"BEGIN_SYNTACTIC_ASSIGNMENTS"
301302
"="
302303
"+="
303304
"-=" # Also used for "−="
@@ -314,9 +315,10 @@ register_kinds!(JuliaSyntax, 0, [
314315
"\\="
315316
"&="
316317
":="
317-
"~"
318318
"\$="
319319
"⊻="
320+
"END_SYNTACTIC_ASSIGNMENTS"
321+
"~"
320322
""
321323
""
322324
""
@@ -1227,6 +1229,7 @@ is_prec_pipe_lt(x) = kind(x) == K"<|"
12271229
is_prec_pipe_gt(x) = kind(x) == K"|>"
12281230
is_syntax_kind(x) = K"BEGIN_SYNTAX_KINDS"<= kind(x) <= K"END_SYNTAX_KINDS"
12291231
is_macro_name(x) = K"BEGIN_MACRO_NAMES" <= kind(x) <= K"END_MACRO_NAMES"
1232+
is_syntactic_assignment(x) = K"BEGIN_SYNTACTIC_ASSIGNMENTS" <= kind(x) <= K"END_SYNTACTIC_ASSIGNMENTS"
12301233

12311234
function is_number(x)
12321235
kind(x) in (K"Integer", K"BinInt", K"HexInt", K"OctInt", K"Float", K"Float32")

src/parser.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ function is_syntactic_operator(k)
279279
# TODO: Do we need to disallow dotted and suffixed forms here?
280280
# The lexer itself usually disallows such tokens, so it's not clear whether
281281
# we need to handle them. (Though note `.->` is a token...)
282-
return k in KSet"&& || . ... ->" || (is_prec_assignment(k) && k != K"~")
282+
return k in KSet"&& || . ... ->" || is_syntactic_assignment(k)
283283
end
284284

285285
function is_syntactic_unary_op(k)
@@ -617,7 +617,8 @@ function parse_assignment_with_initial_ex(ps::ParseState, mark, down::T) where {
617617
# [a ~b] ==> (hcat a (call-pre ~ b))
618618
return
619619
end
620-
# ~ is the only non-syntactic assignment-precedence operator.
620+
# ~ is currently the only assignment-precedence operator which is parsed as a call.
621+
# TODO: Make the other non-syntactic assignments such as `≔ ⩴ ≕` into calls as well?
621622
# a ~ b ==> (call-i a ~ b)
622623
# a .~ b ==> (dotcall-i a ~ b)
623624
# [a ~ b c] ==> (hcat (call-i a ~ b) c)

test/parser.jl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,36 @@ tests = [
763763
"""var"x"1""" => "(var x (error-t))"
764764
"""var"x"y""" => "(var x (error-t))"
765765
# Standalone syntactic operators are errors
766-
"+=" => "(error +=)"
767766
"?" => "(error ?)"
767+
"&&" => "(error &&)"
768+
"||" => "(error ||)"
769+
"." => "(error .)"
770+
"..." => "(error ...)"
771+
"+=" => "(error +=)"
772+
"-=" => "(error -=)"
773+
"*=" => "(error *=)"
774+
"/=" => "(error /=)"
775+
"//=" => "(error //=)"
776+
"|=" => "(error |=)"
777+
"^=" => "(error ^=)"
778+
"÷=" => "(error ÷=)"
779+
"%=" => "(error %=)"
780+
"<<=" => "(error <<=)"
781+
">>=" => "(error >>=)"
782+
">>>="=> "(error >>>=)"
783+
"\\=" => "(error \\=)"
784+
"&=" => "(error &=)"
785+
":=" => "(error :=)"
786+
"\$=" => "(error \$=)"
787+
"⊻=" => "(error ⊻=)"
768788
".+=" => "(error (. +=))"
769789
# Normal operators
770790
"+" => "+"
791+
# Assignment-precedence operators which can be used as identifiers
771792
"~" => "~"
793+
"" => ""
794+
"" => ""
795+
"" => ""
772796
# Quoted syntactic operators allowed
773797
":+=" => "(quote-: +=)"
774798
":.=" => "(quote-: (. =))"
@@ -777,7 +801,7 @@ tests = [
777801
":end" => "(quote-: end)"
778802
":(end)" => "(quote-: (parens (error-t)))"
779803
":<:" => "(quote-: <:)"
780-
# unexpect =
804+
# unexpected =
781805
"=" => "(error =)"
782806
# parse_cat
783807
"[]" => "(vect)"

0 commit comments

Comments
 (0)