| 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', | |
| 34 'DEFAULT_ACTION', | 33 'DEFAULT_ACTION', |
| 35 'CATCH_ALL', | 34 'CATCH_ALL', |
| 36 'PUSH_TOKEN', | |
| 37 | 35 |
| 38 'IDENTIFIER', | 36 'IDENTIFIER', |
| 39 'STRING', | 37 'STRING', |
| 40 'REGEX', | 38 'REGEX', |
| 41 'CHARACTER_CLASS_REGEX', | 39 'CHARACTER_CLASS_REGEX', |
| 42 | 40 |
| 43 'PLUS', | 41 'PLUS', |
| 44 'QUESTION_MARK', | 42 'QUESTION_MARK', |
| 45 'EQUALS', | 43 'EQUALS', |
| 46 'OR', | 44 'OR', |
| 47 'STAR', | 45 'STAR', |
| 48 'LEFT_PARENTHESIS', | 46 'LEFT_PARENTHESIS', |
| 49 'RIGHT_PARENTHESIS', | 47 'RIGHT_PARENTHESIS', |
| 50 'LESS_THAN', | 48 'GRAPH_OPEN', |
| 51 'GREATER_THAN', | 49 'GRAPH_CLOSE', |
| 52 'SEMICOLON', | 50 'SEMICOLON', |
| 53 'ACTION_OPEN', | 51 'ACTION_OPEN', |
| 54 'ACTION_CLOSE', | 52 'ACTION_CLOSE', |
| 55 | 53 |
| 56 'LEFT_BRACKET', | 54 'LEFT_BRACKET', |
| 57 'RIGHT_BRACKET', | 55 'RIGHT_BRACKET', |
| 58 | 56 |
| 59 'CODE_FRAGMENT', | 57 'CODE_FRAGMENT', |
| 60 ) | 58 ) |
| 61 | 59 |
| 62 states = ( | 60 states = ( |
| 63 ('code','exclusive'), | 61 ('code','exclusive'), |
| 64 ) | 62 ) |
| 65 | 63 |
| 66 t_ignore = " \t\n\r" | 64 t_ignore = " \t\n\r" |
| 67 t_code_ignore = "" | 65 t_code_ignore = "" |
| 68 | 66 |
| 69 def t_COMMENT(self, t): | 67 def t_COMMENT(self, t): |
| 70 r'\#.*[\n\r]+' | 68 r'\#.*[\n\r]+' |
| 71 pass | 69 pass |
| 72 | 70 |
| 73 __special_identifiers = set(map(lambda s: s.lower(), | 71 __special_identifiers = set(map(lambda s: s.lower(), |
| 74 ['DEFAULT', 'DEFAULT_ACTION', 'CATCH_ALL', 'PUSH_TOKEN'])) | 72 ['DEFAULT_ACTION', 'CATCH_ALL'])) |
| 75 | 73 |
| 76 def t_IDENTIFIER(self, t): | 74 def t_IDENTIFIER(self, t): |
| 77 r'[a-zA-Z][a-zA-Z0-9_]*' | 75 r'[a-zA-Z][a-zA-Z0-9_]*' |
| 78 if t.value in self.__special_identifiers: | 76 if t.value in self.__special_identifiers: |
| 79 t.type = t.value.upper() | 77 t.type = t.value.upper() |
| 80 return t | 78 return t |
| 81 | 79 |
| 82 t_STRING = r'"((\\("|\w|\\))|[^\\"])+"' | 80 t_STRING = r'"((\\("|\w|\\))|[^\\"])+"' |
| 83 t_REGEX = r'/(\\/|[^/])+/' | 81 t_REGEX = r'/(\\/|[^/])+/' |
| 84 t_CHARACTER_CLASS_REGEX = r'\[([^\]]|\\\])+\]' | 82 t_CHARACTER_CLASS_REGEX = r'\[([^\]]|\\\])+\]' |
| 85 | 83 |
| 86 t_PLUS = r'\+' | 84 t_PLUS = r'\+' |
| 87 t_QUESTION_MARK = r'\?' | 85 t_QUESTION_MARK = r'\?' |
| 88 t_STAR = r'\*' | 86 t_STAR = r'\*' |
| 89 t_OR = r'\|' | 87 t_OR = r'\|' |
| 90 t_EQUALS = '=' | 88 t_EQUALS = '=' |
| 91 t_LEFT_PARENTHESIS = r'\(' | 89 t_LEFT_PARENTHESIS = r'\(' |
| 92 t_RIGHT_PARENTHESIS = r'\)' | 90 t_RIGHT_PARENTHESIS = r'\)' |
| 93 t_LESS_THAN = '<' | 91 t_GRAPH_OPEN = '<<' |
| 94 t_GREATER_THAN = '>' | 92 t_GRAPH_CLOSE = '>>' |
| 95 t_SEMICOLON = ';' | 93 t_SEMICOLON = ';' |
| 96 t_ACTION_OPEN = '<<' | 94 t_ACTION_OPEN = '<' |
| 97 t_ACTION_CLOSE = '>>' | 95 t_ACTION_CLOSE = '>' |
| 98 | 96 |
| 99 def t_LEFT_BRACKET(self, t): | 97 def t_LEFT_BRACKET(self, t): |
| 100 r'{' | 98 r'{' |
| 101 self.lexer.push_state('code') | 99 self.lexer.push_state('code') |
| 102 self.nesting = 1 | 100 self.nesting = 1 |
| 103 return t | 101 return t |
| 104 | 102 |
| 105 t_code_CODE_FRAGMENT = r'[^{}]+' | 103 t_code_CODE_FRAGMENT = r'[^{}]+' |
| 106 | 104 |
| 107 def t_code_LEFT_BRACKET(self, t): | 105 def t_code_LEFT_BRACKET(self, t): |
| 108 r'{' | 106 r'{' |
| 109 self.nesting += 1 | 107 self.nesting += 1 |
| 110 t.type = 'CODE_FRAGMENT' | 108 t.type = 'CODE_FRAGMENT' |
| 111 return t | 109 return t |
| 112 | 110 |
| 113 def t_code_RIGHT_BRACKET(self, t): | 111 def t_code_RIGHT_BRACKET(self, t): |
| 114 r'}' | 112 r'}' |
| 115 self.nesting -= 1 | 113 self.nesting -= 1 |
| 116 if self.nesting: | 114 if self.nesting: |
| 117 t.type = 'CODE_FRAGMENT' | 115 t.type = 'CODE_FRAGMENT' |
| 118 else: | 116 else: |
| 119 self.lexer.pop_state() | 117 self.lexer.pop_state() |
| 120 return t | 118 return t |
| 121 | 119 |
| 122 def t_ANY_error(self, t): | 120 def t_ANY_error(self, t): |
| 123 raise Exception("Illegal character '%s'" % t.value[0]) | 121 raise Exception("Illegal character '%s'" % t.value[0]) |
| 124 | 122 |
| 125 def build(self, **kwargs): | 123 def build(self, **kwargs): |
| 126 self.lexer = lex.lex(module=self, **kwargs) | 124 self.lexer = lex.lex(module=self, **kwargs) |
| OLD | NEW |