Crate xschem_parser

Crate xschem_parser 

Source
Expand description

Xschem schematic and symbol parser.

This library supports up to Xschem file version 1.2. See Xschem developer info for more information on the file format.

§Usage

Use from_str or from_slice to parse a token::Schematic from a string or byte slice. The parser is zero-copy so the resulting data structure contains references to the input.

The parse error result Error implements std::fmt::Display to convert the error to a nice human readable format.

§Examples

§Parse from string

use nom::Input;
use xschem_parser::Span;
use xschem_parser::token::{Flip, Objects, Property, Rotation, Schematic, Text, Version};

let input = "\
v {xschem version=3.4.5 file_version=1.2}
K {type=regulator}
T {@name} -17.5 -15 0 0 0.2 0.2 {}
";

// Get a span so we can reference to locations in the input string.
// The parsed schematic contains references with column and line in
// the input string.
let span = Span::new(input);

let expected = Schematic {
    version: Version(Property {
        prop: span.take_from(3).take(37),
        attrs: [
            (span.take_from(10).take(7), span.take_from(18).take(5)),
            (span.take_from(24).take(12), span.take_from(37).take(3)),
        ].into(),
    }),
    spice_property: None,
    verilog_property: None,
    vhdl_property: None,
    tedax_property: None,
    symbol_property: Some(Property {
        prop: span.take_from(45).take(14).into(),
        attrs: [
            (span.take_from(45).take(4), span.take_from(50).take(9)),
        ].into(),
    }.into()),
    texts: vec![Text {
        text: span.take_from(64).take(5),
        position: (-17.5, -15.0).try_into().unwrap(),
        rotation: Rotation::Zero,
        flip: Flip::Unflipped,
        size: (0.2, 0.2).try_into().unwrap(),
        property: Property {
            prop: span.take_from(94).take(0),
            attrs: [].into(),
        },
    }].into(),
    lines: Objects::default(),
    rectangles: Objects::default(),
    polygons: Objects::default(),
    arcs: Objects::default(),
    wires: Objects::default(),
    components: Objects::default(),
};

let result = xschem_parser::from_str(input);

assert_eq!(result, Ok(expected));

§Parse from invalid string

// invalid input, wrong brackets
let input = "v []";

let expected = "\
error: expected '{'
  --> :1:3
   |
 1 | v []
   |   ^
   |
in version
  --> :1:1
   |
 1 | v []
   | ^
   |";

let result = xschem_parser::from_str(input);

assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), expected);

§Parse from file

Since a parsed schematic contains references to the input, this library cannot provide an implementation of parsing from file, since that would require copying the contents of the file contents to make the lifetimes work out.

use std::path::Path;

let path = Path::new("test.sch");
let contents = std::fs::read_to_string(path)?;
match xschem_parser::from_str_file(&contents, path) {
    Ok(schematic) => println!("{schematic}"),
    Err(e) => eprintln!("{e}"),
}

Modules§

error
Parser errors.
parse
Parser combinator functions.
token
Parsed data structures.

Functions§

from_slice
Parse a Schematic from a byte slice.
from_slice_file
Parse a Schematic from a byte slice with Path info.
from_str
Parse a Schematic from a str.
from_str_file
Parse a Schematic from a str with Path info.

Type Aliases§

ByteFileSpan
Bytes reference with location in file.
ByteSpan
Bytes reference with location.
FileSpan
String reference with location in file.
Span
String reference with location.