Sha256: f1ffdc22ae314cffdc2eb80c45c9350737a22dec200653580f2101a45bfbbda4

Contents?: true

Size: 716 Bytes

Versions: 4

Compression:

Stored size: 716 Bytes

Contents

require 'parser_combinator/string_parser'

class MyParser < ParserCombinator::StringParser
  parser :love_sentence do
    str("I") > str("\s") > str("love") > str("you").onfail("Who do you love?")
  end

  parser :hate_sentence do
    str("I") > str("\s") > str("hate") > str("you").onfail("Who do you hate?")
  end

  parser :sentence do
    love_sentence ^ hate_sentence
  end
end

result = MyParser.sentence.parse_from_string("I love")
puts result.status.message # => Who do you love?

result = MyParser.sentence.parse_from_string("I hate")
puts result.status.message # => Who do you hate?

result = MyParser.sentence.parse_from_string("I am laughing")
puts result.status == nil # => true

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
parser_combinator-0.0.4 examples/error_handling.rb
parser_combinator-0.0.3 examples/error_handling.rb
parser_combinator-0.0.2 examples/error_handling.rb
parser_combinator-0.0.1 examples/error_handling.rb