| 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 |
#ifndef JSONNET_AST_H |
| 18 |
#define JSONNET_AST_H |
| 19 |
|
| 20 |
#include <cassert> |
| 21 |
#include <cstdlib> |
| 22 |
|
| 23 |
#include <iostream> |
| 24 |
#include <list> |
| 25 |
#include <map> |
| 26 |
#include <string> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include "lexer.h" |
| 30 |
#include "unicode.h" |
| 31 |
|
| 32 |
namespace jsonnet::internal { |
| 33 |
|
| 34 |
enum ASTType { |
| 35 |
AST_APPLY, |
| 36 |
AST_APPLY_BRACE, |
| 37 |
AST_ARRAY, |
| 38 |
AST_ARRAY_COMPREHENSION, |
| 39 |
AST_ARRAY_COMPREHENSION_SIMPLE, |
| 40 |
AST_ASSERT, |
| 41 |
AST_BINARY, |
| 42 |
AST_BUILTIN_FUNCTION, |
| 43 |
AST_BUILTIN_FUNCTION_BODY, |
| 44 |
AST_CONDITIONAL, |
| 45 |
AST_DESUGARED_OBJECT, |
| 46 |
AST_DOLLAR, |
| 47 |
AST_ERROR, |
| 48 |
AST_FUNCTION, |
| 49 |
AST_IMPORT, |
| 50 |
AST_IMPORTSTR, |
| 51 |
AST_IMPORTBIN, |
| 52 |
AST_INDEX, |
| 53 |
AST_IN_SUPER, |
| 54 |
AST_LITERAL_BOOLEAN, |
| 55 |
AST_LITERAL_NULL, |
| 56 |
AST_LITERAL_NUMBER, |
| 57 |
AST_LITERAL_STRING, |
| 58 |
AST_LOCAL, |
| 59 |
AST_OBJECT, |
| 60 |
AST_OBJECT_COMPREHENSION, |
| 61 |
AST_OBJECT_COMPREHENSION_SIMPLE, |
| 62 |
AST_PARENS, |
| 63 |
AST_SELF, |
| 64 |
AST_SUPER_INDEX, |
| 65 |
AST_UNARY, |
| 66 |
AST_VAR |
| 67 |
}; |
| 68 |
|
| 69 |
static inline std::string ASTTypeToString(ASTType type) |
| 70 |
{ |
| 71 |
switch (type) { |
| 72 |
case AST_APPLY: return "AST_APPLY"; |
| 73 |
case AST_APPLY_BRACE: return "AST_APPLY_BRACE"; |
| 74 |
case AST_ARRAY: return "AST_ARRAY"; |
| 75 |
case AST_ARRAY_COMPREHENSION: return "AST_ARRAY_COMPREHENSION"; |
| 76 |
case AST_ARRAY_COMPREHENSION_SIMPLE: return "AST_ARRAY_COMPREHENSION_SIMPLE"; |
| 77 |
case AST_ASSERT: return "AST_ASSERT"; |
| 78 |
case AST_BINARY: return "AST_BINARY"; |
| 79 |
case AST_BUILTIN_FUNCTION: return "AST_BUILTIN_FUNCTION"; |
| 80 |
case AST_BUILTIN_FUNCTION_BODY: return "AST_BUILTIN_FUNCTION_BODY"; |
| 81 |
case AST_CONDITIONAL: return "AST_CONDITIONAL"; |
| 82 |
case AST_DESUGARED_OBJECT: return "AST_DESUGARED_OBJECT"; |
| 83 |
case AST_DOLLAR: return "AST_DOLLAR"; |
| 84 |
case AST_ERROR: return "AST_ERROR"; |
| 85 |
case AST_FUNCTION: return "AST_FUNCTION"; |
| 86 |
case AST_IMPORT: return "AST_IMPORT"; |
| 87 |
case AST_IMPORTSTR: return "AST_IMPORTSTR"; |
| 88 |
case AST_IMPORTBIN: return "AST_IMPORTBIN"; |
| 89 |
case AST_INDEX: return "AST_INDEX"; |
| 90 |
case AST_IN_SUPER: return "AST_IN_SUPER"; |
| 91 |
case AST_LITERAL_BOOLEAN: return "AST_LITERAL_BOOLEAN"; |
| 92 |
case AST_LITERAL_NULL: return "AST_LITERAL_NULL"; |
| 93 |
case AST_LITERAL_NUMBER: return "AST_LITERAL_NUMBER"; |
| 94 |
case AST_LITERAL_STRING: return "AST_LITERAL_STRING"; |
| 95 |
case AST_LOCAL: return "AST_LOCAL"; |
| 96 |
case AST_OBJECT: return "AST_OBJECT"; |
| 97 |
case AST_OBJECT_COMPREHENSION: return "AST_OBJECT_COMPREHENSION"; |
| 98 |
case AST_OBJECT_COMPREHENSION_SIMPLE: return "AST_OBJECT_COMPREHENSION_SIMPLE"; |
| 99 |
case AST_PARENS: return "AST_PARENS"; |
| 100 |
case AST_SELF: return "AST_SELF"; |
| 101 |
case AST_SUPER_INDEX: return "AST_SUPER_INDEX"; |
| 102 |
case AST_UNARY: return "AST_UNARY"; |
| 103 |
case AST_VAR: return "AST_VAR"; |
| 104 |
} |
| 105 |
std::cerr << "Invalid AST type" |
| 106 |
<< "\n"; |
| 107 |
abort(); |
| 108 |
} |
| 109 |
|
| 110 |
/** Represents a variable / parameter / field name. */ |
| 111 |
struct Identifier { |
| 112 |
UString name; |
| 113 |
Identifier(const UString &name) : name(name) {} |
| 114 |
}; |
| 115 |
|
| 116 |
static inline std::ostream &operator<<(std::ostream &o, const Identifier *id) |
| 117 |
{ |
| 118 |
o << encode_utf8(id->name); |
| 119 |
return o; |
| 120 |
} |
| 121 |
|
| 122 |
typedef std::vector<const Identifier *> Identifiers; |
| 123 |
|
| 124 |
/** All AST nodes are subtypes of this class. |
| 125 |
*/ |
| 126 |
struct AST { |
| 127 |
LocationRange location; |
| 128 |
ASTType type; |
| 129 |
Fodder openFodder; |
| 130 |
Identifiers freeVariables; |
| 131 |
AST(const LocationRange &location, ASTType type, const Fodder &open_fodder) |
| 132 |
: location(location), type(type), openFodder(open_fodder) |
| 133 |
{ |
| 134 |
} |
| 135 |
virtual ~AST(void) {} |
| 136 |
}; |
| 137 |
|
| 138 |
typedef std::list<AST *> ASTs; |
| 139 |
|
| 140 |
/** Either an arg in a function apply, or a param in a closure / other function definition. |
| 141 |
* |
| 142 |
* They happen to have exactly the same structure. |
| 143 |
* |
| 144 |
* In the case of an arg, the id is optional and the expr is required. Presence of the id indicates |
| 145 |
* that this is a named rather than positional argument. |
| 146 |
* |
| 147 |
* In the case of a param, the id is required and if expr is given, it is a default argument to be |
| 148 |
* used when no argument is bound to the param. |
| 149 |
*/ |
| 150 |
struct ArgParam { |
| 151 |
Fodder idFodder; // Empty if no id. |
| 152 |
const Identifier *id; // nullptr if there isn't one |
| 153 |
Fodder eqFodder; // Empty if no id or no expr. |
| 154 |
AST *expr; // nullptr if there wasn't one. |
| 155 |
Fodder commaFodder; // Before the comma (if there is a comma). |
| 156 |
// Only has id |
| 157 |
ArgParam(const Fodder &id_fodder, const Identifier *id, const Fodder &comma_fodder) |
| 158 |
: idFodder(id_fodder), id(id), expr(nullptr), commaFodder(comma_fodder) |
| 159 |
{ |
| 160 |
} |
| 161 |
// Only has expr |
| 162 |
ArgParam(AST *expr, const Fodder &comma_fodder) |
| 163 |
: id(nullptr), expr(expr), commaFodder(comma_fodder) |
| 164 |
{ |
| 165 |
} |
| 166 |
// Has both id and expr |
| 167 |
ArgParam(const Fodder &id_fodder, const Identifier *id, const Fodder &eq_fodder, AST *expr, |
| 168 |
const Fodder &comma_fodder) |
| 169 |
: idFodder(id_fodder), id(id), eqFodder(eq_fodder), expr(expr), commaFodder(comma_fodder) |
| 170 |
{ |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
typedef std::vector<ArgParam> ArgParams; |
| 175 |
|
| 176 |
/** Used in Object & Array Comprehensions. */ |
| 177 |
struct ComprehensionSpec { |
| 178 |
enum Kind { FOR, IF }; |
| 179 |
Kind kind; |
| 180 |
Fodder openFodder; |
| 181 |
Fodder varFodder; // {} when kind != SPEC_FOR. |
| 182 |
const Identifier *var; // Null when kind != SPEC_FOR. |
| 183 |
Fodder inFodder; // {} when kind != SPEC_FOR. |
| 184 |
AST *expr; |
| 185 |
ComprehensionSpec(Kind kind, const Fodder &open_fodder, const Fodder &var_fodder, |
| 186 |
const Identifier *var, const Fodder &in_fodder, AST *expr) |
| 187 |
: kind(kind), |
| 188 |
openFodder(open_fodder), |
| 189 |
varFodder(var_fodder), |
| 190 |
var(var), |
| 191 |
inFodder(in_fodder), |
| 192 |
expr(expr) |
| 193 |
{ |
| 194 |
} |
| 195 |
}; |
| 196 |
|
| 197 |
/** Represents function calls. */ |
| 198 |
struct Apply : public AST { |
| 199 |
AST *target; |
| 200 |
Fodder fodderL; |
| 201 |
ArgParams args; |
| 202 |
bool trailingComma; |
| 203 |
Fodder fodderR; |
| 204 |
Fodder tailstrictFodder; |
| 205 |
bool tailstrict; |
| 206 |
Apply(const LocationRange &lr, const Fodder &open_fodder, AST *target, const Fodder &fodder_l, |
| 207 |
const ArgParams &args, bool trailing_comma, const Fodder &fodder_r, |
| 208 |
const Fodder &tailstrict_fodder, bool tailstrict) |
| 209 |
: AST(lr, AST_APPLY, open_fodder), |
| 210 |
target(target), |
| 211 |
fodderL(fodder_l), |
| 212 |
args(args), |
| 213 |
trailingComma(trailing_comma), |
| 214 |
fodderR(fodder_r), |
| 215 |
tailstrictFodder(tailstrict_fodder), |
| 216 |
tailstrict(tailstrict) |
| 217 |
{ |
| 218 |
} |
| 219 |
}; |
| 220 |
|
| 221 |
/** Represents e { }. Desugared to e + { }. */ |
| 222 |
struct ApplyBrace : public AST { |
| 223 |
AST *left; |
| 224 |
AST *right; // This is always an object or object comprehension. |
| 225 |
ApplyBrace(const LocationRange &lr, const Fodder &open_fodder, AST *left, AST *right) |
| 226 |
: AST(lr, AST_APPLY_BRACE, open_fodder), left(left), right(right) |
| 227 |
{ |
| 228 |
} |
| 229 |
}; |
| 230 |
|
| 231 |
/** Represents array constructors [1, 2, 3]. */ |
| 232 |
struct Array : public AST { |
| 233 |
struct Element { |
| 234 |
AST *expr; |
| 235 |
Fodder commaFodder; |
| 236 |
Element(AST *expr, const Fodder &comma_fodder) : expr(expr), commaFodder(comma_fodder) {} |
| 237 |
}; |
| 238 |
typedef std::vector<Element> Elements; |
| 239 |
Elements elements; |
| 240 |
bool trailingComma; |
| 241 |
Fodder closeFodder; |
| 242 |
Array(const LocationRange &lr, const Fodder &open_fodder, const Elements &elements, |
| 243 |
bool trailing_comma, const Fodder &close_fodder) |
| 244 |
: AST(lr, AST_ARRAY, open_fodder), |
| 245 |
elements(elements), |
| 246 |
trailingComma(trailing_comma), |
| 247 |
closeFodder(close_fodder) |
| 248 |
{ |
| 249 |
} |
| 250 |
}; |
| 251 |
|
| 252 |
/** Represents array comprehensions (which are like Python list comprehensions). */ |
| 253 |
struct ArrayComprehension : public AST { |
| 254 |
AST *body; |
| 255 |
Fodder commaFodder; |
| 256 |
bool trailingComma; |
| 257 |
std::vector<ComprehensionSpec> specs; |
| 258 |
Fodder closeFodder; |
| 259 |
ArrayComprehension(const LocationRange &lr, const Fodder &open_fodder, AST *body, |
| 260 |
const Fodder &comma_fodder, bool trailing_comma, |
| 261 |
const std::vector<ComprehensionSpec> &specs, const Fodder &close_fodder) |
| 262 |
: AST(lr, AST_ARRAY_COMPREHENSION, open_fodder), |
| 263 |
body(body), |
| 264 |
commaFodder(comma_fodder), |
| 265 |
trailingComma(trailing_comma), |
| 266 |
specs(specs), |
| 267 |
closeFodder(close_fodder) |
| 268 |
{ |
| 269 |
assert(specs.size() > 0); |
| 270 |
} |
| 271 |
}; |
| 272 |
|
| 273 |
/** Represents an assert expression (not an object-level assert). |
| 274 |
* |
| 275 |
* After parsing, message can be nullptr indicating that no message was specified. This AST is |
| 276 |
* elimiated by desugaring. |
| 277 |
*/ |
| 278 |
struct Assert : public AST { |
| 279 |
AST *cond; |
| 280 |
Fodder colonFodder; |
| 281 |
AST *message; |
| 282 |
Fodder semicolonFodder; |
| 283 |
AST *rest; |
| 284 |
Assert(const LocationRange &lr, const Fodder &open_fodder, AST *cond, |
| 285 |
const Fodder &colon_fodder, AST *message, const Fodder &semicolon_fodder, AST *rest) |
| 286 |
: AST(lr, AST_ASSERT, open_fodder), |
| 287 |
cond(cond), |
| 288 |
colonFodder(colon_fodder), |
| 289 |
message(message), |
| 290 |
semicolonFodder(semicolon_fodder), |
| 291 |
rest(rest) |
| 292 |
{ |
| 293 |
} |
| 294 |
}; |
| 295 |
|
| 296 |
enum BinaryOp { |
| 297 |
BOP_MULT, |
| 298 |
BOP_DIV, |
| 299 |
BOP_PERCENT, |
| 300 |
|
| 301 |
BOP_PLUS, |
| 302 |
BOP_MINUS, |
| 303 |
|
| 304 |
BOP_SHIFT_L, |
| 305 |
BOP_SHIFT_R, |
| 306 |
|
| 307 |
BOP_GREATER, |
| 308 |
BOP_GREATER_EQ, |
| 309 |
BOP_LESS, |
| 310 |
BOP_LESS_EQ, |
| 311 |
BOP_IN, |
| 312 |
|
| 313 |
BOP_MANIFEST_EQUAL, |
| 314 |
BOP_MANIFEST_UNEQUAL, |
| 315 |
|
| 316 |
BOP_BITWISE_AND, |
| 317 |
BOP_BITWISE_XOR, |
| 318 |
BOP_BITWISE_OR, |
| 319 |
|
| 320 |
BOP_AND, |
| 321 |
BOP_OR |
| 322 |
}; |
| 323 |
|
| 324 |
static inline std::string bop_string(BinaryOp bop) |
| 325 |
{ |
| 326 |
switch (bop) { |
| 327 |
case BOP_MULT: return "*"; |
| 328 |
case BOP_DIV: return "/"; |
| 329 |
case BOP_PERCENT: return "%"; |
| 330 |
|
| 331 |
case BOP_PLUS: return "+"; |
| 332 |
case BOP_MINUS: return "-"; |
| 333 |
|
| 334 |
case BOP_SHIFT_L: return "<<"; |
| 335 |
case BOP_SHIFT_R: return ">>"; |
| 336 |
|
| 337 |
case BOP_GREATER: return ">"; |
| 338 |
case BOP_GREATER_EQ: return ">="; |
| 339 |
case BOP_LESS: return "<"; |
| 340 |
case BOP_LESS_EQ: return "<="; |
| 341 |
case BOP_IN: return "in"; |
| 342 |
|
| 343 |
case BOP_MANIFEST_EQUAL: return "=="; |
| 344 |
case BOP_MANIFEST_UNEQUAL: return "!="; |
| 345 |
|
| 346 |
case BOP_BITWISE_AND: return "&"; |
| 347 |
case BOP_BITWISE_XOR: return "^"; |
| 348 |
case BOP_BITWISE_OR: return "|"; |
| 349 |
|
| 350 |
case BOP_AND: return "&&"; |
| 351 |
case BOP_OR: return "||"; |
| 352 |
|
| 353 |
default: |
| 354 |
std::cerr << "INTERNAL ERROR: Unrecognised binary operator: " << bop << std::endl; |
| 355 |
std::abort(); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
/** Represents binary operators. */ |
| 360 |
struct Binary : public AST { |
| 361 |
AST *left; |
| 362 |
Fodder opFodder; |
| 363 |
BinaryOp op; |
| 364 |
AST *right; |
| 365 |
Binary(const LocationRange &lr, const Fodder &open_fodder, AST *left, const Fodder &op_fodder, |
| 366 |
BinaryOp op, AST *right) |
| 367 |
: AST(lr, AST_BINARY, open_fodder), left(left), opFodder(op_fodder), op(op), right(right) |
| 368 |
{ |
| 369 |
} |
| 370 |
}; |
| 371 |
|
| 372 |
/** Represents the body of a built-in function. |
| 373 |
* |
| 374 |
* There is no parse rule to build this AST; it is generated during desugaring to represent |
| 375 |
* the body of a built-in function. |
| 376 |
*/ |
| 377 |
struct BuiltinFunctionBody : public AST { |
| 378 |
std::string name; |
| 379 |
// TODO: It would probably be cleaner if this a HeapClosure::Params value. |
| 380 |
Identifiers params; |
| 381 |
BuiltinFunctionBody(const LocationRange &lr, const std::string &name, const Identifiers ¶ms) |
| 382 |
: AST(lr, AST_BUILTIN_FUNCTION_BODY, Fodder{}), name(name), params(params) |
| 383 |
{ |
| 384 |
} |
| 385 |
}; |
| 386 |
|
| 387 |
/** Represents built-in functions. |
| 388 |
* |
| 389 |
* There is no parse rule to build this AST. Instead, it is used to build the std object in the |
| 390 |
* interpreter. |
| 391 |
*/ |
| 392 |
struct BuiltinFunction : public AST { |
| 393 |
const BuiltinFunctionBody *body; |
| 394 |
BuiltinFunction(const LocationRange &lr, const BuiltinFunctionBody *body) |
| 395 |
: AST(lr, AST_BUILTIN_FUNCTION, Fodder{}), body(body) |
| 396 |
{ |
| 397 |
} |
| 398 |
}; |
| 399 |
|
| 400 |
/** Represents if then else. |
| 401 |
* |
| 402 |
* After parsing, branchFalse can be nullptr indicating that no else branch was specified. The |
| 403 |
* desugarer fills this in with a LiteralNull. |
| 404 |
*/ |
| 405 |
struct Conditional : public AST { |
| 406 |
AST *cond; |
| 407 |
Fodder thenFodder; |
| 408 |
AST *branchTrue; |
| 409 |
Fodder elseFodder; |
| 410 |
AST *branchFalse; |
| 411 |
Conditional(const LocationRange &lr, const Fodder &open_fodder, AST *cond, |
| 412 |
const Fodder &then_fodder, AST *branch_true, const Fodder &else_fodder, |
| 413 |
AST *branch_false) |
| 414 |
: AST(lr, AST_CONDITIONAL, open_fodder), |
| 415 |
cond(cond), |
| 416 |
thenFodder(then_fodder), |
| 417 |
branchTrue(branch_true), |
| 418 |
elseFodder(else_fodder), |
| 419 |
branchFalse(branch_false) |
| 420 |
{ |
| 421 |
} |
| 422 |
}; |
| 423 |
|
| 424 |
/** Represents the $ keyword. */ |
| 425 |
struct Dollar : public AST { |
| 426 |
Dollar(const LocationRange &lr, const Fodder &open_fodder) : AST(lr, AST_DOLLAR, open_fodder) {} |
| 427 |
}; |
| 428 |
|
| 429 |
/** Represents error e. */ |
| 430 |
struct Error : public AST { |
| 431 |
AST *expr; |
| 432 |
Error(const LocationRange &lr, const Fodder &open_fodder, AST *expr) |
| 433 |
: AST(lr, AST_ERROR, open_fodder), expr(expr) |
| 434 |
{ |
| 435 |
} |
| 436 |
}; |
| 437 |
|
| 438 |
/** Represents closures. */ |
| 439 |
struct Function : public AST { |
| 440 |
Fodder parenLeftFodder; |
| 441 |
ArgParams params; |
| 442 |
bool trailingComma; |
| 443 |
Fodder parenRightFodder; |
| 444 |
AST *body; |
| 445 |
Function(const LocationRange &lr, const Fodder &open_fodder, const Fodder &paren_left_fodder, |
| 446 |
const ArgParams ¶ms, bool trailing_comma, const Fodder &paren_right_fodder, |
| 447 |
AST *body) |
| 448 |
: AST(lr, AST_FUNCTION, open_fodder), |
| 449 |
parenLeftFodder(paren_left_fodder), |
| 450 |
params(params), |
| 451 |
trailingComma(trailing_comma), |
| 452 |
parenRightFodder(paren_right_fodder), |
| 453 |
body(body) |
| 454 |
{ |
| 455 |
} |
| 456 |
}; |
| 457 |
|
| 458 |
struct LiteralString; |
| 459 |
|
| 460 |
/** Represents import "file". */ |
| 461 |
struct Import : public AST { |
| 462 |
LiteralString *file; |
| 463 |
Import(const LocationRange &lr, const Fodder &open_fodder, LiteralString *file) |
| 464 |
: AST(lr, AST_IMPORT, open_fodder), file(file) |
| 465 |
{ |
| 466 |
} |
| 467 |
}; |
| 468 |
|
| 469 |
/** Represents importstr "file". */ |
| 470 |
struct Importstr : public AST { |
| 471 |
LiteralString *file; |
| 472 |
Importstr(const LocationRange &lr, const Fodder &open_fodder, LiteralString *file) |
| 473 |
: AST(lr, AST_IMPORTSTR, open_fodder), file(file) |
| 474 |
{ |
| 475 |
} |
| 476 |
}; |
| 477 |
|
| 478 |
/** Represents importbin "file". */ |
| 479 |
struct Importbin : public AST { |
| 480 |
LiteralString *file; |
| 481 |
Importbin(const LocationRange &lr, const Fodder &open_fodder, LiteralString *file) |
| 482 |
: AST(lr, AST_IMPORTBIN, open_fodder), file(file) |
| 483 |
{ |
| 484 |
} |
| 485 |
}; |
| 486 |
|
| 487 |
/** Represents both e[e] and the syntax sugar e.f. |
| 488 |
* |
| 489 |
* One of index and id will be nullptr before desugaring. After desugaring id will be nullptr. |
| 490 |
*/ |
| 491 |
struct Index : public AST { |
| 492 |
AST *target; |
| 493 |
Fodder dotFodder; // When index is being used, this is the fodder before the [. |
| 494 |
bool isSlice; |
| 495 |
AST *index; |
| 496 |
Fodder endColonFodder; // When end is being used, this is the fodder before the :. |
| 497 |
AST *end; |
| 498 |
Fodder stepColonFodder; // When step is being used, this is the fodder before the :. |
| 499 |
AST *step; |
| 500 |
Fodder idFodder; // When index is being used, this is the fodder before the ]. |
| 501 |
const Identifier *id; |
| 502 |
// Use this constructor for e.f |
| 503 |
Index(const LocationRange &lr, const Fodder &open_fodder, AST *target, const Fodder &dot_fodder, |
| 504 |
const Fodder &id_fodder, const Identifier *id) |
| 505 |
: AST(lr, AST_INDEX, open_fodder), |
| 506 |
target(target), |
| 507 |
dotFodder(dot_fodder), |
| 508 |
isSlice(false), |
| 509 |
index(nullptr), |
| 510 |
end(nullptr), |
| 511 |
step(nullptr), |
| 512 |
idFodder(id_fodder), |
| 513 |
id(id) |
| 514 |
{ |
| 515 |
} |
| 516 |
// Use this constructor for e[x:y:z] with nullptr for index, end or step if not present. |
| 517 |
Index(const LocationRange &lr, const Fodder &open_fodder, AST *target, const Fodder &dot_fodder, |
| 518 |
bool is_slice, AST *index, const Fodder &end_colon_fodder, AST *end, |
| 519 |
const Fodder &step_colon_fodder, AST *step, const Fodder &id_fodder) |
| 520 |
: AST(lr, AST_INDEX, open_fodder), |
| 521 |
target(target), |
| 522 |
dotFodder(dot_fodder), |
| 523 |
isSlice(is_slice), |
| 524 |
index(index), |
| 525 |
endColonFodder(end_colon_fodder), |
| 526 |
end(end), |
| 527 |
stepColonFodder(step_colon_fodder), |
| 528 |
step(step), |
| 529 |
idFodder(id_fodder), |
| 530 |
id(nullptr) |
| 531 |
{ |
| 532 |
} |
| 533 |
}; |
| 534 |
|
| 535 |
/** Represents local x = e; e. After desugaring, functionSugar is false. */ |
| 536 |
struct Local : public AST { |
| 537 |
struct Bind { |
| 538 |
Fodder varFodder; |
| 539 |
const Identifier *var; |
| 540 |
Fodder opFodder; |
| 541 |
AST *body; |
| 542 |
bool functionSugar; |
| 543 |
Fodder parenLeftFodder; |
| 544 |
ArgParams params; // If functionSugar == true |
| 545 |
bool trailingComma; |
| 546 |
Fodder parenRightFodder; |
| 547 |
Fodder closeFodder; |
| 548 |
Bind(const Fodder &var_fodder, const Identifier *var, const Fodder &op_fodder, AST *body, |
| 549 |
bool function_sugar, const Fodder &paren_left_fodder, const ArgParams ¶ms, |
| 550 |
bool trailing_comma, const Fodder &paren_right_fodder, const Fodder &close_fodder) |
| 551 |
: varFodder(var_fodder), |
| 552 |
var(var), |
| 553 |
opFodder(op_fodder), |
| 554 |
body(body), |
| 555 |
functionSugar(function_sugar), |
| 556 |
parenLeftFodder(paren_left_fodder), |
| 557 |
params(params), |
| 558 |
trailingComma(trailing_comma), |
| 559 |
parenRightFodder(paren_right_fodder), |
| 560 |
closeFodder(close_fodder) |
| 561 |
{ |
| 562 |
} |
| 563 |
}; |
| 564 |
typedef std::vector<Bind> Binds; |
| 565 |
Binds binds; |
| 566 |
AST *body; |
| 567 |
Local(const LocationRange &lr, const Fodder &open_fodder, const Binds &binds, AST *body) |
| 568 |
: AST(lr, AST_LOCAL, open_fodder), binds(binds), body(body) |
| 569 |
{ |
| 570 |
} |
| 571 |
}; |
| 572 |
|
| 573 |
/** Represents true and false. */ |
| 574 |
struct LiteralBoolean : public AST { |
| 575 |
bool value; |
| 576 |
LiteralBoolean(const LocationRange &lr, const Fodder &open_fodder, bool value) |
| 577 |
: AST(lr, AST_LITERAL_BOOLEAN, open_fodder), value(value) |
| 578 |
{ |
| 579 |
} |
| 580 |
}; |
| 581 |
|
| 582 |
/** Represents the null keyword. */ |
| 583 |
struct LiteralNull : public AST { |
| 584 |
LiteralNull(const LocationRange &lr, const Fodder &open_fodder) |
| 585 |
: AST(lr, AST_LITERAL_NULL, open_fodder) |
| 586 |
{ |
| 587 |
} |
| 588 |
}; |
| 589 |
|
| 590 |
/** Represents JSON numbers. */ |
| 591 |
struct LiteralNumber : public AST { |
| 592 |
double value; |
| 593 |
std::string originalString; |
| 594 |
LiteralNumber(const LocationRange &lr, const Fodder &open_fodder, const std::string &str) |
| 595 |
: AST(lr, AST_LITERAL_NUMBER, open_fodder), |
| 596 |
value(strtod(str.c_str(), nullptr)), |
| 597 |
originalString(str) |
| 598 |
{ |
| 599 |
} |
| 600 |
}; |
| 601 |
|
| 602 |
/** Represents JSON strings. */ |
| 603 |
struct LiteralString : public AST { |
| 604 |
UString value; |
| 605 |
enum TokenKind { SINGLE, DOUBLE, BLOCK, VERBATIM_SINGLE, VERBATIM_DOUBLE, RAW_DESUGARED }; |
| 606 |
TokenKind tokenKind; |
| 607 |
std::string blockIndent; // Only contains ' ' and '\t'. |
| 608 |
std::string blockTermIndent; // Only contains ' ' and '\t'. |
| 609 |
LiteralString(const LocationRange &lr, const Fodder &open_fodder, const UString &value, |
| 610 |
TokenKind token_kind, const std::string &block_indent, |
| 611 |
const std::string &block_term_indent) |
| 612 |
: AST(lr, AST_LITERAL_STRING, open_fodder), |
| 613 |
value(value), |
| 614 |
tokenKind(token_kind), |
| 615 |
blockIndent(block_indent), |
| 616 |
blockTermIndent(block_term_indent) |
| 617 |
{ |
| 618 |
} |
| 619 |
}; |
| 620 |
|
| 621 |
struct ObjectField { |
| 622 |
// Depending on the kind of Jsonnet field, the fields of this C++ class are used for storing |
| 623 |
// different parts of the AST. |
| 624 |
enum Kind { |
| 625 |
|
| 626 |
// <fodder1> 'assert' <expr2> |
| 627 |
// [ <opFodder> : <expr3> ] |
| 628 |
// <commaFodder> |
| 629 |
ASSERT, |
| 630 |
|
| 631 |
// <fodder1> id |
| 632 |
// [ <fodderL> '(' <params> <fodderR> ')' ] |
| 633 |
// <opFodder> [+]:[:[:]] <expr2> |
| 634 |
// <commaFodder> |
| 635 |
FIELD_ID, |
| 636 |
|
| 637 |
// <fodder1> '[' <expr1> <fodder2> ']' |
| 638 |
// [ <fodderL> '(' <params> <fodderR> ')' ] |
| 639 |
// <opFodder> [+]:[:[:]] <expr2> |
| 640 |
// <commaFodder> |
| 641 |
FIELD_EXPR, |
| 642 |
|
| 643 |
// <expr1> |
| 644 |
// <fodderL> '(' <params> <fodderR> ')' |
| 645 |
// <opFodder> [+]:[:[:]] <expr2> |
| 646 |
// <commaFodder> |
| 647 |
FIELD_STR, |
| 648 |
|
| 649 |
// <fodder1> 'local' <fodder2> id |
| 650 |
// [ <fodderL> '(' <params> <fodderR> ')' ] |
| 651 |
// [ <opFodder> = <expr2> ] |
| 652 |
// <commaFodder> |
| 653 |
LOCAL, |
| 654 |
}; |
| 655 |
|
| 656 |
// NOTE TO SELF: sort out fodder1-4, then modify desugarer (maybe) parser and unparser. |
| 657 |
|
| 658 |
enum Hide { |
| 659 |
HIDDEN, // f:: e |
| 660 |
INHERIT, // f: e |
| 661 |
VISIBLE, // f::: e |
| 662 |
}; |
| 663 |
enum Kind kind; |
| 664 |
Fodder fodder1, fodder2, fodderL, fodderR; |
| 665 |
enum Hide hide; // (ignore if kind != FIELD_something |
| 666 |
bool superSugar; // +: (ignore if kind != FIELD_something) |
| 667 |
bool methodSugar; // f(x, y, z): ... (ignore if kind == ASSERT) |
| 668 |
AST *expr1; // Not in scope of the object |
| 669 |
const Identifier *id; |
| 670 |
LocationRange idLocation; |
| 671 |
ArgParams params; // If methodSugar == true then holds the params. |
| 672 |
bool trailingComma; // If methodSugar == true then remembers the trailing comma. |
| 673 |
Fodder opFodder; // Before the : or = |
| 674 |
AST *expr2, *expr3; // In scope of the object (can see self). |
| 675 |
Fodder commaFodder; // If this field is followed by a comma, this is its fodder. |
| 676 |
|
| 677 |
ObjectField(enum Kind kind, const Fodder &fodder1, const Fodder &fodder2, |
| 678 |
const Fodder &fodder_l, const Fodder &fodder_r, enum Hide hide, bool super_sugar, |
| 679 |
bool method_sugar, AST *expr1, const Identifier *id, const LocationRange &id_lr, |
| 680 |
const ArgParams ¶ms, bool trailing_comma, const Fodder &op_fodder, AST *expr2, |
| 681 |
AST *expr3, const Fodder &comma_fodder) |
| 682 |
: kind(kind), |
| 683 |
fodder1(fodder1), |
| 684 |
fodder2(fodder2), |
| 685 |
fodderL(fodder_l), |
| 686 |
fodderR(fodder_r), |
| 687 |
hide(hide), |
| 688 |
superSugar(super_sugar), |
| 689 |
methodSugar(method_sugar), |
| 690 |
expr1(expr1), |
| 691 |
id(id), |
| 692 |
idLocation(id_lr), |
| 693 |
params(params), |
| 694 |
trailingComma(trailing_comma), |
| 695 |
opFodder(op_fodder), |
| 696 |
expr2(expr2), |
| 697 |
expr3(expr3), |
| 698 |
commaFodder(comma_fodder) |
| 699 |
{ |
| 700 |
// Enforce what is written in comments above. |
| 701 |
assert(kind != ASSERT || (hide == VISIBLE && !superSugar && !methodSugar)); |
| 702 |
assert(kind != LOCAL || (hide == VISIBLE && !superSugar)); |
| 703 |
assert(kind != FIELD_ID || (id != nullptr && expr1 == nullptr)); |
| 704 |
assert(kind == FIELD_ID || kind == LOCAL || id == nullptr); |
| 705 |
assert(methodSugar || (params.size() == 0 && !trailingComma)); |
| 706 |
assert(kind == ASSERT || expr3 == nullptr); |
| 707 |
} |
| 708 |
// For when we don't know if it's a function or not. |
| 709 |
static ObjectField Local(const Fodder &fodder1, const Fodder &fodder2, const Fodder &fodder_l, |
| 710 |
const Fodder &fodder_r, bool method_sugar, const Identifier *id, |
| 711 |
const ArgParams ¶ms, bool trailing_comma, const Fodder &op_fodder, |
| 712 |
AST *body, const Fodder &comma_fodder) |
| 713 |
{ |
| 714 |
return ObjectField(LOCAL, |
| 715 |
fodder1, |
| 716 |
fodder2, |
| 717 |
fodder_l, |
| 718 |
fodder_r, |
| 719 |
VISIBLE, |
| 720 |
false, |
| 721 |
method_sugar, |
| 722 |
nullptr, |
| 723 |
id, |
| 724 |
LocationRange(), |
| 725 |
params, |
| 726 |
trailing_comma, |
| 727 |
op_fodder, |
| 728 |
body, |
| 729 |
nullptr, |
| 730 |
comma_fodder); |
| 731 |
} |
| 732 |
static ObjectField Local(const Fodder &fodder1, const Fodder &fodder2, const Identifier *id, |
| 733 |
const Fodder &op_fodder, AST *body, const Fodder &comma_fodder) |
| 734 |
{ |
| 735 |
return ObjectField(LOCAL, |
| 736 |
fodder1, |
| 737 |
fodder2, |
| 738 |
Fodder{}, |
| 739 |
Fodder{}, |
| 740 |
VISIBLE, |
| 741 |
false, |
| 742 |
false, |
| 743 |
nullptr, |
| 744 |
id, |
| 745 |
LocationRange(), |
| 746 |
ArgParams{}, |
| 747 |
false, |
| 748 |
op_fodder, |
| 749 |
body, |
| 750 |
nullptr, |
| 751 |
comma_fodder); |
| 752 |
} |
| 753 |
static ObjectField LocalMethod(const Fodder &fodder1, const Fodder &fodder2, |
| 754 |
const Fodder &fodder_l, const Fodder &fodder_r, |
| 755 |
const Identifier *id, const ArgParams ¶ms, |
| 756 |
bool trailing_comma, const Fodder &op_fodder, AST *body, |
| 757 |
const Fodder &comma_fodder) |
| 758 |
{ |
| 759 |
return ObjectField(LOCAL, |
| 760 |
fodder1, |
| 761 |
fodder2, |
| 762 |
fodder_l, |
| 763 |
fodder_r, |
| 764 |
VISIBLE, |
| 765 |
false, |
| 766 |
true, |
| 767 |
nullptr, |
| 768 |
id, |
| 769 |
LocationRange(), |
| 770 |
params, |
| 771 |
trailing_comma, |
| 772 |
op_fodder, |
| 773 |
body, |
| 774 |
nullptr, |
| 775 |
comma_fodder); |
| 776 |
} |
| 777 |
static ObjectField Assert(const Fodder &fodder1, AST *body, const Fodder &op_fodder, AST *msg, |
| 778 |
const Fodder &comma_fodder) |
| 779 |
{ |
| 780 |
return ObjectField(ASSERT, |
| 781 |
fodder1, |
| 782 |
Fodder{}, |
| 783 |
Fodder{}, |
| 784 |
Fodder{}, |
| 785 |
VISIBLE, |
| 786 |
false, |
| 787 |
false, |
| 788 |
nullptr, |
| 789 |
nullptr, |
| 790 |
LocationRange(), |
| 791 |
ArgParams{}, |
| 792 |
false, |
| 793 |
op_fodder, |
| 794 |
body, |
| 795 |
msg, |
| 796 |
comma_fodder); |
| 797 |
} |
| 798 |
}; |
| 799 |
typedef std::vector<ObjectField> ObjectFields; |
| 800 |
|
| 801 |
/** Represents object constructors { f: e ... }. |
| 802 |
* |
| 803 |
* The trailing comma is only allowed if fields.size() > 0. Converted to DesugaredObject during |
| 804 |
* desugaring. |
| 805 |
*/ |
| 806 |
struct Object : public AST { |
| 807 |
ObjectFields fields; |
| 808 |
bool trailingComma; |
| 809 |
Fodder closeFodder; |
| 810 |
Object(const LocationRange &lr, const Fodder &open_fodder, const ObjectFields &fields, |
| 811 |
bool trailing_comma, const Fodder &close_fodder) |
| 812 |
: AST(lr, AST_OBJECT, open_fodder), |
| 813 |
fields(fields), |
| 814 |
trailingComma(trailing_comma), |
| 815 |
closeFodder(close_fodder) |
| 816 |
{ |
| 817 |
assert(fields.size() > 0 || !trailing_comma); |
| 818 |
if (fields.size() > 0) |
| 819 |
assert(trailing_comma || fields[fields.size() - 1].commaFodder.size() == 0); |
| 820 |
} |
| 821 |
}; |
| 822 |
|
| 823 |
/** Represents object constructors { f: e ... } after desugaring. |
| 824 |
* |
| 825 |
* The assertions either return true or raise an error. |
| 826 |
*/ |
| 827 |
struct DesugaredObject : public AST { |
| 828 |
struct Field { |
| 829 |
enum ObjectField::Hide hide; |
| 830 |
AST *name; |
| 831 |
AST *body; |
| 832 |
Field(enum ObjectField::Hide hide, AST *name, AST *body) |
| 833 |
: hide(hide), name(name), body(body) |
| 834 |
{ |
| 835 |
} |
| 836 |
}; |
| 837 |
typedef std::vector<Field> Fields; |
| 838 |
ASTs asserts; |
| 839 |
Fields fields; |
| 840 |
DesugaredObject(const LocationRange &lr, const ASTs &asserts, const Fields &fields) |
| 841 |
: AST(lr, AST_DESUGARED_OBJECT, Fodder{}), asserts(asserts), fields(fields) |
| 842 |
{ |
| 843 |
} |
| 844 |
}; |
| 845 |
|
| 846 |
/** Represents object comprehension { [e]: e for x in e for.. if... }. */ |
| 847 |
struct ObjectComprehension : public AST { |
| 848 |
ObjectFields fields; |
| 849 |
bool trailingComma; |
| 850 |
std::vector<ComprehensionSpec> specs; |
| 851 |
Fodder closeFodder; |
| 852 |
ObjectComprehension(const LocationRange &lr, const Fodder &open_fodder, |
| 853 |
const ObjectFields &fields, bool trailing_comma, |
| 854 |
const std::vector<ComprehensionSpec> &specs, const Fodder &close_fodder) |
| 855 |
|
| 856 |
: AST(lr, AST_OBJECT_COMPREHENSION, open_fodder), |
| 857 |
fields(fields), |
| 858 |
trailingComma(trailing_comma), |
| 859 |
specs(specs), |
| 860 |
closeFodder(close_fodder) |
| 861 |
{ |
| 862 |
} |
| 863 |
}; |
| 864 |
|
| 865 |
/** Represents post-desugaring object comprehension { [e]: e for x in e }. */ |
| 866 |
struct ObjectComprehensionSimple : public AST { |
| 867 |
AST *field; |
| 868 |
AST *value; |
| 869 |
const Identifier *id; |
| 870 |
AST *array; |
| 871 |
ObjectComprehensionSimple(const LocationRange &lr, AST *field, AST *value, const Identifier *id, |
| 872 |
AST *array) |
| 873 |
: AST(lr, AST_OBJECT_COMPREHENSION_SIMPLE, Fodder{}), |
| 874 |
field(field), |
| 875 |
value(value), |
| 876 |
id(id), |
| 877 |
array(array) |
| 878 |
{ |
| 879 |
} |
| 880 |
}; |
| 881 |
|
| 882 |
/** Represents (e), which is desugared. */ |
| 883 |
struct Parens : public AST { |
| 884 |
AST *expr; |
| 885 |
Fodder closeFodder; |
| 886 |
Parens(const LocationRange &lr, const Fodder &open_fodder, AST *expr, |
| 887 |
const Fodder &close_fodder) |
| 888 |
: AST(lr, AST_PARENS, open_fodder), expr(expr), closeFodder(close_fodder) |
| 889 |
{ |
| 890 |
} |
| 891 |
}; |
| 892 |
|
| 893 |
/** Represents the self keyword. */ |
| 894 |
struct Self : public AST { |
| 895 |
Self(const LocationRange &lr, const Fodder &open_fodder) : AST(lr, AST_SELF, open_fodder) {} |
| 896 |
}; |
| 897 |
|
| 898 |
/** Represents the super[e] and super.f constructs. |
| 899 |
* |
| 900 |
* Either index or identifier will be set before desugaring. After desugaring, id will be |
| 901 |
* nullptr. |
| 902 |
*/ |
| 903 |
struct SuperIndex : public AST { |
| 904 |
Fodder dotFodder; |
| 905 |
AST *index; |
| 906 |
Fodder idFodder; |
| 907 |
const Identifier *id; |
| 908 |
SuperIndex(const LocationRange &lr, const Fodder &open_fodder, const Fodder &dot_fodder, |
| 909 |
AST *index, const Fodder &id_fodder, const Identifier *id) |
| 910 |
: AST(lr, AST_SUPER_INDEX, open_fodder), |
| 911 |
dotFodder(dot_fodder), |
| 912 |
index(index), |
| 913 |
idFodder(id_fodder), |
| 914 |
id(id) |
| 915 |
{ |
| 916 |
} |
| 917 |
}; |
| 918 |
|
| 919 |
/** Represents the e in super construct. |
| 920 |
*/ |
| 921 |
struct InSuper : public AST { |
| 922 |
AST *element; |
| 923 |
Fodder inFodder; |
| 924 |
Fodder superFodder; |
| 925 |
InSuper(const LocationRange &lr, const Fodder &open_fodder, AST *element, |
| 926 |
const Fodder &in_fodder, const Fodder &super_fodder) |
| 927 |
: AST(lr, AST_IN_SUPER, open_fodder), |
| 928 |
element(element), |
| 929 |
inFodder(in_fodder), |
| 930 |
superFodder(super_fodder) |
| 931 |
{ |
| 932 |
} |
| 933 |
}; |
| 934 |
|
| 935 |
enum UnaryOp { UOP_NOT, UOP_BITWISE_NOT, UOP_PLUS, UOP_MINUS }; |
| 936 |
|
| 937 |
static inline std::string uop_string(UnaryOp uop) |
| 938 |
{ |
| 939 |
switch (uop) { |
| 940 |
case UOP_PLUS: return "+"; |
| 941 |
case UOP_MINUS: return "-"; |
| 942 |
case UOP_BITWISE_NOT: return "~"; |
| 943 |
case UOP_NOT: return "!"; |
| 944 |
|
| 945 |
default: |
| 946 |
std::cerr << "INTERNAL ERROR: Unrecognised unary operator: " << uop << std::endl; |
| 947 |
std::abort(); |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
/** Represents unary operators. */ |
| 952 |
struct Unary : public AST { |
| 953 |
UnaryOp op; |
| 954 |
AST *expr; |
| 955 |
Unary(const LocationRange &lr, const Fodder &open_fodder, UnaryOp op, AST *expr) |
| 956 |
: AST(lr, AST_UNARY, open_fodder), op(op), expr(expr) |
| 957 |
{ |
| 958 |
} |
| 959 |
}; |
| 960 |
|
| 961 |
/** Represents variables. */ |
| 962 |
struct Var : public AST { |
| 963 |
const Identifier *id; |
| 964 |
Var(const LocationRange &lr, const Fodder &open_fodder, const Identifier *id) |
| 965 |
: AST(lr, AST_VAR, open_fodder), id(id) |
| 966 |
{ |
| 967 |
} |
| 968 |
}; |
| 969 |
|
| 970 |
/** Allocates ASTs on demand, frees them in its destructor. |
| 971 |
*/ |
| 972 |
class Allocator { |
| 973 |
std::map<UString, const Identifier *> internedIdentifiers; |
| 974 |
std::map<std::string, const BuiltinFunctionBody *> internedBuiltins; |
| 975 |
ASTs allocated; |
| 976 |
|
| 977 |
public: |
| 978 |
template <class T, class... Args> |
| 979 |
T *make(Args &&... args) |
| 980 |
{ |
| 981 |
auto r = new T(std::forward<Args>(args)...); |
| 982 |
allocated.push_back(r); |
| 983 |
return r; |
| 984 |
} |
| 985 |
|
| 986 |
template <class T> |
| 987 |
T *clone(T *ast) |
| 988 |
{ |
| 989 |
auto r = new T(*ast); |
| 990 |
allocated.push_back(r); |
| 991 |
return r; |
| 992 |
} |
| 993 |
/** Returns interned identifiers. |
| 994 |
* |
| 995 |
* The location used in the Identifier AST is that of the first one parsed. |
| 996 |
*/ |
| 997 |
const Identifier *makeIdentifier(const UString &name) |
| 998 |
{ |
| 999 |
auto it = internedIdentifiers.find(name); |
| 1000 |
if (it != internedIdentifiers.end()) { |
| 1001 |
return it->second; |
| 1002 |
} |
| 1003 |
auto r = new Identifier(name); |
| 1004 |
internedIdentifiers[name] = r; |
| 1005 |
return r; |
| 1006 |
} |
| 1007 |
template <typename F> |
| 1008 |
const BuiltinFunctionBody *makeBuiltinBody(const LocationRange &loc, const std::string &name, F&& make_params) |
| 1009 |
{ |
| 1010 |
auto it = internedBuiltins.find(name); |
| 1011 |
if (it != internedBuiltins.end()) { |
| 1012 |
return it->second; |
| 1013 |
} |
| 1014 |
const Identifiers params = make_params(); |
| 1015 |
auto r = new BuiltinFunctionBody(loc, name, params); |
| 1016 |
internedBuiltins[name] = r; |
| 1017 |
return r; |
| 1018 |
} |
| 1019 |
~Allocator() |
| 1020 |
{ |
| 1021 |
for (auto x : allocated) { |
| 1022 |
delete x; |
| 1023 |
} |
| 1024 |
allocated.clear(); |
| 1025 |
for (const auto &x : internedIdentifiers) { |
| 1026 |
delete x.second; |
| 1027 |
} |
| 1028 |
internedIdentifiers.clear(); |
| 1029 |
for (const auto &x : internedBuiltins) { |
| 1030 |
delete x.second; |
| 1031 |
} |
| 1032 |
internedBuiltins.clear(); |
| 1033 |
} |
| 1034 |
}; |
| 1035 |
|
| 1036 |
namespace { |
| 1037 |
|
| 1038 |
// Precedences used by various compilation units are defined here. |
| 1039 |
const int APPLY_PRECEDENCE = 2; // Function calls and indexing. |
| 1040 |
const int UNARY_PRECEDENCE = 4; // Logical and bitwise negation, unary + - |
| 1041 |
const int MAX_PRECEDENCE = 15; // higher than any other precedence |
| 1042 |
|
| 1043 |
/** These are the binary operator precedences, unary precedence is given by |
| 1044 |
* UNARY_PRECEDENCE. |
| 1045 |
*/ |
| 1046 |
std::map<BinaryOp, int> build_precedence_map(void) |
| 1047 |
{ |
| 1048 |
std::map<BinaryOp, int> r; |
| 1049 |
|
| 1050 |
r[BOP_MULT] = 5; |
| 1051 |
r[BOP_DIV] = 5; |
| 1052 |
r[BOP_PERCENT] = 5; |
| 1053 |
|
| 1054 |
r[BOP_PLUS] = 6; |
| 1055 |
r[BOP_MINUS] = 6; |
| 1056 |
|
| 1057 |
r[BOP_SHIFT_L] = 7; |
| 1058 |
r[BOP_SHIFT_R] = 7; |
| 1059 |
|
| 1060 |
r[BOP_GREATER] = 8; |
| 1061 |
r[BOP_GREATER_EQ] = 8; |
| 1062 |
r[BOP_LESS] = 8; |
| 1063 |
r[BOP_LESS_EQ] = 8; |
| 1064 |
r[BOP_IN] = 8; |
| 1065 |
|
| 1066 |
r[BOP_MANIFEST_EQUAL] = 9; |
| 1067 |
r[BOP_MANIFEST_UNEQUAL] = 9; |
| 1068 |
|
| 1069 |
r[BOP_BITWISE_AND] = 10; |
| 1070 |
|
| 1071 |
r[BOP_BITWISE_XOR] = 11; |
| 1072 |
|
| 1073 |
r[BOP_BITWISE_OR] = 12; |
| 1074 |
|
| 1075 |
r[BOP_AND] = 13; |
| 1076 |
|
| 1077 |
r[BOP_OR] = 14; |
| 1078 |
|
| 1079 |
return r; |
| 1080 |
} |
| 1081 |
|
| 1082 |
std::map<std::string, UnaryOp> build_unary_map(void) |
| 1083 |
{ |
| 1084 |
std::map<std::string, UnaryOp> r; |
| 1085 |
r["!"] = UOP_NOT; |
| 1086 |
r["~"] = UOP_BITWISE_NOT; |
| 1087 |
r["+"] = UOP_PLUS; |
| 1088 |
r["-"] = UOP_MINUS; |
| 1089 |
return r; |
| 1090 |
} |
| 1091 |
|
| 1092 |
std::map<std::string, BinaryOp> build_binary_map(void) |
| 1093 |
{ |
| 1094 |
std::map<std::string, BinaryOp> r; |
| 1095 |
|
| 1096 |
r["*"] = BOP_MULT; |
| 1097 |
r["/"] = BOP_DIV; |
| 1098 |
r["%"] = BOP_PERCENT; |
| 1099 |
|
| 1100 |
r["+"] = BOP_PLUS; |
| 1101 |
r["-"] = BOP_MINUS; |
| 1102 |
|
| 1103 |
r["<<"] = BOP_SHIFT_L; |
| 1104 |
r[">>"] = BOP_SHIFT_R; |
| 1105 |
|
| 1106 |
r[">"] = BOP_GREATER; |
| 1107 |
r[">="] = BOP_GREATER_EQ; |
| 1108 |
r["<"] = BOP_LESS; |
| 1109 |
r["<="] = BOP_LESS_EQ; |
| 1110 |
r["in"] = BOP_IN; |
| 1111 |
|
| 1112 |
r["=="] = BOP_MANIFEST_EQUAL; |
| 1113 |
r["!="] = BOP_MANIFEST_UNEQUAL; |
| 1114 |
|
| 1115 |
r["&"] = BOP_BITWISE_AND; |
| 1116 |
r["^"] = BOP_BITWISE_XOR; |
| 1117 |
r["|"] = BOP_BITWISE_OR; |
| 1118 |
|
| 1119 |
r["&&"] = BOP_AND; |
| 1120 |
r["||"] = BOP_OR; |
| 1121 |
return r; |
| 1122 |
} |
| 1123 |
|
| 1124 |
auto precedence_map = build_precedence_map(); |
| 1125 |
auto unary_map = build_unary_map(); |
| 1126 |
auto binary_map = build_binary_map(); |
| 1127 |
|
| 1128 |
} // namespace |
| 1129 |
|
| 1130 |
} // namespace jsonnet::internal |
| 1131 |
|
| 1132 |
#endif // JSONNET_AST_H |