Chromium Code Reviews| Index: tools/js2c.py |
| =================================================================== |
| --- tools/js2c.py (revision 2783) |
| +++ tools/js2c.py (working copy) |
| @@ -45,6 +45,13 @@ |
| return ", ".join(result) |
| +def RemoveCommentsAndTrailingWhitespace(lines): |
| + lines = re.sub(r'//.*\n', '\n', lines) # end-of-line comments |
| + lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments. |
| + lines = re.sub(r'\s+\n+', '\n', lines) # trailing whitespace |
| + return lines |
| + |
| + |
| def CompressScript(lines, do_jsmin): |
| # If we're not expecting this code to be user visible, we can run it through |
| # a more aggressive minifier. |
| @@ -55,9 +62,7 @@ |
| # people print the source code using Function.prototype.toString(). |
| # Note that we could easily compress the scripts mode but don't |
| # since we want it to remain readable. |
| - lines = re.sub('//.*\n', '\n', lines) # end-of-line comments |
| - lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments. |
| - lines = re.sub('\s+\n+', '\n', lines) # trailing whitespace |
| + lines = RemoveCommentsAndTrailingWhitespace(lines) |
| return lines |
| @@ -96,6 +101,22 @@ |
| return string |
| +EVAL_PATTERN = re.compile(r'\beval\s*\('); |
| +WITH_PATTERN = re.compile(r'\bwith\s*\('); |
| + |
| + |
| +def Validate(lines, file): |
| + lines = RemoveCommentsAndTrailingWhitespace(lines) |
| + # Because of simplified context setup, eval and with is not |
| + # allowed in the natives files. |
| + eval_match = EVAL_PATTERN.search(lines) |
| + if eval_match: |
| + raise ("Eval disallowed in natives: " + file) |
|
Christian Plesner Hansen
2009/08/31 12:42:45
The formatting operator % should be used for strin
|
| + with_match = WITH_PATTERN.search(lines) |
| + if with_match: |
| + raise ("With statements disallowed in natives: " + file) |
| + |
| + |
| def ExpandConstants(lines, constants): |
| for key, value in constants.items(): |
| lines = lines.replace(key, str(value)) |
| @@ -155,9 +176,9 @@ |
| args.append(mapping[arg]) |
| return str(self.fun(*args)) |
| -CONST_PATTERN = re.compile('^const\s+([a-zA-Z0-9_]+)\s*=\s*([^;]*);$') |
| -MACRO_PATTERN = re.compile('^macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$') |
| -PYTHON_MACRO_PATTERN = re.compile('^python\s+macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$') |
| +CONST_PATTERN = re.compile(r'^const\s+([a-zA-Z0-9_]+)\s*=\s*([^;]*);$') |
| +MACRO_PATTERN = re.compile(r'^macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$') |
| +PYTHON_MACRO_PATTERN = re.compile(r'^python\s+macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$') |
| def ReadMacros(lines): |
| constants = { } |
| @@ -281,6 +302,7 @@ |
| do_jsmin = lines.find('// jsminify this file, js2c: jsmin') != -1 |
| lines = ExpandConstants(lines, consts) |
| lines = ExpandMacros(lines, macros) |
| + Validate(lines, str(s)) |
| lines = CompressScript(lines, do_jsmin) |
| data = ToCArray(lines) |
| id = (os.path.split(str(s))[1])[:-3] |
| @@ -291,7 +313,7 @@ |
| ids.append((id, len(lines))) |
| source_lines.append(SOURCE_DECLARATION % { 'id': id, 'data': data }) |
| source_lines_empty.append(SOURCE_DECLARATION % { 'id': id, 'data': 0 }) |
| - |
| + |
| # Build delay support functions |
| get_index_cases = [ ] |
| get_script_source_cases = [ ] |