1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
module Puppet::Pops
module Parser
class PNParser
LIT_TRUE = 'true'.freeze
LIT_FALSE = 'false'.freeze
LIT_NIL = 'nil'.freeze
TOKEN_END = 0
TOKEN_BOOL = 1
TOKEN_NIL = 2
TOKEN_INT = 3
TOKEN_FLOAT = 4
TOKEN_IDENTIFIER = 5
TOKEN_WS = 0x20
TOKEN_STRING = 0x22
TOKEN_KEY = 0x3a
TOKEN_LP = 0x28
TOKEN_RP = 0x29
TOKEN_LB = 0x5b
TOKEN_RB = 0x5d
TOKEN_LC = 0x7b
TOKEN_RC = 0x7d
TYPE_END = 0
TYPE_WS = 1
TYPE_DELIM = 2
TYPE_KEY_START = 3
TYPE_STRING_START = 4
TYPE_IDENTIFIER = 5
TYPE_MINUS = 6
TYPE_DIGIT = 7
TYPE_ALPHA = 8
def initialize
@char_types = self.class.char_types
end
def parse(text, locator = nil, offset = nil)
@locator = locator
@offset = offset
@text = text
@codepoints = text.codepoints.to_a.freeze
@pos = 0
@token = TOKEN_END
@token_value = nil
next_token
parse_next
end
def self.char_types
unless instance_variable_defined?(:@char_types)
@char_types = Array.new(0x80, TYPE_IDENTIFIER)
@char_types[0] = TYPE_END
[0x09, 0x0d, 0x0a, 0x20].each { |n| @char_types[n] = TYPE_WS }
[TOKEN_LP, TOKEN_RP, TOKEN_LB, TOKEN_RB, TOKEN_LC, TOKEN_RC].each { |n| @char_types[n] = TYPE_DELIM }
@char_types[0x2d] = TYPE_MINUS
(0x30..0x39).each { |n| @char_types[n] = TYPE_DIGIT }
(0x41..0x5a).each { |n| @char_types[n] = TYPE_ALPHA }
(0x61..0x7a).each { |n| @char_types[n] = TYPE_ALPHA }
@char_types[TOKEN_KEY] = TYPE_KEY_START
@char_types[TOKEN_STRING] = TYPE_STRING_START
@char_types.freeze
end
@char_types
end
private
def parse_next
case @token
when TOKEN_LB
parse_array
when TOKEN_LC
parse_map
when TOKEN_LP
parse_call
when TOKEN_BOOL, TOKEN_INT, TOKEN_FLOAT, TOKEN_STRING, TOKEN_NIL
parse_literal
when TOKEN_END
parse_error(_('unexpected end of input'))
else
parse_error(_('unexpected %{value}' % { value: @token_value }))
end
end
def parse_error(message)
file = ''
line = 1
pos = 1
if @locator
file = @locator.file
line = @locator.line_for_offset(@offset)
pos = @locator.pos_on_line(@offset)
end
@codepoints[0, @pos].each do |c|
if c == 0x09
line += 1
pos = 1
end
end
raise Puppet::ParseError.new(message, file, line, pos)
end
def parse_literal
pn = PN::Literal.new(@token_value)
next_token
pn
end
def parse_array
next_token
PN::List.new(parse_elements(TOKEN_RB))
end
def parse_map
next_token
entries = []
while @token != TOKEN_RC && @token != TOKEN_END
parse_error(_('map key expected')) unless @token == TOKEN_KEY
key = @token_value
next_token
entries << parse_next.with_name(key)
end
next_token
PN::Map.new(entries)
end
def parse_call
next_token
parse_error(_("expected identifier to follow '('")) unless @token == TOKEN_IDENTIFIER
name = @token_value
next_token
PN::Call.new(name, *parse_elements(TOKEN_RP))
end
def parse_elements(end_token)
elements = []
while @token != end_token && @token != TOKEN_END
elements << parse_next
end
parse_error(_("missing '%{token}' to end list") % { token: end_token.chr(Encoding::UTF_8) } ) unless @token == end_token
next_token
elements
end
# All methods below belong to the PN lexer
def next_token
skip_white
s = @pos
c = next_cp
case @char_types[c]
when TYPE_END
@token_value = nil
@token = TOKEN_END
when TYPE_MINUS
if @char_types[peek_cp] == TYPE_DIGIT
next_token # consume float or integer
@token_value = -@token_value
else
consume_identifier(s)
end
when TYPE_DIGIT
skip_decimal_digits
c = peek_cp
if c == 0x2e # '.'
@pos += 1
consume_float(s, c)
else
@token_value = @text[s..@pos].to_i
@token = TOKEN_INT
end
when TYPE_DELIM
@token_value = @text[s]
@token = c
when TYPE_KEY_START
if @char_types[peek_cp] == TYPE_ALPHA
next_token
@token = TOKEN_KEY
else
parse_error(_("expected identifier after ':'"))
end
when TYPE_STRING_START
consume_string
else
consume_identifier(s)
end
end
def consume_identifier(s)
while @char_types[peek_cp] >= TYPE_IDENTIFIER do
@pos += 1
end
id = @text[s...@pos]
case id
when LIT_TRUE
@token = TOKEN_BOOL
@token_value = true
when LIT_FALSE
@token = TOKEN_BOOL
@token_value = false
when LIT_NIL
@token = TOKEN_NIL
@token_value = nil
else
@token = TOKEN_IDENTIFIER
@token_value = id
end
end
def consume_string
s = @pos
b = ''
loop do
c = next_cp
case c
when TOKEN_END
@pos = s - 1
parse_error(_('unterminated quote'))
when TOKEN_STRING
@token_value = b
@token = TOKEN_STRING
break
when 0x5c # '\'
c = next_cp
case c
when 0x74 # 't'
b << "\t"
when 0x72 # 'r'
b << "\r"
when 0x6e # 'n'
b << "\n"
when TOKEN_STRING
b << '"'
when 0x5c # '\'
b << "\\"
when 0x6f # 'o'
c = 0
3.times do
n = next_cp
if 0x30 <= n && n <= 0x37c
c *= 8
c += n - 0x30
else
parse_error(_('malformed octal quote'))
end
end
b << c
else
b << "\\"
b << c
end
else
b << c
end
end
end
def consume_float(s, d)
parse_error(_('digit expected')) if skip_decimal_digits == 0
c = peek_cp
if d == 0x2e # '.'
if c == 0x45 || c == 0x65 # 'E' or 'e'
@pos += 1
parse_error(_('digit expected')) if skip_decimal_digits == 0
c = peek_cp
end
end
parse_error(_('digit expected')) if @char_types[c] == TYPE_ALPHA
@token_value = @text[s...@pos].to_f
@token = TOKEN_FLOAT
end
def skip_decimal_digits
count = 0
c = peek_cp
if c == 0x2d || c == 0x2b # '-' or '+'
@pos += 1
c = peek_cp
end
while @char_types[c] == TYPE_DIGIT do
@pos += 1
c = peek_cp
count += 1
end
count
end
def skip_white
while @char_types[peek_cp] == TYPE_WS do
@pos += 1
end
end
def next_cp
c = 0
if @pos < @codepoints.size
c = @codepoints[@pos]
@pos += 1
end
c
end
def peek_cp
@pos < @codepoints.size ? @codepoints[@pos] : 0
end
end
end
end
|