@@ -95,161 +95,6 @@ def ExpandConstants(lines, constants):
9595 return lines
9696
9797
98- def ExpandMacroDefinition (lines , pos , name_pattern , macro , expander ):
99- pattern_match = name_pattern .search (lines , pos )
100- while pattern_match is not None :
101- # Scan over the arguments
102- height = 1
103- start = pattern_match .start ()
104- end = pattern_match .end ()
105- assert lines [end - 1 ] == '('
106- last_match = end
107- arg_index = [0 ] # Wrap state into array, to work around Python "scoping"
108- mapping = { }
109- def add_arg (str ):
110- # Remember to expand recursively in the arguments
111- if arg_index [0 ] >= len (macro .args ):
112- lineno = lines .count (os .linesep , 0 , start ) + 1
113- raise Error ('line %s: Too many arguments for macro "%s"' % (lineno , name_pattern .pattern ))
114- replacement = expander (str .strip ())
115- mapping [macro .args [arg_index [0 ]]] = replacement
116- arg_index [0 ] += 1
117- while end < len (lines ) and height > 0 :
118- # We don't count commas at higher nesting levels.
119- if lines [end ] == ',' and height == 1 :
120- add_arg (lines [last_match :end ])
121- last_match = end + 1
122- elif lines [end ] in ['(' , '{' , '[' ]:
123- height = height + 1
124- elif lines [end ] in [')' , '}' , ']' ]:
125- height = height - 1
126- end = end + 1
127- # Remember to add the last match.
128- add_arg (lines [last_match :end - 1 ])
129- if arg_index [0 ] < len (macro .args ) - 1 :
130- lineno = lines .count (os .linesep , 0 , start ) + 1
131- raise Error ('line %s: Too few arguments for macro "%s"' % (lineno , name_pattern .pattern ))
132- result = macro .expand (mapping )
133- # Replace the occurrence of the macro with the expansion
134- lines = lines [:start ] + result + lines [end :]
135- pattern_match = name_pattern .search (lines , start + len (result ))
136- return lines
137-
138- def ExpandMacros (lines , macros ):
139- # We allow macros to depend on the previously declared macros, but
140- # we don't allow self-dependecies or recursion.
141- for name_pattern , macro in reversed (macros ):
142- def expander (s ):
143- return ExpandMacros (s , macros )
144- lines = ExpandMacroDefinition (lines , 0 , name_pattern , macro , expander )
145- return lines
146-
147- class TextMacro :
148- def __init__ (self , args , body ):
149- self .args = args
150- self .body = body
151- def expand (self , mapping ):
152- # Keys could be substrings of earlier values. To avoid unintended
153- # clobbering, apply all replacements simultaneously.
154- any_key_pattern = "|" .join (re .escape (k ) for k in mapping .iterkeys ())
155- def replace (match ):
156- return mapping [match .group (0 )]
157- return re .sub (any_key_pattern , replace , self .body )
158-
159- CONST_PATTERN = re .compile (r'^define\s+([a-zA-Z0-9_]+)\s*=\s*([^;]*);$' )
160- MACRO_PATTERN = re .compile (r'^macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$' )
161-
162-
163- def ReadMacros (lines ):
164- constants = []
165- macros = []
166- for line in lines .split ('\n ' ):
167- hash = line .find ('#' )
168- if hash != - 1 : line = line [:hash ]
169- line = line .strip ()
170- if len (line ) is 0 : continue
171- const_match = CONST_PATTERN .match (line )
172- if const_match :
173- name = const_match .group (1 )
174- value = const_match .group (2 ).strip ()
175- constants .append ((re .compile ("\\ b%s\\ b" % name ), value ))
176- else :
177- macro_match = MACRO_PATTERN .match (line )
178- if macro_match :
179- name = macro_match .group (1 )
180- args = [match .strip () for match in macro_match .group (2 ).split (',' )]
181- body = macro_match .group (3 ).strip ()
182- macros .append ((re .compile ("\\ b%s\\ (" % name ), TextMacro (args , body )))
183- else :
184- raise Error ("Illegal line: " + line )
185- return (constants , macros )
186-
187-
188- TEMPLATE_PATTERN = re .compile (r'^\s+T\(([A-Z][a-zA-Z0-9]*),' )
189-
190- def ReadMessageTemplates (lines ):
191- templates = []
192- index = 0
193- for line in lines .split ('\n ' ):
194- template_match = TEMPLATE_PATTERN .match (line )
195- if template_match :
196- name = "k%s" % template_match .group (1 )
197- value = index
198- index = index + 1
199- templates .append ((re .compile ("\\ b%s\\ b" % name ), value ))
200- return templates
201-
202- INLINE_MACRO_PATTERN = re .compile (r'macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*\n' )
203- INLINE_MACRO_END_PATTERN = re .compile (r'endmacro\s*\n' )
204-
205- def ExpandInlineMacros (lines ):
206- pos = 0
207- while True :
208- macro_match = INLINE_MACRO_PATTERN .search (lines , pos )
209- if macro_match is None :
210- # no more macros
211- return lines
212- name = macro_match .group (1 )
213- args = [match .strip () for match in macro_match .group (2 ).split (',' )]
214- end_macro_match = INLINE_MACRO_END_PATTERN .search (lines , macro_match .end ());
215- if end_macro_match is None :
216- raise Error ("Macro %s unclosed" % name )
217- body = lines [macro_match .end ():end_macro_match .start ()]
218-
219- # remove macro definition
220- lines = lines [:macro_match .start ()] + lines [end_macro_match .end ():]
221- name_pattern = re .compile ("\\ b%s\\ (" % name )
222- macro = TextMacro (args , body )
223-
224- # advance position to where the macro definition was
225- pos = macro_match .start ()
226-
227- def non_expander (s ):
228- return s
229- lines = ExpandMacroDefinition (lines , pos , name_pattern , macro , non_expander )
230-
231-
232- INLINE_CONSTANT_PATTERN = re .compile (r'define\s+([a-zA-Z0-9_]+)\s*=\s*([^;\n]+);\n' )
233-
234- def ExpandInlineConstants (lines ):
235- pos = 0
236- while True :
237- const_match = INLINE_CONSTANT_PATTERN .search (lines , pos )
238- if const_match is None :
239- # no more constants
240- return lines
241- name = const_match .group (1 )
242- replacement = const_match .group (2 )
243- name_pattern = re .compile ("\\ b%s\\ b" % name )
244-
245- # remove constant definition and replace
246- lines = (lines [:const_match .start ()] +
247- re .sub (name_pattern , replacement , lines [const_match .end ():]))
248-
249- # advance position to where the constant definition was
250- pos = const_match .start ()
251-
252-
25398HEADER_TEMPLATE = """\
25499 // Copyright 2011 Google Inc. All Rights Reserved.
255100
@@ -317,32 +162,16 @@ def ExpandInlineConstants(lines):
317162"""
318163
319164
320- def BuildFilterChain (macro_filename , message_template_file ):
165+ def BuildFilterChain ():
321166 """Build the chain of filter functions to be applied to the sources.
322167
323- Args:
324- macro_filename: Name of the macro file, if any.
325-
326168 Returns:
327169 A function (string -> string) that processes a source file.
328170 """
329- filter_chain = []
330-
331- if macro_filename :
332- (consts , macros ) = ReadMacros (ReadFile (macro_filename ))
333- filter_chain .append (lambda l : ExpandMacros (l , macros ))
334- filter_chain .append (lambda l : ExpandConstants (l , consts ))
335-
336- if message_template_file :
337- message_templates = ReadMessageTemplates (ReadFile (message_template_file ))
338- filter_chain .append (lambda l : ExpandConstants (l , message_templates ))
339-
340- filter_chain .extend ([
171+ filter_chain = [
341172 RemoveCommentsEmptyLinesAndWhitespace ,
342- ExpandInlineMacros ,
343- ExpandInlineConstants ,
344173 Validate ,
345- ])
174+ ]
346175
347176 def chain (f1 , f2 ):
348177 return lambda x : f2 (f1 (x ))
@@ -357,19 +186,11 @@ def __init__(self):
357186 self .names = []
358187 self .modules = []
359188
360- def IsMacroFile (filename ):
361- return filename .endswith ("macros.py" )
362-
363- def IsMessageTemplateFile (filename ):
364- return filename .endswith ("message-template.h" )
365-
366-
367189def PrepareSources (source_files , native_type , emit_js ):
368190 """Read, prepare and assemble the list of source files.
369191
370192 Args:
371- source_files: List of JavaScript-ish source files. A file named macros.py
372- will be treated as a list of macros.
193+ source_files: List of JavaScript-ish source files.
373194 native_type: String corresponding to a NativeType enum value, allowing us
374195 to treat different types of sources differently.
375196 emit_js: True if we should skip the byte conversion and just leave the
@@ -378,25 +199,7 @@ def PrepareSources(source_files, native_type, emit_js):
378199 Returns:
379200 An instance of Sources.
380201 """
381- macro_file = None
382- macro_files = filter (IsMacroFile , source_files )
383- assert len (macro_files ) in [0 , 1 ]
384- if macro_files :
385- source_files .remove (macro_files [0 ])
386- macro_file = macro_files [0 ]
387-
388- message_template_file = None
389- message_template_files = filter (IsMessageTemplateFile , source_files )
390- assert len (message_template_files ) in [0 , 1 ]
391- if message_template_files :
392- source_files .remove (message_template_files [0 ])
393- message_template_file = message_template_files [0 ]
394-
395- filters = None
396- if native_type in ("EXTRAS" ):
397- filters = BuildExtraFilterChain ()
398- else :
399- filters = BuildFilterChain (macro_file , message_template_file )
202+ filters = BuildFilterChain ()
400203
401204 source_files_and_contents = [(f , ReadFile (f )) for f in source_files ]
402205
@@ -552,7 +355,7 @@ def main():
552355 parser .set_usage ("""js2c out.cc type sources.js ...
553356 out.cc: C code to be generated.
554357 type: type parameter for NativesCollection template.
555- sources.js: JS internal sources or macros.py .""" )
358+ sources.js: JS internal sources.""" )
556359 (options , args ) = parser .parse_args ()
557360 JS2C (args [2 :],
558361 args [0 ],
0 commit comments