class HCL::Checker::Lexer

Attributes

filename[R]
lineno[R]
state[RW]

Public Instance Methods

_next_token() click to toggle source
# 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
action() { || ... } click to toggle source
# File lib/hcl/checker/lexer.rb, line 22
def action
  yield
end
consume_comment(input) click to toggle source
# 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_heredoc() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 ) click to toggle source
# 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() click to toggle source
# 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(str)
Alias for: scan_str
scan_file( filename ) click to toggle source
# File lib/hcl/checker/lexer.rb, line 39
def scan_file( filename )
  load_file(filename)
  do_parse
end
scan_setup(str) click to toggle source
# File lib/hcl/checker/lexer.rb, line 16
def scan_setup(str)
  @ss = StringScanner.new(str)
  @lineno =  1
  @state  = nil
end
scan_str(str) click to toggle source
# File lib/hcl/checker/lexer.rb, line 26
def scan_str(str)
  scan_setup(str)
  do_parse
end
Also aliased as: scan
to_boolean(input) click to toggle source
# 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