Class: Citrus::StringTerminal

Inherits:
Terminal show all
Defined in:
lib/citrus.rb

Overview

A StringTerminal is a Terminal that may be instantiated from a String object. The Citrus notation is any sequence of characters enclosed in either single or double quotes, e.g.:

'expr'
"expr"

This notation works the same as it does in Ruby; i.e. strings in double quotes may contain escape sequences while strings in single quotes may not. In order to specify that a string should ignore case when matching, enclose it in backticks instead of single or double quotes, e.g.:

`expr`

Besides case sensitivity, case-insensitive strings have the same semantics as double-quoted strings.

Instance Attribute Summary

Attributes inherited from Terminal

#regexp

Attributes included from Rule

#extension, #grammar, #label, #name

Instance Method Summary collapse

Methods inherited from Terminal

#case_sensitive?, #exec, #terminal?

Methods included from Rule

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

Constructor Details

#initialize(rule = '', flags = 0) ⇒ StringTerminal

The flags will be passed directly to Regexp#new.



946
947
948
949
# File 'lib/citrus.rb', line 946

def initialize(rule='', flags=0)
  super(Regexp.new(Regexp.escape(rule), flags))
  @string = rule
end

Instance Method Details

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



951
952
953
954
955
956
957
958
# File 'lib/citrus.rb', line 951

def ==(other)
  case other
  when String
    @string == other
  else
    super
  end
end

#to_citrusObject

Returns the Citrus notation of this rule as a string.



963
964
965
966
967
968
969
# File 'lib/citrus.rb', line 963

def to_citrus # :nodoc:
  if case_sensitive?
    @string.inspect
  else
    @string.inspect.gsub(/^"|"$/, '`')
  end
end