MacroEnv

This an environment of macro declarations

It implements ISO/IEC 9899:1999(E) section 6 (aka ‘C’) and ISO/IEC 14882:1998(E) section 16 (aka ‘C++’)

exception cpip.core.MacroEnv.ExceptionMacroEnv

Exception when handling MacroEnv object.

exception cpip.core.MacroEnv.ExceptionMacroEnvInvalidRedefinition

Exception for a invalid redefinition of a macro. NOTE: Under C rules (C Rationale 6.10.3) callers should merely issue a suitable diagnostic.

exception cpip.core.MacroEnv.ExceptionMacroEnvNoMacroDefined

Exception when trying to access a PpDefine that is not currently defined.

exception cpip.core.MacroEnv.ExceptionMacroIndexError

Exception when an access to a PpDefine that generates a IndexError.

exception cpip.core.MacroEnv.ExceptionMacroReplacementInit

Exception in the constructor.

exception cpip.core.MacroEnv.ExceptionMacroReplacementPredefinedRedefintion

Exception for a redefinition of a macro id that is predefined.

class cpip.core.MacroEnv.MacroEnv(enableTrace=False, stdPredefMacros=None)

Represents a set of #define directives that represent a macro processing environment. This provides support for #define and #undef directives. It also provides support for macro replacement see: ISO/IEC 9899:1999 (E) 6.10.3 Macro replacement.

enableTrace allows calls to _debugTokenStream() that may or may not produce log output (depending on logging level). If True this makes this code run slower, typically 3x slower

stdPredefMacros if present should be a dictionary of: {identifier : replacement_string_\n_terminated, ...} For example:

{
    '__DATE__' : 'First of June\n',
    '__TIME__' : 'Just before lunchtime.\n',
}

Each identifier must be in STD_PREDEFINED_NAMES

allStaticMacroDependencies()

Returns a DuplexAdjacencyList() of macro dependencies for the Macro environment. All objects in the DuplexAdjacencyList() are macro identifiers as strings. A DuplexAdjacencyList() can be converted to a util.Tree() and that can be converted to a DictTree()

clear()

Clears the macro environment.

define(theGen, theFile, theLine)

Defines a macro. theGen should be in the state immediately after the #define i.e. this will consume leading whitespace and the trailing newline.

Will raise a ExceptionMacroEnvInvalidRedefinition if the redefinition is not valid. May raise a ExceptionCpipDefineInit (or sub class) on failure.

On success it returns the identifier of the macro as a string.. The insertion is stable i.e. a valid re-definition does not replace the existing definition so that the existing state of the macro definition (file, line, reference count etc. are preserved.

defined(theTtt, flagInvert, theFileLineCol=None)

If the PpToken theTtt is an identifier that is currently defined then this returns 1 as a PpToken, 0 as a PpToken otherwise. If the macro exists in the environment its reference count is incremented. See: ISO/IEC 9899:1999 (E) 6.10.1. theFileLineCol is a FileLocation.FileLineCol object.

genMacros(theIdentifier=None)

Generates PpDefine objects encountered during my existence. Macros that have been undefined will be generated first in order of un-definition followed by the currently defined macros in identifier order. Macros that have been #undef’d will have the attribute isCurrentlyDefined as False.

genMacrosInScope(theIdent=None)

Generates PpDefine objects encountered during my existence and still in scope i.e. not yet un-defined. If theIdent is not None then only that named macros will be yielded.

genMacrosOutOfScope(theIdent=None)

Generates PpDefine objects encountered during my existence but then undefined in the order of un-definition. If theIdent is not None then only that named macros will be yielded.

getUndefMacro(theIdx)

Returns the PpDefine object from the undef list for the given index. Will raise an ExceptionMacroIndexError if the index is out of range.

hasMacro(theIdentifier)

Returns True if the environment has the macro. NOTE: This does _not_ increment the reference count so should not be used when processing #ifdef ..., #if defined ... or #if !defined ... for those use isDefined() and defined() instead.

isDefined(theTtt, theFileLineCol=None)

Returns True theTtt is an identifier that is currently defined, False otherwise. If True this increments the macro reference. See: ISO/IEC 9899:1999 (E) 6.10.1. theFileLineCol is a FileLocation.FileLineCol object.

macro(theIdentifier)

Returns the macro identified by the identifier. Will raise a ExceptionMacroEnvNoMacroDefined is undefined.

macroHistory(incEnv=True, onlyRef=True)

Returns the macro history as a multi-line string

macroHistoryMap()

Returns a map of {ident : ([ints, ...], True/False), ...} Where the macro identifier is mapped to a pair where: pair[0] is a list of indexes into getUndefMacro(). pair[1] is boolean, True if the identifier is currently defined i.e. it is the value ofself.hasMacro(ident). The macro can be obtained by self.macro().

macroNotDefinedDependencies()

Returns a map of {identifier : [class FileLineColumn, ...], ...} where there has been an #ifdef and nothing is defined. Thus these macros, if present, could alter the outcome i.e. it is dependency on them NOT being defined.

macroNotDefinedDependencyNames()

Returns an unsorted list of identifies where there has been an #ifdef and nothing is defined. Thus these macros, if present, could alter the outcome i.e. it is dependency on them NOT being defined.

macroNotDefinedDependencyReferences(theIdentifier)

Returns an ordered list of class FileLineColumn for an identifier where there has been an #ifdef and nothing is defined. Thus these macros, if present, could alter the outcome i.e. it is dependency on them NOT being defined.

macros()

Returns and unsorted list of strings of current macro identifiers.

mightReplace(theTtt)

Returns True if theTok might be able to be expanded. ‘Might’ is not ‘can’ or ‘will’ because of this:

#define FUNC(a,b) a-b
FUNC FUNC(45,3)

Becomes:

FUNC 45 -3

Thus mightReplace('FUNC', ...) is True in both cases but actual replacement only occurs once for the second FUNC.

referencedMacroIdentifiers(sortedByRefcount=False)

Returns an unsorted list of macro identifiers that have a reference count > 0. If sortedByRefcount is True the list will be in increasing order of reference count then by name. Use reverse() on the result to get decreasing order. If sortedByRefcount is False the return value is unsorted.

replace(theTtt, theGen, theFileLineCol=None)

Given a PpToken this returns the replacement as a list of [class PpToken, ...] that is the result of the substitution of macro definitions. theGen is a generator that might be used in the case of function-like macros to consume their argument lists. theFileLineCol is a FileLocation.FileLineCol object.

set__FILE__(theStr)

This sets the __FILE__ macro directly.

set__LINE__(theStr)

This sets the __LINE__ macro directly.

undef(theGen, theFile, theLine)

Removes a definition from the map and adds the PpDefine to self._undefS. It returns None. If no definition exists this has no side-effects on the internal representation.

Previous topic

ItuToTokens

Next topic

PpDefine

This Page