Class: HCL::Checker::Lexer

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

Overview

– DO NOT MODIFY!!!! This file is automatically generated by rex 1.0.7 from lexical definition file “./assets/lexer.rex”. ++

Defined Under Namespace

Classes: ScanError

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



13
14
15
# File 'lib/hcl/checker/lexer.rb', line 13

def filename
  @filename
end

#linenoObject (readonly)

Returns the value of attribute lineno.



12
13
14
# File 'lib/hcl/checker/lexer.rb', line 12

def lineno
  @lineno
end

#stateObject

Returns the value of attribute state.



14
15
16
# File 'lib/hcl/checker/lexer.rb', line 14

def state
  @state
end

Instance Method Details

#_next_tokenObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/hcl/checker/lexer.rb', line 53

def _next_token
  text = @ss.peek(1)
  @lineno  +=  1  if text == "\n"
  token = case @state
    when nil
  case
          when (text = @ss.scan(/\s+/))
            ;

          when (text = @ss.scan(/\#.*|\/\/.*$/))
            ;

          when (text = @ss.scan(/\n|\r/))
            ;

          when (text = @ss.scan(/\/\*/))
             action { consume_comment(text) }

          when (text = @ss.scan(/true|false/))
             action { [:BOOL,         to_boolean(text)]}

          when (text = @ss.scan(/\-?\d+\.\d+/))
             action { [:FLOAT,        text.to_f] }

          when (text = @ss.scan(/-?\d+/))
             action { [:NUMBER,       text.to_i] }

          when (text = @ss.scan(/\"/))
             action { [:STRING,       consume_string(text)] }

          when (text = @ss.scan(/\<<\-?/))
             action { [:STRING,       consume_heredoc] }

          when (text = @ss.scan(/\{/))
             action { [:LEFTBRACE,        text]}

          when (text = @ss.scan(/\}/))
             action { [:RIGHTBRACE,       text]}

          when (text = @ss.scan(/\[/))
             action { [:LEFTBRACKET,      text]}

          when (text = @ss.scan(/\]/))
             action { [:RIGHTBRACKET,     text]}

          when (text = @ss.scan(/\(/))
             action { [:LEFTPARENTHESES,  text]}

          when (text = @ss.scan(/\)/))
             action { [:RIGHTPARENTHESES, text]}

          when (text = @ss.scan(/\,/))
             action { [:COMMA,        text]}

          when (text = @ss.scan(/[a-zA-Z_][a-zA-Z0-9_\-\.]*/))
             action { [:IDENTIFIER,   text]}

          when (text = @ss.scan(/\=/))
             action { [:EQUAL,        text]}

          when (text = @ss.scan(/\-/))
             action { [:MINUS,        text]}

  
  else
    text = @ss.string[@ss.pos .. -1]
    raise  ScanError, "can not match: '" + text + "'"
  end  # if

else
  raise  ScanError, "undefined state: '" + state.to_s + "'"
end  # case state
  token
end

#actionObject



22
23
24
# File 'lib/hcl/checker/lexer.rb', line 22

def action
  yield
end

#consume_comment(input) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/hcl/checker/lexer.rb', line 146

def consume_comment(input)
  nested = 1
  until nested.zero?
    case(text = @ss.scan_until(%r{/\*|\*/|\z}) )
    when %r{/\*\z}
      nested =+ 1
    when %r{\*/\z}
      nested -= 1
    else
      break
    end
  end
end

#consume_heredocObject



177
178
179
180
181
# File 'lib/hcl/checker/lexer.rb', line 177

def consume_heredoc
    token = Regexp.new @ss.scan_until(%r{\n})
    document = @ss.scan_until(token)
    document.chop
end

#consume_string(input) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/hcl/checker/lexer.rb', line 159

def consume_string(input)
  result = ''
  nested = 0
  loop do
    case(text = @ss.scan_until(%r{\"|\$\{|\}|\\}))
    when %r{\$\{\z}
      nested += 1
    when %r{\}\z}
      nested -= 1 if nested.positive?
    when %r{\\\z}
      result += text.chop + @ss.getch
      next
    end
    result += text.to_s
    break if nested.zero? && text =~ %r{\"\z}
  end
  result.chop
end

#lex(input) ⇒ Object

def _next_token



128
129
130
131
132
133
134
135
# File 'lib/hcl/checker/lexer.rb', line 128

def lex(input)
  scan_setup(input)
  tokens = []
  while token = next_token
    tokens << token
  end
  tokens
end

#load_file(filename) ⇒ Object



32
33
34
35
36
37
# File 'lib/hcl/checker/lexer.rb', line 32

def load_file( filename )
  @filename = filename
  File.open(filename, "r") do |f|
    scan_setup(f.read)
  end
end

#next_tokenObject



45
46
47
48
49
50
51
# File 'lib/hcl/checker/lexer.rb', line 45

def next_token
  return if @ss.eos?

  # skips empty actions
  until token = _next_token or @ss.eos?; end
  token
end

#scan_file(filename) ⇒ Object



39
40
41
42
# File 'lib/hcl/checker/lexer.rb', line 39

def scan_file( filename )
  load_file(filename)
  do_parse
end

#scan_setup(str) ⇒ Object



16
17
18
19
20
# File 'lib/hcl/checker/lexer.rb', line 16

def scan_setup(str)
  @ss = StringScanner.new(str)
  @lineno =  1
  @state  = nil
end

#scan_str(str) ⇒ Object Also known as: scan



26
27
28
29
# File 'lib/hcl/checker/lexer.rb', line 26

def scan_str(str)
  scan_setup(str)
  do_parse
end

#to_boolean(input) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/hcl/checker/lexer.rb', line 136

def to_boolean(input)
  case input
  when /true/
    true
  when /false/
    false
  else
    raise "Invalid value for `to_boolean`, expected true/false got #{input}"
  end
end