This commit is contained in:
arookas 2016-01-31 21:32:41 -05:00
parent be3c2b1582
commit e57a55056b

View file

@ -1,215 +1,215 @@
%header% %header%
GRAMMARTYPE = "LL" GRAMMARTYPE = "LL"
%tokens% %tokens%
// whitespace // whitespace
WHITESPACE = <<[ \t\r\n]+>> %ignore% WHITESPACE = <<[ \t\r\n]+>> %ignore%
// comments // comments
SINGLE_LINE_COMMENT = <<//.*>> %ignore% SINGLE_LINE_COMMENT = <<//.*>> %ignore%
MULTI_LINE_COMMENT = <</\*([^*]|\*+[^*/])*\*+/>> %ignore% MULTI_LINE_COMMENT = <</\*([^*]|\*+[^*/])*\*+/>> %ignore%
// keywords // keywords
IMPORT = "import" IMPORT = "import"
BUILTIN = "builtin" BUILTIN = "builtin"
FUNCTION = "function" FUNCTION = "function"
VAR = "var" VAR = "var"
CONST = "const" CONST = "const"
IF = "if" IF = "if"
ELSE = "else" ELSE = "else"
DO = "do" DO = "do"
WHILE = "while" WHILE = "while"
FOR = "for" FOR = "for"
RETURN = "return" RETURN = "return"
BREAK = "break" BREAK = "break"
CONTINUE = "continue" CONTINUE = "continue"
YIELD = "yield" YIELD = "yield"
EXIT = "exit" EXIT = "exit"
LOCK = "lock" LOCK = "lock"
UNLOCK = "unlock" UNLOCK = "unlock"
INT = "int" INT = "int"
FLOAT = "float" FLOAT = "float"
TYPEOF = "typeof" TYPEOF = "typeof"
TRUE = "true" TRUE = "true"
FALSE = "false" FALSE = "false"
// punctuation // punctuation
L_BRACE = "{" L_BRACE = "{"
R_BRACE = "}" R_BRACE = "}"
L_PAREN = "(" L_PAREN = "("
R_PAREN = ")" R_PAREN = ")"
L_BRACKET = "[" L_BRACKET = "["
R_BRACKET = "]" R_BRACKET = "]"
COLON = ":" COLON = ":"
SEMICOLON = ";" SEMICOLON = ";"
COMMA = "," COMMA = ","
DOT = "." DOT = "."
ELLIPSIS = "..." ELLIPSIS = "..."
QMARK = "?" QMARK = "?"
// operators // operators
ADD = "+" ADD = "+"
SUB = "-" // doubles as the negation operator SUB = "-" // doubles as the negation operator
MUL = "*" MUL = "*"
DIV = "/" DIV = "/"
MOD = "%" MOD = "%"
BIT_AND = "&" BIT_AND = "&"
BIT_OR = "|" BIT_OR = "|"
BIT_LSH = "<<" BIT_LSH = "<<"
BIT_RSH = ">>" BIT_RSH = ">>"
LOG_NOT = "!" LOG_NOT = "!"
LOG_AND = "&&" LOG_AND = "&&"
LOG_OR = "||" LOG_OR = "||"
EQ = "==" EQ = "=="
NEQ = "!=" NEQ = "!="
LT = "<" LT = "<"
LTEQ = "<=" LTEQ = "<="
GT = ">" GT = ">"
GTEQ = ">=" GTEQ = ">="
ASSIGN = "=" ASSIGN = "="
ASSIGN_ADD = "+=" ASSIGN_ADD = "+="
ASSIGN_SUB = "-=" ASSIGN_SUB = "-="
ASSIGN_MUL = "*=" ASSIGN_MUL = "*="
ASSIGN_DIV = "/=" ASSIGN_DIV = "/="
ASSIGN_MOD = "%=" ASSIGN_MOD = "%="
ASSIGN_BIT_AND = "&=" ASSIGN_BIT_AND = "&="
ASSIGN_BIT_OR = "|=" ASSIGN_BIT_OR = "|="
ASSIGN_BIT_LSH = "<<=" ASSIGN_BIT_LSH = "<<="
ASSIGN_BIT_RSH = ">>=" ASSIGN_BIT_RSH = ">>="
INCREMENT = "++" INCREMENT = "++"
DECREMENT = "--" DECREMENT = "--"
// literals // literals
IDENTIFIER = <<[_A-Za-z][_A-Za-z0-9]*>> IDENTIFIER = <<[_A-Za-z][_A-Za-z0-9]*>>
DEC_NUMBER = <<-?[0-9]+\.[0-9]+>> DEC_NUMBER = <<-?[0-9]+\.[0-9]+>>
HEX_NUMBER = <<-?0x[0-9A-Fa-f]+>> HEX_NUMBER = <<-?0x[0-9A-Fa-f]+>>
INT_NUMBER = <<-?[0-9]+>> INT_NUMBER = <<-?[0-9]+>>
STRING = <<"(\\.|[^"])*">> STRING = <<"(\\.|[^"])*">>
%productions% %productions%
script = root_statement+; script = root_statement+;
// statements // statements
root_statement = root_statement =
import_statement SEMICOLON | import_statement SEMICOLON |
function_definition | function_definition |
builtin_declaration SEMICOLON | builtin_declaration SEMICOLON |
statement; statement;
statement = statement =
compound_statement SEMICOLON | compound_statement SEMICOLON |
if_statement | if_statement |
[name_label] while_statement | [name_label] while_statement |
[name_label] do_statement SEMICOLON | [name_label] do_statement SEMICOLON |
[name_label] for_statement | [name_label] for_statement |
return_statement SEMICOLON | return_statement SEMICOLON |
break_statement SEMICOLON | break_statement SEMICOLON |
continue_statement SEMICOLON | continue_statement SEMICOLON |
yield_statement SEMICOLON | yield_statement SEMICOLON |
exit_statement SEMICOLON | exit_statement SEMICOLON |
lock_statement SEMICOLON | lock_statement SEMICOLON |
unlock_statement SEMICOLON | unlock_statement SEMICOLON |
statement_block; statement_block;
compound_statement = compound_statement_item {COMMA compound_statement_item}; compound_statement = compound_statement_item {COMMA compound_statement_item};
compound_statement_item = compound_statement_item =
const_definition | const_definition |
variable_definition | variable_definition |
variable_declaration | variable_declaration |
variable_assignment | variable_assignment |
variable_augment | variable_augment |
function_call; function_call;
statement_block = L_BRACE {statement} R_BRACE; statement_block = L_BRACE {statement} R_BRACE;
import_statement = IMPORT STRING; import_statement = IMPORT STRING;
yield_statement = YIELD; yield_statement = YIELD;
exit_statement = EXIT; exit_statement = EXIT;
lock_statement = LOCK; lock_statement = LOCK;
unlock_statement = UNLOCK; unlock_statement = UNLOCK;
name_label = IDENTIFIER COLON; name_label = IDENTIFIER COLON;
// operators // operators
assignment_operator = ASSIGN | ASSIGN_ADD | ASSIGN_SUB | ASSIGN_MUL | ASSIGN_DIV | ASSIGN_MOD | ASSIGN_BIT_AND | ASSIGN_BIT_OR | ASSIGN_BIT_LSH | ASSIGN_BIT_RSH; assignment_operator = ASSIGN | ASSIGN_ADD | ASSIGN_SUB | ASSIGN_MUL | ASSIGN_DIV | ASSIGN_MOD | ASSIGN_BIT_AND | ASSIGN_BIT_OR | ASSIGN_BIT_LSH | ASSIGN_BIT_RSH;
ternary_operator = expression QMARK expression COLON expression; ternary_operator = expression QMARK expression COLON expression;
binary_operator = binary_operator =
ADD | SUB | MUL | DIV | MOD | // arithmetic ADD | SUB | MUL | DIV | MOD | // arithmetic
BIT_AND | BIT_OR | BIT_LSH | BIT_RSH | // bitwise BIT_AND | BIT_OR | BIT_LSH | BIT_RSH | // bitwise
EQ | NEQ | LT | LTEQ | GT | GTEQ | // comparison EQ | NEQ | LT | LTEQ | GT | GTEQ | // comparison
LOG_AND | LOG_OR; // logical LOG_AND | LOG_OR; // logical
unary_operator = LOG_NOT | SUB; unary_operator = LOG_NOT | SUB;
augment_operator = INCREMENT | DECREMENT; augment_operator = INCREMENT | DECREMENT;
// expressions // expressions
expression = operand {binary_operator operand}; expression = operand {binary_operator operand};
operand = [unary_operator_list] term; operand = [unary_operator_list] term;
term = term =
int_cast | int_cast |
float_cast | float_cast |
typeof_cast | typeof_cast |
function_call | function_call |
variable_reference | variable_reference |
variable_augment | variable_augment |
STRING | STRING |
DEC_NUMBER | DEC_NUMBER |
HEX_NUMBER | HEX_NUMBER |
INT_NUMBER | INT_NUMBER |
TRUE | TRUE |
FALSE | FALSE |
L_PAREN expression R_PAREN | L_PAREN expression R_PAREN |
L_BRACKET ternary_operator R_BRACKET; // HACK: the brackets remove ambiguity L_BRACKET ternary_operator R_BRACKET; // HACK: the brackets remove ambiguity
unary_operator_list = unary_operator+; unary_operator_list = unary_operator+;
int_cast = INT L_PAREN expression R_PAREN; int_cast = INT L_PAREN expression R_PAREN;
float_cast = FLOAT L_PAREN expression R_PAREN; float_cast = FLOAT L_PAREN expression R_PAREN;
typeof_cast = TYPEOF L_PAREN expression R_PAREN; typeof_cast = TYPEOF L_PAREN expression R_PAREN;
// constants // constants
const_definition = CONST IDENTIFIER ASSIGN expression; const_definition = CONST IDENTIFIER ASSIGN expression;
// variables // variables
variable_reference = IDENTIFIER; // used in expressions variable_reference = IDENTIFIER; // used in expressions
variable_declaration = VAR IDENTIFIER; variable_declaration = VAR IDENTIFIER;
variable_definition = VAR IDENTIFIER ASSIGN expression; variable_definition = VAR IDENTIFIER ASSIGN expression;
variable_assignment = IDENTIFIER assignment_operator expression; variable_assignment = IDENTIFIER assignment_operator expression;
variable_augment = postfix_augment | prefix_augment; variable_augment = postfix_augment | prefix_augment;
postfix_augment = IDENTIFIER augment_operator; postfix_augment = IDENTIFIER augment_operator;
prefix_augment = augment_operator IDENTIFIER; prefix_augment = augment_operator IDENTIFIER;
// functions // functions
function_definition = FUNCTION IDENTIFIER parameter_list statement_block; function_definition = FUNCTION IDENTIFIER parameter_list statement_block;
function_call = IDENTIFIER argument_list; function_call = IDENTIFIER argument_list;
parameter_list = L_PAREN [parameter {COMMA parameter}] R_PAREN; // e.g. (a, b, ...) parameter_list = L_PAREN [parameter {COMMA parameter}] R_PAREN; // e.g. (a, b, ...)
parameter = IDENTIFIER | ELLIPSIS; parameter = IDENTIFIER | ELLIPSIS;
argument_list = L_PAREN [argument {COMMA argument}] R_PAREN; argument_list = L_PAREN [argument {COMMA argument}] R_PAREN;
argument = expression; argument = expression;
// builtins // builtins
builtin_declaration = BUILTIN IDENTIFIER parameter_list; builtin_declaration = BUILTIN IDENTIFIER parameter_list;
// flow control // flow control
if_statement = IF expression statement [ELSE statement]; if_statement = IF expression statement [ELSE statement];
while_statement = WHILE expression statement; while_statement = WHILE expression statement;
do_statement = DO statement WHILE expression; do_statement = DO statement WHILE expression;
for_statement = FOR L_PAREN [for_declaration] SEMICOLON [for_condition] SEMICOLON [for_iteration] R_PAREN statement; for_statement = FOR L_PAREN [for_declaration] SEMICOLON [for_condition] SEMICOLON [for_iteration] R_PAREN statement;
for_declaration = compound_statement; for_declaration = compound_statement;
for_condition = expression; for_condition = expression;
for_iteration = compound_statement; for_iteration = compound_statement;
return_statement = RETURN [expression]; return_statement = RETURN [expression];
break_statement = BREAK [IDENTIFIER]; break_statement = BREAK [IDENTIFIER];
continue_statement = CONTINUE [IDENTIFIER]; continue_statement = CONTINUE [IDENTIFIER];