def _pp_else(self, args): if self._num_ignoring_if > 0: return if not self._ifstack: raise PreProcessorError('#else missing #if(def)') words = args.split() if len(words) > 0: warnings.warn('Ignored tokens in #else: %s' % ', '.join(words[1:]), PreProcessorWarning) if self._elsestack[-1] == _IN_ELSE: raise PreProcessorError('#else following #else') self._elsestack[-1] = _IN_ELSE self._satisfiedstack[-1] = not self._satisfiedstack[-1]
def __iter__(self): """ Step through the preprocessed file line-by-line """ for line in self._fileobj: rematch = ppre.match(line) if rematch: cmd, args = rematch.groups() args = args or '' self._ppcmdmap[cmd](self, args) # If we defined an include file, step through it if self._includefile is not None: for line in self._includefile: yield line self._includefile.close() # We have to pass our defines back to our caller self.defines = self._includefile.defines self._includefile = None continue if self._satisfiedstack and not self._satisfiedstack[-1]: # We are inside an unsatisfied conditional continue yield _replace_defines(line, self.defines) # Make sure we don't have any dangling ifs if self._ifstack: raise PreProcessorError('EOF: Unterminated #if(def)')
def _pp_endif(self, args): if self._num_ignoring_if > 0: self._num_ignoring_if -= 1 return if args.strip(): warnings.warn('Ignored tokens in #endif: %s' % args.strip(), PreProcessorWarning) if not self._ifstack: raise PreProcessorError('#endif missing #if(def)') self._ifstack.pop() self._satisfiedstack.pop() self._elsestack.pop()
def _pp_include(self, args): if self._satisfiedstack and not self._satisfiedstack[-1]: return # Locate the include file rematch = includere.match(args) if not rematch: raise PreProcessorError('Bad #include syntax') includefile = rematch.groups()[0] self.included_files.append(includefile) for folder in self._includes: testfile = path.join(folder, includefile) if path.isfile(testfile): break else: if self._notfound_fatal: raise PreProcessorError('Could not find %s' % includefile) warnings.warn('Could not find %s; skipping' % includefile, PreProcessorWarning) self._includefile = CPreProcessor(testfile, defines=self.defines, includes=self._includes, notfound_fatal=self._notfound_fatal)
def _pp_ifndef(self, args): if self._satisfiedstack and not self._satisfiedstack[-1]: self._num_ignoring_if += 1 return words = args.split() if len(words) == 0: raise PreProcessorError('Bad #ifndef syntax: "#ifndef %s"' % args) elif len(words) > 1: warnings.warn( 'Ignored tokens in #ifndef: %s' % ', '.join(words[1:]), PreProcessorWarning) self._ifstack.append('%s not in self.defines' % words[0]) self._elsestack.append(_IN_IF) self._satisfiedstack.append(words[0] not in self.defines)
def _pp_define(self, args): if self._satisfiedstack and not self._satisfiedstack[-1]: return # Define a new variable words = args.split() if len(words) == 0: raise PreProcessorError('Nothing defined in #define') # Warn about a double-define if words[0] in self.defines: warnings.warn('%s already defined; overwriting' % words[0], PreProcessorWarning) if len(words) == 1: self.defines[words[0]] = '1' elif len(words) >= 2: self.defines[words[0]] = args[len(words[0]):].strip()
def _pp_undef(self, args): if self._satisfiedstack and not self._satisfiedstack[-1]: return # Define a new variable words = args.split() if len(words) == 1: try: del self.defines[words[0]] except KeyError: # Undefining an undefined variable is a no-op pass elif len(words) > 1: warnings.warn( 'Ignored tokens in #undef: %s' % ', '.join(words[1:]), PreProcessorWarning) elif len(words) == 0: raise PreProcessorError('Nothing defined in #undef')