def expand_macro( # pylint: disable=too-many-arguments self, macro_token, stream, defines, include_paths, included_files): """ Expand a macro """ macro = defines[macro_token.value] macro_point = ( strip_previous(macro_token.location), hash(frozenset(defines.keys())), ) if macro_point in self._macro_trace: raise LocationException.error( "Circular macro expansion of %s detected" % macro_token.value, macro_token.location, ) self._macro_trace.add(macro_point) tokens = self._preprocess( macro.expand_from_stream(macro_token, stream, previous=macro_token.location), defines=defines, include_paths=include_paths, included_files=included_files, ) self._macro_trace.remove(macro_point) return tokens
def include(self, token, stream, include_paths, included_files, defines): # pylint: disable=too-many-arguments """ Handle `include directive """ stream.skip_while(WHITESPACE) try: tok = stream.pop() except EOFException: raise LocationException.warning("EOF reached when parsing `include argument", token.location) if tok.kind == PREPROCESSOR: if tok.value in defines: macro = defines[tok.value] else: raise LocationException.warning("Verilog `include argument not defined", tok.location) expanded_tokens = self.expand_macro(tok, stream, defines, include_paths, included_files) if len(expanded_tokens) == 0: raise LocationException.warning( "Verilog `include has bad argument, empty define `%s" % macro.name, tok.location ) if expanded_tokens[0].kind != STRING: raise LocationException.warning("Verilog `include has bad argument", expanded_tokens[0].location) file_name_tok = expanded_tokens[0] elif tok.kind == STRING: file_name_tok = tok else: raise LocationException.warning("Verilog `include bad argument", tok.location) included_file = find_included_file(include_paths, file_name_tok.value) included_files.append((file_name_tok.value, included_file)) if included_file is None: # Is debug message since there are so many builtin includes in tools raise LocationException.debug( "Could not find `include file %s" % file_name_tok.value, file_name_tok.location ) include_point = (strip_previous(token.location), hash(frozenset(defines.keys()))) if include_point in self._include_trace: raise LocationException.error( "Circular `include of %s detected" % file_name_tok.value, file_name_tok.location ) self._include_trace.add(include_point) included_tokens = self._tokenizer.tokenize( read_file(included_file), file_name=included_file, previous_location=token.location ) included_tokens = self._preprocess(included_tokens, defines, include_paths, included_files) self._include_trace.remove(include_point) return included_tokens
def expand_macro(self, # pylint: disable=too-many-arguments macro_token, stream, defines, include_paths, included_files): """ Expand a macro """ macro = defines[macro_token.value] macro_point = (strip_previous(macro_token.location), hash(frozenset(defines.keys()))) if macro_point in self._macro_trace: raise LocationException.error( "Circular macro expansion of %s detected" % macro_token.value, macro_token.location) self._macro_trace.add(macro_point) tokens = self._preprocess(macro.expand_from_stream(macro_token, stream, previous=macro_token.location), defines=defines, include_paths=include_paths, included_files=included_files) self._macro_trace.remove(macro_point) return tokens
def include( # pylint: disable=too-many-arguments self, token, stream, include_paths, included_files, defines): """ Handle `include directive """ stream.skip_while(WHITESPACE) try: tok = stream.pop() except EOFException: raise LocationException.warning( "EOF reached when parsing `include argument", token.location) if tok.kind == PREPROCESSOR: if tok.value in defines: macro = defines[tok.value] else: raise LocationException.warning( "Verilog `include argument not defined", tok.location) expanded_tokens = self.expand_macro(tok, stream, defines, include_paths, included_files) # pylint crashes when trying to fix the warning below if len(expanded_tokens) == 0: # pylint: disable=len-as-condition raise LocationException.warning( "Verilog `include has bad argument, empty define `%s" % macro.name, tok.location, ) if expanded_tokens[0].kind != STRING: raise LocationException.warning( "Verilog `include has bad argument", expanded_tokens[0].location) file_name_tok = expanded_tokens[0] elif tok.kind == STRING: file_name_tok = tok else: raise LocationException.warning("Verilog `include bad argument", tok.location) included_file = find_included_file(include_paths, file_name_tok.value) included_files.append((file_name_tok.value, included_file)) if included_file is None: # Is debug message since there are so many builtin includes in tools raise LocationException.debug( "Could not find `include file %s" % file_name_tok.value, file_name_tok.location, ) include_point = ( strip_previous(token.location), hash(frozenset(defines.keys())), ) if include_point in self._include_trace: raise LocationException.error( "Circular `include of %s detected" % file_name_tok.value, file_name_tok.location, ) self._include_trace.add(include_point) included_tokens = self._tokenizer.tokenize( read_file(included_file), file_name=included_file, previous_location=token.location, ) included_tokens = self._preprocess(included_tokens, defines, include_paths, included_files) self._include_trace.remove(include_point) return included_tokens