Debian Codegraph @

gcc/d/dmd/tokens.d

raw
1 /**
2 * Defines lexical tokens.
3 *
4 * Specification: $(LINK2 https://dlang.org/spec/lex.html#tokens, Tokens)
5 *
6 * Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
7 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
8 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/tokens.d, _tokens.d)
10 * Documentation: https://dlang.org/phobos/dmd_tokens.html
11 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/tokens.d
12 */
13
14 module dmd.tokens;
15
16 import core.stdc.ctype;
17 import core.stdc.stdio;
18 import core.stdc.string;
19 import dmd.identifier;
20 import dmd.location;
21 import dmd.root.ctfloat;
22 import dmd.common.outbuffer;
23 import dmd.root.rmem;
24 import dmd.root.utf;
25
26 enum TOK : ubyte
27 {
28 reserved,
29
30 // if this list changes, update
31 // tokens.h, ../tests/cxxfrontend.cc and ../../test/unit/lexer/location_offset.d to match
32
33 // Other
34 leftParenthesis,
35 rightParenthesis,
36 leftBracket,
37 rightBracket,
38 leftCurly,
39 rightCurly,
40 colon,
41 semicolon,
42 dotDotDot,
43 endOfFile,
44 cast_,
45 null_,
46 assert_,
47 true_,
48 false_,
49 throw_,
50 new_,
51 variable,
52 slice,
53 version_,
54 module_,
55 dollar,
56 template_,
57 typeof_,
58 pragma_,
59 typeid_,
60 comment,
61
62 // Operators
63 lessThan,
64 greaterThan,
65 lessOrEqual,
66 greaterOrEqual,
67 equal,
68 notEqual,
69 identity,
70 notIdentity,
71 is_,
72
73 leftShift,
74 rightShift,
75 leftShiftAssign,
76 rightShiftAssign,
77 unsignedRightShift,
78 unsignedRightShiftAssign,
79 concatenateAssign, // ~=
80 add,
81 min,
82 addAssign,
83 minAssign,
84 mul,
85 div,
86 mod,
87 mulAssign,
88 divAssign,
89 modAssign,
90 and,
91 or,
92 xor,
93 andAssign,
94 orAssign,
95 xorAssign,
96 assign,
97 not,
98 tilde,
99 plusPlus,
100 minusMinus,
101 dot,
102 comma,
103 question,
104 andAnd,
105 orOr,
106
107 // Numeric literals
108 int32Literal,
109 uns32Literal,
110 int64Literal,
111 uns64Literal,
112 int128Literal,
113 uns128Literal,
114 float32Literal,
115 float64Literal,
116 float80Literal,
117 imaginary32Literal,
118 imaginary64Literal,
119 imaginary80Literal,
120
121 // Char constants
122 charLiteral,
123 wcharLiteral,
124 dcharLiteral,
125
126 // Leaf operators
127 identifier,
128 string_,
129 interpolated,
130 hexadecimalString,
131 this_,
132 super_,
133 error,
134
135 // Basic types
136 void_,
137 int8,
138 uns8,
139 int16,
140 uns16,
141 int32,
142 uns32,
143 int64,
144 uns64,
145 int128,
146 uns128,
147 float32,
148 float64,
149 float80,
150 imaginary32,
151 imaginary64,
152 imaginary80,
153 complex32,
154 complex64,
155 complex80,
156 char_,
157 wchar_,
158 dchar_,
159 bool_,
160
161 // Aggregates
162 struct_,
163 class_,
164 interface_,
165 union_,
166 enum_,
167 import_,
168 alias_,
169 override_,
170 delegate_,
171 function_,
172 mixin_,
173 align_,
174 extern_,
175 private_,
176 protected_,
177 public_,
178 export_,
179 static_,
180 final_,
181 const_,
182 abstract_,
183 debug_,
184 deprecated_,
185 in_,
186 out_,
187 inout_,
188 lazy_,
189 auto_,
190 package_,
191 immutable_,
192
193 // Statements
194 if_,
195 else_,
196 while_,
197 for_,
198 do_,
199 switch_,
200 case_,
201 default_,
202 break_,
203 continue_,
204 with_,
205 synchronized_,
206 return_,
207 goto_,
208 try_,
209 catch_,
210 finally_,
211 asm_,
212 foreach_,
213 foreach_reverse_,
214 scope_,
215 onScopeExit,
216 onScopeFailure,
217 onScopeSuccess,
218
219 // Contracts
220 invariant_,
221
222 // Testing
223 unittest_,
224
225 // Added after 1.0
226 argumentTypes,
227 ref_,
228 macro_,
229
230 parameters,
231 traits,
232 pure_,
233 nothrow_,
234 gshared,
235 line,
236 file,
237 fileFullPath,
238 moduleString, // __MODULE__
239 functionString, // __FUNCTION__
240 prettyFunction, // __PRETTY_FUNCTION__
241 shared_,
242 at,
243 pow,
244 powAssign,
245 goesTo,
246 vector,
247 pound,
248
249 arrow, // ->
250 colonColon, // ::
251 wchar_tLiteral,
252 endOfLine, // \n, \r, \u2028, \u2029
253 whitespace,
254 rvalue,
255
256 // C only keywords
257 inline,
258 register,
259 restrict,
260 signed,
261 sizeof_,
262 typedef_,
263 unsigned,
264 volatile,
265 _Alignas,
266 _Alignof,
267 _Atomic,
268 _Bool,
269 _Complex,
270 _Generic,
271 _Imaginary,
272 _Noreturn,
273 _Static_assert,
274 _Thread_local,
275
276 // C only extended keywords
277 _assert,
278 _import,
279 __cdecl,
280 __declspec,
281 __stdcall,
282 __thread,
283 __pragma,
284 __int128,
285 __attribute__,
286 }
287
288 /// Expression nodes
289 enum EXP : ubyte
290 {
291 reserved,
292
293 // Other
294 negate,
295 cast_,
296 null_,
297 assert_,
298 array,
299 call,
300 address,
301 type,
302 throw_,
303 new_,
304 delete_,
305 star,
306 symbolOffset,
307 variable,
308 dotVariable,
309 dotIdentifier,
310 dotTemplateInstance,
311 dotType,
312 slice,
313 arrayLength,
314 dollar,
315 template_,
316 dotTemplateDeclaration,
317 declaration,
318 dSymbol,
319 typeid_,
320 uadd,
321 remove,
322 newAnonymousClass,
323 arrayLiteral,
324 assocArrayLiteral,
325 structLiteral,
326 classReference,
327 thrownException,
328 delegatePointer,
329 delegateFunctionPointer,
330
331 // Operators
332 lessThan,
333 greaterThan,
334 lessOrEqual,
335 greaterOrEqual,
336 equal,
337 notEqual,
338 identity,
339 notIdentity,
340 index,
341 is_,
342
343 leftShift,
344 rightShift,
345 leftShiftAssign,
346 rightShiftAssign,
347 unsignedRightShift,
348 unsignedRightShiftAssign,
349 concatenate,
350 concatenateAssign, // ~=
351 concatenateElemAssign,
352 concatenateDcharAssign,
353 add,
354 min,
355 addAssign,
356 minAssign,
357 mul,
358 div,
359 mod,
360 mulAssign,
361 divAssign,
362 modAssign,
363 and,
364 or,
365 xor,
366 andAssign,
367 orAssign,
368 xorAssign,
369 assign,
370 not,
371 tilde,
372 plusPlus,
373 minusMinus,
374 construct,
375 blit,
376 dot,
377 comma,
378 question,
379 andAnd,
380 orOr,
381 prePlusPlus,
382 preMinusMinus,
383
384 // Leaf operators
385 identifier,
386 string_,
387 interpolated,
388 this_,
389 super_,
390 halt,
391 tuple,
392 error,
393
394 // Basic types
395 void_,
396 int64,
397 float64,
398 complex80,
399 import_,
400 delegate_,
401 function_,
402 mixin_,
403 in_,
404 break_,
405 continue_,
406 goto_,
407 scope_,
408
409 traits,
410 overloadSet,
411 line,
412 file,
413 fileFullPath,
414 moduleString, // __MODULE__
415 functionString, // __FUNCTION__
416 prettyFunction, // __PRETTY_FUNCTION__
417 pow,
418 powAssign,
419 vector,
420
421 voidExpression,
422 cantExpression,
423 showCtfeContext,
424 objcClassReference,
425 vectorArray,
426 compoundLiteral, // ( type-name ) { initializer-list }
427 _Generic,
428 interval,
429
430 loweredAssignExp,
431 rvalue,
432 }
433
434 enum FirstCKeyword = TOK.inline;
435
436 // Assert that all token enum members have consecutive values and
437 // that none of them overlap
438 static assert(() {
439 foreach (idx, enumName; __traits(allMembers, TOK))
440 {
441 static if (idx != __traits(getMember, TOK, enumName))
442 {
443 pragma(msg, "Error: Expected TOK.", enumName, " to be ", idx, " but is ", __traits(getMember, TOK, enumName));
444 static assert(0);
445 }
446 }
447 return true;
448 }());
449
450 /****************************************
451 */
452
453 private immutable TOK[] keywords =
454 [
455 TOK.this_,
456 TOK.super_,
457 TOK.assert_,
458 TOK.null_,
459 TOK.true_,
460 TOK.false_,
461 TOK.cast_,
462 TOK.new_,
463 TOK.throw_,
464 TOK.module_,
465 TOK.pragma_,
466 TOK.typeof_,
467 TOK.typeid_,
468 TOK.template_,
469 TOK.void_,
470 TOK.int8,
471 TOK.uns8,
472 TOK.int16,
473 TOK.uns16,
474 TOK.int32,
475 TOK.uns32,
476 TOK.int64,
477 TOK.uns64,
478 TOK.int128,
479 TOK.uns128,
480 TOK.float32,
481 TOK.float64,
482 TOK.float80,
483 TOK.bool_,
484 TOK.char_,
485 TOK.wchar_,
486 TOK.dchar_,
487 TOK.imaginary32,
488 TOK.imaginary64,
489 TOK.imaginary80,
490 TOK.complex32,
491 TOK.complex64,
492 TOK.complex80,
493 TOK.delegate_,
494 TOK.function_,
495 TOK.is_,
496 TOK.if_,
497 TOK.else_,
498 TOK.while_,
499 TOK.for_,
500 TOK.do_,
501 TOK.switch_,
502 TOK.case_,
503 TOK.default_,
504 TOK.break_,
505 TOK.continue_,
506 TOK.synchronized_,
507 TOK.return_,
508 TOK.goto_,
509 TOK.try_,
510 TOK.catch_,
511 TOK.finally_,
512 TOK.with_,
513 TOK.asm_,
514 TOK.foreach_,
515 TOK.foreach_reverse_,
516 TOK.scope_,
517 TOK.struct_,
518 TOK.class_,
519 TOK.interface_,
520 TOK.union_,
521 TOK.enum_,
522 TOK.import_,
523 TOK.mixin_,
524 TOK.static_,
525 TOK.final_,
526 TOK.const_,
527 TOK.alias_,
528 TOK.override_,
529 TOK.abstract_,
530 TOK.debug_,
531 TOK.deprecated_,
532 TOK.in_,
533 TOK.out_,
534 TOK.inout_,
535 TOK.lazy_,
536 TOK.auto_,
537 TOK.align_,
538 TOK.extern_,
539 TOK.private_,
540 TOK.package_,
541 TOK.protected_,
542 TOK.public_,
543 TOK.export_,
544 TOK.invariant_,
545 TOK.unittest_,
546 TOK.version_,
547 TOK.argumentTypes,
548 TOK.parameters,
549 TOK.ref_,
550 TOK.macro_,
551 TOK.pure_,
552 TOK.nothrow_,
553 TOK.gshared,
554 TOK.traits,
555 TOK.vector,
556 TOK.file,
557 TOK.fileFullPath,
558 TOK.line,
559 TOK.moduleString,
560 TOK.functionString,
561 TOK.prettyFunction,
562 TOK.shared_,
563 TOK.immutable_,
564 TOK.rvalue,
565
566 // C only keywords
567 TOK.inline,
568 TOK.register,
569 TOK.restrict,
570 TOK.signed,
571 TOK.sizeof_,
572 TOK.typedef_,
573 TOK.unsigned,
574 TOK.volatile,
575 TOK._Alignas,
576 TOK._Alignof,
577 TOK._Atomic,
578 TOK._Bool,
579 TOK._Complex,
580 TOK._Generic,
581 TOK._Imaginary,
582 TOK._Noreturn,
583 TOK._Static_assert,
584 TOK._Thread_local,
585
586 // C only extended keywords
587 TOK._assert,
588 TOK._import,
589 TOK.__cdecl,
590 TOK.__declspec,
591 TOK.__stdcall,
592 TOK.__thread,
593 TOK.__pragma,
594 TOK.__int128,
595 TOK.__attribute__,
596 ];
597
598 // Initialize the identifier pool
599 shared static this() nothrow
600 {
601 Identifier.initTable();
602 foreach (kw; keywords)
603 {
604 //printf("keyword[%d] = '%s'\n",kw, Token.tochars[kw].ptr);
605 Identifier.idPool(Token.tochars[kw], kw);
606 }
607 }
608
609 /************************************
610 * This is used to pick the C keywords out of the tokens.
611 * If it's not a C keyword, then it's an identifier.
612 */
613 static immutable TOK[TOK.max + 1] Ckeywords =
614 () {
615 with (TOK)
616 {
617 TOK[TOK.max + 1] tab = identifier; // default to identifier
618 enum Ckwds = [ auto_, break_, case_, char_, const_, continue_, default_, do_, float64, else_,
619 enum_, extern_, float32, for_, goto_, if_, inline, int32, int64, register,
620 restrict, return_, int16, signed, sizeof_, static_, struct_, switch_, typedef_,
621 union_, unsigned, void_, volatile, while_, asm_, typeof_,
622 _Alignas, _Alignof, _Atomic, _Bool, _Complex, _Generic, _Imaginary, _Noreturn,
623 _Static_assert, _Thread_local,
624 _import, __cdecl, __declspec, __stdcall, __thread, __pragma, __int128, __attribute__,
625 _assert ];
626
627 foreach (kw; Ckwds)
628 tab[kw] = cast(TOK) kw;
629
630 return tab;
631 }
632 } ();
633
634 struct InterpolatedSet {
635 // all strings in the parts are zero terminated at length+1
636 string[] parts;
637 }
638
639 /***********************************************************
640 */
641 extern (C++) struct Token
642 {
643 Token* next;
644 Loc loc;
645 const(char)* ptr; // pointer to first character of this token within buffer
646 TOK value;
647 const(char)[] blockComment; // doc comment string prior to this token
648 const(char)[] lineComment; // doc comment for previous token
649
650 union
651 {
652 // Integers
653 long intvalue;
654 ulong unsvalue;
655 // Floats
656 real_t floatvalue;
657
658 struct
659 {
660 union
661 {
662 const(char)* ustring; // UTF8 string
663 InterpolatedSet* interpolatedSet;
664 }
665 uint len;
666 ubyte postfix; // 'c', 'w', 'd'
667 }
668
669 Identifier ident;
670 }
671
672 extern (D) private static immutable string[TOK.max + 1] tochars =
673 [
674 // Keywords
675 TOK.this_: "this",
676 TOK.super_: "super",
677 TOK.assert_: "assert",
678 TOK.null_: "null",
679 TOK.true_: "true",
680 TOK.false_: "false",
681 TOK.cast_: "cast",
682 TOK.new_: "new",
683 TOK.throw_: "throw",
684 TOK.module_: "module",
685 TOK.pragma_: "pragma",
686 TOK.typeof_: "typeof",
687 TOK.typeid_: "typeid",
688 TOK.rvalue: "__rvalue",
689 TOK.template_: "template",
690 TOK.void_: "void",
691 TOK.int8: "byte",
692 TOK.uns8: "ubyte",
693 TOK.int16: "short",
694 TOK.uns16: "ushort",
695 TOK.int32: "int",
696 TOK.uns32: "uint",
697 TOK.int64: "long",
698 TOK.uns64: "ulong",
699 TOK.int128: "cent",
700 TOK.uns128: "ucent",
701 TOK.float32: "float",
702 TOK.float64: "double",
703 TOK.float80: "real",
704 TOK.bool_: "bool",
705 TOK.char_: "char",
706 TOK.wchar_: "wchar",
707 TOK.dchar_: "dchar",
708 TOK.imaginary32: "ifloat",
709 TOK.imaginary64: "idouble",
710 TOK.imaginary80: "ireal",
711 TOK.complex32: "cfloat",
712 TOK.complex64: "cdouble",
713 TOK.complex80: "creal",
714 TOK.delegate_: "delegate",
715 TOK.function_: "function",
716 TOK.is_: "is",
717 TOK.if_: "if",
718 TOK.else_: "else",
719 TOK.while_: "while",
720 TOK.for_: "for",
721 TOK.do_: "do",
722 TOK.switch_: "switch",
723 TOK.case_: "case",
724 TOK.default_: "default",
725 TOK.break_: "break",
726 TOK.continue_: "continue",
727 TOK.synchronized_: "synchronized",
728 TOK.return_: "return",
729 TOK.goto_: "goto",
730 TOK.try_: "try",
731 TOK.catch_: "catch",
732 TOK.finally_: "finally",
733 TOK.with_: "with",
734 TOK.asm_: "asm",
735 TOK.foreach_: "foreach",
736 TOK.foreach_reverse_: "foreach_reverse",
737 TOK.scope_: "scope",
738 TOK.struct_: "struct",
739 TOK.class_: "class",
740 TOK.interface_: "interface",
741 TOK.union_: "union",
742 TOK.enum_: "enum",
743 TOK.import_: "import",
744 TOK.mixin_: "mixin",
745 TOK.static_: "static",
746 TOK.final_: "final",
747 TOK.const_: "const",
748 TOK.alias_: "alias",
749 TOK.override_: "override",
750 TOK.abstract_: "abstract",
751 TOK.debug_: "debug",
752 TOK.deprecated_: "deprecated",
753 TOK.in_: "in",
754 TOK.out_: "out",
755 TOK.inout_: "inout",
756 TOK.lazy_: "lazy",
757 TOK.auto_: "auto",
758 TOK.align_: "align",
759 TOK.extern_: "extern",
760 TOK.private_: "private",
761 TOK.package_: "package",
762 TOK.protected_: "protected",
763 TOK.public_: "public",
764 TOK.export_: "export",
765 TOK.invariant_: "invariant",
766 TOK.unittest_: "unittest",
767 TOK.version_: "version",
768 TOK.argumentTypes: "__argTypes",
769 TOK.parameters: "__parameters",
770 TOK.ref_: "ref",
771 TOK.macro_: "macro",
772 TOK.pure_: "pure",
773 TOK.nothrow_: "nothrow",
774 TOK.gshared: "__gshared",
775 TOK.traits: "__traits",
776 TOK.vector: "__vector",
777 TOK.file: "__FILE__",
778 TOK.fileFullPath: "__FILE_FULL_PATH__",
779 TOK.line: "__LINE__",
780 TOK.moduleString: "__MODULE__",
781 TOK.functionString: "__FUNCTION__",
782 TOK.prettyFunction: "__PRETTY_FUNCTION__",
783 TOK.shared_: "shared",
784 TOK.immutable_: "immutable",
785
786 TOK.endOfFile: "End of File",
787 TOK.leftCurly: "{",
788 TOK.rightCurly: "}",
789 TOK.leftParenthesis: "(",
790 TOK.rightParenthesis: ")",
791 TOK.leftBracket: "[",
792 TOK.rightBracket: "]",
793 TOK.semicolon: ";",
794 TOK.colon: ":",
795 TOK.comma: ",",
796 TOK.dot: ".",
797 TOK.xor: "^",
798 TOK.xorAssign: "^=",
799 TOK.assign: "=",
800 TOK.lessThan: "<",
801 TOK.greaterThan: ">",
802 TOK.lessOrEqual: "<=",
803 TOK.greaterOrEqual: ">=",
804 TOK.equal: "==",
805 TOK.notEqual: "!=",
806 TOK.not: "!",
807 TOK.leftShift: "<<",
808 TOK.rightShift: ">>",
809 TOK.unsignedRightShift: ">>>",
810 TOK.add: "+",
811 TOK.min: "-",
812 TOK.mul: "*",
813 TOK.div: "/",
814 TOK.mod: "%",
815 TOK.slice: "..",
816 TOK.dotDotDot: "...",
817 TOK.and: "&",
818 TOK.andAnd: "&&",
819 TOK.or: "|",
820 TOK.orOr: "||",
821 TOK.tilde: "~",
822 TOK.dollar: "$",
823 TOK.plusPlus: "++",
824 TOK.minusMinus: "--",
825 TOK.question: "?",
826 TOK.variable: "var",
827 TOK.addAssign: "+=",
828 TOK.minAssign: "-=",
829 TOK.mulAssign: "*=",
830 TOK.divAssign: "/=",
831 TOK.modAssign: "%=",
832 TOK.leftShiftAssign: "<<=",
833 TOK.rightShiftAssign: ">>=",
834 TOK.unsignedRightShiftAssign: ">>>=",
835 TOK.andAssign: "&=",
836 TOK.orAssign: "|=",
837 TOK.concatenateAssign: "~=",
838 TOK.identity: "is",
839 TOK.notIdentity: "!is",
840 TOK.identifier: "identifier",
841 TOK.at: "@",
842 TOK.pow: "^^",
843 TOK.powAssign: "^^=",
844 TOK.goesTo: "=>",
845 TOK.pound: "#",
846 TOK.arrow: "->",
847 TOK.colonColon: "::",
848
849 // For debugging
850 TOK.error: "error",
851 TOK.string_: "string",
852 TOK.interpolated: "interpolated string",
853 TOK.onScopeExit: "scope(exit)",
854 TOK.onScopeSuccess: "scope(success)",
855 TOK.onScopeFailure: "scope(failure)",
856
857 // Finish up
858 TOK.reserved: "reserved",
859 TOK.comment: "comment",
860 TOK.int32Literal: "int32v",
861 TOK.uns32Literal: "uns32v",
862 TOK.int64Literal: "int64v",
863 TOK.uns64Literal: "uns64v",
864 TOK.int128Literal: "int128v",
865 TOK.uns128Literal: "uns128v",
866 TOK.float32Literal: "float32v",
867 TOK.float64Literal: "float64v",
868 TOK.float80Literal: "float80v",
869 TOK.imaginary32Literal: "imaginary32v",
870 TOK.imaginary64Literal: "imaginary64v",
871 TOK.imaginary80Literal: "imaginary80v",
872 TOK.charLiteral: "charv",
873 TOK.wcharLiteral: "wcharv",
874 TOK.dcharLiteral: "dcharv",
875 TOK.wchar_tLiteral: "wchar_tv",
876 TOK.hexadecimalString: "xstring",
877 TOK.endOfLine: "\\n",
878 TOK.whitespace: "whitespace",
879
880 // C only keywords
881 TOK.inline : "inline",
882 TOK.register : "register",
883 TOK.restrict : "restrict",
884 TOK.signed : "signed",
885 TOK.sizeof_ : "sizeof",
886 TOK.typedef_ : "typedef",
887 TOK.unsigned : "unsigned",
888 TOK.volatile : "volatile",
889 TOK._Alignas : "_Alignas",
890 TOK._Alignof : "_Alignof",
891 TOK._Atomic : "_Atomic",
892 TOK._Bool : "_Bool",
893 TOK._Complex : "_Complex",
894 TOK._Generic : "_Generic",
895 TOK._Imaginary: "_Imaginary",
896 TOK._Noreturn : "_Noreturn",
897 TOK._Static_assert : "_Static_assert",
898 TOK._Thread_local : "_Thread_local",
899
900 // C only extended keywords
901 TOK._assert : "__check",
902 TOK._import : "__import",
903 TOK.__cdecl : "__cdecl",
904 TOK.__declspec : "__declspec",
905 TOK.__stdcall : "__stdcall",
906 TOK.__thread : "__thread",
907 TOK.__pragma : "__pragma",
908 TOK.__int128 : "__int128",
909 TOK.__attribute__ : "__attribute__",
910 ];
911
912 static assert(() {
913 foreach (s; tochars)
914 assert(s.length);
915 return true;
916 }());
917
918 nothrow:
919
920 extern (D) int isKeyword() pure const @safe @nogc
921 {
922 foreach (kw; keywords)
923 {
924 if (kw == value)
925 return 1;
926 }
927 return 0;
928 }
929
930 extern(D) void appendInterpolatedPart(const ref OutBuffer buf)
931 {
932 appendInterpolatedPart(cast(const(char)*)buf[].ptr, buf.length);
933 }
934
935 extern(D) void appendInterpolatedPart(const(char)[] str)
936 {
937 appendInterpolatedPart(str.ptr, str.length);
938 }
939
940 extern(D) void appendInterpolatedPart(const(char)* ptr, size_t length)
941 {
942 assert(value == TOK.interpolated);
943 if (interpolatedSet is null)
944 interpolatedSet = new InterpolatedSet;
945
946 auto s = cast(char*)mem.xmalloc_noscan(length + 1);
947 memcpy(s, ptr, length);
948 s[length] = 0;
949
950 interpolatedSet.parts ~= cast(string) s[0 .. length];
951 }
952
953 /****
954 * Set to contents of ptr[0..length]
955 * Params:
956 * ptr = pointer to string
957 * length = length of string
958 */
959 void setString(const(char)* ptr, size_t length)
960 {
961 value = TOK.string_;
962 auto s = cast(char*)mem.xmalloc_noscan(length + 1);
963 memcpy(s, ptr, length);
964 s[length] = 0;
965 ustring = s;
966 len = cast(uint)length;
967 postfix = 0;
968 }
969
970 /****
971 * Set to contents of buf
972 * Params:
973 * buf = string (not zero terminated)
974 */
975 void setString(const ref OutBuffer buf)
976 {
977 setString(cast(const(char)*)buf[].ptr, buf.length);
978 }
979
980 /****
981 * Set to empty string
982 */
983 void setString()
984 {
985 value = TOK.string_;
986 ustring = "";
987 len = 0;
988 postfix = 0;
989 }
990
991 extern (C++) const(char)* toChars() const
992 {
993 return toString().ptr;
994 }
995
996 /*********************************
997 * Returns:
998 * a zero-terminated string representation of the token,
999 * sometimes reusing a static buffer, sometimes leaking memory
1000 */
1001 extern (D) const(char)[] toString() const
1002 {
1003 const bufflen = 3 + 3 * floatvalue.sizeof + 1;
1004 __gshared char[bufflen + 2] buffer; // extra 2 for suffixes
1005 char* p = &buffer[0];
1006 switch (value)
1007 {
1008 case TOK.int32Literal:
1009 const length = snprintf(p, bufflen, "%d", cast(int)intvalue);
1010 return p[0 .. length];
1011
1012 case TOK.uns32Literal:
1013 case TOK.wchar_tLiteral:
1014 const length = snprintf(p, bufflen, "%uU", cast(uint)unsvalue);
1015 return p[0 .. length];
1016
1017 case TOK.wcharLiteral:
1018 case TOK.dcharLiteral:
1019 case TOK.charLiteral:
1020 OutBuffer buf;
1021 buf.writeSingleCharLiteral(cast(dchar) intvalue);
1022 return buf.extractSlice(true);
1023
1024 case TOK.int64Literal:
1025 const length = snprintf(p, bufflen, "%lldL", cast(long)intvalue);
1026 return p[0 .. length];
1027
1028 case TOK.uns64Literal:
1029 const length = snprintf(p, bufflen, "%lluUL", cast(ulong)unsvalue);
1030 return p[0 .. length];
1031
1032 case TOK.float32Literal:
1033 const length = CTFloat.sprint(p, bufflen, 'g', floatvalue);
1034 p[length] = 'f';
1035 p[length + 1] = 0;
1036 return p[0 .. length + 1];
1037
1038 case TOK.float64Literal:
1039 const length = CTFloat.sprint(p, bufflen, 'g', floatvalue);
1040 return p[0 .. length];
1041
1042 case TOK.float80Literal:
1043 const length = CTFloat.sprint(p, bufflen, 'g', floatvalue);
1044 p[length] = 'L';
1045 p[length + 1] = 0;
1046 return p[0 .. length + 1];
1047
1048 case TOK.imaginary32Literal:
1049 const length = CTFloat.sprint(p, bufflen, 'g', floatvalue);
1050 p[length ] = 'f';
1051 p[length + 1] = 'i';
1052 p[length + 2] = 0;
1053 return p[0 .. length + 2];
1054
1055 case TOK.imaginary64Literal:
1056 const length = CTFloat.sprint(p, bufflen, 'g', floatvalue);
1057 p[length] = 'i';
1058 p[length + 1] = 0;
1059 return p[0 .. length + 1];
1060
1061 case TOK.imaginary80Literal:
1062 const length = CTFloat.sprint(p, bufflen, 'g', floatvalue);
1063 p[length ] = 'L';
1064 p[length + 1] = 'i';
1065 p[length + 2] = 0;
1066 return p[0 .. length + 2];
1067
1068 case TOK.string_:
1069 OutBuffer buf;
1070 buf.writeByte('"');
1071 for (size_t i = 0; i < len;)
1072 {
1073 dchar c;
1074 utf_decodeChar(ustring[0 .. len], i, c);
1075 writeCharLiteral(buf, c);
1076 }
1077 buf.writeByte('"');
1078 if (postfix)
1079 buf.writeByte(postfix);
1080 return buf.extractSlice(true);
1081
1082 case TOK.hexadecimalString:
1083 OutBuffer buf;
1084 buf.writeByte('x');
1085 buf.writeByte('"');
1086 foreach (size_t i; 0 .. len)
1087 {
1088 if (i)
1089 buf.writeByte(' ');
1090 buf.printf("%02x", ustring[i]);
1091 }
1092 buf.writeByte('"');
1093 if (postfix)
1094 buf.writeByte(postfix);
1095 return buf.extractSlice(true);
1096
1097 case TOK.identifier:
1098 case TOK.enum_:
1099 case TOK.struct_:
1100 case TOK.import_:
1101 case TOK.wchar_:
1102 case TOK.dchar_:
1103 case TOK.bool_:
1104 case TOK.char_:
1105 case TOK.int8:
1106 case TOK.uns8:
1107 case TOK.int16:
1108 case TOK.uns16:
1109 case TOK.int32:
1110 case TOK.uns32:
1111 case TOK.int64:
1112 case TOK.uns64:
1113 case TOK.int128:
1114 case TOK.uns128:
1115 case TOK.float32:
1116 case TOK.float64:
1117 case TOK.float80:
1118 case TOK.imaginary32:
1119 case TOK.imaginary64:
1120 case TOK.imaginary80:
1121 case TOK.complex32:
1122 case TOK.complex64:
1123 case TOK.complex80:
1124 case TOK.void_:
1125 return ident.toString();
1126
1127 default:
1128 return tochars[value];
1129 }
1130 }
1131
1132 static const(char)* toChars(TOK value)
1133 {
1134 return toString(value).ptr;
1135 }
1136
1137 extern (D) static string toString(TOK value) pure nothrow @nogc @safe
1138 {
1139 return tochars[value];
1140 }
1141 }
1142
1143 /**
1144 * Write a character, using a readable escape sequence if needed
1145 *
1146 * Useful for printing "" string literals in e.g. error messages, ddoc, or the `.stringof` property
1147 *
1148 * Params:
1149 * buf = buffer to append character in
1150 * c = code point to write
1151 */
1152 nothrow
1153 void writeCharLiteral(ref OutBuffer buf, dchar c)
1154 {
1155 switch (c)
1156 {
1157 case '\0':
1158 buf.writestring("\\0");
1159 break;
1160 case '\n':
1161 buf.writestring("\\n");
1162 break;
1163 case '\r':
1164 buf.writestring("\\r");
1165 break;
1166 case '\t':
1167 buf.writestring("\\t");
1168 break;
1169 case '\b':
1170 buf.writestring("\\b");
1171 break;
1172 case '\f':
1173 buf.writestring("\\f");
1174 break;
1175 case '"':
1176 case '\\':
1177 buf.writeByte('\\');
1178 goto default;
1179 default:
1180 if (c <= 0xFF)
1181 {
1182 if (isprint(c))
1183 buf.writeByte(c);
1184 else
1185 buf.printf("\\x%02x", c);
1186 }
1187 else if (c <= 0xFFFF)
1188 buf.printf("\\u%04x", c);
1189 else
1190 buf.printf("\\U%08x", c);
1191 break;
1192 }
1193 }
1194
1195 unittest
1196 {
1197 OutBuffer buf;
1198 foreach(dchar d; "a\n\r\t\b\f\0\x11\u7233\U00017233"d)
1199 {
1200 writeCharLiteral(buf, d);
1201 }
1202 assert(buf[] == `a\n\r\t\b\f\0\x11\u7233\U00017233`);
1203 }
1204
1205 /**
1206 * Write a single-quoted character literal
1207 *
1208 * Useful for printing '' char literals in e.g. error messages, ddoc, or the `.stringof` property
1209 *
1210 * Params:
1211 * buf = buffer to append character in
1212 * c = code point to write
1213 */
1214 nothrow
1215 void writeSingleCharLiteral(ref OutBuffer buf, dchar c)
1216 {
1217 buf.writeByte('\'');
1218 if (c == '\'')
1219 buf.writeByte('\\');
1220
1221 if (c == '"')
1222 buf.writeByte('"');
1223 else
1224 writeCharLiteral(buf, c);
1225
1226 buf.writeByte('\'');
1227 }
1228
1229 unittest
1230 {
1231 OutBuffer buf;
1232 writeSingleCharLiteral(buf, '\'');
1233 assert(buf[] == `'\''`);
1234 buf.reset();
1235 writeSingleCharLiteral(buf, '"');
1236 assert(buf[] == `'"'`);
1237 buf.reset();
1238 writeSingleCharLiteral(buf, '\n');
1239 assert(buf[] == `'\n'`);
1240 }