Skip to content

Commit 45832b0

Browse files
committed
Merge branch 'main' into ppdl-01
2 parents cf80f48 + 03c4859 commit 45832b0

File tree

9 files changed

+39
-8
lines changed

9 files changed

+39
-8
lines changed

docs/tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ Bob lives at the following address:
378378
## Parsing the output of a block
379379

380380
As we saw in the previous section, it is possible to use the `parser: json` setting to parse the result of a block as a JSON.
381-
Other possible values for `parser` are `yaml`, `jsonl`, or `regex`.
381+
Other possible values for `parser` are `yaml`, `jsonl`, `regex`, or `csv`.
382382

383383
The following example extracts using a regular expression parser the code between triple backtick generated by a model:
384384

pdl-live-react/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdl-live-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "PDL",
33
"private": true,
4-
"version": "0.9.0",
4+
"version": "0.9.1",
55
"type": "module",
66
"scripts": {
77
"prod:mac:1": "npm run tauri build -- --no-bundle --target=universal-apple-darwin",

pdl-live-react/src-tauri/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdl-live-react/src-tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pdl"
3-
version = "0.9.0"
3+
version = "0.9.1"
44
description = "Prompt Declaration Language"
55
authors = ["[email protected]"]
66
edition = "2024"

src/pdl/pdl-schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4157,7 +4157,8 @@
41574157
"enum": [
41584158
"json",
41594159
"jsonl",
4160-
"yaml"
4160+
"yaml",
4161+
"csv"
41614162
],
41624163
"type": "string"
41634164
},

src/pdl/pdl_ast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class RegexParser(Parser):
304304

305305

306306
ParserType = TypeAliasType(
307-
"ParserType", Union[Literal["json", "jsonl", "yaml"], PdlParser, RegexParser]
307+
"ParserType", Union[Literal["json", "jsonl", "yaml", "csv"], PdlParser, RegexParser]
308308
)
309309
"""Different parsers."""
310310
OptionalParserType = TypeAliasType("OptionalParserType", Optional[ParserType])

src/pdl/pdl_interpreter.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pylint: disable=import-outside-toplevel
2+
import csv
23
import json
34
import re
45
import shlex
@@ -13,6 +14,7 @@
1314
from abc import ABC, abstractmethod
1415
from concurrent.futures import ThreadPoolExecutor
1516
from functools import partial, reduce
17+
from io import StringIO
1618
from itertools import count
1719
from os import getenv
1820
from pathlib import Path
@@ -2673,6 +2675,18 @@ def parse_result(parser: ParserType, text: str) -> JSONReturnType:
26732675
raise PDLRuntimeParserError(
26742676
f"Attempted to parse ill-formed YAML: {repr(exc)}"
26752677
) from exc
2678+
case "csv":
2679+
try:
2680+
result = []
2681+
reader = csv.reader(StringIO(text))
2682+
for row in reader:
2683+
result.append(row)
2684+
except KeyboardInterrupt as exc:
2685+
raise exc from exc
2686+
except Exception as exc:
2687+
raise PDLRuntimeParserError(
2688+
f"Attempted to parse ill-formed CSV: {repr(exc)}"
2689+
) from exc
26762690
case PdlParser():
26772691
assert False, "TODO"
26782692
case RegexParser(mode="search" | "match" | "fullmatch"):

tests/test_parser.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,19 @@ def test_parser_case2():
172172
"""
173173
result = exec_str(prog)
174174
assert result == ["1", "2", "3", "4"]
175+
176+
177+
def test_parser_csv():
178+
csv_parser = """
179+
text: |
180+
1,Apple,Red
181+
2,Orange,Orange
182+
3,Banana,Yellow
183+
parser: csv
184+
"""
185+
result = exec_str(csv_parser)
186+
assert result == [
187+
["1", "Apple", "Red"],
188+
["2", "Orange", "Orange"],
189+
["3", "Banana", "Yellow"],
190+
]

0 commit comments

Comments
 (0)