bump product version to 4.1.6.2
[LibreOffice.git] / basic / source / comp / scanner.cxx
blobb59e22ac263cbb74fa107bdd37e33aef728bd174
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
21 #include "sbcomp.hxx"
23 #include <vcl/svapp.hxx>
25 SbiScanner::SbiScanner( const OUString& rBuf, StarBASIC* p ) : aBuf( rBuf )
27 pBasic = p;
28 pLine = NULL;
29 nVal = 0;
30 eScanType = SbxVARIANT;
31 nErrors = 0;
32 nBufPos = 0;
33 nCurCol1 = 0;
34 nSavedCol1 = 0;
35 nColLock = 0;
36 nLine = 0;
37 nCol1 = 0;
38 nCol2 = 0;
39 nCol = 0;
40 bError =
41 bAbort =
42 bSpaces =
43 bNumber =
44 bSymbol =
45 bCompatible =
46 bVBASupportOn =
47 bInStatement =
48 bPrevLineExtentsComment = false;
49 bHash = true;
52 SbiScanner::~SbiScanner()
55 void SbiScanner::LockColumn()
57 if( !nColLock++ )
58 nSavedCol1 = nCol1;
61 void SbiScanner::UnlockColumn()
63 if( nColLock )
64 nColLock--;
67 void SbiScanner::GenError( SbError code )
69 if( GetSbData()->bBlockCompilerError )
71 bAbort = true;
72 return;
74 if( !bError )
76 bool bRes = true;
77 // report only one error per statement
78 bError = true;
79 if( pBasic )
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;
84 switch( code )
86 case SbERR_EXPECTED:
87 case SbERR_UNEXPECTED:
88 case SbERR_SYMBOL_EXPECTED:
89 case SbERR_LABEL_EXPECTED:
90 nc = nCol1;
91 if( nc > nCol2 ) nCol2 = nc;
92 break;
94 bRes = pBasic->CError( code, aError, nLine, nc, nCol2 );
96 bAbort = bAbort || !bRes || ( code == SbERR_NO_MEMORY || code == SbERR_PROG_TOO_LARGE );
98 nErrors++;
102 // used by SbiTokenizer::MayBeLabel() to detect a label
103 bool SbiScanner::DoesColonFollow()
105 if(nCol < aLine.getLength() && aLine[nCol] == ':')
107 ++pLine; ++nCol;
108 return true;
110 else
111 return false;
114 // test for legal suffix
115 static SbxDataType GetSuffixType( sal_Unicode c )
117 switch (c)
119 case '%':
120 return SbxDataType(SbxINTEGER);
121 case '&':
122 return SbxDataType(SbxLONG);
123 case '!':
124 return SbxDataType(SbxSINGLE);
125 case '#':
126 return SbxDataType(SbxDOUBLE);
127 case '@':
128 return SbxDataType(SbxCURRENCY);
129 case '$':
130 return SbxDataType(SbxSTRING);
131 default:
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
138 #define BUF_SIZE 80
140 void SbiScanner::scanAlphanumeric()
142 sal_Int32 n = nCol;
143 while(nCol < aLine.getLength() && (theBasicCharClass::get().isAlphaNumeric(aLine[nCol], bCompatible) || aLine[nCol] == '_'))
145 ++pLine;
146 ++nCol;
148 aSym = aLine.copy(n, nCol - n);
151 void SbiScanner::scanGoto()
153 sal_Int32 n = nCol;
154 while(n < aLine.getLength() && theBasicCharClass::get().isWhitespace(aLine[n]))
155 ++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;
164 nCol = n + 2;
169 bool SbiScanner::readLine()
171 if(nBufPos >= aBuf.getLength())
172 return false;
174 sal_Int32 n = nBufPos;
175 sal_Int32 nLen = aBuf.getLength();
177 while(n < nLen && aBuf[n] != '\r' && aBuf[n] != '\n')
178 ++n;
180 // Trim trailing whitespace
181 sal_Int32 nEnd = n;
182 while(nBufPos < nEnd && theBasicCharClass::get().isWhitespace(aBuf[nEnd - 1]))
183 --nEnd;
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')
189 n += 2;
190 else if(n < nLen)
191 ++n;
193 nBufPos = n;
194 pLine = aLine.getStr();
196 ++nLine;
197 nCol = nCol1 = nCol2 = 0;
198 nColLock = 0;
200 return true;
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;
212 aSym = OUString();
213 bHash = bSymbol = bNumber = bSpaces = false;
215 // read in line?
216 if( !pLine )
218 if(!readLine())
219 return false;
221 nOldLine = nLine;
222 nOldCol1 = nOldCol2 = 0;
225 if(nCol < aLine.getLength() && theBasicCharClass::get().isWhitespace(aLine[nCol]))
227 bSpaces = true;
228 while(nCol < aLine.getLength() && theBasicCharClass::get().isWhitespace(aLine[nCol]))
229 ++pLine, ++nCol;
232 nCol1 = nCol;
234 // only blank line?
235 if(nCol >= aLine.getLength())
236 goto eoln;
238 if( bPrevLineExtentsComment )
239 goto PrevLineCommentLbl;
241 if(nCol < aLine.getLength() && aLine[nCol] == '#')
243 ++pLine;
244 ++nCol;
245 bHash = true;
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...
255 ++pLine;
256 goto eoln;
259 bSymbol = true;
261 scanAlphanumeric();
263 // Special handling for "go to"
264 if(nCol < aLine.getLength() && bCompatible && aSym.equalsIgnoreAsciiCase("go"))
265 scanGoto();
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() );
274 aSym = aSymCopy;
276 // HACK: modifying a potentially shared string here!
277 *((sal_Unicode*)(pLine-1)) = ' ';
280 // type recognition?
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 )
291 eScanType = t;
292 ++pLine;
293 ++nCol;
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)))
303 short exp = 0;
304 short comma = 0;
305 short ndig = 0;
306 short ncdig = 0;
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) )
314 bBufOverflow = true;
315 ++pLine, ++nCol;
316 continue;
318 // point or exponent?
319 if(aLine[nCol] == '.')
321 if( ++comma > 1 )
323 ++pLine; ++nCol; continue;
325 else
327 *p = '.';
328 ++p, ++pLine, ++nCol;
331 else if(strchr("DdEe", aLine[nCol]))
333 if (++exp > 1)
335 ++pLine; ++nCol; continue;
338 *p = 'E';
339 ++p, ++pLine, ++nCol;
341 if(aLine[nCol] == '+')
342 ++pLine, ++nCol;
343 else if(aLine[nCol] == '-')
345 *p = '-';
346 ++p, ++pLine, ++nCol;
349 else
351 *p = aLine[nCol];
352 ++p, ++pLine, ++nCol;
353 if( comma && !exp ) ++ncdig;
355 if (!exp) ++ndig;
357 *p = 0;
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 );
370 ndig = ndig - comma;
371 if( !comma && !exp )
373 if( nVal >= SbxMININT && nVal <= SbxMAXINT )
374 eScanType = SbxINTEGER;
375 else
376 if( nVal >= SbxMINLNG && nVal <= SbxMAXLNG )
377 eScanType = SbxLONG;
379 if( bBufOverflow )
380 GenError( SbERR_MATH_OVERFLOW );
382 // type recognition?
383 SbxDataType t(GetSuffixType(aLine[nCol]));
384 if( t != SbxVARIANT )
386 eScanType = t;
387 ++pLine;
388 ++nCol;
392 // Hex/octal number? Read in and convert:
393 else if(nCol < aLine.getLength() && aLine[nCol] == '&')
395 ++pLine; ++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;
402 ++pLine; ++nCol;
403 switch( toupper( xch ) )
405 case 'O':
406 cmp = cmp2; base = 8; ndig = 11; break;
407 case 'H':
408 break;
409 default :
410 // treated as an operator
411 --pLine; --nCol; nCol1 = nCol-1;
412 aSym = OUString("&");
413 return true;
415 bNumber = true;
416 // Hex literals are signed Integers ( as defined by basic
417 // e.g. -2,147,483,648 through 2,147,483,647 (signed)
418 sal_Int32 l = 0;
419 int i;
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));
425 ++pLine; ++nCol;
426 // from 4.1.1996: buffer full, go on scanning empty
427 if( (p-buf) == (BUF_SIZE-1) )
428 bBufOverflow = true;
429 else if( OUString( cmp ).indexOf( ch ) != -1 )
430 *p++ = ch;
431 else
433 aError = OUString(ch);
434 GenError( SbERR_BAD_CHAR_IN_NUMBER );
437 *p = 0;
438 for( p = buf; *p; ++p )
440 i = (*p & 0xFF) - '0';
441 if( i > 9 ) i -= 7;
442 l = ( l * base ) + i;
443 if( !ndig-- )
445 GenError( SbERR_MATH_OVERFLOW ); break;
448 if(nCol < aLine.getLength() && aLine[nCol] == '&') ++pLine, ++nCol;
449 nVal = (double) l;
450 eScanType = ( l >= SbxMININT && l <= SbxMAXINT ) ? SbxINTEGER : SbxLONG;
451 if( bBufOverflow )
452 GenError( SbERR_MATH_OVERFLOW );
455 // Strings:
456 else if( *pLine == '"' || *pLine == '[' )
458 sal_Unicode cSep = *pLine;
459 if( cSep == '[' )
460 bSymbol = true, cSep = ']';
461 sal_Int32 n = nCol + 1;
462 while( *pLine )
464 do pLine++, nCol++;
465 while( *pLine && ( *pLine != cSep ) );
466 if( *pLine == cSep )
468 pLine++; nCol++;
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);
475 else
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 )
483 ++i;
485 aSym = aSymBuf.makeStringAndClear();
486 if( cSep != ']' )
487 eScanType = ( cSep == '#' ) ? SbxDATE : SbxSTRING;
489 // invalid characters:
490 else if( ( *pLine & 0xFF ) >= 0x7F )
492 GenError( SbERR_SYNTAX ); pLine++; nCol++;
494 // other groups:
495 else
497 sal_Int32 n = 1;
498 switch( *pLine++ )
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;
508 nCol2 = nCol-1;
510 PrevLineCommentLbl:
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;
521 pLine = NULL;
523 return true;
526 eoln:
527 if( nCol && *--pLine == '_' )
529 pLine = NULL;
530 bool bRes = NextSym();
531 if( bVBASupportOn && aSym[0] == '.' )
533 // object _
534 // .Method
535 // ^^^ <- spaces is legal in MSO VBA
536 OSL_TRACE("*** resetting bSpaces***");
537 bSpaces = false;
539 return bRes;
541 else
543 pLine = NULL;
544 nLine = nOldLine;
545 nCol1 = nOldCol1;
546 nCol2 = nOldCol2;
547 aSym = OUString("\n");
548 nColLock = 0;
549 return true;
553 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */