Skip to content

Commit 791e0a1

Browse files
committed
feat: add a parse_dict function to pdl_parser (#943)
Signed-off-by: Louis Mandel <[email protected]>
1 parent 7fc7f9a commit 791e0a1

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/pdl/pdl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
)
2121
from .pdl_interpreter import InterpreterState, process_prog
2222
from .pdl_lazy import PdlDict
23-
from .pdl_parser import parse_file, parse_str
23+
from .pdl_parser import parse_dict, parse_file, parse_str
2424
from .pdl_runner import exec_docker
2525
from .pdl_utils import validate_scope
2626

@@ -104,7 +104,7 @@ def exec_dict(
104104
Returns:
105105
Return the final result.
106106
"""
107-
program = Program.model_validate(prog)
107+
program = parse_dict(prog)
108108
result = exec_program(program, config, scope, loc, output)
109109
return result
110110

src/pdl/pdl_parser.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import json
22
from pathlib import Path
3-
from typing import Optional
3+
from typing import Any, Optional
44

55
import yaml
66
from pydantic import ValidationError
77

8-
from .pdl_ast import PDLException, PdlLocationType, Program
8+
from .pdl_ast import PDLException, PdlLocationType, Program, empty_block_location
99
from .pdl_location_utils import get_line_map
1010
from .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

Comments
 (0)