scenic.syntax.translator

Translator turning Scenic programs into Scenario objects.

The top-level interface to Scenic is provided by two functions:

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

compileStream

Compile a stream of Scenic code and execute it in a namespace.

compileTranslatedTree

constructScenarioFrom

Build a Scenario object from an executed Scenic module.

executeCodeIn

Execute the final translated Python code in the given namespace.

executePythonFunction

Execute a Python function, giving correct Scenic backtraces for any exceptions.

findConstructorsIn

Find all constructors (Scenic classes) defined in a namespace.

generateTracebackFrom

Trim an exception’s traceback to the last line of Scenic code.

hooked_import

Version of __import__ hooked by Scenic to capture Scenic modules.

parseTranslatedSource

partitionByImports

Partition the tokens into blocks ending with import statements.

peek

scenarioFromFile

Compile a Scenic file into a Scenario.

scenarioFromStream

Compile a stream of Scenic code into a Scenario.

scenarioFromString

Compile a string of Scenic code into a Scenario.

storeScenarioStateIn

Post-process an executed Scenic module, extracting state from the veneer.

topLevelNamespace

Creates an environment like that of a Python script being run directly.

translateParseTree

Modify the Python AST to produce the desired Scenic semantics.

Classes

ASTSurgeon

AttributeFinder

Utility class for finding all referenced attributes of a given name.

Constructor

InfixOp

Peekable

Utility class to allow iterator lookahead.

ScenicLoader

ScenicMetaFinder

TokenTranslator

Translates a Scenic token stream into valid Python syntax.

Exceptions

ASTParseError

Parse error occuring during modification of the Python AST.

InterpreterParseError

Parse error occuring during Python execution.

PythonParseError

Parse error occurring during Python parsing or compilation.

TokenParseError

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:

  1. Tokenize the input using the Python tokenizer.

  2. Partition the tokens into blocks separated by import statements. This is done by the partitionByImports function.

  3. Translate Scenic constructions into valid Python syntax. This is done by the TokenTranslator.

  4. Parse the resulting Python code into an AST using the Python parser.

  5. Modify the AST to achieve the desired semantics for Scenic. This is done by the translateParseTree function.

  6. Compile and execute the modified AST.

  7. 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.

partitionByImports(tokens)[source]

Partition the tokens into blocks ending with import statements.

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 Peekable(gen)[source]

Bases: object

Utility class to allow iterator lookahead.

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.

translate(tokens)[source]

Do the actual translation of the token stream.

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.

storeScenarioStateIn(namespace, requirementSyntax, filename)[source]

Post-process an executed Scenic module, extracting state from the veneer.

constructScenarioFrom(namespace)[source]

Build a Scenario object from an executed Scenic module.