| 1 |
/* |
| 2 |
Copyright 2015 Google Inc. All rights reserved. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <cassert> |
| 18 |
|
| 19 |
#include <map> |
| 20 |
#include <sstream> |
| 21 |
#include <string> |
| 22 |
|
| 23 |
#include "lexer.h" |
| 24 |
#include "static_error.h" |
| 25 |
#include "unicode.h" |
| 26 |
|
| 27 |
namespace jsonnet::internal { |
| 28 |
|
| 29 |
static const std::vector<std::string> EMPTY; |
| 30 |
|
| 31 |
/** Is the char whitespace (excluding \n). */ |
| 32 |
static bool is_horz_ws(char c) |
| 33 |
{ |
| 34 |
return c == ' ' || c == '\t' || c == '\r'; |
| 35 |
} |
| 36 |
|
| 37 |
/** Is the char whitespace. */ |
| 38 |
static bool is_ws(char c) |
| 39 |
{ |
| 40 |
return c == '\n' || is_horz_ws(c); |
| 41 |
} |
| 42 |
|
| 43 |
/** Strip whitespace from both ends of a string, but only up to margin on the left hand side. */ |
| 44 |
static std::string strip_ws(const std::string &s, unsigned margin) |
| 45 |
{ |
| 46 |
if (s.size() == 0) |
| 47 |
return s; // Avoid underflow below. |
| 48 |
size_t i = 0; |
| 49 |
while (i < s.length() && is_horz_ws(s[i]) && i < margin) |
| 50 |
i++; |
| 51 |
size_t j = s.size(); |
| 52 |
while (j > i && is_horz_ws(s[j - 1])) { |
| 53 |
j--; |
| 54 |
} |
| 55 |
return std::string(&s[i], &s[j]); |
| 56 |
} |
| 57 |
|
| 58 |
/** Split a string by \n and also strip left (up to margin) & right whitespace from each line. */ |
| 59 |
static std::vector<std::string> line_split(const std::string &s, unsigned margin) |
| 60 |
{ |
| 61 |
std::vector<std::string> ret; |
| 62 |
std::stringstream ss; |
| 63 |
for (size_t i = 0; i < s.length(); ++i) { |
| 64 |
if (s[i] == '\n') { |
| 65 |
ret.emplace_back(strip_ws(ss.str(), margin)); |
| 66 |
ss.str(""); |
| 67 |
} else { |
| 68 |
ss << s[i]; |
| 69 |
} |
| 70 |
} |
| 71 |
ret.emplace_back(strip_ws(ss.str(), margin)); |
| 72 |
return ret; |
| 73 |
} |
| 74 |
|
| 75 |
/** Consume whitespace. |
| 76 |
* |
| 77 |
* Return number of \n and number of spaces after last \n. Convert \t to spaces. |
| 78 |
*/ |
| 79 |
static void lex_ws(const char *&c, unsigned &new_lines, unsigned &indent, const char *&line_start, |
| 80 |
unsigned long &line_number) |
| 81 |
{ |
| 82 |
indent = 0; |
| 83 |
new_lines = 0; |
| 84 |
for (; *c != '\0' && is_ws(*c); c++) { |
| 85 |
switch (*c) { |
| 86 |
case '\r': |
| 87 |
// Ignore. |
| 88 |
break; |
| 89 |
|
| 90 |
case '\n': |
| 91 |
indent = 0; |
| 92 |
new_lines++; |
| 93 |
line_number++; |
| 94 |
line_start = c + 1; |
| 95 |
break; |
| 96 |
|
| 97 |
case ' ': indent += 1; break; |
| 98 |
|
| 99 |
// This only works for \t at the beginning of lines, but we strip it everywhere else |
| 100 |
// anyway. The only case where this will cause a problem is spaces followed by \t |
| 101 |
// at the beginning of a line. However that is rare, ill-advised, and if re-indentation |
| 102 |
// is enabled it will be fixed later. |
| 103 |
case '\t': indent += 8; break; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
# Consume all text until the end of the line, return number of newlines after that and indent |
| 110 |
*/ |
| 111 |
static void lex_until_newline(const char *&c, std::string &text, unsigned &blanks, unsigned &indent, |
| 112 |
const char *&line_start, unsigned long &line_number) |
| 113 |
{ |
| 114 |
const char *original_c = c; |
| 115 |
const char *last_non_space = c; |
| 116 |
for (; *c != '\0' && *c != '\n'; c++) { |
| 117 |
if (!is_horz_ws(*c)) |
| 118 |
last_non_space = c; |
| 119 |
} |
| 120 |
text = std::string(original_c, last_non_space - original_c + 1); |
| 121 |
// Consume subsequent whitespace including the '\n'. |
| 122 |
unsigned new_lines; |
| 123 |
lex_ws(c, new_lines, indent, line_start, line_number); |
| 124 |
blanks = new_lines == 0 ? 0 : new_lines - 1; |
| 125 |
} |
| 126 |
|
| 127 |
static bool is_upper(char c) |
| 128 |
{ |
| 129 |
return c >= 'A' && c <= 'Z'; |
| 130 |
} |
| 131 |
|
| 132 |
static bool is_lower(char c) |
| 133 |
{ |
| 134 |
return c >= 'a' && c <= 'z'; |
| 135 |
} |
| 136 |
|
| 137 |
static bool is_number(char c) |
| 138 |
{ |
| 139 |
return c >= '0' && c <= '9'; |
| 140 |
} |
| 141 |
|
| 142 |
static bool is_identifier_first(char c) |
| 143 |
{ |
| 144 |
return is_upper(c) || is_lower(c) || c == '_'; |
| 145 |
} |
| 146 |
|
| 147 |
static bool is_identifier(char c) |
| 148 |
{ |
| 149 |
return is_identifier_first(c) || is_number(c); |
| 150 |
} |
| 151 |
|
| 152 |
static bool is_symbol(char c) |
| 153 |
{ |
| 154 |
switch (c) { |
| 155 |
case '!': |
| 156 |
case '$': |
| 157 |
case ':': |
| 158 |
case '~': |
| 159 |
case '+': |
| 160 |
case '-': |
| 161 |
case '&': |
| 162 |
case '|': |
| 163 |
case '^': |
| 164 |
case '=': |
| 165 |
case '<': |
| 166 |
case '>': |
| 167 |
case '*': |
| 168 |
case '/': |
| 169 |
case '%': return true; |
| 170 |
} |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
bool allowed_at_end_of_operator(char c) { |
| 175 |
switch (c) { |
| 176 |
case '+': |
| 177 |
case '-': |
| 178 |
case '~': |
| 179 |
case '!': |
| 180 |
case '$': return false; |
| 181 |
} |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
static const std::map<std::string, Token::Kind> keywords = { |
| 186 |
{"assert", Token::ASSERT}, |
| 187 |
{"else", Token::ELSE}, |
| 188 |
{"error", Token::ERROR}, |
| 189 |
{"false", Token::FALSE}, |
| 190 |
{"for", Token::FOR}, |
| 191 |
{"function", Token::FUNCTION}, |
| 192 |
{"if", Token::IF}, |
| 193 |
{"import", Token::IMPORT}, |
| 194 |
{"importstr", Token::IMPORTSTR}, |
| 195 |
{"importbin", Token::IMPORTBIN}, |
| 196 |
{"in", Token::IN}, |
| 197 |
{"local", Token::LOCAL}, |
| 198 |
{"null", Token::NULL_LIT}, |
| 199 |
{"self", Token::SELF}, |
| 200 |
{"super", Token::SUPER}, |
| 201 |
{"tailstrict", Token::TAILSTRICT}, |
| 202 |
{"then", Token::THEN}, |
| 203 |
{"true", Token::TRUE}, |
| 204 |
}; |
| 205 |
|
| 206 |
Token::Kind lex_get_keyword_kind(const std::string &identifier) |
| 207 |
{ |
| 208 |
auto it = keywords.find(identifier); |
| 209 |
if (it == keywords.end()) |
| 210 |
return Token::IDENTIFIER; |
| 211 |
return it->second; |
| 212 |
} |
| 213 |
|
| 214 |
std::string lex_number(const char *&c, const std::string &filename, const Location &begin) |
| 215 |
{ |
| 216 |
// This function should be understood with reference to the linked image: |
| 217 |
// https://www.json.org/img/number.png |
| 218 |
|
| 219 |
// Note, we deviate from the json.org documentation as follows: |
| 220 |
// * There is no reason to lex negative numbers as atomic tokens, it is better to parse them |
| 221 |
// as a unary operator combined with a numeric literal. This avoids x-1 being tokenized as |
| 222 |
// <identifier> <number> instead of the intended <identifier> <binop> <number>. |
| 223 |
// * We support digit separators using the _ character for readability in |
| 224 |
// large numeric literals. |
| 225 |
|
| 226 |
enum State { |
| 227 |
BEGIN, |
| 228 |
AFTER_ZERO, |
| 229 |
AFTER_ONE_TO_NINE, |
| 230 |
AFTER_INT_UNDERSCORE, |
| 231 |
AFTER_DOT, |
| 232 |
AFTER_DIGIT, |
| 233 |
AFTER_FRAC_UNDERSCORE, |
| 234 |
AFTER_E, |
| 235 |
AFTER_EXP_SIGN, |
| 236 |
AFTER_EXP_DIGIT, |
| 237 |
AFTER_EXP_UNDERSCORE |
| 238 |
} state; |
| 239 |
|
| 240 |
std::string r; |
| 241 |
|
| 242 |
state = BEGIN; |
| 243 |
while (true) { |
| 244 |
switch (state) { |
| 245 |
case BEGIN: |
| 246 |
switch (*c) { |
| 247 |
case '0': state = AFTER_ZERO; break; |
| 248 |
|
| 249 |
case '1': |
| 250 |
case '2': |
| 251 |
case '3': |
| 252 |
case '4': |
| 253 |
case '5': |
| 254 |
case '6': |
| 255 |
case '7': |
| 256 |
case '8': |
| 257 |
case '9': state = AFTER_ONE_TO_NINE; break; |
| 258 |
|
| 259 |
default: throw StaticError(filename, begin, "couldn't lex number"); |
| 260 |
} |
| 261 |
break; |
| 262 |
|
| 263 |
case AFTER_ZERO: |
| 264 |
switch (*c) { |
| 265 |
case '.': state = AFTER_DOT; break; |
| 266 |
|
| 267 |
case 'e': |
| 268 |
case 'E': state = AFTER_E; break; |
| 269 |
|
| 270 |
case '_': { |
| 271 |
std::stringstream ss; |
| 272 |
ss << "couldn't lex number, _ not allowed after leading 0"; |
| 273 |
throw StaticError(filename, begin, ss.str()); |
| 274 |
} |
| 275 |
|
| 276 |
default: goto end; |
| 277 |
} |
| 278 |
break; |
| 279 |
|
| 280 |
case AFTER_ONE_TO_NINE: |
| 281 |
switch (*c) { |
| 282 |
case '.': state = AFTER_DOT; break; |
| 283 |
|
| 284 |
case 'e': |
| 285 |
case 'E': state = AFTER_E; break; |
| 286 |
|
| 287 |
case '0': |
| 288 |
case '1': |
| 289 |
case '2': |
| 290 |
case '3': |
| 291 |
case '4': |
| 292 |
case '5': |
| 293 |
case '6': |
| 294 |
case '7': |
| 295 |
case '8': |
| 296 |
case '9': state = AFTER_ONE_TO_NINE; break; |
| 297 |
|
| 298 |
case '_': state = AFTER_INT_UNDERSCORE; goto skip_char; |
| 299 |
|
| 300 |
default: goto end; |
| 301 |
} |
| 302 |
break; |
| 303 |
|
| 304 |
case AFTER_INT_UNDERSCORE: |
| 305 |
switch (*c) { |
| 306 |
// The only valid transition from _ is to a digit. |
| 307 |
case '0': |
| 308 |
case '1': |
| 309 |
case '2': |
| 310 |
case '3': |
| 311 |
case '4': |
| 312 |
case '5': |
| 313 |
case '6': |
| 314 |
case '7': |
| 315 |
case '8': |
| 316 |
case '9': state = AFTER_ONE_TO_NINE; break; |
| 317 |
|
| 318 |
default: { |
| 319 |
std::stringstream ss; |
| 320 |
ss << "couldn't lex number, junk after _: " << *c; |
| 321 |
throw StaticError(filename, begin, ss.str()); |
| 322 |
} |
| 323 |
} |
| 324 |
break; |
| 325 |
|
| 326 |
case AFTER_DOT: |
| 327 |
switch (*c) { |
| 328 |
case '0': |
| 329 |
case '1': |
| 330 |
case '2': |
| 331 |
case '3': |
| 332 |
case '4': |
| 333 |
case '5': |
| 334 |
case '6': |
| 335 |
case '7': |
| 336 |
case '8': |
| 337 |
case '9': state = AFTER_DIGIT; break; |
| 338 |
|
| 339 |
default: { |
| 340 |
std::stringstream ss; |
| 341 |
ss << "couldn't lex number, junk after decimal point: " << *c; |
| 342 |
throw StaticError(filename, begin, ss.str()); |
| 343 |
} |
| 344 |
} |
| 345 |
break; |
| 346 |
|
| 347 |
case AFTER_DIGIT: |
| 348 |
switch (*c) { |
| 349 |
case 'e': |
| 350 |
case 'E': state = AFTER_E; break; |
| 351 |
|
| 352 |
case '0': |
| 353 |
case '1': |
| 354 |
case '2': |
| 355 |
case '3': |
| 356 |
case '4': |
| 357 |
case '5': |
| 358 |
case '6': |
| 359 |
case '7': |
| 360 |
case '8': |
| 361 |
case '9': state = AFTER_DIGIT; break; |
| 362 |
|
| 363 |
case '_': state = AFTER_FRAC_UNDERSCORE; goto skip_char; |
| 364 |
|
| 365 |
default: goto end; |
| 366 |
} |
| 367 |
break; |
| 368 |
|
| 369 |
case AFTER_FRAC_UNDERSCORE: |
| 370 |
switch (*c) { |
| 371 |
// The only valid transition from _ is to a digit. |
| 372 |
case '0': |
| 373 |
case '1': |
| 374 |
case '2': |
| 375 |
case '3': |
| 376 |
case '4': |
| 377 |
case '5': |
| 378 |
case '6': |
| 379 |
case '7': |
| 380 |
case '8': |
| 381 |
case '9': state = AFTER_DIGIT; break; |
| 382 |
|
| 383 |
default: { |
| 384 |
std::stringstream ss; |
| 385 |
ss << "couldn't lex number, junk after _: " << *c; |
| 386 |
throw StaticError(filename, begin, ss.str()); |
| 387 |
} |
| 388 |
} |
| 389 |
break; |
| 390 |
|
| 391 |
case AFTER_E: |
| 392 |
switch (*c) { |
| 393 |
case '+': |
| 394 |
case '-': state = AFTER_EXP_SIGN; break; |
| 395 |
|
| 396 |
case '0': |
| 397 |
case '1': |
| 398 |
case '2': |
| 399 |
case '3': |
| 400 |
case '4': |
| 401 |
case '5': |
| 402 |
case '6': |
| 403 |
case '7': |
| 404 |
case '8': |
| 405 |
case '9': state = AFTER_EXP_DIGIT; break; |
| 406 |
|
| 407 |
default: { |
| 408 |
std::stringstream ss; |
| 409 |
ss << "couldn't lex number, junk after 'E': " << *c; |
| 410 |
throw StaticError(filename, begin, ss.str()); |
| 411 |
} |
| 412 |
} |
| 413 |
break; |
| 414 |
|
| 415 |
case AFTER_EXP_SIGN: |
| 416 |
switch (*c) { |
| 417 |
case '0': |
| 418 |
case '1': |
| 419 |
case '2': |
| 420 |
case '3': |
| 421 |
case '4': |
| 422 |
case '5': |
| 423 |
case '6': |
| 424 |
case '7': |
| 425 |
case '8': |
| 426 |
case '9': state = AFTER_EXP_DIGIT; break; |
| 427 |
|
| 428 |
default: { |
| 429 |
std::stringstream ss; |
| 430 |
ss << "couldn't lex number, junk after exponent sign: " << *c; |
| 431 |
throw StaticError(filename, begin, ss.str()); |
| 432 |
} |
| 433 |
} |
| 434 |
break; |
| 435 |
|
| 436 |
case AFTER_EXP_DIGIT: |
| 437 |
switch (*c) { |
| 438 |
case '0': |
| 439 |
case '1': |
| 440 |
case '2': |
| 441 |
case '3': |
| 442 |
case '4': |
| 443 |
case '5': |
| 444 |
case '6': |
| 445 |
case '7': |
| 446 |
case '8': |
| 447 |
case '9': state = AFTER_EXP_DIGIT; break; |
| 448 |
|
| 449 |
case '_': state = AFTER_EXP_UNDERSCORE; goto skip_char; |
| 450 |
|
| 451 |
default: goto end; |
| 452 |
} |
| 453 |
break; |
| 454 |
|
| 455 |
case AFTER_EXP_UNDERSCORE: |
| 456 |
switch (*c) { |
| 457 |
// The only valid transition from _ is to a digit. |
| 458 |
case '0': |
| 459 |
case '1': |
| 460 |
case '2': |
| 461 |
case '3': |
| 462 |
case '4': |
| 463 |
case '5': |
| 464 |
case '6': |
| 465 |
case '7': |
| 466 |
case '8': |
| 467 |
case '9': state = AFTER_EXP_DIGIT; break; |
| 468 |
|
| 469 |
default: { |
| 470 |
std::stringstream ss; |
| 471 |
ss << "couldn't lex number, junk after _: " << *c; |
| 472 |
throw StaticError(filename, begin, ss.str()); |
| 473 |
} |
| 474 |
} |
| 475 |
break; |
| 476 |
} |
| 477 |
r += *c; |
| 478 |
|
| 479 |
skip_char: |
| 480 |
c++; |
| 481 |
} |
| 482 |
end: |
| 483 |
return r; |
| 484 |
} |
| 485 |
|
| 486 |
// Check that b has at least the same whitespace prefix as a and returns the amount of this |
| 487 |
// whitespace, otherwise returns 0. If a has no whitespace prefix than return 0. |
| 488 |
static int whitespace_check(const char *a, const char *b) |
| 489 |
{ |
| 490 |
int i = 0; |
| 491 |
while (a[i] == ' ' || a[i] == '\t') { |
| 492 |
if (b[i] != a[i]) |
| 493 |
return 0; |
| 494 |
i++; |
| 495 |
} |
| 496 |
return i; |
| 497 |
} |
| 498 |
|
| 499 |
static void describe_whitespace(std::stringstream& msg, const std::string& ws) { |
| 500 |
int spaces = 0; |
| 501 |
int tabs = 0; |
| 502 |
for (char c : ws) { |
| 503 |
if (c == ' ') |
| 504 |
spaces++; |
| 505 |
else if (c == '\t') |
| 506 |
tabs++; |
| 507 |
} |
| 508 |
if (spaces > 0 && tabs > 0) { |
| 509 |
msg << spaces << (spaces == 1 ? " space" : " spaces") << " and " << tabs |
| 510 |
<< (tabs == 1 ? " tab" : " tabs"); |
| 511 |
} else if (spaces > 0) { |
| 512 |
msg << spaces << (spaces == 1 ? " space" : " spaces"); |
| 513 |
} else if (tabs > 0) { |
| 514 |
msg << tabs << (tabs == 1 ? " tab" : " tabs"); |
| 515 |
} else { |
| 516 |
msg << "no indentation"; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
Tokens jsonnet_lex(const std::string &filename, const char *input) |
| 521 |
{ |
| 522 |
unsigned long line_number = 1; |
| 523 |
const char *line_start = input; |
| 524 |
|
| 525 |
Tokens r; |
| 526 |
|
| 527 |
const char *c = input; |
| 528 |
|
| 529 |
Fodder fodder; |
| 530 |
bool fresh_line = true; // Are we tokenizing from the beginning of a new line? |
| 531 |
|
| 532 |
while (*c != '\0') { |
| 533 |
// Used to ensure we have actually advanced the pointer by the end of the iteration. |
| 534 |
const char *original_c = c; |
| 535 |
|
| 536 |
Token::Kind kind; |
| 537 |
std::string data; |
| 538 |
std::string string_block_indent; |
| 539 |
std::string string_block_term_indent; |
| 540 |
|
| 541 |
unsigned new_lines, indent; |
| 542 |
lex_ws(c, new_lines, indent, line_start, line_number); |
| 543 |
|
| 544 |
// If it's the end of the file, discard final whitespace. |
| 545 |
if (*c == '\0') |
| 546 |
break; |
| 547 |
|
| 548 |
if (new_lines > 0) { |
| 549 |
// Otherwise store whitespace in fodder. |
| 550 |
unsigned blanks = new_lines - 1; |
| 551 |
fodder.emplace_back(FodderElement::LINE_END, blanks, indent, EMPTY); |
| 552 |
fresh_line = true; |
| 553 |
} |
| 554 |
|
| 555 |
Location begin(line_number, c - line_start + 1); |
| 556 |
|
| 557 |
switch (*c) { |
| 558 |
// The following operators should never be combined with subsequent symbols. |
| 559 |
case '{': |
| 560 |
kind = Token::BRACE_L; |
| 561 |
c++; |
| 562 |
break; |
| 563 |
|
| 564 |
case '}': |
| 565 |
kind = Token::BRACE_R; |
| 566 |
c++; |
| 567 |
break; |
| 568 |
|
| 569 |
case '[': |
| 570 |
kind = Token::BRACKET_L; |
| 571 |
c++; |
| 572 |
break; |
| 573 |
|
| 574 |
case ']': |
| 575 |
kind = Token::BRACKET_R; |
| 576 |
c++; |
| 577 |
break; |
| 578 |
|
| 579 |
case ',': |
| 580 |
kind = Token::COMMA; |
| 581 |
c++; |
| 582 |
break; |
| 583 |
|
| 584 |
case '.': |
| 585 |
kind = Token::DOT; |
| 586 |
c++; |
| 587 |
break; |
| 588 |
|
| 589 |
case '(': |
| 590 |
kind = Token::PAREN_L; |
| 591 |
c++; |
| 592 |
break; |
| 593 |
|
| 594 |
case ')': |
| 595 |
kind = Token::PAREN_R; |
| 596 |
c++; |
| 597 |
break; |
| 598 |
|
| 599 |
case ';': |
| 600 |
kind = Token::SEMICOLON; |
| 601 |
c++; |
| 602 |
break; |
| 603 |
|
| 604 |
// Numeric literals. |
| 605 |
case '0': |
| 606 |
case '1': |
| 607 |
case '2': |
| 608 |
case '3': |
| 609 |
case '4': |
| 610 |
case '5': |
| 611 |
case '6': |
| 612 |
case '7': |
| 613 |
case '8': |
| 614 |
case '9': |
| 615 |
kind = Token::NUMBER; |
| 616 |
data = lex_number(c, filename, begin); |
| 617 |
break; |
| 618 |
|
| 619 |
// UString literals. |
| 620 |
case '"': { |
| 621 |
c++; |
| 622 |
for (;; ++c) { |
| 623 |
if (*c == '\0') { |
| 624 |
throw StaticError(filename, begin, "unterminated string"); |
| 625 |
} |
| 626 |
if (*c == '"') { |
| 627 |
break; |
| 628 |
} |
| 629 |
if (*c == '\\' && *(c + 1) != '\0') { |
| 630 |
data += *c; |
| 631 |
++c; |
| 632 |
} |
| 633 |
if (*c == '\n') { |
| 634 |
// Maintain line/column counters. |
| 635 |
line_number++; |
| 636 |
line_start = c + 1; |
| 637 |
} |
| 638 |
data += *c; |
| 639 |
} |
| 640 |
c++; // Advance beyond the ". |
| 641 |
kind = Token::STRING_DOUBLE; |
| 642 |
} break; |
| 643 |
|
| 644 |
// UString literals. |
| 645 |
case '\'': { |
| 646 |
c++; |
| 647 |
for (;; ++c) { |
| 648 |
if (*c == '\0') { |
| 649 |
throw StaticError(filename, begin, "unterminated string"); |
| 650 |
} |
| 651 |
if (*c == '\'') { |
| 652 |
break; |
| 653 |
} |
| 654 |
if (*c == '\\' && *(c + 1) != '\0') { |
| 655 |
data += *c; |
| 656 |
++c; |
| 657 |
} |
| 658 |
if (*c == '\n') { |
| 659 |
// Maintain line/column counters. |
| 660 |
line_number++; |
| 661 |
line_start = c + 1; |
| 662 |
} |
| 663 |
data += *c; |
| 664 |
} |
| 665 |
c++; // Advance beyond the '. |
| 666 |
kind = Token::STRING_SINGLE; |
| 667 |
} break; |
| 668 |
|
| 669 |
// Verbatim string literals. |
| 670 |
// ' and " quoting is interpreted here, unlike non-verbatim strings |
| 671 |
// where it is done later by jsonnet_string_unescape. This is OK |
| 672 |
// in this case because no information is lost by resoving the |
| 673 |
// repeated quote into a single quote, so we can go back to the |
| 674 |
// original form in the formatter. |
| 675 |
case '@': { |
| 676 |
c++; |
| 677 |
if (*c != '"' && *c != '\'') { |
| 678 |
std::stringstream ss; |
| 679 |
ss << "couldn't lex verbatim string, junk after '@': " << *c; |
| 680 |
throw StaticError(filename, begin, ss.str()); |
| 681 |
} |
| 682 |
const char quot = *c; |
| 683 |
c++; // Advance beyond the opening quote. |
| 684 |
for (;; ++c) { |
| 685 |
if (*c == '\0') { |
| 686 |
throw StaticError(filename, begin, "unterminated verbatim string"); |
| 687 |
} |
| 688 |
if (*c == quot) { |
| 689 |
if (*(c + 1) == quot) { |
| 690 |
c++; |
| 691 |
} else { |
| 692 |
break; |
| 693 |
} |
| 694 |
} |
| 695 |
data += *c; |
| 696 |
} |
| 697 |
c++; // Advance beyond the closing quote. |
| 698 |
if (quot == '"') { |
| 699 |
kind = Token::VERBATIM_STRING_DOUBLE; |
| 700 |
} else { |
| 701 |
kind = Token::VERBATIM_STRING_SINGLE; |
| 702 |
} |
| 703 |
} break; |
| 704 |
|
| 705 |
// Keywords |
| 706 |
default: |
| 707 |
if (is_identifier_first(*c)) { |
| 708 |
std::string id; |
| 709 |
for (; is_identifier(*c); ++c) |
| 710 |
id += *c; |
| 711 |
kind = lex_get_keyword_kind(id); |
| 712 |
data = id; |
| 713 |
|
| 714 |
} else if (is_symbol(*c) || *c == '#') { |
| 715 |
// Single line C++ and Python style comments. |
| 716 |
if (*c == '#' || (*c == '/' && *(c + 1) == '/')) { |
| 717 |
std::vector<std::string> comment(1); |
| 718 |
unsigned blanks; |
| 719 |
unsigned indent; |
| 720 |
lex_until_newline(c, comment[0], blanks, indent, line_start, line_number); |
| 721 |
auto kind = fresh_line ? FodderElement::PARAGRAPH : FodderElement::LINE_END; |
| 722 |
fodder.emplace_back(kind, blanks, indent, comment); |
| 723 |
fresh_line = true; |
| 724 |
continue; // We've not got a token, just fodder, so keep scanning. |
| 725 |
} |
| 726 |
|
| 727 |
// Multi-line C style comment. |
| 728 |
if (*c == '/' && *(c + 1) == '*') { |
| 729 |
unsigned margin = c - line_start; |
| 730 |
|
| 731 |
const char *initial_c = c; |
| 732 |
c += 2; // Avoid matching /*/: skip the /* before starting the search for |
| 733 |
// */. |
| 734 |
|
| 735 |
while (!(*c == '*' && *(c + 1) == '/')) { |
| 736 |
if (*c == '\0') { |
| 737 |
auto msg = "multi-line comment has no terminating */."; |
| 738 |
throw StaticError(filename, begin, msg); |
| 739 |
} |
| 740 |
if (*c == '\n') { |
| 741 |
// Just keep track of the line / column counters. |
| 742 |
line_number++; |
| 743 |
line_start = c + 1; |
| 744 |
} |
| 745 |
++c; |
| 746 |
} |
| 747 |
c += 2; // Move the pointer to the char after the closing '/'. |
| 748 |
|
| 749 |
std::string comment(initial_c, |
| 750 |
c - initial_c); // Includes the "/*" and "*/". |
| 751 |
|
| 752 |
// Lex whitespace after comment |
| 753 |
unsigned new_lines_after, indent_after; |
| 754 |
lex_ws(c, new_lines_after, indent_after, line_start, line_number); |
| 755 |
std::vector<std::string> lines; |
| 756 |
if (comment.find('\n') >= comment.length()) { |
| 757 |
// Comment looks like /* foo */ |
| 758 |
lines.push_back(comment); |
| 759 |
fodder.emplace_back(FodderElement::INTERSTITIAL, 0, 0, lines); |
| 760 |
if (new_lines_after > 0) { |
| 761 |
fodder.emplace_back(FodderElement::LINE_END, |
| 762 |
new_lines_after - 1, |
| 763 |
indent_after, |
| 764 |
EMPTY); |
| 765 |
fresh_line = true; |
| 766 |
} |
| 767 |
} else { |
| 768 |
lines = line_split(comment, margin); |
| 769 |
assert(lines[0][0] == '/'); |
| 770 |
// Little hack to support PARAGRAPHs with * down the LHS: |
| 771 |
// Add a space to lines that start with a '*' |
| 772 |
bool all_star = true; |
| 773 |
for (auto &l : lines) { |
| 774 |
if (l[0] != '*') |
| 775 |
all_star = false; |
| 776 |
} |
| 777 |
if (all_star) { |
| 778 |
for (auto &l : lines) { |
| 779 |
if (l[0] == '*') |
| 780 |
l = " " + l; |
| 781 |
} |
| 782 |
} |
| 783 |
if (new_lines_after == 0) { |
| 784 |
// Ensure a line end after the paragraph. |
| 785 |
new_lines_after = 1; |
| 786 |
indent_after = 0; |
| 787 |
} |
| 788 |
fodder_push_back(fodder, |
| 789 |
FodderElement(FodderElement::PARAGRAPH, |
| 790 |
new_lines_after - 1, |
| 791 |
indent_after, |
| 792 |
lines)); |
| 793 |
fresh_line = true; |
| 794 |
} |
| 795 |
continue; // We've not got a token, just fodder, so keep scanning. |
| 796 |
} |
| 797 |
|
| 798 |
// Text block |
| 799 |
if (*c == '|' && *(c + 1) == '|' && *(c + 2) == '|') { |
| 800 |
c += 3; // Skip the "|||". |
| 801 |
|
| 802 |
bool chomp_trailing_nl = false; |
| 803 |
if (*c == '-') { |
| 804 |
chomp_trailing_nl = true; |
| 805 |
c++; |
| 806 |
} |
| 807 |
|
| 808 |
while (is_horz_ws(*c)) ++c; // Chomp whitespace at end of line. |
| 809 |
if (*c != '\n') { |
| 810 |
auto msg = "text block syntax requires new line after |||."; |
| 811 |
throw StaticError(filename, begin, msg); |
| 812 |
} |
| 813 |
std::stringstream block; |
| 814 |
c++; // Skip the "\n" |
| 815 |
line_number++; |
| 816 |
// Skip any blank lines at the beginning of the block. |
| 817 |
while (*c == '\n') { |
| 818 |
line_number++; |
| 819 |
++c; |
| 820 |
block << '\n'; |
| 821 |
} |
| 822 |
line_start = c; |
| 823 |
const char *first_line = c; |
| 824 |
int ws_chars = whitespace_check(first_line, c); |
| 825 |
string_block_indent = std::string(first_line, ws_chars); |
| 826 |
if (ws_chars == 0) { |
| 827 |
auto msg = "text block's first line must start with whitespace."; |
| 828 |
throw StaticError(filename, begin, msg); |
| 829 |
} |
| 830 |
while (true) { |
| 831 |
assert(ws_chars > 0); |
| 832 |
// Read up to the \n |
| 833 |
for (c = &c[ws_chars]; *c != '\n'; ++c) { |
| 834 |
if (*c == '\0') |
| 835 |
throw StaticError(filename, begin, "unexpected EOF"); |
| 836 |
block << *c; |
| 837 |
} |
| 838 |
// Add the \n |
| 839 |
block << '\n'; |
| 840 |
++c; |
| 841 |
line_number++; |
| 842 |
line_start = c; |
| 843 |
// Skip any blank lines |
| 844 |
while (*c == '\n') { |
| 845 |
line_number++; |
| 846 |
++c; |
| 847 |
block << '\n'; |
| 848 |
} |
| 849 |
// Examine next line |
| 850 |
ws_chars = whitespace_check(first_line, c); |
| 851 |
if (ws_chars == 0) { |
| 852 |
// End of text block (or indentation error). |
| 853 |
// Count actual whitespace on this line. |
| 854 |
int actual_ws = 0; |
| 855 |
while (c[actual_ws] == ' ' || |
| 856 |
c[actual_ws] == '\t') { |
| 857 |
actual_ws++; |
| 858 |
} |
| 859 |
|
| 860 |
// Check if this is the terminator ||| |
| 861 |
bool is_terminator = ( |
| 862 |
c[actual_ws] == '|' && |
| 863 |
c[actual_ws + 1] == '|' && |
| 864 |
c[actual_ws + 2] == '|'); |
| 865 |
|
| 866 |
if (!is_terminator) { |
| 867 |
// Not a terminator - check if it's an |
| 868 |
// indentation issue. |
| 869 |
if (actual_ws > 0) { |
| 870 |
// Has whitespace but doesn't match expected |
| 871 |
// indentation. |
| 872 |
std::stringstream msg; |
| 873 |
msg << "text block indentation mismatch: " |
| 874 |
"expected at least "; |
| 875 |
describe_whitespace(msg, string_block_indent); |
| 876 |
msg << ", found "; |
| 877 |
describe_whitespace(msg, std::string(c, actual_ws)); |
| 878 |
throw StaticError(filename, begin, msg.str()); |
| 879 |
} else { |
| 880 |
// No whitespace and no ||| - missing |
| 881 |
// terminator. |
| 882 |
auto msg = |
| 883 |
"text block not terminated with |||"; |
| 884 |
throw StaticError(filename, begin, msg); |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
// Valid termination - skip over any whitespace. |
| 889 |
while (*c == ' ' || *c == '\t') { |
| 890 |
string_block_term_indent += *c; |
| 891 |
++c; |
| 892 |
} |
| 893 |
// Skip the ||| |
| 894 |
c += 3; // Leave after the last | |
| 895 |
data = block.str(); |
| 896 |
kind = Token::STRING_BLOCK; |
| 897 |
if (chomp_trailing_nl) { |
| 898 |
assert(data.back() == '\n'); |
| 899 |
data.pop_back(); |
| 900 |
} |
| 901 |
break; // Out of the while loop. |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
break; // Out of the switch. |
| 906 |
} |
| 907 |
|
| 908 |
const char *operator_begin = c; |
| 909 |
for (; is_symbol(*c); ++c) { |
| 910 |
// Not allowed // in operators |
| 911 |
if (*c == '/' && *(c + 1) == '/') |
| 912 |
break; |
| 913 |
// Not allowed /* in operators |
| 914 |
if (*c == '/' && *(c + 1) == '*') |
| 915 |
break; |
| 916 |
// Not allowed ||| in operators |
| 917 |
if (*c == '|' && *(c + 1) == '|' && *(c + 2) == '|') |
| 918 |
break; |
| 919 |
} |
| 920 |
// Not allowed to end with a + - ~ ! unless a single char. |
| 921 |
// So, wind it back if we need to (but not too far). |
| 922 |
while (c > operator_begin + 1 && !allowed_at_end_of_operator(*(c - 1))) { |
| 923 |
c--; |
| 924 |
} |
| 925 |
data += std::string(operator_begin, c); |
| 926 |
if (data == "$") { |
| 927 |
kind = Token::DOLLAR; |
| 928 |
data = ""; |
| 929 |
} else { |
| 930 |
kind = Token::OPERATOR; |
| 931 |
} |
| 932 |
} else { |
| 933 |
std::stringstream ss; |
| 934 |
ss << "Could not lex the character "; |
| 935 |
auto uc = (unsigned char)(*c); |
| 936 |
if (*c < 32) |
| 937 |
ss << "code " << unsigned(uc); |
| 938 |
else |
| 939 |
ss << "'" << *c << "'"; |
| 940 |
throw StaticError(filename, begin, ss.str()); |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
// Ensure that a bug in the above code does not cause an infinite memory consuming loop due |
| 945 |
// to pushing empty tokens. |
| 946 |
if (c == original_c) { |
| 947 |
throw StaticError(filename, begin, "internal lexing error: pointer did not advance"); |
| 948 |
} |
| 949 |
|
| 950 |
Location end(line_number, (c + 1) - line_start); |
| 951 |
r.emplace_back(kind, |
| 952 |
fodder, |
| 953 |
data, |
| 954 |
string_block_indent, |
| 955 |
string_block_term_indent, |
| 956 |
LocationRange(filename, begin, end)); |
| 957 |
fodder.clear(); |
| 958 |
fresh_line = false; |
| 959 |
} |
| 960 |
|
| 961 |
Location begin(line_number, c - line_start + 1); |
| 962 |
Location end(line_number, (c + 1) - line_start + 1); |
| 963 |
r.emplace_back(Token::END_OF_FILE, fodder, "", "", "", LocationRange(filename, begin, end)); |
| 964 |
return r; |
| 965 |
} |
| 966 |
|
| 967 |
std::string jsonnet_unlex(const Tokens &tokens) |
| 968 |
{ |
| 969 |
std::stringstream ss; |
| 970 |
for (const auto &t : tokens) { |
| 971 |
for (const auto &f : t.fodder) { |
| 972 |
switch (f.kind) { |
| 973 |
case FodderElement::LINE_END: { |
| 974 |
if (f.comment.size() > 0) { |
| 975 |
ss << "LineEnd(" << f.blanks << ", " << f.indent << ", " << f.comment[0] |
| 976 |
<< ")\n"; |
| 977 |
} else { |
| 978 |
ss << "LineEnd(" << f.blanks << ", " << f.indent << ")\n"; |
| 979 |
} |
| 980 |
} break; |
| 981 |
|
| 982 |
case FodderElement::INTERSTITIAL: { |
| 983 |
ss << "Interstitial(" << f.comment[0] << ")\n"; |
| 984 |
} break; |
| 985 |
|
| 986 |
case FodderElement::PARAGRAPH: { |
| 987 |
ss << "Paragraph(\n"; |
| 988 |
for (const auto &line : f.comment) { |
| 989 |
ss << " " << line << '\n'; |
| 990 |
} |
| 991 |
ss << ")" << f.blanks << "\n"; |
| 992 |
} break; |
| 993 |
} |
| 994 |
} |
| 995 |
if (t.kind == Token::END_OF_FILE) { |
| 996 |
ss << "EOF\n"; |
| 997 |
break; |
| 998 |
} |
| 999 |
if (t.kind == Token::STRING_DOUBLE) { |
| 1000 |
ss << "\"" << t.data << "\"\n"; |
| 1001 |
} else if (t.kind == Token::STRING_SINGLE) { |
| 1002 |
ss << "'" << t.data << "'\n"; |
| 1003 |
} else if (t.kind == Token::STRING_BLOCK) { |
| 1004 |
ss << "|||\n"; |
| 1005 |
ss << t.stringBlockIndent; |
| 1006 |
for (const char *cp = t.data.c_str(); *cp != '\0'; ++cp) { |
| 1007 |
ss << *cp; |
| 1008 |
if (*cp == '\n' && *(cp + 1) != '\n' && *(cp + 1) != '\0') { |
| 1009 |
ss << t.stringBlockIndent; |
| 1010 |
} |
| 1011 |
} |
| 1012 |
ss << t.stringBlockTermIndent << "|||\n"; |
| 1013 |
} else { |
| 1014 |
ss << t.data << "\n"; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
return ss.str(); |
| 1018 |
} |
| 1019 |
|
| 1020 |
} // namespace jsonnet::internal |