Module: Parsr::Rules::Regexp

Defined in:
lib/parsr/rules/regexp.rb

Defined Under Namespace

Classes: Unterminated

Constant Summary collapse

OPTIONS =
{
  'i' => Regexp::IGNORECASE,
  'm' => Regexp::MULTILINE,
  'x' => Regexp::EXTENDED,
}.freeze

Class Method Summary collapse

Class Method Details

.match(scanner) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/parsr/rules/regexp.rb', line 15

def match(scanner)
  if scanner.scan(/\//)
    buffer = ''
    while chunk = (parse_content(scanner) || parse_escape(scanner))
      buffer << chunk
    end
    return Parsr::Token.new(Regexp.new(buffer, parse_options(scanner)))
  end
end

.parse_content(scanner) ⇒ Object



25
26
27
# File 'lib/parsr/rules/regexp.rb', line 25

def parse_content(scanner)
  scanner.matched if scanner.scan(/[^\\\/]+/)
end

.parse_escape(scanner) ⇒ Object



29
30
31
# File 'lib/parsr/rules/regexp.rb', line 29

def parse_escape(scanner)
  return scanner.matched if scanner.scan(%r{\\.})
end

.parse_options(scanner) ⇒ Object

Raises:



33
34
35
36
# File 'lib/parsr/rules/regexp.rb', line 33

def parse_options(scanner)
  raise Unterminated.new(scanner) unless scanner.scan(/\/[imx]*/)
  scanner.matched[1..-1].chars.map{ |o| OPTIONS[o] }.reduce(0) { |a, b| a | b }
end