diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-12 18:53:19 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-12 18:53:19 +1300 |
| commit | 4724a2809f80d3fd7e221ab569ce01fe0161fa9d (patch) | |
| tree | a8f9f8f8a658132eb3514b7d3a5b180a549b468c /src | |
| parent | df556b703bdeb860405a8f4ec1698a032f93af07 (diff) | |
Documentation for Left and Right
Diffstat (limited to 'src')
| -rw-r--r-- | src/parcom.cr | 28 | ||||
| -rw-r--r-- | src/parcom/Left.cr | 26 | ||||
| -rw-r--r-- | src/parcom/Right.cr | 26 |
3 files changed, 52 insertions, 28 deletions
diff --git a/src/parcom.cr b/src/parcom.cr index acb0503..e421e5e 100644 --- a/src/parcom.cr +++ b/src/parcom.cr @@ -81,34 +81,6 @@ module Parcom end end - class Left(T, V, U) < Parser(T, V) - @p : Map(T, {V, U}, V) - - def initialize(p1 : Parser(T, V), p2 : Parser(T, U)) - @p = (p1 + p2).map(&.first) - end - - def parse(tokens : Tokens(T)) : Result(T, V) - @p.parse(tokens) - rescue ex : ParserFail - raise ParserFail.new("Left: #{ex.message}") - end - end - - class Right(T, V, U) < Parser(T, U) - @p : Map(T, {V, U}, U) - - def initialize(p1 : Parser(T, V), p2 : Parser(T, U)) - @p = (p1 + p2).map(&.last) - end - - def parse(tokens : Tokens(T)) : Result(T, U) - @p.parse(tokens) - rescue ex : ParserFail - raise ParserFail.new("Right: #{ex.message}") - end - end - class Recover(T, V) < Parser(T, V) @p : Map(T, V?, V) diff --git a/src/parcom/Left.cr b/src/parcom/Left.cr new file mode 100644 index 0000000..201f497 --- /dev/null +++ b/src/parcom/Left.cr @@ -0,0 +1,26 @@ +require "./parser.cr" +require "./map.cr" + +module Parcom + # `Left` is a `Parser` that tries to parse with two different + # parsers in succession and fails if either of the two parsers fails. + # This parser behaves the same as `Plus`, but only returns the result + # of the first parser. + class Left(T, V, U) < Parser(T, V) + @p : Map(T, {V, U}, V) + + # Accepts the two parsers to use, in order. + def initialize(p1 : Parser(T, V), p2 : Parser(T, U)) + @p = (p1 + p2).map(&.first) + end + + # Tries to parse with the two given parsers, and returns the + # result of the first parser if they both succeed. + def parse(tokens : Tokens(T)) : Result(T, V) + @p.parse(tokens) + rescue ex : ParserFail + raise ParserFail.new("Left: #{ex.message}") + end + end +end + diff --git a/src/parcom/Right.cr b/src/parcom/Right.cr new file mode 100644 index 0000000..202754d --- /dev/null +++ b/src/parcom/Right.cr @@ -0,0 +1,26 @@ +require "./parser.cr" +require "./map.cr" + +module Parcom + # `Right` is a `Parser` that tries to parse with two different + # parsers in succession and fails if either of the two parsers fails. + # This parser behaves the same as `Plus`, but only returns the result + # of the second parser. + class Right(T, V, U) < Parser(T, U) + @p : Map(T, {V, U}, V) + + # Accepts the two parsers to use, in order. + def initialize(p1 : Parser(T, V), p2 : Parser(T, U)) + @p = (p1 + p2).map(&.last) + end + + # Tries to parse with the two given parsers, and returns the + # result of the second parser if they both succeed. + def parse(tokens : Tokens(T)) : Result(T, U) + @p.parse(tokens) + rescue ex : ParserFail + raise ParserFail.new("Left: #{ex.message}") + end + end +end + |
