| Index: tools/js2c.py
|
| diff --git a/tools/js2c.py b/tools/js2c.py
|
| index f67d053ad26360c07cf190fefea4c7cb73ce6ab6..2eeca7ade3958f0bbca6ec1b36eab7bcd1d0f47f 100755
|
| --- a/tools/js2c.py
|
| +++ b/tools/js2c.py
|
| @@ -68,17 +68,6 @@ def ReadFile(filename):
|
| return lines
|
|
|
|
|
| -def ReadLines(filename):
|
| - result = []
|
| - for line in open(filename, "rt"):
|
| - if '#' in line:
|
| - line = line[:line.index('#')]
|
| - line = line.strip()
|
| - if len(line) > 0:
|
| - result.append(line)
|
| - return result
|
| -
|
| -
|
| def LoadConfigFrom(name):
|
| import ConfigParser
|
| config = ConfigParser.ConfigParser()
|
| @@ -179,14 +168,40 @@ class PythonMacro:
|
| args.append(mapping[arg])
|
| return str(self.fun(*args))
|
|
|
| +
|
| 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*([^;]*);$')
|
| +MACRO_START_PATTERN = re.compile(r'macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*\n')
|
| +MACRO_END_PATTERN = re.compile(r'endmacro\s*\n')
|
| PYTHON_MACRO_PATTERN = re.compile(r'^python\s+macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
|
|
|
|
|
| -def ReadMacros(lines):
|
| +def ReadMacros(contents):
|
| constants = []
|
| macros = []
|
| +
|
| + multiple_line_macros = []
|
| + pos = 0
|
| + while True:
|
| + macro_start_match = MACRO_START_PATTERN.search(contents, pos)
|
| + if macro_start_match is None:
|
| + # no more multiple line macros
|
| + break
|
| + name = macro_start_match.group(1)
|
| + args = [match.strip() for match in macro_start_match.group(2).split(',')]
|
| + macro_end_match = MACRO_END_PATTERN.search(contents, macro_start_match.end());
|
| + if macro_end_match is None:
|
| + raise ("Macro %s unclosed in %s" % (name, 'macros.py'))
|
| + body = contents[macro_start_match.end():macro_end_match.start()]
|
| +
|
| + # remove multiple line macro definition
|
| + contents = contents[:macro_start_match.start()] + contents[macro_end_match.end():]
|
| + # record multiple line macro definition
|
| + multiple_line_macros.append((re.compile("\\b%s\\(" % name), TextMacro(args, body)))
|
| + # advance position to where the multiple line macro defintion was
|
| + pos = macro_start_match.start()
|
| +
|
| + lines = contents.split('\n')
|
| for line in lines:
|
| hash = line.find('#')
|
| if hash != -1: line = line[:hash]
|
| @@ -214,36 +229,13 @@ def ReadMacros(lines):
|
| macros.append((re.compile("\\b%s\\(" % name), PythonMacro(args, fun)))
|
| else:
|
| raise ("Illegal line: " + line)
|
| - return (constants, macros)
|
|
|
| -INLINE_MACRO_PATTERN = re.compile(r'macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*\n')
|
| -INLINE_MACRO_END_PATTERN = re.compile(r'endmacro\s*\n')
|
| + # append multiple line macros to macros so that they will be expanded first
|
| + for m in multiple_line_macros:
|
| + macros.append(m)
|
| +
|
| + return (constants, macros)
|
|
|
| -def ExpandInlineMacros(lines, filename):
|
| - pos = 0
|
| - while True:
|
| - macro_match = INLINE_MACRO_PATTERN.search(lines, pos)
|
| - if macro_match is None:
|
| - # no more macros
|
| - return lines
|
| - name = macro_match.group(1)
|
| - args = [match.strip() for match in macro_match.group(2).split(',')]
|
| - end_macro_match = INLINE_MACRO_END_PATTERN.search(lines, macro_match.end());
|
| - if end_macro_match is None:
|
| - raise ("Macro %s unclosed in %s" % (name, filename))
|
| - body = lines[macro_match.end():end_macro_match.start()]
|
| -
|
| - # remove macro definition
|
| - lines = lines[:macro_match.start()] + lines[end_macro_match.end():]
|
| - name_pattern = re.compile("\\b%s\\(" % name)
|
| - macro = TextMacro(args, body)
|
| -
|
| - # advance position to where the macro defintion was
|
| - pos = macro_match.start()
|
| -
|
| - def non_expander(s):
|
| - return s
|
| - lines = ExpandMacroDefinition(lines, pos, name_pattern, macro, non_expander)
|
|
|
| HEADER_TEMPLATE = """\
|
| // Copyright 2011 Google Inc. All Rights Reserved.
|
| @@ -345,7 +337,7 @@ def JS2C(source, target, env):
|
| macros = []
|
| for s in source:
|
| if 'macros.py' == (os.path.split(str(s))[1]):
|
| - (consts, macros) = ReadMacros(ReadLines(str(s)))
|
| + (consts, macros) = ReadMacros(ReadFile(str(s)))
|
| else:
|
| modules.append(s)
|
|
|
| @@ -357,10 +349,9 @@ def JS2C(source, target, env):
|
| filename = str(module)
|
| debugger = filename.endswith('-debugger.js')
|
| lines = ReadFile(filename)
|
| - lines = ExpandConstants(lines, consts)
|
| lines = ExpandMacros(lines, macros)
|
| + lines = ExpandConstants(lines, consts)
|
| lines = RemoveCommentsAndTrailingWhitespace(lines)
|
| - lines = ExpandInlineMacros(lines, filename)
|
| Validate(lines, filename)
|
| lines = minifier.JSMinify(lines)
|
| id = (os.path.split(filename)[1])[:-3]
|
|
|