Line data Source code
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 : #include "basiccharclass.hxx"
21 : #include "sbcomp.hxx"
22 :
23 : #include <vcl/svapp.hxx>
24 :
25 468 : SbiScanner::SbiScanner( const OUString& rBuf, StarBASIC* p ) : aBuf( rBuf )
26 : {
27 468 : pBasic = p;
28 468 : pLine = NULL;
29 468 : nVal = 0;
30 468 : eScanType = SbxVARIANT;
31 468 : nErrors = 0;
32 468 : nBufPos = 0;
33 468 : nCurCol1 = 0;
34 468 : nSavedCol1 = 0;
35 468 : nColLock = 0;
36 468 : nLine = 0;
37 468 : nCol1 = 0;
38 468 : nCol2 = 0;
39 468 : nCol = 0;
40 : bError =
41 : bAbort =
42 : bSpaces =
43 : bNumber =
44 : bSymbol =
45 : bCompatible =
46 : bVBASupportOn =
47 : bInStatement =
48 468 : bPrevLineExtentsComment = false;
49 468 : bHash = true;
50 468 : pSaveLine = NULL;
51 468 : }
52 :
53 2629 : SbiScanner::~SbiScanner()
54 2629 : {}
55 :
56 9515 : void SbiScanner::LockColumn()
57 : {
58 9515 : if( !nColLock++ )
59 6821 : nSavedCol1 = nCol1;
60 9515 : }
61 :
62 9254 : void SbiScanner::UnlockColumn()
63 : {
64 9254 : if( nColLock )
65 6302 : nColLock--;
66 9254 : }
67 :
68 8 : void SbiScanner::GenError( SbError code )
69 : {
70 8 : if( GetSbData()->bBlockCompilerError )
71 : {
72 0 : bAbort = true;
73 8 : return;
74 : }
75 8 : if( !bError )
76 : {
77 8 : bool bRes = true;
78 : // report only one error per statement
79 8 : bError = true;
80 8 : if( pBasic )
81 : {
82 : // in case of EXPECTED or UNEXPECTED it always refers
83 : // to the last token, so take the Col1 over
84 0 : sal_Int32 nc = nColLock ? nSavedCol1 : nCol1;
85 0 : switch( code )
86 : {
87 : case SbERR_EXPECTED:
88 : case SbERR_UNEXPECTED:
89 : case SbERR_SYMBOL_EXPECTED:
90 : case SbERR_LABEL_EXPECTED:
91 0 : nc = nCol1;
92 0 : if( nc > nCol2 ) nCol2 = nc;
93 0 : break;
94 : }
95 0 : bRes = pBasic->CError( code, aError, nLine, nc, nCol2 );
96 : }
97 8 : bAbort = bAbort || !bRes || ( code == SbERR_NO_MEMORY || code == SbERR_PROG_TOO_LARGE );
98 : }
99 8 : nErrors++;
100 : }
101 :
102 :
103 : // used by SbiTokenizer::MayBeLabel() to detect a label
104 3076 : bool SbiScanner::DoesColonFollow()
105 : {
106 3076 : if(nCol < aLine.getLength() && aLine[nCol] == ':')
107 : {
108 52 : ++pLine; ++nCol;
109 52 : return true;
110 : }
111 : else
112 3024 : return false;
113 : }
114 :
115 : // test for legal suffix
116 39447 : static SbxDataType GetSuffixType( sal_Unicode c )
117 : {
118 39447 : switch (c)
119 : {
120 : case '%':
121 206 : return SbxDataType(SbxINTEGER);
122 : case '&':
123 2 : return SbxDataType(SbxLONG);
124 : case '!':
125 1 : return SbxDataType(SbxSINGLE);
126 : case '#':
127 5 : return SbxDataType(SbxDOUBLE);
128 : case '@':
129 1 : return SbxDataType(SbxCURRENCY);
130 : case '$':
131 393 : return SbxDataType(SbxSTRING);
132 : default:
133 38839 : return SbxDataType(SbxVARIANT);
134 : }
135 : }
136 :
137 : // reading the next symbol into the variables aSym, nVal and eType
138 : // return value is sal_False at EOF or errors
139 : #define BUF_SIZE 80
140 :
141 44648 : void SbiScanner::scanAlphanumeric()
142 : {
143 44648 : sal_Int32 n = nCol;
144 396527 : while(nCol < aLine.getLength() && (theBasicCharClass::get().isAlphaNumeric(aLine[nCol], bCompatible) || aLine[nCol] == '_'))
145 : {
146 307231 : ++pLine;
147 307231 : ++nCol;
148 : }
149 44648 : aSym = aLine.copy(n, nCol - n);
150 44648 : }
151 :
152 1 : void SbiScanner::scanGoto()
153 : {
154 1 : sal_Int32 n = nCol;
155 4 : while(n < aLine.getLength() && theBasicCharClass::get().isWhitespace(aLine[n]))
156 2 : ++n;
157 :
158 1 : if(n + 1 < aLine.getLength())
159 : {
160 1 : OUString aTemp = aLine.copy(n, 2);
161 1 : if(aTemp.equalsIgnoreAsciiCase("to"))
162 : {
163 1 : aSym = "goto";
164 1 : pLine += n + 2 - nCol;
165 1 : nCol = n + 2;
166 1 : }
167 : }
168 1 : }
169 :
170 18660 : bool SbiScanner::readLine()
171 : {
172 18660 : if(nBufPos >= aBuf.getLength())
173 468 : return false;
174 :
175 18192 : sal_Int32 n = nBufPos;
176 18192 : sal_Int32 nLen = aBuf.getLength();
177 :
178 636511 : while(n < nLen && aBuf[n] != '\r' && aBuf[n] != '\n')
179 600127 : ++n;
180 :
181 : // Trim trailing whitespace
182 18192 : sal_Int32 nEnd = n;
183 41302 : while(nBufPos < nEnd && theBasicCharClass::get().isWhitespace(aBuf[nEnd - 1]))
184 4918 : --nEnd;
185 :
186 18192 : aLine = aBuf.copy(nBufPos, nEnd - nBufPos);
187 :
188 : // Fast-forward past the line ending
189 18192 : if(n + 1 < nLen && aBuf[n] == '\r' && aBuf[n + 1] == '\n')
190 5 : n += 2;
191 18187 : else if(n < nLen)
192 18085 : ++n;
193 :
194 18192 : nBufPos = n;
195 18192 : pLine = aLine.getStr();
196 :
197 18192 : ++nLine;
198 18192 : nCol = nCol1 = nCol2 = 0;
199 18192 : nColLock = 0;
200 :
201 18192 : return true;
202 : }
203 :
204 108827 : bool SbiScanner::NextSym()
205 : {
206 : // memorize for the EOLN-case
207 108827 : sal_Int32 nOldLine = nLine;
208 108827 : sal_Int32 nOldCol1 = nCol1;
209 108827 : sal_Int32 nOldCol2 = nCol2;
210 108827 : sal_Unicode buf[ BUF_SIZE ], *p = buf;
211 :
212 108827 : eScanType = SbxVARIANT;
213 108827 : aSym = "";
214 108827 : bHash = bSymbol = bNumber = bSpaces = false;
215 :
216 : // read in line?
217 108827 : if( !pLine )
218 : {
219 18660 : if(!readLine())
220 468 : return false;
221 :
222 18192 : nOldLine = nLine;
223 18192 : nOldCol1 = nOldCol2 = 0;
224 : }
225 :
226 108359 : if(nCol < aLine.getLength() && theBasicCharClass::get().isWhitespace(aLine[nCol]))
227 : {
228 49980 : bSpaces = true;
229 183447 : while(nCol < aLine.getLength() && theBasicCharClass::get().isWhitespace(aLine[nCol]))
230 83487 : ++pLine, ++nCol;
231 : }
232 :
233 108359 : nCol1 = nCol;
234 :
235 : // only blank line?
236 108359 : if(nCol >= aLine.getLength())
237 16163 : goto eoln;
238 :
239 92196 : if( bPrevLineExtentsComment )
240 0 : goto PrevLineCommentLbl;
241 :
242 92196 : if(nCol < aLine.getLength() && aLine[nCol] == '#')
243 : {
244 62 : ++pLine;
245 62 : ++nCol;
246 62 : bHash = true;
247 : }
248 :
249 : // copy character if symbol
250 92196 : if(nCol < aLine.getLength() && (theBasicCharClass::get().isAlpha(aLine[nCol], bCompatible) || aLine[nCol] == '_'))
251 : {
252 : // if there's nothing behind '_' , it's the end of a line!
253 44700 : if(nCol + 1 == aLine.getLength() && aLine[nCol] == '_')
254 : {
255 : // Note that nCol is not incremented here...
256 52 : ++pLine;
257 52 : goto eoln;
258 : }
259 :
260 44648 : bSymbol = true;
261 :
262 44648 : scanAlphanumeric();
263 :
264 : // Special handling for "go to"
265 44648 : if(nCol < aLine.getLength() && bCompatible && aSym.equalsIgnoreAsciiCase("go"))
266 1 : scanGoto();
267 :
268 : // replace closing '_' by space when end of line is following
269 : // (wrong line continuation otherwise)
270 44648 : if(nCol == aLine.getLength() && aLine[nCol - 1] == '_' )
271 : {
272 : // We are going to modify a potentially shared string, so force
273 : // a copy, so that aSym is not modified by the following operation
274 1 : OUString aSymCopy( aSym.getStr(), aSym.getLength() );
275 1 : aSym = aSymCopy;
276 :
277 : // HACK: modifying a potentially shared string here!
278 1 : *((sal_Unicode*)(pLine-1)) = ' ';
279 : }
280 :
281 : // type recognition?
282 : // don't test the exclamation mark
283 : // if there's a symbol behind it
284 89295 : else if((nCol >= aLine.getLength() || aLine[nCol] != '!') ||
285 3 : (nCol + 1 >= aLine.getLength() || !theBasicCharClass::get().isAlpha(aLine[nCol + 1], bCompatible)))
286 : {
287 44646 : if(nCol < aLine.getLength())
288 : {
289 37195 : SbxDataType t(GetSuffixType(aLine[nCol]));
290 37195 : if( t != SbxVARIANT )
291 : {
292 604 : eScanType = t;
293 604 : ++pLine;
294 604 : ++nCol;
295 : }
296 : }
297 : }
298 : }
299 :
300 : // read in and convert if number
301 94994 : else if((nCol < aLine.getLength() && theBasicCharClass::get().isDigit(aLine[nCol] & 0xFF)) ||
302 83969 : (nCol + 1 < aLine.getLength() && aLine[nCol] == '.' && theBasicCharClass::get().isDigit(aLine[nCol + 1] & 0xFF)))
303 : {
304 3940 : short exp = 0;
305 3940 : short dec = 0;
306 3940 : eScanType = SbxDOUBLE;
307 3940 : bool bScanError = false;
308 3940 : bool bBufOverflow = false;
309 : // All this because of 'D' or 'd' floating point type, sigh..
310 13792 : while(!bScanError && nCol < aLine.getLength() && strchr("0123456789.DEde", aLine[nCol]))
311 : {
312 : // from 4.1.1996: buffer full? -> go on scanning empty
313 5912 : if( (p-buf) == (BUF_SIZE-1) )
314 : {
315 47 : bBufOverflow = true;
316 47 : ++pLine, ++nCol;
317 47 : continue;
318 : }
319 : // point or exponent?
320 5865 : if(aLine[nCol] == '.')
321 : {
322 154 : if( ++dec > 1 )
323 1 : bScanError = true;
324 : else
325 153 : *p++ = '.';
326 : }
327 5711 : else if(strchr("DdEe", aLine[nCol]))
328 : {
329 8 : if (++exp > 1)
330 1 : bScanError = true;
331 : else
332 : {
333 7 : *p++ = 'E';
334 7 : if (nCol + 1 < aLine.getLength() && (aLine[nCol+1] == '+' || aLine[nCol+1] == '-'))
335 : {
336 4 : ++pLine, ++nCol;
337 4 : if( (p-buf) == (BUF_SIZE-1) )
338 : {
339 0 : bBufOverflow = true;
340 0 : continue;
341 : }
342 4 : *p++ = aLine[nCol];
343 : }
344 : }
345 : }
346 : else
347 : {
348 5703 : *p++ = aLine[nCol];
349 : }
350 5865 : ++pLine, ++nCol;
351 : }
352 3940 : *p = 0;
353 3940 : aSym = p; bNumber = true;
354 :
355 : // For bad characters, scan and parse errors generate only one error.
356 3940 : SbError nError = 0;
357 3940 : if (bScanError)
358 : {
359 2 : --pLine, --nCol;
360 2 : aError = OUString( aLine[nCol]);
361 2 : nError = SbERR_BAD_CHAR_IN_NUMBER;
362 : }
363 :
364 3940 : rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok;
365 3940 : const sal_Unicode* pParseEnd = buf;
366 3940 : nVal = rtl_math_uStringToDouble( buf, buf+(p-buf), '.', ',', &eStatus, &pParseEnd );
367 3940 : if (pParseEnd != buf+(p-buf))
368 : {
369 : // e.g. "12e" or "12e+", or with bScanError "12d"+"E".
370 2 : sal_Int32 nChars = buf+(p-buf) - pParseEnd;
371 2 : pLine -= nChars;
372 2 : nCol -= nChars;
373 : // For bScanError, pLine and nCol were already decremented, just
374 : // add that character to the parse end.
375 2 : if (bScanError)
376 1 : ++nChars;
377 : // Copy error position from original string, not the buffer
378 : // replacement where "12dE" => "12EE".
379 2 : aError = aLine.copy( nCol, nChars);
380 2 : nError = SbERR_BAD_CHAR_IN_NUMBER;
381 : }
382 3938 : else if (eStatus != rtl_math_ConversionStatus_Ok)
383 : {
384 : // Keep the scan error and character at position, if any.
385 1 : if (!nError)
386 1 : nError = SbERR_MATH_OVERFLOW;
387 : }
388 :
389 3940 : if (nError)
390 4 : GenError( nError );
391 :
392 3940 : if( !dec && !exp )
393 : {
394 3780 : if( nVal >= SbxMININT && nVal <= SbxMAXINT )
395 3759 : eScanType = SbxINTEGER;
396 21 : else if( nVal >= SbxMINLNG && nVal <= SbxMAXLNG )
397 19 : eScanType = SbxLONG;
398 : }
399 :
400 3940 : if( bBufOverflow )
401 1 : GenError( SbERR_MATH_OVERFLOW );
402 :
403 : // type recognition?
404 3940 : if( nCol < aLine.getLength() )
405 : {
406 2252 : SbxDataType t(GetSuffixType(aLine[nCol]));
407 2252 : if( t != SbxVARIANT )
408 : {
409 4 : eScanType = t;
410 4 : ++pLine;
411 4 : ++nCol;
412 : }
413 : }
414 : }
415 :
416 : // Hex/octal number? Read in and convert:
417 43556 : else if(nCol < aLine.getLength() && aLine[nCol] == '&')
418 : {
419 2021 : ++pLine; ++nCol;
420 2021 : sal_Unicode cmp1[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F', 0 };
421 2021 : sal_Unicode cmp2[] = { '0', '1', '2', '3', '4', '5', '6', '7', 0 };
422 2021 : sal_Unicode *cmp = cmp1;
423 2021 : sal_Unicode base = 16;
424 2021 : sal_Unicode ndig = 8;
425 2021 : sal_Unicode xch = aLine[nCol] & 0xFF;
426 2021 : ++pLine; ++nCol;
427 2021 : switch( toupper( xch ) )
428 : {
429 : case 'O':
430 3 : cmp = cmp2; base = 8; ndig = 11; break;
431 : case 'H':
432 14 : break;
433 : default :
434 : // treated as an operator
435 2004 : --pLine; --nCol; nCol1 = nCol-1;
436 2004 : aSym = "&";
437 2004 : return true;
438 : }
439 17 : bNumber = true;
440 : // Hex literals are signed Integers ( as defined by basic
441 : // e.g. -2,147,483,648 through 2,147,483,647 (signed)
442 17 : sal_Int32 l = 0;
443 : int i;
444 17 : bool bBufOverflow = false;
445 119 : while(nCol < aLine.getLength() && theBasicCharClass::get().isAlphaNumeric(aLine[nCol] & 0xFF, bCompatible))
446 : {
447 : sal_Unicode ch = sal::static_int_cast< sal_Unicode >(
448 85 : toupper(aLine[nCol] & 0xFF));
449 85 : ++pLine; ++nCol;
450 : // from 4.1.1996: buffer full, go on scanning empty
451 85 : if( (p-buf) == (BUF_SIZE-1) )
452 0 : bBufOverflow = true;
453 85 : else if( OUString( cmp ).indexOf( ch ) != -1 )
454 83 : *p++ = ch;
455 : else
456 : {
457 2 : aError = OUString(ch);
458 2 : GenError( SbERR_BAD_CHAR_IN_NUMBER );
459 : }
460 : }
461 17 : *p = 0;
462 87 : for( p = buf; *p; ++p )
463 : {
464 71 : i = (*p & 0xFF) - '0';
465 71 : if( i > 9 ) i -= 7;
466 71 : l = ( l * base ) + i;
467 71 : if( !ndig-- )
468 : {
469 1 : GenError( SbERR_MATH_OVERFLOW ); break;
470 : }
471 : }
472 17 : if(nCol < aLine.getLength() && aLine[nCol] == '&') ++pLine, ++nCol;
473 17 : nVal = (double) l;
474 17 : eScanType = ( l >= SbxMININT && l <= SbxMAXINT ) ? SbxINTEGER : SbxLONG;
475 17 : if( bBufOverflow )
476 0 : GenError( SbERR_MATH_OVERFLOW );
477 : }
478 :
479 : // Strings:
480 41535 : else if( *pLine == '"' || *pLine == '[' )
481 : {
482 6535 : sal_Unicode cSep = *pLine;
483 6535 : if( cSep == '[' )
484 0 : bSymbol = true, cSep = ']';
485 6535 : sal_Int32 n = nCol + 1;
486 13494 : while( *pLine )
487 : {
488 94302 : do pLine++, nCol++;
489 94302 : while( *pLine && ( *pLine != cSep ) );
490 6959 : if( *pLine == cSep )
491 : {
492 6959 : pLine++; nCol++;
493 6959 : if( *pLine != cSep || cSep == ']' ) break;
494 0 : } else aError = OUString(cSep), GenError( SbERR_EXPECTED );
495 : }
496 : // If VBA Interop then doen't eat the [] chars
497 6535 : if ( cSep == ']' && bVBASupportOn )
498 0 : aSym = aLine.copy( n - 1, nCol - n + 1);
499 : else
500 6535 : aSym = aLine.copy( n, nCol - n - 1 );
501 : // get out duplicate string delimiters
502 6535 : OUStringBuffer aSymBuf;
503 94302 : for ( sal_Int32 i = 0, len = aSym.getLength(); i < len; ++i )
504 : {
505 87767 : aSymBuf.append( aSym[i] );
506 87767 : if ( aSym[i] == cSep && ( i+1 < len ) && aSym[i+1] == cSep )
507 424 : ++i;
508 : }
509 6535 : aSym = aSymBuf.makeStringAndClear();
510 6535 : if( cSep != ']' )
511 6535 : eScanType = ( cSep == '#' ) ? SbxDATE : SbxSTRING;
512 : }
513 : // invalid characters:
514 35000 : else if( ( *pLine & 0xFF ) >= 0x7F )
515 : {
516 0 : GenError( SbERR_SYNTAX ); pLine++; nCol++;
517 : }
518 : // other groups:
519 : else
520 : {
521 35000 : sal_Int32 n = 1;
522 35000 : switch( *pLine++ )
523 : {
524 326 : case '<': if( *pLine == '>' || *pLine == '=' ) n = 2; break;
525 64 : case '>': if( *pLine == '=' ) n = 2; break;
526 395 : case ':': if( *pLine == '=' ) n = 2; break;
527 : }
528 35000 : aSym = aLine.copy( nCol, n );
529 35000 : pLine += n-1; nCol = nCol + n;
530 : }
531 :
532 90140 : nCol2 = nCol-1;
533 :
534 : PrevLineCommentLbl:
535 :
536 175328 : if( bPrevLineExtentsComment || (eScanType != SbxSTRING &&
537 164866 : ( aSym.startsWith("'") || aSym.equalsIgnoreAsciiCase( "REM" ) ) ) )
538 : {
539 1976 : bPrevLineExtentsComment = false;
540 1976 : aSym = "REM";
541 1976 : sal_Int32 nLen = rtl_ustr_getLength(pLine);
542 1976 : if( bCompatible && pLine[ nLen - 1 ] == '_' && pLine[ nLen - 2 ] == ' ' )
543 0 : bPrevLineExtentsComment = true;
544 1976 : nCol2 = nCol2 + nLen;
545 1976 : pLine = NULL;
546 : }
547 90140 : return true;
548 :
549 :
550 : eoln:
551 16215 : if( nCol && *--pLine == '_' )
552 : {
553 52 : pLine = NULL;
554 52 : bool bRes = NextSym();
555 52 : if( bVBASupportOn && aSym[0] == '.' )
556 : {
557 : // object _
558 : // .Method
559 : // ^^^ <- spaces is legal in MSO VBA
560 : OSL_TRACE("*** resetting bSpaces***");
561 0 : bSpaces = false;
562 : }
563 52 : return bRes;
564 : }
565 : else
566 : {
567 16163 : pLine = NULL;
568 16163 : nLine = nOldLine;
569 16163 : nCol1 = nOldCol1;
570 16163 : nCol2 = nOldCol2;
571 16163 : aSym = "\n";
572 16163 : nColLock = 0;
573 16163 : return true;
574 : }
575 : }
576 :
577 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|