forked from JuliaLang/JuliaSyntax.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenize.jl
More file actions
1188 lines (1082 loc) · 30.4 KB
/
tokenize.jl
File metadata and controls
1188 lines (1082 loc) · 30.4 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
module Tokenize
export tokenize, untokenize, Tokens
using ..JuliaSyntax: Kind, @K_str
import ..JuliaSyntax: kind,
is_literal, is_error, is_contextual_keyword, is_word_operator
include("tokenize_utils.jl")
#-------------------------------------------------------------------------------
# Tokens
# Error kind => description
TOKEN_ERROR_DESCRIPTION = Dict{Kind, String}(
K"ErrorEofMultiComment" => "unterminated multi-line comment #= ... =#",
K"ErrorEofChar" => "unterminated character literal",
K"ErrorInvalidNumericConstant" => "invalid numeric constant",
K"ErrorInvalidOperator" => "invalid operator",
K"ErrorInvalidInterpolationTerminator" => "interpolated variable ends with invalid character; use `\$(...)` instead",
K"error" => "unknown error",
)
struct Token
kind::Kind
# Offsets into a string or buffer
startbyte::Int # The byte where the token start in the buffer
endbyte::Int # The byte where the token ended in the buffer
dotop::Bool
suffix::Bool
end
function Token(kind::Kind, startbyte::Int, endbyte::Int)
Token(kind, startbyte, endbyte, false, false)
end
Token() = Token(K"error", 0, 0, false, false)
const EMPTY_TOKEN = Token()
kind(t::Token) = t.kind
startbyte(t::Token) = t.startbyte
endbyte(t::Token) = t.endbyte
function untokenize(t::Token, str::String)
String(codeunits(str)[1 .+ (t.startbyte:t.endbyte)])
end
function Base.show(io::IO, t::Token)
print(io, rpad(string(startbyte(t), "-", endbyte(t)), 11, " "))
print(io, rpad(kind(t), 15, " "))
end
#-------------------------------------------------------------------------------
# Lexer
@inline ishex(c::Char) = isdigit(c) || ('a' <= c <= 'f') || ('A' <= c <= 'F')
@inline isbinary(c::Char) = c == '0' || c == '1'
@inline isoctal(c::Char) = '0' ≤ c ≤ '7'
@inline iswhitespace(c::Char) = Base.isspace(c) || c === '\ufeff'
struct StringState
triplestr::Bool
raw::Bool
delim::Char
paren_depth::Int
end
"""
`Lexer` reads from an input stream and emits a single token each time
`next_token` is called.
Ideally a lexer is stateless but some state is needed here for:
* Disambiguating cases like x' (adjoint) vs 'x' (character literal)
* Tokenizing code within string interpolations
"""
mutable struct Lexer{IO_t <: IO}
io::IO_t
io_startpos::Int
token_start_row::Int
token_start_col::Int
token_startpos::Int
current_row::Int
current_col::Int
current_pos::Int
last_token::Kind
string_states::Vector{StringState}
charstore::IOBuffer
chars::Tuple{Char,Char,Char,Char}
charspos::Tuple{Int,Int,Int,Int}
doread::Bool
dotop::Bool
errored::Bool
end
function Lexer(io::IO)
c1 = ' '
p1 = position(io)
if eof(io)
c2, p2 = EOF_CHAR, p1
c3, p3 = EOF_CHAR, p1
c4, p4 = EOF_CHAR, p1
else
c2 = read(io, Char)
p2 = position(io)
if eof(io)
c3, p3 = EOF_CHAR, p1
c4, p4 = EOF_CHAR, p1
else
c3 = read(io, Char)
p3 = position(io)
if eof(io)
c4, p4 = EOF_CHAR, p1
else
c4 = read(io, Char)
p4 = position(io)
end
end
end
Lexer(io, position(io), 1, 1, position(io), 1, 1, position(io),
K"error", Vector{StringState}(), IOBuffer(),
(c1,c2,c3,c4), (p1,p2,p3,p4), false, false, false)
end
Lexer(str::AbstractString) = Lexer(IOBuffer(str))
"""
tokenize(x, T = Token)
Returns an `Iterable` containing the tokenized input. Can be reverted by e.g.
`join(untokenize.(tokenize(x)))`. Setting `T` chooses the type of token
produced by the lexer (`Token` or `Token`).
"""
tokenize(x) = Lexer(x)
# Iterator interface
Base.IteratorSize(::Type{<:Lexer}) = Base.SizeUnknown()
Base.IteratorEltype(::Type{<:Lexer}) = Base.HasEltype()
Base.eltype(::Type{<:Lexer}) = Token
function Base.iterate(l::Lexer)
seekstart(l)
l.token_startpos = position(l)
l.token_start_row = 1
l.token_start_col = 1
l.current_row = 1
l.current_col = 1
l.current_pos = l.io_startpos
t = next_token(l)
return t, t.kind == K"EndMarker"
end
function Base.iterate(l::Lexer, isdone::Any)
isdone && return nothing
t = next_token(l)
return t, t.kind == K"EndMarker"
end
function Base.show(io::IO, l::Lexer)
print(io, typeof(l), " at position: ", position(l))
end
"""
startpos(l::Lexer)
Return the latest `Token`'s starting position.
"""
startpos(l::Lexer) = l.token_startpos
"""
startpos!(l::Lexer, i::Integer)
Set a new starting position.
"""
startpos!(l::Lexer, i::Integer) = l.token_startpos = i
Base.seekstart(l::Lexer) = seek(l.io, l.io_startpos)
"""
seek2startpos!(l::Lexer)
Sets the lexer's current position to the beginning of the latest `Token`.
"""
seek2startpos!(l::Lexer) = seek(l, startpos(l))
"""
peekchar(l::Lexer)
Returns the next character without changing the lexer's state.
"""
peekchar(l::Lexer) = l.chars[2]
"""
dpeekchar(l::Lexer)
Returns the next two characters without changing the lexer's state.
"""
dpeekchar(l::Lexer) = l.chars[2], l.chars[3]
"""
peekchar3(l::Lexer)
Returns the next three characters without changing the lexer's state.
"""
peekchar3(l::Lexer) = l.chars[2], l.chars[3], l.chars[4]
"""
position(l::Lexer)
Returns the current position.
"""
Base.position(l::Lexer) = l.charspos[1]
"""
eof(l::Lexer)
Determine whether the end of the lexer's underlying buffer has been reached.
"""# Base.position(l::Lexer) = Base.position(l.io)
Base.eof(l::Lexer) = eof(l.io)
Base.seek(l::Lexer, pos) = seek(l.io, pos)
"""
start_token!(l::Lexer)
Updates the lexer's state such that the next `Token` will start at the current
position.
"""
function start_token!(l::Lexer)
l.token_startpos = l.charspos[1]
l.token_start_row = l.current_row
l.token_start_col = l.current_col
end
"""
readchar(l::Lexer)
Returns the next character and increments the current position.
"""
function readchar end
function readchar(l::Lexer)
c = readchar(l.io)
l.chars = (l.chars[2], l.chars[3], l.chars[4], c)
l.charspos = (l.charspos[2], l.charspos[3], l.charspos[4], position(l.io))
return l.chars[1]
end
"""
accept(l::Lexer, f::Union{Function, Char, Vector{Char}, String})
Consumes the next character `c` if either `f::Function(c)` returns true, `c == f`
for `c::Char` or `c in f` otherwise. Returns `true` if a character has been
consumed and `false` otherwise.
"""
@inline function accept(l::Lexer, f::Union{Function, Char, Vector{Char}, String})
c = peekchar(l)
if isa(f, Function)
ok = f(c)
elseif isa(f, Char)
ok = c == f
else
ok = c in f
end
ok && readchar(l)
return ok
end
"""
accept_batch(l::Lexer, f)
Consumes all following characters until `accept(l, f)` is `false`.
"""
@inline function accept_batch(l::Lexer, f)
ok = false
while accept(l, f)
ok = true
end
return ok
end
"""
emit(l::Lexer, kind::Kind)
Returns a `Token` of kind `kind` with contents `str` and starts a new `Token`.
"""
function emit(l::Lexer, kind::Kind)
suffix = false
if optakessuffix(kind)
while isopsuffix(peekchar(l))
readchar(l)
suffix = true
end
end
tok = Token(kind, startpos(l), position(l) - 1, l.dotop, suffix)
l.dotop = false
l.last_token = kind
return tok
end
"""
emit_error(l::Lexer, err::Kind=K"error")
Returns an `K"error"` token with error `err` and starts a new `Token`.
"""
function emit_error(l::Lexer, err::Kind = K"error")
l.errored = true
@assert is_error(err)
return emit(l, err)
end
"""
next_token(l::Lexer)
Returns the next `Token`.
"""
function next_token(l::Lexer, start = true)
start && start_token!(l)
if !isempty(l.string_states)
lex_string_chunk(l)
else
_next_token(l, readchar(l))
end
end
function _next_token(l::Lexer, c)
if c == EOF_CHAR
return emit(l, K"EndMarker")
elseif iswhitespace(c)
return lex_whitespace(l, c)
elseif c == '['
return emit(l, K"[")
elseif c == ']'
return emit(l, K"]")
elseif c == '{'
return emit(l, K"{")
elseif c == ';'
return emit(l, K";")
elseif c == '}'
return emit(l, K"}")
elseif c == '('
return emit(l, K"(")
elseif c == ')'
return emit(l, K")")
elseif c == ','
return emit(l, K",")
elseif c == '*'
return lex_star(l);
elseif c == '^'
return lex_circumflex(l);
elseif c == '@'
return emit(l, K"@")
elseif c == '?'
return emit(l, K"?")
elseif c == '$'
return lex_dollar(l);
elseif c == '⊻'
return lex_xor(l);
elseif c == '~'
return emit(l, K"~")
elseif c == '#'
return lex_comment(l)
elseif c == '='
return lex_equal(l)
elseif c == '!'
return lex_exclaim(l)
elseif c == '>'
return lex_greater(l)
elseif c == '<'
return lex_less(l)
elseif c == ':'
return lex_colon(l)
elseif c == '|'
return lex_bar(l)
elseif c == '&'
return lex_amper(l)
elseif c == '\''
return lex_prime(l)
elseif c == '÷'
return lex_division(l)
elseif c == '"'
return lex_quote(l);
elseif c == '%'
return lex_percent(l);
elseif c == '/'
return lex_forwardslash(l);
elseif c == '\\'
return lex_backslash(l);
elseif c == '.'
return lex_dot(l);
elseif c == '+'
return lex_plus(l);
elseif c == '-'
return lex_minus(l);
elseif c == '−' # \minus '−' treated as hyphen '-'
return emit(l, accept(l, '=') ? K"-=" : K"-")
elseif c == '`'
return lex_backtick(l);
elseif is_identifier_start_char(c)
return lex_identifier(l, c)
elseif isdigit(c)
return lex_digit(l, K"Integer")
elseif (k = get(UNICODE_OPS, c, K"error")) != K"error"
return emit(l, k)
else
emit_error(l)
end
end
# We're inside a string; possibly reading the string characters, or maybe in
# Julia code within an interpolation.
function lex_string_chunk(l)
state = last(l.string_states)
if state.paren_depth > 0
# Read normal Julia code inside an interpolation but track nesting of
# parentheses.
c = readchar(l)
if c == '('
l.string_states[end] = StringState(state.triplestr, state.raw, state.delim,
state.paren_depth + 1)
return emit(l, K"(")
elseif c == ')'
l.string_states[end] = StringState(state.triplestr, state.raw, state.delim,
state.paren_depth - 1)
return emit(l, K")")
else
return _next_token(l, c)
end
end
pc = peekchar(l)
if l.last_token == K"$"
pc = peekchar(l)
# Interpolated symbol or expression
if pc == '('
readchar(l)
l.string_states[end] = StringState(state.triplestr, state.raw, state.delim,
state.paren_depth + 1)
return emit(l, K"(")
elseif is_identifier_start_char(pc)
return lex_identifier(l, readchar(l))
else
# Getting here is a syntax error - fall through to reading string
# characters and let the parser deal with it.
end
elseif l.last_token == K"Identifier" &&
!(pc == EOF_CHAR || is_operator_start_char(pc) || is_never_id_char(pc))
# Only allow certain characters after interpolated vars
# https://github.com/JuliaLang/julia/pull/25234
return emit_error(l, K"ErrorInvalidInterpolationTerminator")
end
if pc == EOF_CHAR
return emit(l, K"EndMarker")
elseif !state.raw && pc == '$'
# Start interpolation
readchar(l)
return emit(l, K"$")
elseif !state.raw && pc == '\\' && (pc2 = dpeekchar(l)[2];
pc2 == '\r' || pc2 == '\n')
# Process escaped newline as whitespace
readchar(l)
readchar(l)
if pc2 == '\r' && peekchar(l) == '\n'
readchar(l)
end
while (pc = peekchar(l); pc == ' ' || pc == '\t')
readchar(l)
end
return emit(l, K"Whitespace")
elseif pc == state.delim && string_terminates(l, state.delim, state.triplestr)
# Terminate string
pop!(l.string_states)
readchar(l)
if state.triplestr
readchar(l); readchar(l)
return emit(l, state.delim == '"' ?
K"\"\"\"" : K"```")
else
return emit(l, state.delim == '"' ? K"\"" : K"`")
end
end
# Read a chunk of string characters
if state.raw
# Raw strings treat all characters as literals with the exception that
# the closing quotes can be escaped with an odd number of \ characters.
while true
pc = peekchar(l)
if string_terminates(l, state.delim, state.triplestr) || pc == EOF_CHAR
break
elseif state.triplestr && (pc == '\n' || pc == '\r')
# triple quoted newline splitting
readchar(l)
if pc == '\r' && peekchar(l) == '\n'
readchar(l)
end
break
end
c = readchar(l)
if c == '\\'
n = 1
while peekchar(l) == '\\'
readchar(l)
n += 1
end
if peekchar(l) == state.delim && !iseven(n)
readchar(l)
end
end
end
else
while true
pc = peekchar(l)
if pc == '$' || pc == EOF_CHAR
break
elseif state.triplestr && (pc == '\n' || pc == '\r')
# triple quoted newline splitting
readchar(l)
if pc == '\r' && peekchar(l) == '\n'
readchar(l)
end
break
elseif pc == state.delim && string_terminates(l, state.delim, state.triplestr)
break
elseif pc == '\\'
# Escaped newline
pc2 = dpeekchar(l)[2]
if pc2 == '\r' || pc2 == '\n'
break
end
end
c = readchar(l)
if c == '\\'
c = readchar(l)
c == EOF_CHAR && break
continue
end
end
end
return emit(l, state.delim == '"' ? K"String" : K"CmdString")
end
# Lex whitespace, a whitespace char `c` has been consumed
function lex_whitespace(l::Lexer, c)
k = K"Whitespace"
while true
if c == '\n'
k = K"NewlineWs"
end
pc = peekchar(l)
# stop on non whitespace and limit to a single newline in a token
if !iswhitespace(pc) || (k == K"NewlineWs" && pc == '\n')
break
end
c = readchar(l)
end
return emit(l, k)
end
function lex_comment(l::Lexer, doemit=true)
if peekchar(l) != '='
while true
pc = peekchar(l)
if pc == '\n' || pc == EOF_CHAR
return doemit ? emit(l, K"Comment") : EMPTY_TOKEN
end
readchar(l)
end
else
pc = '#'
c = readchar(l) # consume the '='
n_start, n_end = 1, 0
while true
if c == EOF_CHAR
return doemit ? emit_error(l, K"ErrorEofMultiComment") : EMPTY_TOKEN
end
nc = readchar(l)
if c == '#' && nc == '='
n_start += 1
elseif c == '=' && nc == '#' && pc != '#'
n_end += 1
end
if n_start == n_end
return doemit ? emit(l, K"Comment") : EMPTY_TOKEN
end
pc = c
c = nc
end
end
end
# Lex a greater char, a '>' has been consumed
function lex_greater(l::Lexer)
if accept(l, '>')
if accept(l, '>')
if accept(l, '=')
return emit(l, K">>>=")
else # >>>?, ? not a =
return emit(l, K">>>")
end
elseif accept(l, '=')
return emit(l, K">>=")
else
return emit(l, K">>")
end
elseif accept(l, '=')
return emit(l, K">=")
elseif accept(l, ':')
return emit(l, K">:")
else
return emit(l, K">")
end
end
# Lex a less char, a '<' has been consumed
function lex_less(l::Lexer)
if accept(l, '<')
if accept(l, '=')
return emit(l, K"<<=")
else # '<<?', ? not =, ' '
return emit(l, K"<<")
end
elseif accept(l, '=')
return emit(l, K"<=")
elseif accept(l, ':')
return emit(l, K"<:")
elseif accept(l, '|')
return emit(l, K"<|")
elseif dpeekchar(l) == ('-', '-')
readchar(l); readchar(l)
if accept(l, '>')
return emit(l, K"<-->")
else
return emit(l, K"<--")
end
else
return emit(l, K"<")
end
end
# Lex all tokens that start with an = character.
# An '=' char has been consumed
function lex_equal(l::Lexer)
if accept(l, '=')
if accept(l, '=')
emit(l, K"===")
else
emit(l, K"==")
end
elseif accept(l, '>')
emit(l, K"=>")
else
emit(l, K"=")
end
end
# Lex a colon, a ':' has been consumed
function lex_colon(l::Lexer)
if accept(l, ':')
return emit(l, K"::")
elseif accept(l, '=')
return emit(l, K":=")
else
return emit(l, K":")
end
end
function lex_exclaim(l::Lexer)
if accept(l, '=')
if accept(l, '=')
return emit(l, K"!==")
else
return emit(l, K"!=")
end
else
return emit(l, K"!")
end
end
function lex_percent(l::Lexer)
if accept(l, '=')
return emit(l, K"%=")
else
return emit(l, K"%")
end
end
function lex_bar(l::Lexer)
if accept(l, '=')
return emit(l, K"|=")
elseif accept(l, '>')
return emit(l, K"|>")
elseif accept(l, '|')
return emit(l, K"||")
else
emit(l, K"|")
end
end
function lex_plus(l::Lexer)
if accept(l, '+')
return emit(l, K"++")
elseif accept(l, '=')
return emit(l, K"+=")
end
return emit(l, K"+")
end
function lex_minus(l::Lexer)
if accept(l, '-')
if accept(l, '>')
return emit(l, K"-->")
else
return emit_error(l, K"ErrorInvalidOperator") # "--" is an invalid operator
end
elseif !l.dotop && accept(l, '>')
return emit(l, K"->")
elseif accept(l, '=')
return emit(l, K"-=")
end
return emit(l, K"-")
end
function lex_star(l::Lexer)
if accept(l, '*')
return emit_error(l, K"ErrorInvalidOperator") # "**" is an invalid operator use ^
elseif accept(l, '=')
return emit(l, K"*=")
end
return emit(l, K"*")
end
function lex_circumflex(l::Lexer)
if accept(l, '=')
return emit(l, K"^=")
end
return emit(l, K"^")
end
function lex_division(l::Lexer)
if accept(l, '=')
return emit(l, K"÷=")
end
return emit(l, K"÷")
end
function lex_dollar(l::Lexer)
if accept(l, '=')
return emit(l, K"$=")
end
return emit(l, K"$")
end
function lex_xor(l::Lexer)
if accept(l, '=')
return emit(l, K"⊻=")
end
return emit(l, K"⊻")
end
function accept_number(l::Lexer, f::F) where {F}
lexed_number = false
while true
pc, ppc = dpeekchar(l)
if pc == '_' && !f(ppc)
return lexed_number
elseif f(pc) || pc == '_'
readchar(l)
else
return lexed_number
end
lexed_number = true
end
end
# A digit has been consumed
function lex_digit(l::Lexer, kind)
accept_number(l, isdigit)
pc,ppc = dpeekchar(l)
if pc == '.'
if kind === K"Float"
# If we enter the function with kind == K"Float" then a '.' has been parsed.
readchar(l)
return emit_error(l, K"ErrorInvalidNumericConstant")
elseif ppc == '.'
return emit(l, kind)
elseif is_operator_start_char(ppc) && ppc !== ':'
readchar(l)
return emit_error(l)
elseif (!(isdigit(ppc) ||
iswhitespace(ppc) ||
is_identifier_start_char(ppc)
|| ppc == '('
|| ppc == ')'
|| ppc == '['
|| ppc == ']'
|| ppc == '{'
|| ppc == '}'
|| ppc == ','
|| ppc == ';'
|| ppc == '@'
|| ppc == '`'
|| ppc == '"'
|| ppc == ':'
|| ppc == '?'
|| ppc == EOF_CHAR))
kind = K"Integer"
return emit(l, kind)
end
readchar(l)
kind = K"Float"
accept_number(l, isdigit)
pc, ppc = dpeekchar(l)
if (pc == 'e' || pc == 'E' || pc == 'f') && (isdigit(ppc) || ppc == '+' || ppc == '-' || ppc == '−')
kind = K"Float"
readchar(l)
accept(l, "+-−")
if accept_batch(l, isdigit)
pc,ppc = dpeekchar(l)
if pc === '.' && !dotop2(ppc, ' ')
accept(l, '.')
return emit_error(l, K"ErrorInvalidNumericConstant")
end
else
return emit_error(l)
end
elseif pc == '.' && (is_identifier_start_char(ppc) || ppc == EOF_CHAR)
readchar(l)
return emit_error(l, K"ErrorInvalidNumericConstant")
end
elseif (pc == 'e' || pc == 'E' || pc == 'f') && (isdigit(ppc) || ppc == '+' || ppc == '-' || ppc == '−')
kind = K"Float"
readchar(l)
accept(l, "+-−")
if accept_batch(l, isdigit)
pc,ppc = dpeekchar(l)
if pc === '.' && !dotop2(ppc, ' ')
accept(l, '.')
return emit_error(l, K"ErrorInvalidNumericConstant")
end
else
return emit_error(l)
end
elseif position(l) - startpos(l) == 1 && l.chars[1] == '0'
kind == K"Integer"
if pc == 'x'
kind = K"HexInt"
isfloat = false
readchar(l)
!(ishex(ppc) || ppc == '.') && return emit_error(l, K"ErrorInvalidNumericConstant")
accept_number(l, ishex)
if accept(l, '.')
accept_number(l, ishex)
isfloat = true
end
if accept(l, "pP")
kind = K"Float"
accept(l, "+-−")
if !accept_number(l, isdigit)
return emit_error(l, K"ErrorInvalidNumericConstant")
end
elseif isfloat
return emit_error(l, K"ErrorInvalidNumericConstant")
end
elseif pc == 'b'
!isbinary(ppc) && return emit_error(l, K"ErrorInvalidNumericConstant")
readchar(l)
accept_number(l, isbinary)
kind = K"BinInt"
elseif pc == 'o'
!isoctal(ppc) && return emit_error(l, K"ErrorInvalidNumericConstant")
readchar(l)
accept_number(l, isoctal)
kind = K"OctInt"
end
end
return emit(l, kind)
end
function lex_prime(l, doemit = true)
if l.last_token == K"Identifier" ||
is_contextual_keyword(l.last_token) ||
is_word_operator(l.last_token) ||
l.last_token == K"." ||
l.last_token == K")" ||
l.last_token == K"]" ||
l.last_token == K"}" ||
l.last_token == K"'" ||
l.last_token == K"end" || is_literal(l.last_token)
return emit(l, K"'")
else
if accept(l, '\'')
if accept(l, '\'')
return doemit ? emit(l, K"Char") : EMPTY_TOKEN
else
# Empty char literal
# Arguably this should be an error here, but we generally
# look at the contents of the char literal in the parser,
# so we defer erroring until there.
return doemit ? emit(l, K"Char") : EMPTY_TOKEN
end
end
while true
c = readchar(l)
if c == EOF_CHAR
return doemit ? emit_error(l, K"ErrorEofChar") : EMPTY_TOKEN
elseif c == '\\'
if readchar(l) == EOF_CHAR
return doemit ? emit_error(l, K"ErrorEofChar") : EMPTY_TOKEN
end
elseif c == '\''
return doemit ? emit(l, K"Char") : EMPTY_TOKEN
end
end
end
end
function lex_amper(l::Lexer)
if accept(l, '&')
return emit(l, K"&&")
elseif accept(l, '=')
return emit(l, K"&=")
else
return emit(l, K"&")
end
end
# Parse a token starting with a quote.
# A '"' has been consumed
function lex_quote(l::Lexer)
raw = l.last_token == K"Identifier" ||
is_contextual_keyword(l.last_token) ||
is_word_operator(l.last_token)
pc, dpc = dpeekchar(l)
triplestr = pc == '"' && dpc == '"'
push!(l.string_states, StringState(triplestr, raw, '"', 0))
if triplestr
readchar(l)
readchar(l)
emit(l, K"\"\"\"")
else
emit(l, K"\"")
end
end
function string_terminates(l, delim::Char, triplestr::Bool)
if triplestr
c1, c2, c3 = peekchar3(l)
c1 === delim && c2 === delim && c3 === delim
else
peekchar(l) === delim
end
end
# Parse a token starting with a forward slash.
# A '/' has been consumed
function lex_forwardslash(l::Lexer)
if accept(l, '/')
if accept(l, '=')
return emit(l, K"//=")
else
return emit(l, K"//")
end
elseif accept(l, '=')
return emit(l, K"/=")
else
return emit(l, K"/")
end
end
function lex_backslash(l::Lexer)
if accept(l, '=')
return emit(l, K"\=")
end
return emit(l, K"\\")
end
# TODO .op
function lex_dot(l::Lexer)
if accept(l, '.')
if accept(l, '.')
return emit(l, K"...")
else
return emit(l, K"..")
end
elseif Base.isdigit(peekchar(l))
return lex_digit(l, K"Float")
else
pc, dpc = dpeekchar(l)
if dotop1(pc)