From de03594f5269d7554772c86aeb5d0bba0d2cb600 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Wed, 8 Mar 2023 00:57:59 +1300 Subject: A bunch of stuff --- src/parcom.cr | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'src') 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 -- cgit v1.2.1