• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3.8
2# @generated by pegen from ./Tools/peg_generator/pegen/metagrammar.gram
3
4import ast
5import sys
6import tokenize
7
8from typing import Any, Optional
9
10from pegen.parser import memoize, memoize_left_rec, logger, Parser
11from ast import literal_eval
12
13from pegen.grammar import (
14    Alt,
15    Cut,
16    Gather,
17    Group,
18    Item,
19    Lookahead,
20    LookaheadOrCut,
21    MetaTuple,
22    MetaList,
23    NameLeaf,
24    NamedItem,
25    NamedItemList,
26    NegativeLookahead,
27    Opt,
28    Plain,
29    PositiveLookahead,
30    Repeat0,
31    Repeat1,
32    Rhs,
33    Rule,
34    RuleList,
35    RuleName,
36    Grammar,
37    StringLeaf,
38)
39
40class GeneratedParser(Parser):
41
42    @memoize
43    def start(self) -> Optional[Grammar]:
44        # start: grammar $
45        mark = self.mark()
46        cut = False
47        if (
48            (grammar := self.grammar())
49            and
50            (endmarker := self.expect('ENDMARKER'))
51        ):
52            return grammar
53        self.reset(mark)
54        if cut: return None
55        return None
56
57    @memoize
58    def grammar(self) -> Optional[Grammar]:
59        # grammar: metas rules | rules
60        mark = self.mark()
61        cut = False
62        if (
63            (metas := self.metas())
64            and
65            (rules := self.rules())
66        ):
67            return Grammar ( rules , metas )
68        self.reset(mark)
69        if cut: return None
70        cut = False
71        if (
72            (rules := self.rules())
73        ):
74            return Grammar ( rules , [ ] )
75        self.reset(mark)
76        if cut: return None
77        return None
78
79    @memoize
80    def metas(self) -> Optional[MetaList]:
81        # metas: meta metas | meta
82        mark = self.mark()
83        cut = False
84        if (
85            (meta := self.meta())
86            and
87            (metas := self.metas())
88        ):
89            return [ meta ] + metas
90        self.reset(mark)
91        if cut: return None
92        cut = False
93        if (
94            (meta := self.meta())
95        ):
96            return [ meta ]
97        self.reset(mark)
98        if cut: return None
99        return None
100
101    @memoize
102    def meta(self) -> Optional[MetaTuple]:
103        # meta: "@" NAME NEWLINE | "@" NAME NAME NEWLINE | "@" NAME STRING NEWLINE
104        mark = self.mark()
105        cut = False
106        if (
107            (literal := self.expect("@"))
108            and
109            (name := self.name())
110            and
111            (newline := self.expect('NEWLINE'))
112        ):
113            return ( name . string , None )
114        self.reset(mark)
115        if cut: return None
116        cut = False
117        if (
118            (literal := self.expect("@"))
119            and
120            (a := self.name())
121            and
122            (b := self.name())
123            and
124            (newline := self.expect('NEWLINE'))
125        ):
126            return ( a . string , b . string )
127        self.reset(mark)
128        if cut: return None
129        cut = False
130        if (
131            (literal := self.expect("@"))
132            and
133            (name := self.name())
134            and
135            (string := self.string())
136            and
137            (newline := self.expect('NEWLINE'))
138        ):
139            return ( name . string , literal_eval ( string . string ) )
140        self.reset(mark)
141        if cut: return None
142        return None
143
144    @memoize
145    def rules(self) -> Optional[RuleList]:
146        # rules: rule rules | rule
147        mark = self.mark()
148        cut = False
149        if (
150            (rule := self.rule())
151            and
152            (rules := self.rules())
153        ):
154            return [ rule ] + rules
155        self.reset(mark)
156        if cut: return None
157        cut = False
158        if (
159            (rule := self.rule())
160        ):
161            return [ rule ]
162        self.reset(mark)
163        if cut: return None
164        return None
165
166    @memoize
167    def rule(self) -> Optional[Rule]:
168        # rule: rulename memoflag? ":" alts NEWLINE INDENT more_alts DEDENT | rulename memoflag? ":" NEWLINE INDENT more_alts DEDENT | rulename memoflag? ":" alts NEWLINE
169        mark = self.mark()
170        cut = False
171        if (
172            (rulename := self.rulename())
173            and
174            (opt := self.memoflag(),)
175            and
176            (literal := self.expect(":"))
177            and
178            (alts := self.alts())
179            and
180            (newline := self.expect('NEWLINE'))
181            and
182            (indent := self.expect('INDENT'))
183            and
184            (more_alts := self.more_alts())
185            and
186            (dedent := self.expect('DEDENT'))
187        ):
188            return Rule ( rulename [ 0 ] , rulename [ 1 ] , Rhs ( alts . alts + more_alts . alts ) , memo = opt )
189        self.reset(mark)
190        if cut: return None
191        cut = False
192        if (
193            (rulename := self.rulename())
194            and
195            (opt := self.memoflag(),)
196            and
197            (literal := self.expect(":"))
198            and
199            (newline := self.expect('NEWLINE'))
200            and
201            (indent := self.expect('INDENT'))
202            and
203            (more_alts := self.more_alts())
204            and
205            (dedent := self.expect('DEDENT'))
206        ):
207            return Rule ( rulename [ 0 ] , rulename [ 1 ] , more_alts , memo = opt )
208        self.reset(mark)
209        if cut: return None
210        cut = False
211        if (
212            (rulename := self.rulename())
213            and
214            (opt := self.memoflag(),)
215            and
216            (literal := self.expect(":"))
217            and
218            (alts := self.alts())
219            and
220            (newline := self.expect('NEWLINE'))
221        ):
222            return Rule ( rulename [ 0 ] , rulename [ 1 ] , alts , memo = opt )
223        self.reset(mark)
224        if cut: return None
225        return None
226
227    @memoize
228    def rulename(self) -> Optional[RuleName]:
229        # rulename: NAME '[' NAME '*' ']' | NAME '[' NAME ']' | NAME
230        mark = self.mark()
231        cut = False
232        if (
233            (name := self.name())
234            and
235            (literal := self.expect('['))
236            and
237            (type := self.name())
238            and
239            (literal_1 := self.expect('*'))
240            and
241            (literal_2 := self.expect(']'))
242        ):
243            return ( name . string , type . string + "*" )
244        self.reset(mark)
245        if cut: return None
246        cut = False
247        if (
248            (name := self.name())
249            and
250            (literal := self.expect('['))
251            and
252            (type := self.name())
253            and
254            (literal_1 := self.expect(']'))
255        ):
256            return ( name . string , type . string )
257        self.reset(mark)
258        if cut: return None
259        cut = False
260        if (
261            (name := self.name())
262        ):
263            return ( name . string , None )
264        self.reset(mark)
265        if cut: return None
266        return None
267
268    @memoize
269    def memoflag(self) -> Optional[str]:
270        # memoflag: '(' 'memo' ')'
271        mark = self.mark()
272        cut = False
273        if (
274            (literal := self.expect('('))
275            and
276            (literal_1 := self.expect('memo'))
277            and
278            (literal_2 := self.expect(')'))
279        ):
280            return "memo"
281        self.reset(mark)
282        if cut: return None
283        return None
284
285    @memoize
286    def alts(self) -> Optional[Rhs]:
287        # alts: alt "|" alts | alt
288        mark = self.mark()
289        cut = False
290        if (
291            (alt := self.alt())
292            and
293            (literal := self.expect("|"))
294            and
295            (alts := self.alts())
296        ):
297            return Rhs ( [ alt ] + alts . alts )
298        self.reset(mark)
299        if cut: return None
300        cut = False
301        if (
302            (alt := self.alt())
303        ):
304            return Rhs ( [ alt ] )
305        self.reset(mark)
306        if cut: return None
307        return None
308
309    @memoize
310    def more_alts(self) -> Optional[Rhs]:
311        # more_alts: "|" alts NEWLINE more_alts | "|" alts NEWLINE
312        mark = self.mark()
313        cut = False
314        if (
315            (literal := self.expect("|"))
316            and
317            (alts := self.alts())
318            and
319            (newline := self.expect('NEWLINE'))
320            and
321            (more_alts := self.more_alts())
322        ):
323            return Rhs ( alts . alts + more_alts . alts )
324        self.reset(mark)
325        if cut: return None
326        cut = False
327        if (
328            (literal := self.expect("|"))
329            and
330            (alts := self.alts())
331            and
332            (newline := self.expect('NEWLINE'))
333        ):
334            return Rhs ( alts . alts )
335        self.reset(mark)
336        if cut: return None
337        return None
338
339    @memoize
340    def alt(self) -> Optional[Alt]:
341        # alt: items '$' action | items '$' | items action | items
342        mark = self.mark()
343        cut = False
344        if (
345            (items := self.items())
346            and
347            (literal := self.expect('$'))
348            and
349            (action := self.action())
350        ):
351            return Alt ( items + [ NamedItem ( None , NameLeaf ( 'ENDMARKER' ) ) ] , action = action )
352        self.reset(mark)
353        if cut: return None
354        cut = False
355        if (
356            (items := self.items())
357            and
358            (literal := self.expect('$'))
359        ):
360            return Alt ( items + [ NamedItem ( None , NameLeaf ( 'ENDMARKER' ) ) ] , action = None )
361        self.reset(mark)
362        if cut: return None
363        cut = False
364        if (
365            (items := self.items())
366            and
367            (action := self.action())
368        ):
369            return Alt ( items , action = action )
370        self.reset(mark)
371        if cut: return None
372        cut = False
373        if (
374            (items := self.items())
375        ):
376            return Alt ( items , action = None )
377        self.reset(mark)
378        if cut: return None
379        return None
380
381    @memoize
382    def items(self) -> Optional[NamedItemList]:
383        # items: named_item items | named_item
384        mark = self.mark()
385        cut = False
386        if (
387            (named_item := self.named_item())
388            and
389            (items := self.items())
390        ):
391            return [ named_item ] + items
392        self.reset(mark)
393        if cut: return None
394        cut = False
395        if (
396            (named_item := self.named_item())
397        ):
398            return [ named_item ]
399        self.reset(mark)
400        if cut: return None
401        return None
402
403    @memoize
404    def named_item(self) -> Optional[NamedItem]:
405        # named_item: NAME '=' ~ item | item | lookahead
406        mark = self.mark()
407        cut = False
408        if (
409            (name := self.name())
410            and
411            (literal := self.expect('='))
412            and
413            (cut := True)
414            and
415            (item := self.item())
416        ):
417            return NamedItem ( name . string , item )
418        self.reset(mark)
419        if cut: return None
420        cut = False
421        if (
422            (item := self.item())
423        ):
424            return NamedItem ( None , item )
425        self.reset(mark)
426        if cut: return None
427        cut = False
428        if (
429            (it := self.lookahead())
430        ):
431            return NamedItem ( None , it )
432        self.reset(mark)
433        if cut: return None
434        return None
435
436    @memoize
437    def lookahead(self) -> Optional[LookaheadOrCut]:
438        # lookahead: '&' ~ atom | '!' ~ atom | '~'
439        mark = self.mark()
440        cut = False
441        if (
442            (literal := self.expect('&'))
443            and
444            (cut := True)
445            and
446            (atom := self.atom())
447        ):
448            return PositiveLookahead ( atom )
449        self.reset(mark)
450        if cut: return None
451        cut = False
452        if (
453            (literal := self.expect('!'))
454            and
455            (cut := True)
456            and
457            (atom := self.atom())
458        ):
459            return NegativeLookahead ( atom )
460        self.reset(mark)
461        if cut: return None
462        cut = False
463        if (
464            (literal := self.expect('~'))
465        ):
466            return Cut ( )
467        self.reset(mark)
468        if cut: return None
469        return None
470
471    @memoize
472    def item(self) -> Optional[Item]:
473        # item: '[' ~ alts ']' | atom '?' | atom '*' | atom '+' | atom '.' atom '+' | atom
474        mark = self.mark()
475        cut = False
476        if (
477            (literal := self.expect('['))
478            and
479            (cut := True)
480            and
481            (alts := self.alts())
482            and
483            (literal_1 := self.expect(']'))
484        ):
485            return Opt ( alts )
486        self.reset(mark)
487        if cut: return None
488        cut = False
489        if (
490            (atom := self.atom())
491            and
492            (literal := self.expect('?'))
493        ):
494            return Opt ( atom )
495        self.reset(mark)
496        if cut: return None
497        cut = False
498        if (
499            (atom := self.atom())
500            and
501            (literal := self.expect('*'))
502        ):
503            return Repeat0 ( atom )
504        self.reset(mark)
505        if cut: return None
506        cut = False
507        if (
508            (atom := self.atom())
509            and
510            (literal := self.expect('+'))
511        ):
512            return Repeat1 ( atom )
513        self.reset(mark)
514        if cut: return None
515        cut = False
516        if (
517            (sep := self.atom())
518            and
519            (literal := self.expect('.'))
520            and
521            (node := self.atom())
522            and
523            (literal_1 := self.expect('+'))
524        ):
525            return Gather ( sep , node )
526        self.reset(mark)
527        if cut: return None
528        cut = False
529        if (
530            (atom := self.atom())
531        ):
532            return atom
533        self.reset(mark)
534        if cut: return None
535        return None
536
537    @memoize
538    def atom(self) -> Optional[Plain]:
539        # atom: '(' ~ alts ')' | NAME | STRING
540        mark = self.mark()
541        cut = False
542        if (
543            (literal := self.expect('('))
544            and
545            (cut := True)
546            and
547            (alts := self.alts())
548            and
549            (literal_1 := self.expect(')'))
550        ):
551            return Group ( alts )
552        self.reset(mark)
553        if cut: return None
554        cut = False
555        if (
556            (name := self.name())
557        ):
558            return NameLeaf ( name . string )
559        self.reset(mark)
560        if cut: return None
561        cut = False
562        if (
563            (string := self.string())
564        ):
565            return StringLeaf ( string . string )
566        self.reset(mark)
567        if cut: return None
568        return None
569
570    @memoize
571    def action(self) -> Optional[str]:
572        # action: "{" ~ target_atoms "}"
573        mark = self.mark()
574        cut = False
575        if (
576            (literal := self.expect("{"))
577            and
578            (cut := True)
579            and
580            (target_atoms := self.target_atoms())
581            and
582            (literal_1 := self.expect("}"))
583        ):
584            return target_atoms
585        self.reset(mark)
586        if cut: return None
587        return None
588
589    @memoize
590    def target_atoms(self) -> Optional[str]:
591        # target_atoms: target_atom target_atoms | target_atom
592        mark = self.mark()
593        cut = False
594        if (
595            (target_atom := self.target_atom())
596            and
597            (target_atoms := self.target_atoms())
598        ):
599            return target_atom + " " + target_atoms
600        self.reset(mark)
601        if cut: return None
602        cut = False
603        if (
604            (target_atom := self.target_atom())
605        ):
606            return target_atom
607        self.reset(mark)
608        if cut: return None
609        return None
610
611    @memoize
612    def target_atom(self) -> Optional[str]:
613        # target_atom: "{" ~ target_atoms "}" | NAME | NUMBER | STRING | "?" | ":" | !"}" OP
614        mark = self.mark()
615        cut = False
616        if (
617            (literal := self.expect("{"))
618            and
619            (cut := True)
620            and
621            (target_atoms := self.target_atoms())
622            and
623            (literal_1 := self.expect("}"))
624        ):
625            return "{" + target_atoms + "}"
626        self.reset(mark)
627        if cut: return None
628        cut = False
629        if (
630            (name := self.name())
631        ):
632            return name . string
633        self.reset(mark)
634        if cut: return None
635        cut = False
636        if (
637            (number := self.number())
638        ):
639            return number . string
640        self.reset(mark)
641        if cut: return None
642        cut = False
643        if (
644            (string := self.string())
645        ):
646            return string . string
647        self.reset(mark)
648        if cut: return None
649        cut = False
650        if (
651            (literal := self.expect("?"))
652        ):
653            return "?"
654        self.reset(mark)
655        if cut: return None
656        cut = False
657        if (
658            (literal := self.expect(":"))
659        ):
660            return ":"
661        self.reset(mark)
662        if cut: return None
663        cut = False
664        if (
665            self.negative_lookahead(self.expect, "}")
666            and
667            (op := self.op())
668        ):
669            return op . string
670        self.reset(mark)
671        if cut: return None
672        return None
673
674
675if __name__ == '__main__':
676    from pegen.parser import simple_parser_main
677    simple_parser_main(GeneratedParser)
678