This file is indexed.

/usr/include/wibble/parse.h is in libwibble-dev 1.1-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// -*- C++ -*- (c) 2011 Petr Rockai
//             (c) 2013 Jan Kriho
// Support code for writing backtracking recursive-descent parsers
#include <string>
#include <cassert>
#include <deque>
#include <vector>
#include <map>
#include <queue>

#include <wibble/regexp.h>

#ifndef WIBBLE_PARSE_H
#define WIBBLE_PARSE_H

namespace wibble {

struct Position {
    std::string source;
    int line;
    int column;
    Position() : source("-"), line(1), column(1) {}
    bool operator==( const Position &o ) const {
        return o.source == source && o.line == line && o.column == column;
    }
};

template< typename _Id >
struct Token {
    typedef _Id Id;
    Id id;
    std::string data;
    Position position;
    bool _valid;

    bool valid() { return _valid; }

    Token( Id _id, char c ) : id( _id ), _valid( true ) {
        data.push_back( c );
    }

    Token( Id _id, std::string d ) : id( _id ), data( d ), _valid( true ) {}
    Token() : id( Id( 0 ) ), _valid( false ) {}

    bool operator==( const Token &o ) const {
        return o._valid == _valid && o.id == id && o.data == data && o.position == position;
    }

};

template< typename X, typename Y >
inline std::ostream &operator<<( std::ostream &o, const std::pair< X, Y > &x ) {
    return o << "(" << x.first << ", " << x.second << ")";
}

/*
 * This is a SLOW lexer (at least compared to systems like lex/flex, which
 * build a finite-state machine to make decisions per input character. We could
 * do that in theory, but it would still be slow, since we would be in effect
 * interpreting the FSM anyway, while (f)lex uses an optimising compiler like
 * gcc. So while this is friendly and flexible, it won't give you a fast
 * scanner.
 */
template< typename Token, typename Stream >
struct Lexer {
    Stream &stream;
    typedef std::deque< char > Window;
    Window _window;
    Position current;

    Token _match;

    void shift() {
        assert( !stream.eof() );
        std::string r = stream.remove();
        std::copy( r.begin(), r.end(), std::back_inserter( _window ) );
    }

    bool eof() {
        return _window.empty() && stream.eof();
    }

    std::string window( unsigned n ) {
        bool valid = ensure_window( n );
        assert( valid );
        static_cast< void >( valid );
        std::deque< char >::iterator b = _window.begin(), e = b;
        e += n;
        return std::string( b, e );
    }

    bool ensure_window( unsigned n ) {
        while ( _window.size() < n && !stream.eof() )
            shift();
        return _window.size() >= n;
    }

    void consume( int n ) {
        for( int i = 0; i < n; ++i ) {
            if ( _window[i] == '\n' ) {
                current.line ++;
                current.column = 1;
            } else
                current.column ++;
        }
        std::deque< char >::iterator b = _window.begin(), e = b;
        e += n;
        _window.erase( b, e );
    }

    void consume( const std::string &s ) {
        consume( s.length() );
    }

    void consume( const Token &t ) {
        // std::cerr << "consuming " << t << std::endl;
        consume( t.data );
    }

    void keep( typename Token::Id id, const std::string &data ) {
        Token t( id, data );
        t.position = current;
        if ( t.data.length() > _match.data.length() )
            _match = t;
    }

    template< typename I >
    bool match( I begin, I end ) {
        if ( !ensure_window( end - begin ) )
            return false;
        return std::equal( begin, end, _window.begin() );
    }

    void match( const std::string &data, typename Token::Id id ) {
        if ( match( data.begin(), data.end() ) )
            return keep( id, data );
    }

    void match( Regexp &r, typename Token::Id id ) {
        unsigned n = 1, max = 0;
        while ( r.match( window( n ) ) ) {
            if ( max && max == r.matchLength( 0 ) )
                return keep( id, window( max ) );
            max = r.matchLength( 0 );
            ++ n;
        }
    }

    void match( int (*first)(int), int (*rest)(int), typename Token::Id id )
    {
        unsigned n = 1;

        if ( !ensure_window( 1 ) )
            return;

        if ( !first( _window[0] ) )
            return;

        while ( true ) {
            ++ n;
            if ( ensure_window( n ) && rest( _window[ n - 1 ] ) )
                continue;
            return keep( id, window( n - 1 ) );
        }
    }

    void match( const std::string &from, const std::string &to, typename Token::Id id ) {
        if ( !match( from.begin(), from.end() ) )
            return;

        Window::iterator where = _window.begin();
        int n = from.length();
        where += n;
        while ( true ) {
            if ( !ensure_window( n + to.length() ) )
                return;

            if ( std::equal( to.begin(), to.end(), where ) )
                return keep( id, window( n + to.length() ) );
            ++ where;
            ++ n;
        }
    }

    void skipWhitespace() {
        while ( !eof() && isspace( window( 1 )[ 0 ] ) )
            consume( 1 );
    }

    Token decide() {
        Token t;
        std::swap( t, _match );
        consume( t );
        return t;
    }

    Lexer( Stream &s ) : stream( s ) {}
};

template< typename Token, typename Stream >
struct ParseContext
{
    Stream *_stream;
    Stream &stream() { assert( _stream ); return *_stream; }

    std::deque< Token > window;
    int window_pos;
    int position;
    std::string name;
    typedef ParseContext< Token, Stream > This;
    std::vector< This > children;

    struct Fail {
        enum Type { Syntax, Semantic };

        int position;
        const char *expected;
        Type type;

        bool operator<( const Fail &other ) const {
            return position > other.position;
        }

        Fail( const char *err, int pos, Type t = Syntax) {
            expected = err;
            position = pos;
            type = t;
        }
        ~Fail() throw () {}
    };

    std::priority_queue< Fail > failures;

    void clearErrors() {
        failures = std::priority_queue< Fail >();
    }

    void error( std::ostream &o, std::string prefix, const Fail &fail ) {
        Token t = window[ fail.position ];
        switch ( fail.type ) {
            case Fail::Syntax:
                o << prefix
                << "expected " << fail.expected
                << " at line " << t.position.line
                << ", column " << t.position.column
                << ", but seen " << Token::tokenName[ t.id ] << " '" << t.data << "'"
                << std::endl;
                return;
            case Fail::Semantic:
                o << prefix
                << fail.expected
                << " at line " << t.position.line
                << ", column " << t.position.column
                << std::endl;
                return;
        }
    }

    void errors( std::ostream &o ) {
        for ( typename std::vector< This >::iterator pc = children.begin(); pc != children.end(); ++pc )
            pc->errors( o );

        if ( failures.empty() )
            return;

        std::string prefix;
        switch ( failures.top().type ) {
            case Fail::Syntax:
                o << "parse";
                break;
            case Fail::Semantic:
                o << "semantic";
                break;
        }
        o << " error in context " << name << ": ";
        if ( failures.size() > 1 ) {
            o << failures.size() << " rightmost alternatives:" << std::endl;
            prefix = "    ";
        }
        while ( !failures.empty() ) {
            error( o, prefix, failures.top() );
            failures.pop();
        }
    }

    Token remove() {
        if ( int( window.size() ) <= window_pos ) {
            Token t;
            do {
                t = stream().remove();
            } while ( t.id == Token::Comment ); // XXX
            window.push_back( t );
        }

        ++ window_pos;
        ++ position;
        return window[ window_pos - 1 ];
    }

    void rewind( int n ) {
        assert( n >= 0 );
        assert( n <= window_pos );
        window_pos -= n;
        position -= n;
    }

    This & createChild( Stream &s, std::string name ) {
        children.push_back( This( s, name ) );
        return children.back();
    }

    ParseContext( Stream &s, std::string name )
        : _stream( &s ), window_pos( 0 ), position( 0 ), name( name )
    {}
};

template< typename Token, typename Stream >
struct Parser {
    typedef typename Token::Id TokenId;
    typedef ParseContext< Token, Stream > Context;
    Context *ctx;
    typedef typename Context::Fail Fail;
    typedef typename Fail::Type FailType;
    int _position;

    bool valid() const {
        return ctx;
    }

    Context &context() {
        assert( ctx );
        return *ctx;
    }

    int position() {
        return context().position;
    }

    void rewind( int i ) {
        context().rewind( i );
        _position = context().position;
    }

    void fail( const char *what, FailType type = FailType::Syntax ) __attribute__((noreturn))
    {
        Fail f( what, _position, type );
        context().failures.push( f );
        while ( context().failures.top().position < _position )
            context().failures.pop();
        throw f;
    }

    void semicolon() {
        Token t = eat();
        if ( t.id == Token::Punctuation && t.data == ";" )
            return;

        rewind( 1 );
        fail( "semicolon" );
    }

    void colon() {
        Token t = eat();
        if ( t.id == Token::Punctuation && t.data == ":" )
            return;

        rewind( 1 );
        fail( "colon" );
    }

    Token eat( TokenId id ) {
        Token t = eat( false );
        if ( t.id == id )
            return t;
        rewind( 1 );
        fail( Token::tokenName[id].c_str() );
    }


#if __cplusplus >= 201103L
    template< typename F >
    void either( void (F::*f)() ) {
        (static_cast< F* >( this )->*f)();
    }

    template< typename F, typename... Args >
    void either( F f, Args... args ) {
        if ( maybe( f ) )
            return;
        either( args... );
    }
#else
    template< typename F, typename G >
    void either( F f, void (G::*g)() ) {
        if ( maybe( f ) )
            return;
        (static_cast< G* >( this )->*g)();
    }
#endif


#if __cplusplus >= 201103L
    template< typename F, typename... Args >
    bool maybe( F f, Args... args ) {
        if ( maybe( f ) )
            return true;
        return maybe( args... );
    }

#else
    template< typename F, typename G >
    bool maybe( F f, G g ) {
        if ( maybe( f ) )
            return true;
        return maybe( g );
    }


    template< typename F, typename G, typename H >
    bool maybe( F f, G g, H h ) {
        if ( maybe( f ) )
            return true;
        if ( maybe( g ) )
            return true;
        return maybe( h );
    }

#endif

    template< typename F >
    bool maybe( void (F::*f)() ) {
        int fallback = position();
        try {
            (static_cast< F* >( this )->*f)();
            return true;
        } catch ( Fail fail ) {
            rewind( position() - fallback );
            return false;
        }
    }
    
    bool maybe( TokenId id ) {
        int fallback = position();
        try {
            eat( id );
            return true;
        } catch (Fail) {
            rewind( position() - fallback );
            return false;
        }
    }

    template< typename T, typename I >
    void many( I i ) {
        int fallback = 0;
        try {
            while ( true ) {
                fallback = position();
                *i++ = T( context() );
            }
        } catch (Fail) {
            rewind( position() - fallback );
        }
    }
#if __cplusplus >= 201103L
    template< typename F >
    bool arbitrary( F f ) {
        return maybe( f );
    }

    template< typename F, typename... Args >
    bool arbitrary( F f, Args... args ) {
        bool retval = arbitrary( args... );
        retval |= maybe( f );
        retval |= arbitrary( args... );
        return retval;
    }
#endif

    template< typename T, typename I >
    void list( I i, TokenId sep ) {
        do {
            *i++ = T( context() );
        } while ( next( sep ) );
    }

    template< typename T, typename I, typename F >
    void list( I i, void (F::*sep)() ) {
        int fallback = position();
        try {
            while ( true ) {                
                *i++ = T( context() );
                fallback = position();
                (static_cast< F* >( this )->*sep)();
            }
        } catch(Fail) {
            rewind( position() - fallback );
        }
    }

    template< typename T, typename I >
    void list( I i, TokenId first, TokenId sep, TokenId last ) {
        eat( first );
        list< T >( i, sep );
        eat( last );
    }

    Token eat( bool _fail = true ) {
        Token t = context().remove();
        _position = context().position;
        if ( _fail && !t.valid() ) {
            rewind( 1 );
            fail( "valid token" );
        }
        return t;
    }

    bool next( TokenId id ) {
        Token t = eat( false );
        if ( t.id == id )
            return true;
        rewind( 1 );
        return false;
    }

    Parser( Context &c ) : ctx( &c ) {}
    Parser() : ctx( 0 ) {}

};

}

#endif