MysoreScript
grammar.hh
Go to the documentation of this file.
1 #include "Pegmatite/pegmatite.hh"
2 namespace Parser
3 {
4  using pegmatite::Rule;
5  using pegmatite::operator""_E;
6  using pegmatite::operator""_S;
7  using pegmatite::ExprPtr;
8  using pegmatite::BindAST;
9  using pegmatite::any;
10  using pegmatite::nl;
11  using pegmatite::trace;
12 
17 {
21  Rule whitespace = ' '_E | '\t' | nl('\n');
26  Rule comment = ("/*"_E >>
27  (*(!ExprPtr("*/") >> (nl('\n') | any()))) >>
28  "*/") |
29  "//"_E >> *(!(ExprPtr("\n")) >> any()) >> nl('\n');
33  Rule ignored = *(comment | whitespace);
37  ExprPtr digit = '0'_E - '9';
41  Rule num = -("+-"_S) >> +digit;
45  Rule val = num | '(' >> arith_expr >> ')';
53  Rule mul_op = mul >> '*' >> val;
57  Rule div_op = mul >> '/' >> val;
62  Rule mul = mul_op | div_op | val;
67  Rule add_op = arith_expr >> '+' >> arith_expr;
71  Rule sub_op = arith_expr >> '-' >> arith_expr;
75  Rule ne_cmp = arith_expr >> "!=" >> arith_expr;
79  Rule eq_cmp = arith_expr >> "==" >> arith_expr;
83  Rule lt_cmp = arith_expr >> '<' >> arith_expr;
87  Rule gt_cmp = arith_expr >> '>' >> arith_expr;
91  Rule le_cmp = arith_expr >> "<=" >> arith_expr;
95  Rule ge_cmp = arith_expr >> ">=" >> arith_expr;
99  Rule cmp = eq_cmp | ne_cmp | lt_cmp | gt_cmp | le_cmp | ge_cmp;
103  Rule arith_expr = add_op | sub_op | mul | cmp | expression;
107  ExprPtr character = term(("\\\""_E | !ExprPtr('"') >> (nl('\n') | any())));
116  Rule string = term('"' >> string_body >> '"');
120  ExprPtr letter = (('a'_E - 'z') | ('A'_E - 'Z'));
125  Rule identifier = term(letter >> *(letter | digit));
130  Rule argList = '('_E >> -(identifier >> *(',' >> identifier)) >> ')';
135  Rule closure = "func"_E >> identifier >> argList >>
136  '{' >> statements >> '}';
145  Rule assignment = variable >> '=' >> expression;
151  Rule callArgList = '('_E >> -(expression >> *(',' >> expression)) >> ')';
158  Rule call = callable >> -('.'_E >> identifier) >> callArgList;
164  Rule callable = closure | newExpr | arith_expr | variable | string |
165  call;
175  Rule expression = call | closure | newExpr | arith_expr | variable |
176  string;
180  Rule newExpr = "new"_E >> identifier;
184  Rule decl = "var"_E >> identifier >> -('='_E >> expression) >> ';';
188  Rule ret = "return"_E >> expression >> ';';
193  Rule ifStatement = "if"_E >> '(' >> expression >> ')' >>
194  '{' >> statements >> '}';
199  Rule whileLoop = "while"_E >> '(' >> expression >> ')' >>
200  '{' >> statements >> '}';
205  Rule cls = "class"_E >> identifier >> -(':'_E >> identifier) >> '{'
206  >> *decl >> *closure >> '}';
211  Rule statement = cls | ret | ifStatement | whileLoop | decl |
212  ((assignment | expression) >> ';');
216  Rule statements = *(statement);
220  static const MysoreScriptGrammar& get()
221  {
222  static MysoreScriptGrammar g;
223  return g;
224  }
225  private:
230  MysoreScriptGrammar() {};
231 };
232 }
233 
Rule sub_op
Subtract operations follow the same structure as add.
Definition: grammar.hh:71
Rule ignored
Rule for treating both comments and whitespace as ignored tokens.
Definition: grammar.hh:33
Rule ge_cmp
Greater-than-or-equal comparison.
Definition: grammar.hh:95
Rule call
A call is something that is callable, followed eventually by an argument list.
Definition: grammar.hh:158
Rule statement
All valid statement types.
Definition: grammar.hh:211
Rule eq_cmp
Equal comparison.
Definition: grammar.hh:79
Rule le_cmp
Less-than-or-equal comparison.
Definition: grammar.hh:91
Rule mul
Multiply-precedence operations are either multiply or divide operations, or simple values (numbers of...
Definition: grammar.hh:62
Rule whileLoop
A while loop, with the condition in brackets followed by the body in braces.
Definition: grammar.hh:199
Rule variable
Variable references are single identifiers.
Definition: grammar.hh:140
Rule num
Numbers are one or more digits, optionally prefixed with its sign.
Definition: grammar.hh:41
Rule div_op
Divide operations follow the same syntax as multiply.
Definition: grammar.hh:57
Rule lt_cmp
Less-than comparison.
Definition: grammar.hh:83
Rule decl
A variable declaration, optionally with an initialiser.
Definition: grammar.hh:184
Rule argList
Argument list.
Definition: grammar.hh:130
ExprPtr character
A character in a string.
Definition: grammar.hh:107
Rule ne_cmp
Not-equal comparison.
Definition: grammar.hh:75
Rule expression
All of the valid kinds of expression.
Definition: grammar.hh:175
Rule identifier
Identifiers are a letter followed by zero or more alphanumeric characters.
Definition: grammar.hh:125
Rule callArgList
The argument list for a call.
Definition: grammar.hh:151
Rule whitespace
Whitespace: spaces, tabs, newlines.
Definition: grammar.hh:21
Rule newExpr
A new expression: the keyword new followed by a class name.
Definition: grammar.hh:180
Rule ifStatement
An if statement, with a condition in brackets followed by the body in braces.
Definition: grammar.hh:193
ExprPtr digit
Digits are things in the range 0-9.
Definition: grammar.hh:37
Rule cls
Classes are the keyword class, followed by the class name and optionally a superclass.
Definition: grammar.hh:205
Rule ret
A return statement.
Definition: grammar.hh:188
Rule gt_cmp
Greater-than comparison.
Definition: grammar.hh:87
Rule statements
A list of statements: the top-level for programs in this grammar.
Definition: grammar.hh:216
Definition: parser.hh:4
Rule val
Values are either numbers or expressions in brackets (highest precedence).
Definition: grammar.hh:45
Rule string_body
The body of a string.
Definition: grammar.hh:112
Grammar for the MysoreScript language.
Definition: grammar.hh:16
Rule comment
Comments, including tracking newlines inside comments via the whitespace rule.
Definition: grammar.hh:26
Rule assignment
Assignments are variables, followed by a single equals sign, and then the expression to assign to the...
Definition: grammar.hh:145
Rule cmp
General rule for comparisons.
Definition: grammar.hh:99
Rule arith_expr
Expressions can be any of the other types.
Definition: grammar.hh:103
Rule closure
A closure starts with the keyword &#39;func&#39;, followed by a function name, a list of arguments in bracket...
Definition: grammar.hh:135
Rule callable
Callable expression.
Definition: grammar.hh:164
Rule add_op
Add operations can have any expression on the left (including other add expressions), but only higher-precedence operations on the right.
Definition: grammar.hh:67
Rule mul_op
Multiply operations are values or multiply, or divide operations, followed by a multiply symbol...
Definition: grammar.hh:53
ExprPtr letter
Letters - valid characters for the start of an identifier.
Definition: grammar.hh:120
Rule string
Strings any characters, enclosed in quotes.
Definition: grammar.hh:116