Treetop: Can the parser be put inside a module?

Discussion in 'Ruby' started by Chiyuan Zhang, Feb 3, 2008.

  1. 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!
    Chiyuan Zhang, Feb 3, 2008
    #1
    1. Advertising

  2. Chiyuan Zhang

    Phil Tomson Guest

    On 2/3/08, Chiyuan Zhang <> 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
    Phil Tomson, Feb 3, 2008
    #2
    1. Advertising

  3. 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
    Clifford Heath, Feb 3, 2008
    #3
  4. Yes! That's exactly what I'm looking for! Thanks!

    2008/2/4, Clifford Heath <>:
    > 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, Feb 4, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phrogz
    Replies:
    18
    Views:
    234
    IƱaki Baz Castillo
    Apr 22, 2008
  2. David Rose

    when to use PEP treetop vs regexp

    David Rose, Jan 28, 2008, in forum: Ruby
    Replies:
    1
    Views:
    91
    Phrogz
    Jan 28, 2008
  3. Phrogz

    Treetop Email Parser

    Phrogz, Jan 28, 2008, in forum: Ruby
    Replies:
    13
    Views:
    202
    Phil Tomson
    Feb 1, 2008
  4. Lex Williams

    how can I debug treetop grammar?

    Lex Williams, Aug 29, 2008, in forum: Ruby
    Replies:
    3
    Views:
    134
    Lex Williams
    Aug 29, 2008
  5. Young tae Kim

    Treetop Parser whitespace

    Young tae Kim, Mar 9, 2009, in forum: Ruby
    Replies:
    5
    Views:
    110
    Young Tae Kim
    Mar 10, 2009
Loading...

Share This Page