Class: Ibex::Frontend::DSL::RuleBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/frontend/dsl.rb

Overview

Collects alternatives for one nonterminal.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grammar, default_location) ⇒ RuleBuilder

Returns a new instance of RuleBuilder.



235
236
237
238
239
# File 'lib/ibex/frontend/dsl.rb', line 235

def initialize(grammar, default_location)
  @grammar = grammar
  @default_location = default_location
  @alternatives = [] #: Array[AST::Alternative]
end

Instance Attribute Details

#alternativesObject (readonly)

: Array



229
230
231
# File 'lib/ibex/frontend/dsl.rb', line 229

def alternatives
  @alternatives
end

Instance Method Details

#alt(*items, action: nil, precedence: nil, node: nil) ⇒ Object



242
243
244
245
246
247
248
249
250
251
# File 'lib/ibex/frontend/dsl.rb', line 242

def alt(*items, action: nil, precedence: nil, node: nil)
  location = @grammar.next_location || @default_location
  normalized = items.map { |item| @grammar.normalize_item(item) }
  action_node = action && AST::InlineAction.new(code: action.to_s, loc: location)
  alternative = AST::Alternative.new(
    items: normalized, action: action_node, precedence: precedence&.to_s,
    node_annotation: node, loc: location
  )
  @alternatives << alternative
end