Sha256: 6b5163da35007baeb4ac8bc036c229ae3f3f0f6d2e22616dc1efe846366f65fc

Contents?: true

Size: 1.19 KB

Versions: 148

Compression:

Stored size: 1.19 KB

Contents

# Customizing Parsers

While the default setup of the parsers provided by this Gem should be suitable
for most some developers might want to change parts of it. An example would be
the use of a custom class for nodes instead of `Parser::AST::Node`.

Customizing the AST is done by creating a custom builder class and passing it
to the constructor method of a parser. The default setup comes down to the
following:

    builder = Parser::Builders::Default.new
    parser  = Parser::Ruby19.new(builder)

When creating your own builder class it's best to subclass the default one so
that you don't have to redefine every used method again:

    class MyBuilder < Parser::Builders::Default

    end

    builder = MyBuilder.new
    parser  = Parser::Ruby19.new(builder)

## Custom Node Classes

To use a custom node class you have to override the method
`Parser::Builders::Default#n`:

    class MyBuilder < Parser::Builders::Default
      def n(type, children, location)
        return MyNodeClass.new(type, children, :location => location)
      end
    end

Note that the used class (and corresponding instance) must be compatible with
`Parser::AST::Node` so it's best to subclass it and override/add code where
needed.

Version data entries

148 entries across 133 versions & 13 rubygems

Version Path
passbase-1.3.0 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/doc/CUSTOMIZATION.md
passbase-1.2.0 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/doc/CUSTOMIZATION.md
passbase-1.1.0 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/doc/CUSTOMIZATION.md
passbase-1.0.3 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/doc/CUSTOMIZATION.md
passbase-1.0.2 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/doc/CUSTOMIZATION.md
passbase-1.0.1 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/doc/CUSTOMIZATION.md
passbase-1.0.0 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/doc/CUSTOMIZATION.md
parser-2.7.1.5 doc/CUSTOMIZATION.md
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/parser-2.7.1.4/doc/CUSTOMIZATION.md
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/parser-2.7.0.2/doc/CUSTOMIZATION.md
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/parser-2.7.1.4/doc/CUSTOMIZATION.md
parser-2.7.1.4 doc/CUSTOMIZATION.md
parser-2.7.1.3 doc/CUSTOMIZATION.md
parser-2.7.1.2 doc/CUSTOMIZATION.md
parser-2.7.1.1 doc/CUSTOMIZATION.md
talon_one-2.0.0 vendor/bundle/ruby/2.3.0/gems/parser-2.7.1.0/doc/CUSTOMIZATION.md
talon_one-2.0.0 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.0/doc/CUSTOMIZATION.md
parser-2.7.1.0 doc/CUSTOMIZATION.md
parser-2.7.0.5 doc/CUSTOMIZATION.md
parser-2.7.0.4 doc/CUSTOMIZATION.md