aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md42
1 files changed, 37 insertions, 5 deletions
diff --git a/README.md b/README.md
index 243c3de..b532ea2 100644
--- a/README.md
+++ b/README.md
@@ -99,20 +99,52 @@ Another way to do this is to use `Parser#recover`, which allows a default value
```
# `#map_const` is like `#map`, but it takes a single value to replace
# the parser value with unconditionally.
-sign = Parser.token('-').map_const(-1_i32).recover(1_i32)
+sign = Parser.token('-')
+ .map_const(-1_i32)
+ .recover(1_i32)
```
Final code:
+TODO: add to practical tests
+
```
-d = Parser(Char, Char).satisfy(&.number?)
-abs_num = d.some.map { |ds| ds.join.to_i32 }
+d = Parser(Char, Char).satisfy(&.number?).named("digit")
+abs_num = d.some.map { |ds| ds.join.to_i32 }.named("abs_num")
-sign = Parser.token('-').map_const(-1_i32).recover(1_i32)
+sign = Parser.token('-').map_const(-1_i32).recover(1_i32).named("sign")
int32 = parser_chain Char, Int32, "int32",
{s, sign},
{n, abs_num},
- finally: Parser.pure(n * s)
+ pure: n * s
+
+ws = Parser(Char, Char).satisfy(&.whitespace?)
+ .many
+ .named("whitespace")
+
+arrow = (ws >> Parser.token_sequence("=>".chars) >> ws).named("arrow")
+
+pair = parser_chain Char, {Int32, Int32}, "pair",
+ {x, int32},
+ {_, sep},
+ {y, int32},
+ pure: {x, y}
+
+delim = (ws >> Parser.token(',') >> ws).named("delim")
+
+elements = parser_chain Char, Array({Int32, Int32}), "elements",
+ {pairs, pair.sep_by(delim)},
+ {_, delim.optional}, # trailing comma
+ pure: pairs
+
+hash_start = (Parser.token('{') >> ws).named("start")
+hash_end = (ws >> Parser.token('}')).named("end")
+
+hash = parser_chain Char, Hash(Int32, Int32), "hash",
+ {_, hash_start},
+ {es, elements.recover([] of {Int32, Int32})}
+ {_, hash_end},
+ pure: es.to_h
```