-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
1157 lines (938 loc) · 40.3 KB
/
parser.py
File metadata and controls
1157 lines (938 loc) · 40.3 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
import sys
import re
import os
import copy
import importlib.util
from pprint import pprint
from typing import Any, List, Dict, Optional, Callable
MISSING = object()
PARSER_NO_MATCH = object()
class QuotedString(str):
pass
SchemaTypeValidator = Callable[[Any], bool]
LiteralParser = Callable[[str], Any]
SCHEMA_TYPE_VALIDATORS: Dict[str, SchemaTypeValidator] = {}
LITERAL_PARSERS: List[LiteralParser] = []
def normalize_schema_type_name(name: str) -> str:
return name.strip().casefold()
def register_schema_type(name: str, validator: SchemaTypeValidator) -> None:
SCHEMA_TYPE_VALIDATORS[normalize_schema_type_name(name)] = validator
def register_value_parser(parser: LiteralParser, prepend: bool = False) -> None:
if prepend:
LITERAL_PARSERS.insert(0, parser)
else:
LITERAL_PARSERS.append(parser)
def register_custom_type(
name: str,
validator: SchemaTypeValidator,
parser: Optional[LiteralParser] = None,
prepend_parser: bool = False,
) -> None:
register_schema_type(name, validator)
if parser is not None:
register_value_parser(parser, prepend=prepend_parser)
def load_custom_type_plugins(plugin_dir: Optional[str] = None) -> None:
base_dir = plugin_dir or os.path.dirname(os.path.abspath(__file__))
for entry in sorted(os.listdir(base_dir)):
if not entry.endswith("_type.py"):
continue
plugin_path = os.path.join(base_dir, entry)
if not os.path.isfile(plugin_path):
continue
module_name = f"_ynfo_type_plugin_{os.path.splitext(entry)[0]}"
spec = importlib.util.spec_from_file_location(module_name, plugin_path)
if spec is None or spec.loader is None:
raise RuntimeError(f"Unable to load type plugin: {plugin_path}")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
def read_text_file(path: str) -> List[str]:
with open(path, "r") as f:
content = f.read()
return content.replace('\r\n', '\n').replace('\r', '\n').split('\n')
def normalize_input_path(path: str, required_ext: str) -> str:
if os.path.exists(path):
return path
if not path.endswith(required_ext):
candidate = f"{path}{required_ext}"
if os.path.exists(candidate):
return candidate
raise FileNotFoundError(f"File not found: {path}")
def parse_numeric_bound(raw: str) -> float:
bound = raw.strip()
if re.match(r"^[+-]?[0-9]+$", bound):
return float(int(bound))
if re.match(r"^[+-]?[0-9]+\.[0-9]+$", bound):
return float(bound)
raise SyntaxError(f"Invalid numeric bound in schema constraint: {raw}")
def parse_range_constraint(text: str) -> Dict[str, float]:
body = text[1:-1].strip()
parts = [p.strip() for p in body.split(",")]
if len(parts) != 2:
raise SyntaxError(f"Range constraint must be '(min,max)': {text}")
min_v = parse_numeric_bound(parts[0])
max_v = parse_numeric_bound(parts[1])
if min_v > max_v:
raise SyntaxError(f"Invalid range constraint (min > max): {text}")
return {"min": min_v, "max": max_v}
def constraint_target_type(tokens: List[str]) -> Optional[str]:
if not tokens:
return None
idx = 0
while idx < len(tokens) and normalize_schema_type_name(tokens[idx]) == "list":
idx += 1
if idx != len(tokens) - 1:
return None
terminal = tokens[idx]
terminal_norm = normalize_schema_type_name(terminal)
if terminal_norm in ("int", "float"):
return terminal
return None
def normalize_schema_default_value(value: Any) -> Any:
if isinstance(value, QuotedString):
return str(value)
if isinstance(value, list):
return [normalize_schema_default_value(v) for v in value]
if isinstance(value, dict):
return {k: normalize_schema_default_value(v) for k, v in value.items()}
return value
def split_schema_declaration(
text: str
) -> tuple[str, List[str], bool, Optional[Dict[str, float]], Any]:
parts = text.split(None, 1)
field_name = parts[0].strip() if parts else ""
rest = parts[1].strip() if len(parts) > 1 else ""
tokens = []
optional = False
constraint: Optional[Dict[str, float]] = None
default_value = MISSING
if rest:
if rest == ":":
return field_name, tokens, optional, constraint, default_value
tokens, leftover = parse_schema_prefix(rest)
leftover = leftover.strip()
if leftover.startswith('?'):
optional = True
leftover = leftover[1:].strip()
if leftover.startswith('('):
end_idx = leftover.find(')')
if end_idx == -1:
raise SyntaxError(f"Invalid schema declaration: {text}")
constraint_text = leftover[:end_idx + 1]
constraint = parse_range_constraint(constraint_text)
if constraint_target_type(tokens) is None:
raise SyntaxError(f"Range constraints require Int/Float schema: {text}")
leftover = leftover[end_idx + 1:].strip()
if leftover.startswith(':'):
default_text = leftover[1:].strip()
if default_text:
default_value = normalize_schema_default_value(parse_value_or_list(default_text))
leftover = ""
if leftover:
raise SyntaxError(f"Invalid schema declaration: {text}")
if default_value is not MISSING:
if tokens:
validate_schema(tokens, default_value, f"schema default for {field_name}")
if constraint is not None:
validate_numeric_range_constraint(default_value, constraint, f"schema default for {field_name}")
return field_name, tokens, optional, constraint, default_value
def parse_schema_block(lines: List[str], start: int, indent: int) -> tuple[Dict[str, Any], int]:
schema_fields: Dict[str, Any] = {}
i = start
while i < len(lines):
line = lines[i]
line_without_tabs = line.replace('\t', ' ')
current_indent = len(line_without_tabs) - len(line_without_tabs.lstrip(' '))
clean_line = strip_inline_comment(line.rstrip())
if not clean_line.strip():
i += 1
continue
if current_indent < indent:
break
if current_indent > indent:
raise SyntaxError(f"Unexpected indentation in schema: {clean_line.strip()}")
stripped = clean_line.lstrip()
if not stripped.startswith('.'):
raise SyntaxError(f"Schema lines must start with '.': {clean_line.strip()}")
declaration = stripped[1:].strip()
field_name, schema_tokens, optional, constraint, default_value = split_schema_declaration(declaration)
if not field_name:
raise SyntaxError(f"Missing field name in schema: {clean_line.strip()}")
if field_name in schema_fields:
raise ValueError(f"Duplicate schema key: {field_name}")
i += 1
child_lines: List[str] = []
while i < len(lines):
next_line = lines[i]
next_without_tabs = next_line.replace('\t', ' ')
next_indent = len(next_without_tabs) - len(next_without_tabs.lstrip(' '))
clean_next = strip_inline_comment(next_line.rstrip())
if not clean_next.strip():
child_lines.append(next_line)
i += 1
continue
if next_indent <= current_indent:
break
child_lines.append(next_line)
i += 1
children = None
if child_lines:
first_indent = None
for child in child_lines:
child_clean = strip_inline_comment(child.rstrip())
if not child_clean.strip():
continue
child_no_tabs = child.replace('\t', ' ')
first_indent = len(child_no_tabs) - len(child_no_tabs.lstrip(' '))
break
if first_indent is not None:
children, _ = parse_schema_block(child_lines, 0, first_indent)
schema_fields[field_name] = {
"tokens": schema_tokens,
"optional": optional,
"constraint": constraint,
"default": default_value,
"children": children,
}
return schema_fields, i
def load_yns_schema(schema_file: str) -> Dict[str, Any]:
path = normalize_input_path(schema_file, '.yns')
lines = read_text_file(path)
first_indent = None
for line in lines:
clean = strip_inline_comment(line.rstrip())
if not clean.strip():
continue
line_no_tabs = line.replace('\t', ' ')
first_indent = len(line_no_tabs) - len(line_no_tabs.lstrip(' '))
break
if first_indent is None:
return {}
schema_fields, _ = parse_schema_block(lines, 0, first_indent)
return schema_fields
def validate_against_yns_schema(data: Any, schema_fields: Dict[str, Any], context: str = "root") -> None:
if not isinstance(data, dict):
raise ValueError(f"Schema mismatch for {context}: expected object, got {type(data).__name__}")
apply_schema_defaults(data, schema_fields)
expected = set(schema_fields.keys())
actual = set(data.keys())
missing_required = [
key for key, field in schema_fields.items()
if not field["optional"] and key not in data
]
if missing_required:
raise ValueError(f"Schema mismatch for {context}: missing required fields {missing_required}")
extra = sorted(actual - expected)
if extra:
raise ValueError(f"Schema mismatch for {context}: unexpected fields {extra}")
for key, field in schema_fields.items():
if key not in data:
continue
value = data[key]
field_context = f"{context}.{key}"
if field["children"] is not None:
validate_against_yns_schema(value, field["children"], field_context)
tokens = field["tokens"]
if tokens:
validate_schema(tokens, value, field_context)
constraint = field["constraint"]
if constraint is not None:
validate_numeric_range_constraint(value, constraint, field_context)
def apply_schema_defaults(data: Dict[str, Any], schema_fields: Dict[str, Any]) -> None:
for key, field in schema_fields.items():
if key not in data and field["default"] is not MISSING:
data[key] = copy.deepcopy(field["default"])
for key, field in schema_fields.items():
if key not in data:
continue
if field["children"] is not None and isinstance(data[key], dict):
apply_schema_defaults(data[key], field["children"])
def validate_numeric_range_constraint(value: Any, constraint: Dict[str, float], context: str) -> None:
if isinstance(value, list):
for idx, item in enumerate(value):
validate_numeric_range_constraint(item, constraint, f"{context}[{idx}]")
return
if isinstance(value, bool) or not isinstance(value, (int, float)):
raise ValueError(f"Schema mismatch for {context}: range constraint requires numeric value")
min_v = constraint["min"]
max_v = constraint["max"]
numeric_value = float(value)
if numeric_value < min_v or numeric_value > max_v:
raise ValueError(
f"Schema mismatch for {context}: value {value} out of allowed range [{min_v:g}, {max_v:g}]"
)
def parse_cli_args(argv: List[str]) -> tuple[str, Optional[str]]:
input_file: Optional[str] = None
schema_file: Optional[str] = None
i = 0
while i < len(argv):
arg = argv[i]
if arg in ("-s", "--schema"):
if i + 1 >= len(argv):
raise ValueError("Missing schema file after -s/--schema")
schema_file = argv[i + 1]
i += 2
continue
if arg.startswith("--schema="):
schema_file = arg.split("=", 1)[1]
i += 1
continue
if arg.startswith("-"):
raise ValueError(f"Unknown flag: {arg}")
if input_file is not None:
raise ValueError("Only one input .ynfo file can be provided")
input_file = arg
i += 1
if input_file is None:
raise ValueError("Missing input .ynfo file")
return input_file, schema_file
class RefResolver:
def __init__(self):
self.cache: Dict[str, Any] = {}
def get_file_data(self, filename: str) -> Any:
if re.match(r"^[0-9]", filename):
raise ValueError(f"Invalid filename '{filename}': filenames cannot start with a number.")
if filename not in self.cache:
path = filename if filename.endswith('.ynfo') else f"{filename}.ynfo"
if not os.path.exists(path):
raise FileNotFoundError(f"File not found: {path}")
lines = read_text_file(path)
self.cache[filename] = parse_lines(lines, allow_top_level_scalar=True)
return self.cache[filename]
def resolve_path(self, data: Any, path: str) -> Any:
parts = re.split(r'\.|\[|\]', path)
parts = [p for p in parts if p]
current = data
for part in parts:
try:
if isinstance(current, list) and part.isdigit():
current = current[int(part)]
elif isinstance(current, dict):
if part in current:
current = current[part]
else:
return MISSING
else:
return MISSING
except (IndexError, KeyError, TypeError):
return MISSING
return current
def process(self, data: Any, current_file: str) -> Any:
if isinstance(data, dict):
return {k: self.process(v, current_file) for k, v in data.items()}
elif isinstance(data, list):
return [self.process(i, current_file) for i in data]
elif isinstance(data, QuotedString):
return str(data)
elif isinstance(data, str):
if is_ip(data):
return data
match = re.match(r"^([\w\-]+)\.([\w\.\[\]\-]+)$", data)
if match:
prefix, path = match.groups()
if re.match(r"^[0-9]", prefix):
raise ValueError(f"Invalid filename '{prefix}': filenames cannot start with a number.")
target_filename = current_file if prefix == "self" else prefix
target_data = self.get_file_data(target_filename)
resolved = self.resolve_path(target_data, path)
if resolved is MISSING:
raise ValueError(f"Reference not found: {prefix}.{path}")
return resolved
return data
def parse_lines(
lines: List[str],
indent: int = 0,
allow_top_level_scalar: bool = False,
) -> Any:
entries = []
seen_fields = set()
seen_unnamed = False
i = 0
while i < len(lines):
line = lines[i]
line_without_tabs = line.replace('\t', ' ')
current_indent = len(line_without_tabs) - len(line_without_tabs.lstrip(' '))
if current_indent < indent:
break
if not line.strip():
i += 1
continue
clean_line = strip_inline_comment(line.rstrip())
if not clean_line.strip():
i += 1
continue
if clean_line.lstrip().startswith('.'):
field_content = clean_line.lstrip()[1:].lstrip()
if ':' not in field_content:
raise SyntaxError(f"Missing ':' in field declaration: {clean_line.strip()}")
if ':' in field_content:
colon_pos = -1
in_quotes = False
for idx, ch in enumerate(field_content):
if ch == '"':
in_quotes = not in_quotes
elif ch == ':' and not in_quotes:
colon_pos = idx
break
if colon_pos >= 0:
field_schema_part = field_content[:colon_pos].strip()
value_part = field_content[colon_pos + 1:].strip()
else:
raise SyntaxError(f"Missing ':' in field declaration: {clean_line.strip()}")
else:
field_name = field_content.strip()
value_part = ""
i += 1
if ':' not in field_content:
field_schema_part = field_content.strip()
field_name, schema_tokens = parse_field_schema(field_schema_part)
if not field_name:
raise SyntaxError(f"Missing field name: {clean_line.strip()}")
if field_name in seen_fields:
raise ValueError(f"Duplicate key: {field_name}")
seen_fields.add(field_name)
nested_lines = []
while i < len(lines):
next_line = lines[i]
next_without_tabs = next_line.replace('\t', ' ')
next_indent = len(next_without_tabs) - len(next_without_tabs.lstrip(' '))
clean_next = strip_inline_comment(next_line.rstrip())
if not clean_next.strip():
nested_lines.append(next_line)
i += 1
continue
if next_indent <= current_indent:
break
nested_lines.append(next_line)
i += 1
if value_part:
value = parse_value_or_list(value_part)
validate_schema(schema_tokens, value, field_name)
entries.append(("field", field_name, value))
if nested_lines:
pass
else:
if nested_lines:
first_nested_clean = strip_inline_comment(nested_lines[0].rstrip())
if first_nested_clean.lstrip().startswith('.'):
value = parse_lines(
nested_lines,
indent=current_indent + 1,
allow_top_level_scalar=False,
)
elif first_nested_clean.lstrip().startswith(':'):
value = parse_list(nested_lines, indent=current_indent + 1)
elif first_nested_clean.lstrip().startswith('-'):
value = parse_list(nested_lines, indent=current_indent + 1)
else:
value = parse_list(nested_lines, indent=current_indent + 1)
else:
value = ""
validate_schema(schema_tokens, value, field_name)
entries.append(("field", field_name, value))
elif clean_line.lstrip().startswith(':') or clean_line.lstrip().startswith('['):
# Unnamed field/list item at this level
if clean_line.lstrip().startswith(':'):
item_content = clean_line.lstrip()[1:].strip()
schema_tokens, item_content = parse_unnamed_schema(item_content)
else:
schema_tokens, rest = parse_schema_prefix(clean_line.lstrip())
if not rest.startswith(':'):
raise SyntaxError(f"Missing ':' after schema: {clean_line.strip()}")
item_content = rest[1:].lstrip()
nested_lines = []
i += 1
while i < len(lines):
next_line = lines[i]
next_without_tabs = next_line.replace('\t', ' ')
next_indent = len(next_without_tabs) - len(next_without_tabs.lstrip(' '))
clean_next = strip_inline_comment(next_line.rstrip())
if not clean_next.strip():
nested_lines.append(next_line)
i += 1
continue
if next_indent <= current_indent:
break
nested_lines.append(next_line)
i += 1
if item_content:
item_value = parse_value_or_list(item_content)
validate_schema(schema_tokens, item_value, "<unnamed>")
if nested_lines:
first_nested = strip_inline_comment(nested_lines[0].rstrip())
if first_nested.lstrip().startswith('.'):
nested_obj = parse_lines(
nested_lines,
indent=current_indent + 1,
allow_top_level_scalar=False,
)
if isinstance(item_value, dict):
item_value.update(nested_obj)
entries.append(("unnamed", item_value))
else:
entries.append(("unnamed", {'value': item_value, **nested_obj}))
else:
nested_list = parse_list(nested_lines, indent=current_indent + 1)
entries.append(("unnamed", {'value': item_value, 'items': nested_list}))
else:
entries.append(("unnamed", item_value))
else:
if nested_lines:
first_nested = strip_inline_comment(nested_lines[0].rstrip())
if first_nested.lstrip().startswith('.'):
unnamed_value = parse_lines(
nested_lines,
indent=current_indent + 1,
allow_top_level_scalar=False,
)
validate_schema(schema_tokens, unnamed_value, "<unnamed>")
entries.append(("unnamed", unnamed_value))
else:
unnamed_value = parse_list(nested_lines, indent=current_indent + 1)
validate_schema(schema_tokens, unnamed_value, "<unnamed>")
entries.append(("unnamed", unnamed_value))
else:
validate_schema(schema_tokens, [], "<unnamed>")
entries.append(("unnamed", []))
seen_unnamed = True
else:
if not entries and allow_top_level_scalar:
if clean_line.lstrip().startswith('-'):
return parse_list(lines[i:], indent=current_indent)
values = []
while i < len(lines):
current_line = lines[i]
line_without_tabs = current_line.replace('\t', ' ')
line_indent = len(line_without_tabs) - len(line_without_tabs.lstrip(' '))
if line_indent != current_indent:
break
clean_current = strip_inline_comment(current_line.rstrip())
if clean_current.strip():
if clean_current.lstrip().startswith('-'):
# List item
item_content = clean_current.lstrip()[1:].strip()
if item_content:
values.append(parse_value_or_list(item_content))
elif clean_current.lstrip().startswith(':'):
item_content = clean_current.lstrip()[1:].strip()
if item_content:
values.append(parse_value_or_list(item_content))
else:
values.append([])
else:
tokens = tokenize_values(clean_current.strip())
for token in tokens:
values.append(parse_value(token))
i += 1
if len(values) == 1:
return values[0]
return values
else:
raise SyntaxError(f"Unexpected line (missing '.' or ':'): {clean_line.strip()}")
if not entries:
return {}
if not seen_unnamed:
return {name: value for _, name, value in entries}
list_out = []
for entry in entries:
if entry[0] == "field":
_, name, value = entry
list_out.append({name: value})
else:
_, value = entry
list_out.append(value)
if len(list_out) == 1 and entries[0][0] == "unnamed":
return list_out[0]
return list_out
def parse_list(lines: List[str], indent: int) -> List[Any]:
items = []
i = 0
while i < len(lines):
line = lines[i]
line_without_tabs = line.replace('\t', ' ')
current_indent = len(line_without_tabs) - len(line_without_tabs.lstrip(' '))
if current_indent < indent:
break
if not line.strip():
i += 1
continue
clean_line = strip_inline_comment(line.rstrip())
if not clean_line.strip():
i += 1
continue
if clean_line.lstrip().startswith('-'):
item_content = clean_line.lstrip()[1:].strip()
nested_lines = []
i += 1
while i < len(lines):
next_line = lines[i]
next_without_tabs = next_line.replace('\t', ' ')
next_indent = len(next_without_tabs) - len(next_without_tabs.lstrip(' '))
clean_next = strip_inline_comment(next_line.rstrip())
if not clean_next.strip():
nested_lines.append(next_line)
i += 1
continue
if next_indent <= current_indent:
break
nested_lines.append(next_line)
i += 1
if item_content:
item_value = parse_value_or_list(item_content)
if nested_lines:
first_nested = strip_inline_comment(nested_lines[0].rstrip())
if first_nested.lstrip().startswith('.'):
nested_obj = parse_lines(
nested_lines,
indent=current_indent + 1,
allow_top_level_scalar=False,
)
if isinstance(item_value, dict):
item_value.update(nested_obj)
items.append(item_value)
elif item_value == "":
items.append(nested_obj)
else:
items.append({'value': item_value, **nested_obj})
else:
nested_list = parse_list(nested_lines, indent=current_indent + 1)
if item_value == "":
items.append(nested_list)
else:
items.append({'value': item_value, 'items': nested_list})
else:
items.append(item_value)
else:
if nested_lines:
first_nested = strip_inline_comment(nested_lines[0].rstrip())
if first_nested.lstrip().startswith('.'):
items.append(
parse_lines(
nested_lines,
indent=current_indent + 1,
allow_top_level_scalar=False,
)
)
else:
items.append(parse_list(nested_lines, indent=current_indent + 1))
else:
items.append("")
elif clean_line.lstrip().startswith(':') or clean_line.lstrip().startswith('['):
if clean_line.lstrip().startswith(':'):
item_content = clean_line.lstrip()[1:].strip()
schema_tokens, item_content = parse_unnamed_schema(item_content)
else:
schema_tokens, rest = parse_schema_prefix(clean_line.lstrip())
if not rest.startswith(':'):
raise SyntaxError(f"Missing ':' after schema: {clean_line.strip()}")
item_content = rest[1:].lstrip()
nested_lines = []
i += 1
while i < len(lines):
next_line = lines[i]
next_without_tabs = next_line.replace('\t', ' ')
next_indent = len(next_without_tabs) - len(next_without_tabs.lstrip(' '))
clean_next = strip_inline_comment(next_line.rstrip())
if not clean_next.strip():
nested_lines.append(next_line)
i += 1
continue
if next_indent <= current_indent:
break
nested_lines.append(next_line)
i += 1
if item_content:
item_value = parse_value_or_list(item_content)
validate_schema(schema_tokens, item_value, "<unnamed>")
if nested_lines:
first_nested = strip_inline_comment(nested_lines[0].rstrip())
if first_nested.lstrip().startswith('.'):
nested_obj = parse_lines(nested_lines, indent=current_indent + 1, allow_top_level_scalar=False)
if isinstance(item_value, dict):
item_value.update(nested_obj)
items.append(item_value)
else:
items.append({'value': item_value, **nested_obj})
else:
nested_list = parse_list(nested_lines, indent=current_indent + 1)
items.append({'value': item_value, 'items': nested_list})
else:
items.append(item_value)
else:
if nested_lines:
first_nested = strip_inline_comment(nested_lines[0].rstrip())
if first_nested.lstrip().startswith('.'):
unnamed_value = parse_lines(nested_lines, indent=current_indent + 1, allow_top_level_scalar=False)
validate_schema(schema_tokens, unnamed_value, "<unnamed>")
items.append(unnamed_value)
else:
unnamed_value = parse_list(nested_lines, indent=current_indent + 1)
validate_schema(schema_tokens, unnamed_value, "<unnamed>")
items.append(unnamed_value)
else:
validate_schema(schema_tokens, [], "<unnamed>")
items.append([])
elif clean_line.lstrip().startswith('.'):
nested_lines = [line]
i += 1
while i < len(lines):
next_line = lines[i]
next_without_tabs = next_line.replace('\t', ' ')
next_indent = len(next_without_tabs) - len(next_without_tabs.lstrip(' '))
if next_indent < current_indent:
break
nested_lines.append(next_line)
i += 1
items.append(
parse_lines(
nested_lines,
indent=current_indent,
allow_top_level_scalar=False,
)
)
else:
tokens = tokenize_values(clean_line.strip())
for token in tokens:
items.append(parse_value(token))
i += 1
return items
def parse_quoted_string_literal(value: str) -> Any:
if value.startswith('"') and value.endswith('"'):
return QuotedString(value[1:-1])
return PARSER_NO_MATCH
def parse_ip_literal(value: str) -> Any:
if is_ip(value):
return IP(value)
return PARSER_NO_MATCH
def parse_float_literal(value: str) -> Any:
if re.match(r"^[+-]?[0-9]+\.[0-9]+$", value):
return float(value)
return PARSER_NO_MATCH
def parse_int_literal(value: str) -> Any:
if re.match(r"^[+-]?[0-9]+$", value):
return int(value)
return PARSER_NO_MATCH
def parse_bool_or_null_literal(value: str) -> Any:
lowered = value.lower()
if lowered == "true":
return True
if lowered == "false":
return False
if lowered == "null":
return None
return PARSER_NO_MATCH
def parse_reference_literal(value: str) -> Any:
if re.match(r"^[\w\-]+\.[\w\.\[\]\-]+$", value):
return value
return PARSER_NO_MATCH
def parse_value(value: str) -> Any:
value = value.strip()
if not value:
return ""
for parser in LITERAL_PARSERS:
parsed = parser(value)
if parsed is not PARSER_NO_MATCH:
return parsed
raise SyntaxError(f"Unquoted or invalid value: {value}")
def parse_value_or_list(text: str) -> Any:
if not text.strip():
return ""
tokens = tokenize_values(text)
if len(tokens) == 1:
return parse_value(tokens[0])
return [parse_value(t) for t in tokens]
def parse_field_schema(text: str) -> tuple[str, List[str]]:
if '[' not in text:
return text.strip(), []
name_part, schema_part = text.split('[', 1)
name_part = name_part.strip()
schema_part = '[' + schema_part
tokens = parse_schema_tokens(schema_part)
return name_part, tokens
def parse_unnamed_schema(text: str) -> tuple[List[str], str]:
tokens, rest = parse_schema_prefix(text)
if rest.startswith(':'):
rest = rest[1:].lstrip()
return tokens, rest
def parse_schema_prefix(text: str) -> tuple[List[str], str]:
tokens = []
i = 0
text_len = len(text)
while i < text_len:
while i < text_len and text[i].isspace():
i += 1
if i >= text_len or text[i] != '[':
break
end = text.find(']', i + 1)
if end == -1:
raise SyntaxError(f"Unterminated schema token in: {text}")
token = text[i + 1:end].strip()
if not token:
raise SyntaxError(f"Empty schema token in: {text}")
tokens.append(token)
i = end + 1
rest = text[i:].lstrip()
if tokens:
validate_schema_tokens(tokens)
return tokens, rest
def parse_schema_tokens(text: str) -> List[str]:
tokens, rest = parse_schema_prefix(text)
if rest:
raise SyntaxError(f"Invalid schema syntax: {text}")
return tokens
def validate_schema_tokens(tokens: List[str]) -> None:
allowed = set(SCHEMA_TYPE_VALIDATORS.keys())
for t in tokens:
if normalize_schema_type_name(t) not in allowed:
raise SyntaxError(f"Unknown schema type: {t}")
if tokens and normalize_schema_type_name(tokens[0]) != "list" and len(tokens) > 1:
raise SyntaxError("Only List can chain multiple types.")
def build_schema(tokens: List[str]) -> Dict[str, Any]:
if not tokens:
return {"kind": "any"}
first = normalize_schema_type_name(tokens[0])
if first != "list":
return {"kind": "type", "name": first}
if len(tokens) == 1:
return {"kind": "list", "element": {"kind": "any"}}
if normalize_schema_type_name(tokens[1]) == "list":
return {"kind": "list", "element": build_schema(tokens[1:])}
element_types = []
for t in tokens[1:]:
t_norm = normalize_schema_type_name(t)
if t_norm == "list":
raise SyntaxError("List must be the first type in a list schema.")
element_types.append({"kind": "type", "name": t_norm})
return {"kind": "list", "element": {"kind": "union", "types": element_types}}
def validate_schema(tokens: List[str], value: Any, context: str) -> None:
if not tokens:
return
schema = build_schema(tokens)
if not validate_value(schema, value):
raise ValueError(f"Schema mismatch for {context}: expected {tokens}, got {type(value).__name__}")
def validate_value(schema: Dict[str, Any], value: Any) -> bool: