~ubuntu-branches/ubuntu/precise/nodejs/precise

« back to all changes in this revision

Viewing changes to deps/v8/tools/js2c.py

  • Committer: Bazaar Package Importer
  • Author(s): Jérémy Lal
  • Date: 2010-08-20 11:49:04 UTC
  • mfrom: (7.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100820114904-lz22w6fkth7yh179
Tags: 0.2.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
104
104
 
105
105
 
106
106
def ExpandConstants(lines, constants):
107
 
  for key, value in constants.items():
108
 
    lines = lines.replace(key, str(value))
 
107
  for key, value in constants:
 
108
    lines = key.sub(str(value), lines)
109
109
  return lines
110
110
 
111
111
 
112
112
def ExpandMacros(lines, macros):
113
 
  for name, macro in macros.items():
114
 
    start = lines.find(name + '(', 0)
115
 
    while start != -1:
 
113
  # We allow macros to depend on the previously declared macros, but
 
114
  # we don't allow self-dependecies or recursion.
 
115
  for name_pattern, macro in reversed(macros):
 
116
    pattern_match = name_pattern.search(lines, 0)
 
117
    while pattern_match is not None:
116
118
      # Scan over the arguments
117
 
      assert lines[start + len(name)] == '('
118
119
      height = 1
119
 
      end = start + len(name) + 1
 
120
      start = pattern_match.start()
 
121
      end = pattern_match.end()
 
122
      assert lines[end - 1] == '('
120
123
      last_match = end
121
124
      arg_index = 0
122
125
      mapping = { }
139
142
      result = macro.expand(mapping)
140
143
      # Replace the occurrence of the macro with the expansion
141
144
      lines = lines[:start] + result + lines[end:]
142
 
      start = lines.find(name + '(', end)
 
145
      pattern_match = name_pattern.search(lines, start + len(result))
143
146
  return lines
144
147
 
145
148
class TextMacro:
166
169
MACRO_PATTERN = re.compile(r'^macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
167
170
PYTHON_MACRO_PATTERN = re.compile(r'^python\s+macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
168
171
 
 
172
 
169
173
def ReadMacros(lines):
170
 
  constants = { }
171
 
  macros = { }
 
174
  constants = []
 
175
  macros = []
172
176
  for line in lines:
173
177
    hash = line.find('#')
174
178
    if hash != -1: line = line[:hash]
178
182
    if const_match:
179
183
      name = const_match.group(1)
180
184
      value = const_match.group(2).strip()
181
 
      constants[name] = value
 
185
      constants.append((re.compile("\\b%s\\b" % name), value))
182
186
    else:
183
187
      macro_match = MACRO_PATTERN.match(line)
184
188
      if macro_match:
185
189
        name = macro_match.group(1)
186
190
        args = map(string.strip, macro_match.group(2).split(','))
187
191
        body = macro_match.group(3).strip()
188
 
        macros[name] = TextMacro(args, body)
 
192
        macros.append((re.compile("\\b%s\\(" % name), TextMacro(args, body)))
189
193
      else:
190
194
        python_match = PYTHON_MACRO_PATTERN.match(line)
191
195
        if python_match:
193
197
          args = map(string.strip, python_match.group(2).split(','))
194
198
          body = python_match.group(3).strip()
195
199
          fun = eval("lambda " + ",".join(args) + ': ' + body)
196
 
          macros[name] = PythonMacro(args, fun)
 
200
          macros.append((re.compile("\\b%s\\(" % name), PythonMacro(args, fun)))
197
201
        else:
198
202
          raise ("Illegal line: " + line)
199
203
  return (constants, macros)
271
275
  debugger_ids = []
272
276
  modules = []
273
277
  # Locate the macros file name.
274
 
  consts = {}
275
 
  macros = {}
 
278
  consts = []
 
279
  macros = []
276
280
  for s in source:
277
281
    if 'macros.py' == (os.path.split(str(s))[1]):
278
282
      (consts, macros) = ReadMacros(ReadLines(str(s)))