11import json
22from pathlib import Path
3- from typing import Optional
3+ from typing import Any , Optional
44
55import yaml
66from pydantic import ValidationError
77
8- from .pdl_ast import PDLException , PdlLocationType , Program
8+ from .pdl_ast import PDLException , PdlLocationType , Program , empty_block_location
99from .pdl_location_utils import get_line_map
1010from .pdl_schema_error_analyzer import analyze_errors
1111
@@ -25,22 +25,34 @@ def parse_str(
2525) -> tuple [Program , PdlLocationType ]:
2626 if file_name is None :
2727 file_name = ""
28- prog_yaml = yaml .safe_load (pdl_str )
28+ prog_dict = yaml .safe_load (pdl_str )
2929 line_table = get_line_map (pdl_str )
3030 loc = PdlLocationType (path = [], file = file_name , table = line_table )
31+ prog = parse_dict (prog_dict , loc )
32+ return prog , loc
33+
34+
35+ def parse_dict (
36+ pdl_dict : dict [str , Any ], loc : Optional [PdlLocationType ] = None
37+ ) -> Program :
3138 try :
32- prog = Program .model_validate (prog_yaml )
39+ prog = Program .model_validate (pdl_dict )
3340 # set_program_location(prog, pdl_str)
3441 except ValidationError as exc :
3542 pdl_schema_file = Path (__file__ ).parent / "pdl-schema.json"
3643 with open (pdl_schema_file , "r" , encoding = "utf-8" ) as schema_fp :
3744 schema = json .load (schema_fp )
3845 defs = schema ["$defs" ]
39- errors = analyze_errors (defs , defs ["Program" ], prog_yaml , loc )
46+ if loc is None :
47+ loc = empty_block_location
48+ errors = analyze_errors (defs , defs ["Program" ], pdl_dict , loc )
4049 if errors == []:
41- errors = [f"The file PDL { file_name } does not respect the schema." ]
50+ if loc .file == "" :
51+ errors = ["The PDL program does not respect the schema." ]
52+ else :
53+ errors = [f"The file PDL { loc .file } does not respect the schema." ]
4254 raise PDLParseError (errors ) from exc
43- return prog , loc
55+ return prog
4456
4557
4658# def set_program_location(prog: Program, pdl_str: str, file_name: str = ""):
0 commit comments