Class: ComponentEmbeddedRuby::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/component_embedded_ruby/lexer.rb,
lib/component_embedded_ruby/lexer/input_reader.rb,
lib/component_embedded_ruby/lexer/string_reader.rb,
lib/component_embedded_ruby/lexer/ruby_code_reader.rb

Defined Under Namespace

Classes: InputReader, Position, RubyCodeReader, StringReader, Token

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Lexer

Returns a new instance of Lexer.



12
13
14
15
16
# File 'lib/component_embedded_ruby/lexer.rb', line 12

def initialize(content)
  @reader = InputReader.new(content)

  @tokens = []
end

Instance Method Details

#lexObject

rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



18
19
20
21
22
23
24
25
26
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
52
53
54
55
56
57
# File 'lib/component_embedded_ruby/lexer.rb', line 18

def lex # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  while !reader.eof?
    char = reader.current_char

    if char == "<"
      add_token(:open_carrot, "<")
      reader.next
    elsif char == ">"
      add_token(:close_carrot, ">")
      reader.next
    elsif char == "="
      add_token(:equals, "=")
      reader.next
    elsif char == '"'
      add_token(:string, read_quoted_string)
    elsif char == "/"
      add_token(:slash, "/")
      reader.next
    elsif char == "{"
      if reader.peek == "-"
        reader.next
        add_token(:ruby_no_eval, read_ruby_string)
      else
        add_token(:ruby, read_ruby_string)
      end
    elsif letter?(char)
      position = Position.new(reader.current_line, reader.current_column)

      if @tokens[-1]&.type == :close_carrot
        add_token(:string, read_body_string, position)
      else
        add_token(:identifier, read_string, position)
      end
    else
      reader.next
    end
  end

  @tokens
end