VR-Forces 4.5 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DtGLSLPreprocessor.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2012 MAK Technologies, Inc.
3  * All rights reserved.
4  *****************************************************************************/
5 
8 
37 #ifndef DTGLSLPREPROCESSOR_H
38 #define DTGLSLPREPROCESSOR_H
39 
40 #include <string.h>
41 #include <stdlib.h>
42 
43 namespace makVrv {
44 
70 {
84  class Token
85  {
86  public:
87  enum Kind
88  {
89  TK_EOS, // End of input stream
90  TK_ERROR, // An error has been encountered
91  TK_WHITESPACE, // A whitespace span (but not newline)
92  TK_NEWLINE, // A single newline (CR & LF)
93  TK_LINECONT, // Line continuation ('\' followed by LF)
94  TK_NUMBER, // A number
95  TK_KEYWORD, // A keyword
96  TK_PUNCTUATION, // A punctuation character
97  TK_DIRECTIVE, // A preprocessor directive
98  TK_STRING, // A string
99  TK_COMMENT, // A block comment
100  TK_LINECOMMENT, // A line comment
101  TK_TEXT, // An unparsed text (cannot be returned from GetToken())
102  };
103 
107  mutable size_t Allocated;
108  union
109  {
111  const char *String;
113  char *Buffer;
114  };
116  size_t Length;
117 
118  Token () : Type(TK_WHITESPACE), Allocated (0), String (NULL), Length (0)
119  { }
120 
121  Token (Kind iType) : Type (iType), Allocated (0), String (NULL), Length (0)
122  { }
123 
124  Token (Kind iType, const char *iString, size_t iLength) :
125  Type (iType), Allocated (0), String (iString), Length (iLength)
126  { }
127 
128  Token (const Token &iOther)
129  {
130  Type = iOther.Type;
131  Allocated = iOther.Allocated;
132  iOther.Allocated = 0; // !!! not quite correct but effective
133  String = iOther.String;
134  Length = iOther.Length;
135  }
136 
138  { if (Allocated) free (Buffer); }
139 
141  Token &operator = (const Token &iOther)
142  {
143  if (Allocated) free (Buffer);
144  Type = iOther.Type;
145  Allocated = iOther.Allocated;
146  iOther.Allocated = 0; // !!! not quite correct but effective
147  String = iOther.String;
148  Length = iOther.Length;
149  return *this;
150  }
151 
153  void Append (const char *iString, size_t iLength);
154 
156  void Append (const Token &iOther);
157 
159  void AppendNL (int iCount);
160 
162  int CountNL ();
163 
165  bool GetValue (long &oValue) const;
166 
168  void SetValue (long iValue);
169 
171  bool operator == (const Token &iOther)
172  {
173  if (iOther.Length != Length)
174  return false;
175  return (memcmp (String, iOther.String, Length) == 0);
176  }
177  };
178 
180  class Macro
181  {
182  public:
186  int NumArgs;
196  Token (*ExpandFunc) (CPreprocessor *iParent, int iNumArgs, Token *iArgs);
198  bool Expanding;
199 
200  Macro (const Token &iName) :
201  Name (iName), NumArgs (0), Args (NULL), Next (NULL),
202  ExpandFunc (NULL), Expanding (false)
203  { }
204 
206  { delete [] Args; delete Next; }
207 
209  Token Expand (int iNumArgs, Token *iArgs, Macro *iMacros);
210  };
211 
212  friend class CPreprocessor::Macro;
213 
215  const char *Source;
217  const char *SourceEnd;
219  int Line;
221  bool BOL;
223  unsigned EnableOutput;
226 
230  CPreprocessor (const Token &iToken, int iLine);
231 
239  Token GetToken (bool iExpand);
240 
250  Token HandleDirective (Token &iToken, int iLine);
251 
262  bool HandleDefine (Token &iBody, int iLine);
263 
274  bool HandleUnDef (Token &iBody, int iLine);
275 
286  bool HandleIfDef (Token &iBody, int iLine);
287 
298  bool HandleIf (Token &iBody, int iLine);
299 
310  bool HandleElse (Token &iBody, int iLine);
311 
322  bool HandleEndIf (Token &iBody, int iLine);
323 
334  Token GetArgument (Token &oArg, bool iExpand);
335 
346  Token GetArguments (int &oNumArgs, Token *&oArgs, bool iExpand);
347 
361  Token GetExpression (Token &oResult, int iLine, int iOpPriority = 0);
362 
378  bool GetValue (const Token &iToken, long &oValue, int iLine);
379 
388  Token ExpandMacro (const Token &iToken);
389 
397  Macro *IsDefined (const Token &iToken);
398 
410  static Token ExpandDefined (CPreprocessor *iParent, int iNumArgs, Token *iArgs);
411 
419  Token Parse (const Token &iSource);
420 
430  void Error (int iLine, const char *iError, const Token *iToken = NULL);
431 
432 public:
435  : MacroList(NULL)
436  , Source(NULL)
437  , SourceEnd(NULL)
438  , Line(0)
439  , BOL(false)
440  , EnableOutput(false)
441  , ErrorData(NULL)
442  { }
443 
445  virtual ~CPreprocessor ();
446 
458  void Define (const char *iMacroName, size_t iMacroNameLen,
459  const char *iMacroValue, size_t iMacroValueLen);
460 
470  void Define (const char *iMacroName, size_t iMacroNameLen, long iMacroValue);
471 
481  bool Undef (const char *iMacroName, size_t iMacroNameLen);
482 
505  char *Parse (const char *iSource, size_t iLength, size_t &oLength);
506 
522  typedef void (*ErrorHandlerFunc) (
523  void *iData, int iLine, const char *iError,
524  const char *iToken, size_t iTokenLen);
525 
532 
534  void *ErrorData;
535 };
536 
537 }
538 
539 #endif // DTGLSLPREPROCESSOR_H

Document ID: Generated on Thu Mar 23 18:54:12 EDT 2017 from SVN revision 174804
Copyright © 2005-2017 VT MÄK. All Rights Reserved (www.mak.com)