Skip to content

Reading netlists

Readers translate source formats into the same intermediate representation used by circuit construction and writers. Lark is an implementation detail of the current Spectre reader; custom readers can use any parser.

Read text

from nimphel.readers import SpectreReader

source = """\
global 0 gnd!
R1 (vin vout) resistor r=1k
"""

circuit = SpectreReader().loads(source)

assert circuit.instances[0].ref == "R1"
assert circuit.instances[0].nodes == ("vin", "vout")

Read a file object or path

reader = SpectreReader()

with open("input.scs") as source_file:
    circuit = reader.load(source_file)

circuit = reader.load_path("input.scs")
assert circuit.path.name == "input.scs"

The naming follows common Python deserialization APIs:

  • loads(source) reads an in-memory string or bytes.
  • load(file_object) reads an open file object.
  • load_path(path) is a Spectre convenience that records the resolved path.

Functional fidelity

Nimphel preserves supported circuit meaning rather than source formatting. Parsing and writing can normalize whitespace, line continuations, numeric spelling, and comments.

Understood commands become semantic directives such as GlobalNodes or TransientAnalysis. An unsupported command becomes a dialect-tagged RawDirective, which can be emitted only to the same simulator format.

A functional round trip is:

from nimphel.writers import SpectreWriter

reader = SpectreReader()
writer = SpectreWriter()

original = reader.loads(source)
reparsed = reader.loads(writer.dumps(original))

assert reparsed == original

Syntax errors retain Lark's line and column information and are raised to the caller. They are not printed and suppressed.

Custom readers

A custom reader implements the generic Reader interface:

from nimphel import Circuit
from nimphel.readers import Reader

class CustomReader(Reader[Circuit]):
    def loads(self, source: str | bytes) -> Circuit:
        ...

This boundary permits future JSON, S-expression, or handwritten SPICE readers without changing the circuit API.