BEAST - Free Software Audio Synthesizer and Tracker  0.9.2
sfidl-parser.hh
Go to the documentation of this file.
1  // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
2 
3 #ifndef _SFIDL_PARSER_H_
4 #define _SFIDL_PARSER_H_
5 
6 #include <map>
7 #include "sfidl-utils.hh"
8 
9 namespace Sfidl {
10 
11 /* we implement a get() function since operator[] is not const */
12 template<typename Key, typename Value>
13 class Map : public std::map<Key,Value> {
14 private:
15  Value default_value;
16 
17 public:
18  const Value& get(const Key& k) const {
19  typename std::map<Key,Value>::const_iterator i = this->find(k);
20  if (i != this->end())
21  return i->second;
22  else
23  return default_value;
24  }
25 };
26 
27 /*
28  * internationalized string: can store a conventional string,
29  * however it can also store whether the string was given in
30  * conventional form "foo" (i18n = false)
31  * on in the i18n form _("foo") (i18n = true)
32  */
33 class IString : public String {
34 public:
35  bool i18n;
36 
37  IString() : i18n (false) {
38  }
39 
40  IString(const char *str) : String (str), i18n (false) {
41  }
42 
43  /* produces an escaped version "foo" or _("foo") */
44  String escaped (const String &i18n_prefix = "_") const
45  {
46  String result;
47  char *x = g_strescape (c_str(), 0);
48  if (i18n)
49  result = i18n_prefix + "(\"" + String (x) + "\")";
50  else
51  result = "\"" + String(x) + "\"";
52  g_free (x);
53  return result;
54  }
55 };
56 
57 struct LineInfo {
58  bool isInclude;
59  int line;
60  String filename;
61 
62  // Produce a human readable location (file:line, using "stdin" where appropriate)
63  String location() const
64  {
65  String result;
66  char *x = g_strdup_format ("%s:%d", (filename == "-") ? "stdin" : filename.c_str(), line);
67  result = x;
68  g_free (x);
69  return result;
70  }
71 };
72 
73 struct Pragma {
74  String filename;
75  String text;
76  int line;
77  bool fromInclude; /* true for normal includes; false for implIncludes and the base file */
78 
79  bool getString (const String& key, String& value);
80 };
81 
82 struct Constant {
83  String name;
84  String file;
85  enum { tString = 1, tFloat = 2, tInt = 3, tIdent = 4 } type;
86 
87  String str;
88  float f;
89  int64 i;
90 };
91 
92 struct Param {
93  String type;
94  String name;
95  String file;
96 
97  IString group;
98  String pspec;
99  int line;
100  String args;
101 
102  String label; /* first argument of the param spec contructor */
103  String blurb; /* second argument of the param spec contructor */
104  String options; /* last argument of the param spec contructor */
105  String literal_options; /* the real option string; note that conversion might not work,
106  if building the literal option string requires things like C function calls */
107 };
108 
109 struct Stream {
110  enum Type { IStream, JStream, OStream } type;
111  String ident;
112  IString label;
113  IString blurb;
114  String file;
115  int line;
116 };
117 
118 struct ChoiceValue {
119  String name;
120  String file;
121  IString label;
122  IString blurb;
123 
124  int value;
125  int sequentialValue;
126  bool neutral;
127 };
128 
129 struct Choice {
130  /*
131  * name if the enum, "_anonymous_" for anonymous enum - of course, when
132  * using namespaces, this can also lead to things like "Arts::_anonymous_",
133  * which would mean an anonymous enum in the Arts namespace
134  */
135  String name;
136  String file;
137 
138  std::vector<ChoiceValue> contents;
139  Map<String, IString> infos;
140 };
141 
142 struct Record {
143  String name;
144  String file;
145 
146  std::vector<Param> contents;
147  Map<String, IString> infos;
148 };
149 
150 struct Sequence {
151  String name;
152  String file;
153  Param content;
154  Map<String, IString> infos;
155 };
156 
157 struct Method {
158  String name;
159  String file;
160 
161  std::vector<Param> params;
162  Param result;
163  Map<String, IString> infos;
164 };
165 
166 struct Class {
167  String name;
168  String file;
169  String inherits;
170 
171  std::vector<Method> methods;
172  std::vector<Method> signals;
173  std::vector<Param> properties;
174  std::vector<Stream> istreams, jstreams, ostreams;
175  Map<String, IString> infos;
176 };
177 
178 enum TypeDeclaration {
179  tdChoice = 1,
180  tdRecord = 2,
181  tdSequence = 3,
182  tdClass = 4,
183  tdProto = 8,
184  tdChoiceProto = tdChoice | tdProto,
185  tdRecordProto = tdRecord | tdProto,
186  tdSequenceProto = tdSequence | tdProto,
187  tdClassProto = tdClass | tdProto,
188 };
189 
190 enum Type {
191  // primitive types
192  VOID,
193  BOOL,
194  INT,
195  NUM,
196  REAL,
197  STRING,
198  // enums
199  CHOICE,
200  // blocks of byte/float
201  BBLOCK,
202  FBLOCK,
203  // generic record type:
204  SFIREC,
205  // user defined types
206  SEQUENCE,
207  RECORD,
208  OBJECT, /* PROXY */
209 };
210 
211 class Symbol {
212 public:
213  String name;
214 
215  Symbol *parent;
216  std::vector<Symbol *> children;
217 
218  Symbol();
219  virtual ~Symbol();
220 
221  String fullName ();
222  Symbol *find (const String& name);
223  bool insert (Symbol *symbol);
224 };
225 
226 class Namespace : public Symbol {
227 public:
228  std::vector<Namespace *> used; /* from "using namespace Foo;" statements */
229 };
230 
231 class Parser {
232 protected:
233  const class Options& options;
234 
235  GScanner *scanner;
236  std::vector<char> scannerInputData;
237  std::vector<LineInfo> scannerLineInfo;
238 
239  Namespace rootNamespace;
240  Namespace *currentNamespace;
241 
242  std::vector<String> includedNames;
243  std::vector<String> types;
244  std::map<String,int> typeMap;
245 
246  std::vector<String> includes; // files to include
247  std::vector<Pragma> pragmas;
248  std::vector<Constant> constants;
249  std::vector<Choice> choices;
250  std::vector<Sequence> sequences;
251  std::vector<Record> records;
252  std::vector<Class> classes;
253  std::vector<Method> procedures;
254 
255  // namespace related functions
256 
257  String defineSymbol (const String& name);
258  Symbol *qualifyHelper (const String& name);
259  String qualifySymbol (const String& name);
260  bool enterNamespace (const String& name);
261  void leaveNamespace ();
262  bool usingNamespace (const String& name);
263 
264  // scanner related functions
265 
266  static void scannerMsgHandler (GScanner *scanner, gchar *message, gboolean is_error);
267 
268  template<class... Args> void print_error (const char *format, const Args &...args)
269  {
270  if (scanner->parse_errors < scanner->max_parse_errors)
271  g_scanner_error (scanner, "%s", string_format (format, args...).c_str());
272  }
273  template<class... Args> void print_warning (const char *format, const Args &...args)
274  {
275  g_scanner_warn (scanner, "%s", string_format (format, args...).c_str());
276  }
277 
278  // preprocessor
279 
280  void preprocess (const String& filename, bool includeImpl = false);
281  void preprocessContents (const String& filename);
282  bool haveIncluded (const String& filename) const;
283  bool insideInclude () const;
284 
285  // parser
286 
287  void addConstantTodo (const Constant& cdef);
288  void addChoiceTodo (const Choice& cdef);
289  void addRecordTodo (const Record& rdef);
290  void addSequenceTodo (const Sequence& sdef);
291  void addClassTodo (const Class& cdef);
292  void addProcedureTodo (const Method& pdef);
293 
294  void addPrototype (const String& type, TypeDeclaration typeDecl);
295  void addType (const String& type, TypeDeclaration typeDecl);
296 
297  GTokenType parseTypeName (String& s);
298  GTokenType parseStringOrConst (String &s);
299  GTokenType parseConstant (bool isident = false);
300  GTokenType parseNamespace ();
301  GTokenType parseChoice ();
302  GTokenType parseChoiceValue (ChoiceValue& comp, int& value, int& sequentialValue);
303  GTokenType parseRecord ();
304  GTokenType parseRecordField (Param& comp, const IString& group);
305  GTokenType parseStream (Stream& stream, Stream::Type);
306  GTokenType parseSequence ();
307  GTokenType parseParamHints (Param &def);
308  GTokenType parseClass ();
309  GTokenType parseMethod (Method& def);
310  GTokenType parseInfoOptional (Map<String,IString>& infos);
311 public:
312  Parser ();
313 
314  bool parse (const String& fileName);
315 
316  String fileName() const { return scanner->input_name; }
317  const std::vector<String>& getIncludes () const { return includes; }
318  const std::vector<Constant>& getConstants () const { return constants; }
319  const std::vector<Choice>& getChoices () const { return choices; }
320  const std::vector<Sequence>& getSequences () const { return sequences; }
321  const std::vector<Record>& getRecords () const { return records; }
322  const std::vector<Class>& getClasses () const { return classes; }
323  const std::vector<Method>& getProcedures () const { return procedures; }
324  const std::vector<String>& getTypes () const { return types; }
325 
326  std::vector<Pragma> getPragmas (const String& binding) const;
327 
328  Sequence findSequence (const String& name) const;
329  Record findRecord (const String& name) const;
330  const Class* findClass (const String &name) const;
331 
332  bool isChoice (const String& type) const;
333  bool isSequence (const String& type) const;
334  bool isRecord (const String& type) const;
335  bool isClass (const String& type) const;
336  Type typeOf (const String& type) const;
337  bool fromInclude (const String& type) const;
338 };
339 
340 }
341 #endif /* _SFIDL_PARSER_H_ */
342 
343 /* vim:set ts=8 sts=2 sw=2: */
Definition: sfidl-parser.hh:92
Definition: sfidl-parser.hh:82
Definition: sfidl-parser.hh:226
Definition: sfidl-parser.hh:57
Definition: sfidl-parser.hh:129
Definition: sfidl-options.hh:16
Definition: sfidl-parser.hh:166
Definition: sfidl-parser.hh:150
Value end(Value...args)
The Sfidl namespace contains implementation and API of the Sfi IDL compiler.
Definition: sfidl-cbase.hh:14
STL class.
Definition: sfidl-parser.hh:231
Definition: sfidl-parser.hh:73
STL class.
Definition: sfidl-parser.hh:13
Definition: sfidl-parser.hh:157
Value find(Value...args)
STL class.
T c_str(T...args)
Definition: sfidl-parser.hh:33
Definition: sfidl-parser.hh:142
Definition: sfidl-parser.hh:109
Definition: sfidl-parser.hh:118
Definition: sfidl-parser.hh:211