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
# 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", "u", "U"
          lang = o
        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.



132
133
134
# File 'lib/kpeg/grammar.rb', line 132

def regexp
  @regexp
end

Instance Method Details

#==(obj) ⇒ Object



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

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

#inspectObject



155
156
157
# File 'lib/kpeg/grammar.rb', line 155

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

#match(x) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/kpeg/grammar.rb', line 138

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

#stringObject



134
135
136
# File 'lib/kpeg/grammar.rb', line 134

def string
  @regexp.source
end