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

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

Issue 69293005: Experimental parser: add catch all rule (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
OLDNEW
1 # Copyright 2013 the V8 project authors. All rights reserved. 1 # Copyright 2013 the V8 project authors. All rights reserved.
2 # Redistribution and use in source and binary forms, with or without 2 # Redistribution and use in source and binary forms, with or without
3 # modification, are permitted provided that the following conditions are 3 # modification, are permitted provided that the following conditions are
4 # met: 4 # met:
5 # 5 #
6 # * Redistributions of source code must retain the above copyright 6 # * Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer. 7 # notice, this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above 8 # * Redistributions in binary form must reproduce the above
9 # copyright notice, this list of conditions and the following 9 # copyright notice, this list of conditions and the following
10 # disclaimer in the documentation and/or other materials provided 10 # disclaimer in the documentation and/or other materials provided
(...skipping 14 matching lines...) Expand all
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 import ply.lex as lex 28 import ply.lex as lex
29 29
30 class RuleLexer: 30 class RuleLexer:
31 31
32 tokens = ( 32 tokens = (
33 'DEFAULT', 33 'DEFAULT',
34 'DEFAULT_ACTION', 34 'DEFAULT_ACTION',
35 'CATCH_ALL',
36
35 'IDENTIFIER', 37 'IDENTIFIER',
36 'STRING', 38 'STRING',
37 'REGEX', 39 'REGEX',
38 'CHARACTER_CLASS_REGEX', 40 'CHARACTER_CLASS_REGEX',
39 41
40 'PLUS', 42 'PLUS',
41 'QUESTION_MARK', 43 'QUESTION_MARK',
42 'EQUALS', 44 'EQUALS',
43 'OR', 45 'OR',
44 'STAR', 46 'STAR',
(...skipping 15 matching lines...) Expand all
60 ('code','exclusive'), 62 ('code','exclusive'),
61 ) 63 )
62 64
63 t_ignore = " \t\n\r" 65 t_ignore = " \t\n\r"
64 t_code_ignore = "" 66 t_code_ignore = ""
65 67
66 def t_COMMENT(self, t): 68 def t_COMMENT(self, t):
67 r'\#.*[\n\r]+' 69 r'\#.*[\n\r]+'
68 pass 70 pass
69 71
72 __special_identifiers = set(map(lambda s: s.lower(),
73 ['DEFAULT', 'DEFAULT_ACTION', 'CATCH_ALL',]))
74
70 def t_IDENTIFIER(self, t): 75 def t_IDENTIFIER(self, t):
71 r'[a-zA-Z][a-zA-Z0-9_]*' 76 r'[a-zA-Z][a-zA-Z0-9_]*'
72 if t.value == 'default': 77 if t.value in self.__special_identifiers:
73 t.type = 'DEFAULT' 78 t.type = t.value.upper()
74 if t.value == 'default_action':
75 t.type = 'DEFAULT_ACTION'
76 return t 79 return t
77 80
78 t_STRING = r'"((\\("|\w|\\))|[^\\"])+"' 81 t_STRING = r'"((\\("|\w|\\))|[^\\"])+"'
79 t_REGEX = r'/[^\/]+/' 82 t_REGEX = r'/[^\/]+/'
80 t_CHARACTER_CLASS_REGEX = r'\[([^\]]|\\\])+\]' 83 t_CHARACTER_CLASS_REGEX = r'\[([^\]]|\\\])+\]'
81 84
82 t_PLUS = r'\+' 85 t_PLUS = r'\+'
83 t_QUESTION_MARK = r'\?' 86 t_QUESTION_MARK = r'\?'
84 t_STAR = r'\*' 87 t_STAR = r'\*'
85 t_OR = r'\|' 88 t_OR = r'\|'
(...skipping 27 matching lines...) Expand all
113 t.type = 'CODE_FRAGMENT' 116 t.type = 'CODE_FRAGMENT'
114 else: 117 else:
115 self.lexer.pop_state() 118 self.lexer.pop_state()
116 return t 119 return t
117 120
118 def t_ANY_error(self, t): 121 def t_ANY_error(self, t):
119 raise Exception("Illegal character '%s'" % t.value[0]) 122 raise Exception("Illegal character '%s'" % t.value[0])
120 123
121 def build(self, **kwargs): 124 def build(self, **kwargs):
122 self.lexer = lex.lex(module=self, **kwargs) 125 self.lexer = lex.lex(module=self, **kwargs)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698