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 .
20 #include "basiccharclass.hxx"
23 #include <vcl/svapp.hxx>
25 SbiScanner::SbiScanner( const OUString
& rBuf
, StarBASIC
* p
) : aBuf( rBuf
)
30 eScanType
= SbxVARIANT
;
48 bPrevLineExtentsComment
= false;
52 SbiScanner::~SbiScanner()
55 void SbiScanner::LockColumn()
61 void SbiScanner::UnlockColumn()
67 void SbiScanner::GenError( SbError code
)
69 if( GetSbData()->bBlockCompilerError
)
77 // report only one error per statement
81 // in case of EXPECTED or UNEXPECTED it always refers
82 // to the last token, so take the Col1 over
83 sal_Int32 nc
= nColLock
? nSavedCol1
: nCol1
;
87 case SbERR_UNEXPECTED
:
88 case SbERR_SYMBOL_EXPECTED
:
89 case SbERR_LABEL_EXPECTED
:
91 if( nc
> nCol2
) nCol2
= nc
;
94 bRes
= pBasic
->CError( code
, aError
, nLine
, nc
, nCol2
);
96 bAbort
= bAbort
|| !bRes
|| ( code
== SbERR_NO_MEMORY
|| code
== SbERR_PROG_TOO_LARGE
);
102 // used by SbiTokenizer::MayBeLabel() to detect a label
103 bool SbiScanner::DoesColonFollow()
105 if(nCol
< aLine
.getLength() && aLine
[nCol
] == ':')
114 // test for legal suffix
115 static SbxDataType
GetSuffixType( sal_Unicode c
)
120 return SbxDataType(SbxINTEGER
);
122 return SbxDataType(SbxLONG
);
124 return SbxDataType(SbxSINGLE
);
126 return SbxDataType(SbxDOUBLE
);
128 return SbxDataType(SbxCURRENCY
);
130 return SbxDataType(SbxSTRING
);
132 return SbxDataType(SbxVARIANT
);
136 // reading the next symbol into the variables aSym, nVal and eType
137 // return value is sal_False at EOF or errors
140 void SbiScanner::scanAlphanumeric()
143 while(nCol
< aLine
.getLength() && (theBasicCharClass::get().isAlphaNumeric(aLine
[nCol
], bCompatible
) || aLine
[nCol
] == '_'))
148 aSym
= aLine
.copy(n
, nCol
- n
);
151 void SbiScanner::scanGoto()
154 while(n
< aLine
.getLength() && theBasicCharClass::get().isWhitespace(aLine
[n
]))
157 if(n
+ 1 < aLine
.getLength())
159 OUString aTemp
= aLine
.copy(n
, 2);
160 if(aTemp
.equalsIgnoreAsciiCase("to"))
162 aSym
= OUString("goto");
163 pLine
+= n
+ 2 - nCol
;
169 bool SbiScanner::readLine()
171 if(nBufPos
>= aBuf
.getLength())
174 sal_Int32 n
= nBufPos
;
175 sal_Int32 nLen
= aBuf
.getLength();
177 while(n
< nLen
&& aBuf
[n
] != '\r' && aBuf
[n
] != '\n')
180 // Trim trailing whitespace
182 while(nBufPos
< nEnd
&& theBasicCharClass::get().isWhitespace(aBuf
[nEnd
- 1]))
185 aLine
= aBuf
.copy(nBufPos
, nEnd
- nBufPos
);
187 // Fast-forward past the line ending
188 if(n
+ 1 < nLen
&& aBuf
[n
] == '\r' && aBuf
[n
+ 1] == '\n')
194 pLine
= aLine
.getStr();
197 nCol
= nCol1
= nCol2
= 0;
203 bool SbiScanner::NextSym()
205 // memorize for the EOLN-case
206 sal_Int32 nOldLine
= nLine
;
207 sal_Int32 nOldCol1
= nCol1
;
208 sal_Int32 nOldCol2
= nCol2
;
209 sal_Unicode buf
[ BUF_SIZE
], *p
= buf
;
211 eScanType
= SbxVARIANT
;
213 bHash
= bSymbol
= bNumber
= bSpaces
= false;
222 nOldCol1
= nOldCol2
= 0;
225 if(nCol
< aLine
.getLength() && theBasicCharClass::get().isWhitespace(aLine
[nCol
]))
228 while(nCol
< aLine
.getLength() && theBasicCharClass::get().isWhitespace(aLine
[nCol
]))
235 if(nCol
>= aLine
.getLength())
238 if( bPrevLineExtentsComment
)
239 goto PrevLineCommentLbl
;
241 if(nCol
< aLine
.getLength() && aLine
[nCol
] == '#')
248 // copy character if symbol
249 if(nCol
< aLine
.getLength() && (theBasicCharClass::get().isAlpha(aLine
[nCol
], bCompatible
) || aLine
[nCol
] == '_'))
251 // if there's nothing behind '_' , it's the end of a line!
252 if(nCol
+ 1 == aLine
.getLength() && aLine
[nCol
] == '_')
254 // Note that nCol is not incremented here...
263 // Special handling for "go to"
264 if(nCol
< aLine
.getLength() && bCompatible
&& aSym
.equalsIgnoreAsciiCase("go"))
267 // replace closing '_' by space when end of line is following
268 // (wrong line continuation otherwise)
269 if(nCol
== aLine
.getLength() && aLine
[nCol
- 1] == '_' )
271 // We are going to modify a potentially shared string, so force
272 // a copy, so that aSym is not modified by the following operation
273 OUString
aSymCopy( aSym
.getStr(), aSym
.getLength() );
276 // HACK: modifying a potentially shared string here!
277 *((sal_Unicode
*)(pLine
-1)) = ' ';
281 // don't test the exclamation mark
282 // if there's a symbol behind it
283 else if((nCol
>= aLine
.getLength() || aLine
[nCol
] != '!') ||
284 (nCol
+ 1 >= aLine
.getLength() || !theBasicCharClass::get().isAlpha(aLine
[nCol
+ 1], bCompatible
)))
286 if(nCol
< aLine
.getLength())
288 SbxDataType
t(GetSuffixType(aLine
[nCol
]));
289 if( t
!= SbxVARIANT
)
299 // read in and convert if number
300 else if((nCol
< aLine
.getLength() && theBasicCharClass::get().isDigit(aLine
[nCol
] & 0xFF)) ||
301 (nCol
+ 1 < aLine
.getLength() && aLine
[nCol
] == '.' && theBasicCharClass::get().isDigit(aLine
[nCol
+ 1] & 0xFF)))
307 eScanType
= SbxDOUBLE
;
308 bool bBufOverflow
= false;
309 while(nCol
< aLine
.getLength() && strchr("0123456789.DEde", aLine
[nCol
]))
311 // from 4.1.1996: buffer full? -> go on scanning empty
312 if( (p
-buf
) == (BUF_SIZE
-1) )
318 // point or exponent?
319 if(aLine
[nCol
] == '.')
323 ++pLine
; ++nCol
; continue;
328 ++p
, ++pLine
, ++nCol
;
331 else if(strchr("DdEe", aLine
[nCol
]))
335 ++pLine
; ++nCol
; continue;
339 ++p
, ++pLine
, ++nCol
;
341 if(aLine
[nCol
] == '+')
343 else if(aLine
[nCol
] == '-')
346 ++p
, ++pLine
, ++nCol
;
352 ++p
, ++pLine
, ++nCol
;
353 if( comma
&& !exp
) ++ncdig
;
358 aSym
= p
; bNumber
= true;
360 if( comma
> 1 || exp
> 1 )
361 { aError
= OUString('.');
362 GenError( SbERR_BAD_CHAR_IN_NUMBER
); }
364 rtl_math_ConversionStatus eStatus
= rtl_math_ConversionStatus_Ok
;
365 const sal_Unicode
* pParseEnd
= buf
;
366 nVal
= rtl_math_uStringToDouble( buf
, buf
+(p
-buf
), '.', ',', &eStatus
, &pParseEnd
);
367 if (eStatus
!= rtl_math_ConversionStatus_Ok
|| pParseEnd
!= buf
+(p
-buf
))
368 GenError( SbERR_MATH_OVERFLOW
);
373 if( nVal
>= SbxMININT
&& nVal
<= SbxMAXINT
)
374 eScanType
= SbxINTEGER
;
376 if( nVal
>= SbxMINLNG
&& nVal
<= SbxMAXLNG
)
380 GenError( SbERR_MATH_OVERFLOW
);
383 SbxDataType
t(GetSuffixType(aLine
[nCol
]));
384 if( t
!= SbxVARIANT
)
392 // Hex/octal number? Read in and convert:
393 else if(nCol
< aLine
.getLength() && aLine
[nCol
] == '&')
396 sal_Unicode cmp1
[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F', 0 };
397 sal_Unicode cmp2
[] = { '0', '1', '2', '3', '4', '5', '6', '7', 0 };
398 sal_Unicode
*cmp
= cmp1
;
399 sal_Unicode base
= 16;
400 sal_Unicode ndig
= 8;
401 sal_Unicode xch
= aLine
[nCol
] & 0xFF;
403 switch( toupper( xch
) )
406 cmp
= cmp2
; base
= 8; ndig
= 11; break;
410 // treated as an operator
411 --pLine
; --nCol
; nCol1
= nCol
-1;
412 aSym
= OUString("&");
416 // Hex literals are signed Integers ( as defined by basic
417 // e.g. -2,147,483,648 through 2,147,483,647 (signed)
420 bool bBufOverflow
= false;
421 while(nCol
< aLine
.getLength() && theBasicCharClass::get().isAlphaNumeric(aLine
[nCol
] & 0xFF, bCompatible
))
423 sal_Unicode ch
= sal::static_int_cast
< sal_Unicode
>(
424 toupper(aLine
[nCol
] & 0xFF));
426 // from 4.1.1996: buffer full, go on scanning empty
427 if( (p
-buf
) == (BUF_SIZE
-1) )
429 else if( OUString( cmp
).indexOf( ch
) != -1 )
433 aError
= OUString(ch
);
434 GenError( SbERR_BAD_CHAR_IN_NUMBER
);
438 for( p
= buf
; *p
; ++p
)
440 i
= (*p
& 0xFF) - '0';
442 l
= ( l
* base
) + i
;
445 GenError( SbERR_MATH_OVERFLOW
); break;
448 if(nCol
< aLine
.getLength() && aLine
[nCol
] == '&') ++pLine
, ++nCol
;
450 eScanType
= ( l
>= SbxMININT
&& l
<= SbxMAXINT
) ? SbxINTEGER
: SbxLONG
;
452 GenError( SbERR_MATH_OVERFLOW
);
456 else if( *pLine
== '"' || *pLine
== '[' )
458 sal_Unicode cSep
= *pLine
;
460 bSymbol
= true, cSep
= ']';
461 sal_Int32 n
= nCol
+ 1;
465 while( *pLine
&& ( *pLine
!= cSep
) );
469 if( *pLine
!= cSep
|| cSep
== ']' ) break;
470 } else aError
= OUString(cSep
), GenError( SbERR_EXPECTED
);
472 // If VBA Interop then doen't eat the [] chars
473 if ( cSep
== ']' && bVBASupportOn
)
474 aSym
= aLine
.copy( n
- 1, nCol
- n
+ 1);
476 aSym
= aLine
.copy( n
, nCol
- n
- 1 );
477 // get out duplicate string delimiters
478 OUStringBuffer aSymBuf
;
479 for ( sal_Int32 i
= 0, len
= aSym
.getLength(); i
< len
; ++i
)
481 aSymBuf
.append( aSym
[i
] );
482 if ( aSym
[i
] == cSep
&& ( i
+1 < len
) && aSym
[i
+1] == cSep
)
485 aSym
= aSymBuf
.makeStringAndClear();
487 eScanType
= ( cSep
== '#' ) ? SbxDATE
: SbxSTRING
;
489 // invalid characters:
490 else if( ( *pLine
& 0xFF ) >= 0x7F )
492 GenError( SbERR_SYNTAX
); pLine
++; nCol
++;
500 case '<': if( *pLine
== '>' || *pLine
== '=' ) n
= 2; break;
501 case '>': if( *pLine
== '=' ) n
= 2; break;
502 case ':': if( *pLine
== '=' ) n
= 2; break;
504 aSym
= aLine
.copy( nCol
, n
);
505 pLine
+= n
-1; nCol
= nCol
+ n
;
512 if( bPrevLineExtentsComment
|| (eScanType
!= SbxSTRING
&&
513 ( aSym
[0] == '\'' || aSym
.equalsIgnoreAsciiCase( "REM" ) ) ) )
515 bPrevLineExtentsComment
= false;
516 aSym
= OUString("REM");
517 sal_Int32 nLen
= rtl_ustr_getLength(pLine
);
518 if( bCompatible
&& pLine
[ nLen
- 1 ] == '_' && pLine
[ nLen
- 2 ] == ' ' )
519 bPrevLineExtentsComment
= true;
520 nCol2
= nCol2
+ nLen
;
527 if( nCol
&& *--pLine
== '_' )
530 bool bRes
= NextSym();
531 if( bVBASupportOn
&& aSym
[0] == '.' )
535 // ^^^ <- spaces is legal in MSO VBA
536 OSL_TRACE("*** resetting bSpaces***");
547 aSym
= OUString("\n");
553 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */