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 <tools/errcode.hxx>
21 : #include <vcl/svapp.hxx>
22 : #include <basic/sbx.hxx>
23 :
24 :
25 : class SbxSimpleCharClass
26 : {
27 : public:
28 0 : bool isAlpha( sal_Unicode c ) const
29 : {
30 0 : bool bRet = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
31 0 : return bRet;
32 : }
33 :
34 0 : bool isDigit( sal_Unicode c ) const
35 : {
36 0 : bool bRet = (c >= '0' && c <= '9');
37 0 : return bRet;
38 : }
39 :
40 0 : bool isAlphaNumeric( sal_Unicode c ) const
41 : {
42 0 : bool bRet = isDigit( c ) || isAlpha( c );
43 0 : return bRet;
44 : }
45 : };
46 :
47 :
48 : static SbxVariable* Element
49 : ( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf,
50 : SbxClassType, const SbxSimpleCharClass& rCharClass );
51 :
52 0 : static const sal_Unicode* SkipWhitespace( const sal_Unicode* p )
53 : {
54 0 : while( *p && ( *p == ' ' || *p == '\t' ) )
55 0 : p++;
56 0 : return p;
57 : }
58 :
59 : // Scanning of a symbol. The symbol were inserted in rSym, the return value
60 : // is the new scan position. The symbol is at errors empty.
61 :
62 0 : static const sal_Unicode* Symbol( const sal_Unicode* p, OUString& rSym, const SbxSimpleCharClass& rCharClass )
63 : {
64 0 : sal_uInt16 nLen = 0;
65 : // Did we have a nonstandard symbol?
66 0 : if( *p == '[' )
67 : {
68 0 : rSym = ++p;
69 0 : while( *p && *p != ']' )
70 : {
71 0 : p++, nLen++;
72 : }
73 0 : p++;
74 : }
75 : else
76 : {
77 : // A symbol had to begin with a alphabetic character or an underline
78 0 : if( !rCharClass.isAlpha( *p ) && *p != '_' )
79 : {
80 0 : SbxBase::SetError( SbxERR_SYNTAX );
81 : }
82 : else
83 : {
84 0 : rSym = p;
85 : // The it can contain alphabetic characters, numbers or underlines
86 0 : while( *p && (rCharClass.isAlphaNumeric( *p ) || *p == '_') )
87 : {
88 0 : p++, nLen++;
89 : }
90 : // BASIC-Standard-Suffixes were ignored
91 0 : if( *p && (*p == '%' || *p == '&' || *p == '!' || *p == '#' || *p == '$' ) )
92 : {
93 0 : p++;
94 : }
95 : }
96 : }
97 0 : rSym = rSym.copy( 0, nLen );
98 0 : return p;
99 : }
100 :
101 : // Qualified name. Element.Element....
102 :
103 0 : static SbxVariable* QualifiedName
104 : ( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf, SbxClassType t )
105 : {
106 : static SbxSimpleCharClass aCharClass;
107 :
108 0 : SbxVariableRef refVar;
109 0 : const sal_Unicode* p = SkipWhitespace( *ppBuf );
110 0 : if( aCharClass.isAlpha( *p ) || *p == '_' || *p == '[' )
111 : {
112 : // Read in the element
113 0 : refVar = Element( pObj, pGbl, &p, t, aCharClass );
114 0 : while( refVar.Is() && (*p == '.' || *p == '!') )
115 : {
116 : // It follows still an objectelement. The current element
117 : // had to be a SBX-Object or had to deliver such an object!
118 0 : pObj = PTR_CAST(SbxObject,(SbxVariable*) refVar);
119 0 : if( !pObj )
120 : // Then it had to deliver an object
121 0 : pObj = PTR_CAST(SbxObject,refVar->GetObject());
122 0 : refVar.Clear();
123 0 : if( !pObj )
124 0 : break;
125 0 : p++;
126 : // And the next element please
127 0 : refVar = Element( pObj, pGbl, &p, t, aCharClass );
128 : }
129 : }
130 : else
131 0 : SbxBase::SetError( SbxERR_SYNTAX );
132 0 : *ppBuf = p;
133 0 : if( refVar.Is() )
134 0 : refVar->AddRef();
135 0 : return refVar;
136 : }
137 :
138 : // Read in of an operand. This could be a number, a string or
139 : // a function (with optional parameters).
140 :
141 0 : static SbxVariable* Operand
142 : ( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf, bool bVar )
143 : {
144 : static SbxSimpleCharClass aCharClass;
145 :
146 0 : SbxVariableRef refVar( new SbxVariable );
147 0 : const sal_Unicode* p = SkipWhitespace( *ppBuf );
148 0 : if( !bVar && ( aCharClass.isDigit( *p )
149 0 : || ( *p == '.' && aCharClass.isDigit( *( p+1 ) ) )
150 : || *p == '-'
151 : || *p == '&' ) )
152 : {
153 : // A number could be scanned in directly!
154 : sal_uInt16 nLen;
155 0 : if( !refVar->Scan( OUString( p ), &nLen ) )
156 : {
157 0 : refVar.Clear();
158 : }
159 : else
160 : {
161 0 : p += nLen;
162 : }
163 : }
164 0 : else if( !bVar && *p == '"' )
165 : {
166 : // A string
167 0 : OUString aString;
168 0 : p++;
169 0 : for( ;; )
170 : {
171 : // This is perhaps an error
172 0 : if( !*p )
173 : {
174 0 : return NULL;
175 : }
176 : // Double quotes are OK
177 0 : if( *p == '"' )
178 : {
179 0 : if( *++p != '"' )
180 : {
181 0 : break;
182 : }
183 : }
184 0 : aString += OUString(*p++);
185 : }
186 0 : refVar->PutString( aString );
187 : }
188 : else
189 : {
190 0 : refVar = QualifiedName( pObj, pGbl, &p, SbxCLASS_DONTCARE );
191 : }
192 0 : *ppBuf = p;
193 0 : if( refVar.Is() )
194 0 : refVar->AddRef();
195 0 : return refVar;
196 : }
197 :
198 : // Read in of a simple term. The operands +, -, * and /
199 : // are supported.
200 :
201 0 : static SbxVariable* MulDiv( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf )
202 : {
203 0 : const sal_Unicode* p = *ppBuf;
204 0 : SbxVariableRef refVar( Operand( pObj, pGbl, &p, false ) );
205 0 : p = SkipWhitespace( p );
206 0 : while( refVar.Is() && ( *p == '*' || *p == '/' ) )
207 : {
208 0 : sal_Unicode cOp = *p++;
209 0 : SbxVariableRef refVar2( Operand( pObj, pGbl, &p, false ) );
210 0 : if( refVar2.Is() )
211 : {
212 : // temporary variable!
213 0 : SbxVariable* pVar = refVar;
214 0 : pVar = new SbxVariable( *pVar );
215 0 : refVar = pVar;
216 0 : if( cOp == '*' )
217 0 : *refVar *= *refVar2;
218 : else
219 0 : *refVar /= *refVar2;
220 : }
221 : else
222 : {
223 0 : refVar.Clear();
224 : break;
225 : }
226 0 : }
227 0 : *ppBuf = p;
228 0 : if( refVar.Is() )
229 0 : refVar->AddRef();
230 0 : return refVar;
231 : }
232 :
233 0 : static SbxVariable* PlusMinus( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf )
234 : {
235 0 : const sal_Unicode* p = *ppBuf;
236 0 : SbxVariableRef refVar( MulDiv( pObj, pGbl, &p ) );
237 0 : p = SkipWhitespace( p );
238 0 : while( refVar.Is() && ( *p == '+' || *p == '-' ) )
239 : {
240 0 : sal_Unicode cOp = *p++;
241 0 : SbxVariableRef refVar2( MulDiv( pObj, pGbl, &p ) );
242 0 : if( refVar2.Is() )
243 : {
244 : // temporaere Variable!
245 0 : SbxVariable* pVar = refVar;
246 0 : pVar = new SbxVariable( *pVar );
247 0 : refVar = pVar;
248 0 : if( cOp == '+' )
249 0 : *refVar += *refVar2;
250 : else
251 0 : *refVar -= *refVar2;
252 : }
253 : else
254 : {
255 0 : refVar.Clear();
256 : break;
257 : }
258 0 : }
259 0 : *ppBuf = p;
260 0 : if( refVar.Is() )
261 0 : refVar->AddRef();
262 0 : return refVar;
263 : }
264 :
265 0 : static SbxVariable* Assign( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf )
266 : {
267 0 : const sal_Unicode* p = *ppBuf;
268 0 : SbxVariableRef refVar( Operand( pObj, pGbl, &p, true ) );
269 0 : p = SkipWhitespace( p );
270 0 : if( refVar.Is() )
271 : {
272 0 : if( *p == '=' )
273 : {
274 : // Assign only onto properties!
275 0 : if( refVar->GetClass() != SbxCLASS_PROPERTY )
276 : {
277 0 : SbxBase::SetError( SbxERR_BAD_ACTION );
278 0 : refVar.Clear();
279 : }
280 : else
281 : {
282 0 : p++;
283 0 : SbxVariableRef refVar2( PlusMinus( pObj, pGbl, &p ) );
284 0 : if( refVar2.Is() )
285 : {
286 0 : SbxVariable* pVar = refVar;
287 0 : SbxVariable* pVar2 = refVar2;
288 0 : *pVar = *pVar2;
289 0 : pVar->SetParameters( NULL );
290 0 : }
291 : }
292 : }
293 : else
294 : // Simple call: once activating
295 0 : refVar->Broadcast( SBX_HINT_DATAWANTED );
296 : }
297 0 : *ppBuf = p;
298 0 : if( refVar.Is() )
299 0 : refVar->AddRef();
300 0 : return refVar;
301 : }
302 :
303 : // Read in of an element. This is a symbol, optional followed
304 : // by a parameter list. The symbol will be searched in the
305 : // specified object and the parameter list will be attached if necessary.
306 :
307 0 : static SbxVariable* Element
308 : ( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf,
309 : SbxClassType t, const SbxSimpleCharClass& rCharClass )
310 : {
311 0 : OUString aSym;
312 0 : const sal_Unicode* p = Symbol( *ppBuf, aSym, rCharClass );
313 0 : SbxVariableRef refVar;
314 0 : if( !aSym.isEmpty() )
315 : {
316 0 : sal_uInt16 nOld = pObj->GetFlags();
317 0 : if( pObj == pGbl )
318 : {
319 0 : pObj->SetFlag( SBX_GBLSEARCH );
320 : }
321 0 : refVar = pObj->Find( aSym, t );
322 0 : pObj->SetFlags( nOld );
323 0 : if( refVar.Is() )
324 : {
325 0 : refVar->SetParameters( NULL );
326 : // Follow still parameter?
327 0 : p = SkipWhitespace( p );
328 0 : if( *p == '(' )
329 : {
330 0 : p++;
331 0 : SbxArrayRef refPar = new SbxArray;
332 0 : sal_uInt16 nArg = 0;
333 : // We are once relaxed and accept as well
334 : // the line- or commandend as delimiter
335 : // Search parameter always global!
336 0 : while( *p && *p != ')' && *p != ']' )
337 : {
338 0 : SbxVariableRef refArg = PlusMinus( pGbl, pGbl, &p );
339 0 : if( !refArg )
340 : {
341 : // Error during the parsing
342 0 : refVar.Clear(); break;
343 : }
344 : else
345 : {
346 : // One copies the parameter, so that
347 : // one have the current status (triggers also
348 : // the call per access)
349 0 : SbxVariable* pArg = refArg;
350 0 : refPar->Put( new SbxVariable( *pArg ), ++nArg );
351 : }
352 0 : p = SkipWhitespace( p );
353 0 : if( *p == ',' )
354 0 : p++;
355 0 : }
356 0 : if( *p == ')' )
357 0 : p++;
358 0 : if( refVar.Is() )
359 0 : refVar->SetParameters( refPar );
360 : }
361 : }
362 : else
363 0 : SbxBase::SetError( SbxERR_NO_METHOD );
364 : }
365 0 : *ppBuf = p;
366 0 : if( refVar.Is() )
367 0 : refVar->AddRef();
368 0 : return refVar;
369 : }
370 :
371 : // Mainroutine
372 :
373 0 : SbxVariable* SbxObject::Execute( const OUString& rTxt )
374 : {
375 0 : SbxVariable* pVar = NULL;
376 0 : const sal_Unicode* p = rTxt.getStr();
377 0 : for( ;; )
378 : {
379 0 : p = SkipWhitespace( p );
380 0 : if( !*p )
381 : {
382 0 : break;
383 : }
384 0 : if( *p++ != '[' )
385 : {
386 0 : SetError( SbxERR_SYNTAX ); break;
387 : }
388 0 : pVar = Assign( this, this, &p );
389 0 : if( !pVar )
390 : {
391 0 : break;
392 : }
393 0 : p = SkipWhitespace( p );
394 0 : if( *p++ != ']' )
395 : {
396 0 : SetError( SbxERR_SYNTAX ); break;
397 : }
398 : }
399 0 : return pVar;
400 : }
401 :
402 0 : SbxVariable* SbxObject::FindQualified( const OUString& rName, SbxClassType t )
403 : {
404 0 : SbxVariable* pVar = NULL;
405 0 : const sal_Unicode* p = rName.getStr();
406 0 : p = SkipWhitespace( p );
407 0 : if( !*p )
408 : {
409 0 : return NULL;;
410 : }
411 0 : pVar = QualifiedName( this, this, &p, t );
412 0 : p = SkipWhitespace( p );
413 0 : if( *p )
414 : {
415 0 : SetError( SbxERR_SYNTAX );
416 : }
417 0 : return pVar;
418 : }
419 :
420 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|