views:

32

answers:

0

I am using treetop to construct a grammar but am having problems with what seems to be a circular dependency. Note: this is not really a treetop question, but more of a question with loading files.

Consider the following structure:

test.rb
lib/A.treetop
lib/B.treetop
lib/AB.treetop
lib/all.rb

And the sources for the files

# test.rb
require File.dirname(__FILE__) + "/lib/all"

# lib/A.treetop
grammar A
  include AB
end

# lib/B.treetop
grammar B
  include AB
end

# lib/AB.treetop
grammar AB
  include A
  include B
end

# lib/all.rb
require 'treetop'

require File.dirname(__FILE__) + '/A'
require File.dirname(__FILE__) + '/B'
require File.dirname(__FILE__) + '/AB'

Now it's very obvious that I've introduced a circular dependency, however it is impossible to construct this grammar without it.

Now when I run test.rb

ruby test.rb

I receive the following error:

/home/eric/.rvm/gems/ruby-1.9.2-rc2/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb:42:in `class_eval': uninitialized constant A::AB (NameError)
....

meaning that it could not include the AB grammar in the A grammar since it's after it in the all.rb file.

How would I resolve this issue?