module Drudge::Parsers::Primitives
Parser
primitives
Public Instance Methods
accept(expected = nil, eos_failure_msg: "expected a
click to toggle source
produces a parser that expects the expected
value expected
can is checked using ‘===’ so
accept(String) -> will accept any string accept(2) -> will accept 2 accept(-> v { v / 2 == 4 }) will use the lambda to check the value accept { |v| v / 2 == 4 } is also possible
# File lib/drudge/parsers/primitives.rb, line 23 def accept(expected = nil, eos_failure_msg: "expected a #{expected}", failure_msg: -> value { "'#{value}' doesn't match #{expected}" }, &expected_block) expected = expected_block if expected_block to_message = -> (value, msg) do case msg when String msg when Proc msg[value] end end parser do |input| value, *rest = input case when input.nil? || input.empty? Failure(to_message[value, eos_failure_msg], input) when expected === value Success(Single(value), rest) else Failure(to_message[value, failure_msg], input) end end end
commit(prs)
click to toggle source
Commits the provided parser. If prs
returns a Failure
, it will be converted into an Error
that will stop backtracking inside a ‘|’ operator
# File lib/drudge/parsers/primitives.rb, line 72 def commit(prs) parser do |input| result = prs[input] case result when Success, Error result when Failure Error(result.message, result.remaining) end end.describe(prs.to_s) end
eos(message)
click to toggle source
matches the end of the stream
# File lib/drudge/parsers/primitives.rb, line 59 def eos(message) parser do |input| if input.empty? Success(Empty(), []) else Failure(message, input) end end.describe "" end
parser(&prs)
click to toggle source
a method that helps bulding parsers converts a block into a parser
# File lib/drudge/parsers/primitives.rb, line 12 def parser(&prs) prs.singleton_class.send :include, parser_mixin prs end
success(parse_value)
click to toggle source
returns a parser that always succeeds with the provided ParseValue
# File lib/drudge/parsers/primitives.rb, line 54 def success(parse_value) parser { |input| Success(parse_value, input) }.describe "SUCC: #{parse_value}" end
Protected Instance Methods
parser_mixin()
click to toggle source
Returns the module which is to be mixed in in every constructed parser. Can be overriden to mix in additonal features
# File lib/drudge/parsers/primitives.rb, line 88 def parser_mixin Parser end