Reviewers: Vyacheslav Egorov,
Description:
JS2C tweaks:
o Only substitute constant values for full-words and not for
accidental partial matches.
o Keep the order of macros as in macros.py and allow depending on the
previous macros. We used to allow depending on macros that
happened to sort after the current one in the dictionary.
Please review this at http://codereview.chromium.org/2800042/show
Affected files:
M tools/js2c.py
Index: tools/js2c.py
diff --git a/tools/js2c.py b/tools/js2c.py
index
64de7d31567b7631e494731ee6fde317a38f6449..0ee99264640157273e90755474793479d6d127c0
100755
--- a/tools/js2c.py
+++ b/tools/js2c.py
@@ -104,14 +104,29 @@ def Validate(lines, file):
def ExpandConstants(lines, constants):
- for key, value in constants.items():
- lines = lines.replace(key, str(value))
+ for key, value in constants:
+ lines = key.sub(str(value), lines)
return lines
+IDENTIFIER_PART_PATTERN = re.compile(r'[0-9a-zA-Z_]')
+
+
+def FindMacroCall(lines, name, offset):
+ start = offset
+ while True:
+ start = lines.find(name + '(', offset)
+ if start >= 0 and IDENTIFIER_PART_PATTERN.match(lines[start - 1]):
+ start += len(name) + 1
+ continue
+ return start
+
+
def ExpandMacros(lines, macros):
- for name, macro in macros.items():
- start = lines.find(name + '(', 0)
+ # We allow macros to depend on the previously declared macros, but
+ # we don't allow self-dependecies or recursion.
+ for name, macro in reversed(macros):
+ start = FindMacroCall(lines, name, 0)
while start != -1:
# Scan over the arguments
assert lines[start + len(name)] == '('
@@ -139,7 +154,7 @@ def ExpandMacros(lines, macros):
result = macro.expand(mapping)
# Replace the occurrence of the macro with the expansion
lines = lines[:start] + result + lines[end:]
- start = lines.find(name + '(', end)
+ start = FindMacroCall(lines, name, end)
return lines
class TextMacro:
@@ -167,8 +182,8 @@ 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 = { }
- macros = { }
+ constants = []
+ macros = []
for line in lines:
hash = line.find('#')
if hash != -1: line = line[:hash]
@@ -178,14 +193,14 @@ def ReadMacros(lines):
if const_match:
name = const_match.group(1)
value = const_match.group(2).strip()
- constants[name] = value
+ constants.append((re.compile("\\b%s\\b" % name), value))
else:
macro_match = MACRO_PATTERN.match(line)
if macro_match:
name = macro_match.group(1)
args = map(string.strip, macro_match.group(2).split(','))
body = macro_match.group(3).strip()
- macros[name] = TextMacro(args, body)
+ macros.append((name, TextMacro(args, body)))
else:
python_match = PYTHON_MACRO_PATTERN.match(line)
if python_match:
@@ -193,7 +208,7 @@ def ReadMacros(lines):
args = map(string.strip, python_match.group(2).split(','))
body = python_match.group(3).strip()
fun = eval("lambda " + ",".join(args) + ': ' + body)
- macros[name] = PythonMacro(args, fun)
+ macros.append((name, PythonMacro(args, fun)))
else:
raise ("Illegal line: " + line)
return (constants, macros)
@@ -293,6 +308,7 @@ def JS2C(source, target, env):
lines = ExpandMacros(lines, macros)
Validate(lines, filename)
lines = minifier.JSMinify(lines)
+ print lines
data = ToCArray(lines)
id = (os.path.split(filename)[1])[:-3]
if debugger: id = id[:-9]
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev