summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmscanner.h
blob: b18d1722db0124766d2b13714d2da0dcb35c0537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//-*-C++-*-
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2000-2001 by Andreas Zehender
    email                : zehender@kde.org
**************************************************************************

**************************************************************************
*                                                                        *
*  This program is free software; you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation; either version 2 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
**************************************************************************/


#ifndef PMSCANNER_H
#define PMSCANNER_H

#include <tqiodevice.h>
#include <tqstring.h>
#include <tqasciidict.h>

/**
 * Dictionary of reserved words for fast lookup
 *
 * The class @ref PMScanner has two static dictionaries: one for reserved
 * words and one for directives. The constructor will insert the items.
 */
class PMReservedWordDict : protected TQAsciiDict<int>
{
public:
   /**
    * Mode for constructor.
    */
   enum PMDictMode { PMDReservedWords, PMDDirectives };
   /**
    * Creates a dictionary for povray reserved words or directives.
    */
   PMReservedWordDict( PMDictMode mode );
   /**
    * Deletes the dictionary
    */
   ~PMReservedWordDict( );

   /**
    * Returns the token constant for the key if found, otherwise -1
    */
   int operator[] ( const char* key ) const { return tqfind( key ); }
   /**
    * Returns the token constant for the key if found, otherwise -1
    */
   int tqfind( const char* key ) const
   {
      int* result = TQAsciiDict<int>::tqfind( key );
      if( result )
         return *result;
      return -1;
   }
};


/**
 * Scanner that scans povray tokens out of a TQIODevice
 */
class PMScanner
{
public:
   /**
    * Creates a scanner that scans the TQIODevice device
    */
   PMScanner( TQIODevice* device );
   /**
    * Deletes the scanner
    */
   ~PMScanner( );

   /**
    * Scans the device for the next token. Returns a value of @ref PMToken
    * ( > 0xFF ) or a single character
    */
   int nextToken( );
   /**
    * Returns the current token
    */
   int currentToken( ) const { return m_token; }

   /**
    * Returns the integer value of the current token if currentToken
    * is INTEGER_TOK
    */
   int iValue( ) const { return m_ivalue; }
   /**
    * Returns the double value of the current token if currentToken
    * is FLOAT_TOK
    */
   double fValue( ) const { return m_fvalue; }
   /**
    * Returns the string value of the current token if currentToken
    * is ID_TOK, COMMENT_TOK, STRING_TOK
    */
   const char* sValue( ) const { return m_svalue; }
   /**
    * Returns the current line number
    */
   int currentLine( ) const { return m_line; }
   /**
    * Returns the error string if current token is SCANNER_ERROR_TOK
    */
   TQString error( ) const { return m_error; }
   /**
    * Special parse method for a function statement
    */
   void scanFunction( );
   /**
    * Returns a pointer to a dictionary with reserved words
    */
   static PMReservedWordDict* reservedWords( ) { return &m_reservedWords; }
   /**
    * Returns a pointer to a dictionary with directives
    */
   static PMReservedWordDict* directives( ) { return &m_directives; }
private:
   /**
    * returns true if c is one of the following characters:
    * space, tab, newline, '{', '}', '<', '>', '+', '-', '*', '/', ',',
    * '(', ')', '=', '[', ']', ';'
    */
   inline bool isseparation( int c );
   /**
    * Called on unexpected character
    */
   void scanError( int c );
   /**
    * Reads the next character out of the device
    */
   inline void nextChar( );
   /**
    * Adds the char to m_svalue
    */
   inline void addChar( char c );
   /**
    * Clears m_svalue
    */
   inline void clearSValue( );

   static PMReservedWordDict m_reservedWords;
   static PMReservedWordDict m_directives;

   /**
    * States for the state machine of the scanner
    */
   enum PMScanStates
   {
      START_ST = 0, ID_ENDST, INTEGER_ENDST,
      FLOAT1_ST, FLOAT_ENDST, FLOAT_EXP1_ST, FLOAT_EXP2_ST, FLOAT_EXP_ENDST,
      POINT_ST,
      DIRECTIVE1_ST, DIRECTIVE_ENDST,
      STRING1_ST, STRINGBS_ST,
      SLASH_ST, LINE_COMMENT_FIRST_ST, LINE_COMMENT_ST,
      C_COMMENT_ST, COMMENT_ST_ST, COMMENT_SL_ST, COMMENT_NEW_LINE_ST,
      PMNAME_ST,
      RAW_POVRAY_FIRST_ST, RAW_POVRAY_LB_ST, RAW_POVRAY_ST, RAW_POVRAY_END_ST,
      RAW_POVRAY_END_END_ST,
      TOKEN_END_ST
   };

   TQIODevice* m_pDevice;

   int m_char;
   int m_token;
   int m_ivalue;
   double m_fvalue;
   char* m_svalue;
   char* m_lastChar;
   char* m_lastAlloc;
   unsigned int m_svalueAlloc;
   int m_indentation;
   int m_rawIndentation;
   int m_rawPovrayEnd;
   bool m_bFunctionMode;

   int m_line;
   TQString m_error;
};


#endif