Class: KPeg::LiteralRegexp

Inherits:
Operator show all
Defined in:
lib/kpeg/grammar.rb

Instance Attribute Summary collapse

Attributes inherited from Operator

#action

Instance Method Summary collapse

Methods inherited from Operator

#detect_tags, #inspect_type, #prune_values, #set_action, #|

Constructor Details

#initialize(reg, opts = nil) ⇒ LiteralRegexp

Returns a new instance of LiteralRegexp.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/kpeg/grammar.rb', line 104

def initialize(reg, opts=nil)
  super()

  if reg.kind_of? String
    flags = 0
    lang = nil

    if opts
      opts.split("").each do |o|
        case o
        when "n", "N", "e", "E", "s", "S"
          lang = o.downcase
        when "u", "U"
          if RUBY_VERSION > "1.8.7"
            # Ruby 1.9 defaults to UTF-8 for string matching
            lang = ""
          else
            lang = "u"
          end
        when "m"
          flags |= Regexp::MULTILINE
        when "x"
          flags |= Regexp::EXTENDED
        when "i"
          flags |= Regexp::IGNORECASE
        end
      end
    end

    @regexp = Regexp.new(reg, flags, lang)
  else
    @regexp = reg
  end
end

Instance Attribute Details

#regexpObject (readonly)

Returns the value of attribute regexp.



139
140
141
# File 'lib/kpeg/grammar.rb', line 139

def regexp
  @regexp
end

Instance Method Details

#==(obj) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/kpeg/grammar.rb', line 153

def ==(obj)
  case obj
  when LiteralRegexp
    @regexp == obj.regexp
  else
    super
  end
end

#inspectObject



162
163
164
# File 'lib/kpeg/grammar.rb', line 162

def inspect
  inspect_type 'reg', @regexp.inspect
end

#match(x) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/kpeg/grammar.rb', line 145

def match(x)
  if str = x.scan(@regexp)
    MatchString.new(self, str)
  else
    x.fail(self)
  end
end

#stringObject



141
142
143
# File 'lib/kpeg/grammar.rb', line 141

def string
  @regexp.source
end