Class: HCL::Checker::Lexer
- Inherits:
-
Object
- Object
- HCL::Checker::Lexer
- Defined in:
- lib/hcl/checker/lexer.rb
Overview
-- DO NOT MODIFY!!!! This file is automatically generated by rex 1.0.8 from lexical definition file "./assets/lexer.rex". ++
Defined Under Namespace
Classes: ScanError
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#lineno ⇒ Object
readonly
Returns the value of attribute lineno.
-
#state ⇒ Object
Returns the value of attribute state.
-
#token_positions ⇒ Object
readonly
def _next_token.
Instance Method Summary collapse
- #_next_token ⇒ Object
- #action ⇒ Object
- #consume_comment(input) ⇒ Object
- #consume_heredoc(indented) ⇒ Object
- #consume_string(input) ⇒ Object
- #current_position ⇒ Object
- #decode_escape ⇒ Object
- #dedent(text) ⇒ Object
- #lex(input) ⇒ Object
- #load_file(filename) ⇒ Object
- #next_token ⇒ Object
- #scan_file(filename) ⇒ Object
- #scan_setup(str) ⇒ Object
- #scan_str(str) ⇒ Object (also: #scan)
- #to_boolean(input) ⇒ Object
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
13 14 15 |
# File 'lib/hcl/checker/lexer.rb', line 13 def filename @filename end |
#lineno ⇒ Object (readonly)
Returns the value of attribute lineno.
12 13 14 |
# File 'lib/hcl/checker/lexer.rb', line 12 def lineno @lineno end |
#state ⇒ Object
Returns the value of attribute state.
14 15 16 |
# File 'lib/hcl/checker/lexer.rb', line 14 def state @state end |
#token_positions ⇒ Object (readonly)
def _next_token
191 192 193 |
# File 'lib/hcl/checker/lexer.rb', line 191 def token_positions @token_positions end |
Instance Method Details
#_next_token ⇒ Object
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# 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(/null(?![a-zA-Z0-9_\-])/)) action { [:NULL, text]} when (text = @ss.scan(/for(?![a-zA-Z0-9_\-])/)) action { [:FOR, text]} when (text = @ss.scan(/in(?![a-zA-Z0-9_\-])/)) action { [:IN, text]} when (text = @ss.scan(/if(?![a-zA-Z0-9_\-])/)) action { [:IF, text]} when (text = @ss.scan(/\d+(\.\d+)?[eE][-+]?\d+|\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(text.end_with?('-'))] } 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 { [:EQEQ, text]} when (text = @ss.scan(/\!\=/)) action { [:NEQ, text]} when (text = @ss.scan(/\<\=/)) action { [:LE, text]} when (text = @ss.scan(/\>\=/)) action { [:GE, text]} when (text = @ss.scan(/\=\>/)) action { [:ARROW, text]} when (text = @ss.scan(/\</)) action { [:LT, text]} when (text = @ss.scan(/\>/)) action { [:GT, text]} when (text = @ss.scan(/\=/)) action { [:EQUAL, text]} when (text = @ss.scan(/\-/)) action { [:MINUS, text]} when (text = @ss.scan(/\+/)) action { [:PLUS, text]} when (text = @ss.scan(/\*/)) action { [:STAR, text]} when (text = @ss.scan(/\//)) action { [:SLASH, text]} when (text = @ss.scan(/\%/)) action { [:PERCENT, text]} when (text = @ss.scan(/\&\&/)) action { [:ANDAND, text]} when (text = @ss.scan(/\|\|/)) action { [:OROR, text]} when (text = @ss.scan(/\?/)) action { [:QUESTION, text]} when (text = @ss.scan(/\:/)) action { [:COLON, text]} when (text = @ss.scan(/\./)) action { [:PERIOD, text]} when (text = @ss.scan(/\!/)) action { [:BANG, 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 |
#action ⇒ Object
22 23 24 |
# File 'lib/hcl/checker/lexer.rb', line 22 def action yield end |
#consume_comment(input) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/hcl/checker/lexer.rb', line 211 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 raise ScanError, 'unterminated block comment' end end end |
#consume_heredoc(indented) ⇒ Object
264 265 266 267 268 269 270 271 272 273 |
# File 'lib/hcl/checker/lexer.rb', line 264 def consume_heredoc(indented) marker_line = @ss.scan_until(%r{\n}) raise ScanError, 'unterminated heredoc: missing marker' if marker_line.nil? marker = marker_line.strip closing = /^[ \t]*#{Regexp.escape(marker)}[ \t]*(?:\n|\z)/ raw = @ss.scan_until(closing) raise ScanError, "unterminated heredoc: missing closing marker '#{marker}'" if raw.nil? content = raw.sub(closing, '') indented ? dedent(content) : content end |
#consume_string(input) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/hcl/checker/lexer.rb', line 224 def consume_string(input) result = '' nested = 0 loop do text = @ss.scan_until(%r{\"|\$\{|%\{|\}|\\}) raise ScanError, 'unterminated string' if text.nil? case text when %r{\$\{\z} nested += 1 when %r{%\{\z} nested += 1 when %r{\}\z} nested -= 1 if nested.positive? when %r{\\\z} result += text.chop + decode_escape next end result += text.to_s break if nested.zero? && text =~ %r{\"\z} end result.chop end |
#current_position ⇒ Object
202 203 204 205 206 207 |
# File 'lib/hcl/checker/lexer.rb', line 202 def current_position consumed = @ss.string[0...@ss.pos] line = consumed.count("\n") + 1 column = consumed.length - (consumed.rindex("\n") || -1) [line, column] end |
#decode_escape ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/hcl/checker/lexer.rb', line 246 def decode_escape char = @ss.getch case char when 'n' then "\n" when 't' then "\t" when 'r' then "\r" when '"' then '"' when '\\' then '\\' when 'u' hex = @ss.scan(/[0-9a-fA-F]{4}/) hex ? [hex.to_i(16)].pack('U') : 'u' when 'U' hex = @ss.scan(/[0-9a-fA-F]{8}/) hex ? [hex.to_i(16)].pack('U') : 'U' else char.to_s end end |
#dedent(text) ⇒ Object
274 275 276 277 278 279 |
# File 'lib/hcl/checker/lexer.rb', line 274 def dedent(text) lines = text.split("\n", -1) indents = lines.reject { |line| line.strip.empty? }.map { |line| line[/\A[ \t]*/].length } indent = indents.min || 0 lines.map { |line| line.strip.empty? ? line : line[indent..] }.join("\n") end |
#lex(input) ⇒ Object
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/hcl/checker/lexer.rb', line 192 def lex(input) scan_setup(input) tokens = [] @token_positions = [] while token = next_token tokens << token @token_positions << current_position 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_token ⇒ Object
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
208 209 210 |
# File 'lib/hcl/checker/lexer.rb', line 208 def to_boolean(input) input == 'true' end |