Debian Codegraph @

src/yapps3/yapps_grammar.g

raw
1 #!/usr/bin/python2
2 #
3 # grammar.py, part of Yapps 2 - yet another python parser system
4 # Copyright 1999-2003 by Amit J. Patel <[email protected]>
5 #
6 # This version of the Yapps 2 grammar can be distributed under the
7 # terms of the MIT open source license, either found in the LICENSE
8 # file included with the Yapps distribution
9 # <http://theory.stanford.edu/~amitp/yapps/> or at
10 # <http://www.opensource.org/licenses/mit-license.php>
11 #
12
13 """Parser for Yapps grammars.
14
15 This file defines the grammar of Yapps grammars. Naturally, it is
16 implemented in Yapps. The grammar.py module needed by Yapps is built
17 by running Yapps on yapps_grammar.g. (Holy circularity, Batman!)
18
19 """
20
21 import sys, re
22 import parsetree
23
24 ######################################################################
25 def cleanup_choice(rule, lst):
26 if len(lst) == 0: return Sequence(rule, [])
27 if len(lst) == 1: return lst[0]
28 return parsetree.Choice(rule, *tuple(lst))
29
30 def cleanup_sequence(rule, lst):
31 if len(lst) == 1: return lst[0]
32 return parsetree.Sequence(rule, *tuple(lst))
33
34 def resolve_name(rule, tokens, id, args):
35 if id in [x[0] for x in tokens]:
36 # It's a token
37 if args:
38 print 'Warning: ignoring parameters on TOKEN %s<<%s>>' % (id, args)
39 return parsetree.Terminal(rule, id)
40 else:
41 # It's a name, so assume it's a nonterminal
42 return parsetree.NonTerminal(rule, id, args)
43
44 %%
45 parser ParserDescription:
46 option: "context-insensitive-scanner"
47
48 ignore: "[ \t\r\n]+"
49 ignore: "#.*?\r?\n"
50 token EOF: "$"
51 token ATTR: "<<.+?>>"
52 token STMT: "{{.+?}}"
53 token STR: '[uUrR]?\'([^\\n\'\\\\]|\\\\.)*\'|[uUrR]?"([^\\n"\\\\]|\\\\.)*"'
54 token ID: '[a-zA-Z_][a-zA-Z_0-9]*'
55 token LP: '\\('
56 token RP: '\\)'
57 token LB: '\\['
58 token RB: '\\]'
59 token OR: '[|]'
60 token STAR: '[*]'
61 token PLUS: '[+]'
62 token QUEST: '[?]'
63 token COLON: ':'
64
65 rule LINENO: # This is a pseudotoken. It matches nothing; returns the line number
66 {{ return 1 + self._scanner.get_input_scanned().count('\n') }}
67
68 rule Parser: "parser" ID ":"
69 Options
70 Tokens
71 Rules<<Tokens>>
72 EOF
73 {{ return parsetree.Generator(ID,Options,Tokens,Rules) }}
74
75 rule Options: {{ opt = {} }}
76 ( "option" ":" Str {{ opt[Str] = 1 }} )*
77 {{ return opt }}
78
79 rule Tokens: {{ tok = [] }}
80 (
81 "token" ID ":" Str {{ tok.append( (ID,Str) ) }}
82 | "ignore" ":" Str {{ tok.append( ('#ignore',Str) ) }}
83 )*
84 {{ return tok }}
85
86 rule Rules<<tokens>>:
87 {{ rul = [] }}
88 ( LINENO
89 "rule" ID OptParam ":" ClauseA<<ID, tokens>>
90 # TODO: save LINENO somewhere?
91 {{ rul.append( (ID, OptParam, ClauseA) ) }}
92 )*
93 {{ return rul }}
94
95 rule ClauseA<<rule, tokens>>:
96 ClauseB<<rule, tokens>>
97 {{ v = [ClauseB] }}
98 ( OR ClauseB<<rule, tokens>> {{ v.append(ClauseB) }} )*
99 {{ return cleanup_choice(rule, v) }}
100
101 rule ClauseB<<rule, tokens>>:
102 {{ v = [] }}
103 ( ClauseC<<rule, tokens>> {{ v.append(ClauseC) }} )*
104 {{ return cleanup_sequence(rule, v) }}
105
106 rule ClauseC<<rule, tokens>>:
107 ClauseD<<rule, tokens>>
108 ( PLUS {{ return parsetree.Plus(rule, ClauseD) }}
109 | STAR {{ return parsetree.Star(rule, ClauseD) }}
110 | QUEST {{ return parsetree.Option(rule, ClauseD) }}
111 | {{ return ClauseD }} )
112
113 rule ClauseD<<rule, tokens>>:
114 STR {{ t = (STR, eval(STR,{},{})) }}
115 {{ if t not in tokens: tokens.insert( 0, t ) }}
116 {{ return parsetree.Terminal(rule, STR) }}
117 | ID OptParam {{ return resolve_name(rule, tokens, ID, OptParam) }}
118 | LP ClauseA<<rule, tokens>> RP {{ return ClauseA }}
119 | LB ClauseA<<rule, tokens>> RB {{ return parsetree.Option(rule, ClauseA) }}
120 | STMT {{ return parsetree.Eval(rule, STMT[2:-2]) }}
121
122 rule OptParam:
123 ATTR {{ return ATTR[2:-2] }}
124 | {{ return '' }}
125 rule Str: STR {{ return eval(STR,{},{}) }}
126 %%