xref: /trunk/main/basic/source/inc/token.hxx (revision 234bd5c5)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _TOKEN_HXX
25 #define _TOKEN_HXX
26 
27 #include "scanner.hxx"
28 #ifndef _SBDEF_HXX
29 #include <basic/sbdef.hxx>
30 #endif
31 
32 #if defined( SHARED )
33 #define SbiTokenSHAREDTMPUNDEF
34 #undef SHARED
35 #endif
36 
37 // Der Tokenizer ist stand-alone, d.h. er kann von ueberallher verwendet
38 // werden. Eine BASIC-Instanz ist fuer Fehlermeldungen notwendig. Ohne
39 // BASIC werden die Fehler nur gezaehlt. Auch ist Basic notwendig, wenn
40 // eine erweiterte SBX-Variable zur Erkennung von Datentypen etc. verwendet
41 // werden soll.
42 
43 enum SbiToken {
44 	NIL = 0,
45 	// Token zwischen 0x20 und 0x3F sind Literale:
46 	LPAREN = '(', RPAREN = ')', COMMA = ',', DOT = '.', EXCLAM = '!',
47 	HASH = '#', SEMICOLON = ';',
48 
49 	// Anweisungen:
50 	FIRSTKWD = 0x40,
51 	AS = FIRSTKWD, ALIAS, ASSIGN,
52 	CALL, CASE, CLOSE, COMPARE, _CONST_,
53 	DECLARE, DIM, DO,
54 
55 	// in der Reihenfolge der Datentyp-Enums!
56 	DEFINT, DEFLNG, DEFSNG, DEFDBL, DEFCUR, DEFDATE, DEFSTR, DEFOBJ,
57 	DEFERR, DEFBOOL, DEFVAR,
58 	// in der Reihenfolge der Datentyp-Enums!
59 	DATATYPE1,
60 	TINTEGER = DATATYPE1,
61 	TLONG, TSINGLE, TDOUBLE, TCURRENCY, TDATE, TSTRING, TOBJECT,
62 	_ERROR_, TBOOLEAN, TVARIANT, TBYTE,
63 	DATATYPE2 = TBYTE,
64 
65 	EACH, ELSE,	ELSEIF, END, ERASE, EXIT,
66 	FOR, FUNCTION,
67 	GET, GLOBAL, GOSUB, GOTO,
68 	IF, _IN_, INPUT,
69 	LET, LINE, LINEINPUT, LOCAL, LOOP, LPRINT, LSET,
70 	NAME, NEW, NEXT,
71 	ON, OPEN, OPTION, IMPLEMENTS,
72 	PRINT, PRIVATE, PROPERTY, PUBLIC,
73 	REDIM, REM, RESUME, RETURN, RSET,
74 	SELECT, SET, SHARED, STATIC, STEP, STOP, SUB,
75 	TEXT, THEN, TO, TYPE, ENUM,
76 	UNTIL,
77 	WEND, WHILE, WITH, WRITE,
78 	ENDENUM, ENDIF, ENDFUNC, ENDPROPERTY, ENDSUB, ENDTYPE, ENDSELECT, ENDWITH,
79 	// Ende aller Keywords
80 	LASTKWD = ENDWITH,
81 	// Statement-Ende
82 	EOS, EOLN,
83 	// Operatoren:
84 	EXPON, NEG, MUL,
85 	DIV, IDIV, MOD, PLUS, MINUS,
86 	EQ, NE, LT, GT, LE, GE,
87 	NOT, AND, OR, XOR, EQV,
88 	IMP, CAT, LIKE, IS, TYPEOF,
89 	// Sonstiges:
90 	FIRSTEXTRA,
91 	NUMBER=FIRSTEXTRA, FIXSTRING, SYMBOL, _CDECL_, BYVAL, BYREF,
92 	OUTPUT, RANDOM, APPEND, BINARY, ACCESS,
93 	LOCK, READ, PRESERVE, BASE, ANY, LIB, _OPTIONAL_,
94 	EXPLICIT, COMPATIBLE, CLASSMODULE, PARAMARRAY, WITHEVENTS,
95 
96 	// Ab hier kommen JavaScript-Tokens (gleiches enum, damit gleicher Typ)
97 	FIRSTJAVA,
98 	JS_BREAK=FIRSTJAVA, JS_CONTINUE, JS_FOR, JS_FUNCTION, JS_IF, JS_NEW,
99 	JS_RETURN, JS_THIS, JS_VAR, JS_WHILE, JS_WITH,
100 
101 	// JavaScript-Operatoren
102 	// _ASS_ = Assignment
103 	JS_COMMA, JS_ASSIGNMENT, JS_ASS_PLUS, JS_ASS_MINUS, JS_ASS_MUL,
104 	JS_ASS_DIV, JS_ASS_MOD, JS_ASS_LSHIFT, JS_ASS_RSHIFT, JS_ASS_RSHIFT_Z,
105 	JS_ASS_AND, JS_ASS_XOR, JS_ASS_OR,
106 	JS_COND_QUEST, JS_COND_SEL, JS_LOG_OR, JS_LOG_AND, JS_BIT_OR,
107 	JS_BIT_XOR, JS_BIT_AND,	JS_EQ, JS_NE, JS_LT, JS_LE,
108 	JS_GT, JS_GE, JS_LSHIFT, JS_RSHIFT, JS_RSHIFT_Z,
109 	JS_PLUS, JS_MINUS, JS_MUL, JS_DIV, JS_MOD, JS_LOG_NOT, JS_BIT_NOT,
110 	JS_INC, JS_DEC, JS_LPAREN, JS_RPAREN, JS_LINDEX, JS_RINDEX
111 	, VBASUPPORT
112 };
113 
114 #ifdef SbiTokenSHAREDTMPUNDEF
115 #define SHARED
116 #undef SbiTokenSHAREDTMPUNDEF
117 #endif
118 
119 // #i109076
120 class TokenLabelInfo
121 {
122 	bool* m_pTokenCanBeLabelTab;
123 
124 public:
125 	TokenLabelInfo( void );
126 	TokenLabelInfo( const TokenLabelInfo& rInfo )
127 		: m_pTokenCanBeLabelTab( NULL )
128 			{ (void)rInfo; }
129 	~TokenLabelInfo();
130 
131 	bool canTokenBeLabel( SbiToken eTok )
132 		{ return m_pTokenCanBeLabelTab[eTok]; }
133 };
134 
135 class SbiTokenizer : public SbiScanner {
136 	TokenLabelInfo	m_aTokenLabelInfo;
137 
138 protected:
139 	SbiToken eCurTok;				// aktuelles Token
140 	SbiToken ePush;					// Pushback-Token
141 	sal_uInt16  nPLine, nPCol1, nPCol2; // Pushback-Location
142 	sal_Bool bEof;						// sal_True bei Dateiende
143 	sal_Bool bEos;						// sal_True bei Statement-Ende
144 	sal_Bool bKeywords;					// sal_True, falls Keywords geparst werden
145 	sal_Bool bAs;						// letztes Keyword war AS
146 	sal_Bool bErrorIsSymbol;			// Handle Error token as Symbol, not keyword
147 public:
148     SbiTokenizer( const ::rtl::OUString&, StarBASIC* = NULL );
149    ~SbiTokenizer();
150 
151 	inline sal_Bool IsEof()				{ return bEof; }
152 	inline sal_Bool IsEos()				{ return bEos; }
153 
154 	void  Push( SbiToken ); 			// Pushback eines Tokens
155 	const String& Symbol( SbiToken );// Rueckumwandlung
156 
157 	SbiToken Peek();				 	// das naechste Token lesen
158 	SbiToken Next();				  	// Ein Token lesen
159 	sal_Bool MayBeLabel( sal_Bool= sal_False );	// Kann es ein Label sein?
160 
161 	void Hilite( SbTextPortions& );	// Syntax-Highlighting
162 
163 	void Error( SbError c ) { GenError( c ); }
164 	void Error( SbError, SbiToken );
165 	void Error( SbError, const char* );
166 	void Error( SbError, String );
167 
168 	void Keywords( sal_Bool b ) { bKeywords = b; }
169 
170 	static sal_Bool IsEoln( SbiToken t )
171 		{ return sal_Bool( t == EOS || t == EOLN || t == REM ); }
172 	static sal_Bool IsKwd( SbiToken t )
173 		{ return sal_Bool( t >= FIRSTKWD && t <= LASTKWD ); }
174 	static sal_Bool IsExtra( SbiToken t )
175 		{ return sal_Bool( t >= FIRSTEXTRA ); }
176 };
177 
178 
179 #endif
180