This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathparser.jl
2156 lines (1997 loc) · 74.7 KB
/
parser.jl
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
# Julia Source Parser
module Parser
using Compat
using ..Lexer
using ..Lexer: here
using ..Tokens
using ..Tokens: √
using ..Diagnostics: diag, before, after, Incomplete, Diagnostic
using Base.Meta
using AbstractTrees: children
export parse
typealias CharSymbol @compat(Union{Char, Symbol})
type ParseState
# disable range colon for parsing ternary cond op
range_colon_enabled::Bool
# in space sensitvie mode "x -y" is 2 exprs, not subtraction
space_sensitive::Bool
# treat "end" like a normal symbol, instead of a reserved word
inside_vector::Bool
# treat newline like ordinary whitespace instead of a reserved word
end_symbol::Bool
# treat newline like ordinary whitespace instead of as a potential separator
whitespace_newline::Bool
end
ParseState() = ParseState(true, false, false, false, false)
peek_token(ps::ParseState, ts::TokenStream) = Lexer.peek_token(ts, ps.whitespace_newline)
next_token(ps::ParseState, ts::TokenStream) = Lexer.next_token(ts, ps.whitespace_newline)
require_token(ps::ParseState, ts::TokenStream) = Lexer.require_token(ts, ps.whitespace_newline)
macro with_normal_ops(ps, body)
quote
local tmp1 = $(esc(ps)).range_colon_enabled
local tmp2 = $(esc(ps)).space_sensitive
try
$(esc(ps)).range_colon_enabled = true
$(esc(ps)).space_sensitive = false
$(esc(body))
finally
$(esc(ps)).range_colon_enabled = tmp1
$(esc(ps)).space_sensitive = tmp2
end
end
end
macro without_range_colon(ps, body)
quote
local tmp1 = $(esc(ps)).range_colon_enabled
try
$(esc(ps)).range_colon_enabled = false
$(esc(body))
finally
$(esc(ps)).range_colon_enabled = tmp1
end
end
end
macro with_inside_vec(ps, body)
quote
local tmp1 = $(esc(ps)).space_sensitive
local tmp2 = $(esc(ps)).inside_vector
local tmp3 = $(esc(ps)).whitespace_newline
try
$(esc(ps)).space_sensitive = true
$(esc(ps)).inside_vector = true
$(esc(ps)).whitespace_newline = false
$(esc(body))
finally
$(esc(ps)).space_sensitive = tmp1
$(esc(ps)).inside_vector = tmp2
$(esc(ps)).whitespace_newline = tmp3
end
end
end
macro with_end_symbol(ps, body)
quote
local tmp1 = $(esc(ps)).end_symbol
try
$(esc(ps)).end_symbol = true
$(esc(body))
finally
$(esc(ps)).end_symbol = tmp1
end
end
end
macro with_whitespace_newline(ps, body)
quote
local tmp1 = $(esc(ps)).whitespace_newline
try
$(esc(ps)).whitespace_newline = true
$(esc(body))
finally
$(esc(ps)).whitespace_newline = tmp1
end
end
end
macro without_whitespace_newline(ps, body)
quote
local tmp1 = $(esc(ps)).whitespace_newline
try
$(esc(ps)).whitespace_newline = false
$(esc(body))
finally
$(esc(ps)).whitespace_newline = tmp1
end
end
end
macro space_sensitive(ps, body)
quote
local tmp1 = $(esc(ps)).space_sensitive
local tmp2 = $(esc(ps)).whitespace_newline
try
$(esc(ps)).space_sensitive = true
$(esc(ps)).whitespace_newline = false
$(esc(body))
finally
$(esc(ps)).space_sensitive = tmp1
$(esc(ps)).whitespace_newline = tmp2
end
end
end
curline(ts::TokenStream) = ts.lineno
filename(ts::TokenStream) = Symbol(ts.filename)
line_number_node(ts) = LineNumberNode(curline(ts))
line_number_filename_node(lno, filename) = Expr(:line, lno, filename)
line_number_filename_node(ts::TokenStream) =
line_number_filename_node(curline(ts), filename(ts)) ⤄ Lexer.nullrange(ts)
# insert line/file for short form function defs, otherwise leave alone
function short_form_function_loc(ts, ex, lno, filename)
if isa(¬ex, Expr) && (¬ex).head === :(=) && isa((¬ex).args[1], Expr) && (¬ex).args[1].head === :call
block = ⨳(:block, line_number_filename_node(lno, filename)) ⤄ Lexer.nullrange(ts)
args = collect(children(ex))
block ⪥ args[2:end]
return ⨳(:(=), args[1], block)
end
return ex
end
const SYM_DO = Symbol("do")
const SYM_ELSE = Symbol("else")
const SYM_ELSEIF = Symbol("elseif")
const SYM_END = Symbol("end")
const SYM_CATCH = Symbol("catch")
const SYM_FINALLY = Symbol("finally")
const SYM_SQUOTE = Symbol("'")
const is_invalid_initial_token = let invalid = Set([')', ']', '}', SYM_ELSE, SYM_ELSEIF, SYM_CATCH, SYM_FINALLY])
is_invalid_initial_token(t) = isa(t, CharSymbol) && t in invalid
is_invalid_initial_token(t::AbstractToken) = is_invalid_initial_token(¬t)
end
const is_closing_token = let closing = Set([',', ')', ']', '}', ';', SYM_ELSE, SYM_ELSEIF, SYM_CATCH, SYM_FINALLY])
is_closing_token(ps::ParseState, t) =
((¬t === SYM_END && !ps.end_symbol) || Lexer.eof(t) || (isa(¬t, CharSymbol) && ¬t in closing))
end
is_dict_literal(ex) = isexpr(¬ex, :(=>)) && length((¬ex).args) == 2
is_parameter(ex) = isexpr(¬ex, :parameters)
function parse_chain(ps::ParseState, ts::TokenStream, down::Function, op)
chain = Any[down(ps, ts)]
while true
t = peek_token(ps, ts)
¬t !== op && return chain
take_token(ts)
if (ps.space_sensitive && ts.isspace &&
(isa(¬t, Symbol) && ¬t in Lexer.UNARY_AND_BINARY_OPS) &&
Lexer.peekchar(ts) != ' ')
# here we have "x -y"
put_back!(ts, t)
return chain
end
push!(chain, down(ps, ts))
end
end
# parse left to right chains of certain binary operator
# ex. a + b + c => Expr(:call, :+, a, b, c)
function parse_with_chains{T}(ps::ParseState, ts::TokenStream, down::Function, ops::Set{T}, chain_op)
ex = down(ps, ts)
while true
t = peek_token(ps, ts)
!(¬t in ops) && return ex
take_token(ts)
if (ps.space_sensitive && ts.isspace && (¬t in Lexer.UNARY_AND_BINARY_OPS) && Lexer.peekchar(ts) != ' ')
# here we have "x -y"
put_back!(ts, t)
return ex
elseif ¬t === chain_op
ex = ⨳(:call, t, ex) ⪥ parse_chain(ps, ts, down, ¬t)
else
ex = ⨳(:call, t, ex, down(ps, ts))
end
end
end
function parse_LtoR{T}(ps::ParseState, ts::TokenStream, down::Function, ops::Set{T}, ex=down(ps, ts))
while true
t = peek_token(ps, ts)
!(¬t in ops) && return ex
take_token(ts)
if Lexer.is_syntactic_op(¬t) || ¬t === :(::) ||
(VERSION < v"0.4.0-dev+573" && ¬t === :(in))
ex = ⨳(t, ex, down(ps, ts))
else
ex = ⨳(:call, t, ex, down(ps, ts))
end
t = peek_token(ps, ts)
end
end
function parse_RtoL{T}(ps::ParseState, ts::TokenStream, down::Function, ops::Set{T}, ex=down(ps, ts))
while true
t = peek_token(ps, ts)
!(¬t in ops) && return ex
take_token(ts)
if (ps.space_sensitive && ts.isspace &&
(isa(¬t, Symbol) && ¬t in Lexer.UNARY_AND_BINARY_OPS) && Lexer.peekchar(ts) !== ' ')
put_back!(ts, t)
return ex
elseif Lexer.is_syntactic_op(¬t)
return ⨳(t, ex, parse_RtoL(ps, ts, down, ops))
elseif ¬t === :(~)
args = parse_chain(ps, ts, down, :(~))
nt = peek_token(ps, ts)
if isa(¬nt, CharSymbol) && ¬nt in ops
ex = ⨳(:macrocall, Symbol("@~"), ex)
for i=1:length(args)-1
ex = ex ⪥ (args[i],)
end
ex = ex ⪥ (parse_RtoL(ps, ts, down, ops, args[end]),)
return ex
else
ex = ⨳(:macrocall, Symbol("@~"), ex) ⪥ args
return ex
end
else
return ⨳(:call, t, ex, parse_RtoL(ps, ts, down, ops))
end
end
end
function parse_cond(ps::ParseState, ts::TokenStream)
ex = (VERSION < v"0.4.0-dev+573" ? parse_or : parse_arrow)(ps, ts)
if ¬peek_token(ps, ts) === :(?)
t = take_token(ts)
then = @without_range_colon ps begin
parse_eqs(ps, ts)
end
nt = take_token(ts)
if ¬nt !== :(:)
D = diag(√nt, "colon expected in \"?\" expression")
diag(D, √t, "\"?\" was here")
throw(D)
end
return ⨳(:if, ex, then, parse_eqs(ps, ts))
end
return ex
end
function parse_Nary{T1, T2}(ps::ParseState, ts::TokenStream, down::Function, ops::Set{T1},
head::Symbol, closers::Set{T2}, allow_empty::Bool, add_linenums::Bool, opener = nothing)
t = require_token(ps, ts)
if is_invalid_initial_token(¬t)
D = diag(√t, "unexpected \"$(¬t)\" in \"$head\" expression")
diag(D, √opener, "expression started here")
throw(D)
end
loc = line_number_filename_node(ts) ⤄ Lexer.nullrange(ts)
# empty block
if isa(¬t, CharSymbol) && ¬t in closers
return ⨳(head, add_linenums ? loc : nothing) ⤄ t
end
local args::Vector{Any}
# in allow empty mode, skip leading runs of operator
if allow_empty && isa(¬t, CharSymbol) && ¬t in ops
args = Any[]
else
args = Any[down(ps, ts)]
# line-number must happend before (down s)
end
add_linenums && unshift!(args, loc)
isfirst = true
t = peek_token(ps, ts)
while true
if !(¬t in ops)
if !(Lexer.eof(t) || ¬t === '\n' || ',' in ops || ¬t in closers)
throw(diag(√t,"extra token \"$(¬t)\" after end of expression"))
end
if isempty(args) || length(args) >= 2 || !isfirst
# [] => Expr(:head)
# [ex1, ex2] => Expr(head, ex1, ex2)
# (ex1) if operator appeared => Expr(head,ex1) (handles "x;")
ex = ⨳(head, args...) ⤄ t
return ex
else
# [ex1] => ex1
return args[1]
end
end
isfirst = false
take_token(ts)
# allow input to end with the operator, as in a;b;
nt = peek_token(ps, ts)
if Lexer.eof(nt) || (isa(¬nt, CharSymbol) && ¬nt in closers) ||
(allow_empty && isa(¬nt, CharSymbol) && ¬nt in ops) ||
(length(ops) == 1 && ',' in ops && ¬nt === :(=))
t = nt
continue
end
add_linenums && !(length(args) >= 1 && isexpr(¬(args[end]), :line)) && push!(args, line_number_filename_node(ts))
push!(args, down(ps, ts))
t = peek_token(ps, ts)
end
end
# the principal non-terminals follow, in increasing precedence order
const BLOCK_OPS = Set(['\n', ';'])
const BLOCK_CLOSERS = Set([SYM_END, SYM_ELSE, SYM_ELSEIF, SYM_CATCH, SYM_FINALLY])
function parse_block(ps::ParseState, ts::TokenStream, down = parse_eq)
parse_Nary(ps, ts, down, BLOCK_OPS, :block, BLOCK_CLOSERS, true, true)
end
# for sequenced eval inside expressions, e.g. (a;b, c;d)
const WEXPR_OPS = Set([';'])
const WEXPR_CLOSERS = Set([',', ')'])
function parse_stmts_within_expr(ps::ParseState, ts::TokenStream)
parse_Nary(ps, ts, parse_eqs, WEXPR_OPS, :block, WEXPR_CLOSERS, true, false)
end
#; at the top level produces a sequence of top level expressions
const NL_CLOSER = Set(['\n'])
function parse_stmts(ps::ParseState, ts::TokenStream)
ex = parse_Nary(ps, ts, (ps, ts)->parse_docstring(ps, ts, parse_eq),
WEXPR_OPS, :toplevel, NL_CLOSER, true, false)
# check for unparsed junk after an expression
t = peek_token(ps, ts)
if !(Lexer.eof(t) || ¬t === '\n')
throw(diag(√t,"extra token \"$(¬t)\" after end of expression"))
end
return ex
end
function parse_assignment(ps, ts, down, ex = down(ps, ts))
t = peek_token(ps, ts)
if ¬t in Lexer.ASSIGNMENT_OPS
take_token(ts)
if ¬t == :~
if ps.space_sensitive && ts.isspace && Lexer.peekchar(ts) != ' '
put_back!(ts, t)
return ex
else
args = collect(children(parse_chain(ps, ts, down, :~)))
ex = ⨳(:macrocall,Symbol("@~")⤄t,ex) ⪥ args[1:end-1]
ex = ex ⪥ (parse_assignment(ps, ts, down, args[end]),)
end
else
ex = ⨳(t, ex, parse_assignment(ps, ts, down))
end
end
ex
end
function parse_eq(ps::ParseState, ts::TokenStream)
lno = curline(ts)
ex = parse_assignment(ps, ts, parse_comma)
return short_form_function_loc(ts, ex, lno, filename(ts))
end
# parse-eqs is used where commas are special for example in an argument list
function parse_eqs(ps::ParseState, ts::TokenStream)
lno = curline(ts)
ex = parse_assignment(ps, ts, parse_cond)
return short_form_function_loc(ts, ex, lno, filename(ts))
end
# parse-comma is neeed for commas outside parens, for example a = b, c
const EMPTY_SET = Set()
const COMMA_OPS = Set([','])
parse_comma(ps::ParseState, ts::TokenStream) = parse_Nary(ps, ts, parse_cond, COMMA_OPS, :tuple, EMPTY_SET, false, false)
const OR_OPS = Lexer.precedent_ops(:lazy_or)
function parse_or(ps::ParseState, ts::TokenStream)
(VERSION < v"0.4.0-dev+573" ? parse_LtoR : parse_RtoL)(
ps, ts, parse_and, OR_OPS)
end
const AND_OPS = Lexer.precedent_ops(:lazy_and)
function parse_and(ps::ParseState, ts::TokenStream)
if VERSION < v"0.4.0-dev+573"
parse_LtoR(ps, ts, parse_arrow, AND_OPS)
else
parse_RtoL(ps, ts, parse_comparison, AND_OPS)
end
end
const ARROW_OPS = Lexer.precedent_ops(:arrow)
parse_arrow(ps::ParseState, ts::TokenStream) =
parse_RtoL(ps, ts, VERSION < v"0.4.0-dev+573" ? parse_ineq : parse_or,
ARROW_OPS)
const INEQ_OPS = Lexer.precedent_ops(:comparison)
parse_ineq(ps::ParseState, ts::TokenStream) = parse_comparison(ps, ts, INEQ_OPS)
const PIPES_OPS = Lexer.precedent_ops(:pipe)
parse_pipes(ps::ParseState, ts::TokenStream) = parse_LtoR(ps, ts, parse_range, PIPES_OPS)
const IN_OPS = Set([:(in)])
parse_in(ps::ParseState, ts::TokenStream) = parse_LtoR(ps, ts, parse_pipes, IN_OPS)
const EXPR_OPS = Lexer.precedent_ops(:plus)
parse_expr(ps::ParseState, ts::TokenStream) = parse_with_chains(ps, ts, parse_shift, EXPR_OPS, :(+))
const SHIFT_OPS = Lexer.precedent_ops(:bitshift)
parse_shift(ps::ParseState, ts::TokenStream) = parse_LtoR(ps, ts, parse_term, SHIFT_OPS)
const TERM_OPS = Lexer.precedent_ops(:times)
parse_term(ps::ParseState, ts::TokenStream) = parse_with_chains(ps, ts, parse_rational, TERM_OPS, :(*))
const RAT_OPS = Lexer.precedent_ops(:rational)
parse_rational(ps::ParseState, ts::TokenStream) = parse_LtoR(ps, ts, parse_unary, RAT_OPS)
function parse_comparison(ps::ParseState, ts::TokenStream, ops=INEQ_OPS)
ex = VERSION < v"0.4.0-dev+573" ? parse_in(ps, ts) : parse_pipes(ps, ts)
isfirst = true
while true
t = peek_token(ps, ts)
if !(¬t in ops || ¬t === :in)
if VERSION > v"0.5.0-dev+3167" && !isfirst && length((¬ex).args) == 3
args = collect(children(ex))
return subtype_syntax(⨳(:call, args[2], args[1], args[3]))
end
return subtype_syntax(ex)
end
take_token(ts)
if isfirst
isfirst = false
ex = ⨳(:comparison, ex, t, parse_range(ps, ts))
else
ex = ex ⪥ (t,parse_range(ps, ts))
end
end
end
is_large_number(n::BigInt) = true
is_large_number(n::Number) = false
const is_juxtaposed = let invalid_chars = Set{Char}(['(', '[', '{'])
is_juxtaposed(ps::ParseState, ex, t) = begin
return !(Lexer.is_operator(¬t)) &&
!(Lexer.is_operator(¬ex)) &&
!(¬t in Lexer.RESERVED_WORDS) &&
!(is_closing_token(ps, t)) &&
!(Lexer.isnewline(¬t)) &&
!(isa(¬ex, Expr) && (¬ex).head === :(...)) &&
((isa(¬ex, Number) && !isa(¬ex, Char)) || !in(¬t, invalid_chars))
end
end
#= This handles forms such as 2x => Expr(:call, :*, 2, :x) =#
function parse_juxtaposed(ps::ParseState, ts::TokenStream, ex)
# numeric literal juxtaposition is a unary operator
if is_juxtaposed(ps, ex, peek_token(ps, ts)) && !ts.isspace
return ⨳(:call, :(*) ⤄ Lexer.nullrange(ts), ex, parse_unary(ps, ts))
end
return ex
end
function parse_range(ps::ParseState, ts::TokenStream)
ex = parse_expr(ps, ts)
isfirst = true
while true
t = peek_token(ps, ts)
if isfirst && ¬t === :(..)
take_token(ts)
return ⨳(:call, t, ex, parse_expr(ps, ts))
end
if ps.range_colon_enabled && ¬t === :(:)
take_token(ts)
if ps.space_sensitive && ts.isspace
peek_token(ps, ts)
if !ts.isspace
# "a :b" in space sensitive mode
put_back!(ts, t)
return ex
end
end
nt = peek_token(ps, ts)
range_error(D) = (diag(D, √ex ⤄ √t, "start of range was here"); throw(D))
if is_closing_token(ps, nt)
# handles :(>:) case
if isa(ex, Symbol) && Lexer.is_operator(¬ex)
op = Symbol(string(ex, t))
Lexer.is_operator(¬op) && return op
end
range_error(diag(√nt, "missing last argument in range expression"))
end
if Lexer.isnewline(¬nt)
range_error(diag(√nt, "line break in \":\" expression"))
end
arg = parse_expr(ps, ts)
if !ts.isspace && (¬arg === :(<) || ¬arg === :(>))
loc = √arg ⤄ √t
D = diag(loc,"Invalid \"$(¬arg)\" in range expression. Did you mean \"$(¬arg):\"?")
diag(D, loc,"$(¬arg):",:fixit)
# The user probably didn't intend a range, so don't add the
# range note
throw(D)
end
if isfirst
ex = ⨳(t, ex, arg)
isfirst = false
else
ex = ex ⪥ (arg,)
isfirst = true
end
continue
elseif ¬t === :(...)
take_token(ts)
return ⨳(t, ex)
else
return ex
end
end
end
function parse_decl(ps::ParseState, ts::TokenStream)
ex = parse_call(ps, ts)
while true
nt = peek_token(ps, ts)
# type assertion => x::Int
if ¬nt === :(::)
take_token(ts)
ex = ⨳(:(::), ex, parse_call(ps, ts))
continue
end
# anonymous function => (x) -> x + 1
if ¬nt === :(->)
take_token(ts)
# -> is unusual it binds tightly on the left and loosely on the right
lno = line_number_filename_node(ts) ⤄ Lexer.nullrange(ts)
eqs = parse_eqs(ps, ts)
return ⨳(:(->), ex, ⨳(:block, lno, eqs))
end
return ex
end
end
# handle ^ and .^
function parse_factorh(ps::ParseState, ts::TokenStream, down::Function, ops)
ex = down(ps, ts)
nt = peek_token(ps, ts)
!(¬nt in ops) && return ex
take_token(ts)
pf = parse_factorh(ps, ts, parse_unary, ops)
return ⨳(:call, nt, ex, pf)
end
function negate(n)
if isa(¬n, Number)
if isa(¬n, Int64) && ¬n == -9223372036854775808
# promote to Int128
return typeof(n)(9223372036854775808 ⤄ n)
end
if isa(¬n, Int128) && ¬n == -170141183460469231731687303715884105728
# promote to BigInt
return typeof(n)(Base.parse(BigInt, "170141183460469231731687303715884105728") ⤄ n)
end
return typeof(n)(-(¬n)) ⤄ n
elseif isa(¬n, Expr)
return (¬n).head === :(-) && length(n.args) == 1 ? (¬n).args[1] : ⨳(:-, n)
end
throw(ArgumentError("negate argument is not a Number or Expr"))
end
# -2^3 is parsed as -(2^3) so call parse-decl for the first arg,
# and parse unary from then on (handles 2^-3)
const FAC_OPS = Lexer.precedent_ops(13)
parse_factor(ps::ParseState, ts::TokenStream) = parse_factorh(ps, ts, parse_decl, FAC_OPS)
function parse_unary(ps::ParseState, ts::TokenStream)
t = require_token(ps, ts)
if is_closing_token(ps, t)
D = diag(√t, "unexpected \"$(¬t)\"")
throw(D)
end
if !(isa(¬t, Symbol) && ¬t in Lexer.UNARY_OPS)
pf = parse_factor(ps, ts)
return parse_juxtaposed(ps, ts, pf)
end
op = take_token(ts)
nc = Lexer.peekchar(ts)
if (¬op === :(-) || ¬op === :(+)) && (isdigit(nc) || nc === '.')
neg = ¬op === :(-)
leadingdot = nc === '.'
leadingdot && Lexer.readchar(ts)
n = Lexer.read_number(ts, leadingdot, neg)
num = parse_juxtaposed(ps, ts, n) ⤄ op
nt = peek_token(ps, ts)
if ¬nt === :(^) || ¬nt === :(.^)
# -2^x parsed as (- (^ 2 x))
put_back!(ts, neg ? negate(num) : num)
return ⨳(:call, op, parse_factor(ps, ts))
end
return num
end
nt = peek_token(ps, ts)
if is_closing_token(ps, nt) || Lexer.isnewline(¬nt) || (¬nt == :(=))
# return operator by itself, as in (+)
return op
elseif ¬nt === '{'
# this case is +{T}(x::T)
put_back!(ts, op)
return parse_factor(ps, ts)
else
arg = parse_unary(ps, ts)
if isexpr(¬arg, :tuple)
ex = ⨳(:call, op) ⪥ arg
return ex
end
return ⨳(:call, op, arg)
end
end
subtype_symbol(sym) = (sym == :(<:) || sym == :(>:))
function subtype_syntax(ex)
if isa(¬ex, Expr) && length((¬ex).args) == 3 &&
(((¬ex).head === :comparison && subtype_symbol((¬ex).args[2])) ||
((¬ex).head === :call && subtype_symbol((¬ex).args[1])))
args = collect(children(ex))
return ⨳((¬ex).head == :call ? (¬ex).args[1] : (¬ex).args[2],
(¬ex).head == :call ? args[2] : args[1], args[3]) ⤄ ex
end
return ex
end
function parse_unary_prefix(ps::ParseState, ts::TokenStream)
op = peek_token(ps, ts)
if isa(¬op, Symbol) && Lexer.is_syntactic_unary_op(¬op)
take_token(ts)
next = peek_token(ps, ts)
if is_closing_token(ps, next) || Lexer.isnewline(next)
return op
elseif ¬op === :(&) || ¬op === :(::)
return ⨳(op, parse_call(ps, ts))
else
return ⨳(op, parse_atom(ps, ts))
end
end
return parse_atom(ps, ts)
end
# parse function all, indexing, dot, and transpose expressions
# also handles looking for reserved words
function parse_call(ps::ParseState, ts::TokenStream)
ex = parse_unary_prefix(ps, ts)
if isa(¬ex, Symbol) && ¬ex in Lexer.RESERVED_WORDS
return parse_resword(ps, ts, ex)
end
return parse_call_chain(ps, ts, ex, false)
end
function separate(f::Function, collection)
tcoll, fcoll = Any[], Any[]
for c in collection
f(c) ? push!(tcoll, c) : push!(fcoll, c)
end
return (tcoll, fcoll)
end
const BEGIN_CHARS = Set(['(', '[','{', '"'])
function parse_call_chain(ps::ParseState, ts::TokenStream, ex, one_call::Bool)
while true
t = peek_token(ps, ts)
if (ps.space_sensitive && ts.isspace &&
(¬t === SYM_SQUOTE || ¬t in BEGIN_CHARS) ||
(isa(¬ex, Number) && ¬t === '('))
return ex
end
if ¬t === '('
take_token(ts)
arglist = parse_arglist(ps, ts, ')', t)
closer = take_token(ts)
params, args = separate(is_parameter, arglist)
nt = peek_token(ps, ts)
if ¬nt === SYM_DO
take_token(ts)
ex = ⨳(:call, ex) ⪥ params
ex ⪥ (parse_do(ps,ts,nt),)
ex ⪥ args
else
ex = ⨳(:call, ex) ⪥ arglist
end
ex = ex ⤄ closer
one_call && return ex
continue
elseif ¬t === '['
take_token(ts)
# ref is syntax so can distinguish a[i] = x from ref(a, i) = x
al = @with_end_symbol ps begin
parse_cat(ps, ts, ']', t, is_dict_literal(ex))
end
if isempty((¬al).args) && ((¬al).head === :cell1d || (¬al).head === :vcat)
ex = is_dict_literal(ex) ? ⨳(:typed_dict, ex) : ⨳(:ref, ex)
continue
end
if (¬al).head === :dict
ex = ⨳(is_dict_literal(ex) ? :typed_dict : :ref, ex) ⪥ al
elseif (¬al).head === :hcat
ex = ⨳(:typed_hcat, ex) ⪥ al
elseif (¬al).head === :vect
@assert VERSION >= v"0.4"
ex = ⨳(:ref, ex) ⪥ al
elseif (¬al).head === :vcat && VERSION < v"0.4"
ex = ⨳(:ref, ex)
for arg in (¬al).args
if isa(¬arg, Expr) && arg.head === :row
ex.head = :typed_vcat
end
push!((¬ex).args, arg)
end
elseif (¬al).head === :vcat
ex = ⨳(:typed_vcat, ex) ⪥ al
elseif (¬al).head === :comprehension
ex = ⨳(:typed_comprehension, ex) ⪥ al
elseif (¬al).head === :dict_comprehension
ex = ⨳(:typed_dict_comprehension, ex) ⪥ al
else
error("unknown parse-cat result (internal error)")
end
continue
elseif ¬t === :(.)
take_token(ts)
nt = peek_token(ps, ts)
if ¬nt === '('
ex = ⨳(:(.), ex, parse_atom(ps, ts))
elseif ¬nt === :($)
dollar_ex = parse_unary(ps, ts)
inert = ⨳(:inert, ⨳(nt) ⪥ dollar_ex)
ex = ⨳(:(.), ex, inert)
else
name = parse_atom(ps, ts)
if isexpr(¬name, :macrocall)
args = collect(children(name))
ex = ⨳(:macrocall, ⨳(:(.), ex, ⨳(:quote, args[1]))) ⪥ args[2:end]
else
ex = ⨳(:(.), ex, QuoteNode(¬name) ⤄ name)
end
end
continue
elseif ¬t === :(.') || ¬t === SYM_SQUOTE # '
take_token(ts)
ex = ⨳(t, ex)
continue
elseif ¬t === '{'
take_token(ts)
args = map(subtype_syntax, parse_arglist(ps, ts, '}', t))
# ::Type{T}
ex = ⨳(:curly, ex) ⪥ args
ex = ex ⤄ take_token(ts)
continue
elseif ¬t === '"'
if isa(¬ex, Symbol) && !Lexer.is_operator(¬ex) && !ts.isspace
# custom prefexed string literals x"s" => @x_str "s"
take_token(ts)
str = parse_string_literal(ps, ts, true)
nt = peek_token(ps, ts)
if VERSION < v"0.4"
suffix = triplequote_string_literal(str) ? "_mstr" : "_str"
macname = Symbol(string('@', ¬ex, suffix))
macstr = (¬str).args[1]
else
macname = Symbol(string('@',¬ex,"_str"))
macstr = first(children(str))
end
if isa(¬nt, Symbol) && !Lexer.is_operator(¬nt) && !ts.isspace
# string literal suffix "s"x
t = take_token(ts)
ex = ⨳(:macrocall, macname, macstr, string(¬t) ⤄ t)
else
ex = ⨳(:macrocall, macname, macstr)
end
continue
end
return ex
end
return ex
end
end
const expect_end_current_line = 0
function expect_end(ps::ParseState, ts::TokenStream, word)
t = peek_token(ps, ts)
if ¬t === SYM_END
take_token(ts)
elseif Lexer.eof(t)
D = Incomplete(:block, diag(here(ts),"incomplete: \"$(¬word)\" requires end"))
diag(D,√word,"\"$(¬word)\" began here")
throw(D)
else
D = diag(√t,"\"$(¬word)\" requires \"end\", got \"$(¬t)\"")
diag(D,√word,"\"$(¬word)\" began here")
throw(D)
end
end
parse_subtype_spec(ps::ParseState, ts::TokenStream) = subtype_syntax(parse_ineq(ps, ts))
#TODO: remove this after the 0.4 dev period
@eval function tryexpr(ts, tryb, catchv, catchb, finalb)
r = Lexer.nullrange(ts)
if finalb == nothing
return catchb != nothing ? ⨳(:try, tryb, catchv, catchb) :
$(VERSION > v"0.4.0-dev" ?
:(⨳(:try, tryb, false ⤄ r, ⨳(:block) ⤄ r)) :
:(⨳(:try, tryb, false ⤄ r, false ⤄ r)))
else
return catchb != nothing ? ⨳(:try, tryb, catchv, catchb, finalb) :
$(VERSION > v"0.4.0-dev" ?
:(⨳(:try, tryb, false ⤄ r, false ⤄ r, finalb)) :
:(⨳(:try, tryb, false ⤄ r, false ⤄ r, finalb)))
end
end
const _QuoteNode = QuoteNode
is_symbol_or_interpolate(ex) = isa(¬ex, Symbol) || isexpr(¬ex, :$)
# parse expressions or blocks introduced by syntatic reserved words
function parse_resword(ps::ParseState, ts::TokenStream, word, chain = nothing)
expect_end_current_line = curline(ts)
@with_normal_ops ps begin
@without_whitespace_newline ps begin
if ¬word === :quote || ¬word === :begin
Lexer.skipws_and_comments(ts)
loc = line_number_filename_node(ts)
blk = parse_block(ps, ts)
expect_end(ps, ts, word)
ex = blk
if !isempty((¬blk).args)
arg1 = collect(children(blk))[1]
if ((isa(¬arg1, Expr) && (¬arg1).head === :line) ||
(isa(¬arg1, LineNumberNode)))
ex = ⨳(:block, loc ⤄ Lexer.nullrange(ts))
ex = ex ⪥ collect(children(blk))[2:end]
end
else
ex = blk
end
return ¬word === :quote ? ⨳(word, ex) : ex
elseif ¬word === :while
ex = ⨳(word, parse_cond(ps, ts), parse_block(ps, ts))
expect_end(ps, ts, word)
return ex
elseif ¬word === :for
ranges = parse_comma_sep_iters(ps, ts, word)
nranges = length(ranges)
body = parse_block(ps, ts)
expect_end(ps, ts, word)
if nranges == 1
return ⨳(word, ranges[1], body)
else
blk = ⨳(:block, ranges...)
return ⨳(word, blk, body)
end
elseif ¬word === :if || ¬word == :elseif
if chain == nothing && ¬word == :elseif
throw(diag(√word,"\"elseif\" without preceeding if"))
end
if Lexer.isnewline(¬peek_token(ps, ts))
D = diag(√word, "missing condition in \"$(¬word)\"")
diag(D, √chain, "previous \"$(¬chain)\" was here")
throw(D)
end
test = parse_cond(ps, ts)
t = require_token(ps, ts)
then = (¬t === SYM_ELSE || ¬t === SYM_ELSEIF) ? ⨳(:block) ⤄ Lexer.nullrange(ts) :
parse_block(ps, ts)
nxt = require_token(ps, ts)
take_token(ts)
if ¬nxt === SYM_END
return ⨳(:if, test, then) ⤄ word
elseif ¬nxt === SYM_ELSEIF
blk = ⨳(:block, line_number_filename_node(ts), parse_resword(ps, ts, nxt, word))
return ⨳(:if, test, then, blk) ⤄ word
elseif ¬nxt === SYM_ELSE
nnxt = peek_token(ps, ts)
if ¬nnxt === :if
loc = √nxt ⤄ √nnxt
D = diag(loc, "use \"elseif\" instead of \"else if\"")
diag(D, loc, "elseif", :fixit)
throw(D)
end
blk = parse_block(ps, ts)
ex = ⨳(:if, test, then, blk) ⤄ word
expect_end(ps, ts, word)
return ex
else
D = diag(√nxt,"Unexpected \"$(¬nxt)\" in if expression")
diag(D, √word, "previous \"$(¬word)\" was here")
throw(D)
end
elseif ¬word === :let
nt = peek_token(ps, ts)
lno = curline(ts)
binds = Lexer.isnewline(¬nt) || ¬nt === ';' ? Any[] : parse_comma_sep_assigns(ps, ts)
nt = peek_token(ps, ts)
if !(Lexer.eof(nt) || (isa(¬nt, CharSymbol) && (¬nt === '\n' || ¬nt === ';' || ¬nt === SYM_END)))
D = diag(√nt, "let variables should end in \";\" or newline")
diag(D, √word, "\"let\" was here")
throw(D)
end
ex = parse_block(ps, ts)
expect_end(ps, ts, word)
# Don't need line number node in empty let blocks
length((¬ex).args) == 1 && (ex = ⨳(:block) ⤄ ex)
ex = ⨳(:let, ex) ⪥ binds
return ex
elseif ¬word === :global || ¬word === :local
lno = curline(ts)
isconst = ¬peek_token(ps, ts) === :const ? (take_token(ts); true) : false
args = parse_comma_sep_assigns(ps, ts)
if isconst
ex = ⨳(word, args...)
return ⨳(:const, ex)
else
ex = ⨳(word, args...)
return ex
end
elseif ¬word === :function || ¬word === :macro || ¬word === :stagedfunction
paren = ¬require_token(ps, ts) === '('
sig = parse_call(ps, ts)
local def
if is_symbol_or_interpolate(sig) ||
(isa(¬sig, Expr) && (¬sig).head === :(::) && isa((¬sig).args[1], Symbol))
if paren
# in function(x) the (x) is a tuple
def = ⨳(:tuple, sig)
else
¬peek_token(ps, ts) !== SYM_END && Lexer.skipws_and_comments(ts)
require_token(ps, ts)
expect_end(ps, ts, word)
return ⨳(word, sig)
end
else
if (isa(¬sig, Expr) && ((¬sig).head === :call || (¬sig).head === :tuple))
def = sig
else
D = diag(after(√sig), "expected \"(\" in $(¬word) definition")
diag(D, √word, "\"$(¬word)\" was here")
throw(D)
end
end
¬peek_token(ps, ts) !== SYM_END && Lexer.skipws_and_comments(ts)
loc = line_number_filename_node(ts)
body = parse_block(ps, ts)
expect_end(ps, ts, word)
add_filename_to_block!(body, loc)
ex = ⨳(word, def, body)
return ex
elseif ¬word === :abstract
return ⨳(:abstract, parse_subtype_spec(ps, ts))
elseif ¬word === :type || ¬word === :immutable
istype = ¬word === :type
if VERSION < v"0.4"
# allow "immutable type"