Class: Rattler::Parsers::Grammar

Inherits:
Util::Node show all
Defined in:
lib/rattler/parsers/grammar.rb

Overview

Grammar represents a parsed grammar

Constant Summary collapse

DEFAULT_PARSER_BASE =

The name of the default parser base class

'Rattler::Runtime::PackratParser'
@@default_opts =
{
  :grammar_name => nil,
  :parser_name => nil,
  :base_name => DEFAULT_PARSER_BASE,
  :requires => [],
  :includes => []
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Util::Node

#==, #[], [], #attrs, #can_equal?, #child, #children, #each, #empty?, #eql?, #inspect, #method_missing, #name, #pretty_print, #pretty_print_cycle, #respond_to?, #same_contents?, #to_graphviz, #with_attrs, #with_attrs!, #with_children

Constructor Details

#initialize(rules, opts = {}) ⇒ Grammar

Returns a new instance of Grammar.

Parameters:

  • rules (RuleSet)

    the parse rules that define the parser

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • start_rule (String) — default: rules.first.name
  • grammar_name (String) — default: nil
  • parser_name (String) — default: nil
  • base_name (String) — default: Rattler::Runtime::RecursiveDescentParser
  • requires (Array<String>) — default: []
  • includes (Array<String>) — default: []


33
34
35
36
37
38
39
40
41
# File 'lib/rattler/parsers/grammar.rb', line 33

def initialize(rules, opts={})
  start_rule =
    opts[:start_rule] || rules.start_rule || (rules.first && rules.first.name)

  super rules.with_attrs(:start_rule => start_rule),
    @@default_opts.merge(:start_rule => start_rule).merge(opts)

  attrs[:name] ||= grammar_name || parser_name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rattler::Util::Node

Class Method Details

.parsed(results, *_) ⇒ Object



20
21
22
23
# File 'lib/rattler/parsers/grammar.rb', line 20

def self.parsed(results, *_) #:nodoc:
  options, rules = results
  self.new(rules, options)
end

Instance Method Details

#analysisAnalysis

Returns a static analysis of the grammar rules.

Returns:

  • (Analysis)

    a static analysis of the grammar rules



52
53
54
# File 'lib/rattler/parsers/grammar.rb', line 52

def analysis
  rules.analysis
end

#rule(name) ⇒ Rule

Returns the parse rule referenced by name.

Parameters:

  • name (Symbol)

    the name of a parse rule in the grammar

Returns:

  • (Rule)

    the parse rule referenced by name



47
48
49
# File 'lib/rattler/parsers/grammar.rb', line 47

def rule(name)
  rules[name]
end

#with_rules(new_rules) ⇒ Grammar

Returns a new grammar with the parse rules replaced by new_rules.

Parameters:

Returns:

  • (Grammar)

    a new grammar with the parse rules replaced by new_rules



59
60
61
# File 'lib/rattler/parsers/grammar.rb', line 59

def with_rules(new_rules)
  self.class.new new_rules, attrs
end