]> git.phdru.name Git - phdru.name/cgi-bin/blog-ru/search-tags.git/blob - parser/parser.py
460a552e43691262d2fac6ba192c00c69fa783b2
[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, 9, 18, 10, 4, 5)
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._group():
62             with self._choice():
63                 with self._option():
64                     self._expression_()
65                     self._and_op_()
66                     self._expression_()
67                 with self._option():
68                     self._expression_()
69                     self._or_op_()
70                     self._expression_()
71                 with self._option():
72                     self._not_op_()
73                     self._expression_()
74                 with self._option():
75                     self._expression_parens_()
76                 with self._option():
77                     self._name_()
78                 self._error('no available options')
79
80     @graken()
81     def _expression_parens_(self):
82         self._token('(')
83         self._expression_()
84         self._token(')')
85
86     @graken()
87     def _name_(self):
88         self._pattern(r'[a-z][a-z0-9_]+')
89
90     @graken()
91     def _and_op_(self):
92         with self._choice():
93             with self._option():
94                 self._token('&')
95             with self._option():
96                 self._token('&&')
97             with self._option():
98                 self._token('AND')
99             with self._option():
100                 self._token('and')
101             self._error('expecting one of: & && AND and')
102
103     @graken()
104     def _or_op_(self):
105         with self._choice():
106             with self._option():
107                 self._token('|')
108             with self._option():
109                 self._token('||')
110             with self._option():
111                 self._token('OR')
112             with self._option():
113                 self._token('or')
114             self._error('expecting one of: OR or | ||')
115
116     @graken()
117     def _not_op_(self):
118         with self._choice():
119             with self._option():
120                 self._token('!')
121             with self._option():
122                 self._token('NOT')
123             with self._option():
124                 self._token('not')
125             self._error('expecting one of: ! NOT not')
126
127
128 class TagsSemantics(object):
129     def start(self, ast):
130         return ast
131
132     def expression(self, ast):
133         return ast
134
135     def expression_parens(self, ast):
136         return ast
137
138     def name(self, ast):
139         return ast
140
141     def and_op(self, ast):
142         return ast
143
144     def or_op(self, ast):
145         return ast
146
147     def not_op(self, ast):
148         return ast
149
150
151 def main(
152         filename,
153         startrule,
154         trace=False,
155         whitespace=None,
156         nameguard=None,
157         comments_re=None,
158         eol_comments_re=None,
159         ignorecase=None,
160         left_recursion=True,
161         **kwargs):
162
163     with open(filename) as f:
164         text = f.read()
165     whitespace = whitespace or None
166     parser = TagsParser(parseinfo=False)
167     ast = parser.parse(
168         text,
169         startrule,
170         filename=filename,
171         trace=trace,
172         whitespace=whitespace,
173         nameguard=nameguard,
174         ignorecase=ignorecase,
175         **kwargs)
176     return ast
177
178 if __name__ == '__main__':
179     import json
180     ast = generic_main(main, TagsParser, name='Tags')
181     print('AST:')
182     print(ast)
183     print()
184     print('JSON:')
185     print(json.dumps(ast, indent=2))
186     print()