Class: Rattler::Grammar::Grammar

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

Overview

Grammar represents a parsed grammar

Author:

  • Jason Arhart

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 => []
}

Instance Attribute Summary collapse

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, #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
  • 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: []


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rattler/grammar/grammar.rb', line 41

def initialize(rules, opts={})
  super @@default_opts.merge(opts)

  case attrs[:start_rule]
  when Symbol then attrs[:start_rule] = rules[start_rule]
  when nil    then attrs[:start_rule] = rules.first
  end

  @rules = rules.with_attrs(:start_rule => start_rule.name)

  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

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



54
55
56
# File 'lib/rattler/grammar/grammar.rb', line 54

def rules
  @rules
end

Class Method Details

.parsed(results, *_) ⇒ Object



28
29
30
31
# File 'lib/rattler/grammar/grammar.rb', line 28

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

Instance Method Details

#analysisObject



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

def analysis
  rules.analysis
end

#rule(name) ⇒ Object



56
57
58
# File 'lib/rattler/grammar/grammar.rb', line 56

def rule(name)
  rules[name]
end

#with_rules(new_rules) ⇒ Object



64
65
66
# File 'lib/rattler/grammar/grammar.rb', line 64

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