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>
29 struct TokenTable
{ SbiToken t
; const char *s
; };
31 static const TokenTable aTokTable_Basic
[] = {
51 { ATTRIBUTE
,"Attribute" },
54 { TBOOLEAN
, "Boolean" },
61 { CLASSMODULE
, "ClassModule" },
63 { COMPARE
, "Compare" },
64 { COMPATIBLE
,"Compatible" },
66 { TCURRENCY
,"Currency" },
68 { DECLARE
, "Declare" },
69 { DEFBOOL
, "DefBool" },
71 { DEFDATE
, "DefDate" },
82 { TDOUBLE
, "Double" },
87 { ENDENUM
, "End Enum" },
88 { ENDFUNC
, "End Function" },
90 { ENDPROPERTY
, "End Property" },
91 { ENDSELECT
,"End Select" },
92 { ENDSUB
, "End Sub" },
93 { ENDTYPE
, "End Type" },
100 { BASIC_EXPLICIT
, "Explicit" },
102 { FUNCTION
, "Function" },
104 { GLOBAL
, "Global" },
109 { IMPLEMENTS
, "Implements" },
111 { INPUT
, "Input" }, // also INPUT #
112 { TINTEGER
, "Integer" },
118 { LINEINPUT
,"Line Input" },
123 { LPRINT
, "LPrint" },
124 { LSET
, "LSet" }, // JSM
130 { TOBJECT
, "Object" },
133 { OPTION
, "Option" },
134 { OPTIONAL_
, "Optional" },
136 { OUTPUT
, "Output" },
137 { PARAMARRAY
, "ParamArray" },
138 { PRESERVE
, "Preserve" },
140 { PRIVATE
, "Private" },
141 { PROPERTY
, "Property" },
142 { PTRSAFE
, "PtrSafe" },
143 { PUBLIC
, "Public" },
144 { RANDOM
, "Random" },
148 { RESUME
, "Resume" },
149 { RETURN
, "Return" },
150 { RSET
, "RSet" }, // JSM
151 { SELECT
, "Select" },
153 { SHARED
, "Shared" },
154 { TSINGLE
, "Single" },
155 { STATIC
, "Static" },
158 { TSTRING
, "String" },
165 { TYPEOF
, "TypeOf" },
167 { TVARIANT
, "Variant" },
168 { VBASUPPORT
, "VbaSupport" },
172 { WITHEVENTS
, "WithEvents" },
173 { WRITE
, "Write" }, // also WRITE #
180 std::array
<bool,VBASUPPORT
+1> m_pTokenCanBeLabelTab
;
185 bool canTokenBeLabel( SbiToken eTok
)
186 { return m_pTokenCanBeLabelTab
[eTok
]; }
189 class StaticTokenLabelInfo
: public ::rtl::Static
< TokenLabelInfo
, StaticTokenLabelInfo
>{};
192 TokenLabelInfo::TokenLabelInfo()
194 m_pTokenCanBeLabelTab
.fill(false);
196 // Token accepted as label by VBA
197 static const SbiToken eLabelToken
[] = { ACCESS
, ALIAS
, APPEND
, BASE
, BINARY
, CLASSMODULE
,
198 COMPARE
, COMPATIBLE
, DEFERR
, ERROR_
, BASIC_EXPLICIT
, LIB
, LINE
, LPRINT
, NAME
,
199 TOBJECT
, OUTPUT
, PROPERTY
, RANDOM
, READ
, STEP
, STOP
, TEXT
, VBASUPPORT
};
200 for( SbiToken eTok
: eLabelToken
)
202 m_pTokenCanBeLabelTab
[eTok
] = true;
207 SbiTokenizer::SbiTokenizer( const OUString
& rSrc
, StarBASIC
* pb
)
208 : SbiScanner(rSrc
, pb
)
217 , bErrorIsSymbol(true)
221 void SbiTokenizer::Push( SbiToken t
)
224 Error( ERRCODE_BASIC_INTERNAL_ERROR
, "PUSH" );
228 void SbiTokenizer::Error( ErrCode code
, const OUString
&aMsg
)
234 void SbiTokenizer::Error( ErrCode code
, SbiToken tok
)
236 aError
= Symbol( tok
);
240 // reading in the next token without absorbing it
242 SbiToken
SbiTokenizer::Peek()
246 sal_Int32 nOldLine
= nLine
;
247 sal_Int32 nOldCol1
= nCol1
;
248 sal_Int32 nOldCol2
= nCol2
;
250 nPLine
= nLine
; nLine
= nOldLine
;
251 nPCol1
= nCol1
; nCol1
= nOldCol1
;
252 nPCol2
= nCol2
; nCol2
= nOldCol2
;
254 return eCurTok
= ePush
;
257 // For decompilation. Numbers and symbols return an empty string.
259 const OUString
& SbiTokenizer::Symbol( SbiToken t
)
264 aSym
= OUString(sal::static_int_cast
<sal_Unicode
>(t
));
281 for( auto& rTok
: aTokTable_Basic
)
285 aSym
= OStringToOUString(rTok
.s
, RTL_TEXTENCODING_ASCII_US
);
289 const sal_Unicode
*p
= aSym
.getStr();
297 // Reading in the next token and put it down.
298 // Tokens that don't appear in the token table
299 // are directly returned as a character.
300 // Some words are treated in a special way.
302 SbiToken
SbiTokenizer::Next()
308 // have read in one already?
316 bEos
= IsEoln( eCurTok
);
319 const TokenTable
*tp
;
324 return eCurTok
= EOLN
;
327 if( aSym
.startsWith("\n") )
330 return eCurTok
= EOLN
;
336 return eCurTok
= NUMBER
;
338 else if( ( eScanType
== SbxDATE
|| eScanType
== SbxSTRING
) && !bSymbol
)
340 return eCurTok
= FIXSTRING
;
342 else if( aSym
.isEmpty() )
344 //something went wrong
346 return eCurTok
= EOLN
;
348 // Special cases of characters that are between "Z" and "a". ICompare()
349 // evaluates the position of these characters in different ways.
350 else if( aSym
[0] == '^' )
352 return eCurTok
= EXPON
;
354 else if( aSym
[0] == '\\' )
356 return eCurTok
= IDIV
;
360 if( eScanType
!= SbxVARIANT
)
361 return eCurTok
= SYMBOL
;
364 short ub
= SAL_N_ELEMENTS(aTokTable_Basic
)-1;
368 delta
= (ub
- lb
) >> 1;
369 tp
= &aTokTable_Basic
[ lb
+ delta
];
370 sal_Int32 res
= aSym
.compareToIgnoreAsciiCaseAscii( tp
->s
);
400 // Symbol? if not >= token
401 sal_Unicode ch
= aSym
[0];
402 if( !BasicCharClass::isAlpha( ch
, bCompatible
) && !bSymbol
)
404 return eCurTok
= static_cast<SbiToken
>(ch
& 0x00FF);
406 return eCurTok
= SYMBOL
;
410 bool bStartOfLine
= (eCurTok
== NIL
|| eCurTok
== REM
|| eCurTok
== EOLN
||
411 eCurTok
== THEN
|| eCurTok
== ELSE
); // single line If
412 if( !bStartOfLine
&& (tp
->t
== NAME
|| tp
->t
== LINE
) )
414 return eCurTok
= SYMBOL
;
416 else if( tp
->t
== TEXT
)
418 return eCurTok
= SYMBOL
;
420 // maybe we can expand this for other statements that have parameters
421 // that are keywords ( and those keywords are only used within such
423 // what's happening here is that if we come across 'append' ( and we are
424 // not in the middle of parsing a special statement ( like 'Open')
425 // we just treat keyword 'append' as a normal 'SYMBOL'.
426 // Also we accept Dim APPEND
427 else if ( ( !bInStatement
|| eCurTok
== DIM
) && tp
->t
== APPEND
)
429 return eCurTok
= SYMBOL
;
431 // #i92642: Special LINE token handling -> SbiParser::Line()
433 // END IF, CASE, SUB, DEF, FUNCTION, TYPE, CLASS, WITH
436 // from 15.3.96, special treatment for END, at Peek() the current
437 // time is lost, so memorize everything and restore after
438 sal_Int32 nOldLine
= nLine
;
439 sal_Int32 nOldCol
= nCol
;
440 sal_Int32 nOldCol1
= nCol1
;
441 sal_Int32 nOldCol2
= nCol2
;
442 OUString aOldSym
= aSym
;
443 SaveLine(); // save pLine in the scanner
448 case IF
: Next(); eCurTok
= ENDIF
; break;
449 case SELECT
: Next(); eCurTok
= ENDSELECT
; break;
450 case SUB
: Next(); eCurTok
= ENDSUB
; break;
451 case FUNCTION
: Next(); eCurTok
= ENDFUNC
; break;
452 case PROPERTY
: Next(); eCurTok
= ENDPROPERTY
; break;
453 case TYPE
: Next(); eCurTok
= ENDTYPE
; break;
454 case ENUM
: Next(); eCurTok
= ENDENUM
; break;
455 case WITH
: Next(); eCurTok
= ENDWITH
; break;
456 default : eCurTok
= END
; break;
461 // reset everything so that token is read completely newly after END
471 // are data types keywords?
472 // there is ERROR(), DATA(), STRING() etc.
474 // AS: data types are keywords
485 else if( eCurTok
>= DATATYPE1
&& eCurTok
<= DATATYPE2
&& (bErrorIsSymbol
|| eCurTok
!= ERROR_
) )
491 // CLASSMODULE, PROPERTY, GET, ENUM token only visible in compatible mode
492 SbiToken eTok
= tp
->t
;
495 // #129904 Suppress system
496 if( eTok
== STOP
&& aSym
.equalsIgnoreAsciiCase("system") )
500 if( eTok
== GET
&& bStartOfLine
)
507 if( eTok
== CLASSMODULE
||
508 eTok
== IMPLEMENTS
||
509 eTok
== PARAMARRAY
||
519 bEos
= IsEoln( eCurTok
);
523 bool SbiTokenizer::MayBeLabel( bool bNeedsColon
)
525 if( eCurTok
== SYMBOL
|| StaticTokenLabelInfo::get().canTokenBeLabel( eCurTok
) )
527 return !bNeedsColon
|| DoesColonFollow();
531 return ( eCurTok
== NUMBER
532 && eScanType
== SbxINTEGER
538 OUString
SbiTokenizer::GetKeywordCase( const OUString
& sKeyword
)
540 for( auto& rTok
: aTokTable_Basic
)
542 if( sKeyword
.equalsIgnoreAsciiCaseAscii(rTok
.s
) )
543 return OStringToOUString(rTok
.s
, RTL_TEXTENCODING_ASCII_US
);
548 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */