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

Unified Diff: tools/lexer_generator/regex_lexer.py

Issue 66313005: Experimental parser: better escaping (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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/lexer_generator/generator.py ('k') | tools/lexer_generator/rule_parser.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/lexer_generator/regex_lexer.py
diff --git a/tools/lexer_generator/regex_lexer.py b/tools/lexer_generator/regex_lexer.py
index 78f36484c1ab85e347fbcad89a9083e2452afaf8..ea5b3e06aa254072d95db4bf5ed8243df03928a0 100644
--- a/tools/lexer_generator/regex_lexer.py
+++ b/tools/lexer_generator/regex_lexer.py
@@ -27,6 +27,13 @@
import ply.lex as lex
+def build_escape_map(chars):
+ def add_escape(d, char):
+ d['\\' + char] = char
+ return d
+ return reduce(add_escape, chars,
+ {'\\t' : '\t', '\\r' : '\r', '\\n' : '\n', '\\v' : '\v', '\\f' : '\f'})
+
class RegexLexer:
tokens = (
@@ -62,10 +69,12 @@ class RegexLexer:
('repeat','exclusive'),
)
+ __escaped_literals = build_escape_map("(){}[]?+.*|\\")
+
def t_ESCAPED_LITERAL(self, t):
- r'\\\(|\\\)|\\\[|\\\]|\\\||\\\+|\\\*|\\\?|\\\.|\\\\|\\\{|\\\}'
+ r'\\.'
t.type = 'LITERAL'
- t.value = t.value[1:]
+ t.value = RegexLexer.__escaped_literals[t.value]
return t
t_GROUP_BEGIN = r'\('
@@ -98,15 +107,12 @@ class RegexLexer:
r'\\\d+'
return t
- escaped_class_literals = {
- '\\t' : '\t', '\\r' : '\r', '\\n' : '\n', '\\v' : '\v', '\\f' : '\f',
- '\\^' : '^', '\\[' : '[', '\\]' : ']', '\\-' : '-', '\\:' : ':',
- }
+ __escaped_class_literals = build_escape_map("^[]-:")
def t_class_ESCAPED_CLASS_LITERAL(self, t):
- r'\\\^|\\-|\\\[|\\\]|\\\:|\\\w'
+ r'\\.'
t.type = 'CLASS_LITERAL'
- t.value = RegexLexer.escaped_class_literals[t.value]
+ t.value = RegexLexer.__escaped_class_literals[t.value]
return t
t_class_CLASS_LITERAL = r'[\w $_+]'
« no previous file with comments | « tools/lexer_generator/generator.py ('k') | tools/lexer_generator/rule_parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698