I'm trying to learn treetop and was taking most of the code from https://github.com/survival/lordbishop for parsing names and was going to build from that.
My structure is a bit different because I'm building it in rails, rather than ruby command line.
When I run a very simple parse, I have a parse error being returned on a space (which should be one of the simpler things in my grammar. What am I doing wrong?
My code is fairly simple, in my model
require 'treetop' require 'polyglot' require 'grammars/name' class Name def self.parse(data) parser = FullNameParser.new tree = parser.parse(data) if tree.nil? return "Parse error at offset: #{parser.index}" end result_hash = {} tree.value.each do |node| result_hash[node[0] = node[1].strip if node.is_a?(Array) && !node[1].blank? end return result_hash end end
I've stripped most of the grammar down to just getting words and spaces
grammar FullName rule word [^\s]+ { def value text_value end } end rule s [\s]+ { def value "" end } end end
I'm trying to parse 'john smith',i was hoping to just get back words and spaces and build my logic from there, but I'm stuck at even this simple level. Any suggestions??