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
# File 'lib/kpeg/grammar.rb', line 104

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

  if reg.kind_of? String
    flags = 0

    if opts
      opts.split("").each do |o|
        case o
        when "m"
          flags |= Regexp::MULTILINE
        when "x"
          flags |= Regexp::EXTENDED
        when "i"
          flags |= Regexp::IGNORECASE
        end
      end
    end

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

Instance Attribute Details

#regexpObject (readonly)

Returns the value of attribute regexp.



129
130
131
# File 'lib/kpeg/grammar.rb', line 129

def regexp
  @regexp
end

Instance Method Details

#==(obj) ⇒ Object



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

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

#inspectObject



152
153
154
# File 'lib/kpeg/grammar.rb', line 152

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

#match(x) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/kpeg/grammar.rb', line 135

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

#stringObject



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

def string
  @regexp.source
end