MysoreScript
|
Grammar for the MysoreScript language. More...
#include <grammar.hh>
Static Public Member Functions | |
static const MysoreScriptGrammar & | get () |
Returns a singleton instance of this grammar. More... | |
Public Attributes | |
Rule | whitespace = ' '_E | '\t' | nl('\n') |
Whitespace: spaces, tabs, newlines. More... | |
Rule | comment |
Comments, including tracking newlines inside comments via the whitespace rule. More... | |
Rule | ignored = *(comment | whitespace) |
Rule for treating both comments and whitespace as ignored tokens. More... | |
ExprPtr | digit = '0'_E - '9' |
Digits are things in the range 0-9. More... | |
Rule | num = -("+-"_S) >> +digit |
Numbers are one or more digits, optionally prefixed with its sign. More... | |
Rule | val = num | '(' >> arith_expr >> ')' |
Values are either numbers or expressions in brackets (highest precedence). More... | |
Rule | mul_op = mul >> '*' >> val |
Multiply operations are values or multiply, or divide operations, followed by a multiply symbol, followed by a value. More... | |
Rule | div_op = mul >> '/' >> val |
Divide operations follow the same syntax as multiply. More... | |
Rule | mul = mul_op | div_op | val |
Multiply-precedence operations are either multiply or divide operations, or simple values (numbers of parenthetical expressions). More... | |
Rule | add_op = arith_expr >> '+' >> arith_expr |
Add operations can have any expression on the left (including other add expressions), but only higher-precedence operations on the right. More... | |
Rule | sub_op = arith_expr >> '-' >> arith_expr |
Subtract operations follow the same structure as add. More... | |
Rule | ne_cmp = arith_expr >> "!=" >> arith_expr |
Not-equal comparison. More... | |
Rule | eq_cmp = arith_expr >> "==" >> arith_expr |
Equal comparison. More... | |
Rule | lt_cmp = arith_expr >> '<' >> arith_expr |
Less-than comparison. More... | |
Rule | gt_cmp = arith_expr >> '>' >> arith_expr |
Greater-than comparison. More... | |
Rule | le_cmp = arith_expr >> "<=" >> arith_expr |
Less-than-or-equal comparison. More... | |
Rule | ge_cmp = arith_expr >> ">=" >> arith_expr |
Greater-than-or-equal comparison. More... | |
Rule | cmp = eq_cmp | ne_cmp | lt_cmp | gt_cmp | le_cmp | ge_cmp |
General rule for comparisons. More... | |
Rule | arith_expr = add_op | sub_op | mul | cmp | expression |
Expressions can be any of the other types. More... | |
ExprPtr | character = term(("\\\""_E | !ExprPtr('"') >> (nl('\n') | any()))) |
A character in a string. More... | |
Rule | string_body = *character |
The body of a string. More... | |
Rule | string = term('"' >> string_body >> '"') |
Strings any characters, enclosed in quotes. More... | |
ExprPtr | letter = (('a'_E - 'z') | ('A'_E - 'Z')) |
Letters - valid characters for the start of an identifier. More... | |
Rule | identifier = term(letter >> *(letter | digit)) |
Identifiers are a letter followed by zero or more alphanumeric characters. More... | |
Rule | argList = '('_E >> -(identifier >> *(',' >> identifier)) >> ')' |
Argument list. More... | |
Rule | closure |
A closure starts with the keyword 'func', followed by a function name, a list of arguments in brackets and a list of statements in braces. More... | |
Rule | variable = identifier |
Variable references are single identifiers. More... | |
Rule | assignment = variable >> '=' >> expression |
Assignments are variables, followed by a single equals sign, and then the expression to assign to the variable. More... | |
Rule | callArgList = '('_E >> -(expression >> *(',' >> expression)) >> ')' |
The argument list for a call. More... | |
Rule | call = callable >> -('.'_E >> identifier) >> callArgList |
A call is something that is callable, followed eventually by an argument list. More... | |
Rule | callable |
Callable expression. More... | |
Rule | expression |
All of the valid kinds of expression. More... | |
Rule | newExpr = "new"_E >> identifier |
A new expression: the keyword new followed by a class name. More... | |
Rule | decl = "var"_E >> identifier >> -('='_E >> expression) >> ';' |
A variable declaration, optionally with an initialiser. More... | |
Rule | ret = "return"_E >> expression >> ';' |
A return statement. More... | |
Rule | ifStatement |
An if statement, with a condition in brackets followed by the body in braces. More... | |
Rule | whileLoop |
A while loop, with the condition in brackets followed by the body in braces. More... | |
Rule | cls |
Classes are the keyword class, followed by the class name and optionally a superclass. More... | |
Rule | statement |
All valid statement types. More... | |
Rule | statements = *(statement) |
A list of statements: the top-level for programs in this grammar. More... | |
Grammar for the MysoreScript language.
Definition at line 16 of file grammar.hh.
|
inlinestatic |
Returns a singleton instance of this grammar.
Definition at line 220 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::add_op = arith_expr >> '+' >> arith_expr |
Add operations can have any expression on the left (including other add expressions), but only higher-precedence operations on the right.
Definition at line 67 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::argList = '('_E >> -(identifier >> *(',' >> identifier)) >> ')' |
Argument list.
Zero or more comma-separated arguments, wrapped in brackets.
Definition at line 130 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::arith_expr = add_op | sub_op | mul | cmp | expression |
Expressions can be any of the other types.
Definition at line 103 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::assignment = variable >> '=' >> expression |
Assignments are variables, followed by a single equals sign, and then the expression to assign to the variable.
Definition at line 145 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::call = callable >> -('.'_E >> identifier) >> callArgList |
A call is something that is callable, followed eventually by an argument list.
If the callable expression is followed by a dot and a method name, then this is a method invocation, otherwise the expression on the left is assumed to be a closure and is invoked directly.
Definition at line 158 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::callable |
Callable expression.
This is the same as an expression, but with the call at the end so that each of the other possibilities will be tried before hitting left recursion.
Definition at line 164 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::callArgList = '('_E >> -(expression >> *(',' >> expression)) >> ')' |
The argument list for a call.
This is a separate rule to allow automatic AST construction to distinguish between the expressions that are part of the argument list from others in the call.
Definition at line 151 of file grammar.hh.
ExprPtr Parser::MysoreScriptGrammar::character = term(("\\\""_E | !ExprPtr('"') >> (nl('\n') | any()))) |
A character in a string.
Matches anything except the closing quote.
Definition at line 107 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::closure |
A closure starts with the keyword 'func', followed by a function name, a list of arguments in brackets and a list of statements in braces.
Definition at line 135 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::cls |
Classes are the keyword class, followed by the class name and optionally a superclass.
Instance variables must be declared first, then methods.
Definition at line 205 of file grammar.hh.
General rule for comparisons.
Matches one of the comparison types above.
Definition at line 99 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::comment |
Comments, including tracking newlines inside comments via the whitespace rule.
Definition at line 26 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::decl = "var"_E >> identifier >> -('='_E >> expression) >> ';' |
A variable declaration, optionally with an initialiser.
Definition at line 184 of file grammar.hh.
ExprPtr Parser::MysoreScriptGrammar::digit = '0'_E - '9' |
Digits are things in the range 0-9.
Definition at line 37 of file grammar.hh.
Divide operations follow the same syntax as multiply.
Definition at line 57 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::eq_cmp = arith_expr >> "==" >> arith_expr |
Equal comparison.
Definition at line 79 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::expression |
All of the valid kinds of expression.
Note that the order places calls first, as greedy matching will try cause them to then be matched in the callable
order (which places call last) and then parse the argument list, but would just successfully parse an expression and stop if call were not first here. Note also that expression types that start with a keyword are placed before those that (may) begin with identifiers so that we can trivially disambiguate these cases.
Definition at line 175 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::ge_cmp = arith_expr >> ">=" >> arith_expr |
Greater-than-or-equal comparison.
Definition at line 95 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::gt_cmp = arith_expr >> '>' >> arith_expr |
Greater-than comparison.
Definition at line 87 of file grammar.hh.
Identifiers are a letter followed by zero or more alphanumeric characters.
Definition at line 125 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::ifStatement |
An if statement, with a condition in brackets followed by the body in braces.
Definition at line 193 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::ignored = *(comment | whitespace) |
Rule for treating both comments and whitespace as ignored tokens.
Definition at line 33 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::le_cmp = arith_expr >> "<=" >> arith_expr |
Less-than-or-equal comparison.
Definition at line 91 of file grammar.hh.
ExprPtr Parser::MysoreScriptGrammar::letter = (('a'_E - 'z') | ('A'_E - 'Z')) |
Letters - valid characters for the start of an identifier.
Definition at line 120 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::lt_cmp = arith_expr >> '<' >> arith_expr |
Less-than comparison.
Definition at line 83 of file grammar.hh.
Multiply-precedence operations are either multiply or divide operations, or simple values (numbers of parenthetical expressions).
Definition at line 62 of file grammar.hh.
Multiply operations are values or multiply, or divide operations, followed by a multiply symbol, followed by a value.
The sides can never be add or subtract operations, because they have lower precedence and so can only be parents of multiply or divide operations (or children via parenthetical expressions), not direct children.
Definition at line 53 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::ne_cmp = arith_expr >> "!=" >> arith_expr |
Not-equal comparison.
Definition at line 75 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::newExpr = "new"_E >> identifier |
A new
expression: the keyword new followed by a class name.
Definition at line 180 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::num = -("+-"_S) >> +digit |
Numbers are one or more digits, optionally prefixed with its sign.
Definition at line 41 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::ret = "return"_E >> expression >> ';' |
A return statement.
Definition at line 188 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::statement |
All valid statement types.
Statements that are disambiguated by keywords are first. All expressions are valid statements.
Definition at line 211 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::statements = *(statement) |
A list of statements: the top-level for programs in this grammar.
Definition at line 216 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::string = term('"' >> string_body >> '"') |
Strings any characters, enclosed in quotes.
Definition at line 116 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::string_body = *character |
The body of a string.
An AST builder can use this to get just the characters in the string, without the enclosing quotes.
Definition at line 112 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::sub_op = arith_expr >> '-' >> arith_expr |
Subtract operations follow the same structure as add.
Definition at line 71 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::val = num | '(' >> arith_expr >> ')' |
Values are either numbers or expressions in brackets (highest precedence).
Definition at line 45 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::variable = identifier |
Variable references are single identifiers.
Definition at line 140 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::whileLoop |
A while loop, with the condition in brackets followed by the body in braces.
Definition at line 199 of file grammar.hh.
Rule Parser::MysoreScriptGrammar::whitespace = ' '_E | '\t' | nl('\n') |
Whitespace: spaces, tabs, newlines.
Definition at line 21 of file grammar.hh.