Pegmatite
 All Classes Functions Variables Enumerations
parser.hh
1 /*
2  * Copyright (c) 2014 David Chisnall
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy of
5  * this software and associated documentation files (the "Software"), to deal in
6  * the Software without restriction, including without limitation the rights to
7  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8  * the Software, and to permit persons to whom the Software is furnished to do so,
9  * subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20  */
21 #include "ast.hh"
22 
23 namespace Parser
24 {
25  using pegmatite::Rule;
26  using pegmatite::operator""_E;
27  using pegmatite::ExprPtr;
28  using pegmatite::BindAST;
29  using pegmatite::any;
30  using pegmatite::nl;
31 
36  {
40  Rule whitespace = ' '_E | '\t' | nl('\n');
45  Rule comment = '"' >>
46  (*(!ExprPtr('"'_E) >> (nl('\n') | any()))) >>
47  '"';
51  Rule ignored = *(comment | whitespace);
55  Rule digit = '0'_E - '9';
59  Rule literal = +digit;
63  Rule v_reg = 'v'_E;
67  Rule local_reg = 'a' >> digit;
71  Rule global_reg = 'g' >> digit;
75  Rule reg = v_reg | local_reg | global_reg;
79  Rule op = '+'_E | '=' |'-' | '*' | '/' | "min" | "max";
84  Rule arithmetic = op >> reg >> expression;
89  Rule neighbours = "neighbours"_E >> '(' >> statements >> ')';
94  Rule range = literal | ('('_E >> literal >> ',' >> literal >> ')');
99  Rule range_expr = range >> "=>" >> expression;
104  Rule range_map = '['_E >> reg >> '|' >>
105  +range_expr >> *(','_E >> range_expr) >>']';
109  Rule expression = literal | reg | range_map;
114  Rule statement = neighbours | arithmetic;
118  Rule statements = *statement;
122  static const CellAtomGrammar& get()
123  {
124  static CellAtomGrammar g;
125  return g;
126  }
127  private:
132  CellAtomGrammar() {};
133  };
134 
139  class CellAtomParser : public pegmatite::ASTParserDelegate
140  {
141 #define CONNECT(cls, r) BindAST<AST::cls> r = CellAtomGrammar::get().r
142  CONNECT(StatementList, statements);
143  CONNECT(Literal, literal);
144  CONNECT(Op, op);
145  CONNECT(VRegister, v_reg);
146  CONNECT(LocalRegister, local_reg);
147  CONNECT(GlobalRegister, global_reg);
148  CONNECT(Arithmetic, arithmetic);
149  CONNECT(Range, range_expr);
150  CONNECT(RangeExpr, range_map);
151  CONNECT(Neighbours, neighbours);
152 #undef CONNECT
153  public:
155  };
156 }
Rule range_expr
A single entry in a range map: the source range and the target expression.
Definition: parser.hh:99
Singleton class encapsulating the grammar for the CellAtom language.
Definition: parser.hh:35
Rule v_reg
The current-value register v.
Definition: parser.hh:63
Rule statements
List of zero or more statements.
Definition: parser.hh:118
Class representing a parser for the CellAtom language.
Definition: parser.hh:139
static const CellAtomGrammar & get()
Returns a singleton instance of this grammar.
Definition: parser.hh:122
Rule neighbours
Neighbours expressions are simple a keyword and then a statement list in brackets.
Definition: parser.hh:89
Rule comment
Comments, including tracking newlines inside comments via the whitespace rule.
Definition: parser.hh:45
Rule reg
Any register.
Definition: parser.hh:75
Rule local_reg
Local registers, l0 to l9.
Definition: parser.hh:67
Rule ignored
Rule for treating both comments and whitespace as ignored tokens.
Definition: parser.hh:51
Rule statement
Valid statements in this language are the arithmetic statements or neighbours statements.
Definition: parser.hh:114
Rule expression
Valid expressions in this language are.
Definition: parser.hh:109
Definition: parser.hh:23
Rule literal
Literal values are one or more digits.
Definition: parser.hh:59
Rule arithmetic
Arithmetic statements are an operation, a target register, and an expression.
Definition: parser.hh:84
Rule op
Operation keywords.
Definition: parser.hh:79
Rule digit
Decimal digits.
Definition: parser.hh:55
Rule range_map
A range map, evaluating to a different expression depending on the value in the register argument...
Definition: parser.hh:104
Rule range
The range part of a range expression (either a single value or an inclusive range).
Definition: parser.hh:94
Rule global_reg
Global registers, g0 to g9.
Definition: parser.hh:71
Rule whitespace
Whitespace: spaces, tabs, newlines.
Definition: parser.hh:40