aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-14 23:13:40 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-14 23:13:40 +1300
commit9407354b36fbae0f7349ea2e2c47f847fee03f11 (patch)
tree5c982661c51f4dc8b632156c2c885ccf3127e21f
parent77c370d27be174e0b036b33d1469e84e67a7153a (diff)
Change Parser from a Class to a Module
-rw-r--r--src/parcom/alt.cr3
-rw-r--r--src/parcom/any_token.cr4
-rw-r--r--src/parcom/assert.cr4
-rw-r--r--src/parcom/at_least.cr4
-rw-r--r--src/parcom/at_most.cr4
-rw-r--r--src/parcom/between.cr4
-rw-r--r--src/parcom/eof.cr4
-rw-r--r--src/parcom/exactly.cr4
-rw-r--r--src/parcom/first_of.cr4
-rw-r--r--src/parcom/flunk.cr4
-rw-r--r--src/parcom/left.cr4
-rw-r--r--src/parcom/many.cr4
-rw-r--r--src/parcom/map.cr4
-rw-r--r--src/parcom/optional.cr4
-rw-r--r--src/parcom/parser.cr2
-rw-r--r--src/parcom/peek.cr4
-rw-r--r--src/parcom/phrase.cr4
-rw-r--r--src/parcom/plus.cr4
-rw-r--r--src/parcom/recover.cr4
-rw-r--r--src/parcom/right.cr4
-rw-r--r--src/parcom/satisfy.cr4
-rw-r--r--src/parcom/sep_by.cr4
-rw-r--r--src/parcom/sequence.cr4
-rw-r--r--src/parcom/some.cr4
-rw-r--r--src/parcom/token.cr4
-rw-r--r--src/parcom/token_seq.cr4
26 files changed, 75 insertions, 26 deletions
diff --git a/src/parcom/alt.cr b/src/parcom/alt.cr
index dedd41d..68ab506 100644
--- a/src/parcom/alt.cr
+++ b/src/parcom/alt.cr
@@ -5,7 +5,8 @@ module Parcom
# to parse with one of them.
# If the first (left) parser succeeds, its result is returned.
# If the first parser fails, it will try the second (right) parser.
- class Alt(T, V) < Parser(T, V)
+ class Alt(T, V)
+ include Parser(T, V)
# Accepts the two parsers to try to parse with.
def initialize(@p1 : Parser(T, V), @p2 : Parser(T, V))
end
diff --git a/src/parcom/any_token.cr b/src/parcom/any_token.cr
index 1f65bfc..d4800fd 100644
--- a/src/parcom/any_token.cr
+++ b/src/parcom/any_token.cr
@@ -4,7 +4,9 @@ module Parcom
# `AnyToken` is a `Parser` that consumes exactly one token from
# the input stream and returns it.
# It fails if the input stream is empty.
- class AnyToken(T) < Parser(T, T)
+ class AnyToken(T)
+ include Parser(T, T)
+
# Parses the first token in the input, fails if the input is empty.
def parse(tokens : Tokens(T)) : Result(T, T)
if tokens.empty?
diff --git a/src/parcom/assert.cr b/src/parcom/assert.cr
index 5a3e621..cfe6855 100644
--- a/src/parcom/assert.cr
+++ b/src/parcom/assert.cr
@@ -20,7 +20,9 @@ module Parcom
# # This parser is identical to the above example.
# letter = AnyToken.new(Char).assert { |x| x.letter? }
# ```
- class Assert(T, V) < Parser(T, V)
+ class Assert(T, V)
+ include Parser(T, V)
+
# Accepts the parser to run and a `Bool`-retuning block
# containing the test to perform on the other parser's result.
def initialize(@p : Parser(T, V), &block : V -> Bool)
diff --git a/src/parcom/at_least.cr b/src/parcom/at_least.cr
index 2fb8dcf..dc43d11 100644
--- a/src/parcom/at_least.cr
+++ b/src/parcom/at_least.cr
@@ -7,7 +7,9 @@ module Parcom
# a specific number of times. The results of each successful parse
# are returned in an array. If the number of successes is less than
# the specified number, the parser fails.
- class AtLeast(T, V) < Parser(T, Array(V))
+ class AtLeast(T, V)
+ include Parser(T, Array(V))
+
@p : Map(T, {Array(V), Array(V)}, Array(V))
# Accepts the number of parsing attempts, and the parser to use.
diff --git a/src/parcom/at_most.cr b/src/parcom/at_most.cr
index b70164f..c44ef11 100644
--- a/src/parcom/at_most.cr
+++ b/src/parcom/at_most.cr
@@ -9,7 +9,9 @@ module Parcom
# array. If the specific number of parses succeed, the parsing stops, even
# if it is possible to keep parsing. If the parser is never successful, it
# succeeds with an empty array.
- class AtMost(T, V) < Parser(T, Array(V))
+ class AtMost(T, V)
+ include Parser(T, Array(V))
+
@p : Map(T, Array(V?), Array(V))
# Accepts the number of parsing attempts, and the parser to use.
diff --git a/src/parcom/between.cr b/src/parcom/between.cr
index 05519e4..f2d5986 100644
--- a/src/parcom/between.cr
+++ b/src/parcom/between.cr
@@ -10,7 +10,9 @@ module Parcom
# the lower bound of the range, the parser fails. If the number of successful
# parses reaches thhe upper bound of the range, the parsing stops, even if it
# is possible to keep parsing.
- class Between(T, V) < Parser(T, Array(V))
+ class Between(T, V)
+ include Parser(T, Array(V))
+
@p : Map(T, {Array(V), Array(V)}, Array(V))
# Accepts the lower and upper bounds for the number of parsing attempts,
diff --git a/src/parcom/eof.cr b/src/parcom/eof.cr
index 650da56..64dbaca 100644
--- a/src/parcom/eof.cr
+++ b/src/parcom/eof.cr
@@ -8,7 +8,9 @@ module Parcom
#
# This parser is also one of the few cases where it is ideal to not
# modify or take from the input stream, as it should be empty anyway.
- class EOF(T) < Parser(T, Nil)
+ class EOF(T)
+ include Parser(T, Nil)
+
# Succeeds is the input stream is empty and returns `nil`.
# Raises a `ParserFail` exception when the input is not empty.
def parse(tokens : Tokens(T)) : Result(T, Nil)
diff --git a/src/parcom/exactly.cr b/src/parcom/exactly.cr
index a34fd0d..85c71d1 100644
--- a/src/parcom/exactly.cr
+++ b/src/parcom/exactly.cr
@@ -5,7 +5,9 @@ module Parcom
# a specific number of times. The results of each successful parse
# are returned in an array. If the number of successes is less than
# OR greater than the specified number, the parser fails.
- class Exactly(T, V) < Parser(T, Array(V))
+ class Exactly(T, V)
+ include Parser(T, Array(V))
+
@p : Sequence(T, V)
# Accepts the number of parsing attempts, and the parser to use.
diff --git a/src/parcom/first_of.cr b/src/parcom/first_of.cr
index c4077eb..a41168b 100644
--- a/src/parcom/first_of.cr
+++ b/src/parcom/first_of.cr
@@ -5,7 +5,9 @@ module Parcom
# with all of them, in order. As soon as one of the parsers succeeds,
# that parser's result is returned. If none of the parsers are successful,
# the parsing fails.
- class FirstOf(T, V) < Parser(T, V)
+ class FirstOf(T, V)
+ include Parser(T, V)
+
@p : Parser(T, V)
# Accepts the parsers to use. Raises an `ArgumentError` if
diff --git a/src/parcom/flunk.cr b/src/parcom/flunk.cr
index 00a0bd6..6a784e7 100644
--- a/src/parcom/flunk.cr
+++ b/src/parcom/flunk.cr
@@ -2,7 +2,9 @@ require "./parser.cr"
module Parcom
# `Flunk` is a `Parser` that always fails.
- class Flunk(T, V) < Parser(T, V)
+ class Flunk(T, V)
+ include Parser(T, V)
+
# Always raises `ParserFail`
def parse(tokens) : Result(T, V)
raise ParserFail.new("Flunk: parsing failed")
diff --git a/src/parcom/left.cr b/src/parcom/left.cr
index 201f497..d9a5ad5 100644
--- a/src/parcom/left.cr
+++ b/src/parcom/left.cr
@@ -6,7 +6,9 @@ module Parcom
# 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)
+ class Left(T, V, U)
+ include Parser(T, V)
+
@p : Map(T, {V, U}, V)
# Accepts the two parsers to use, in order.
diff --git a/src/parcom/many.cr b/src/parcom/many.cr
index a734c63..c4a08c5 100644
--- a/src/parcom/many.cr
+++ b/src/parcom/many.cr
@@ -9,7 +9,9 @@ module Parcom
#
# `Many` will return an empty array if the parser never succeeds or consumes
# input. For cases where at least one parserr success is needed, use `Some`.
- class Many(T, V) < Parser(T, Array(V))
+ class Many(T, V)
+ include Parser(T, Array(V))
+
# Accepts the parser to use.
def initialize(@p : Parser(T, V))
end
diff --git a/src/parcom/map.cr b/src/parcom/map.cr
index 34961b5..9f37f8e 100644
--- a/src/parcom/map.cr
+++ b/src/parcom/map.cr
@@ -11,7 +11,9 @@ module Parcom
# result = parse_i32.parse(Tokens.from_string("1234"))
# result.value # => 1234 (Int32)
# ```
- class Map(T, V, U) < Parser(T, U)
+ class Map(T, V, U)
+ include Parser(T, U)
+
# Accepts the parser to use and the function to apply to the result.
def initialize(@p : Parser(T, V), &block : V -> U)
@f = block
diff --git a/src/parcom/optional.cr b/src/parcom/optional.cr
index b368d4e..c23097b 100644
--- a/src/parcom/optional.cr
+++ b/src/parcom/optional.cr
@@ -20,7 +20,9 @@ module Parcom
# positive_int.parse(input1)
# positive_int.parse(input2)
# ```
- class Optional(T, V) < Parser(T, V?)
+ class Optional(T, V)
+ include Parser(T, V?)
+
# Accepts the parser to use.
def initialize(@p : Parser(T, V))
end
diff --git a/src/parcom/parser.cr b/src/parcom/parser.cr
index ca350df..cd95ac9 100644
--- a/src/parcom/parser.cr
+++ b/src/parcom/parser.cr
@@ -8,7 +8,7 @@ module Parcom
# operate on (usually `Char`).
# The generic type `V` represents the type of value that a parser
# will return when parsing is successful.
- abstract class Parser(T, V)
+ module Parser(T, V)
# Accepts a `Tokens(T)` and either retuns a `Result(T, V)`, or raises a
# `ParserFail` exception with a message providing context of where
# in the chain the parsing failed.
diff --git a/src/parcom/peek.cr b/src/parcom/peek.cr
index 2b6f657..83341ee 100644
--- a/src/parcom/peek.cr
+++ b/src/parcom/peek.cr
@@ -3,7 +3,9 @@ require "./parser.cr"
module Parcom
# `Peek` is a `Parser` that runs another `Parser` while
# leaving the input stream unmodified.
- class Peek(T, V) < Parser(T, V)
+ class Peek(T, V)
+ include Parser(T, V)
+
# Accepts the parser to run.
def initialize(@p : Parser(T, V))
end
diff --git a/src/parcom/phrase.cr b/src/parcom/phrase.cr
index 1996fd4..4c5116d 100644
--- a/src/parcom/phrase.cr
+++ b/src/parcom/phrase.cr
@@ -13,7 +13,9 @@ module Parcom
# one_a = Phrase(Char, Char).new(letter_a)
# result = one_a.parse(tokens) # This fails
# ```
- class Phrase(T, V) < Parser(T, V)
+ class Phrase(T, V)
+ include Parser(T, V)
+
@p : Map(T, {V, Nil}, V)
# Accepts the parser to parse with.
diff --git a/src/parcom/plus.cr b/src/parcom/plus.cr
index 53c9b4f..1b6b558 100644
--- a/src/parcom/plus.cr
+++ b/src/parcom/plus.cr
@@ -27,7 +27,9 @@ module Parcom
#
# If you need to parse more than two things in this manner,
# consider using `Many`, `Some`, `Sequence`, or `TokenSeq` instead.
- class Plus(T, V, U) < Parser(T, {V, U})
+ class Plus(T, V, U)
+ include Parser(T, {V, U})
+
# Accepts the two parsers to use, in order.
def initialize(@p1 : Parser(T, V), @p2 : Parser(T, U))
end
diff --git a/src/parcom/recover.cr b/src/parcom/recover.cr
index 378f8d6..9948230 100644
--- a/src/parcom/recover.cr
+++ b/src/parcom/recover.cr
@@ -7,7 +7,9 @@ module Parcom
# but does not fail the parser chain if the wrapped parser fails.
# If the wrapped parser fails, a `Recover` will not modify the input
# stream, and return an arbitrary value of type `V` instead.
- class Recover(T, V) < Parser(T, V)
+ class Recover(T, V)
+ include Parser(T, V)
+
@p : Map(T, V?, V)
# Accepts the parser to use, and the default value
diff --git a/src/parcom/right.cr b/src/parcom/right.cr
index a0489b1..21ac843 100644
--- a/src/parcom/right.cr
+++ b/src/parcom/right.cr
@@ -6,7 +6,9 @@ module Parcom
# 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)
+ class Right(T, V, U)
+ include Parser(T, U)
+
@p : Map(T, {V, U}, U)
# Accepts the two parsers to use, in order.
diff --git a/src/parcom/satisfy.cr b/src/parcom/satisfy.cr
index 9734635..560c71f 100644
--- a/src/parcom/satisfy.cr
+++ b/src/parcom/satisfy.cr
@@ -12,7 +12,9 @@ module Parcom
# letter_assert = Assert.new(AnyToken(Char).new) { |x| x.letter? }
# letter_satisfy = Satisfy(Char).new { |x| x.letter? }
# ```
- class Satisfy(T) < Parser(T, T)
+ class Satisfy(T)
+ include Parser(T, T)
+
@p : Assert(T, T)
# Accepts the `Bool`-returning block containing the test
diff --git a/src/parcom/sep_by.cr b/src/parcom/sep_by.cr
index fa19027..16bdbc8 100644
--- a/src/parcom/sep_by.cr
+++ b/src/parcom/sep_by.cr
@@ -16,7 +16,9 @@ module Parcom
# result = p.parse(tokens)
# puts result.value # => ['a', 'a', 'a', 'a', 'a']
# ```
- class SepBy(T, V, U) < Parser(T, Array(V))
+ class SepBy(T, V, U)
+ include Parser(T, Array(V))
+
@p : Map(T, {V, Array(V)}, Array(V))
# Accepts the parser that parses the result values, and the
diff --git a/src/parcom/sequence.cr b/src/parcom/sequence.cr
index 6a05cde..5b9144e 100644
--- a/src/parcom/sequence.cr
+++ b/src/parcom/sequence.cr
@@ -6,7 +6,9 @@ module Parcom
# succeed, the values parsed are returned in an array, in the order
# they were parsed in. If any of the parsers fail,
# the `Sequence` also fails.
- class Sequence(T, V) < Parser(T, Array(V))
+ class Sequence(T, V)
+ include Parser(T, Array(V))
+
# Accepts the parsers to use.
def initialize(@ps : Iterable(Parser(T, V)))
end
diff --git a/src/parcom/some.cr b/src/parcom/some.cr
index a2e3563..5f4b9fd 100644
--- a/src/parcom/some.cr
+++ b/src/parcom/some.cr
@@ -13,7 +13,9 @@ module Parcom
# `Some` will raise a `ParserFail` exception if the parser never succeeds
# or consumes input. For cases where parsing should allow for 0 successes,
# use `Many`.
- class Some(T, V) < Parser(T, Array(V))
+ class Some(T, V)
+ include Parser(T, Array(V))
+
@p : Assert(T, Array(V))
# Accepts the parser to use.
diff --git a/src/parcom/token.cr b/src/parcom/token.cr
index b4e1fef..a3e1a53 100644
--- a/src/parcom/token.cr
+++ b/src/parcom/token.cr
@@ -3,7 +3,9 @@ require "./satisfy"
module Parcom
# `Token` is a `Parser` that tries to parse a specific token
# from the input stream.
- class Token(T) < Parser(T, T)
+ class Token(T)
+ include Parser(T, T)
+
# Accepts the token that this parser will try to parse.
def initialize(@expected : T)
@p = Satisfy(T).new { |x| x == @expected }
diff --git a/src/parcom/token_seq.cr b/src/parcom/token_seq.cr
index 45900f9..5df9097 100644
--- a/src/parcom/token_seq.cr
+++ b/src/parcom/token_seq.cr
@@ -16,7 +16,9 @@ module Parcom
# puts result.value # => ['t', 'e', 's', 't']
# puts result.tokens # => ['i', 'n', 'g']
# ```
- class TokenSeq(T) < Parser(T, Array(T))
+ class TokenSeq(T)
+ include Parser(T, Array(T))
+
@p : Sequence(T, T)
# Accepts the tokens to try and parse, in order.