簡単な例。rule を一部関数で
#!/usr/bin/env ruby require 'tdp' include TDParser parser = TDParser.define {|g| g.input = (rule(:abc) | rule(:def)) >> proc {|x| x } def abc token("a") - token("b") - token("c") >> proc {|x| x } end def def token("d") - token("e") - token("f") >> proc {|x| x } end def parse(str) tokens = str.split(/\s+/) input.parse(tokens) end } p parser.parse("a b c") p parser.parse("d e f") p parser.parse("abc")
で、
[["a", "b", "c"]] [["d", "e", "f"]] nil