Class: Citrus::Choice

Inherits:
Object show all
Includes:
Nonterminal
Defined in:
lib/citrus.rb

Overview

A Choice is a Nonterminal where only one rule must match. The Citrus notation is two or more expressions separated by a vertical bar, e.g.:

expr | expr

Instance Attribute Summary

Attributes included from Nonterminal

#rules

Attributes included from Rule

#extension, #grammar, #label, #name

Instance Method Summary collapse

Methods included from Nonterminal

#grammar=, #initialize

Methods included from Rule

#==, #===, #default_options, #extend_match, for, #inspect, #needs_paren?, #parse, #terminal?, #test, #to_embedded_s, #to_s

Instance Method Details

#elide? ⇒ Boolean

Returns true if this rule should extend a match but should not appear in its event stream.

Returns:

  • (Boolean)


1260
1261
1262
# File 'lib/citrus.rb', line 1260

def elide? # :nodoc:
  true
end

#exec(input, events = []) ⇒ Object

Returns an array of events for this rule on the given input.



1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
# File 'lib/citrus.rb', line 1237

def exec(input, events=[])
  events << self

  index = events.size
  n = 0
  m = rules.length

  while n < m && input.exec(rules[n], events).size == index
    n += 1
  end

  if index < events.size
    events << CLOSE
    events << events[-2]
  else
    events.pop
  end

  events
end

#to_citrus ⇒ Object

Returns the Citrus notation of this rule as a string.



1265
1266
1267
# File 'lib/citrus.rb', line 1265

def to_citrus # :nodoc:
  rules.map {|r| r.to_embedded_s }.join(' | ')
end