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