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