]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/blob - parser/parser.py
Fix negative lookahead in grammar syntax
[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, 11, 18, 15, 20, 0)
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                     self._or_op_()
66             with self._option():
67                 self._or_expression_()
68             self._error('no available options')
69
70     @graken()
71     def _or_expression_(self):
72         self._expression1_()
73         self._or_op_()
74         self._expression_()
75
76     @graken()
77     def _and_expression_(self):
78         self._expression2_()
79         self._and_op_()
80         self._expression1_()
81
82     @graken()
83     def _not_expression_(self):
84         self._not_op_()
85         self._expression3_()
86
87     @graken()
88     def _parens_expression_(self):
89         self._token('(')
90         self._expression_()
91         self._token(')')
92
93     @graken()
94     def _expression1_(self):
95         with self._choice():
96             with self._option():
97                 self._expression2_()
98                 with self._ifnot():
99                     self._and_op_()
100             with self._option():
101                 self._and_expression_()
102             self._error('no available options')
103
104     @graken()
105     def _expression2_(self):
106         with self._choice():
107             with self._option():
108                 with self._ifnot():
109                     self._not_op_()
110                 self._expression3_()
111             with self._option():
112                 self._not_expression_()
113             self._error('no available options')
114
115     @graken()
116     def _expression3_(self):
117         with self._choice():
118             with self._option():
119                 self._parens_expression_()
120             with self._option():
121                 self._name_()
122             self._error('no available options')
123
124     @graken()
125     def _and_op_(self):
126         with self._choice():
127             with self._option():
128                 self._token('&&')
129             with self._option():
130                 self._token('&')
131             with self._option():
132                 self._token('AND')
133             with self._option():
134                 self._token('and')
135             self._error('expecting one of: & && AND and')
136
137     @graken()
138     def _or_op_(self):
139         with self._choice():
140             with self._option():
141                 self._token('||')
142             with self._option():
143                 self._token('|')
144             with self._option():
145                 self._token('OR')
146             with self._option():
147                 self._token('or')
148             self._error('expecting one of: OR or | ||')
149
150     @graken()
151     def _not_op_(self):
152         with self._choice():
153             with self._option():
154                 self._token('!')
155             with self._option():
156                 self._token('NOT')
157             with self._option():
158                 self._token('not')
159             self._error('expecting one of: ! NOT not')
160
161     @graken()
162     def _name_(self):
163         self._pattern(r'[a-z][a-z0-9_]+')
164
165
166 class TagsSemantics(object):
167     def start(self, ast):
168         return ast
169
170     def expression(self, ast):
171         return ast
172
173     def or_expression(self, ast):
174         return ast
175
176     def and_expression(self, ast):
177         return ast
178
179     def not_expression(self, ast):
180         return ast
181
182     def parens_expression(self, ast):
183         return ast
184
185     def expression1(self, ast):
186         return ast
187
188     def expression2(self, ast):
189         return ast
190
191     def expression3(self, ast):
192         return ast
193
194     def and_op(self, ast):
195         return ast
196
197     def or_op(self, ast):
198         return ast
199
200     def not_op(self, ast):
201         return ast
202
203     def name(self, ast):
204         return ast
205
206
207 def main(
208         filename,
209         startrule,
210         trace=False,
211         whitespace=None,
212         nameguard=None,
213         comments_re=None,
214         eol_comments_re=None,
215         ignorecase=None,
216         left_recursion=True,
217         **kwargs):
218
219     with open(filename) as f:
220         text = f.read()
221     whitespace = whitespace or None
222     parser = TagsParser(parseinfo=False)
223     ast = parser.parse(
224         text,
225         startrule,
226         filename=filename,
227         trace=trace,
228         whitespace=whitespace,
229         nameguard=nameguard,
230         ignorecase=ignorecase,
231         **kwargs)
232     return ast
233
234 if __name__ == '__main__':
235     import json
236     ast = generic_main(main, TagsParser, name='Tags')
237     print('AST:')
238     print(ast)
239     print()
240     print('JSON:')
241     print(json.dumps(ast, indent=2))
242     print()