Module Citrus::Rule

  1. lib/citrus.rb

A Rule is an object that is used by a grammar to create matches on an Input during parsing.

Methods

public class

  1. for

public instance

  1. ==
  2. ===
  3. elide?
  4. eql?
  5. extension=
  6. label=
  7. name=
  8. parse
  9. terminal?
  10. test
  11. to_s
  12. to_str

Attributes

extension [R] The module this rule uses to extend new matches.
grammar [RW] The grammar this rule belongs to, if any.
label [R] A label for this rule. If a rule has a label, all matches that it creates will be accessible as named captures from the scope of their parent match using that label.
name [R] The name of this rule.

Public class methods

for (obj)

Returns a new Rule object depending on the type of object given.

[show source]
     # File lib/citrus.rb, line 589
589:     def self.for(obj)
590:       case obj
591:       when Rule     then obj
592:       when Symbol   then Alias.new(obj)
593:       when String   then StringTerminal.new(obj)
594:       when Regexp   then Terminal.new(obj)
595:       when Array    then Sequence.new(obj)
596:       when Range    then Choice.new(obj.to_a)
597:       when Numeric  then StringTerminal.new(obj.to_s)
598:       else
599:         raise ArgumentError, "Invalid rule object: #{obj.inspect}"
600:       end
601:     end

Public instance methods

== (other)
[show source]
     # File lib/citrus.rb, line 732
732:     def ==(other)
733:       case other
734:       when Rule
735:         to_s == other.to_s
736:       else
737:         super
738:       end
739:     end
=== (obj)

Tests the given obj for case equality with this rule.

[show source]
     # File lib/citrus.rb, line 685
685:     def ===(obj)
686:       !test(obj).nil?
687:     end
elide? ()

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

[show source]
     # File lib/citrus.rb, line 696
696:     def elide?
697:       false
698:     end
eql? (other)

Alias for #==

extension= (mod)

Specifies a module that will be used to extend all Match objects that result from this rule. If mod is a Proc, it is used to create an anonymous module with a value method.

[show source]
     # File lib/citrus.rb, line 627
627:     def extension=(mod)
628:       if Proc === mod
629:         mod = Module.new { define_method(:value, &mod) }
630:       end
631: 
632:       raise ArgumentError, "Extension must be a Module" unless Module === mod
633: 
634:       @extension = mod
635:     end
label= (label)

Sets the label of this rule.

[show source]
     # File lib/citrus.rb, line 615
615:     def label=(label)
616:       @label = label.to_sym
617:     end
name= (name)

Sets the name of this rule.

[show source]
     # File lib/citrus.rb, line 607
607:     def name=(name)
608:       @name = name.to_sym
609:     end
parse (source, options={})

Attempts to parse the given string and return a Match if any can be made. options may contain any of the following keys:

consume:If this is true a ParseError will be raised unless the entire input string is consumed. Defaults to true.
memoize:If this is true the matches generated during a parse are memoized. See MemoizedInput for more information. Defaults to false.
offset:The offset in string at which to start parsing. Defaults to 0.
[show source]
     # File lib/citrus.rb, line 658
658:     def parse(source, options={})
659:       opts = default_options.merge(options)
660: 
661:       input = (opts[:memoize] ? MemoizedInput : Input).new(source)
662:       string = input.string
663:       input.pos = opts[:offset] if opts[:offset] > 0
664: 
665:       events = input.exec(self)
666:       length = events[-1]
667: 
668:       if !length || (opts[:consume] && length < (string.length - opts[:offset]))
669:         raise ParseError, input
670:       end
671: 
672:       Match.new(input, events, opts[:offset])
673:     end
terminal? ()

Returns true if this rule is a Terminal.

[show source]
     # File lib/citrus.rb, line 690
690:     def terminal?
691:       false
692:     end
test (string, options={})

Tests whether or not this rule matches on the given string. Returns the length of the match if any can be made, nil otherwise. Accepts the same options as parse.

[show source]
     # File lib/citrus.rb, line 678
678:     def test(string, options={})
679:       parse(string, options).length
680:     rescue ParseError
681:       nil
682:     end
to_s ()

Returns the Citrus notation of this rule as a string.

[show source]
     # File lib/citrus.rb, line 707
707:     def to_s
708:       if label
709:         "#{label}:" + (needs_paren? ? "(#{to_citrus})" : to_citrus)
710:       else
711:         to_citrus
712:       end
713:     end
to_str ()

Alias for to_s