Method: RLTK::Parser.clause

Defined in:
lib/rltk/parser.rb

.clause(expression, precedence = nil, &action) ⇒ void Also known as: c

This method returns an undefined value.

Declares a new clause inside of a production. The right-hand side is specified by expression and the precedence of this production can be changed by setting the precedence argument to some terminal symbol.

Parameters:

  • expression (String)

    Right-hand side of a production.

  • precedence (Symbol) (defaults to: nil)

    Symbol representing the precedence of this production.

  • action (Proc)

    Action to be taken when the production is reduced.



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/rltk/parser.rb', line 353

def clause(expression, precedence = nil, &action)
  # Use the curr_prec only if it isn't overridden for this
  # clause.
  precedence ||= @curr_prec
  
  production = @grammar.clause(expression)
  
  # Check to make sure the action's arity matches the number
  # of symbols on the right-hand side.
  if @args == :splat and action.arity != production.rhs.length
    raise ParserConstructionException, 'Incorrect number of arguments to action.  Action arity must match the number of ' +
      'terminals and non-terminals in the clause.'
  end
  
  # Add the action to our proc list.
  @procs[production.id] = [action, production.rhs.length]
  
  # If no precedence is specified use the precedence of the
  # last terminal in the production.
  @production_precs[production.id] = precedence || production.last_terminal
end