class LL::CompiledGrammar

The CompiledGrammar class contains compilation results such as the parser name, the rules of the grammar, the terminals, etc.

Attributes

errors[R]
header[RW]
inner[RW]
name[RW]
warnings[R]

Public Class Methods

new() click to toggle source
# File lib/ll/compiled_grammar.rb, line 11
def initialize
  @warnings  = []
  @errors    = []
  @terminals = {}
  @rules     = {}
  @inner     = nil
  @header    = nil
end

Public Instance Methods

add_error(message, source_line) click to toggle source

@param [String] message @param [LL::SourceLine] source_line

# File lib/ll/compiled_grammar.rb, line 24
def add_error(message, source_line)
  @errors << Message.new(:error, message, source_line)
end
add_rule(rule) click to toggle source

@param [LL::Rule] rule @return [LL::Rule]

# File lib/ll/compiled_grammar.rb, line 77
def add_rule(rule)
  return @rules[rule.name] = rule
end
add_terminal(name, source_line) click to toggle source

@param [String] name @param [LL::SourceLine] source_line @return [LL::Terminal]

# File lib/ll/compiled_grammar.rb, line 49
def add_terminal(name, source_line)
  return @terminals[name] = Terminal.new(name, source_line)
end
add_warning(message, source_line) click to toggle source

@param [String] message @param [LL::SourceLine] source_line

# File lib/ll/compiled_grammar.rb, line 32
def add_warning(message, source_line)
  @warnings << Message.new(:warning, message, source_line)
end
display_messages() click to toggle source

Displays all warnings and errors.

# File lib/ll/compiled_grammar.rb, line 152
def display_messages
  [:errors, :warnings].each do |type|
    send(type).each do |msg|
      output.puts(msg.to_s)
    end
  end
end
has_rule?(name) click to toggle source

Returns true if a rule for the given name has already been assigned.

@param [String] name @return [TrueClass|FalseClass]

# File lib/ll/compiled_grammar.rb, line 59
def has_rule?(name)
  return @rules.key?(name)
end
has_rule_with_branches?(name) click to toggle source

Returns true if a rule already exists for a given name and has at least 1 branch.

@see [#has_rule?]

# File lib/ll/compiled_grammar.rb, line 69
def has_rule_with_branches?(name)
  return has_rule?(name) && !@rules[name].branches.empty?
end
has_terminal?(name) click to toggle source

@param [String] name @return [TrueClass|FalseClass]

# File lib/ll/compiled_grammar.rb, line 40
def has_terminal?(name)
  return @terminals.key?(name)
end
lookup_identifier(name) click to toggle source

Looks up an identifier from the list of terminals and/or rules. Rules take precedence over terminals.

If no rule/terminal could be found nil is returned instead.

@param [String] name @return [LL::Rule|LL::Terminal|NilClass]

# File lib/ll/compiled_grammar.rb, line 98
def lookup_identifier(name)
  if has_rule?(name)
    ident = lookup_rule(name)
  elsif has_terminal?(name)
    ident = @terminals[name]
  else
    ident = nil
  end

  return ident
end
lookup_rule(name) click to toggle source

@param [String] name @return [LL::Rule]

# File lib/ll/compiled_grammar.rb, line 85
def lookup_rule(name)
  return @rules[name]
end
output() click to toggle source

@return [IO]

# File lib/ll/compiled_grammar.rb, line 163
def output
  return STDERR
end
rule_indices() click to toggle source

@return [Hash]

# File lib/ll/compiled_grammar.rb, line 120
def rule_indices
  return rules.each_with_index.each_with_object({}) do |(rule, idx), h|
    h[rule] = idx
  end
end
rules() click to toggle source

@return [Array]

# File lib/ll/compiled_grammar.rb, line 113
def rules
  return @rules.values
end
terminal_indices() click to toggle source

@return [Hash]

# File lib/ll/compiled_grammar.rb, line 136
def terminal_indices
  return terminals.each_with_index.each_with_object({}) do |(term, idx), h|
    h[term] = idx
  end
end
terminals() click to toggle source

@return [Array]

# File lib/ll/compiled_grammar.rb, line 129
def terminals
  return @terminals.values
end
valid?() click to toggle source

@return [TrueClass|FalseClass]

# File lib/ll/compiled_grammar.rb, line 145
def valid?
  return @errors.empty?
end