1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
23 #include <basic/sberrors.hxx>
24 #include <rtl/instance.hxx>
25 #include <sal/macros.h>
26 #include <basiccharclass.hxx>
31 struct TokenTable
{ SbiToken t
; const char *s
; };
35 const TokenTable aTokTable_Basic
[] = {
55 { ATTRIBUTE
,"Attribute" },
58 { TBOOLEAN
, "Boolean" },
65 { CLASSMODULE
, "ClassModule" },
67 { COMPARE
, "Compare" },
68 { COMPATIBLE
,"Compatible" },
70 { TCURRENCY
,"Currency" },
72 { DECLARE
, "Declare" },
73 { DEFBOOL
, "DefBool" },
75 { DEFDATE
, "DefDate" },
86 { TDOUBLE
, "Double" },
91 { ENDENUM
, "End Enum" },
92 { ENDFUNC
, "End Function" },
94 { ENDPROPERTY
, "End Property" },
95 { ENDSELECT
,"End Select" },
96 { ENDSUB
, "End Sub" },
97 { ENDTYPE
, "End Type" },
104 { BASIC_EXPLICIT
, "Explicit" },
106 { FUNCTION
, "Function" },
108 { GLOBAL
, "Global" },
113 { IMPLEMENTS
, "Implements" },
115 { INPUT
, "Input" }, // also INPUT #
116 { TINTEGER
, "Integer" },
122 { LINEINPUT
,"Line Input" },
127 { LPRINT
, "LPrint" },
128 { LSET
, "LSet" }, // JSM
134 { TOBJECT
, "Object" },
137 { OPTION
, "Option" },
138 { OPTIONAL_
, "Optional" },
140 { OUTPUT
, "Output" },
141 { PARAMARRAY
, "ParamArray" },
142 { PRESERVE
, "Preserve" },
144 { PRIVATE
, "Private" },
145 { PROPERTY
, "Property" },
146 { PTRSAFE
, "PtrSafe" },
147 { PUBLIC
, "Public" },
148 { RANDOM
, "Random" },
152 { RESUME
, "Resume" },
153 { RETURN
, "Return" },
154 { RSET
, "RSet" }, // JSM
155 { SELECT
, "Select" },
157 { SHARED
, "Shared" },
158 { TSINGLE
, "Single" },
159 { STATIC
, "Static" },
162 { TSTRING
, "String" },
169 { TYPEOF
, "TypeOf" },
171 { TVARIANT
, "Variant" },
172 { VBASUPPORT
, "VbaSupport" },
176 { WITHEVENTS
, "WithEvents" },
177 { WRITE
, "Write" }, // also WRITE #
186 std::array
<bool,VBASUPPORT
+1> m_pTokenCanBeLabelTab
;
191 bool canTokenBeLabel( SbiToken eTok
)
192 { return m_pTokenCanBeLabelTab
[eTok
]; }
195 class StaticTokenLabelInfo
: public ::rtl::Static
< TokenLabelInfo
, StaticTokenLabelInfo
>{};
200 TokenLabelInfo::TokenLabelInfo()
202 m_pTokenCanBeLabelTab
.fill(false);
204 // Token accepted as label by VBA
205 static const SbiToken eLabelToken
[] = { ACCESS
, ALIAS
, APPEND
, BASE
, BINARY
, CLASSMODULE
,
206 COMPARE
, COMPATIBLE
, DEFERR
, ERROR_
, BASIC_EXPLICIT
, LIB
, LINE
, LPRINT
, NAME
,
207 TOBJECT
, OUTPUT
, PROPERTY
, RANDOM
, READ
, STEP
, STOP
, TEXT
, VBASUPPORT
};
208 for( SbiToken eTok
: eLabelToken
)
210 m_pTokenCanBeLabelTab
[eTok
] = true;
215 SbiTokenizer::SbiTokenizer( const OUString
& rSrc
, StarBASIC
* pb
)
216 : SbiScanner(rSrc
, pb
)
225 , bErrorIsSymbol(true)
229 void SbiTokenizer::Push( SbiToken t
)
232 Error( ERRCODE_BASIC_INTERNAL_ERROR
, "PUSH" );
236 void SbiTokenizer::Error( ErrCode code
, const OUString
&aMsg
)
242 void SbiTokenizer::Error( ErrCode code
, SbiToken tok
)
244 aError
= Symbol( tok
);
248 // reading in the next token without absorbing it
250 SbiToken
SbiTokenizer::Peek()
254 sal_Int32 nOldLine
= nLine
;
255 sal_Int32 nOldCol1
= nCol1
;
256 sal_Int32 nOldCol2
= nCol2
;
258 nPLine
= nLine
; nLine
= nOldLine
;
259 nPCol1
= nCol1
; nCol1
= nOldCol1
;
260 nPCol2
= nCol2
; nCol2
= nOldCol2
;
266 // For decompilation. Numbers and symbols return an empty string.
268 const OUString
& SbiTokenizer::Symbol( SbiToken t
)
273 aSym
= OUString(sal::static_int_cast
<sal_Unicode
>(t
));
290 for( auto& rTok
: aTokTable_Basic
)
294 aSym
= OStringToOUString(rTok
.s
, RTL_TEXTENCODING_ASCII_US
);
298 const sal_Unicode
*p
= aSym
.getStr();
306 // Reading in the next token and put it down.
307 // Tokens that don't appear in the token table
308 // are directly returned as a character.
309 // Some words are treated in a special way.
311 SbiToken
SbiTokenizer::Next()
317 // have read in one already?
325 bEos
= IsEoln( eCurTok
);
328 const TokenTable
*tp
;
337 if( aSym
.startsWith("\n") )
350 else if( ( eScanType
== SbxDATE
|| eScanType
== SbxSTRING
) && !bSymbol
)
355 else if( aSym
.isEmpty() )
357 //something went wrong
362 // Special cases of characters that are between "Z" and "a". ICompare()
363 // evaluates the position of these characters in different ways.
364 else if( aSym
[0] == '^' )
369 else if( aSym
[0] == '\\' )
376 if( eScanType
!= SbxVARIANT
)
383 short ub
= SAL_N_ELEMENTS(aTokTable_Basic
)-1;
387 delta
= (ub
- lb
) >> 1;
388 tp
= &aTokTable_Basic
[ lb
+ delta
];
389 sal_Int32 res
= aSym
.compareToIgnoreAsciiCaseAscii( tp
->s
);
419 // Symbol? if not >= token
420 sal_Unicode ch
= aSym
[0];
421 if( !BasicCharClass::isAlpha( ch
, bCompatible
) && !bSymbol
)
423 eCurTok
= static_cast<SbiToken
>(ch
& 0x00FF);
431 bool bStartOfLine
= (eCurTok
== NIL
|| eCurTok
== REM
|| eCurTok
== EOLN
||
432 eCurTok
== THEN
|| eCurTok
== ELSE
); // single line If
433 if( !bStartOfLine
&& (tp
->t
== NAME
|| tp
->t
== LINE
) )
438 else if( tp
->t
== TEXT
)
443 // maybe we can expand this for other statements that have parameters
444 // that are keywords ( and those keywords are only used within such
446 // what's happening here is that if we come across 'append' ( and we are
447 // not in the middle of parsing a special statement ( like 'Open')
448 // we just treat keyword 'append' as a normal 'SYMBOL'.
449 // Also we accept Dim APPEND
450 else if ( ( !bInStatement
|| eCurTok
== DIM
) && tp
->t
== APPEND
)
455 // #i92642: Special LINE token handling -> SbiParser::Line()
457 // END IF, CASE, SUB, DEF, FUNCTION, TYPE, CLASS, WITH
460 // from 15.3.96, special treatment for END, at Peek() the current
461 // time is lost, so memorize everything and restore after
462 sal_Int32 nOldLine
= nLine
;
463 sal_Int32 nOldCol
= nCol
;
464 sal_Int32 nOldCol1
= nCol1
;
465 sal_Int32 nOldCol2
= nCol2
;
466 OUString aOldSym
= aSym
;
467 SaveLine(); // save pLine in the scanner
472 case IF
: Next(); eCurTok
= ENDIF
; break;
473 case SELECT
: Next(); eCurTok
= ENDSELECT
; break;
474 case SUB
: Next(); eCurTok
= ENDSUB
; break;
475 case FUNCTION
: Next(); eCurTok
= ENDFUNC
; break;
476 case PROPERTY
: Next(); eCurTok
= ENDPROPERTY
; break;
477 case TYPE
: Next(); eCurTok
= ENDTYPE
; break;
478 case ENUM
: Next(); eCurTok
= ENDENUM
; break;
479 case WITH
: Next(); eCurTok
= ENDWITH
; break;
480 default : eCurTok
= END
; break;
485 // reset everything so that token is read completely newly after END
495 // are data types keywords?
496 // there is ERROR(), DATA(), STRING() etc.
498 // AS: data types are keywords
509 else if( eCurTok
>= DATATYPE1
&& eCurTok
<= DATATYPE2
&& (bErrorIsSymbol
|| eCurTok
!= ERROR_
) )
515 // CLASSMODULE, PROPERTY, GET, ENUM token only visible in compatible mode
516 SbiToken eTok
= tp
->t
;
519 // #129904 Suppress system
520 if( eTok
== STOP
&& aSym
.equalsIgnoreAsciiCase("system") )
524 if( eTok
== GET
&& bStartOfLine
)
531 if( eTok
== CLASSMODULE
||
532 eTok
== IMPLEMENTS
||
533 eTok
== PARAMARRAY
||
543 bEos
= IsEoln( eCurTok
);
547 bool SbiTokenizer::MayBeLabel( bool bNeedsColon
)
549 if( eCurTok
== SYMBOL
|| StaticTokenLabelInfo::get().canTokenBeLabel( eCurTok
) )
551 return !bNeedsColon
|| DoesColonFollow();
555 return ( eCurTok
== NUMBER
556 && eScanType
== SbxINTEGER
562 OUString
SbiTokenizer::GetKeywordCase( const OUString
& sKeyword
)
564 for( auto& rTok
: aTokTable_Basic
)
566 if( sKeyword
.equalsIgnoreAsciiCaseAscii(rTok
.s
) )
567 return OStringToOUString(rTok
.s
, RTL_TEXTENCODING_ASCII_US
);
572 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */