summaryrefslogtreecommitdiff
path: root/idl/source/prj/parser.cxx
blob: cb2cd8b83e1f061ab469ef32bb0fa8bc0f19f880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <sal/config.h>

#include <algorithm>

#include <parser.hxx>
#include <database.hxx>
#include <globals.hxx>
#include <osl/file.hxx>

void SvIdlParser::ReadSvIdl( const OUString & rPath )
{
    rBase.SetPath(rPath); // only valid for this iteration
    SvToken& rTok = rInStm.GetToken();

    while( true )
    {
        rTok = rInStm.GetToken();
        if( rTok.IsEof() )
            return;

        Read( SvHash_module() );
        tools::SvRef<SvMetaModule> aModule = new SvMetaModule;
        ReadModuleHeader(*aModule);
        rBase.GetModuleList().push_back( aModule );
    }
}

void SvIdlParser::ReadModuleHeader(SvMetaModule& rModule)
{
    OString aName = ReadIdentifier();
    rModule.SetName( aName );
    rBase.Push( &rModule ); // onto the context stack
    ReadModuleBody(rModule);
    rBase.GetStack().pop_back(); // remove from stack
}

void SvIdlParser::ReadModuleBody(SvMetaModule& rModule)
{
    if( ReadIf( '[' ) )
    {
        while( true )
        {
            OString aSlotIdFile;
            if( !ReadStringSvIdl( SvHash_SlotIdFile(), rInStm, aSlotIdFile ) )
                break;
            if( !rBase.ReadIdFile( aSlotIdFile ) )
            {
                throw SvParseException( rInStm, "cannot read file: " + aSlotIdFile );
            }
            ReadIfDelimiter();
        }
        Read( ']' );
    }

    if( !ReadIf( '{' ) )
        return;

    sal_uInt32 nBeginPos = 0;
    while( nBeginPos != rInStm.Tell() )
    {
        nBeginPos = rInStm.Tell();
        ReadModuleElement( rModule );
        ReadIfDelimiter();
    }
    Read( '}' );
}

void SvIdlParser::ReadModuleElement( SvMetaModule& rModule )
{
    if( ReadIf( SvHash_interface() ) )
    {
        ReadInterfaceOrShell(rModule, MetaTypeType::Interface);
    }
    else if( ReadIf( SvHash_shell() ) )
    {
        ReadInterfaceOrShell(rModule, MetaTypeType::Shell);
    }
    else if( ReadIf( SvHash_enum() ) )
    {
        ReadEnum();
    }
    else if( ReadIf( SvHash_item() ) )
    {
        ReadItem();
    }
    else if( ReadIf( SvHash_struct() ) )
    {
        ReadStruct();
    }
    else if( ReadIf( SvHash_include() ) )
    {
        ReadInclude(rModule);
    }
    else
    {
        tools::SvRef<SvMetaSlot> xSlot( new SvMetaSlot() );

        if( xSlot->ReadSvIdl( rBase, rInStm ) )
        {
            if( xSlot->Test( rInStm ) )
            {
                // announce globally
                rBase.AppendSlot( xSlot );
            }
        }
    }
}

void SvIdlParser::ReadInclude( SvMetaModule& rModule )
{
    sal_uInt32  nTokPos = rInStm.Tell();
    bool bOk = false;
    OUString aFullName(OStringToOUString(ReadString(), RTL_TEXTENCODING_ASCII_US));
    rBase.StartNewFile( aFullName );
    osl::FileBase::RC searchError = osl::File::searchFileURL(aFullName, rBase.GetPath(), aFullName);
    if( osl::FileBase::E_None != searchError )
    {
        OStringBuffer aStr("cannot find file:");
        aStr.append(OUStringToOString(aFullName, RTL_TEXTENCODING_UTF8));
        throw SvParseException(aStr.makeStringAndClear(), rInStm.GetToken());
    }
    osl::FileBase::getSystemPathFromFileURL( aFullName, aFullName );
    rBase.AddDepFile( aFullName );
    SvTokenStream aTokStm( aFullName );
    if( SVSTREAM_OK != aTokStm.GetStream().GetError() )
    {
        OStringBuffer aStr("cannot open file: ");
        aStr.append(OUStringToOString(aFullName, RTL_TEXTENCODING_UTF8));
        throw SvParseException(aStr.makeStringAndClear(), rInStm.GetToken());
    }
    // rescue error from old file
    SvIdlError aOldErr = rBase.GetError();
    // reset error
    rBase.SetError( SvIdlError() );

    try {
        SvIdlParser aIncludeParser( rBase, aTokStm );
        sal_uInt32 nBeginPos = 0xFFFFFFFF; // can not happen with Tell
        while( nBeginPos != aTokStm.Tell() )
        {
            nBeginPos = aTokStm.Tell();
            aIncludeParser.ReadModuleElement(rModule);
            aTokStm.ReadIfDelimiter();
        }
    } catch (const SvParseException& ex) {
        rBase.SetError(ex.aError);
        rBase.WriteError(aTokStm);
    }
    bOk = aTokStm.GetToken().IsEof();
    if( !bOk )
    {
        rBase.WriteError( aTokStm );
    }
    // recover error from old file
    rBase.SetError( aOldErr );
    if( !bOk )
        rInStm.Seek( nTokPos );
}

void SvIdlParser::ReadStruct()
{
    tools::SvRef<SvMetaType> xStruct(new SvMetaType() );
    xStruct->SetType( MetaTypeType::Struct );
    xStruct->SetName( ReadIdentifier() );
    Read( '{' );
    sal_uInt32 nBeginPos = 0; // can not happen with Tell
    while( nBeginPos != rInStm.Tell() )
    {
        nBeginPos = rInStm.Tell();
        tools::SvRef<SvMetaAttribute> xAttr( new SvMetaAttribute() );
        xAttr->aType = ReadKnownType();
        xAttr->SetName(ReadIdentifier());
        xAttr->aSlotId.setString(ReadIdentifier());
        sal_uLong n;
        if( !rBase.FindId( xAttr->aSlotId.getString(), &n ) )
            throw SvParseException( rInStm, "no value for identifier <" + xAttr->aSlotId.getString() + "> " );
        xAttr->aSlotId.SetValue(n);
        xStruct->GetAttrList().push_back( xAttr );
        if( !ReadIfDelimiter() )
            break;
        if( rInStm.GetToken().IsChar() && rInStm.GetToken().GetChar() == '}')
            break;
    }
    Read( '}' );
    ReadDelimiter();
    // announce globally
    rBase.GetTypeList().push_back( xStruct );
}

void SvIdlParser::ReadItem()
{
    tools::SvRef<SvMetaType> xItem(new SvMetaType() );
    xItem->SetItem(true);
    xItem->SetRef( ReadKnownType() );
    xItem->SetName( ReadIdentifier() );
    // announce globally
    rBase.GetTypeList().push_back( xItem );
}

void SvIdlParser::ReadEnum()
{
    tools::SvRef<SvMetaTypeEnum> xEnum( new SvMetaTypeEnum() );
    xEnum->SetType( MetaTypeType::Enum );
    xEnum->SetName( ReadIdentifier() );

    Read('{');
    while( true )
    {
        ReadEnumValue( *xEnum );
        if( !ReadIfDelimiter() )
            break;
    }
    Read( '}' );
    // announce globally
    rBase.GetTypeList().push_back( xEnum );
}

static OString getCommonSubPrefix(const OString &rA, const OString &rB)
{
    sal_Int32 nMax = std::min(rA.getLength(), rB.getLength());
    sal_Int32 nI = 0;
    while (nI < nMax)
    {
        if (rA[nI] != rB[nI])
            break;
        ++nI;
    }
    return rA.copy(0, nI);
}

void SvIdlParser::ReadEnumValue( SvMetaTypeEnum& rEnum )
{
    tools::SvRef<SvMetaEnumValue> aEnumVal = new SvMetaEnumValue();
    aEnumVal->SetName( ReadIdentifier() );
    if( rEnum.aEnumValueList.empty() )
    {
       // the first
       rEnum.aPrefix = aEnumVal->GetName();
    }
    else
    {
        rEnum.aPrefix = getCommonSubPrefix(rEnum.aPrefix, aEnumVal->GetName());
    }
    rEnum.aEnumValueList.push_back( aEnumVal );
}

void SvIdlParser::ReadInterfaceOrShell( SvMetaModule& rModule, MetaTypeType aMetaTypeType )
{
    tools::SvRef<SvMetaClass> aClass( new SvMetaClass() );

    aClass->SetType( aMetaTypeType );

    aClass->SetName( ReadIdentifier() );

    if( ReadIf( ':' ) )
    {
        aClass->aSuperClass = ReadKnownClass();
    }
    if( ReadIf( '{' ) )
    {
        sal_uInt32 nBeginPos = 0; // can not happen with Tell
        while( nBeginPos != rInStm.Tell() )
        {
            nBeginPos = rInStm.Tell();
            ReadInterfaceOrShellEntry(*aClass);
            ReadIfDelimiter();
        }
        Read( '}' );
    }
    rModule.aClassList.push_back( aClass );
    // announce globally
    rBase.GetClassList().push_back( aClass );
}

void SvIdlParser::ReadInterfaceOrShellEntry(SvMetaClass& rClass)
{
    sal_uInt32  nTokPos = rInStm.Tell();
    SvToken&    rTok = rInStm.GetToken_Next();

    if( rTok.Is( SvHash_import() ) )
    {
        SvMetaClass * pClass = ReadKnownClass();
        SvClassElement aEle(pClass);
        rTok = rInStm.GetToken();
        if( rTok.IsString() )
        {
            aEle.SetPrefix( rTok.GetString() );
            rInStm.GetToken_Next();
        }
        rClass.aClassElementList.push_back( aEle );
        return;
    }
    else
    {
        rInStm.Seek( nTokPos );
        SvMetaType * pType = rBase.ReadKnownType( rInStm );
        tools::SvRef<SvMetaAttribute> xAttr;
        bool bOk = false;
        if( !pType || pType->IsItem() )
        {
            xAttr = new SvMetaSlot( pType );
            bOk = ReadInterfaceOrShellSlot(static_cast<SvMetaSlot&>(*xAttr));
        }
        else
        {
            xAttr = new SvMetaAttribute( pType );
            ReadInterfaceOrShellMethodOrAttribute(*xAttr);
            bOk = true;
        }
        if( bOk )
            bOk = xAttr->Test( rInStm );
        if( bOk )
            bOk = rClass.TestAttribute( rBase, rInStm, *xAttr );
        if( bOk )
        {
            if( !xAttr->GetSlotId().IsSet() )
                xAttr->SetSlotId( SvIdentifier(rBase.GetUniqueId()) );
            rClass.aAttrList.push_back( xAttr );
            return;
        }
    }
    rInStm.Seek( nTokPos );
}

bool SvIdlParser::ReadInterfaceOrShellSlot(SvMetaSlot& rSlot)
{
    sal_uInt32  nTokPos = rInStm.Tell();
    bool        bOk     = true;

    SvMetaAttribute * pAttr = rBase.ReadKnownAttr( rInStm, rSlot.GetType() );
    if( pAttr )
    {
        SvMetaSlot * pKnownSlot = dynamic_cast<SvMetaSlot*>( pAttr );
        if( !pKnownSlot )
            throw SvParseException( rInStm, "attribute " + pAttr->GetName() + " is method or variable but not a slot" );
        rSlot.SetRef( pKnownSlot );
        rSlot.SetName( pKnownSlot->GetName() );
        if( ReadIf( '[' ) )
        {
            sal_uInt32 nBeginPos = 0; // can not happen with Tell
            while( nBeginPos != rInStm.Tell() )
            {
                nBeginPos = rInStm.Tell();
                rSlot.ReadAttributesSvIdl( rBase, rInStm );
                ReadIfDelimiter();
            }
            bOk = ReadIf( ']' );
        }
    }
    else
    {
        bOk = rSlot.SvMetaAttribute::ReadSvIdl( rBase, rInStm );
        SvMetaAttribute *pAttr2 = rBase.FindKnownAttr( rSlot.GetSlotId() );
        if( pAttr2 )
        {
            SvMetaSlot * pKnownSlot = dynamic_cast<SvMetaSlot*>( pAttr2 );
            if( !pKnownSlot )
                throw SvParseException( rInStm, "attribute " + pAttr2->GetName() + " is method or variable but not a slot" );
            rSlot.SetRef( pKnownSlot );
            // names may differ, because explicitly given
            if ( pKnownSlot->GetName() != rSlot.GetName() )
                throw SvParseException( rInStm, "Illegal definition!" );
        }
    }

    if( !bOk )
        rInStm.Seek( nTokPos );

    return bOk;
}

void SvIdlParser::ReadInterfaceOrShellMethodOrAttribute( SvMetaAttribute& rAttr )
{
    rAttr.SetName( ReadIdentifier() );
    rAttr.aSlotId.setString( ReadIdentifier() );
    sal_uLong n;
    if( !rBase.FindId( rAttr.aSlotId.getString(), &n ) )
        throw SvParseException( rInStm, "no value for identifier <" + rAttr.aSlotId.getString() + "> " );
    rAttr.aSlotId.SetValue(n);

    Read( '(' );
    // read method arguments
    tools::SvRef<SvMetaType> xT(new SvMetaType() );
    xT->SetRef(rAttr.GetType() );
    rAttr.aType = xT;
    sal_uInt32 nBeginPos = 0; // can not happen with Tell
    while( nBeginPos != rInStm.Tell() )
    {
        nBeginPos = rInStm.Tell();
        tools::SvRef<SvMetaAttribute> xAttr( new SvMetaAttribute() );
        if( xAttr->ReadSvIdl( rBase, rInStm ) )
        {
            if( xAttr->Test( rInStm ) )
                rAttr.aType->GetAttrList().push_back( xAttr );
        }
        ReadIfDelimiter();
    }
    Read( ')' );
    rAttr.aType->SetType( MetaTypeType::Method );
}

SvMetaClass * SvIdlParser::ReadKnownClass()
{
    OString aName(ReadIdentifier());
    SvMetaClass* pClass = rBase.FindKnownClass( aName );
    if( !pClass )
        throw SvParseException( rInStm, "unknown class" );
    return pClass;
}

SvMetaType * SvIdlParser::ReadKnownType()
{
    OString aName = ReadIdentifier();
    for( const auto& aType : rBase.GetTypeList() )
    {
        if( aType->GetName() == aName )
        {
            return aType;
        }
    }
    throw SvParseException( rInStm, "wrong typedef: ");
}


void SvIdlParser::ReadDelimiter()
{
    if( !ReadIfDelimiter() )
        throw SvParseException(rInStm, "expected delimiter");
}

bool SvIdlParser::ReadIfDelimiter()
{
    if( rInStm.GetToken().IsChar()
        && (';' == rInStm.GetToken().GetChar()
             || ',' == rInStm.GetToken().GetChar()) )
    {
        rInStm.GetToken_Next();
        return true;
    }
    return false;
}

OString SvIdlParser::ReadIdentifier()
{
    SvToken& rTok = rInStm.GetToken();
    if( !rTok.IsIdentifier() )
        throw SvParseException("expected identifier", rTok);
    rInStm.GetToken_Next();
    return rTok.GetString();
}

OString SvIdlParser::ReadString()
{
    SvToken& rTok = rInStm.GetToken();
    if( !rTok.IsString() )
        throw SvParseException("expected string", rTok);
    rInStm.GetToken_Next();
    return rTok.GetString();
}

void SvIdlParser::Read(char cChar)
{
    if( !(rInStm.GetToken().IsChar() && rInStm.GetToken().GetChar() == cChar ) )
        throw SvParseException(rInStm, "expected char '" + OString(cChar) + "'");
    rInStm.GetToken_Next();
}

bool SvIdlParser::ReadIf(char cChar)
{
    if( rInStm.GetToken().IsChar() && rInStm.GetToken().GetChar() == cChar )
    {
        rInStm.GetToken_Next();
        return true;
    }
    return false;
}

void SvIdlParser::Read(SvStringHashEntry* entry)
{
    if( !rInStm.GetToken().Is(entry) )
        throw SvParseException("expected " + entry->GetName(), rInStm.GetToken());
    rInStm.GetToken_Next();
}

bool SvIdlParser::ReadIf(SvStringHashEntry* entry)
{
    if( rInStm.GetToken().Is(entry) )
    {
        rInStm.GetToken_Next();
        return true;
    }
    return false;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */