Wallaroo  0.8
grammar.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * wallaroo - A library for configurable creation and wiring of C++ classes.
3  * Copyright (C) 2012 Daniele Pallastrelli
4  *
5  * This file is part of wallaroo.
6  * For more information, see http://wallaroolib.sourceforge.net/
7  *
8  * Boost Software License - Version 1.0 - August 17th, 2003
9  *
10  * Permission is hereby granted, free of charge, to any person or organization
11  * obtaining a copy of the software and accompanying documentation covered by
12  * this license (the "Software") to use, reproduce, display, distribute,
13  * execute, and transmit the Software, and to prepare derivative works of the
14  * Software, and to permit third-parties to whom the Software is furnished to
15  * do so, all subject to the following:
16  *
17  * The copyright notices in the Software and this entire statement, including
18  * the above license grant, this restriction and the following disclaimer,
19  * must be included in all copies of the Software, in whole or in part, and
20  * all derivative works of the Software, unless such copies or derivative
21  * works are solely in the form of machine-executable object code generated by
22  * a source language processor.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
27  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
28  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
29  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30  * DEALINGS IN THE SOFTWARE.
31  ******************************************************************************/
32 
33 #ifndef WALLAROO_DETAIL_GRAMMAR_H_
34 #define WALLAROO_DETAIL_GRAMMAR_H_
35 
36 #include <string>
37 #include <sstream>
38 #include "wallaroo/exceptions.h"
40 
41 namespace wallaroo
42 {
43 namespace detail
44 {
45 
46 /*
47 GRAMMAR
48 
49  start -> list <done>
50  list -> statement <stmtsep> list | <empty>
51  statement -> loadexpr | assignexpr
52  loadexpr -> <load> <value>
53  assignexpr -> <id> <assign> <create> <id> <optparamlist> | <id> depexpr
54  optparamlist -> <empty> | <open> attrlist <close>
55  attrlist -> <empty> | attrassign moreattrassign
56  attrassign -> <id> <assign> <value>
57  moreattrassign -> <empty> | <attrsep> attrassign
58  depexpr -> <collsep> <id> <assign> <id>
59 
60 e.g.:
61  load value;
62  id = new id ( id=value, id=value, ... );
63  id.id=id;
64 
65  ---
66 
67  load "plugin.so";
68  dev = new Device ( name="dev1", port=30, load=3.14 );
69  dev2 = new Device2;
70  dev.dep = dev2;
71 
72 terminals:
73  "load" <load>
74  "new" <create>
75  "("
76  ")"
77  ";"
78  ","
79  "="
80  "."
81  id
82  value
83  done
84 */
85 
86 template < typename SemanticActions >
87 class Grammar
88 {
89 public:
90  Grammar( std::istream& in, SemanticActions& sa ) :
91  input( in ), lookahead( Token::done ), actions( sa )
92  {}
93 
94  // throws SyntaxError
95  // start -> list <done>
96  // list -> statement <stmtsep> list | <empty>
97  void Parse()
98  {
99  lookahead = input.Next();
100  while ( lookahead.type != Token::done )
101  {
102  Statement();
103  Match( Token::stmtsep );
104  }
105  }
106 private:
107  // statement -> loadexpr | assignexpr
108  void Statement()
109  {
110  switch ( lookahead.type )
111  {
112  case Token::load: LoadExpr(); break;
113  case Token::id: AssignExpr(); break;
114  default:
115  throw SyntaxError( "expecting load, create or id", input.Line(), input.Col() ); // TODO: expecting load, create or id
116  }
117  }
118  // loadexpr -> <load> <value>
119  void LoadExpr()
120  {
121  Match( Token::load );
122  const std::string value = NextLexem();
123  Match( Token::value );
124  actions.Load( value );
125  }
126  // assignexpr -> <id> <assign> <create> <id> <optparamlist> | <id> depexpr
127  // optparamlist -> <empty> | <open> attrlist <close>
128  // attrlist -> <empty> | attrassign moreattrassign
129  // attrassign -> <id> <assign> <value>
130  // moreattrassign -> <empty> | <attrsep> attrassign
131  // depexpr -> <collsep> <id> <assign> <id>
132  void AssignExpr()
133  {
134  const std::string lvalue = NextLexem();
135  Match( Token::id );
136  switch ( lookahead.type )
137  {
138  case Token::assign:
139  {
140  Match( Token::assign );
141  Match( Token::create );
142  const std::string rvalue = NextLexem();
143  Match( Token::id );
144  if ( lookahead.type == Token::open )
145  {
146  Match( Token::open );
147  while ( lookahead.type == Token::id )
148  {
149  const std::string attId = NextLexem();
150  Match( Token::id );
151  Match( Token::assign );
152  const std::string attValue = NextLexem();
153  Match( Token::value );
154  actions.AssignAttribute( lvalue, attId, attValue );
155  if ( lookahead.type == Token::attrsep ) Match( Token::attrsep );
156  }
157  Match( Token::close );
158  }
159  actions.Create( lvalue, rvalue );
160  break;
161  }
162  case Token::collsep:
163  {
164  Match( Token::collsep );
165  const std::string dep = NextLexem();
166  Match( Token::id );
167  Match( Token::assign );
168  const std::string rvalue = NextLexem();
169  Match( Token::id );
170  actions.AssignDep( lvalue, dep, rvalue );
171  break;
172  }
173  default:
174  throw SyntaxError( "expecting = or .", input.Line(), input.Col() ); // TODO
175  }
176  }
177  void Match( Token::Type t )
178  {
179  if ( lookahead.type == t ) lookahead = input.Next();
180  else throw SyntaxError( "expecting token " + Token::Description( t ), input.Line(), input.Col() ); // TODO error msg (e.g., "expecting t")
181  }
182  std::string NextLexem() const
183  {
184  return lookahead.lexem;
185  }
186  TokenSource input;
187  Token lookahead;
188  SemanticActions& actions;
189 };
190 
191 } // detail
192 } // namespace
193 
194 #endif // WALLAROO_DETAIL_GRAMMAR_H_
Definition: tokensource.h:57
std::size_t Line() const
Definition: tokensource.h:209
Definition: tokensource.h:48
Type type
Definition: tokensource.h:64
std::string lexem
Definition: tokensource.h:65
static std::string Description(Type t)
Definition: tokensource.h:68
Definition: exceptions.h:227
Grammar(std::istream &in, SemanticActions &sa)
Definition: grammar.h:90
Definition: tokensource.h:52
Definition: tokensource.h:62
Definition: tokensource.h:55
Definition: tokensource.h:59
Definition: tokensource.h:56
void Parse()
Definition: grammar.h:97
Definition: attribute.h:45
Definition: tokensource.h:54
Definition: tokensource.h:61
std::size_t Col() const
Definition: tokensource.h:210
Token Next()
Definition: tokensource.h:96
Definition: tokensource.h:58
Definition: tokensource.h:60
Type
Definition: tokensource.h:51
Definition: grammar.h:87
Definition: tokensource.h:53