forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.dd
1124 lines (910 loc) · 29.9 KB
/
lex.dd
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
Ddoc
$(SPEC_S Lexical,
The lexical analysis is independent of the syntax parsing and the
semantic analysis. The lexical analyzer splits the source text up into
tokens. The lexical grammar describes what those tokens are. The
grammar is designed to be suitable for high speed scanning, it
has a minimum of special case rules, there is only one phase of
translation, and to make it easy to write a correct scanner
for. The tokens are readily recognizable by those familiar with C and
C++.
$(H3 Source Text)
D source text can be in one of the following formats:
$(LIST
ASCII,
UTF-8,
UTF-16BE,
UTF-16LE,
UTF-32BE,
UTF-32LE
)
UTF-8 is a superset of traditional 7-bit ASCII.
One of the
following UTF BOMs (Byte Order Marks) can be present at the beginning
of the source text:
$(P )
$(TABLE2 UTF Byte Order Marks,
$(THEAD Format, BOM)
$(TROW UTF-8, EF BB BF)
$(TROW UTF-16BE, FE FF)
$(TROW UTF-16LE, FF FE)
$(TROW UTF-32BE, 00 00 FE FF)
$(TROW UTF-32LE, FF FE 00 00)
$(TROW ASCII, no BOM)
)
$(P If the source file does not start with a BOM, then the first
character must be less than or equal to U0000007F.)
$(P There are no digraphs or trigraphs in D.)
$(P The source text is decoded from its source representation
into Unicode $(GLINK Character)s.
The $(GLINK Character)s are further divided into:
$(GLINK WhiteSpace),
$(GLINK EndOfLine),
$(GLINK Comment)s,
$(GLINK SpecialTokenSequence)s,
$(GLINK Token)s,
all followed by $(GLINK EndOfFile).
)
$(P The source text is split into tokens using the maximal munch
technique, i.e., the
lexical analyzer tries to make the longest token it can. For example
$(D >>) is a right shift token,
not two greater than tokens. An exception to this rule is that a $(D ..)
embedded inside what looks like two floating point literals, as in
$(D 1..2), is interpreted as if the $(D ..) was separated by a space from the
first integer.
)
$(H3 Character Set)
$(GRAMMAR
$(GNAME Character):
$(I any Unicode character)
)
$(H3 End of File)
$(GRAMMAR
$(GNAME EndOfFile):
$(I physical end of the file)
$(D \u0000)
$(D \u001A))
The source text is terminated by whichever comes first.
$(H3 End of Line)
$(GRAMMAR
$(GNAME EndOfLine):
$(D \u000D)
$(D \u000A)
$(D \u000D \u000A)
$(D \u2028)
$(D \u2029)
$(GLINK EndOfFile))
There is no backslash line splicing, nor are there any limits
on the length of a line.
$(H3 White Space)
$(GRAMMAR
$(GNAME WhiteSpace):
$(GLINK Space)
$(GLINK Space) $(I WhiteSpace)
$(GNAME Space):
$(D \u0020)
$(D \u0009)
$(D \u000B)
$(D \u000C)
)
$(H3 $(LNAME2 comment, Comments))
$(GRAMMAR
$(GNAME Comment):
$(GLINK BlockComment)
$(GLINK LineComment)
$(GLINK NestingBlockComment)
$(GNAME BlockComment):
$(B /*) $(GLINK Characters) $(B */)
$(GNAME LineComment):
$(B //) $(GLINK Characters) $(GLINK EndOfLine)
$(GNAME NestingBlockComment):
$(B /+) $(GLINK NestingBlockCommentCharacters) $(B +/)
$(GNAME NestingBlockCommentCharacters):
$(GLINK NestingBlockCommentCharacter)
$(GLINK NestingBlockCommentCharacter) $(I NestingBlockCommentCharacters)
$(GNAME NestingBlockCommentCharacter):
$(GLINK Character)
$(GLINK NestingBlockComment)
$(GNAME Characters):
$(GLINK Character)
$(GLINK Character) $(I Characters))
$(P D has three kinds of comments:)
$(OL
$(LI Block comments can span multiple lines, but do not nest.)
$(LI Line comments terminate at the end of the line.)
$(LI Nesting block comments can span multiple lines and can nest.)
)
$(P
The contents of strings and comments are not tokenized. Consequently,
comment openings occurring within a string do not begin a comment, and
string delimiters within a comment do not affect the recognition of
comment closings and nested "/+" comment openings. With the exception
of "/+" occurring within a "/+" comment, comment openings within a
comment are ignored.
)
-------------
a = /+ // +/ 1; // parses as if 'a = 1;'
a = /+ "+/" +/ 1"; // parses as if 'a = " +/ 1";'
a = /+ /* +/ */ 3; // parses as if 'a = */ 3;'
-------------
Comments cannot be used as token concatenators, for example,
$(D abc/**/def) is two tokens, $(D abc) and $(D def),
not one $(D abcdef) token.
$(H3 Tokens)
$(GRAMMAR
$(GNAME Token):
$(MULTICOLS 4, $(GLINK Identifier)
$(GLINK StringLiteral)
$(GLINK CharacterLiteral)
$(GLINK IntegerLiteral)
$(GLINK FloatLiteral)
$(GLINK Keyword)
$(D /)
$(D /=)
$(D .)
$(D ..)
$(D ...)
$(CODE_AMP)
$(CODE_AMP)$(D =)
$(CODE_AMP)$(CODE_AMP)
$(D |)
$(D |=)
$(D ||)
$(D -)
$(D -=)
$(D --)
$(D +)
$(D +=)
$(D ++)
$(D <)
$(D <)$(D =)
$(D <)$(D <)
$(D <)$(D <)$(D =)
$(D <)$(D >)
$(D <)$(D >)$(D =)
$(D >)
$(D >)$(D =)
$(D >)$(D >)$(D =)
$(D >)$(D >)$(D >)$(D =)
$(D >)$(D >)
$(D >)$(D >)$(D >)
$(D !)
$(D !=)
$(D !<>)
$(D !<>=)
$(D !<)
$(D !<=)
$(D !>)
$(D !>=)
$(D $(LPAREN))
$(D $(RPAREN))
$(D [)
$(D ])
$(CODE_LCURL)
$(CODE_RCURL)
$(D ?)
$(D ,)
$(D ;)
$(D :)
$(D $)
$(D =)
$(D ==)
$(D *)
$(D *=)
$(CODE_PERCENT)
$(CODE_PERCENT)$(D =)
$(D ^)
$(D ^=)
$(D ^^)
$(D ^^=)
$(D ~)
$(D ~=)
$(D @)
$(D =>)
$(D #)
)
)
$(H3 Identifiers)
$(GRAMMAR
$(GNAME Identifier):
$(GLINK IdentifierStart)
$(GLINK IdentifierStart) $(GLINK IdentifierChars)
$(GNAME IdentifierChars):
$(GLINK IdentifierChar)
$(GLINK IdentifierChar) $(I IdentifierChars)
$(GNAME IdentifierStart):
$(D _)
$(I Letter)
$(I UniversalAlpha)
$(GNAME IdentifierChar):
$(GLINK IdentifierStart)
$(B 0)
$(GLINK NonZeroDigit)
)
Identifiers start with a letter, $(D _), or universal alpha,
and are followed by any number
of letters, $(D _), digits, or universal alphas.
Universal alphas are as defined in ISO/IEC 9899:1999(E) Appendix D.
(This is the C99 Standard.)
Identifiers can be arbitrarily long, and are case sensitive.
Identifiers starting with $(D __) (two underscores) are reserved.
$(H3 String Literals)
$(GRAMMAR
$(GNAME StringLiteral):
$(GLINK WysiwygString)
$(GLINK AlternateWysiwygString)
$(GLINK DoubleQuotedString)
$(GLINK HexString)
$(GLINK DelimitedString)
$(GLINK TokenString)
$(GNAME WysiwygString):
$(B r") $(GLINK WysiwygCharacters) $(B ") $(GLINK StringPostfix)$(OPT)
$(GNAME AlternateWysiwygString):
$(B `) $(GLINK WysiwygCharacters) $(B `) $(GLINK StringPostfix)$(OPT)
$(GNAME WysiwygCharacters):
$(GLINK WysiwygCharacter)
$(GLINK WysiwygCharacter) $(I WysiwygCharacters)
$(GNAME WysiwygCharacter):
$(GLINK Character)
$(GLINK EndOfLine)
$(GNAME DoubleQuotedString):
$(B ") $(GLINK DoubleQuotedCharacters) $(B ") $(GLINK StringPostfix)$(OPT)
$(GNAME DoubleQuotedCharacters):
$(GLINK DoubleQuotedCharacter)
$(GLINK DoubleQuotedCharacter) $(I DoubleQuotedCharacters)
$(GNAME DoubleQuotedCharacter):
$(GLINK Character)
$(GLINK EscapeSequence)
$(GLINK EndOfLine)
$(GNAME EscapeSequence):
$(D \')
$(D \")
$(D \?)
$(D \\)
$(D \0)
$(D \a)
$(D \b)
$(D \f)
$(D \n)
$(D \r)
$(D \t)
$(D \v)
$(D \x) $(GLINK HexDigit) $(GLINK HexDigit)
$(D \) $(GLINK OctalDigit)
$(D \) $(GLINK OctalDigit) $(GLINK OctalDigit)
$(D \) $(GLINK OctalDigit) $(GLINK OctalDigit) $(GLINK OctalDigit)
$(D \u) $(GLINK HexDigit) $(GLINK HexDigit) $(GLINK HexDigit) $(GLINK HexDigit)
$(D \U) $(GLINK HexDigit) $(GLINK HexDigit) $(GLINK HexDigit) $(GLINK HexDigit) $(GLINK HexDigit) $(GLINK HexDigit) $(GLINK HexDigit) $(GLINK HexDigit)
$(D \) $(GLINK2 entity, NamedCharacterEntity)
$(GNAME HexString):
$(D x") $(GLINK HexStringChars) $(D ") $(GLINK StringPostfix)$(OPT)
$(GNAME HexStringChars):
$(GLINK HexStringChar)
$(GLINK HexStringChar) $(I HexStringChars)
$(GNAME HexStringChar):
$(GLINK HexDigit)
$(GLINK WhiteSpace)
$(GLINK EndOfLine)
$(GNAME StringPostfix):
$(B c)
$(B w)
$(B d)
$(GNAME DelimitedString):
$(B q") $(I Delimiter) $(GLINK WysiwygCharacters) $(I MatchingDelimiter) $(B ")
$(GNAME TokenString):
$(D q)$(CODE_LCURL) $(GLINK Token)s $(CODE_RCURL)
)
$(P
A string literal is either a double quoted string, a wysiwyg quoted
string, an escape sequence,
a delimited string, a token string,
or a hex string.
)
$(P In all string literal forms, an $(GLINK EndOfLine) is regarded as a single
$(D \n) character.)
$(H4 Wysiwyg Strings)
$(P
Wysiwyg "what you see is what you get" quoted strings are enclosed by r" and ".
All characters between
the r" and " are part of the string.
There are no escape sequences inside r" ":
)
---------------
r"hello"
r"c:\root\foo.exe"
r"ab\n" // string is 4 characters,
// 'a', 'b', '\', 'n'
---------------
$(P
An alternate form of wysiwyg strings are enclosed by backquotes,
the ` character. The ` character is not available on some keyboards
and the font rendering of it is sometimes indistinguishable from
the regular ' character. Since, however, the ` is rarely used,
it is useful to delineate strings with " in them.
)
---------------
`hello`
`c:\root\foo.exe`
`ab\n` // string is 4 characters,
// 'a', 'b', '\', 'n'
---------------
$(H4 Double Quoted Strings)
$(P Double quoted strings are enclosed by "". Escape sequences can be
embedded into them with the typical $(D \) notation.)
---------------
"hello"
"c:\\root\\foo.exe"
"ab\n" // string is 3 characters,
// 'a', 'b', and a linefeed
"ab
" // string is 3 characters,
// 'a', 'b', and a linefeed
---------------
$(H4 Hex Strings)
$(P Hex strings allow string literals to be created using hex data.
The hex data need not form valid UTF characters.
)
--------------
x"0A" // same as "\x0A"
x"00 FBCD 32FD 0A" // same as
// "\x00\xFB\xCD\x32\xFD\x0A"
--------------
Whitespace and newlines are ignored, so the hex data can be
easily formatted.
The number of hex characters must be a multiple of 2.
$(P )
Adjacent strings are concatenated with the ~ operator, or by simple
juxtaposition:
--------------
"hello " ~ "world" ~ "\n" // forms the string
// 'h','e','l','l','o',' ',
// 'w','o','r','l','d',linefeed
--------------
The following are all equivalent:
-----------------
"ab" "c"
r"ab" r"c"
r"a" "bc"
"a" ~ "b" ~ "c"
-----------------
$(P The optional $(I StringPostfix) character gives a specific type
to the string, rather than it being inferred from the context.
This is useful when the type cannot be unambiguously inferred,
such as when overloading based on string type. The types corresponding
to the postfix characters are:
)
$(TABLE2 String Literal Postfix Characters,
$(THEAD Postfix, Type, Aka)
$(TROW $(B c), $(D immutable(char)[]), $(D string))
$(TROW $(B w), $(D immutable(wchar)[]), $(D wstring))
$(TROW $(B d), $(D immutable(dchar)[]), $(D dstring))
)
---
"hello"c // string
"hello"w // wstring
"hello"d // dstring
---
$(P The string literals are assembled as UTF-8 char arrays,
and the postfix is applied
to convert to wchar or dchar as necessary as a final step.)
$(P String literals are read only. Writes to string literals
cannot always be detected, but cause undefined behavior.)
$(H4 Delimited Strings)
$(P Delimited strings use various forms of delimiters.
The delimiter, whether a character or identifer,
must immediately follow the " without any intervening whitespace.
The terminating delimiter must immediately precede the closing "
without any intervening whitespace.
A $(I nesting delimiter) nests, and is one of the
following characters:
)
$(TABLE2 Nesting Delimiters,
$(THEAD Delimiter, Matching Delimiter)
$(TROW $(D [), $(D ]))
$(TROW $(LPAREN), $(RPAREN))
$(TROW $(D <), $(D >))
$(TROW $(CODE_LCURL), $(CODE_RCURL))
)
---
q"(foo(xxx))" // "foo(xxx)"
q"[foo{]" // "foo{"
---
$(P If the delimiter is an identifier, the identifier must
be immediately followed by a newline, and the matching
delimiter is the same identifier starting at the beginning
of the line:
)
---
writefln(q"EOS
This
is a multi-line
heredoc string
EOS"
);
---
$(P The newline following the opening identifier is not part
of the string, but the last newline before the closing
identifier is part of the string. The closing identifier
must be placed on its own line at the leftmost column.
)
$(P Otherwise, the matching delimiter is the same as
the delimiter character:)
---
q"/foo]/" // "foo]"
// q"/abc/def/" // error
---
$(H4 Token Strings)
$(P Token strings open with the characters $(D q)$(CODE_LCURL) and close with
the token $(CODE_RCURL). In between must be valid D tokens.
The $(CODE_LCURL) and $(CODE_RCURL) tokens nest.
The string is formed of all the characters between the opening
and closing of the token string, including comments.
)
---
q{foo} // "foo"
q{/*}*/ } // "/*}*/ "
q{ foo(q{hello}); } // " foo(q{hello}); "
q{ __TIME__ } // " __TIME__ "
// i.e. it is not replaced with the time
// q{ __EOF__ } // error
// __EOF__ is not a token, it's end of file
---
$(H3 $(LNAME2 characterliteral, Character Literals))
$(GRAMMAR
$(GNAME CharacterLiteral):
$(B ') $(GLINK SingleQuotedCharacter) $(B ')
$(GNAME SingleQuotedCharacter):
$(GLINK Character)
$(GLINK EscapeSequence)
)
Character literals are a single character or escape sequence
enclosed by single quotes, $(D ' ').
$(H3 $(LNAME2 integerliteral, Integer Literals))
$(GRAMMAR
$(GNAME IntegerLiteral):
$(GLINK Integer)
$(GLINK Integer) $(I IntegerSuffix)
$(GNAME Integer):
$(GLINK DecimalInteger)
$(GLINK BinaryInteger)
$(GLINK HexadecimalInteger)
$(GNAME IntegerSuffix):
$(B L)
$(B u)
$(B U)
$(B Lu)
$(B LU)
$(B uL)
$(B UL)
$(GNAME DecimalInteger):
$(B 0)
$(GLINK NonZeroDigit)
$(GLINK NonZeroDigit) $(I DecimalDigitsUS)
$(GNAME BinaryInteger):
$(GLINK BinPrefix) $(GLINK BinaryDigits)
$(GNAME BinPrefix):
$(B 0b)
$(B 0B)
$(GNAME HexadecimalInteger):
$(GLINK HexPrefix) $(GLINK HexDigitsNoSingleUS)
$(GNAME NonZeroDigit):
$(B 1)
$(B 2)
$(B 3)
$(B 4)
$(B 5)
$(B 6)
$(B 7)
$(B 8)
$(B 9)
$(GNAME DecimalDigits):
$(GLINK DecimalDigit)
$(GLINK DecimalDigit) $(I DecimalDigits)
$(GNAME DecimalDigitsUS):
$(GLINK DecimalDigitUS)
$(GLINK DecimalDigitUS) $(I DecimalDigitsUS)
$(GNAME DecimalDigitsNoSingleUS):
$(GLINK DecimalDigit)
$(GLINK DecimalDigit) $(GLINK DecimalDigitsUS)
$(GLINK DecimalDigitsUS) $(GLINK DecimalDigit)
$(GNAME DecimalDigitsNoStartingUS):
$(GLINK DecimalDigit)
$(GLINK DecimalDigit) $(GLINK DecimalDigitsUS)
$(GNAME DecimalDigit):
$(B 0)
$(GLINK NonZeroDigit)
$(GNAME DecimalDigitUS):
$(GLINK DecimalDigit)
$(D _)
$(GNAME BinaryDigitsUS):
$(GLINK BinaryDigitUS)
$(GLINK BinaryDigitUS) $(I BinaryDigitsUS)
$(GNAME BinaryDigit):
$(B 0)
$(B 1)
$(GNAME BinaryDigitUS):
$(GLINK BinaryDigit)
$(D _)
$(GNAME OctalDigits):
$(GLINK OctalDigit)
$(GLINK OctalDigit) $(I OctalDigits)
$(GNAME OctalDigitsUS):
$(GLINK OctalDigitUS)
$(GLINK OctalDigitUS) $(I OctalDigitsUS)
$(GNAME OctalDigit):
$(B 0)
$(B 1)
$(B 2)
$(B 3)
$(B 4)
$(B 5)
$(B 6)
$(B 7)
$(GNAME OctalDigitUS):
$(GLINK OctalDigit)
$(D _)
$(GNAME HexDigits):
$(GLINK HexDigit)
$(GLINK HexDigit) $(I HexDigits)
$(GNAME HexDigitsUS):
$(GLINK HexDigitUS)
$(GLINK HexDigitUS) $(I HexDigitsUS)
$(GNAME HexDigitsNoSingleUS):
$(GLINK HexDigit)
$(GLINK HexDigit) $(GLINK HexDigitsUS)
$(GLINK HexDigitsUS) $(GLINK HexDigit)
$(GNAME HexDigit):
$(GLINK DecimalDigit)
$(GLINK HexLetter)
$(GNAME HexLetter):
$(B a)
$(B b)
$(B c)
$(B d)
$(B e)
$(B f)
$(B A)
$(B B)
$(B C)
$(B D)
$(B E)
$(B F)
$(D _)
)
$(P Integers can be specified in decimal, binary, octal, or hexadecimal.)
$(P Decimal integers are a sequence of decimal digits.)
$(P $(LNAME2 binary-literals, Binary integers) are a sequence of binary digits preceded
by a $(SINGLEQUOTE 0b).
)
$(P C-style octal integer notation was deemed too easy to mix up with decimal notation.
The above is only fully supported in string literals.
D still supports octal integer literals interpreted at compile time through the $(FULL_XREF conv, octal)
template, as in $(D octal!167).)
$(P Hexadecimal integers are a sequence of hexadecimal digits preceded
by a $(SINGLEQUOTE 0x).
)
$(P Integers can have embedded $(SINGLEQUOTE $(UNDERSCORE)) characters, which are ignored.
The embedded $(SINGLEQUOTE $(UNDERSCORE)) are useful for formatting long literals, such
as using them as a thousands separator:
)
-------------
123_456 // 123456
1_2_3_4_5_6_ // 123456
-------------
$(P Integers can be immediately followed by one $(SINGLEQUOTE L) or one of
$(SINGLEQUOTE u) or $(SINGLEQUOTE U) or both.
Note that there is no $(SINGLEQUOTE l) suffix.
)
$(P The type of the integer is resolved as follows:)
$(TABLE2 Decimal Literal Types,
$(THEAD Literal, Type)
$(TROW_EXPLANATORY $(I Usual decimal notation))
$(TROW $(D 0 .. 2_147_483_647), $(D int))
$(TROW $(D 2_147_483_648 .. 9_223_372_036_854_775_807), $(D long))
$(MIDRULE)
$(TROW_EXPLANATORY $(I Explicit suffixes))
$(TROW $(D 0L .. 9_223_372_036_854_775_807L), $(D long))
$(TROW $(D 0U .. 4_294_967_296U), $(D uint))
$(TROW $(D 4_294_967_296U .. 18_446_744_073_709_551_615U), $(D
ulong))
$(TROW $(D 0UL .. 18_446_744_073_709_551_615UL), $(D ulong))
$(MIDRULE)
$(TROW_EXPLANATORY $(I Hexadecimal notation))
$(TROW $(D 0x0 .. 0x7FFF_FFFF), $(D int))
$(TROW $(D 0x8000_0000 .. 0xFFFF_FFFF), $(D uint))
$(TROW $(D 0x1_0000_0000 .. 0x7FFF_FFFF_FFFF_FFFF), $(D long))
$(TROW $(D 0x8000_0000_0000_0000 .. 0xFFFF_FFFF_FFFF_FFFF), $(D
ulong))
$(MIDRULE)
$(TROW_EXPLANATORY $(I Hexadecimal notation with explicit suffixes))
$(TROW $(D 0x0L .. 0x7FFF_FFFF_FFFF_FFFFL), $(D long))
$(TROW $(D 0x8000_0000_0000_0000L .. 0xFFFF_FFFF_FFFF_FFFFL), $(D
ulong))
$(TROW $(D 0x0U .. 0xFFFF_FFFFU), $(D uint))
$(TROW $(D 0x1_0000_0000U .. 0xFFFF_FFFF_FFFF_FFFFU), $(D
ulong))
$(TROW $(D 0x0UL .. 0xFFFF_FFFF_FFFF_FFFFUL), $(D ulong))
)
$(H3 $(LNAME2 floatliteral, Floating Point Literals))
$(GRAMMAR
$(GNAME FloatLiteral):
$(GLINK Float)
$(GLINK Float) $(GLINK Suffix)
$(GLINK Integer) $(GLINK ImaginarySuffix)
$(GLINK Integer) $(GLINK FloatSuffix) $(GLINK ImaginarySuffix)
$(GLINK Integer) $(GLINK RealSuffix) $(GLINK ImaginarySuffix)
$(GNAME Float):
$(GLINK DecimalFloat)
$(GLINK HexFloat)
$(GNAME DecimalFloat):
$(GLINK LeadingDecimal) $(B .)
$(GLINK LeadingDecimal) $(B .) $(GLINK DecimalDigits)
$(GLINK DecimalDigits) $(B .) $(GLINK DecimalDigitsNoSingleUS) $(GLINK DecimalExponent)
$(B .) $(GLINK DecimalInteger)
$(B .) $(GLINK DecimalInteger) $(GLINK DecimalExponent)
$(GLINK LeadingDecimal) $(GLINK DecimalExponent)
$(GNAME DecimalExponent)
$(GLINK DecimalExponentStart) $(GLINK DecimalDigitsNoSingleUS)
$(GNAME DecimalExponentStart)
$(B e)
$(B E)
$(B e+)
$(B E+)
$(B e-)
$(B E-)
$(GNAME HexFloat):
$(GLINK HexPrefix) $(GLINK HexDigitsNoSingleUS) $(B .) $(GLINK HexDigitsNoSingleUS) $(GLINK HexExponent)
$(GLINK HexPrefix) $(B .) $(GLINK HexDigitsNoSingleUS) $(GLINK HexExponent)
$(GLINK HexPrefix) $(GLINK HexDigitsNoSingleUS) $(GLINK HexExponent)
$(GNAME HexPrefix):
$(B 0x)
$(B 0X)
$(GNAME HexExponent):
$(GLINK HexExponentStart) $(GLINK DecimalDigitsNoSingleUS)
$(GNAME HexExponentStart):
$(B p)
$(B P)
$(B p+)
$(B P+)
$(B p-)
$(B P-)
$(GNAME Suffix):
$(GLINK FloatSuffix)
$(GLINK RealSuffix)
$(GLINK ImaginarySuffix)
$(GLINK FloatSuffix) $(GLINK ImaginarySuffix)
$(GLINK RealSuffix) $(GLINK ImaginarySuffix)
$(GNAME FloatSuffix):
$(B f)
$(B F)
$(GNAME RealSuffix):
$(B L)
$(GNAME ImaginarySuffix):
$(B i)
$(GNAME LeadingDecimal):
$(GLINK DecimalInteger)
$(B 0) $(GLINK DecimalDigitsNoSingleUS)
)
$(P Floats can be in decimal or hexadecimal format.)
$(P Hexadecimal floats are preceded with a $(B 0x) and the
exponent is a $(B p)
or $(B P) followed by a decimal number serving as the exponent
of 2.
)
$(P Floating literals can have embedded $(SINGLEQUOTE $(UNDERSCORE)) characters, which are ignored.
The embedded $(SINGLEQUOTE $(UNDERSCORE)) are useful for formatting long literals to
make them more readable, such
as using them as a thousands separator:
)
---------
123_456.567_8 // 123456.5678
1_2_3_4_5_6_._5_6_7_8 // 123456.5678
1_2_3_4_5_6_._5e-6_ // 123456.5e-6
---------
$(P Floating literals with no suffix are of type double.
Floats can be followed by one $(B f), $(B F),
or $(B L) suffix.
The $(B f) or $(B F) suffix means it is a
float, and $(B L) means it is a real.
)
$(P If a floating literal is followed by $(B i), then it is an
$(I ireal) (imaginary) type.
)
$(P Examples:)
---------
0x1.FFFFFFFFFFFFFp1023 // double.max
0x1p-52 // double.epsilon
1.175494351e-38F // float.min
6.3i // idouble 6.3
6.3fi // ifloat 6.3
6.3Li // ireal 6.3
---------
$(P It is an error if the literal exceeds the range of the type.
It is not an error if the literal is rounded to fit into
the significant digits of the type.
)
$(P Complex literals are not tokens, but are assembled from
real and imaginary expressions during semantic analysis:
)
---------
4.5 + 6.2i // complex number (phased out)
---------
$(H3 Keywords)
Keywords are reserved identifiers.
See Also: Globally Defined $(GLINK Symbols).
$(GRAMMAR
$(GNAME Keyword):
$(MULTICOLS 4,
$(D $(XLINK2 attribute.html#abstract, abstract))
$(D $(XLINK2 declaration.html#alias, alias))
$(D $(XLINK2 iasm.html#IntegerExpression, align))
$(D $(XLINK2 statement.html#AsmStatement, asm))
$(D $(XLINK2 expression.html#AssertExpression, assert))
$(D $(XLINK2 attribute.html#auto, auto))
$(D $(XLINK2 function.html#BodyStatement, body))
$(D $(XLINK2 type.html, bool))
$(D $(XLINK2 statement.html#BreakStatement, break))
$(D $(XLINK2 type.html, byte))
$(D $(XLINK2 statement.html#SwitchStatement, case))
$(D $(XLINK2 expression.html#CastExpression, cast))
$(D $(XLINK2 statement.html#TryStatement, catch))
$(D $(XLINK2 type.html, cdouble))
$(D $(XLINK2 type.html, cent))
$(D $(XLINK2 type.html, cfloat))
$(D $(XLINK2 type.html, char))
$(D $(XLINK2 class.html, class))
$(D $(XLINK2 attribute.html#const, const))
$(D $(XLINK2 statement.html#ContinueStatement, continue))
$(D $(XLINK2 type.html, creal))
$(D $(XLINK2 type.html, dchar))
$(D $(XLINK2 version.html#debug, debug))
$(D $(XLINK2 statement.html#SwitchStatement, default))
$(D $(XLINK2 type.html#delegates, delegate))
$(D $(XLINK2 expression.html#DeleteExpression, delete)) ($(XLINK2 deprecate.html#delete, deprecated))
$(D $(XLINK2 attribute.html#deprecated, deprecated))
$(D $(XLINK2 statement.html#DoStatement, do))
$(D $(XLINK2 type.html, double))
$(D $(XLINK2 statement.html#IfStatement, else))
$(D $(XLINK2 enum.html, enum))
$(D $(XLINK2 attribute.html#ProtectionAttribute, export))
$(D $(XLINK2 attribute.html#linkage, extern))
$(D $(XLINK2 type.html, false)) <!-- could use an anchor for this -->
$(D $(XLINK2 class.html#final, final))
$(D $(XLINK2 statement.html#TryStatement, finally))
$(D $(XLINK2 type.html, float))
$(D $(XLINK2 statement.html#ForStatement, for))
$(D $(XLINK2 statement.html#ForeachStatement, foreach))
$(D $(XLINK2 statement.html#ForeachStatement, foreach_reverse))
$(D $(XLINK2 expression.html#FunctionLiteral, function))
$(D $(XLINK2 statement.html#GotoStatement, goto))
$(D $(XLINK2 type.html, idouble))
$(D $(XLINK2 statement.html#IfStatement, if))
$(D $(XLINK2 type.html, ifloat))
$(D $(XLINK2 attribute.html#immutable, immutable))
$(D $(XLINK2 expression.html#ImportExpression, import)) <!-- alt. module.html#ImportDeclaration -->
$(D $(XLINK2 expression.html#InExpression, in)) <!-- might be better: function.html#InStatement; also, function.html#overload-sets table -->
$(D $(XLINK2 function.html#inout-functions, inout)) <!-- alt. attribute.html#inout -->
$(D $(XLINK2 type.html, int))
$(D $(XLINK2 interface.html, interface))
$(D $(XLINK2 dbc.html, invariant)) <!-- could use an anchor to the invariant section -->
$(D $(XLINK2 type.html, ireal))
$(D $(XLINK2 expression.html#IsExpression, is))
$(D $(XLINK2 function.html#overload-sets, lazy)) (See also: $(XLINK2 lazy-evaluation.html, Lazy Evaluation of Function Arguments))
$(D $(XLINK2 type.html, long))
$(D macro) (Reserved; unused)
$(D $(XLINK2 expression.html#MixinExpression, mixin)) <!-- Template Mixins are easy to find. -->
$(D $(XLINK2 module.html#ModuleDeclaration, module))
$(D $(XLINK2 expression.html#NewExpression, new))
$(D $(XLINK2 function.html#nothrow-functions, nothrow))
$(D $(XLINK2 expression.html#null, null))
$(D $(XLINK2 function.html#OutStatement, out))
$(D $(XLINK2 attribute.html#override, override))
$(D $(XLINK2 attribute.html#ProtectionAttribute, package))
$(D $(XLINK2 pragma.html, pragma))
$(D $(XLINK2 attribute.html#ProtectionAttribute, private))
$(D $(XLINK2 attribute.html#ProtectionAttribute, protected))
$(D $(XLINK2 attribute.html#ProtectionAttribute, public))
$(D $(XLINK2 function.html#pure-functions, pure))
$(D $(XLINK2 type.html, real))
$(D $(XLINK2 function.html#ref-functions, ref))
$(D $(XLINK2 statement.html#ReturnStatement, return))
$(D $(XLINK2 statement.html#ScopeGuardStatement, scope))