Class: Ascode::Parser::Literal

Inherits:
Object
  • Object
show all
Defined in:
lib/ascode/parser/literal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, literal_begin) ⇒ Literal

Returns a new instance of Literal.



8
9
10
11
# File 'lib/ascode/parser/literal.rb', line 8

def initialize(code, literal_begin)
  @code = code
  @begin = literal_begin
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



6
7
8
# File 'lib/ascode/parser/literal.rb', line 6

def length
  @length
end

Instance Method Details

#astObject



39
40
41
42
43
44
# File 'lib/ascode/parser/literal.rb', line 39

def ast
  @ast = {
    action: "push",
    what: @literal
  }
end

#find_endObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ascode/parser/literal.rb', line 26

def find_end
  escape = false
  @literal.split("").each_with_index do |char, index|
    if @quoted
      return index if char == '"' && !escape
      escape = char == "\\"
    else
      return index unless char =~ /[0-9.]/
    end
  end
  -1
end

#parseObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ascode/parser/literal.rb', line 13

def parse
  @quoted = @code[@begin] == '"'
  @begin += 1 if @quoted
  @literal = @code[@begin..-1]

  @end = find_end
  @literal = @literal[0..(@end - 1)]

  @literal = Converter.convert @literal
  @length = @literal.to_s.length + (@quoted ? 1 : -1)
  ast
end