Skip to content

Commit

Permalink
Add json parsers to stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
mulias committed Jul 17, 2024
1 parent 43c060e commit 311a123
Show file tree
Hide file tree
Showing 4 changed files with 335 additions and 80 deletions.
4 changes: 4 additions & 0 deletions docs/stdlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ These parsers and value functions are always available in Possum programs, unles
| `nls` | Alias for `newlines` | As above |
| `whitespace` | One or more space, tab, or newline characters | Matched string |
| `ws` | Alias for `whitespace` | As above |
| `json_string` | Valid JSON string | Matched string contents, not including quotes |

## Number Parsers

Expand Down Expand Up @@ -71,6 +72,7 @@ These parsers and value functions are always available in Possum programs, unles
| `tuple3_sep(elem1, sep1, elem2, sep2, elem3)` | Runs three element parsers, interspersed with `sep` | Array of length 3 containing parsed elements |
| `table_sep(elem, sep, row_sep)` | One or more `elem`, interspersed with `sep` or `row_sep` | Array of array of values parsed by `elem` |
| `maybe_table_sep(elem, sep, row_sep)` | Zero or more `elem`, interspersed with `sep` or `row_sep` | Array of array of values parsed by `elem`, maybe empty |
| `json_array` | Valid JSON array | Array of arbitrary values |

## Object Parsers

Expand All @@ -86,6 +88,7 @@ These parsers and value functions are always available in Possum programs, unles
| `record2_sep(Key1, value1, sep, Key2, value2)` | Parses `value1`, `sep`, and then `value2` | Object with `Key1` associated to the parsed `value1`, etc |
| `record3(Key1, value1, Key2, value2, Key3, value3)` | Runs three value parsers in order | Object with `Key1` associated to the parsed `value1`, etc |
| `record3_sep(Key1, value1, sep1, Key2, value2, sep2, Key3, value3)` | Runs three value parsers, interspersed with `sep` | Object with `Key1` associated to the parsed `value1`, etc |
| `json_object` | Valid JSON object | Object with arbitrary values |

## Utility Parsers

Expand All @@ -110,6 +113,7 @@ These parsers and value functions are always available in Possum programs, unles
| `end_of_input` | End of string or file input | `null` |
| `end` | Alias for `end_of_input` | As above |
| `input(p)` | Strips leading and trailing whitespace, succeeds if `p` parses to end of input | Result of `p` |
| `json` | Any valid JSON | Matched JSON |

## Value Functions

Expand Down
5 changes: 2 additions & 3 deletions examples/advent_2022_day_13.t/input.parser
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
list = "[" > maybe_array_sep(int | list, ",") < "]"
pair = list -> L & nl & list -> R $ {"left": L, "right": R}
array_sep(pair, ws)
pair = record2_sep("left", json_array, nl, "right", json_array)
array_sep(pair, nl+nl)
Loading

0 comments on commit 311a123

Please sign in to comment.