Class: RegularExpression::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/regular_expression/lexer.rb

Constant Summary collapse

SINGLE =
{
  "^" => :CARET,
  "$" => :ENDING,
  "(" => :LPAREN,
  ")" => :RPAREN,
  "[" => :LBRACKET,
  "]" => :RBRACKET,
  "{" => :LBRACE,
  "}" => :RBRACE,
  "|" => :PIPE,
  "*" => :STAR,
  "+" => :PLUS,
  "?" => :QMARK,
  "." => :PERIOD,
  "-" => :DASH,
  "," => :COMMA
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Lexer

Returns a new instance of Lexer.



23
24
25
# File 'lib/regular_expression/lexer.rb', line 23

def initialize(source)
  @source = source.dup
end

Instance Method Details

#tokensObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/regular_expression/lexer.rb', line 27

def tokens
  result = []

  until @source.empty?
    case @source
    when /\A\\[wWdD]/
      result << [:CHAR_CLASS, $&]
    when /\A(?:\\[Az]|\$)/
      result << [:ANCHOR, $&]
    when /\A[\^$()\[\]{}|*+?.\-,]/
      result << [SINGLE[$&], $&]
    when /\A\d+/
      result << [:INTEGER, $&.to_i]
    when /\A(?:\u0009|\u000A|\u000D|[\u0020-\uD7FF]|[\uE000-\uFFFD])/
      result << [:CHAR, $&]
    else
      raise SyntaxError, @source
    end

    @source = $'
  end

  result << [false, "end"]
  result
end