scenic.syntax.translator¶
Translator turning Scenic programs into Scenario objects.
The top-level interface to Scenic is provided by two functions:
scenarioFromString
– compile a string of Scenic code;scenarioFromFile
– compile a Scenic file.
These output a Scenario
object, from which scenes can be generated.
See the documentation for Scenario
for details.
When imported, this module hooks the Python import system so that Scenic
modules can be imported using the import
statement. This is primarily for the
translator’s own use, but you could import Scenic modules from Python to
inspect them. Because Scenic uses Python’s import system, the latter’s rules
for finding modules apply, including the handling of packages.
Scenic is compiled in two main steps: translating the code into Python, and
executing the resulting Python module to generate a Scenario object encoding
the objects, distributions, etc. in the scenario. For details, see the function
compileStream
below.
Summary of Module Members¶
Functions
Compile a stream of Scenic code and execute it in a namespace. |
|
|
|
Build a Scenario object from an executed Scenic module. |
|
Execute the final translated Python code in the given namespace. |
|
Execute a Python function, giving correct Scenic backtraces for any exceptions. |
|
Find all constructors (Scenic classes) defined in a namespace. |
|
Trim an exception’s traceback to the last line of Scenic code. |
|
Version of __import__ hooked by Scenic to capture Scenic modules. |
|
|
|
Partition the tokens into blocks ending with import statements. |
|
|
|
Compile a Scenic file into a |
|
Compile a stream of Scenic code into a |
|
Compile a string of Scenic code into a |
|
Post-process an executed Scenic module, extracting state from the veneer. |
|
Creates an environment like that of a Python script being run directly. |
|
Modify the Python AST to produce the desired Scenic semantics. |
Classes
|
|
Utility class for finding all referenced attributes of a given name. |
|
Utility class to allow iterator lookahead. |
|
|
|
|
|
Translates a Scenic token stream into valid Python syntax. |
Exceptions
Parse error occuring during modification of the Python AST. |
|
Parse error occuring during Python execution. |
|
Parse error occurring during Python parsing or compilation. |
|
Parse error occurring during token translation. |
Member Details¶
-
scenarioFromString
(string, filename='<string>', cacheImports=False)[source]¶ Compile a string of Scenic code into a
Scenario
.The optional filename is used for error messages.
-
scenarioFromFile
(path, cacheImports=False)[source]¶ Compile a Scenic file into a
Scenario
.- Parameters
path (str) – path to a Scenic file
cacheImports (bool) – Whether to cache any imported Scenic modules. The default behavior is to not do this, so that subsequent attempts to import such modules will cause them to be recompiled. If it is safe to cache Scenic modules across multiple compilations, set this argument to True. Then importing a Scenic module will have the same behavior as importing a Python module.
- Returns
A
Scenario
object representing the Scenic scenario.
-
scenarioFromStream
(stream, filename='<stream>', path=None, cacheImports=False)[source]¶ Compile a stream of Scenic code into a
Scenario
.
-
topLevelNamespace
(path=None)[source]¶ Creates an environment like that of a Python script being run directly.
Specifically, __name__ is ‘__main__’, __file__ is the path used to invoke the script (not necessarily its absolute path), and the parent directory is added to the path so that ‘import blobbo’ will import blobbo from that directory if it exists there.
-
compileStream
(stream, namespace, filename='<stream>')[source]¶ Compile a stream of Scenic code and execute it in a namespace.
The compilation procedure consists of the following main steps:
Tokenize the input using the Python tokenizer.
Partition the tokens into blocks separated by import statements. This is done by the
partitionByImports
function.Translate Scenic constructions into valid Python syntax. This is done by the
TokenTranslator
.Parse the resulting Python code into an AST using the Python parser.
Modify the AST to achieve the desired semantics for Scenic. This is done by the
translateParseTree
function.Compile and execute the modified AST.
After executing all blocks, extract the global state (e.g. objects). This is done by the
storeScenarioStateIn
function.
-
class
Constructor
(name, parent, specifiers)¶ Bases:
tuple
-
_asdict
()¶ Return a new OrderedDict which maps field names to their values.
-
classmethod
_make
(iterable, new=<built-in method __new__ of type object>, len=<built-in function len>)¶ Make a new Constructor object from a sequence or iterable
-
_replace
(**kwds)¶ Return a new Constructor object replacing specified fields with new values
-
property
name
¶ Alias for field number 0
-
property
parent
¶ Alias for field number 1
-
property
specifiers
¶ Alias for field number 2
-
-
class
InfixOp
(syntax, implementation, arity, token, node)¶ Bases:
tuple
-
_asdict
()¶ Return a new OrderedDict which maps field names to their values.
-
classmethod
_make
(iterable, new=<built-in method __new__ of type object>, len=<built-in function len>)¶ Make a new InfixOp object from a sequence or iterable
-
_replace
(**kwds)¶ Return a new InfixOp object replacing specified fields with new values
-
property
arity
¶ Alias for field number 2
-
property
implementation
¶ Alias for field number 1
-
property
node
¶ Alias for field number 4
-
property
syntax
¶ Alias for field number 0
-
property
token
¶ Alias for field number 3
-
-
hooked_import
(*args, **kwargs)[source]¶ Version of __import__ hooked by Scenic to capture Scenic modules.
-
findConstructorsIn
(namespace)[source]¶ Find all constructors (Scenic classes) defined in a namespace.
-
exception
TokenParseError
(tokenOrLine, message)[source]¶ Bases:
scenic.core.utils.ParseError
Parse error occurring during token translation.
-
class
TokenTranslator
(constructors=())[source]¶ Bases:
object
Translates a Scenic token stream into valid Python syntax.
This is a stateful process because constructor (Scenic class) definitions change the way subsequent code is parsed.
-
exception
PythonParseError
[source]¶ Bases:
SyntaxError
,scenic.core.utils.ParseError
Parse error occurring during Python parsing or compilation.
-
class
AttributeFinder
(target)[source]¶ Bases:
ast.NodeVisitor
Utility class for finding all referenced attributes of a given name.
-
exception
ASTParseError
(line, message)[source]¶ Bases:
scenic.core.utils.ParseError
Parse error occuring during modification of the Python AST.
-
translateParseTree
(tree, constructors)[source]¶ Modify the Python AST to produce the desired Scenic semantics.
-
generateTracebackFrom
(exc, sourceFile)[source]¶ Trim an exception’s traceback to the last line of Scenic code.
-
exception
InterpreterParseError
(exc, line)[source]¶ Bases:
scenic.core.utils.ParseError
Parse error occuring during Python execution.
-
executeCodeIn
(code, namespace, filename)[source]¶ Execute the final translated Python code in the given namespace.
-
executePythonFunction
(func, filename)[source]¶ Execute a Python function, giving correct Scenic backtraces for any exceptions.