def preprocessor(self, # pylint: disable=too-many-arguments,too-many-branches token, stream, defines, include_paths, included_files): """ Handle preprocessor token """ if token.value == "define": macro = define(token, stream) if macro is not None: defines[macro.name] = macro elif token.value == "undef": undef(token, stream, defines) elif token.value in ("undefineall", "resetall"): defines.clear() elif token.value == "include": return self.include(token, stream, include_paths, included_files, defines) elif token.value in ("ifdef", "ifndef"): try: tokens = self.if_statement(token, stream, defines) return self._preprocess(tokens, defines=defines, include_paths=include_paths, included_files=included_files) except EOFException: raise LocationException.warning( "EOF reached when parsing `%s" % token.value, token.location) elif token.value in ("celldefine", "endcelldefine", "nounconnected_drive"): # Ignored pass elif token.value in ("timescale", "default_nettype", "unconnected_drive"): # Ignore directive and arguments stream.skip_until(NEWLINE) elif token.value == "pragma": stream.skip_while(WHITESPACE) pp_token = stream.pop() if pp_token.value == "protect": stream.skip_while(WHITESPACE) token = stream.pop() if token.value == "begin_protected": self._skip_protected_region(stream) elif token.value in defines: return self.expand_macro(token, stream, defines, include_paths, included_files) else: raise LocationException.debug( "Verilog undefined name", token.location) return []
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 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