mupp 1.1.0
Loading...
Searching...
No Matches
PStatementDef.hpp
Go to the documentation of this file.
1/***************************************************************************
2
3 PStatementDef.hpp
4
5 Author: Andreas Suter
6 e-mail: andreas.suter@psi.ch
7
8 Based on Joel de Guzman example on calc7,
9 see https://github.com/boostorg/spirit
10
11 This file contains the implementation (definition) of the PStatement
12 grammar. It defines the grammar rules for parsing variable
13 declarations and assignments using Boost.Spirit X3.
14
15 The grammar supports:
16 - Variable declarations: var <identifier> = <expression>
17 - Variable declarations without initialization: var <identifier>
18 - Assignments: <identifier> = <expression>
19
20 The grammar integrates with PExpression for parsing right-hand side
21 expressions and includes error handling and AST annotation.
22
23***************************************************************************/
24
25/***************************************************************************
26 * Copyright (C) 2023 by Andreas Suter *
27 * andreas.suter@psi.ch *
28 * *
29 * This program is free software; you can redistribute it and/or modify *
30 * it under the terms of the GNU General Public License as published by *
31 * the Free Software Foundation; either version 2 of the License, or *
32 * (at your option) any later version. *
33 * *
34 * This program is distributed in the hope that it will be useful, *
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
37 * GNU General Public License for more details. *
38 * *
39 * You should have received a copy of the GNU General Public License *
40 * along with this program; if not, write to the *
41 * Free Software Foundation, Inc., *
42 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
43 ***************************************************************************/
44
45#include "PStatement.hpp"
46#include "PExpressionDef.hpp"
47#include "PErrorHandler.hpp"
48#include "PAnnotation.hpp"
49
50namespace mupp { namespace parser
51{
52 namespace x3 = boost::spirit::x3;
53 namespace ascii = boost::spirit::x3::ascii;
54
55 using x3::raw;
56 using x3::lexeme;
57 using ascii::alpha;
58 using ascii::alnum;
59
61 // Rule definitions
63
64 inline statement_list_type const statement_list = "statement_list";
65 inline variable_declaration_type const variable_declaration = "variable_declaration";
66 inline assignment_type const assignment = "assignment";
67 inline stmt_identifier_type const stmt_identifier = "identifier";
68
70 // Grammar
72
73 inline auto const statement_list_def =
75 ;
76
77 inline auto const stmt_identifier_def =
78 raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
79 ;
80
81 inline auto const variable_declaration_def =
82 lexeme["var" >> !(alnum | '_')] // make sure we have whole words
84 > -('=' > expr)
85 ;
86
87 inline auto const assignment_def =
89 > '='
90 > expr
91 ;
92
94
95}}
96
97
variable_declaration_type const variable_declaration
x3::rule< stmt_identifier_class, std::string > stmt_identifier_type
auto const variable_declaration_def
statement_list_type const statement_list
expr_type const expr
x3::rule< assignment_class, ast::assignment > assignment_type
x3::rule< statement_list_class, ast::statement_list > statement_list_type
auto const statement_list_def
auto const stmt_identifier_def
stmt_identifier_type const stmt_identifier
x3::rule< variable_declaration_class, ast::variable_declaration > variable_declaration_type
auto const assignment_def
assignment_type const assignment