| OLD | NEW |
| (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.lex as lex | |
| 29 | |
| 30 class RuleLexer: | |
| 31 | |
| 32 tokens = ( | |
| 33 'DEFAULT_ACTION', | |
| 34 'EOS', | |
| 35 'CATCH_ALL', | |
| 36 | |
| 37 'IDENTIFIER', | |
| 38 'STRING', | |
| 39 'REGEX', | |
| 40 'CHARACTER_CLASS_REGEX', | |
| 41 | |
| 42 'PLUS', | |
| 43 'QUESTION_MARK', | |
| 44 'EQUALS', | |
| 45 'OR', | |
| 46 'STAR', | |
| 47 'LEFT_PARENTHESIS', | |
| 48 'RIGHT_PARENTHESIS', | |
| 49 'GRAPH_OPEN', | |
| 50 'GRAPH_CLOSE', | |
| 51 'SEMICOLON', | |
| 52 'ACTION_OPEN', | |
| 53 'ACTION_CLOSE', | |
| 54 | |
| 55 'COMMA', | |
| 56 ) | |
| 57 | |
| 58 t_ignore = " \t\n\r" | |
| 59 | |
| 60 def t_COMMENT(self, t): | |
| 61 r'\#.*[\n\r]+' | |
| 62 pass | |
| 63 | |
| 64 __special_identifiers = set(map(lambda s: s.lower(), | |
| 65 ['DEFAULT_ACTION', 'CATCH_ALL', 'EOS'])) | |
| 66 | |
| 67 def t_IDENTIFIER(self, t): | |
| 68 r'[a-zA-Z0-9_]+' | |
| 69 if t.value in self.__special_identifiers: | |
| 70 t.type = t.value.upper() | |
| 71 return t | |
| 72 | |
| 73 t_STRING = r'"((\\("|\w|\\))|[^\\"])+"' | |
| 74 t_REGEX = r'/(\\/|[^/])+/' | |
| 75 t_CHARACTER_CLASS_REGEX = r'\[([^\]]|\\\])+\]' | |
| 76 | |
| 77 t_PLUS = r'\+' | |
| 78 t_QUESTION_MARK = r'\?' | |
| 79 t_STAR = r'\*' | |
| 80 t_OR = r'\|' | |
| 81 t_EQUALS = '=' | |
| 82 t_LEFT_PARENTHESIS = r'\(' | |
| 83 t_RIGHT_PARENTHESIS = r'\)' | |
| 84 t_GRAPH_OPEN = '<<' | |
| 85 t_GRAPH_CLOSE = '>>' | |
| 86 t_SEMICOLON = ';' | |
| 87 t_ACTION_OPEN = '<' | |
| 88 t_ACTION_CLOSE = '>' | |
| 89 t_COMMA = ',' | |
| 90 | |
| 91 def t_LEFT_BRACKET(self, t): | |
| 92 r'{' | |
| 93 self.lexer.push_state('code') | |
| 94 self.nesting = 1 | |
| 95 return t | |
| 96 | |
| 97 def t_ANY_error(self, t): | |
| 98 raise Exception("Illegal character '%s'" % t.value[0]) | |
| 99 | |
| 100 def build(self, **kwargs): | |
| 101 self.lexer = lex.lex(module=self, **kwargs) | |
| OLD | NEW |