knit_script.knit_script_interpreter.expressions.Indexed_Expression module

Expressions associated with slicing and indexing elements.

This module provides classes for handling Python-style indexing and slicing operations in knit script expressions.

It includes support for both simple indexing and slice operations with optional step values, as well as indexed assignment operations.

class Slice_Index(start, end, spacer, parser_node)[source]

Bases: Expression

An expression that slices a given expression using python notation.

The Slice_Index class implements Python-style slice notation for knit script expressions. It supports start, end, and step parameters, allowing for flexible slicing operations on lists, strings, and other sequence types. The class creates Python slice objects that can be used with standard indexing operations.

start

The start index expression for the slice.

Type:

Expression | None

end

The end index expression for the slice.

Type:

Expression | None

spacer

The step/spacer expression for the slice.

Type:

Expression | None

__init__(start, end, spacer, parser_node)[source]

Initialize the Slice_Index expression.

Parameters:
  • start (Expression | None) – The start index expression for the slice, or None for default start.

  • end (Expression | None) – The end index expression for the slice, or None for default end.

  • spacer (Expression | None) – The step/spacer expression for the slice, or None for default step of 1.

  • parser_node (LRStackNode) – The parser node from the parse tree.

evaluate(context)[source]

Evaluate the expression to create a Python slice object.

Evaluates the start, end, and step expressions and creates a Python slice object with the resulting values. None values are preserved to allow Python’s default slicing behavior.

Parameters:

context (Knit_Script_Context) – The current context of the knit_script_interpreter.

Returns:

A Python slice object with the evaluated start, end, and step values.

Return type:

slice

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

classmethod __init_subclass__(**kwargs)

Automatically wrap evaluate methods in subclasses with appropriate error handling decorator.

This method is called whenever a class inherits from Expression. It checks if the subclass defines its own evaluate method and wraps it with the appropriate decorator.

Parameters:

**kwargs (Any) – Additional keyword arguments passed to super().__init_subclass__

property file_name: str | None

Returns: str | None: The file name of the knitscript program this was parsed from or None if the program was passed as a string.

property line_number: int

Get the line number of the symbol that generated this statement.

Returns:

The line number where this element appears in the source file.

Return type:

int

property local_path: str | None

Returns: str | None: The path to the directory containing the file from which this element was parsed or None if the value was parsed from a python string.

property location: Location

Get the location of this symbol in KnitScript file.

Returns:

The location of this symbol in the source file, including file name, line number, and position information.

Return type:

Location

property location_str: str

Returns: str: The string referencing the line number and possible file name information about this element.

property position_context: str

The position context string is the string from the knitscript program from which this element was parsed. The context string will begin at the start of this element and continue to the end of the line of knitscript or a semicolon on new line are reached.

Returns:

The string used to contextualize this element in the knitscript program.

Return type:

str

class Indexed_Expression(parser_node, item, key, assign)[source]

Bases: Expression

An expression to index into an expression using python notation.

The Indexed_Expression class implements Python-style indexing operations for knit script expressions. It supports both reading from and writing to indexed positions, handling both simple indexing and slice operations. The class provides comprehensive error handling for common indexing issues.

item

The expression to index into.

Type:

Expression

key

The index or slice expression to use for indexing.

Type:

Expression

assign

Optional assignment expression for indexed assignment operations.

Type:

Expression | None

__init__(parser_node, item, key, assign)[source]

Initialize the Indexed_Expression.

Parameters:
  • parser_node (LRStackNode) – The parser node from the parse tree.

  • item (Expression) – The expression to index into (list, dict, string, etc.).

  • key (Expression) – The index or slice expression to use for accessing the item.

  • assign (Expression | None) – Optional assignment expression for setting values at the indexed position.

evaluate(context)[source]

Evaluate the expression to perform indexing or indexed assignment.

Evaluates the item and key expressions, performs the indexing operation, and optionally handles assignment if an assignment expression is provided. Includes comprehensive error handling for common indexing issues.

Parameters:

context (Knit_Script_Context) – The current context of the knit_script_interpreter.

Returns:

The value at the indexed position, or a list of values for slice operations. For assignment operations, returns the accessed value after assignment.

Return type:

list[Any] | Any

Raises:
  • TypeError – If attempting to assign to a slice index.

  • IndexError – If the index is out of range for the item.

  • KeyError – If the key does not exist in a dictionary-like item.

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

classmethod __init_subclass__(**kwargs)

Automatically wrap evaluate methods in subclasses with appropriate error handling decorator.

This method is called whenever a class inherits from Expression. It checks if the subclass defines its own evaluate method and wraps it with the appropriate decorator.

Parameters:

**kwargs (Any) – Additional keyword arguments passed to super().__init_subclass__

property file_name: str | None

Returns: str | None: The file name of the knitscript program this was parsed from or None if the program was passed as a string.

property line_number: int

Get the line number of the symbol that generated this statement.

Returns:

The line number where this element appears in the source file.

Return type:

int

property local_path: str | None

Returns: str | None: The path to the directory containing the file from which this element was parsed or None if the value was parsed from a python string.

property location: Location

Get the location of this symbol in KnitScript file.

Returns:

The location of this symbol in the source file, including file name, line number, and position information.

Return type:

Location

property location_str: str

Returns: str: The string referencing the line number and possible file name information about this element.

property position_context: str

The position context string is the string from the knitscript program from which this element was parsed. The context string will begin at the start of this element and continue to the end of the line of knitscript or a semicolon on new line are reached.

Returns:

The string used to contextualize this element in the knitscript program.

Return type:

str