Skip to content

Commit 625e1e7

Browse files
authored
Make AST comments executable (#795)
1 parent d3628cf commit 625e1e7

File tree

3 files changed

+82
-22
lines changed

3 files changed

+82
-22
lines changed

pdl-live-react/src/pdl_ast.d.ts

+37-8
Original file line numberDiff line numberDiff line change
@@ -2917,9 +2917,8 @@ export interface Defs3 {
29172917
*
29182918
* Example:
29192919
* ```PDL
2920-
* - def: N
2921-
* lang: python
2922-
* code: |
2920+
* lang: python
2921+
* code: |
29232922
* import random
29242923
* # (In PDL, set `result` to the output you wish for your code block.)
29252924
* result = random.randint(1, 20)
@@ -3201,11 +3200,12 @@ export interface Defs7 {
32013200
*
32023201
* Example:
32033202
* ```PDL
3204-
* - if: ${ eval == 'no' }
3205-
* then:
3206-
* text:
3207-
* - read:
3208-
* message: "Why not?\n"
3203+
* defs:
3204+
* answer:
3205+
* read:
3206+
* message: "Enter a number? "
3207+
* if: ${ (answer | int) == 42 }
3208+
* then: You won!
32093209
* ```
32103210
*/
32113211
export interface IfBlock {
@@ -3270,6 +3270,23 @@ export interface Defs8 {
32703270
}
32713271
/**
32723272
* Match control structure.
3273+
*
3274+
* Example:
3275+
* ```PDL
3276+
* defs:
3277+
* answer:
3278+
* read:
3279+
* message: "Enter a number? "
3280+
* match: ${ (answer | int) }
3281+
* with:
3282+
* - case: 42
3283+
* then: You won!
3284+
* - case:
3285+
* any:
3286+
* def: x
3287+
* if: ${ x > 42 }
3288+
* then: Too high
3289+
* - then: Too low
32733290
*/
32743291
export interface MatchBlock {
32753292
description?: Description9
@@ -3706,6 +3723,18 @@ export interface Defs15 {
37063723
}
37073724
/**
37083725
* Read from a file or standard input.
3726+
*
3727+
* Example. Read from the standard input with a prompt starting with `> `.
3728+
* ```PDL
3729+
* read:
3730+
* message: "> "
3731+
* ```
3732+
*
3733+
* Example. Read the file `./data.yaml` in the same directory of the PDL file containing the block and parse it into YAML.
3734+
* ```PDL
3735+
* read: ./data.yaml
3736+
* parser: yaml
3737+
* ```
37093738
*/
37103739
export interface ReadBlock {
37113740
description?: Description16

src/pdl/pdl-schema.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@
13491349
},
13501350
"CodeBlock": {
13511351
"additionalProperties": false,
1352-
"description": "Execute a piece of code.\n\nExample:\n```PDL\n- def: N\n lang: python\n code: |\n import random\n # (In PDL, set `result` to the output you wish for your code block.)\n result = random.randint(1, 20)\n```",
1352+
"description": "Execute a piece of code.\n\nExample:\n```PDL\nlang: python\ncode: |\n import random\n # (In PDL, set `result` to the output you wish for your code block.)\n result = random.randint(1, 20)\n```",
13531353
"properties": {
13541354
"description": {
13551355
"anyOf": [
@@ -4318,7 +4318,7 @@
43184318
},
43194319
"IfBlock": {
43204320
"additionalProperties": false,
4321-
"description": "Conditional control structure.\n\nExample:\n```PDL\n- if: ${ eval == 'no' }\n then:\n text:\n - read:\n message: \"Why not?\\n\"\n```",
4321+
"description": "Conditional control structure.\n\nExample:\n```PDL\ndefs:\n answer:\n read:\n message: \"Enter a number? \"\nif: ${ (answer | int) == 42 }\nthen: You won!\n```",
43224322
"properties": {
43234323
"description": {
43244324
"anyOf": [
@@ -7186,7 +7186,7 @@
71867186
},
71877187
"MatchBlock": {
71887188
"additionalProperties": false,
7189-
"description": "Match control structure.",
7189+
"description": "Match control structure.\n\nExample:\n```PDL\ndefs:\n answer:\n read:\n message: \"Enter a number? \"\nmatch: ${ (answer | int) }\nwith:\n- case: 42\n then: You won!\n- case:\n any:\n def: x\n if: ${ x > 42 }\n then: Too high\n- then: Too low",
71907190
"properties": {
71917191
"description": {
71927192
"anyOf": [
@@ -9208,7 +9208,7 @@
92089208
},
92099209
"ReadBlock": {
92109210
"additionalProperties": false,
9211-
"description": "Read from a file or standard input.",
9211+
"description": "Read from a file or standard input.\n\nExample. Read from the standard input with a prompt starting with `> `.\n```PDL\nread:\nmessage: \"> \"\n```\n\nExample. Read the file `./data.yaml` in the same directory of the PDL file containing the block and parse it into YAML.\n```PDL\nread: ./data.yaml\nparser: yaml\n```",
92129212
"properties": {
92139213
"description": {
92149214
"anyOf": [

src/pdl/pdl_ast.py

+41-10
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,8 @@ class CodeBlock(BaseCodeBlock):
466466
467467
Example:
468468
```PDL
469-
- def: N
470-
lang: python
471-
code: |
469+
lang: python
470+
code: |
472471
import random
473472
# (In PDL, set `result` to the output you wish for your code block.)
474473
result = random.randint(1, 20)
@@ -600,11 +599,12 @@ class IfBlock(StructuredBlock):
600599
601600
Example:
602601
```PDL
603-
- if: ${ eval == 'no' }
604-
then:
605-
text:
606-
- read:
607-
message: "Why not?\\n"
602+
defs:
603+
answer:
604+
read:
605+
message: "Enter a number? "
606+
if: ${ (answer | int) == 42 }
607+
then: You won!
608608
```
609609
"""
610610

@@ -642,7 +642,25 @@ class MatchCase(BaseModel):
642642

643643

644644
class MatchBlock(StructuredBlock):
645-
"""Match control structure."""
645+
"""Match control structure.
646+
647+
Example:
648+
```PDL
649+
defs:
650+
answer:
651+
read:
652+
message: "Enter a number? "
653+
match: ${ (answer | int) }
654+
with:
655+
- case: 42
656+
then: You won!
657+
- case:
658+
any:
659+
def: x
660+
if: ${ x > 42 }
661+
then: Too high
662+
- then: Too low
663+
"""
646664

647665
kind: Literal[BlockKind.MATCH] = BlockKind.MATCH
648666
match_: ExpressionType[Any] = Field(alias="match")
@@ -735,7 +753,20 @@ class RepeatBlock(StructuredBlock):
735753

736754

737755
class ReadBlock(LeafBlock):
738-
"""Read from a file or standard input."""
756+
"""Read from a file or standard input.
757+
758+
Example. Read from the standard input with a prompt starting with `> `.
759+
```PDL
760+
read:
761+
message: "> "
762+
```
763+
764+
Example. Read the file `./data.yaml` in the same directory of the PDL file containing the block and parse it into YAML.
765+
```PDL
766+
read: ./data.yaml
767+
parser: yaml
768+
```
769+
"""
739770

740771
kind: Literal[BlockKind.READ] = BlockKind.READ
741772
read: ExpressionType[str] | None

0 commit comments

Comments
 (0)