Search code examples
rubyparsingtreetop

Ruby Treetop how to include everything that does not match the grammar


I am tying to create a treetop grammar. I have created the rules to match sections in the file that are of interest to me.

grammar Sexp

  rule bodies
    body+
  end

  rule body
    commentPortString (ifdef_blocks / interface)+ (!newLine)
  end
...
end

How do I run this on a file to extract out the bodies and ignore other sections that I dont care about, or do I need to end up writing rules for those sections also?

Thanks in advance


Solution

  • It's a common idiom in PEG grammars to repeatedly match any character . that isn't part of a rule !body. Something like this:

    rule bodies
      ((!body .)* body)+ (!body .)*
    end