diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-08 20:03:26 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-08 20:03:26 +1300 |
| commit | 8835f1de0dbc6a059a42fad78d4acbb0984b94e2 (patch) | |
| tree | e0e6322b83fd9f245bdb03dc5ae3a7e2af494f12 /src/parcom.cr | |
| parent | 1724b6a644ee799bd49b6bbefa2b5d345aae14d9 (diff) | |
Implement Plus
Diffstat (limited to 'src/parcom.cr')
| -rw-r--r-- | src/parcom.cr | 85 |
1 files changed, 48 insertions, 37 deletions
diff --git a/src/parcom.cr b/src/parcom.cr index ef1aa2a..8d1d47b 100644 --- a/src/parcom.cr +++ b/src/parcom.cr @@ -59,15 +59,18 @@ module Parcom return nil end - def assert(f : V -> Bool) - Assert.new(self, f) + def |(other : Parser(T, V)) : Alt(T, V) + Alt.new(self, other) end - def |(other : Parser(T, V)) : Parser(T, V) - Alt.new(self, other) + def +(other : Parser(T, U)) : Plus(T, V, U) forall U + Plus.new(self, other) + end + + def assert(f : V -> Bool) + Assert.new(self, f) end - # TODO: Find a way to annotate this method's type def map(f : V -> U) : Map(T, V, U) forall U Map.new(self, f) end @@ -162,53 +165,61 @@ module Parcom Result.new(result.tokens, @f.call(result.value)) end end -end -class Phrase -end + class Phrase + end -class Plus -end + class Plus(T, V, U) < Parser(T, {V, U}) + def initialize(@p1 : Parser(T, V), @p2 : Parser(T, U)) + end -class Recover -end + def parse(tokens : TokenStream(T)) : Result(T, {V, U}) + r1 = @p1.parse(tokens) + r2 = @p2.parse(r1.tokens) + Result.new(r2.tokens, {r1.value, r2.value}) + end + end -class Optional -end + class Recover + end -class Tokens -end + class Optional + end -class Many -end + class Tokens + end -class Some -end + class Many + end -class Exactly -end + class Some + end -class AtLeast -end + class Exactly + end -class AtMost -end + class AtLeast + end -class Between -end + class AtMost + end -class StopAt -end + class Between + end -class StopAfter -end + class StopAt + end -class StopIf -end + class StopAfter + end -class FirstOf -end + class StopIf + end -class SepBy + class FirstOf + end + + class SepBy + end end |
