Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(612)

Side by Side Diff: tools/lexer_generator/regex_parser.py

Issue 50873003: Experimental Parser: add lexer generator (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/lexer_generator/regex_lexer.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2013 the V8 project authors. All rights reserved.
2 # Redistribution and use in source and binary forms, with or without
3 # modification, are permitted provided that the following conditions are
4 # met:
5 #
6 # * Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above
9 # copyright notice, this list of conditions and the following
10 # disclaimer in the documentation and/or other materials provided
11 # with the distribution.
12 # * Neither the name of Google Inc. nor the names of its
13 # contributors may be used to endorse or promote products derived
14 # from this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 import ply.yacc as yacc
29 from regex_lexer import RegexLexer
30 from types import ListType, TupleType
31
32 class RegexParser:
33
34 tokens = RegexLexer.tokens
35
36 token_map = {
37 '+': 'ONE_OR_MORE',
38 '?': 'ZERO_OR_ONE',
39 '*': 'ZERO_OR_MORE',
40 '|': 'OR',
41 '.': 'ANY',
42 }
43
44 def p_start(self, p):
45 '''start : fragments OR fragments
46 | fragments'''
47 if len(p) == 2:
48 p[0] = p[1]
49 else:
50 p[0] = (self.token_map[p[2]], p[1], p[3])
51
52 def p_fragments(self, p):
53 '''fragments : fragment
54 | fragment fragments'''
55 if len(p) == 2:
56 p[0] = p[1]
57 else:
58 p[0] = self.__cat(p[1], p[2])
59
60 def p_fragment(self, p):
61 '''fragment : literal maybe_modifier
62 | class maybe_modifier
63 | group maybe_modifier
64 | any maybe_modifier
65 '''
66 if p[2] != None:
67 p[0] = (p[2], p[1])
68 else:
69 p[0] = p[1]
70
71 def p_maybe_modifier(self, p):
72 '''maybe_modifier : ONE_OR_MORE
73 | ZERO_OR_ONE
74 | ZERO_OR_MORE
75 | empty'''
76 p[0] = p[1]
77 if p[1] != None:
78 p[0] = self.token_map[p[1]]
79
80 def p_literal(self, p):
81 '''literal : LITERAL'''
82 p[0] = ('LITERAL', p[1])
83
84 def p_any(self, p):
85 '''any : ANY'''
86 p[0] = (self.token_map[p[1]],)
87
88 def p_class(self, p):
89 '''class : CLASS_BEGIN class_content CLASS_END
90 | CLASS_BEGIN NOT class_content CLASS_END'''
91 if len(p) == 4:
92 p[0] = ("CLASS", p[2])
93 else:
94 p[0] = ("NOT_CLASS", p[3])
95
96 def p_group(self, p):
97 '''group : GROUP_BEGIN start GROUP_END'''
98 p[0] = p[2]
99
100 def p_class_content(self, p):
101 '''class_content : CLASS_LITERAL RANGE CLASS_LITERAL maybe_class_content
102 | CLASS_LITERAL maybe_class_content
103 '''
104 if len(p) == 5:
105 left = ("RANGE", p[1], p[3])
106 else:
107 left = ('LITERAL', p[1])
108 p[0] = self.__cat(left, p[len(p)-1])
109
110 def p_maybe_class_content(self, p):
111 '''maybe_class_content : class_content
112 | empty'''
113 p[0] = p[1]
114
115 def p_empty(self, p):
116 'empty :'
117
118 def p_error(self, p):
119 raise Exception("Syntax error in input '%s'" % p)
120
121 @staticmethod
122 def __cat(left, right):
123 if right == None:
124 return left
125 return ('CAT', left, right)
126
127 def build(self, **kwargs):
128 self.parser = yacc.yacc(module=self, **kwargs)
129 self.lexer = RegexLexer()
130 self.lexer.build(**kwargs)
131
132 def parse(self, data):
133 return self.parser.parse(data, lexer=self.lexer.lexer)
134
OLDNEW
« no previous file with comments | « tools/lexer_generator/regex_lexer.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698