]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/blob - parser/parser.py
4984f1862f4a4ef165dfc7ba05a3c263e2936056
[phdru.name/cgi-bin/blog-ru/search-tags.git] / parser / parser.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # CAVEAT UTILITOR
5 #
6 # This file was automatically generated by Grako.
7 #
8 #    https://pypi.python.org/pypi/grako/
9 #
10 # Any changes you make to it will be overwritten the next time
11 # the file is generated.
12
13
14 from __future__ import print_function, division, absolute_import, unicode_literals
15
16 from grako.parsing import graken, Parser
17 from grako.util import re, RE_FLAGS, generic_main  # noqa
18
19
20 __version__ = (2016, 7, 10, 22, 31, 57, 6)
21
22 __all__ = [
23     'TagsParser',
24     'TagsSemantics',
25     'main'
26 ]
27
28 KEYWORDS = set([])
29
30
31 class TagsParser(Parser):
32     def __init__(self,
33                  whitespace=None,
34                  nameguard=None,
35                  comments_re=None,
36                  eol_comments_re=None,
37                  ignorecase=None,
38                  left_recursion=True,
39                  keywords=KEYWORDS,
40                  namechars='',
41                  **kwargs):
42         super(TagsParser, self).__init__(
43             whitespace=whitespace,
44             nameguard=nameguard,
45             comments_re=comments_re,
46             eol_comments_re=eol_comments_re,
47             ignorecase=ignorecase,
48             left_recursion=left_recursion,
49             keywords=keywords,
50             namechars=namechars,
51             **kwargs
52         )
53
54     @graken()
55     def _start_(self):
56         self._expression_()
57         self._check_eof()
58
59     @graken()
60     def _expression_(self):
61         with self._choice():
62             with self._option():
63                 self._expression1_()
64                 with self._ifnot():
65                     with self._if():
66                         self._or_op_()
67             with self._option():
68                 self._or_expression_()
69             self._error('no available options')
70
71     @graken()
72     def _or_expression_(self):
73         self._expression1_()
74         self._or_op_()
75         self._expression_()
76
77     @graken()
78     def _and_expression_(self):
79         self._expression2_()
80         self._and_op_()
81         self._expression1_()
82
83     @graken()
84     def _not_expression_(self):
85         self._not_op_()
86         self._expression3_()
87
88     @graken()
89     def _parens_expression_(self):
90         self._token('(')
91         self._expression_()
92         self._token(')')
93
94     @graken()
95     def _expression1_(self):
96         with self._choice():
97             with self._option():
98                 self._expression2_()
99                 with self._ifnot():
100                     with self._if():
101                         self._and_op_()
102             with self._option():
103                 self._and_expression_()
104             self._error('no available options')
105
106     @graken()
107     def _expression2_(self):
108         with self._choice():
109             with self._option():
110                 with self._ifnot():
111                     with self._if():
112                         self._not_op_()
113                 self._expression3_()
114             with self._option():
115                 self._not_expression_()
116             self._error('no available options')
117
118     @graken()
119     def _expression3_(self):
120         with self._choice():
121             with self._option():
122                 self._parens_expression_()
123             with self._option():
124                 self._name_()
125             self._error('no available options')
126
127     @graken()
128     def _and_op_(self):
129         with self._choice():
130             with self._option():
131                 self._token('&&')
132             with self._option():
133                 self._token('&')
134             with self._option():
135                 self._token('AND')
136             with self._option():
137                 self._token('and')
138             self._error('expecting one of: & && AND and')
139
140     @graken()
141     def _or_op_(self):
142         with self._choice():
143             with self._option():
144                 self._token('||')
145             with self._option():
146                 self._token('|')
147             with self._option():
148                 self._token('OR')
149             with self._option():
150                 self._token('or')
151             self._error('expecting one of: OR or | ||')
152
153     @graken()
154     def _not_op_(self):
155         with self._choice():
156             with self._option():
157                 self._token('!')
158             with self._option():
159                 self._token('NOT')
160             with self._option():
161                 self._token('not')
162             self._error('expecting one of: ! NOT not')
163
164     @graken()
165     def _name_(self):
166         self._pattern(r'[a-z][a-z0-9_]+')
167
168
169 class TagsSemantics(object):
170     def start(self, ast):
171         return ast
172
173     def expression(self, ast):
174         return ast
175
176     def or_expression(self, ast):
177         return ast
178
179     def and_expression(self, ast):
180         return ast
181
182     def not_expression(self, ast):
183         return ast
184
185     def parens_expression(self, ast):
186         return ast
187
188     def expression1(self, ast):
189         return ast
190
191     def expression2(self, ast):
192         return ast
193
194     def expression3(self, ast):
195         return ast
196
197     def and_op(self, ast):
198         return ast
199
200     def or_op(self, ast):
201         return ast
202
203     def not_op(self, ast):
204         return ast
205
206     def name(self, ast):
207         return ast
208
209
210 def main(
211         filename,
212         startrule,
213         trace=False,
214         whitespace=None,
215         nameguard=None,
216         comments_re=None,
217         eol_comments_re=None,
218         ignorecase=None,
219         left_recursion=True,
220         **kwargs):
221
222     with open(filename) as f:
223         text = f.read()
224     whitespace = whitespace or None
225     parser = TagsParser(parseinfo=False)
226     ast = parser.parse(
227         text,
228         startrule,
229         filename=filename,
230         trace=trace,
231         whitespace=whitespace,
232         nameguard=nameguard,
233         ignorecase=ignorecase,
234         **kwargs)
235     return ast
236
237 if __name__ == '__main__':
238     import json
239     ast = generic_main(main, TagsParser, name='Tags')
240     print('AST:')
241     print(ast)
242     print()
243     print('JSON:')
244     print(json.dumps(ast, indent=2))
245     print()