Class: Citrus::Terminal

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

Overview

A Terminal is a Rule that matches directly on the input stream and may not contain any other rule. Terminals are essentially wrappers for regular expressions. As such, the Citrus notation is identical to Ruby’s regular expression notation, e.g.:

/expr/

Character classes and the dot symbol may also be used in Citrus notation for compatibility with other parsing expression implementations, e.g.:

[a-zA-Z]
.

Character classes have the same semantics as character classes inside Ruby regular expressions. The dot matches any character, including newlines.

Direct Known Subclasses

StringTerminal

Instance Attribute Summary collapse

Attributes included from Rule

#extension, #grammar, #label, #name

Instance Method Summary collapse

Methods included from Rule

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

Constructor Details

#initialize(regexp = /^/) ⇒ Terminal

Returns a new instance of Terminal.



881
882
883
# File 'lib/citrus.rb', line 881

def initialize(regexp=/^/)
  @regexp = regexp
end

Instance Attribute Details

#regexpObject (readonly)

The actual Regexp object this rule uses to match.



886
887
888
# File 'lib/citrus.rb', line 886

def regexp
  @regexp
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



906
907
908
909
910
911
912
913
# File 'lib/citrus.rb', line 906

def ==(other)
  case other
  when Regexp
    @regexp == other
  else
    super
  end
end

#case_sensitive?Boolean

Returns true if this rule is case sensitive.

Returns:

  • (Boolean)


902
903
904
# File 'lib/citrus.rb', line 902

def case_sensitive?
  !@regexp.casefold?
end

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

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



889
890
891
892
893
894
895
896
897
898
899
# File 'lib/citrus.rb', line 889

def exec(input, events=[])
  match = input.scan(@regexp)

  if match
    events << self
    events << CLOSE
    events << match.length
  end

  events
end

#terminal?Boolean

Returns true if this rule is a Terminal.

Returns:

  • (Boolean)


918
919
920
# File 'lib/citrus.rb', line 918

def terminal? # :nodoc:
  true
end

#to_citrusObject

Returns the Citrus notation of this rule as a string.



923
924
925
# File 'lib/citrus.rb', line 923

def to_citrus # :nodoc:
  @regexp.inspect
end