Sha256: a84511d23cbaf06995d9a4689e9ab787855ca8aa9689a9f9e5bd3aec6de28ee1
Contents?: true
Size: 1.17 KB
Versions: 4
Compression:
Stored size: 1.17 KB
Contents
require 'parser_combinator/string_parser' class MyParser < ParserCombinator::StringParser parser :noun do str("I") | str("you") | str("it") end parser :verb do str("love") | str("hate") | str("live") | str("die") end parser :token do |p| many(str("\s")) > p < many(str("\s")) end parser :sentence_way1 do token(noun) >> proc{|n1| token(verb) >> proc{|v| token(noun) >> proc{|n2| ok("You said, '#{n1} #{v} #{n2}'") }}} end parser :sentence_way2 do seq(token(noun), token(verb), token(noun)).map do |x| "You said, '#{x[0]} #{x[1]} #{x[2]}'" end end parser :sentence_way3 do seq(token(noun).name(:a), token(verb).name(:b), token(noun).name(:c)).map do |x| "You said, '#{x[:a]} #{x[:b]} #{x[:c]}'" end end end result = MyParser.sentence_way1.parse_from_string("I love you") puts result.parsed # => You said, 'I love you.' result = MyParser.sentence_way2.parse_from_string("I love you") puts result.parsed # => You said, 'I love you.' result = MyParser.sentence_way3.parse_from_string("I love you") puts result.parsed # => You said, 'I love you.'
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
parser_combinator-0.0.4 | examples/basic.rb |
parser_combinator-0.0.3 | examples/basic.rb |
parser_combinator-0.0.2 | examples/basic.rb |
parser_combinator-0.0.1 | examples/basic.rb |