API reference - Class MacroInterpreter

Notation used in Ruby API documentation

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:

  • Derive a new object from RBA::MacroInterpreter
  • Reimplement the execute method for the actual execution of the code
  • In the initialize method register the object as DSL interpreter
  • Create at least one template macro in the initialize method

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 SimpleInterpreter < RBA::MacroInterpreter

  # Constructor
  def initialize
    # 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
  
  # Implements the execute method
  def execute(macro)
    eval(macro.text, nil, macro.path)
  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.

Public constructors

MacroInterpreternewCreates a new object of this class

Public methods

[const]voidassign(const MacroInterpreter other)Assign the contents of another object to self
voidcreateEnsures the C++ object is created
Macro ptrcreate_template(string name)Creates a new macro template
[virtual,const]intdebugger_schemeReturns the debugger scheme (which debugger to use for the DSL macro)
[virtual,const]stringdescriptionReturns a description string
voiddestroyExplicitly destroy the object
[const]booldestroyed?Returns a value indicating whether the object was already destroyed
[const]MacroInterpreterdupCreates a copy of self
[virtual,const]voidexecute(const Macro ptr macro)Gets called to execute a macro
[const]boolis_const_object?Returns a value indicating whether the reference is a const reference
voidregister(string name)Registers the macro interpreter
[virtual,const]intstorage_schemeReturns the storage scheme (the format as which the macro is stored)
[virtual,const]stringsuffixReturns the file suffix
[virtual,const]stringsyntax_schemeReturns a string indicating the syntax highlighter scheme

Public static methods and constants

intMacroFormatIndicates macro (XML) format for storage_scheme
intNoDebuggerIndicates no debugging for debugger_scheme
intPlainTextFormatIndicates plain text format for storage_scheme
intPlainTextWithHashAnnotationsFormatIndicates plain text format for storage_scheme
intRubyDebuggerIndicates Ruby debugger for debugger_scheme

Detailed description

[static] int MacroFormat

Description: Indicates macro (XML) format for storage_scheme

[static] int NoDebugger

Description: Indicates no debugging for debugger_scheme

[static] int PlainTextFormat

Description: Indicates plain text format for storage_scheme

[static] int PlainTextWithHashAnnotationsFormat

Description: Indicates plain text format for storage_scheme

This format is identical to PlainTextFormat but indicates that it is possible to insert annotations (properties) into the text in a hash-commented header.

[static] int RubyDebugger

Description: Indicates Ruby debugger for debugger_scheme

[const] void assign(const MacroInterpreter other)

Description: Assign the contents of another object to self

This method assigns the contents of another object to self. This is a deep copy that does not only copy the reference but the actual content.

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.

Macro ptr create_template(string name)

Description: Creates a new macro template

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

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.

[virtual,const] int debugger_scheme

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

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

[virtual,const] string description

Description: Returns 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. The default implementation returns an empty string.

void destroy

Description: Explicitly destroy the object

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

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

[const] MacroInterpreter dup

Description: Creates a copy of self

[virtual,const] void execute(const Macro ptr macro)

Description: Gets called to execute a macro

macro:The macro to execute

This method must be reimplemented to execute the macro. The system will call this script when a macro with interpreter type 'dsl' and the name of this interpreter is run.

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

[static] MacroInterpreter new

Description: Creates a new object of this class

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.

[virtual,const] int storage_scheme

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

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

[virtual,const] string suffix

Description: Returns the file suffix

This string defines which file suffix to associate with the DSL macro. If an empty string is returned no particular suffix is assciated with that macro type and "lym" is assumed. The default implementation returns an empty string.

[virtual,const] string syntax_scheme

Description: Returns a string indicating the syntax highlighter scheme

The returned 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". The default implementation returns an empty string (no syntax highlighting).