Source

grako / grako / grammars.py

The default branch has multiple heads

Full commit
  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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
# -*- coding: utf-8 -*-
"""
Elements for a model of a parsed Grako grammar.

A model constructed with these elements, and rooted in a Grammar instance is
able to parse the language defined by the grammar, but the main purpose of
the model is the generation of independent, top-down, verbose, and debugable
parsers through the inline templates from the .rendering module.

Models calculate the LL(k) FIRST function to aid in providing more significant
error messages when a choice fails to parse. FOLLOW(k) and LA(k) should be
computed, but they are not.
"""
from __future__ import print_function, division, absolute_import, unicode_literals
import re
from copy import deepcopy
from keyword import iskeyword
import time
from .util import simplify, indent, trim
from .rendering import Renderer, render
from .buffering import Buffer
from .exceptions import *  # @UnusedWildImport
from .ast import AST
from .contexts import ParseContext, ParseInfo

def check(result):
    assert isinstance(result, _Grammar), str(result)

def dot(x, y, k):
    return set([ (a + b)[:k] for a in x for b in y])

def urepr(obj):
    return repr(obj).lstrip('u')

class ModelContext(ParseContext):
    def __init__(self, rules, text, filename, trace, **kwargs):
        super(ModelContext, self).__init__(trace=trace, **kwargs)
        self.rules = {rule.name :rule for rule in rules}
        self._buffer = Buffer(text, filename=filename)
        self._buffer.goto(0)

    @property
    def pos(self):
        return self._buffer.pos

    @property
    def buf(self):
        return self._buffer

    def _find_rule(self, name):
        return self.rules[name]

class _Grammar(Renderer):
    def __init__(self):
        super(_Grammar, self).__init__()
        self._first_set = None

    def parse(self, ctx):
        return None

    @property
    def firstset(self, k=1):
        if self._first_set is None:
            self._first_set = self._first(k, {})
        return self._first_set

    def _validate(self, rules):
        pass

    def _first(self, k, F):
        return set()


class VoidGrammar(_Grammar):
    def __str__(self):
        return '()'

    template = 'pass'


class EOFGrammar(_Grammar):
    def parse(self, ctx):
        ctx._next_token()
        if not ctx.buf.atend():
            raise FailedParse(ctx.buf, 'Expecting end of text.')

    def __str__(self):
        return '$'

    template = 'self._check_eof()'


class _DecoratorGrammar(_Grammar):
    def __init__(self, exp):
        assert isinstance(exp, _Grammar), str(exp)
        super(_DecoratorGrammar, self).__init__()
        self.exp = exp

    def parse(self, ctx):
        return self.exp.parse(ctx)

    def _validate(self, rules):
        self.exp._validate(rules)

    def _first(self, k, F):
        return self.exp._first(k, F)

    def __str__(self):
        return str(self.exp)

    template = '{exp}'


class GroupGrammar(_DecoratorGrammar):
    def __str__(self):
        return '(%s)' % str(self.exp).strip()

    def render_fields(self, fields):
        fields.update(exp=indent(render(self.exp)))

    template = '''\
                with self._group():
                {exp}
                    _e = self.cst
                '''


class TokenGrammar(_Grammar):
    def __init__(self, token):
        super(TokenGrammar, self).__init__()
        self.token = token

    def parse(self, ctx):
        ctx._next_token()
        token = ctx.buf.match(self.token)
        if token is None:
            raise FailedToken(ctx.buf, self.token)
        ctx._trace_match(self.token, None)
        ctx._add_cst_node(token)
        return token

    def _first(self, k, F):
        return set([(self.token,)])

    def __str__(self):
        if "'" in self.token:
            if '"' in self.token:
                return "'%s'" % self.token.encode('string-escape')
            else:
                return '"%s"' % self.token
        return "'%s'" % self.token

    def render_fields(self, fields):
        fields.update(token=urepr(self.token))

    template = "_e = self._token({token})"


class PatternGrammar(_Grammar):
    def __init__(self, pattern):
        super(PatternGrammar, self).__init__()
        self.pattern = pattern
        self._re = re.compile(pattern)

    def parse(self, ctx):
        token = ctx.buf.matchre(self._re)
        if token is None:
            raise FailedPattern(ctx.buf, self.pattern)
        ctx._trace_match(token, self.pattern)
        ctx._add_cst_node(token)
        return token

    def _first(self, k, F):
        return set([(self.pattern,)])

    def __str__(self):
        return '?/%s/?' % self.pattern

    def render_fields(self, fields):
        fields.update(pattern=urepr(self.pattern))

    template = '_e = self._pattern({pattern})'


class LookaheadGrammar(_DecoratorGrammar):
    def __str__(self):
        return '!' + self.exp

    def parse(self, ctx):
        p = ctx.pos
        ctx._push_ast()
        try:
            super(LookaheadNotGrammar, self).parse(ctx)
        finally:
            ctx.goto(p)
            ctx._pop_ast()  # simply discard

    def render_fields(self, fields):
        fields.update(exp=indent(render(self.exp)))

    template = '''\
                with self._if():
                {exp}\
                '''

class LookaheadNotGrammar(_DecoratorGrammar):
    def __str__(self):
        return '!' + self.exp

    def parse(self, ctx):
        p = ctx.pos
        ctx._push_ast()
        try:
            super(LookaheadNotGrammar, self).parse(ctx)
            ctx.goto(p)
            raise FailedLookahead(str(self.exp))
        except FailedParse:
            ctx.goto(p)
            pass
        finally:
            ctx._pop_ast()  # simply discard


    def render_fields(self, fields):
        fields.update(exp=indent(render(self.exp)))

    template = '''\
                with self._ifnot():
                {exp}\
                '''


class SequenceGrammar(_Grammar):
    def __init__(self, sequence):
        super(SequenceGrammar, self).__init__()
        assert isinstance(sequence, list), str(sequence)
        self.sequence = sequence

    def parse(self, ctx):
        return simplify(self.parse_seq(ctx, self.sequence))

    def parse_seq(self, ctx, seq):
        result = []
        for _i, s in enumerate(seq):
            tree = s.parse(ctx)
            result.append(tree)
        return [r for r in result if r is not None]

    def _validate(self, rules):
        for s in self.sequence:
            s._validate(rules)

    def _first(self, k, F):
        result = {()}
        for s in self.sequence:
            result = dot(result, s._first(k, F), k)
        return result

    def __str__(self):
        return ' '.join(str(s).strip() for s in self.sequence)

    def render_fields(self, fields):
        fields.update(seq='\n'.join(render(s) for s in self.sequence))

    template = '''
                {seq}\
                '''


class ChoiceGrammar(_Grammar):
    def __init__(self, options):
        super(ChoiceGrammar, self).__init__()
        assert isinstance(options, list), urepr(options)
        self.options = options

    def parse(self, ctx):
        pos = ctx.pos
        for o in self.options:
            ctx.goto(pos)
            ctx._push_ast()
            try:
                result = o.parse(ctx)
                ast = ctx.ast
                cst = ctx.cst
            except FailedCut:
                raise
            except FailedParse:
                continue
            finally:
                ctx._pop_ast()
            ctx.ast.update(ast)
            ctx._extend_cst(cst)
            return result

        firstset = ' '.join(str(urepr(f[0])) for f in self.firstset if f)
        raise FailedParse(ctx.buf, 'one of {%s}' % firstset)

    def _validate(self, rules):
        for o in self.options:
            o._validate(rules)

    def _first(self, k, F):
        result = set()
        for o in self.options:
            result |= o._first(k, F)
        return result

    def __str__(self):
        return ' | '.join(str(o).strip() for o in self.options)

    def render_fields(self, fields):
        template = trim(self.option_template)
        options = [template.format(option=indent(render(o))) for o in self.options]
        options = '\n'.join(o for o in options)
        firstset = ' '.join(str(urepr(f[0])) for f in self.firstset if f)
        if firstset:
            error = 'expecting one of: ' + firstset
        else:
            error = 'no available options'
        fields.update(n=self.counter(),
                      options=indent(options),
                      error=urepr(error)
                      )

    def render(self, **fields):
        if len(self.options) == 1:
            return render(self.options[0], **fields)
        else:
            return super(ChoiceGrammar, self).render(**fields)

    option_template = '''\
                    with self._option():
                    {option}
                        return _e\
                    '''

    template = '''\
                def choice{n}():
                    _e = None
                {options}
                    self._error({error})
                _e = choice{n}() \
                '''


class RepeatGrammar(_DecoratorGrammar):
    def parse(self, ctx):
        result = []
        while True:
            p = ctx.buf.pos
            try:
                tree = self.exp.parse(ctx)
                if tree is not None:
                    result.append(tree)
            except FailedParse:
                ctx.buf.goto(p)
                break
        return result


    def _first(self, k, F):
        result = {()}
        for _i in range(k):
            result = dot(result, self.exp._first(k, F), k)
        return {()} | result

    def __str__(self):
        return '{%s}' % str(self.exp)

    def render_fields(self, fields):
        fields.update(n=self.counter(),
                      innerexp=indent(render(self.exp)))

    def render(self, **fields):
        if {()} in self.exp.firstset:
            raise GrammarError('may repeat empty sequence')
        return super(RepeatGrammar, self).render(**fields)

    template = '''
                def repeat{n}():
                {innerexp}
                    return _e
                _e = self._repeat(repeat{n})\
                '''


class RepeatOneGrammar(RepeatGrammar):
    def parse(self, ctx):
        head = self.exp.parse(ctx)
        return [head] + super(RepeatOneGrammar, self).parse(ctx)

    def _first(self, k, F):
        result = {()}
        for _i in range(k):
            result = dot(result, self.exp._first(k, F), k)
        return result

    def __str__(self):
        return '{%s}+' % str(self.exp)

    def render_fields(self, fields):
        fields.update(n=self.counter(),
                      exp=render(self.exp),
                      innerexp=indent(render(self.exp)))

    template = '''
                def repeat{n}():
                {innerexp}
                    return _e
                _e = self._repeat(repeat{n}, plus=True)\
                '''


class OptionalGrammar(_DecoratorGrammar):
    def parse(self, ctx):
        p = ctx.pos
        ctx._push_ast()
        try:
            result = self.exp.parse(ctx)
            ast = ctx.ast
            cst = ctx.cst
        except FailedCut:
            raise
        except FailedParse:
            ctx.goto(p)
            return None
        finally:
            ctx._pop_ast()
        ctx.ast.update(ast)
        ctx._extend_cst(cst)
        return result

    def _first(self, k, F):
        return {()} | self.exp._first(k, F)

    def __str__(self):
        return '[%s]' % str(self.exp)

    def render_fields(self, fields):
        fields.update(exp=indent(render(self.exp)))

    template = '''\
                with self._optional():
                    _e = None
                {exp}\
                '''


class CutGrammar(_Grammar):
    def parse(self, ctx):
        return None

    def _first(self, k, F):
        return {('>>',)}

    def __str__(self):
        return '>>'

    template = 'self._cut()'


class NamedGrammar(_DecoratorGrammar):
    def __init__(self, name, exp, force_list):
        super(NamedGrammar, self).__init__(exp)
        assert isinstance(exp, _Grammar), str(exp)
        self.name = name
        self.force_list = force_list

    def parse(self, ctx):
        value = self.exp.parse(ctx)
        ctx._add_ast_node(self.name, value, self.force_list)
        return value

    def __str__(self):
        if self.force_list:
            return '%s+:%s' % (self.name, str(self.exp))
        return '%s:%s' % (self.name, str(self.exp))

    def render_fields(self, fields):
        name = self.name
        if iskeyword(name):
            name += '_'
        fields.update(
                      n=self.counter(),
                      name=name,
                      exp=render(self.exp),
                      force_list=', force_list=True' if self.force_list else ''
                      )
    template = '''
                {exp}
                self.ast.add('{name}', _e{force_list})\
                '''


class OverrideGrammar(_DecoratorGrammar):
    def parse(self, ctx):
        result = super(OverrideGrammar, self).parse(ctx)
        ctx._add_ast_node('@', result)
        return result

    def __str__(self):
        return '@%s' % str(self.exp)

    template = '''
                {exp}
                self.ast['@'] = _e\
                '''


class SpecialGrammar(_Grammar):
    def __init__(self, special):
        super(SpecialGrammar, self).__init__()
        self.special = special

    def _first(self, k, F):
        return set([(self.special,)])

    def __str__(self):
        return '?/%s/?' % self.pattern


class RuleRefGrammar(_Grammar):
    def __init__(self, name):
        super(RuleRefGrammar, self).__init__()
        self.name = name

    def parse(self, ctx):
        try:
            rule = ctx._find_rule(self.name)
            if self.name[0].islower():
                ctx._next_token()
            node = rule.parse(ctx)
            ctx._add_cst_node(node)
            return node
        except KeyError:
            raise FailedRef(ctx.buf, self.name)
        except FailedParse:
            raise

    def _validate(self, rules):
        if self.name not in rules:
            raise GrammarError("ERROR: Reference to unknown rule '%s'." % self.name)

    def _first(self, k, F):
        self._first_set = F.get(self.name, set())
        return self._first_set

    def __str__(self):
        return self.name

    def render_fields(self, fields):
        name = self.name
        if iskeyword(name):
            name += '_'
        fields.update(name=name)

    template = "_e = self._call('{name}')"


class RuleGrammar(NamedGrammar):
    def __init__(self, name, exp, ast_name=None):
        super(RuleGrammar, self).__init__(name, exp, False)
        self.ast_name = ast_name

    def parse(self, ctx):
        ctx._rule_stack.append(self.name)
        try:
            if self.name[0].islower():
                ctx._next_token()
            ctx._trace_event('ENTER ')
            node, newpos = self._invoke_rule(self.name, ctx, ctx.pos)
            ctx.goto(newpos)
            ctx._trace_event('SUCCESS')
            return node
        except FailedParse:
            ctx._trace_event('FAILED')
            raise
        finally:
            ctx._rule_stack.pop()

    def _invoke_rule(self, name, ctx, pos):
        key = (pos, name)
        cache = ctx._memoization_cache

        if key in cache:
            return cache[key]

        ctx.goto(pos)
        ctx._push_ast()
        try:
            self.exp.parse(ctx)
            node = ctx.ast
            if not node:
                node = ctx.cst
            elif '@' in node:
                node = node['@']
            elif ctx.parseinfo:
                node.add('parseinfo', ParseInfo(ctx._buffer, name, pos, ctx._pos))
            if self.ast_name:
                node = AST([(self.ast_name, node)])
        finally:
            ctx._pop_ast()
        result = (node, ctx.pos)
        cache[key] = result
        return result


    def _first(self, k, F):
        if self._first_set:
            return self._first_set
        return self.exp._first(k, F)

    def __str__(self):
        return '%s = %s ;' % (self.name, str(self.exp).strip())

    def render_fields(self, fields):
        name = self.name
        if iskeyword(name):
            name += '_'
        if self.ast_name:
            ast_name_clause = '\nself.ast = AST(%s=self.ast)\n' % self.ast_name_
        else:
            ast_name_clause = ''
        fields.update(
                      name=name,
                      exp=indent(render(self.exp)),
                      ast_name_clause=ast_name_clause
                      )

    template = '''
                def _{name}_(self):
                    _e = None
                {exp}{ast_name_clause}

                '''

class Grammar(Renderer):
    def __init__(self, name, rules):
        super(Grammar, self).__init__()
        assert isinstance(rules, list), str(rules)
        self.name = name
        self.rules = rules
        self._validate()
        self._first_sets = self._calc_first_sets()

    def _validate(self):
        ruledict = {r.name for r in self.rules}
        for rule in self.rules:
            rule._validate(ruledict)

    @property
    def first_sets(self):
        return self._first_sets

    def _calc_first_sets(self, k=1):
        F = dict()
        while True:
            F1 = deepcopy(F)
            for rule in self.rules:
                F[rule.name] = F.get(rule.name, set()) | rule._first(k, F)
            if F1 == F:
                break
        for rule in self.rules:
            rule._first_set = F[rule.name]
        return F

    def parse(self, text, start=None, filename=None, trace=False, **kwargs):
        try:
            try:
                ctx = ModelContext(self.rules, text, filename, trace=trace, **kwargs)
                start_rule = ctx._find_rule(start) if start else self.rules[0]
                return start_rule.parse(ctx)
            except FailedCut as e:
                raise e.nested
        except:
            raise

    def __str__(self):
        return '\n\n'.join(str(rule) for rule in self.rules) + '\n'

    def render_fields(self, fields):
        abstract_template = trim(self.abstract_rule_template)
        abstract_rules = [abstract_template.format(name=rule.name) for rule in self.rules]
        abstract_rules = indent('\n'.join(abstract_rules))
        fields.update(rules=indent(render(self.rules)),
                      abstract_rules=abstract_rules,
                      version=time.strftime('%y.%j.%H.%M.%S', time.gmtime())
                      )


    abstract_rule_template = '''
            def {name}(self, ast):
                return ast
            '''

    template = '''\
                #!/usr/env/bin python
                # -*- coding: utf-8 -*-
                #
                # CAVEAT UTILITOR
                # This file was automatically generated by Grako.
                #    https://bitbucket.org/apalala/grako/
                # Any changes you make to it will be overwritten the
                # next time the file is generated.
                #

                from __future__ import print_function, division, absolute_import, unicode_literals
                from grako.parsing import * # @UnusedWildImport
                from grako.exceptions import * # @UnusedWildImport

                __version__ = '{version}'

                class {name}ParserRoot(Parser):
                {rules}


                class Abstract{name}Parser(AbstractParserMixin, {name}ParserRoot):
                    pass


                class {name}ParserBase({name}ParserRoot):
                {abstract_rules}

                def main(filename, startrule):
                    import json
                    with open(filename) as f:
                        text = f.read()
                    parser = {name}ParserBase(parseinfo=False)
                    ast = parser.parse(text, startrule, filename=filename)
                    print('AST:')
                    print(ast)
                    print()
                    print('JSON:')
                    print(json.dumps(ast, indent=2))
                    print()

                if __name__ == '__main__':
                    import sys
                    if '-l' in sys.argv:
                        print('Rules:')
                        for r in {name}ParserBase.rule_list():
                            print(r)
                        print()
                    elif len(sys.argv) == 3:
                        main(sys.argv[1], sys.argv[2])
                    else:
                        print('Usage:')
                        program = sys.argv[0].split('/')[-1]
                        print(program, ' <filename> <startrule>')
                        print(program, ' -l') # list rules
                        print(program, ' -h')

                '''