Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / basic / source / inc / token.hxx
blob483aab1c3dac217b3f29c8331512a46a91086c44
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_BASIC_SOURCE_INC_TOKEN_HXX
21 #define INCLUDED_BASIC_SOURCE_INC_TOKEN_HXX
23 #include "scanner.hxx"
24 #include <basic/sbdef.hxx>
26 #if defined( EXPLICIT )
27 #undef EXPLICIT
28 #endif
30 // The tokenizer is stand-alone, i. e. he can be used from everywhere.
31 // A BASIC-instance is necessary for error messages. Without BASIC the
32 // errors are only counted. The BASIC is also necessary when an advanced
33 // SBX-variable shall be used for recognition of data types etc.
36 enum SbiToken {
37 NIL = 0,
38 // tokens between 0x20 and 0x3F are literals:
39 LPAREN = '(', RPAREN = ')', COMMA = ',', DOT = '.', EXCLAM = '!',
40 HASH = '#', SEMICOLON = ';',
42 // commands:
43 FIRSTKWD = 0x40,
44 AS = FIRSTKWD, ALIAS, ASSIGN,
45 CALL, CASE, CLOSE, COMPARE, CONST_,
46 DECLARE, DIM, DO,
48 // in the order of the data type enums!
49 DEFINT, DEFLNG, DEFSNG, DEFDBL, DEFCUR, DEFDATE, DEFSTR, DEFOBJ,
50 DEFERR, DEFBOOL, DEFVAR,
51 // in the order of the data type enums!
52 DATATYPE1,
53 TINTEGER = DATATYPE1,
54 TLONG, TSINGLE, TDOUBLE, TCURRENCY, TDATE, TSTRING, TOBJECT,
55 ERROR_, TBOOLEAN, TVARIANT, TBYTE,
56 DATATYPE2 = TBYTE,
58 EACH, ELSE, ELSEIF, END, ERASE, EXIT,
59 FOR, FUNCTION,
60 GET, GLOBAL, GOSUB, GOTO,
61 IF, IN_, INPUT,
62 LET, LINE, LINEINPUT, LOCAL, LOOP, LPRINT, LSET,
63 NAME, NEW, NEXT,
64 ON, OPEN, OPTION, ATTRIBUTE, IMPLEMENTS,
65 PRINT, PRIVATE, PROPERTY, PUBLIC,
66 REDIM, REM, RESUME, RETURN, RSET,
67 SELECT, SET, SHARED, STATIC, STEP, STOP, SUB,
68 TEXT, THEN, TO, TYPE, ENUM,
69 UNTIL,
70 WEND, WHILE, WITH, WRITE,
71 ENDENUM, ENDIF, ENDFUNC, ENDPROPERTY, ENDSUB, ENDTYPE, ENDSELECT, ENDWITH,
72 // end of all keywords
73 LASTKWD = ENDWITH,
74 // statement end
75 EOS, EOLN,
76 // operators:
77 EXPON, NEG, MUL,
78 DIV, IDIV, MOD, PLUS, MINUS,
79 EQ, NE, LT, GT, LE, GE,
80 NOT, AND, OR, XOR, EQV,
81 IMP, CAT, LIKE, IS, TYPEOF,
82 // miscellaneous:
83 FIRSTEXTRA,
84 NUMBER=FIRSTEXTRA, FIXSTRING, SYMBOL, CDECL_, BYVAL, BYREF,
85 OUTPUT, RANDOM, APPEND, BINARY, ACCESS,
86 LOCK, READ, PRESERVE, BASE, ANY, LIB, OPTIONAL_, PTRSAFE,
87 BASIC_EXPLICIT, COMPATIBLE, CLASSMODULE, PARAMARRAY, WITHEVENTS,
89 // from here there are JavaScript-tokens (same enum so that same type)
90 FIRSTJAVA,
91 JS_BREAK=FIRSTJAVA, JS_CONTINUE, JS_FOR, JS_FUNCTION, JS_IF, JS_NEW,
92 JS_RETURN, JS_THIS, JS_VAR, JS_WHILE, JS_WITH,
94 // JavaScript-operators
95 // _ASS_ = Assignment
96 JS_COMMA, JS_ASSIGNMENT, JS_ASS_PLUS, JS_ASS_MINUS, JS_ASS_MUL,
97 JS_ASS_DIV, JS_ASS_MOD, JS_ASS_LSHIFT, JS_ASS_RSHIFT, JS_ASS_RSHIFT_Z,
98 JS_ASS_AND, JS_ASS_XOR, JS_ASS_OR,
99 JS_COND_QUEST, JS_COND_SEL, JS_LOG_OR, JS_LOG_AND, JS_BIT_OR,
100 JS_BIT_XOR, JS_BIT_AND, JS_EQ, JS_NE, JS_LT, JS_LE,
101 JS_GT, JS_GE, JS_LSHIFT, JS_RSHIFT, JS_RSHIFT_Z,
102 JS_PLUS, JS_MINUS, JS_MUL, JS_DIV, JS_MOD, JS_LOG_NOT, JS_BIT_NOT,
103 JS_INC, JS_DEC, JS_LPAREN, JS_RPAREN, JS_LINDEX, JS_RINDEX
104 , VBASUPPORT
107 class SbiTokenizer : public SbiScanner {
108 protected:
109 SbiToken eCurTok;
110 SbiToken ePush;
111 sal_uInt16 nPLine, nPCol1, nPCol2; // pushback location
112 bool bEof;
113 bool bEos;
114 bool bKeywords; // true, if keywords are parsed
115 bool bAs; // last keyword was AS
116 bool bErrorIsSymbol; // Handle Error token as Symbol, not keyword
117 public:
118 SbiTokenizer( const OUString&, StarBASIC* = nullptr );
119 ~SbiTokenizer();
121 bool IsEof() { return bEof; }
122 bool IsEos() { return bEos; }
124 void Push( SbiToken );
125 const OUString& Symbol( SbiToken ); // reconversion
127 SbiToken Peek(); // read the next token
128 SbiToken Next(); // read a token
129 bool MayBeLabel( bool= false );
131 void Error( SbError c ) { GenError( c ); }
132 void Error( SbError, SbiToken );
133 void Error( SbError, const OUString &);
135 static bool IsEoln( SbiToken t )
136 { return t == EOS || t == EOLN || t == REM; }
137 static bool IsKwd( SbiToken t )
138 { return t >= FIRSTKWD && t <= LASTKWD; }
139 static bool IsExtra( SbiToken t )
140 { return t >= FIRSTEXTRA; }
141 static OUString GetKeywordCase( const OUString& sKeyword );
145 #endif
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */