KLayout 0.29.8 (2024-11-26 926dac96c) [master]

API reference - Class MacroInterpreter

Notation used in Ruby API documentation

Module: lay

Description: A custom interpreter for a DSL (domain specific language)

DSL interpreters are a way to provide macros written in a language specific for the application. One example are DRC scripts which are written in some special language optimized for DRC ruledecks. Interpreters for such languages can be built using scripts itself by providing the interpreter implementation through this object.

An interpreter implementation involves at least these steps:

Template macros provide a way for the macro editor to present macros for the new interpreter in the list of templates. Template macros can provide menu bindings, shortcuts and some initial text for example

The simple implementation can be enhanced by providing more information, i.e. syntax highlighter information, the debugger to use etc. This involves reimplementing further methods, i.e. "syntax_scheme".

This is a simple example for an interpreter in Ruby. Is is registered under the name 'simple-dsl' and just evaluates the script text:

class SimpleExecutable < RBA::Excutable

  # Constructor
  def initialize(macro)
    @macro = macro
  end
  
  # Implements the execute method
  def execute
    eval(@macro.text, nil, @macro.path)
    nil
  end

end

class SimpleInterpreter < RBA::MacroInterpreter

  # Constructor
  def initialize
    self.description = "A test interpreter"
    # Registers the new interpreter
    register("simple-dsl")
    # create a template for the macro editor:
    # Name is "new_simple", the description will be "Simple interpreter macro"
    # in the "Special" group.
    mt = create_template("new_simple")
    mt.description = "Special;;Simple interpreter macro"
  end
  
  # Creates the executable delegate
  def executable(macro)
    SimpleExecutable::new(macro)
  end

end

# Register the new interpreter
SimpleInterpreter::new

Please note that such an implementation is dangerous because the evaluation of the script happens in the context of the interpreter object. In this implementation the script could redefine the execute method for example. This implementation is provided as an example only. A real implementation should add execution of prolog and epilog code inside the execute method and proper error handling.

In order to make the above code effective, store the code in an macro, set "early auto-run" and restart KLayout.

This class has been introduced in version 0.23 and modified in 0.27.

Public constructors

new MacroInterpreter ptrnewCreates a new object of this class

Public methods

[const]MacroInterpreter ptr_const_castReturns a non-const reference to self.
void_createEnsures the C++ object is created
void_destroyExplicitly destroys the object
[const]bool_destroyed?Returns a value indicating whether the object was already destroyed
[const]bool_is_const_object?Returns a value indicating whether the reference is a const reference
void_manageMarks the object as managed by the script side.
void_unmanageMarks the object as no longer owned by the script side.
voidassign(const MacroInterpreter other)Assigns another object to self
Macro ptrcreate_template(string url)Creates a new macro template
voiddebugger_scheme=(Macro::Interpreter scheme)Sets the debugger scheme (which debugger to use for the DSL macro)
voiddescription=(string description)Sets a description string
[const]new MacroInterpreter ptrdupCreates a copy of self
[virtual,const]Executable ptrexecutable(const Macro ptr macro)Returns the executable object which implements the macro execution
string[]include_expansion(Macro ptr macro)Provides include expansion as defined by the interpreter
voidregister(string name)Registers the macro interpreter
voidstorage_scheme=(Macro::Format scheme)Sets the storage scheme (the format as which the macro is stored)
voidsuffix=(string suffix)Sets the file suffix
voidsupports_include_expansion=(bool flag)Sets a value indicating whether this interpreter supports the default include file expansion scheme.
voidsyntax_scheme=(string scheme)Sets a string indicating the syntax highlighter scheme

Public static methods and constants

[static,const]Macro::FormatMacroFormatThe macro has macro (XML) format
Macro::InterpreterNoDebuggerIndicates no debugging for debugger_scheme
[static,const]Macro::FormatPlainTextFormatThe macro has plain text format
[static,const]Macro::FormatPlainTextWithHashAnnotationsFormatThe macro has plain text format with special pseudo-comment annotations
Macro::InterpreterRubyDebuggerIndicates Ruby debugger for debugger_scheme

Deprecated methods (protected, public, static, non-static and constructors)

voidcreateUse of this method is deprecated. Use _create instead
voiddestroyUse of this method is deprecated. Use _destroy instead
[const]booldestroyed?Use of this method is deprecated. Use _destroyed? instead
[const]boolis_const_object?Use of this method is deprecated. Use _is_const_object? instead

Detailed description

MacroFormat

Signature: [static,const] Macro::Format MacroFormat

Description: The macro has macro (XML) format

Python specific notes:
The object exposes a readable attribute 'MacroFormat'. This is the getter.

NoDebugger

Signature: [static] Macro::Interpreter NoDebugger

Description: Indicates no debugging for debugger_scheme

Python specific notes:
The object exposes a readable attribute 'NoDebugger'. This is the getter.

PlainTextFormat

Signature: [static,const] Macro::Format PlainTextFormat

Description: The macro has plain text format

Python specific notes:
The object exposes a readable attribute 'PlainTextFormat'. This is the getter.

PlainTextWithHashAnnotationsFormat

Signature: [static,const] Macro::Format PlainTextWithHashAnnotationsFormat

Description: The macro has plain text format with special pseudo-comment annotations

Python specific notes:
The object exposes a readable attribute 'PlainTextWithHashAnnotationsFormat'. This is the getter.

RubyDebugger

Signature: [static] Macro::Interpreter RubyDebugger

Description: Indicates Ruby debugger for debugger_scheme

Python specific notes:
The object exposes a readable attribute 'RubyDebugger'. This is the getter.

_const_cast

Signature: [const] MacroInterpreter ptr _const_cast

Description: Returns a non-const reference to self.

Basically, this method allows turning a const object reference to a non-const one. This method is provided as last resort to remove the constness from an object. Usually there is a good reason for a const object reference, so using this method may have undesired side effects.

This method has been introduced in version 0.29.6.

_create

Signature: void _create

Description: Ensures the C++ object is created

Use this method to ensure the C++ object is created, for example to ensure that resources are allocated. Usually C++ objects are created on demand and not necessarily when the script object is created.

_destroy

Signature: void _destroy

Description: Explicitly destroys the object

Explicitly destroys the object on C++ side if it was owned by the script interpreter. Subsequent access to this object will throw an exception. If the object is not owned by the script, this method will do nothing.

_destroyed?

Signature: [const] bool _destroyed?

Description: Returns a value indicating whether the object was already destroyed

This method returns true, if the object was destroyed, either explicitly or by the C++ side. The latter may happen, if the object is owned by a C++ object which got destroyed itself.

_is_const_object?

Signature: [const] bool _is_const_object?

Description: Returns a value indicating whether the reference is a const reference

This method returns true, if self is a const reference. In that case, only const methods may be called on self.

_manage

Signature: void _manage

Description: Marks the object as managed by the script side.

After calling this method on an object, the script side will be responsible for the management of the object. This method may be called if an object is returned from a C++ function and the object is known not to be owned by any C++ instance. If necessary, the script side may delete the object if the script's reference is no longer required.

Usually it's not required to call this method. It has been introduced in version 0.24.

_unmanage

Signature: void _unmanage

Description: Marks the object as no longer owned by the script side.

Calling this method will make this object no longer owned by the script's memory management. Instead, the object must be managed in some other way. Usually this method may be called if it is known that some C++ object holds and manages this object. Technically speaking, this method will turn the script's reference into a weak reference. After the script engine decides to delete the reference, the object itself will still exist. If the object is not managed otherwise, memory leaks will occur.

Usually it's not required to call this method. It has been introduced in version 0.24.

assign

Signature: void assign (const MacroInterpreter other)

Description: Assigns another object to self

create

Signature: void create

Description: Ensures the C++ object is created

Use of this method is deprecated. Use _create instead

Use this method to ensure the C++ object is created, for example to ensure that resources are allocated. Usually C++ objects are created on demand and not necessarily when the script object is created.

create_template

Signature: Macro ptr create_template (string url)

Description: Creates a new macro template

url:The template will be initialized from that URL.

This method will create a register a new macro template. It returns a Macro object which can be modified in order to adjust the template (for example to set description, add a content, menu binding, autorun flags etc.)

This method must be called after register has called.

debugger_scheme=

Signature: void debugger_scheme= (Macro::Interpreter scheme)

Description: Sets the debugger scheme (which debugger to use for the DSL macro)

The value can be one of the constants RubyDebugger or NoDebugger.

Use this attribute setter in the initializer before registering the interpreter.

Before version 0.25 this attribute was a re-implementable method. It has been turned into an attribute for performance reasons in version 0.25.

Python specific notes:
The object exposes a writable attribute 'debugger_scheme'. This is the setter.

description=

Signature: void description= (string description)

Description: Sets a description string

This string is used for showing the type of DSL macro in the file selection box together with the suffix for example. Use this attribute setter in the initializer before registering the interpreter.

Before version 0.25 this attribute was a re-implementable method. It has been turned into an attribute for performance reasons in version 0.25.

Python specific notes:
The object exposes a writable attribute 'description'. This is the setter.

destroy

Signature: void destroy

Description: Explicitly destroys the object

Use of this method is deprecated. Use _destroy instead

Explicitly destroys the object on C++ side if it was owned by the script interpreter. Subsequent access to this object will throw an exception. If the object is not owned by the script, this method will do nothing.

destroyed?

Signature: [const] bool destroyed?

Description: Returns a value indicating whether the object was already destroyed

Use of this method is deprecated. Use _destroyed? instead

This method returns true, if the object was destroyed, either explicitly or by the C++ side. The latter may happen, if the object is owned by a C++ object which got destroyed itself.

dup

Signature: [const] new MacroInterpreter ptr dup

Description: Creates a copy of self

Python specific notes:
This method also implements '__copy__' and '__deepcopy__'.

executable

Signature: [virtual,const] Executable ptr executable (const Macro ptr macro)

Description: Returns the executable object which implements the macro execution

macro:The macro to execute

This method must be reimplemented to return an Executable object for the actual implementation. The system will use this function to execute the script when a macro with interpreter type 'dsl' and the name of this interpreter is run.

This method has been introduced in version 0.27 and replaces the 'execute' method.

include_expansion

Signature: string[] include_expansion (Macro ptr macro)

Description: Provides include expansion as defined by the interpreter

The return value will be a two-element array with the encoded file path and the include-expanded text.

This method has been introduced in version 0.28.12.

is_const_object?

Signature: [const] bool is_const_object?

Description: Returns a value indicating whether the reference is a const reference

Use of this method is deprecated. Use _is_const_object? instead

This method returns true, if self is a const reference. In that case, only const methods may be called on self.

new

Signature: [static] new MacroInterpreter ptr new

Description: Creates a new object of this class

Python specific notes:
This method is the default initializer of the object.

register

Signature: void register (string name)

Description: Registers the macro interpreter

name:The interpreter name. This is an arbitrary string which should be unique.

Registration of the interpreter makes the object known to the system. After registration, macros whose interpreter is set to 'dsl' can use this object to run the script. For executing a script, the system will call the interpreter's execute method.

storage_scheme=

Signature: void storage_scheme= (Macro::Format scheme)

Description: Sets the storage scheme (the format as which the macro is stored)

This value indicates how files for this DSL macro type shall be stored. The value can be one of the constants PlainTextFormat, PlainTextWithHashAnnotationsFormat and MacroFormat.

Use this attribute setter in the initializer before registering the interpreter.

Before version 0.25 this attribute was a re-implementable method. It has been turned into an attribute for performance reasons in version 0.25.

Python specific notes:
The object exposes a writable attribute 'storage_scheme'. This is the setter.

suffix=

Signature: void suffix= (string suffix)

Description: Sets the file suffix

This string defines which file suffix to associate with the DSL macro. If an empty string is given (the default) no particular suffix is assciated with that macro type and "lym" is assumed. Use this attribute setter in the initializer before registering the interpreter.

Before version 0.25 this attribute was a re-implementable method. It has been turned into an attribute for performance reasons in version 0.25.

Python specific notes:
The object exposes a writable attribute 'suffix'. This is the setter.

supports_include_expansion=

Signature: void supports_include_expansion= (bool flag)

Description: Sets a value indicating whether this interpreter supports the default include file expansion scheme.

If this value is set to true (the default), lines like '# %include ...' will be substituted by the content of the file following the '%include' keyword. Set this value to false if you don't want to support this feature.

This attribute has been introduced in version 0.27.

Python specific notes:
The object exposes a writable attribute 'supports_include_expansion'. This is the setter.

syntax_scheme=

Signature: void syntax_scheme= (string scheme)

Description: Sets a string indicating the syntax highlighter scheme

The scheme string can be empty (indicating no syntax highlighting), "ruby" for the Ruby syntax highlighter or another string. In that case, the highlighter will look for a syntax definition under the resource path ":/syntax/<scheme>.xml".

Use this attribute setter in the initializer before registering the interpreter.

Before version 0.25 this attribute was a re-implementable method. It has been turned into an attribute for performance reasons in version 0.25.

Python specific notes:
The object exposes a writable attribute 'syntax_scheme'. This is the setter.