Cyclus Preprocessor (cyclus.cycpp
)¶
The cyclus preprocessor.
cycpp is a 3-pass preprocessor which adds reflection-like semantics to cyclus agents. This is needed to provide a high-level, user-facing API to cyclus. Code that uses cycpp is entirely valid C++ code and will compile normally even without first running it through the cycpp. This is because cycpp relies on custom #pragma decoration to annotate or inject into the code. These pragmas are skipped - by definition - by the C preprocessor and the C/C++ compiler.
The three passes of cycpp are:
run cpp normally to canonize all other preprocessor directives,
accumulate annotations for agents and state variables, and
generate code based on annotations.
All decorators have the following form:
#pragma cyclus <decorator name> [args]
The #pragma cyclus
portion is a flag so that only cycpp consumes this
#directive. This is followed by the actual <decorator name>
which tells
cycpp what to do with this pragma. Lastly, optional arguments may be passed to
this decorator but all options must be on the same logical line as the
directive. How the arguments are interpreted is a function of the decorators
themselves. Most of them are simple Python statements or expressions. See the
following handy table!
Decorator Arguments:
- var:
Add the following C++ statement as an Agent’s state variable. There is one argument which must be a Python expression that evaluates to a dictionary or other Mapping.
- exec:
Executes arbitrary python code that is passed in as the arguments and loads this into the context. This is useful for importing handy modules, declaring variables for later use, or any of the other things that Python is great for. Any variables defined here are kept in a separate namespace from the classes. Since this gives you direct access to the Python interpreter, try to be a little careful.
- note:
Merges the argument (which like with var must evalutae to a dict) with the current class level annotations. Enrties here overwrite previous entries.
cycpp is implemented entirely in this file and with tools from the Python standard library. It requires Python 2.7+ or Python 3.3+ to run.
- class cyclus.cycpp.AccessFilter(machine=None, *args, **kwargs)¶
Filter for setting the current access control flag.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('\\s*(public|private|protected)\\s*', re.DOTALL)¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.AliasFilter(machine=None, *args, **kwargs)¶
Filter for managing alias (de-)scoping.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('a^')¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.AnnotationsFilter(*args, **kwargs)¶
Filter for handling annotations() code generation: #pragma cyclus [def|decl|impl] annotations [classname]
- ctxstr(classname=None, varname=None)¶
- def_template = '\n{ind}{virt}{rtn} {ns}{methodname}({args}){sep}\n'¶
- impl(ind=' ')¶
- in_class_decl()¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- methodargs()¶
- methodname = 'annotations'¶
- methodrtn = 'Json::Value'¶
- pragmaname = 'annotations'¶
- re_template = '\\s*#\\s*pragma\\s+cyclus\\s*?(\\s+def\\s+|\\s+decl\\s+|\\s+impl\\s+|\\s*?)?(?:\\s*?{0}\\s*?)(\\s+?(?:[\\w:\\.]+)?)?'¶
- regex = re.compile('a^')¶
- revert(statement, sep)¶
Reverts state transformation.
- shapes_impl(ctx, ind=' ')¶
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.ClassAndSuperclassFilter(machine=None, *args, **kwargs)¶
This accumulates superclass information as well as class information.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('(?:\\s*template\\s*<[\\s\\w,]*>)?\\s*(?:class|struct)\\s+(\\w+)(\\s*:[\\n\\s\\w,:]+)?\\s*', re.DOTALL)¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.ClassFilter(machine=None, *args, **kwargs)¶
Filter for picking out class names.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('(?:\\s*template\\s*<[\\s\\w,]*>)?\\s*(?:class|struct)\\s+(\\w+)(\\s*:[\\n\\s\\w,:]+)?\\s*', re.DOTALL)¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.CloneFilter(*args, **kwargs)¶
Filter for handling Clone() code generation: #pragma cyclus [def|decl|impl] clone [classname]
- ctxstr(classname=None, varname=None)¶
- def_template = '\n{ind}{virt}{rtn} {ns}{methodname}({args}){sep}\n'¶
- impl(ind=' ')¶
- in_class_decl()¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- methodargs()¶
- methodname = 'Clone'¶
- methodrtn = 'cyclus::Agent*'¶
- pragmaname = 'clone'¶
- re_template = '\\s*#\\s*pragma\\s+cyclus\\s*?(\\s+def\\s+|\\s+decl\\s+|\\s+impl\\s+|\\s*?)?(?:\\s*?{0}\\s*?)(\\s+?(?:[\\w:\\.]+)?)?'¶
- regex = re.compile('a^')¶
- revert(statement, sep)¶
Reverts state transformation.
- shapes_impl(ctx, ind=' ')¶
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.CodeGenerator(context, superclasses, filename=None)¶
The CodeGenerator class is the pass 3 state machine.
This represents the file as code is being injected into it. At the end of the traversal this final stage it will built up a brand new file for pass 3. It manages both the code insertion pragmas and other bits of C++ syntax as needed to determine locality. It works by passing each statement through a sequence of filters, and injects code based on the directive and the state.
- classname()¶
Returns the current, fully-expanded class name.
- ensure_class_context(classname)¶
Ensures that the context for the class at heand is well-formed.
- generate(statement, sep)¶
Modify the existing statements list by incoprorating, modifying, or ignoring this statement, which is partitioned from the next statement by sep.
- includeloc(statement=None)¶
Current location of the file from includes as a string.
- scoped_classname(classname=None)¶
- class cyclus.cycpp.CodeGeneratorFilter(*args, **kwargs)¶
- ctxstr(classname=None, varname=None)¶
- def_template = '\n{ind}{virt}{rtn} {ns}{methodname}({args}){sep}\n'¶
- in_class_decl()¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- methodargs()¶
- re_template = '\\s*#\\s*pragma\\s+cyclus\\s*?(\\s+def\\s+|\\s+decl\\s+|\\s+impl\\s+|\\s*?)?(?:\\s*?{0}\\s*?)(\\s+?(?:[\\w:\\.]+)?)?'¶
- regex = re.compile('a^')¶
- revert(statement, sep)¶
Reverts state transformation.
- shapes_impl(ctx, ind=' ')¶
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.DecorationFilter(machine=None, *args, **kwargs)¶
Abstract class for annotation accumulation.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('a^')¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.DefaultPragmaFilter(machine=None, *args, **kwargs)¶
Filter for handling default pragma code generation: #pragma cyclus [def|decl|impl]
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('\\s*#\\s*pragma\\s+cyclus(\\s+def|\\s+decl|\\s+impl)?\\s*$', re.DOTALL)¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.ExecFilter(machine=None, *args, **kwargs)¶
Filter for executing arbitrary python code in the exec pragma and adding the results to the context. This pragma has the form:
#pragma cyclus exec <code>
Any Python statement(s) are valid as part of the code block. Be a little careful when using this pragma :).
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('#\\s*pragma\\s+cyclus\\s+exec\\s+(.*)')¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.Filter(machine=None, *args, **kwargs)¶
A basic, no-op filter.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('a^')¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.InfileToDbFilter(*args, **kwargs)¶
Filter for handling InfileToDb() code generation: #pragma cyclus [def|decl|impl] infiletodb [classname]
- ctxstr(classname=None, varname=None)¶
- def_template = '\n{ind}{virt}{rtn} {ns}{methodname}({args}){sep}\n'¶
- impl(ind=' ')¶
- in_class_decl()¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- methodargs()¶
- methodname = 'InfileToDb'¶
- methodrtn = 'void'¶
- pragmaname = 'infiletodb'¶
- re_template = '\\s*#\\s*pragma\\s+cyclus\\s*?(\\s+def\\s+|\\s+decl\\s+|\\s+impl\\s+|\\s*?)?(?:\\s*?{0}\\s*?)(\\s+?(?:[\\w:\\.]+)?)?'¶
- read_list(member, alias, t, uitype=None, ind=' ', idx=None, path='')¶
- read_map(member, alias, t, uitype=None, ind=' ', idx=None, path='')¶
- read_member(member, alias, t, uitype=None, ind=' ', idx=None, path='')¶
- read_pair(member, alias, t, uitype=None, ind=' ', idx=None, path='')¶
- read_primitive(member, alias, t, uitype=None, ind=' ', idx=None, path='')¶
- read_set(member, alias, t, uitype=None, ind=' ', idx=None, path='')¶
- read_vector(member, alias, t, uitype=None, ind=' ', idx=None, path='')¶
- regex = re.compile('a^')¶
- revert(statement, sep)¶
Reverts state transformation.
- shapes_impl(ctx, ind=' ')¶
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.InitFromCopyFilter(*args, **kwargs)¶
Filter for handling copy-constructor-like InitFrom() code generation: #pragma cyclus [def|decl|impl] initfromcopy [classname]
- ctxstr(classname=None, varname=None)¶
- def_template = '\n{ind}{virt}{rtn} {ns}{methodname}({args}){sep}\n'¶
- impl(ind=' ')¶
- in_class_decl()¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- methodargs()¶
- methodname = 'InitFrom'¶
- methodrtn = 'void'¶
- pragmaname = 'initfromcopy'¶
- re_template = '\\s*#\\s*pragma\\s+cyclus\\s*?(\\s+def\\s+|\\s+decl\\s+|\\s+impl\\s+|\\s*?)?(?:\\s*?{0}\\s*?)(\\s+?(?:[\\w:\\.]+)?)?'¶
- regex = re.compile('a^')¶
- res_impl = {'cyclus::toolkit::ResBuf': '{var}.capacity(m->{var}.capacity());\n', 'cyclus::toolkit::ResMap': '{var}.obj_ids(m->obj_ids());\n', 'cyclus::toolkit::TotalInvTracker': '{var}.capacity();\n'}¶
- revert(statement, sep)¶
Reverts state transformation.
- shapes_impl(ctx, ind=' ')¶
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.InitFromDbFilter(*args, **kwargs)¶
Filter for handling db-constructor-like InitFrom() code generation: #pragma cyclus [def|decl|impl] initfromdb [classname]
- ctxstr(classname=None, varname=None)¶
- def_template = '\n{ind}{virt}{rtn} {ns}{methodname}({args}){sep}\n'¶
- impl(ind=' ')¶
- in_class_decl()¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- methodargs()¶
- methodname = 'InitFrom'¶
- methodrtn = 'void'¶
- pragmaname = 'initfromdb'¶
- re_template = '\\s*#\\s*pragma\\s+cyclus\\s*?(\\s+def\\s+|\\s+decl\\s+|\\s+impl\\s+|\\s*?)?(?:\\s*?{0}\\s*?)(\\s+?(?:[\\w:\\.]+)?)?'¶
- regex = re.compile('a^')¶
- res_impl = {'cyclus::toolkit::ResBuf': '{var}.capacity({capacity});\n', 'cyclus::toolkit::ResMap': '{var}.obj_ids(qr.GetVal<{tstr}>("{var}"))\n;', 'cyclus::toolkit::TotalInvTracker': '{var}.capacity();\n'}¶
- revert(statement, sep)¶
Reverts state transformation.
- shapes_impl(ctx, ind=' ')¶
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.InitInvFilter(*args, **kwargs)¶
Filter for handling InitInv() code generation: #pragma cyclus [def|decl|impl] initinv [classname]
- ctxstr(classname=None, varname=None)¶
- def_template = '\n{ind}{virt}{rtn} {ns}{methodname}({args}){sep}\n'¶
- impl(ind=' ')¶
- in_class_decl()¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- methodargs()¶
- methodname = 'InitInv'¶
- methodrtn = 'void'¶
- pragmaname = 'initinv'¶
- re_template = '\\s*#\\s*pragma\\s+cyclus\\s*?(\\s+def\\s+|\\s+decl\\s+|\\s+impl\\s+|\\s*?)?(?:\\s*?{0}\\s*?)(\\s+?(?:[\\w:\\.]+)?)?'¶
- regex = re.compile('a^')¶
- res_impl = {'cyclus::toolkit::ResBuf': '{var}.Push(inv["{var}"]);\n', 'cyclus::toolkit::ResMap': '{var}.ResValues(inv["{var}"]);\n', 'cyclus::toolkit::TotalInvTracker': ';\n'}¶
- revert(statement, sep)¶
Reverts state transformation.
- shapes_impl(ctx, ind=' ')¶
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.LinemarkerFilter(machine=None, *args, **kwargs)¶
Filter for computing the current source code line from preprocessor line marker directives.
# linenum filename [flags]
This is useful for debugging. See the cpp for more info: http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html
- allowed_flags = {'1', '2'}¶
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- last_was_linemarker = False¶
- regex = re.compile('\\s*#\\s+(\\d+)\\s+"(.*?)"(\\s+\\d+)*?', re.DOTALL)¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.NamespaceAliasFilter(machine=None, *args, **kwargs)¶
Filter for accumumating namespace renames.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('\\s*namespace\\s+(\\w+)\\s*=\\s*([\\w:]+)\\s*')¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.NamespaceFilter(machine=None, *args, **kwargs)¶
Filter for accumumating namespace encapsulations.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('\\s*namespace(\\s+\\w*)?\\s*[^=]*', re.DOTALL)¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.NoteDecorationFilter(machine=None, *args, **kwargs)¶
Filter for handling annotation decoration of the form:
#pragma cyclus note <dict>
This evals the contents of dict and merges them in as the class-level annotations dict.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('\\s*#\\s*pragma\\s+cyclus\\s+note\\s+(.*)')¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- update(old, new)¶
Updates the new annotations dictionary into the old one in-place recursively.
- class cyclus.cycpp.PragmaCyclusErrorFilter(machine=None, *args, **kwargs)¶
Filter for handling invalid #pragma cyclus. This should be the last filter.
- ctxstr(classname=None, varname=None)¶
- directives = frozenset({'decl', 'def', 'exec', 'impl', 'note', 'var'})¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('\\s*#\\s*pragma\\s+cyclus(.*)')¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.Proxy(d)¶
A proxy object for scoping purposes.
- clear() None. Remove all items from D. ¶
- get(k[, d]) D[k] if k in D, else d. d defaults to None. ¶
- items() a set-like object providing a view on D's items ¶
- keys() a set-like object providing a view on D's keys ¶
- pop(k[, d]) v, remove specified key and return the corresponding value. ¶
If key is not found, d is returned if given, otherwise KeyError is raised.
- popitem() (k, v), remove and return some (key, value) pair ¶
as a 2-tuple; but raise KeyError if D is empty.
- setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D ¶
- update([E, ]**F) None. Update D from mapping/iterable E and F. ¶
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
- values() an object providing a view on D's values ¶
- class cyclus.cycpp.SchemaFilter(*args, **kwargs)¶
Filter for handling schema() code generation: #pragma cyclus [def|decl|impl] schema [classname]
- alltypes = frozenset({'ENTITIES', 'ENTITY', 'ID', 'IDREF', 'IDREFS', 'NCName', 'NMTOKEN', 'NMTOKENS', 'NOTATION', 'Name', 'QName', 'anySimpleType', 'anyType', 'anyURI', 'base64Binary', 'boolean', 'byte', 'date', 'dateTime', 'decimal', 'double', 'duration', 'float', 'gDay', 'gMonth', 'gMonthDay', 'gYear', 'gYearMonth', 'hexBinary', 'int', 'integer', 'language', 'long', 'negativeInteger', 'nonNegativeInteger', 'nonPositiveInteger', 'normalizedString', 'positiveInteger', 'short', 'string', 'time', 'token', 'unsignedByte', 'unsignedInt', 'unsignedLong', 'unsignedShort'})¶
- ctxstr(classname=None, varname=None)¶
- def_template = '\n{ind}{virt}{rtn} {ns}{methodname}({args}){sep}\n'¶
- default_types = {'': None, 'bool': 'boolean', 'boost::uuids::uuid': 'token', 'combobox': None, 'commodity': None, 'cyclus::Blob': 'string', 'double': 'double', 'facility': None, 'float': 'float', 'incommodity': None, 'inpackage': None, 'inrecipe': None, 'int': 'int', 'intransportunit': None, 'none': None, 'nuclide': 'string', 'outcommodity': None, 'outpackage': None, 'outrecipe': None, 'outtransportunit': None, 'package': None, 'prototype': None, 'range': None, 'recipe': None, 'std::string': 'string', 'transportunit': None, None: None}¶
- impl(ind=' ')¶
- in_class_decl()¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- methodargs()¶
- methodname = 'schema'¶
- methodrtn = 'std::string'¶
- pragmaname = 'schema'¶
- re_template = '\\s*#\\s*pragma\\s+cyclus\\s*?(\\s+def\\s+|\\s+decl\\s+|\\s+impl\\s+|\\s*?)?(?:\\s*?{0}\\s*?)(\\s+?(?:[\\w:\\.]+)?)?'¶
- regex = re.compile('a^')¶
- revert(statement, sep)¶
Reverts state transformation.
- shapes_impl(ctx, ind=' ')¶
- transform(statement, sep)¶
Performs a transformation given this.
- xml_from_ctx(ctx, ind=' ')¶
Creates an XML string for an agent.
- class cyclus.cycpp.SnapshotFilter(*args, **kwargs)¶
Filter for handling copy-constructor-like InitFrom() code generation: #pragma cyclus [def|decl|impl] snapshot [classname]
- ctxstr(classname=None, varname=None)¶
- def_template = '\n{ind}{virt}{rtn} {ns}{methodname}({args}){sep}\n'¶
- impl(ind=' ')¶
- in_class_decl()¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- methodargs()¶
- methodname = 'Snapshot'¶
- methodrtn = 'void'¶
- pragmaname = 'snapshot'¶
- re_template = '\\s*#\\s*pragma\\s+cyclus\\s*?(\\s+def\\s+|\\s+decl\\s+|\\s+impl\\s+|\\s*?)?(?:\\s*?{0}\\s*?)(\\s+?(?:[\\w:\\.]+)?)?'¶
- regex = re.compile('a^')¶
- res_exprs = {'cyclus::toolkit::ResBuf': None, 'cyclus::toolkit::ResMap': '{var}.obj_ids()', 'cyclus::toolkit::TotalInvTracker': None}¶
- revert(statement, sep)¶
Reverts state transformation.
- shapes_impl(ctx, ind=' ')¶
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.SnapshotInvFilter(*args, **kwargs)¶
Filter for handling SnapshotInv() code generation: #pragma cyclus [def|decl|impl] snapshotinv [classname]
- ctxstr(classname=None, varname=None)¶
- def_template = '\n{ind}{virt}{rtn} {ns}{methodname}({args}){sep}\n'¶
- impl(ind=' ')¶
- in_class_decl()¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- methodargs()¶
- methodname = 'SnapshotInv'¶
- methodrtn = 'cyclus::Inventories'¶
- pragmaname = 'snapshotinv'¶
- re_template = '\\s*#\\s*pragma\\s+cyclus\\s*?(\\s+def\\s+|\\s+decl\\s+|\\s+impl\\s+|\\s*?)?(?:\\s*?{0}\\s*?)(\\s+?(?:[\\w:\\.]+)?)?'¶
- regex = re.compile('a^')¶
- res_impl = {'cyclus::toolkit::ResBuf': 'invs["{var}"] = {var}.PopNRes({var}.count());\n{var}.Push(invs["{var}"]);\n', 'cyclus::toolkit::ResMap': 'invs["{var}"] = {var}.ResValues();\n', 'cyclus::toolkit::TotalInvTracker': ';\n'}¶
- revert(statement, sep)¶
Reverts state transformation.
- shapes_impl(ctx, ind=' ')¶
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.StateAccumulator¶
The StateAccumulator class is the pass 2 state machine.
This represents the state of the file as it is being traversed. At the end of the traversal this will have acquired all of the information needed for pass 2. It manages both the decorators and other needed bits of C++ syntax. It works by passing each statement through a sequence of filters, and builds up or destroys context as it goes.
This class also functions as a typesystem for the types it sees.
- accumulate(statement, sep)¶
Modify the existing state by incoprorating the statement, which is partitioned from the next statement by sep.
- canonize_class(cls, _usens=True)¶
This canonizes a classname. The class name need not be the current class whose scope we are in, but may be any class whatsoever. Returns None if the class could not be canonized.
- canonize_shape(ann_dict)¶
This canonizes a shape. We take a look at the current shape, and standardize its format if necessary. We append -1’s to the shape if its length does not match our expected length. Returns the new shape or raises an error if the given shape was longer than expected.
- canonize_type(t, name='<member variable>', statement=None)¶
Returns the canonical form for a type given the current state. This should not be called for types other than state variables. The name argument here is provided for debugging & reporting purposes.
- classname()¶
Returns the current, fully-expanded class name.
- ensure_class_context(classname)¶
Ensures that the context for the class at heand is well-formed.
- includeloc(statement=None)¶
Current location of the file from includes as a string.
- known_templates = {'cyclus::toolkit::ResBuf': ('T',), 'cyclus::toolkit::ResMap': ('K', 'R'), 'std::list': ('T',), 'std::map': ('Key', 'T'), 'std::pair': ('T1', 'T2'), 'std::set': ('T',), 'std::vector': ('T',)}¶
- scopz = '::'¶
- supported_types = {'bool', 'boost::uuids::uuid', 'cyclus::Blob', 'cyclus::Material', 'cyclus::Product', 'cyclus::Resource', 'cyclus::toolkit::ResBuf', 'cyclus::toolkit::ResMap', 'cyclus::toolkit::TotalInvTracker', 'double', 'float', 'int', 'std::string', ('cyclus::toolkit::ResBuf', 'cyclus::Material'), ('cyclus::toolkit::ResBuf', 'cyclus::Product'), ('cyclus::toolkit::ResBuf', 'cyclus::Resource'), ('cyclus::toolkit::TotalInvTracker', 'cyclus::toolkit::ResBuf', 'cyclus::Material')}¶
- class cyclus.cycpp.TypedefFilter(machine=None, *args, **kwargs)¶
Filter for handling typedef as aliases. Note that in-line compound typedefs of structs and unions are not supported.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('\\s*typedef\\s+(.*?\\s+.*)\\s*$')¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.UsingFilter(machine=None, *args, **kwargs)¶
Filter for accumumating using aliases.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('\\s*using\\s+(?!namespace\\s+)([\\w:]+)\\s*')¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.UsingNamespaceFilter(machine=None, *args, **kwargs)¶
Filter for accumumating using namespace statement.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('\\s*using\\s+namespace\\s+([\\w:]*)\\s*')¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- class cyclus.cycpp.VarDeclarationFilter(machine=None, *args, **kwargs)¶
State varible declaration. Only operates if state.var_annotations is not None. Access for member variable must be public.
- canonize_alias(t, name, alias=None)¶
Computes the default alias structure for a C++ type for with the given state variable name.
- canonize_tooltip(t, name, tooltip=None)¶
Computes the default tooltip structure for a C++ type for with the given state variable name.
- canonize_uilabel(t, name, uilabel=None)¶
Computes the default uilabel structure for a C++ type for with the given state variable name.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('(.*\\w+.*?)\\s+(\\w+)')¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- transform_pass2(statement, sep)¶
- transform_pass3(statement, sep)¶
- class cyclus.cycpp.VarDecorationFilter(machine=None, *args, **kwargs)¶
Filter for handling state variable decoration of the form:
#pragma cyclus var <dict>
This evals the contents of dict and puts them in state.var_annotations, to be consumed by the next match with VarDeclarationFilter.
- ctxstr(classname=None, varname=None)¶
- isvalid(statement)¶
Checks if a statement is valid for this fliter.
- regex = re.compile('\\s*#\\s*pragma\\s+cyclus\\s+var\\s+(.*)')¶
- revert(statement, sep)¶
Reverts state transformation.
- transform(statement, sep)¶
Performs a transformation given this.
- cyclus.cycpp.accumulate_state(canon)¶
Takes a canonical C++ source file and separates it out into statements which are fed into a state accumulator. The state is returned.
- cyclus.cycpp.ensure_startswith_newlinehash(x)¶
- cyclus.cycpp.escape_xml(s, ind=' ')¶
Escapes xml string s, prettifies it and puts in c++ string lit form.
- cyclus.cycpp.generate_code(orig, context, superclasses)¶
Takes a canonical C++ source file and separates it out into statements which are fed into a code generator. The new file is returned.
- cyclus.cycpp.insert_line_directives(newfile, filename)¶
Inserts line directives based on diff of original file.
- cyclus.cycpp.main()¶
- cyclus.cycpp.outter_split(s, open_brace='(', close_brace=')', separator=',')¶
Takes a string and only split the outter most level.
- cyclus.cycpp.parent_classes(classname, pdict)¶
- cyclus.cycpp.parent_intersection(classname, queryset, superclasses)¶
Returns all elements in query_set which are parents of classname and not parents of any other class in query_set
- cyclus.cycpp.parse_arg(s, open_brace='<', close_brace='>', separator=',')¶
Takes a string containing one or more c++ template args and returns a list of the argument types as strings.
This is called by parse_template to handle the inner portion of the template braces. For example:
>>> parse_arg('int, double') ['int', 'double'] >>> parse_arg('std::map<int, double>') [['std::map', 'int', 'double']] >>> parse_arg('int, std::map<int, double>') ['int', ['std::map', 'int', 'double']]
- cyclus.cycpp.parse_template(s, open_brace='<', close_brace='>', separator=',')¶
Takes a string – which may represent a template specialization – and returns the corresponding type.
It calls parse_arg to return the type(s) for any template arguments the type may have. For example:
>>> parse_template('std::map<int, double>') ['std::map', 'int', 'double'] >>> parse_template('std::map<int, std::map<int, std::map<int, int> > >') ['std::map', 'int', ['std::map', 'int', ['std::map', 'int', 'int']]]
- cyclus.cycpp.prepare_type(cpptype, othertype)¶
Updates othertype to conform to the length of cpptype using None’s.
- cyclus.cycpp.preprocess_file(filename, includes=(), cpp_path='/opt/conda/envs/cyclus/bin/x86_64-conda-linux-gnu-cpp', cpp_args=('-xc++', '-pipe', '-E', '-DCYCPP'))¶
Preprocess a file using cpp.
- Parameters:
filename : str
Name of the file you want to preprocess.
includes : list
A list of all include directories to tell the preprocessor about
cpp_path : str, optional
cpp_args : str, optional
Refer to the documentation of parse_file for the meaning of these arguments.
Notes
This was forked from pycparser: https://github.com/eliben/pycparser
- cyclus.cycpp.type_to_str(t)¶