-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathparser.jl
More file actions
3397 lines (3261 loc) · 121 KB
/
parser.jl
File metadata and controls
3397 lines (3261 loc) · 121 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
ParseState(stream::ParseStream)
ParseState is an internal data structure wrapping `ParseStream` to carry parser
context as we recursively descend into the parse tree. For example, normally
`x -y` means `(x) - (y)`, but when parsing matrix literals we're in
`space_sensitive` mode, and `[x -y]` means [(x) (-y)].
"""
struct ParseState
stream::ParseStream
# Disable range colon for parsing ternary conditional operator
range_colon_enabled::Bool
# In space-sensitive mode "x -y" is 2 expressions, not a subtraction
space_sensitive::Bool
# Seeing `for` stops parsing macro arguments and makes a generator
for_generator::Bool
# Treat 'end' like a normal symbol instead of a reserved word
end_symbol::Bool
# Treat newline like ordinary whitespace instead of as a potential separator
whitespace_newline::Bool
# Enable parsing `where` with high precedence
where_enabled::Bool
end
# Normal context
function ParseState(stream::ParseStream)
ParseState(stream, true, false, false, false, false, true)
end
function ParseState(ps::ParseState; range_colon_enabled=nothing,
space_sensitive=nothing, for_generator=nothing,
end_symbol=nothing, whitespace_newline=nothing,
where_enabled=nothing)
ParseState(ps.stream,
range_colon_enabled === nothing ? ps.range_colon_enabled : range_colon_enabled,
space_sensitive === nothing ? ps.space_sensitive : space_sensitive,
for_generator === nothing ? ps.for_generator : for_generator,
end_symbol === nothing ? ps.end_symbol : end_symbol,
whitespace_newline === nothing ? ps.whitespace_newline : whitespace_newline,
where_enabled === nothing ? ps.where_enabled : where_enabled)
end
# Functions to change parse state
function normal_context(ps::ParseState)
ParseState(ps,
range_colon_enabled=true,
space_sensitive=false,
where_enabled=true,
for_generator=false,
end_symbol=false,
whitespace_newline=false)
end
function with_space_sensitive(ps::ParseState)
ParseState(ps,
space_sensitive=true,
whitespace_newline=false)
end
# Convenient wrappers for ParseStream
function Base.peek(ps::ParseState, n=1; skip_newlines=nothing)
skip_nl = isnothing(skip_newlines) ? ps.whitespace_newline : skip_newlines
peek(ps.stream, n; skip_newlines=skip_nl)
end
function peek_token(ps::ParseState, n=1; skip_newlines=nothing)
skip_nl = isnothing(skip_newlines) ? ps.whitespace_newline : skip_newlines
peek_token(ps.stream, n, skip_newlines=skip_nl)
end
function peek_full_token(ps::ParseState, n=1; skip_newlines=nothing, kws...)
skip_nl = isnothing(skip_newlines) ? ps.whitespace_newline : skip_newlines
peek_full_token(ps.stream, n; skip_newlines=skip_nl, kws...)
end
function peek_behind(ps::ParseState, args...; kws...)
peek_behind(ps.stream, args...; kws...)
end
function bump(ps::ParseState, flags=EMPTY_FLAGS; skip_newlines=nothing, kws...)
skip_nl = isnothing(skip_newlines) ? ps.whitespace_newline : skip_newlines
bump(ps.stream, flags; skip_newlines=skip_nl, kws...)
end
function bump_trivia(ps::ParseState, args...; kws...)
bump_trivia(ps.stream, args...; kws...)
end
function bump_invisible(ps::ParseState, args...; kws...)
bump_invisible(ps.stream, args...; kws...)
end
function bump_glue(ps::ParseState, args...; kws...)
bump_glue(ps.stream, args...; kws...)
end
function bump_split(ps::ParseState, args...; kws...)
bump_split(ps.stream, args...; kws...)
end
function reset_node!(ps::ParseState, args...; kws...)
reset_node!(ps.stream, args...; kws...)
end
function steal_token_bytes!(ps::ParseState, args...)
steal_token_bytes!(ps.stream, args...)
end
function Base.position(ps::ParseState, args...)
position(ps.stream, args...)
end
function emit(ps::ParseState, args...; kws...)
emit(ps.stream, args...; kws...)
end
function emit_diagnostic(ps::ParseState, args...; kws...)
emit_diagnostic(ps.stream, args...; kws...)
end
function textbuf(ps::ParseState)
textbuf(ps.stream)
end
#-------------------------------------------------------------------------------
# Parser Utils
# Bump an expected closing token. If not found, discard unexpected tokens
# until we find it or another closing token.
#
# Crude recovery heuristic: bump any tokens which aren't block or bracket
# closing tokens.
function bump_closing_token(ps, closing_kind)
# todo: Refactor with recover() ?
bump_trivia(ps)
if peek(ps) == closing_kind
bump(ps, TRIVIA_FLAG)
return
end
# We didn't find the closing token. Read ahead in the stream
mark = position(ps)
while true
k = peek(ps)
if is_closing_token(ps, k) && !(k in KSet", ;")
break
end
bump(ps)
end
# mark as trivia => ignore in AST.
emit(ps, mark, K"error", TRIVIA_FLAG,
error="Expected `$(untokenize(closing_kind))`")
if peek(ps) == closing_kind
bump(ps, TRIVIA_FLAG)
end
end
# Read tokens until we find an expected closing token.
# Bump the big pile of resulting tokens as a single nontrivia error token
function recover(is_closer::Function, ps, flags=EMPTY_FLAGS; mark = position(ps), error="unexpected tokens")
while true
k = peek(ps)
if k == K"EndMarker"
bump_invisible(ps, K"error", TRIVIA_FLAG,
error="premature end of input")
break
elseif is_closer(ps, k)
break
end
bump(ps)
end
emit(ps, mark, K"error", flags, error=error)
end
@noinline function min_supported_version_err(ps, mark, message, min_ver)
major, minor = ps.stream.version
msg = "$message not supported in Julia version $major.$minor < $(min_ver.major).$(min_ver.minor)"
emit(ps, mark, K"error", error=msg)
end
# Emit an error if the version is less than `min_ver`
function min_supported_version(min_ver, ps, mark, message)
if ps.stream.version < (min_ver.major, min_ver.minor)
min_supported_version_err(ps, mark, message, min_ver)
end
end
# flisp: disallow-space
function bump_disallowed_space(ps)
if preceding_whitespace(peek_token(ps))
bump_trivia(ps, TRIVIA_FLAG, skip_newlines=false,
error="whitespace is not allowed here")
end
end
function bump_semicolon_trivia(ps)
while peek(ps) in KSet"; NewlineWs"
bump(ps, TRIVIA_FLAG)
end
end
# Like @assert, but always enabled and calls internal_error()
macro check(ex, msgs...)
msg = isempty(msgs) ? ex : msgs[1]
if isa(msg, AbstractString)
msg = msg
elseif !isempty(msgs) && (isa(msg, Expr) || isa(msg, Symbol))
msg = :(string($(esc(msg))))
else
msg = string(msg)
end
return :($(esc(ex)) ? nothing : internal_error($msg))
end
# Parser internal error, used as an assertion failure for cases we expect can't
# happen.
@noinline function internal_error(strs...)
error("Internal error: ", strs...)
end
#-------------------------------------------------------------------------------
# Parsing-specific predicates on tokens/kinds
#
# All these take either a raw kind or a token.
function is_plain_equals(t)
kind(t) == K"=" && !is_decorated(t)
end
function is_closing_token(ps::ParseState, k)
k = kind(k)
return k in KSet"else elseif catch finally , ) ] } ; EndMarker" ||
(k == K"end" && !ps.end_symbol)
end
function is_closer_or_newline(ps::ParseState, k)
is_closing_token(ps,k) || k == K"NewlineWs"
end
function is_initial_reserved_word(ps::ParseState, k)
k = kind(k)
is_iresword = k in KSet"begin while if for try return break continue function
macro quote let local global const do struct module
baremodule using import export"
# `begin` means firstindex(a) inside a[...]
return is_iresword && !(k == K"begin" && ps.end_symbol)
end
function is_reserved_word(k)
k = kind(k)
is_keyword(k) && !is_contextual_keyword(k)
end
# Return true if the next word (or word pair) is reserved, introducing a
# syntactic structure.
function peek_initial_reserved_words(ps::ParseState)
k = peek(ps)
if is_initial_reserved_word(ps, k)
return true
elseif is_contextual_keyword(k)
k2 = peek(ps,2)
return (k == K"mutable" && k2 == K"struct") ||
(k == K"primitive" && k2 == K"type") ||
(k == K"abstract" && k2 == K"type")
else
return false
end
end
function is_block_form(k)
kind(k) in KSet"block quote if for while let function macro
abstract primitive struct try module"
end
function is_syntactic_operator(k)
k = kind(k)
# TODO: Do we need to disallow dotted and suffixed forms here?
# The lexer itself usually disallows such tokens, so it's not clear whether
# we need to handle them. (Though note `.->` is a token...)
return k in KSet"&& || . ... ->" || (is_prec_assignment(k) && k != K"~")
end
function is_syntactic_unary_op(k)
kind(k) in KSet"$ & ::"
end
function is_type_operator(t)
kind(t) in KSet"<: >:" && !is_dotted(t)
end
function is_unary_op(t)
k = kind(t)
!is_suffixed(t) && (
(k in KSet"<: >:" && !is_dotted(t)) ||
k in KSet"+ - ! ~ ¬ √ ∛ ∜ ⋆ ± ∓" # dotop allowed
)
end
# Operators that are both unary and binary
function is_both_unary_and_binary(t)
k = kind(t)
# Preventing is_suffixed here makes this consistent with the flisp parser.
# But is this by design or happenstance?
!is_suffixed(t) && (
k in KSet"+ - ⋆ ± ∓" || (k in KSet"$ & ~" && !is_dotted(t))
)
end
# operators handled by parse_unary at the start of an expression
function is_initial_operator(t)
k = kind(t)
# TODO(jb): `?` should probably not be listed here except for the syntax hack in osutils.jl
is_operator(k) &&
!is_word_operator(k) &&
!(k in KSet": ' .' ?") &&
!(is_syntactic_unary_op(k) && !is_dotted(t)) &&
!is_syntactic_operator(k)
end
# flisp: invalid-identifier?
function is_valid_identifier(k)
k = kind(k)
!(is_syntactic_operator(k) || k in KSet"? .'")
end
#-------------------------------------------------------------------------------
# Parser
#
# The definitions and top-level comments here were copied to match the
# structure of Julia's official flisp-based parser.
#
# This is to make both codebases mutually understandable and make porting
# changes simple.
#
# The `parse_*` functions are listed here roughly in order of increasing
# precedence (lowest to highest binding power). A few helper functions are
# interspersed.
# parse left-to-right binary operator
# produces structures like (+ (+ (+ 2 3) 4) 5)
#
# flisp: parse-LtoR
function parse_LtoR(ps::ParseState, down, is_op)
mark = position(ps)
down(ps)
while is_op(peek(ps))
bump(ps)
down(ps)
emit(ps, mark, K"call", INFIX_FLAG)
end
end
# parse right-to-left binary operator
# produces structures like (= a (= b (= c d)))
#
# flisp: parse-RtoL
function parse_RtoL(ps::ParseState, down, is_op, self)
mark = position(ps)
down(ps)
k = peek(ps)
if is_op(k)
bump(ps)
self(ps)
emit(ps, mark, K"call", INFIX_FLAG)
end
end
# parse block-like structures
#
# `delimiters` are a set of token kinds acting as delimiters; `closing_tokens`
# stop the parsing.
#
# Returns true if the block was nontrivial and a node needs to be emitted by
# the caller.
#
# flisp: parse-Nary
function parse_Nary(ps::ParseState, down, delimiters, closing_tokens)
bump_trivia(ps)
k = peek(ps)
if k in closing_tokens
return true
end
n_delims = 0
if k in delimiters
# allow leading delimiters
# ; a ==> (block a)
else
# a ; b ==> (block a b)
down(ps)
end
while peek(ps) in delimiters
bump(ps, TRIVIA_FLAG)
n_delims += 1
k = peek(ps)
if k == K"EndMarker" || k in closing_tokens
break
elseif k in delimiters
# ignore empty delimited sections
# a;;;b ==> (block a b)
continue
end
down(ps)
end
return n_delims != 0
end
# Parse a sequence of top level statements separated by newlines, all wrapped
# in a toplevel node.
#
# a \n b ==> (toplevel a b)
#
# Note that parse_stmts can also emit toplevel nodes for semicolon-separated
# statements, so it's possible for these to be nested one level deep.
#
# a;b \n c;d ==> (toplevel (toplevel a b) (toplevel c d))
function parse_toplevel(ps::ParseState)
mark = position(ps)
while true
if peek(ps, skip_newlines=true) == K"EndMarker"
# Allow end of input if there is nothing left but whitespace
# a \n \n ==> (toplevel a)
# Empty files
# ==> (toplevel)
bump_trivia(ps, skip_newlines=true)
break
else
parse_stmts(ps)
end
end
emit(ps, mark, K"toplevel")
nothing
end
# Parse a newline or semicolon-delimited list of expressions.
# Repeated delimiters are allowed but ignored
# a;b;c ==> (block a b c)
# a;;;b;; ==> (block a b)
# ;a ==> (block a)
# \n a ==> (block a)
# a \n b ==> (block a b)
#
# flisp: parse-block
function parse_block(ps::ParseState, down=parse_eq, mark=position(ps),
consume_end=false)
parse_block_inner(ps::ParseState, down)
emit(ps, mark, K"block")
end
# Parse a block, but leave emitting the block up to the caller.
function parse_block_inner(ps::ParseState, down)
parse_Nary(ps, down, KSet"NewlineWs ;", KSet"end else elseif catch finally")
end
# ";" at the top level produces a sequence of top level expressions
#
# a;b;c ==> (toplevel a b c)
# a;;;b;; ==> (toplevel a b)
# "x" a ; "y" b ==> (toplevel (macrocall core_@doc "x" a) (macrocall core_@doc "y" b))
#
# flisp: parse-stmts
function parse_stmts(ps::ParseState)
mark = position(ps)
do_emit = parse_Nary(ps, parse_docstring, (K";",), (K"NewlineWs",))
# check for unparsed junk after an expression
junk_mark = position(ps)
while peek(ps) ∉ KSet"EndMarker NewlineWs"
# Error recovery
bump(ps)
end
if junk_mark != position(ps)
# x y ==> x (error-t y)
emit(ps, junk_mark, K"error", TRIVIA_FLAG,
error="extra tokens after end of expression")
end
if do_emit
emit(ps, mark, K"toplevel")
end
end
# Parse docstrings attached by a space or single newline
# "doc" foo ==> (macrocall core_@doc "doc" foo)
#
# flisp: parse-docstring
function parse_docstring(ps::ParseState, down=parse_eq)
mark = position(ps)
atdoc_mark = bump_invisible(ps, K"TOMBSTONE")
down(ps)
if peek_behind(ps).kind in KSet"String string"
is_doc = true
k = peek(ps)
if is_closing_token(ps, k)
# "notdoc" ] ==> "notdoc"
is_doc = false
elseif k == K"NewlineWs"
k2 = peek(ps, 2)
if is_closing_token(ps, k2) || k2 == K"NewlineWs"
# "notdoc" \n] ==> "notdoc"
# "notdoc" \n\n foo ==> "notdoc"
is_doc = false
else
# Allow a single newline
# "doc" \n foo ==> (macrocall core_@doc "doc" foo)
bump(ps, TRIVIA_FLAG) # NewlineWs
end
else
# "doc" foo ==> (macrocall core_@doc "doc" foo)
# "doc $x" foo ==> (macrocall core_@doc (string "doc " x) foo)
# Allow docstrings with embedded trailing whitespace trivia
# """\n doc\n """ foo ==> (macrocall core_@doc "doc\n" foo)
end
if is_doc
reset_node!(ps, atdoc_mark, kind=K"core_@doc")
down(ps)
emit(ps, mark, K"macrocall")
end
end
end
# Parse assignments with comma separated lists on each side
# a = b ==> (= a b)
# a .= b ==> (.= a b)
# a += b ==> (+= a b)
# a .+= b ==> (.+= a b)
# a, b = c, d ==> (= (tuple a b) (tuple c d))
# x, = xs ==> (= (tuple x) xs)
#
# flisp: parse-eq
function parse_eq(ps::ParseState)
parse_assignment(ps, parse_comma, false)
end
# parse_eq_star is used where commas are special, for example in an argument list
#
# If an `(= x y)` node was emitted, returns the position of that node in the
# output list so that it can be changed to `(kw x y)` later if necessary.
#
# flisp: parse-eq*
function parse_eq_star(ps::ParseState, equals_is_kw=false)
k = peek(ps)
k2 = peek(ps,2)
if (is_literal(k) || k == K"Identifier") && k2 in KSet", ) } ]"
# optimization: skip checking the whole precedence stack if we have a
# simple token followed by a common closing token
bump(ps)
return NO_POSITION
else
return parse_assignment(ps, parse_pair, equals_is_kw)
end
end
# a = b ==> (= a b)
#
# flisp: parse-assignment
function parse_assignment(ps::ParseState, down, equals_is_kw::Bool)
mark = position(ps)
down(ps)
parse_assignment_with_initial_ex(ps, mark, down, equals_is_kw)
end
function parse_assignment_with_initial_ex(ps::ParseState, mark, down, equals_is_kw::Bool)
t = peek_token(ps)
k = kind(t)
if !is_prec_assignment(k)
return NO_POSITION
end
if k == K"~"
if ps.space_sensitive && !preceding_whitespace(peek_token(ps, 2))
# Unary ~ in space sensitive context is not assignment precedence
# [a ~b] ==> (hcat a (call ~ b))
return NO_POSITION
end
# ~ is the only non-syntactic assignment-precedence operator.
# a ~ b ==> (call-i a ~ b)
# [a ~ b c] ==> (hcat (call-i a ~ b) c)
bump(ps)
parse_assignment(ps, down, equals_is_kw)
emit(ps, mark, K"call", INFIX_FLAG)
return NO_POSITION
else
# a += b ==> (+= a b)
# a .= b ==> (.= a b)
bump(ps, TRIVIA_FLAG)
parse_assignment(ps, down, equals_is_kw)
plain_eq = is_plain_equals(t)
equals_pos = emit(ps, mark, plain_eq && equals_is_kw ? K"kw" : k, flags(t))
return plain_eq ? equals_pos : NO_POSITION
end
end
# parse_comma is needed for commas outside parens, for example a = b,c
#
# flisp: parse-comma
function parse_comma(ps::ParseState, do_emit=true)
mark = position(ps)
n_commas = 0
parse_pair(ps)
while true
if peek(ps) != K","
if do_emit && n_commas >= 1
emit(ps, mark, K"tuple")
end
return n_commas
end
bump(ps, TRIVIA_FLAG)
n_commas += 1
if is_plain_equals(peek_token(ps))
# Allow trailing comma before `=`
# x, = xs ==> (tuple x)
continue
end
parse_pair(ps)
end
end
# flisp: parse-pair
# a => b ==> (call-i a => b)
function parse_pair(ps::ParseState)
parse_RtoL(ps, parse_cond, is_prec_pair, parse_pair)
end
# Parse short form conditional expression
# a ? b : c ==> (if a b c)
#
# flisp: parse-cond
function parse_cond(ps::ParseState)
mark = position(ps)
parse_arrow(ps)
t = peek_token(ps)
if kind(t) != K"?"
return
end
if !preceding_whitespace(t)
# a? b : c => (if a (error-t) b c)
bump_invisible(ps, K"error", TRIVIA_FLAG,
error="space required before `?` operator")
end
bump(ps, TRIVIA_FLAG) # ?
t = peek_token(ps)
if !preceding_whitespace(t)
# a ?b : c
bump_invisible(ps, K"error", TRIVIA_FLAG,
error="space required after `?` operator")
end
parse_eq_star(ParseState(ps, range_colon_enabled=false))
t = peek_token(ps)
if !preceding_whitespace(t)
# a ? b: c ==> (if a [ ] [?] [ ] b (error-t) [:] [ ] c)
bump_invisible(ps, K"error", TRIVIA_FLAG,
error="space required before `:` in `?` expression")
end
if kind(t) == K":"
bump(ps, TRIVIA_FLAG)
else
# a ? b c ==> (if a b (error) c)
bump_invisible(ps, K"error", TRIVIA_FLAG, error="`:` expected in `?` expression")
end
t = peek_token(ps)
if !preceding_whitespace(t)
# a ? b :c ==> (if a [ ] [?] [ ] b [ ] [:] (error-t) c)
bump_invisible(ps, K"error", TRIVIA_FLAG,
error="space required after `:` in `?` expression")
end
parse_eq_star(ps)
emit(ps, mark, K"if")
end
# Parse arrows. Like parse_RtoL, but specialized for --> syntactic operator
#
# flisp: parse-arrow
function parse_arrow(ps::ParseState)
mark = position(ps)
parse_or(ps)
t = peek_token(ps)
k = kind(t)
if is_prec_arrow(k)
if kind(t) == K"-->" && !is_decorated(t)
# x --> y ==> (--> x y) # The only syntactic arrow
bump(ps, TRIVIA_FLAG)
parse_arrow(ps)
emit(ps, mark, k, flags(t))
else
# x → y ==> (call-i x → y)
# x <--> y ==> (call-i x <--> y)
# x .--> y ==> (call-i x .--> y)
# x -->₁ y ==> (call-i x -->₁ y)
bump(ps)
parse_arrow(ps)
emit(ps, mark, K"call", INFIX_FLAG)
end
end
end
# Like parse_RtoL, but specialized for the version test of dotted operators.
function parse_lazy_cond(ps::ParseState, down, is_op, self)
mark = position(ps)
down(ps)
t = peek_token(ps)
k = kind(t)
if is_op(k)
bump(ps, TRIVIA_FLAG)
self(ps)
emit(ps, mark, k, flags(t))
if is_dotted(t)
min_supported_version(v"1.7", ps, mark, "dotted operators `.||` and `.&&`")
end
end
end
# x || y || z ==> (|| x (|| y z))
#v1.6: x .|| y ==> (error (.|| x y))
#v1.7: x .|| y ==> (.|| x y)
#
# flisp: parse-or
function parse_or(ps::ParseState)
parse_lazy_cond(ps, parse_and, is_prec_lazy_or, parse_or)
end
# x && y && z ==> (&& x (&& y z))
#v1.6: x .&& y ==> (error (.&& x y))
#v1.7: x .&& y ==> (.&& x y)
#
# flisp: parse-and
function parse_and(ps::ParseState)
parse_lazy_cond(ps, parse_comparison, is_prec_lazy_and, parse_and)
end
# Parse binary comparisons and comparison chains
#
# flisp: parse-comparison
function parse_comparison(ps::ParseState, subtype_comparison=false)
mark = position(ps)
if subtype_comparison && is_reserved_word(peek(ps))
# Recovery
# struct try end ==> (struct false (error (try)) (block))
name = untokenize(peek(ps))
bump(ps)
emit(ps, mark, K"error", error="Invalid type name `$name`")
else
parse_pipe_lt(ps)
end
n_comparisons = 0
op_pos = NO_POSITION
initial_tok = peek_token(ps)
while is_prec_comparison(peek(ps))
n_comparisons += 1
op_pos = bump(ps)
parse_pipe_lt(ps)
end
if n_comparisons == 1
if is_type_operator(initial_tok)
# Type comparisons are syntactic
# x <: y ==> (<: x y)
# x >: y ==> (>: x y)
reset_node!(ps, op_pos, flags=TRIVIA_FLAG)
emit(ps, mark, kind(initial_tok))
else
# Normal binary comparisons
# x < y ==> (call-i x < y)
# x .<: y ==> (call-i x .<: y)
emit(ps, mark, K"call", INFIX_FLAG)
end
elseif n_comparisons > 1
# Comparison chains
# x < y < z ==> (comparison x < y < z)
# x == y < z ==> (comparison x == y < z)
emit(ps, mark, K"comparison")
end
end
# x <| y <| z ==> (call-i x <| (call-i y <| z))
# flisp: parse-pipe<
function parse_pipe_lt(ps::ParseState)
parse_RtoL(ps, parse_pipe_gt, is_prec_pipe_lt, parse_pipe_lt)
end
# x |> y |> z ==> (call-i (call-i x |> y) |> z)
# flisp: parse-pipe>
function parse_pipe_gt(ps::ParseState)
parse_LtoR(ps, parse_range, is_prec_pipe_gt)
end
# parse ranges and postfix ...
# colon is strange; 3 arguments with 2 colons yields one call:
# 1:2 ==> (call-i 1 : 2)
# 1:2:3 ==> (call-i 1 : 2 3)
# Chaining gives
# a:b:c:d:e ==> (call-i (call-i a : b c) : d e)
#
# flisp: parse-range
function parse_range(ps::ParseState)
mark = position(ps)
parse_expr(ps)
initial_kind = peek(ps)
if initial_kind != K":" && is_prec_colon(initial_kind)
# a..b ==> (call-i a .. b)
# a … b ==> (call-i a … b)
bump(ps)
parse_expr(ps)
emit(ps, mark, K"call", INFIX_FLAG)
elseif initial_kind == K":" && ps.range_colon_enabled
# a ? b : c:d ==> (if a b (call-i c : d))
n_colons = 0
while peek(ps) == K":"
if ps.space_sensitive &&
preceding_whitespace(peek_token(ps)) &&
!preceding_whitespace(peek_token(ps, 2))
# Tricky cases in space sensitive mode
# [1 :a] ==> (hcat 1 (quote a))
# [1 2:3 :a] ==> (hcat 1 (call-i 2 : 3) (quote a))
break
end
t2 = peek_token(ps,2)
if kind(t2) in KSet"< >" && !preceding_whitespace(t2)
# Error heuristic: we found `:>` or `:<` which are invalid lookalikes
# for `<:` and `>:`. Attempt to recover by treating them as a
# comparison operator.
# a :> b ==> (call-i a (error : >) b)
bump_trivia(ps, skip_newlines=false)
emark = position(ps)
bump(ps) # K":"
ks = untokenize(peek(ps))
bump(ps) # K"<" or K">"
emit(ps, emark, K"error",
error="Invalid `:$ks` found, maybe replace with `$ks:`")
parse_expr(ps)
emit(ps, mark, K"call", INFIX_FLAG)
break
end
n_colons += 1
bump(ps, n_colons == 1 ? EMPTY_FLAGS : TRIVIA_FLAG)
had_newline = peek(ps, skip_newlines=false) == K"NewlineWs"
t = peek_token(ps)
if is_closing_token(ps, kind(t))
# 1: } ==> (call-i 1 : (error))
# 1:2: } ==> (call-i 1 : 2 (error))
bump_invisible(ps, K"error",
error="missing last argument in range expression")
emit(ps, mark, K"call", INFIX_FLAG)
emit_diagnostic(ps, error="found unexpected closing token")
return
end
if had_newline
# Error message for people coming from python
# 1:\n2 ==> (call-i 1 : (error))
emit_diagnostic(ps, whitespace=true,
error="line break after `:` in range expression")
bump_invisible(ps, K"error")
emit(ps, mark, K"call", INFIX_FLAG)
return
end
parse_expr(ps)
if n_colons == 2
emit(ps, mark, K"call", INFIX_FLAG)
n_colons = 0
end
end
if n_colons > 0
emit(ps, mark, K"call", INFIX_FLAG)
end
end
# x... ==> (... x)
# x:y... ==> (... (call-i x : y))
# x..y... ==> (... (call-i x .. y)) # flisp parser fails here
if peek(ps) == K"..."
bump(ps, TRIVIA_FLAG)
emit(ps, mark, K"...")
end
end
# a - b - c ==> (call-i (call-i a - b) - c)
# a + b + c ==> (call-i a + b c)
#
# flisp: parse-expr
function parse_expr(ps::ParseState)
parse_with_chains(ps, parse_term, is_prec_plus, KSet"+ ++")
end
# a * b * c ==> (call-i a * b c)
#
# flisp: parse-term
function parse_term(ps::ParseState)
parse_with_chains(ps, parse_rational, is_prec_times, KSet"*")
end
# Parse left to right, combining any of `chain_ops` into one call
#
# flisp: parse-with-chains
function parse_with_chains(ps::ParseState, down, is_op, chain_ops)
mark = position(ps)
down(ps)
while (t = peek_token(ps); is_op(kind(t)))
if ps.space_sensitive && preceding_whitespace(t) &&
is_both_unary_and_binary(t) &&
!preceding_whitespace(peek_token(ps, 2))
# The following is two elements of a hcat
# [x +y] ==> (hcat x (call + y))
# [x+y +z] ==> (hcat (call-i x + y) (call + z))
# Conversely the following are infix calls
# [x +₁y] ==> (vect (call-i x +₁ y))
# [x+y+z] ==> (vect (call-i x + y z))
# [x+y + z] ==> (vect (call-i x + y z))
break
end
bump(ps)
down(ps)
if kind(t) in chain_ops && !is_decorated(t)
# a + b + c ==> (call-i a + b c)
# a + b .+ c ==> (call-i (call-i a + b) .+ c)
parse_chain(ps, down, kind(t))
end
# a +₁ b +₁ c ==> (call-i (call-i a +₁ b) +₁ c)
# a .+ b .+ c ==> (call-i (call-i a .+ b) .+ c)
emit(ps, mark, K"call", INFIX_FLAG)
end
end
# parse left to right chains of a given binary operator
#
# flisp: parse-chain
function parse_chain(ps::ParseState, down, op_kind)
while (t = peek_token(ps); kind(t) == op_kind && !is_decorated(t))
if ps.space_sensitive && preceding_whitespace(t) &&
is_both_unary_and_binary(t) &&
!preceding_whitespace(peek_token(ps, 2))
# [x +y] ==> (hcat x (call + y))
break
end
bump(ps, TRIVIA_FLAG)
down(ps)
end
end
# flisp: parse-rational
function parse_rational(ps::ParseState)
parse_LtoR(ps, parse_shift, is_prec_rational)
end
# flisp: parse-shift
function parse_shift(ps::ParseState)
parse_LtoR(ps, parse_unary_subtype, is_prec_bitshift)
end
# parse `<: A where B` as `<: (A where B)` (issue #21545)
#
# flisp: parse-unary-subtype
function parse_unary_subtype(ps::ParseState)
k = peek(ps, skip_newlines=true)
if k in KSet"<: >:"
k2 = peek(ps, 2)
if is_closing_token(ps, k2) || k2 in KSet"NewlineWs ="
# return operator by itself
# <: ) ==> <:
# <: \n ==> <:
# <: = ==> <:
bump(ps)
elseif k2 in KSet"{ ("
# parse <:{T}(x::T) or <:(x::T) like other unary operators
# <:{T}(x::T) ==> (call (curly <: T) (:: x T))
# <:(x::T) ==> (<: (:: x T))
parse_where(ps, parse_juxtapose)
else
# <: A where B ==> (<: (where A B))
mark = position(ps)
bump(ps, TRIVIA_FLAG)
parse_where(ps, parse_juxtapose)
# Flisp parser handled this, but I don't know how it can happen...
@check peek_behind(ps).kind != K"tuple"
emit(ps, mark, k)
end
else
parse_where(ps, parse_juxtapose)
end
end
# flisp: parse-where-chain
function parse_where_chain(ps0::ParseState, mark)
ps = ParseState(ps0, where_enabled=false)
while peek(ps) == K"where"
bump(ps, TRIVIA_FLAG) # where
bump_trivia(ps, skip_newlines=true)
k = peek(ps)
if k == K"{"
m = position(ps)
bump(ps, TRIVIA_FLAG)
# x where \n {T} ==> (where x T)
# x where {T,S} ==> (where x T S)
ckind, cflags = parse_cat(ps, K"}", ps.end_symbol)
if ckind != K"vect"
# Various nonsensical forms permitted here
# x where {T S} ==> (where x (bracescat (row T S)))
# x where {y for y in ys} ==> (where x (braces (generator y (= y ys))))
emit_braces(ps, m, ckind, cflags)
end
emit(ps, mark, K"where")
else
# x where T ==> (where x T)
# x where \n T ==> (where x T)