| OLD | NEW |
| 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 12 matching lines...) Expand all Loading... |
| 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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 'IDENTIFIER', | 34 'IDENTIFIER', |
| 34 'STRING', | 35 'STRING', |
| 35 'REGEX', | 36 'REGEX', |
| 36 'CHARACTER_CLASS_REGEX', | 37 'CHARACTER_CLASS_REGEX', |
| 37 'TRANSITION', | |
| 38 'TRANSITION_WITH_CODE', | |
| 39 | 38 |
| 40 'PLUS', | 39 'PLUS', |
| 41 'QUESTION_MARK', | 40 'QUESTION_MARK', |
| 42 'EQUALS', | 41 'EQUALS', |
| 43 'OR', | 42 'OR', |
| 44 'STAR', | 43 'STAR', |
| 45 'LEFT_PARENTHESIS', | 44 'LEFT_PARENTHESIS', |
| 46 'RIGHT_PARENTHESIS', | 45 'RIGHT_PARENTHESIS', |
| 47 'LESS_THAN', | 46 'LESS_THAN', |
| 48 'GREATER_THAN', | 47 'GREATER_THAN', |
| 49 'SEMICOLON', | 48 'SEMICOLON', |
| 49 'ACTION_OPEN', |
| 50 'ACTION_CLOSE', |
| 50 | 51 |
| 51 'LEFT_BRACKET', | 52 'LEFT_BRACKET', |
| 52 'RIGHT_BRACKET', | 53 'RIGHT_BRACKET', |
| 53 | 54 |
| 54 'CODE_FRAGMENT', | 55 'CODE_FRAGMENT', |
| 55 ) | 56 ) |
| 56 | 57 |
| 57 states = ( | 58 states = ( |
| 58 ('code','exclusive'), | 59 ('code','exclusive'), |
| 59 ) | 60 ) |
| 60 | 61 |
| 61 t_ignore = " \t\n\r" | 62 t_ignore = " \t\n\r" |
| 62 t_code_ignore = "" | 63 t_code_ignore = "" |
| 63 | 64 |
| 64 def t_COMMENT(self, t): | 65 def t_COMMENT(self, t): |
| 65 r'\#.*[\n\r]+' | 66 r'\#.*[\n\r]+' |
| 66 pass | 67 pass |
| 67 | 68 |
| 68 t_IDENTIFIER = r'[a-zA-Z0-9_]+' | 69 def t_IDENTIFIER(self, t): |
| 70 r'[a-zA-Z][a-zA-Z0-9_]*' |
| 71 if t.value == 'default': |
| 72 t.type = 'DEFAULT' |
| 73 return t |
| 74 |
| 69 t_STRING = r'"((\\("|\w|\\))|[^\\"])+"' | 75 t_STRING = r'"((\\("|\w|\\))|[^\\"])+"' |
| 70 t_REGEX = r'/[^\/]+/' | 76 t_REGEX = r'/[^\/]+/' |
| 71 t_CHARACTER_CLASS_REGEX = r'\[([^\]]|\\\])+\]' | 77 t_CHARACTER_CLASS_REGEX = r'\[([^\]]|\\\])+\]' |
| 72 t_TRANSITION = r':=>' | |
| 73 t_TRANSITION_WITH_CODE = r'=>' | |
| 74 | 78 |
| 75 t_PLUS = r'\+' | 79 t_PLUS = r'\+' |
| 76 t_QUESTION_MARK = r'\?' | 80 t_QUESTION_MARK = r'\?' |
| 77 t_STAR = r'\*' | 81 t_STAR = r'\*' |
| 78 t_OR = r'\|' | 82 t_OR = r'\|' |
| 79 t_EQUALS = r'=' | 83 t_EQUALS = '=' |
| 80 t_LEFT_PARENTHESIS = r'\(' | 84 t_LEFT_PARENTHESIS = r'\(' |
| 81 t_RIGHT_PARENTHESIS = r'\)' | 85 t_RIGHT_PARENTHESIS = r'\)' |
| 82 t_LESS_THAN = r'<' | 86 t_LESS_THAN = '<' |
| 83 t_GREATER_THAN = r'>' | 87 t_GREATER_THAN = '>' |
| 84 t_SEMICOLON = r';' | 88 t_SEMICOLON = ';' |
| 89 t_ACTION_OPEN = '<<' |
| 90 t_ACTION_CLOSE = '>>' |
| 85 | 91 |
| 86 def t_LEFT_BRACKET(self, t): | 92 def t_LEFT_BRACKET(self, t): |
| 87 r'{' | 93 r'{' |
| 88 self.lexer.push_state('code') | 94 self.lexer.push_state('code') |
| 89 self.nesting = 1 | 95 self.nesting = 1 |
| 90 return t | 96 return t |
| 91 | 97 |
| 92 t_code_CODE_FRAGMENT = r'[^{}]+' | 98 t_code_CODE_FRAGMENT = r'[^{}]+' |
| 93 | 99 |
| 94 def t_code_LEFT_BRACKET(self, t): | 100 def t_code_LEFT_BRACKET(self, t): |
| 95 r'{' | 101 r'{' |
| 96 self.nesting += 1 | 102 self.nesting += 1 |
| 97 t.type = 'CODE_FRAGMENT' | 103 t.type = 'CODE_FRAGMENT' |
| 98 return t | 104 return t |
| 99 | 105 |
| 100 def t_code_RIGHT_BRACKET(self, t): | 106 def t_code_RIGHT_BRACKET(self, t): |
| 101 r'}' | 107 r'}' |
| 102 self.nesting -= 1 | 108 self.nesting -= 1 |
| 103 if self.nesting: | 109 if self.nesting: |
| 104 t.type = 'CODE_FRAGMENT' | 110 t.type = 'CODE_FRAGMENT' |
| 105 else: | 111 else: |
| 106 self.lexer.pop_state() | 112 self.lexer.pop_state() |
| 107 return t | 113 return t |
| 108 | 114 |
| 109 def t_ANY_error(self, t): | 115 def t_ANY_error(self, t): |
| 110 raise Exception("Illegal character '%s'" % t.value[0]) | 116 raise Exception("Illegal character '%s'" % t.value[0]) |
| 111 | 117 |
| 112 def build(self, **kwargs): | 118 def build(self, **kwargs): |
| 113 self.lexer = lex.lex(module=self, **kwargs) | 119 self.lexer = lex.lex(module=self, **kwargs) |
| OLD | NEW |