Class: HCLLexer
- Inherits:
-
Object
- Object
- HCLLexer
- Defined in:
- lib/hcl/lexer.rb
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.
Instance Method Summary collapse
- #_next_token ⇒ Object
- #action ⇒ Object
- #consume_comment(input) ⇒ Object
- #consume_string(input) ⇒ Object
-
#lex(input) ⇒ Object
def _next_token.
- #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.
14 15 16 |
# File 'lib/hcl/lexer.rb', line 14 def filename @filename end |
#lineno ⇒ Object (readonly)
Returns the value of attribute lineno.
13 14 15 |
# File 'lib/hcl/lexer.rb', line 13 def lineno @lineno end |
#state ⇒ Object
Returns the value of attribute state.
15 16 17 |
# File 'lib/hcl/lexer.rb', line 15 def state @state end |
Instance Method Details
#_next_token ⇒ Object
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 |
# File 'lib/hcl/lexer.rb', line 54 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+/)) action { [:NUMBER, text.to_i] } when (text = @ss.scan(/\-?\d+\.\d+/)) action { [:FLOAT, text.to_f] } when (text = @ss.scan(/\"/)) action { [:STRING, consume_string(text)] } 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 { [: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 ⇒ Object
23 24 25 |
# File 'lib/hcl/lexer.rb', line 23 def action yield end |
#consume_comment(input) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/hcl/lexer.rb', line 136 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_string(input) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/hcl/lexer.rb', line 149 def consume_string(input) result = '' nested = 0 begin case(text = @ss.scan_until(%r{\"|\$\{|\}|\\})) when %r{\$\{\z} nested += 1 when %r{\}\z} nested -= 1 when %r{\\\z} result += text.chop + @ss.getch next end result += text.to_s end until nested == 0 && text =~ %r{\"\z} result.chop end |
#lex(input) ⇒ Object
def _next_token
119 120 121 122 123 124 125 126 |
# File 'lib/hcl/lexer.rb', line 119 def lex(input) scan_setup(input) tokens = [] while token = next_token tokens << token end tokens end |
#load_file(filename) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/hcl/lexer.rb', line 33 def load_file( filename ) @filename = filename open(filename, "r") do |f| scan_setup(f.read) end end |
#next_token ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/hcl/lexer.rb', line 46 def next_token return if @ss.eos? # skips empty actions until token = _next_token or @ss.eos?; end token end |
#scan_file(filename) ⇒ Object
40 41 42 43 |
# File 'lib/hcl/lexer.rb', line 40 def scan_file( filename ) load_file(filename) do_parse end |
#scan_setup(str) ⇒ Object
17 18 19 20 21 |
# File 'lib/hcl/lexer.rb', line 17 def scan_setup(str) @ss = StringScanner.new(str) @lineno = 1 @state = nil end |
#scan_str(str) ⇒ Object Also known as: scan
27 28 29 30 |
# File 'lib/hcl/lexer.rb', line 27 def scan_str(str) scan_setup(str) do_parse end |
#to_boolean(input) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/hcl/lexer.rb', line 127 def to_boolean(input) input = if input =~ /true/ true elsif input =~ /false/ false end return input end |