# Treetop: Can the parser be put inside a module?

**URL:** <https://www.ruby-forum.com/t/treetop-can-the-parser-be-put-inside-a-module/127972>\
**Category:** Ruby\
**Created:** [February 3, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/treetop-can-the-parser-be-put-inside-a-module/127972 "2008-02-03T15:54:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chiyuan\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9e8a1a/32.png) [@Chiyuan\_Z](https://www.ruby-forum.com/u/Chiyuan_Z)\
**Post date:** [February 3, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/treetop-can-the-parser-be-put-inside-a-module/127972/1 "2008-02-03T15:54:14Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Chiyuan\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9e8a1a/32.png) [@Chiyuan\_Z](https://www.ruby-forum.com/u/Chiyuan_Z)\
**Post date:** [February 3, 2008, 8:08pm UTC](https://www.ruby-forum.com/t/treetop-can-the-parser-be-put-inside-a-module/127972/2 "2008-02-03T20:08:43Z")

</div>

On 2/3/08, Chiyuan Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Or something like:  
> 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

---

<div class="post-metadata">

**Author:** ![Chiyuan\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9e8a1a/32.png) [@Chiyuan\_Z](https://www.ruby-forum.com/u/Chiyuan_Z)\
**Post date:** [February 3, 2008, 9:27pm UTC](https://www.ruby-forum.com/t/treetop-can-the-parser-be-put-inside-a-module/127972/3 "2008-02-03T21:27:31Z")

</div>

Chiyuan Z. 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 H.

---

<div class="post-metadata">

**Author:** ![Chiyuan\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9e8a1a/32.png) [@Chiyuan\_Z](https://www.ruby-forum.com/u/Chiyuan_Z)\
**Post date:** [February 4, 2008, 4:17am UTC](https://www.ruby-forum.com/t/treetop-can-the-parser-be-put-inside-a-module/127972/4 "2008-02-04T04:17:34Z")

</div>

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

2008/2/4, Clifford H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
