|
23 | 23 | - [Overloading](#overloading) |
24 | 24 | - [AST functions \(advanced\)](#ast-functions-advanced) |
25 | 25 | - [Security](#security) |
| 26 | +- [Grammar](#grammar) |
26 | 27 | - [Acknowledgments](#acknowledgments) |
27 | 28 |
|
28 | 29 | <!-- /MarkdownTOC --> |
@@ -354,6 +355,133 @@ The following would trigger an exception (or abort the process if exceptions are |
354 | 355 | - running out of heap memory while parsing or evaluating an expression |
355 | 356 |
|
356 | 357 |
|
| 358 | +# Grammar |
| 359 | + |
| 360 | +The formal grammar for the language is provided below. This uses a format similar to [Python's grammar specification](https://docs.python.org/3/reference/grammar.html). |
| 361 | + |
| 362 | +``` |
| 363 | +expression: |
| 364 | + | disjunction 'if' disjunction 'else' expression |
| 365 | + | disjunction |
| 366 | +
|
| 367 | +disjunction: |
| 368 | + | conjunction ('or' conjunction)+ |
| 369 | + | conjunction |
| 370 | +
|
| 371 | +conjunction: |
| 372 | + | inclusion ('and' inclusion)+ |
| 373 | + | inclusion |
| 374 | +
|
| 375 | +inclusion: |
| 376 | + | equality (inclusion_operator equality)+ |
| 377 | + | equality |
| 378 | +
|
| 379 | +inclusion_operator: |
| 380 | + | 'in' |
| 381 | + | 'not in' |
| 382 | +
|
| 383 | +equality: |
| 384 | + | comparison (equality_operator comparison)+ |
| 385 | + | comparison |
| 386 | +
|
| 387 | +equality_operator: |
| 388 | + | '==' |
| 389 | + | '!=' |
| 390 | +
|
| 391 | +comparison: |
| 392 | + | addition (comparison_operator addition)+ |
| 393 | + | addition |
| 394 | +
|
| 395 | +comparison_operator: |
| 396 | + | '<' |
| 397 | + | '<=' |
| 398 | + | '>' |
| 399 | + | '>=' |
| 400 | +
|
| 401 | +addition: |
| 402 | + | multiplication (addition_operator multiplication)+ |
| 403 | + | multiplication |
| 404 | +
|
| 405 | +addition_operator: |
| 406 | + | '+' |
| 407 | + | '-' |
| 408 | +
|
| 409 | +multiplication: |
| 410 | + | exponentiation (multiplication_operator exponentiation)+ |
| 411 | + | exponentiation |
| 412 | +
|
| 413 | +multiplication_operator: |
| 414 | + | '*' |
| 415 | + | '/' |
| 416 | + | '%' |
| 417 | +
|
| 418 | +exponentiation: |
| 419 | + | unary_operation ('**' unary_operation)+ |
| 420 | + | unary_operation |
| 421 | +
|
| 422 | +unary_operation: |
| 423 | + | unary_operator access |
| 424 | + | access |
| 425 | +
|
| 426 | +unary_operator: |
| 427 | + | '+' |
| 428 | + | '-' |
| 429 | + | 'not' |
| 430 | +
|
| 431 | +access: |
| 432 | + | operand '[' expression? ':' expression? ']' |
| 433 | + | operand '[' expression ']' |
| 434 | + | operand '.' identifier |
| 435 | +
|
| 436 | +operand: |
| 437 | + | litteral |
| 438 | + | group |
| 439 | + | function_call |
| 440 | + | identifier |
| 441 | +
|
| 442 | +litteral: |
| 443 | + | array |
| 444 | + | object |
| 445 | + | string |
| 446 | + | number |
| 447 | + | boolean |
| 448 | + | null |
| 449 | +
|
| 450 | +group: |
| 451 | + | '(' expression ')' |
| 452 | +
|
| 453 | +array: |
| 454 | + | '[' (expression (',' expression)*)? ']' |
| 455 | +
|
| 456 | +object: |
| 457 | + | '{' (object_field (',' object_field)*)? '}' |
| 458 | +
|
| 459 | +object_field: |
| 460 | + | expression ':' expression |
| 461 | +
|
| 462 | +function_call: |
| 463 | + | identifier '(' (expression (',' expression)*)? ')' |
| 464 | +
|
| 465 | +identifier: |
| 466 | + | [a-zA-Z_]+[a-zA-Z0-9_]* |
| 467 | + /* ASCII-only letters and digits, plus underscore; cannot start with a digit */ |
| 468 | +
|
| 469 | +string: |
| 470 | + | /* single-quoted string, single quotes escaped with \' */ |
| 471 | + | /* double-quoted string, double quotes escaped with \" */ |
| 472 | +
|
| 473 | +number: |
| 474 | + | /* same as JSON */ |
| 475 | +
|
| 476 | +boolean: |
| 477 | + | 'true' |
| 478 | + | 'false' |
| 479 | +
|
| 480 | +null: |
| 481 | + | 'null' |
| 482 | +``` |
| 483 | + |
| 484 | + |
357 | 485 | # Acknowledgments |
358 | 486 |
|
359 | 487 | This library was written partly on my spare time, and partly during the course of my employment at [IBEX Innovations Ltd.](https://ibexinnovations.co.uk/). I would like to thank my employer for allowing me to open-source this library, with the hope that it is useful to others. |
0 commit comments