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