-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathini.d
3158 lines (2566 loc) · 70.7 KB
/
ini.d
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
/+
== arsd.ini ==
Copyright Elias Batek (0xEAB) 2025.
Distributed under the Boost Software License, Version 1.0.
+/
/++
INI configuration file support
This module provides a configurable INI parser with support for multiple
“dialects” of the format.
### Getting started
$(LIST
* [parseIniDocument] – Parses a string of INI data and stores the
result in a DOM-inspired [IniDocument] structure.
* [parseIniAA] – Parses a string of INI data and stores the result
in an associative array (named sections) of associative arrays
(key/value pairs of the section).
* [parseIniMergedAA] – Parses a string of INI data and stores the
result in a flat associative array (with all sections merged).
* [stringifyIni] – Serializes an [IniDocument] or an associative array
to a string of data in INI format.
)
---
import arsd.ini;
IniDocument!string parseIniFile(string filePath) {
import std.file : readText;
return parseIniDocument(readText(filePath));
}
---
---
import arsd.ini;
void writeIniFile(string filePath, IniDocument!string document) {
import std.file : write;
return write(filePath, stringifyIni(document));
}
---
### On destructiveness and GC usage
Depending on the dialect and string type,
[IniParser] can operate in one of these three modes:
$(LIST
* Non-destructive with no heap alloc (incl. `@nogc`)
* Non-destructive (uses the GC)
* Destructive with no heap alloc (incl. `@nogc`)
)
a) If a given dialect requests no mutation of the input data
(i.e. no escape sequences, no concaternation of substrings etc.)
and is therefore possible to implement with slicing operations only,
the parser will be non-destructive and not do any heap allocations.
Such a parser is verifiably `@nogc`, too.
b) In cases where a dialect requires data-mutating operations,
there are two ways for a parser to implement them:
b.0) Either perform those mutations on the input data itself
and alter the contents of that buffer.
Because of the destructive nature of this operation,
it can be performed only once safely.
(Such an implementation could optionally fix up the modified data
to become valid and parsable again.
Though doing so would come with a performance overhead.)
b.1) Or allocate a new buffer for the result of the operation.
This also has the advantage that it works with `immutable` and `const`
input data.
For convenience reasons the GC is used to perform such allocations.
Use [IniParser.isDestructive] to check for the operating mode.
The construct a non-destructive parser despite a mutable input data,
specify `const(char)[]` as the value of the `string` template parameter.
---
char[] mutableInput = [ /* … */ ];
auto parser = makeIniParser!(dialect, const(char)[])(mutableInput);
assert(parser.isDestructive == false);
---
+/
module arsd.ini;
///
@safe unittest {
// INI example data (e.g. from an `autorun.inf` file)
static immutable string rawIniData =
"[autorun]\n"
~ "open=setup.exe\n"
~ "icon=setup.exe,0\n";
// Parse the document into an associative array:
string[string][string] data = parseIniAA(rawIniData);
string open = data["autorun"]["open"];
string icon = data["autorun"]["icon"];
assert(open == "setup.exe");
assert(icon == "setup.exe,0");
}
///
@safe unittest {
// INI example data (e.g. from an `autorun.inf` file)
static immutable string rawIniData =
"[autorun]\n"
~ "open=setup.exe\n"
~ "icon=setup.exe,0\n";
// Parse the document into a flat associative array.
// (Sections would get merged, but there is only one section in the
// example anyway.)
string[string] data = parseIniMergedAA(rawIniData);
string open = data["open"];
string icon = data["icon"];
assert(open == "setup.exe");
assert(icon == "setup.exe,0");
}
///
@safe unittest {
// INI example data (e.g. from an `autorun.inf` file):
static immutable string rawIniData =
"[autorun]\n"
~ "open=setup.exe\n"
~ "icon=setup.exe,0\n";
// Parse the document.
IniDocument!string document = parseIniDocument(rawIniData);
// Let’s search for the value of an entry `icon` in the `autorun` section.
static string searchAutorunIcon(IniDocument!string document) {
// Iterate over all sections.
foreach (IniSection!string section; document.sections) {
// Search for the `[autorun]` section.
if (section.name == "autorun") {
// Iterate over all items in the section.
foreach (IniKeyValuePair!string item; section.items) {
// Search for the `icon` entry.
if (item.key == "icon") {
// Found!
return item.value;
}
}
}
}
// Not found!
return null;
}
// Call our search function.
string icon = searchAutorunIcon(document);
// Finally, verify the result.
assert(icon == "setup.exe,0");
}
/++
Determines whether a type `T` is a string type compatible with this library.
+/
enum isCompatibleString(T) = (is(T == immutable(char)[]) || is(T == const(char)[]) || is(T == char[]));
//dfmt off
/++
Feature set to be understood by the parser.
---
enum myDialect = (IniDialect.defaults | IniDialect.inlineComments);
---
+/
enum IniDialect : ulong {
/++
Minimum feature set.
No comments, no extras, no nothing.
Only sections, keys and values.
Everything fits into these categories from a certain point of view.
+/
lite = 0,
/++
Parse line comments (starting with `;`).
```ini
; This is a line comment.
;This one too.
key = value ;But this isn't one.
```
+/
lineComments = 0b_0000_0000_0000_0001,
/++
Parse inline comments (starting with `;`).
```ini
key1 = value2 ; Inline comment.
key2 = value2 ;Inline comment.
key3 = value3; Inline comment.
;Not a true inline comment (but technically equivalent).
```
+/
inlineComments = 0b_0000_0000_0000_0011,
/++
Parse line comments starting with `#`.
```ini
# This is a comment.
#Too.
key = value # Not a line comment.
```
+/
hashLineComments = 0b_0000_0000_0000_0100,
/++
Parse inline comments starting with `#`.
```ini
key1 = value2 # Inline comment.
key2 = value2 #Inline comment.
key3 = value3# Inline comment.
#Not a true inline comment (but technically equivalent).
```
+/
hashInlineComments = 0b_0000_0000_0000_1100,
/++
Parse quoted strings.
```ini
key1 = non-quoted value
key2 = "quoted value"
"quoted key" = value
non-quoted key = value
"another key" = "another value"
multi line = "line 1
line 2"
```
+/
quotedStrings = 0b_0000_0000_0001_0000,
/++
Parse quoted strings using single-quotes.
```ini
key1 = non-quoted value
key2 = 'quoted value'
'quoted key' = value
non-quoted key = value
'another key' = 'another value'
multi line = 'line 1
line 2'
```
+/
singleQuoteQuotedStrings = 0b_0000_0000_0010_0000,
/++
Parse key/value pairs separated with a colon (`:`).
```ini
key: value
key= value
```
+/
colonKeys = 0b_0000_0000_0100_0000,
/++
Concats substrings and emits them as a single token.
$(LIST
* For a mutable `char[]` input,
this will rewrite the data in the input array.
* For a non-mutable `immutable(char)[]` (=`string`) or `const(char)[]` input,
this will allocate a new array with the GC.
)
```ini
key = "Value1" "Value2"
; → Value1Value2
```
+/
concatSubstrings = 0b_0000_0001_0000_0000,
/++
Evaluates escape sequences in the input string.
$(LIST
* For a mutable `char[]` input,
this will rewrite the data in the input array.
* For a non-mutable `immutable(char)[]` (=`string`) or `const(char)[]` input,
this will allocate a new array with the GC.
)
$(SMALL_TABLE
Special escape sequences
`\\` | Backslash
`\0` | Null character
`\n` | Line feed
`\r` | Carriage return
`\t` | Tabulator
)
```ini
key1 = Line 1\nLine 2
; → Line 1
; Line 2
key2 = One \\ and one \;
; → One \ and one ;
```
+/
escapeSequences = 0b_0000_0010_0000_0000,
/++
Folds lines on escaped linebreaks.
$(LIST
* For a mutable `char[]` input,
this will rewrite the data in the input array.
* For a non-mutable `immutable(char)[]` (=`string`) or `const(char)[]` input,
this will allocate a new array with the GC.
)
```ini
key1 = word1\
word2
; → word1word2
key2 = foo \
bar
; → foo bar
```
+/
lineFolding = 0b_0000_0100_0000_0000,
/++
Imitates the behavior of the INI parser implementation found in PHP.
$(WARNING
This preset may be adjusted without further notice in the future
in cases where it increases alignment with PHP’s implementation.
)
+/
presetPhp = (
lineComments
| inlineComments
| hashLineComments
| hashInlineComments
| quotedStrings
| singleQuoteQuotedStrings
| concatSubstrings
),
///
presetDefaults = (
lineComments
| quotedStrings
| singleQuoteQuotedStrings
),
///
defaults = presetDefaults,
}
//dfmt on
private bool hasFeature(ulong dialect, ulong feature) @safe pure nothrow @nogc {
return ((dialect & feature) > 0);
}
private T[] spliceImpl(T)(T[] array, size_t at, size_t count) @safe pure nothrow @nogc
in (at < array.length)
in (count <= array.length)
in (at + count <= array.length) {
const upper = array.length - count;
for (size_t idx = at; idx < upper; ++idx) {
array[idx] = array[idx + count];
}
return array[0 .. ($ - count)];
}
private T[] splice(T)(auto ref scope T[] array, size_t at, size_t count) @safe pure nothrow @nogc {
static if (__traits(isRef, array)) {
array = spliceImpl(array, at, count); // @suppress(dscanner.suspicious.auto_ref_assignment)
return array;
} else {
return spliceImpl(array, at, count);
}
}
@safe unittest {
assert("foobar".dup.splice(0, 0) == "foobar");
assert("foobar".dup.splice(0, 6) == "");
assert("foobar".dup.splice(0, 1) == "oobar");
assert("foobar".dup.splice(1, 5) == "f");
assert("foobar".dup.splice(1, 4) == "fr");
assert("foobar".dup.splice(4, 1) == "foobr");
assert("foobar".dup.splice(4, 2) == "foob");
}
@safe unittest {
char[] array = ['a', 's', 'd', 'f'];
array.splice(1, 2);
assert(array == "af");
}
///
char resolveIniEscapeSequence(char c) @safe pure nothrow @nogc {
switch (c) {
case 'n':
return '\x0A';
case 'r':
return '\x0D';
case 't':
return '\x09';
case '\\':
return '\\';
case '0':
return '\x00';
default:
return c;
}
}
///
@safe unittest {
assert(resolveIniEscapeSequence('n') == '\n');
assert(resolveIniEscapeSequence('r') == '\r');
assert(resolveIniEscapeSequence('t') == '\t');
assert(resolveIniEscapeSequence('\\') == '\\');
assert(resolveIniEscapeSequence('0') == '\0');
// Unsupported characters are preserved.
assert(resolveIniEscapeSequence('a') == 'a');
assert(resolveIniEscapeSequence('Z') == 'Z');
assert(resolveIniEscapeSequence('1') == '1');
// Unsupported special characters are preserved.
assert(resolveIniEscapeSequence('@') == '@');
// Line breaks are preserved.
assert(resolveIniEscapeSequence('\n') == '\n');
assert(resolveIniEscapeSequence('\r') == '\r');
// UTF-8 is preserved.
assert(resolveIniEscapeSequence("ü"[0]) == "ü"[0]);
}
private struct StringRange {
private {
const(char)[] _data;
}
@safe pure nothrow @nogc:
public this(const(char)[] data) {
_data = data;
}
bool empty() const {
return (_data.length == 0);
}
char front() const {
return _data[0];
}
void popFront() {
_data = _data[1 .. $];
}
}
private struct StringSliceRange {
private {
const(char)[] _data;
}
@safe pure nothrow @nogc:
public this(const(char)[] data) {
_data = data;
}
bool empty() const {
return (_data.length == 0);
}
const(char)[] front() const {
return _data[0 .. 1];
}
void popFront() {
_data = _data[1 .. $];
}
}
/++
Resolves escape sequences and performs line folding.
Feature set depends on the [Dialect].
+/
string resolveIniEscapeSequences(Dialect dialect)(const(char)[] input) @safe pure nothrow {
size_t irrelevant = 0;
auto source = StringRange(input);
determineIrrelevantLoop: while (!source.empty) {
if (source.front != '\\') {
source.popFront();
continue;
}
source.popFront();
if (source.empty) {
break;
}
static if (dialect.hasFeature(Dialect.lineFolding)) {
switch (source.front) {
case '\n':
source.popFront();
irrelevant += 2;
continue determineIrrelevantLoop;
case '\r':
source.popFront();
irrelevant += 2;
if (source.empty) {
break determineIrrelevantLoop;
}
// CRLF?
if (source.front == '\n') {
source.popFront();
++irrelevant;
}
continue determineIrrelevantLoop;
default:
break;
}
}
static if (dialect.hasFeature(Dialect.escapeSequences)) {
source.popFront();
++irrelevant;
}
}
const escapedSize = input.length - irrelevant;
auto result = new char[](escapedSize);
size_t cursor = 0;
source = StringRange(input);
buildResultLoop: while (!source.empty) {
if (source.front != '\\') {
result[cursor++] = source.front;
source.popFront();
continue;
}
source.popFront();
if (source.empty) {
result[cursor] = '\\';
break;
}
static if (dialect.hasFeature(Dialect.lineFolding)) {
switch (source.front) {
case '\n':
source.popFront();
continue buildResultLoop;
case '\r':
source.popFront();
if (source.empty) {
break buildResultLoop;
}
// CRLF?
if (source.front == '\n') {
source.popFront();
}
continue buildResultLoop;
default:
break;
}
}
static if (dialect.hasFeature(Dialect.escapeSequences)) {
result[cursor++] = resolveIniEscapeSequence(source.front);
source.popFront();
continue;
} else {
result[cursor++] = '\\';
}
}
return result;
}
///
@safe unittest {
enum none = Dialect.lite;
enum escp = Dialect.escapeSequences;
enum fold = Dialect.lineFolding;
enum both = Dialect.escapeSequences | Dialect.lineFolding;
assert(resolveIniEscapeSequences!none("foo\\nbar") == "foo\\nbar");
assert(resolveIniEscapeSequences!escp("foo\\nbar") == "foo\nbar");
assert(resolveIniEscapeSequences!fold("foo\\nbar") == "foo\\nbar");
assert(resolveIniEscapeSequences!both("foo\\nbar") == "foo\nbar");
assert(resolveIniEscapeSequences!none("foo\\\nbar") == "foo\\\nbar");
assert(resolveIniEscapeSequences!escp("foo\\\nbar") == "foo\nbar");
assert(resolveIniEscapeSequences!fold("foo\\\nbar") == "foobar");
assert(resolveIniEscapeSequences!both("foo\\\nbar") == "foobar");
assert(resolveIniEscapeSequences!none("foo\\\n\\nbar") == "foo\\\n\\nbar");
assert(resolveIniEscapeSequences!escp("foo\\\n\\nbar") == "foo\n\nbar");
assert(resolveIniEscapeSequences!fold("foo\\\n\\nbar") == "foo\\nbar");
assert(resolveIniEscapeSequences!both("foo\\\n\\nbar") == "foo\nbar");
assert(resolveIniEscapeSequences!none("foobar\\") == "foobar\\");
assert(resolveIniEscapeSequences!escp("foobar\\") == "foobar\\");
assert(resolveIniEscapeSequences!fold("foobar\\") == "foobar\\");
assert(resolveIniEscapeSequences!both("foobar\\") == "foobar\\");
assert(resolveIniEscapeSequences!none("foo\\\r\nbar") == "foo\\\r\nbar");
assert(resolveIniEscapeSequences!escp("foo\\\r\nbar") == "foo\r\nbar");
assert(resolveIniEscapeSequences!fold("foo\\\r\nbar") == "foobar");
assert(resolveIniEscapeSequences!both("foo\\\r\nbar") == "foobar");
assert(resolveIniEscapeSequences!none(`\nfoobar\n`) == "\\nfoobar\\n");
assert(resolveIniEscapeSequences!escp(`\nfoobar\n`) == "\nfoobar\n");
assert(resolveIniEscapeSequences!fold(`\nfoobar\n`) == "\\nfoobar\\n");
assert(resolveIniEscapeSequences!both(`\nfoobar\n`) == "\nfoobar\n");
assert(resolveIniEscapeSequences!none("\\\nfoo \\\rba\\\r\nr") == "\\\nfoo \\\rba\\\r\nr");
assert(resolveIniEscapeSequences!escp("\\\nfoo \\\rba\\\r\nr") == "\nfoo \rba\r\nr");
assert(resolveIniEscapeSequences!fold("\\\nfoo \\\rba\\\r\nr") == "foo bar");
assert(resolveIniEscapeSequences!both("\\\nfoo \\\rba\\\r\nr") == "foo bar");
}
/++
Type of a token (as output by the parser)
+/
public enum IniTokenType {
/// indicates an error
invalid = 0,
/// insignificant whitespace
whitespace,
/// section header opening bracket
bracketOpen,
/// section header closing bracket
bracketClose,
/// key/value separator, e.g. '='
keyValueSeparator,
/// line break, i.e. LF, CRLF or CR
lineBreak,
/// text comment
comment,
/// item key data
key,
/// item value data
value,
/// section name data
sectionHeader,
}
/++
Token of INI data (as output by the parser)
+/
struct IniToken(string) if (isCompatibleString!string) {
///
IniTokenType type;
/++
Content
+/
string data;
}
private alias TokenType = IniTokenType;
private alias Dialect = IniDialect;
private enum LocationState {
newLine,
key,
preValue,
inValue,
sectionHeader,
}
private enum OperatingMode {
nonDestructive,
destructive,
}
private enum OperatingMode operatingMode(string) = (is(string == char[]))
? OperatingMode.destructive : OperatingMode.nonDestructive;
/++
Low-level INI parser
See_also:
$(LIST
* [IniFilteredParser]
* [parseIniDocument]
* [parseIniAA]
* [parseIniMergedAA]
)
+/
struct IniParser(
IniDialect dialect = IniDialect.defaults,
string = immutable(char)[],
) if (isCompatibleString!string) {
public {
///
alias Token = IniToken!string;
// dfmt off
///
enum isDestructive = (
(operatingMode!string == OperatingMode.destructive)
&& (
dialect.hasFeature(Dialect.concatSubstrings)
|| dialect.hasFeature(Dialect.escapeSequences)
|| dialect.hasFeature(Dialect.lineFolding)
)
);
// dfmt on
}
private {
string _source;
Token _front;
bool _empty = true;
LocationState _locationState = LocationState.newLine;
static if (dialect.hasFeature(Dialect.concatSubstrings)) {
bool _bypassConcatSubstrings = false;
}
}
@safe pure nothrow:
///
public this(string rawIni) {
_source = rawIni;
_empty = false;
this.popFront();
}
// Range API
public {
///
bool empty() const @nogc {
return _empty;
}
///
inout(Token) front() inout @nogc {
return _front;
}
private void popFrontImpl() {
if (_source.length == 0) {
_empty = true;
return;
}
_front = this.fetchFront();
}
/*
This is a workaround.
The compiler doesn’t feel like inferring `@nogc` properly otherwise.
→ cannot call non-@nogc function
`arsd.ini.makeIniParser!(IniDialect.concatSubstrings, char[]).makeIniParser`
→ which calls
`arsd.ini.IniParser!(IniDialect.concatSubstrings, char[]).IniParser.this`
→ which calls
`arsd.ini.IniParser!(IniDialect.concatSubstrings, char[]).IniParser.popFront`
*/
static if (isDestructive) {
///
void popFront() @nogc {
popFrontImpl();
}
} else {
///
void popFront() {
popFrontImpl();
}
}
// Destructive parsers make very poor Forward Ranges.
static if (!isDestructive) {
///
inout(typeof(this)) save() inout @nogc {
return this;
}
}
}
// extras
public {
/++
Skips tokens that are irrelevant for further processing
Returns:
true = if there are no further tokens,
i.e. whether the range is empty now
+/
bool skipIrrelevant(bool skipComments = true) {
static bool isIrrelevant(const TokenType type, const bool skipComments) {
pragma(inline, true);
final switch (type) with (TokenType) {
case invalid:
return false;
case whitespace:
case bracketOpen:
case bracketClose:
case keyValueSeparator:
case lineBreak:
return true;
case comment:
return skipComments;
case sectionHeader:
case key:
case value:
return false;
}
}
while (!this.empty) {
const irrelevant = isIrrelevant(_front.type, skipComments);
if (!irrelevant) {
return false;
}
this.popFront();
}
return true;
}
}
private {
bool isOnFinalChar() const @nogc {
pragma(inline, true);
return (_source.length == 1);
}
bool isAtStartOfLineOrEquivalent() @nogc {
return (_locationState == LocationState.newLine);
}
Token makeToken(TokenType type, size_t length) @nogc {
auto token = Token(type, _source[0 .. length]);
_source = _source[length .. $];
return token;
}
Token makeToken(TokenType type, size_t length, size_t skip) @nogc {
_source = _source[skip .. $];
return this.makeToken(type, length);
}
Token lexWhitespace() @nogc {
foreach (immutable idxM1, const c; _source[1 .. $]) {
switch (c) {
case '\x09':
case '\x0B':
case '\x0C':
case ' ':
break;
default:
return this.makeToken(TokenType.whitespace, (idxM1 + 1));
}
}
// all whitespace
return this.makeToken(TokenType.whitespace, _source.length);
}
Token lexComment() @nogc {
foreach (immutable idxM1, const c; _source[1 .. $]) {
switch (c) {
default:
break;
case '\x0A':
case '\x0D':
return this.makeToken(TokenType.comment, idxM1, 1);
}
}
return this.makeToken(TokenType.comment, (-1 + _source.length), 1);
}
Token lexSubstringImpl(TokenType tokenType)() {
enum Result {
end,
endChomp,
regular,
whitespace,
sequence,
}
enum QuotedString : ubyte {
none = 0,
regular,
single,
}
// dfmt off
enum bool hasAnyQuotedString = (
dialect.hasFeature(Dialect.quotedStrings)
|| dialect.hasFeature(Dialect.singleQuoteQuotedStrings)
);
enum bool hasAnyEscaping = (
dialect.hasFeature(Dialect.lineFolding)
|| dialect.hasFeature(Dialect.escapeSequences)
);
// dfmt on
static if (hasAnyQuotedString) {
auto inQuotedString = QuotedString.none;
}
static if (dialect.hasFeature(Dialect.quotedStrings)) {
if (_source[0] == '"') {
inQuotedString = QuotedString.regular;
// chomp quote initiator
_source = _source[1 .. $];
}
}
static if (dialect.hasFeature(Dialect.singleQuoteQuotedStrings)) {
if (_source[0] == '\'') {
inQuotedString = QuotedString.single;
// chomp quote initiator
_source = _source[1 .. $];
}
}
static if (!hasAnyQuotedString) {
enum inQuotedString = QuotedString.none;
}
Result nextChar(const char c) @safe pure nothrow @nogc {
pragma(inline, true);
switch (c) {
default:
return Result.regular;
case '\x09':
case '\x0B':
case '\x0C':
case ' ':
return (inQuotedString != QuotedString.none)
? Result.regular : Result.whitespace;