[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Treetop: Can the parser be put inside a module?

Chiyuan Zhang

2/3/2008 2:53:00 PM

Hi!

I'm wondering if Treetop can provide (or is it already have?) a
mechanize like this:

module MyModule
Treetop.load("my")
end

so that I'll get a MyModule::MyParser instead of a top-level MyParser.
Or something like:

Treetop.load_into("my", MyModule)

which might be implemented like the original `load':

# def self.load(path)
def self.load_into(path, module)
adjusted_path = path =~ /\.(treetop|tt)\Z/ ? path : path + '.treetop'
compiler = Treetop::Compiler::GrammarCompiler.new
# Object.class_eval(compiler.ruby_source(adjusted_path))
module.class_eval(compiler.ruby_source(adjusted_path))
end

Any idea? Thanks!

3 Answers

rubyfan

2/3/2008 7:08:00 PM

0

On 2/3/08, Chiyuan Zhang <pluskid@gmail.com> wrote:
> Hi!
>
> I'm wondering if Treetop can provide (or is it already have?) a
> mechanize like this:
>
> module MyModule
> Treetop.load("my")
> end
>
> so that I'll get a MyModule::MyParser instead of a top-level MyParser.
> Or something like:
>
> Treetop.load_into("my", MyModule)
>
> which might be implemented like the original `load':
>
> # def self.load(path)
> def self.load_into(path, module)
> adjusted_path = path =~ /\.(treetop|tt)\Z/ ? path : path + '.treetop'
> compiler = Treetop::Compiler::GrammarCompiler.new
> # Object.class_eval(compiler.ruby_source(adjusted_path))
> module.class_eval(compiler.ruby_source(adjusted_path))
> end
>
> Any idea? Thanks!
>
>

You can use tt to generate the parsing module:

tt my.treetop

Then to get it into your MyModule:

require 'my'
module MyModule
include My
end

Clifford Heath

2/3/2008 8:22:00 PM

0

Chiyuan Zhang wrote:
> I'm wondering if Treetop can provide (or is it already have?) a
> mechanize like this:
>
> module MyModule
> Treetop.load("my")
> end
> so that I'll get a MyModule::MyParser instead of a top-level MyParser.

The Treetop meta-grammar allows you to define your grammar inside a
module, even more than one level. The parser will be emitted inside
a module of the same name.

module MyModule
grammar MyLang
rule top
...
end
end
end

Clifford Heath

Chiyuan Zhang

2/4/2008 3:17:00 AM

0

Yes! That's exactly what I'm looking for! Thanks!

2008/2/4, Clifford Heath <no@spam.please.net>:
> Chiyuan Zhang wrote:
> > I'm wondering if Treetop can provide (or is it already have?) a
> > mechanize like this:
> >
> > module MyModule
> > Treetop.load("my")
> > end
> > so that I'll get a MyModule::MyParser instead of a top-level MyParser.
>
> The Treetop meta-grammar allows you to define your grammar inside a
> module, even more than one level. The parser will be emitted inside
> a module of the same name.
>
> module MyModule
> grammar MyLang
> rule top
> ...
> end
> end
> end
>
> Clifford Heath
>
>