diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-08 00:57:59 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-08 00:57:59 +1300 |
| commit | de03594f5269d7554772c86aeb5d0bba0d2cb600 (patch) | |
| tree | d4935a61202e8e926fb4f0388342744532123d69 /src/parcom.cr | |
| parent | f9148bb172e68e0f584c1040e92f34ea3959b94e (diff) | |
A bunch of stuff
Diffstat (limited to 'src/parcom.cr')
| -rw-r--r-- | src/parcom.cr | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/parcom.cr b/src/parcom.cr index 9063bf9..8835778 100644 --- a/src/parcom.cr +++ b/src/parcom.cr @@ -14,6 +14,12 @@ module Parcom abstract class Parser(T, V) abstract def parse(tokens : Array(T)) : Result(T, V) + def parse?(tokens : Array(T)) : Result(T, V)? + self.parse(tokens) + rescue + return nil + end + def assert(f : V -> Bool) Assert.new(self, f) end @@ -21,6 +27,11 @@ module Parcom def |(other : Parser(T, V)) : Parser(T, V) Alt.new(self, other) end + + # TODO: Find a way to annotate this method's type + def map(f) + Map.new(self, f) + end end class Flunk(T, V) < Parser(T, V) @@ -102,5 +113,63 @@ module Parcom @p2.parse(tokens) end end + + class Map(T, V, U) < Parser(T, U) + def initialize(@p : Parser(T, V), @f : V -> U) + end + + def parse(tokens : Array(T)) : Result(T, U) + result = @p.parse(tokens) + Result.new(result.tokens, @f.call(result.value)) + end + end +end + +class Phrase +end + +class Plus +end + +class Recover +end + +class Optional +end + +class Tokens +end + +class Many +end + +class Some +end + +class Exactly +end + +class AtLeast +end + +class AtMost +end + +class Between +end + +class StopAt +end + +class StopAfter +end + +class StopIf +end + +class FirstOf +end + +class SepBy end |
