Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric committed Jul 24, 2021
1 parent 058534e commit 9af5a2f
Show file tree
Hide file tree
Showing 8 changed files with 432 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "gds2_parser"
version = "0.1.0"
edition = "2018"
keyword = ["parser","science"]

[dependencies]
nom = "6.0"
byteorder = "*"
thiserror = "*"
9 changes: 9 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum GDSIIErrorKind {
#[error("Cannot parse the given *.gds")]
InvalidGDSII,
#[error("Cannot open the file at `{0}`")]
InvalidPath(String),
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![allow(dead_code)]
mod error;
mod model;
mod parser;
109 changes: 109 additions & 0 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use std::collections::HashMap;

#[derive(Default)]
pub struct GDSIIModel {
pub header: HashMap<String, FileHeader>,
pub structure_time: [i16; 12],
pub structure_name: String,
pub s_boundary: Vec<Tuctosin>,
pub s_path: Vec<Tuctosin>,
pub s_sref: Vec<Tuctosin>,
pub s_aref: Vec<Tuctosin>,
pub s_text: Vec<Tuctosin>,
pub s_node: Vec<Tuctosin>,
pub s_box: Vec<Tuctosin>,
}

impl GDSIIModel {
pub fn summerize(&self) {
unimplemented!()
}
}

pub enum GDSIIVariant {
FileHeader(FileHeader),
ModuleHeader(ModuleHeader),
TuctosinHeader(TuctosinHeader),
Tuctosin(Tuctosin),
TuctosinEnd,
ModuleEnd,
FileEnd,
}

/// File header variant in GDSII
pub enum FileHeader {
Header(i16), // 0x00_02
BgnLib([i16; 12]), // 0x01_02
LibName(String), // 0x02_06
RefLibs(String), // 0x1F_06
Fonts(String), // 0x20_06
AttrTable(String), // 0x23_06
Generations(i16), // 0x22_02
Format(i16), // 0x36_02
Mask(String), // 0x37_06
EndMask, // 0x38_00
Units([f64; 2]), // 0x03_05
}

impl FileHeader {
pub fn get_tag(&self) -> String {
let tag_str = match self {
FileHeader::Header(_) => "head",
FileHeader::BgnLib(_) => "bgn",
FileHeader::LibName(_) => "libname",
FileHeader::RefLibs(_) => "reflib",
FileHeader::Fonts(_) => "font",
FileHeader::AttrTable(_) => "attr",
FileHeader::Generations(_) => "generation",
FileHeader::Format(_) => "format",
FileHeader::Mask(_) => "mask",
FileHeader::EndMask => "endmask",
FileHeader::Units(_) => "unit",
};
tag_str.to_string()
}
}

/// Module header in GDSII
pub enum ModuleHeader {
BgnStr([i16; 12]), // 0x05_02
StrName(String), // 0x06_06
}

/// shape header in GDSII
#[derive(PartialEq)]
pub enum TuctosinHeader {
Boundary, // 0x08_00
Path, // 0x09_00
Sref, // 0x0A_00
Aref, // 0x0B_00
Text, // 0x0C_00
Node, // 0x15_00
Box, // 0x2D_00
}

impl Default for TuctosinHeader {
fn default() -> Self {
TuctosinHeader::Boundary
}
}

pub enum Tuctosin {
ElfFlags(i16), // 0x26_01
Plex(i32), // 0x2F_03
Layer(i16), // 0x0D_O2
DataType(i16), // 0xOE_O2
Xy(Vec<(i32, i32)>), // 0x10_03
PathType(i16), // 0x21_02
Width(i32), // 0x0F_03
Sname(String), // 0x12_06
Strans(i16), // 0x1A_01
Mag(i64), // 0x1B_05
Angle(i64), // 0x1C_05
ColRow((i16, i16)), // 0x13_02
TextType(i16), // 0x16_02
Persentation(i16), // 0x17_01
AsciiString(String), // 0x19_06
NodeType(i16), // 0x2A_02
BoxType(i16), // 0x2E_02
}
13 changes: 13 additions & 0 deletions src/parser/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::ParseGDIIRes;
use nom::bytes::streaming::take;

// return valid data size(exclude two byte "size" and two byte "type")
pub(super) fn take_size(s: &str) -> ParseGDIIRes<&str, usize> {
let (s, d) = take(2usize)(s)?;
let parsed_d = usize::from_str_radix(d, 16).unwrap();
Ok((s, parsed_d - 4usize))
}

pub(super) fn take_type(s: &str) -> ParseGDIIRes<&str, &str> {
take(2usize)(s)
}
49 changes: 49 additions & 0 deletions src/parser/gds2_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use super::variant_parser::*;
use crate::error::GDSIIErrorKind;
use crate::model::*;
use nom::multi::many1;
use nom::Finish;

pub(super) fn gds2_parser(s: &str) -> std::result::Result<GDSIIModel, GDSIIErrorKind> {
match many1(variant_parser)(s).finish() {
Ok((_, data)) => {
let mut gds2_model = GDSIIModel::default();
let mut current_tuctosin_type = TuctosinHeader::default();
// main process
for d in data {
match d {
GDSIIVariant::FileHeader(header) => {
gds2_model.header.insert(header.get_tag(), header);
}
GDSIIVariant::ModuleHeader(module) => match module {
ModuleHeader::BgnStr(t) => gds2_model.structure_time = t,
ModuleHeader::StrName(t) => gds2_model.structure_name = t,
},
GDSIIVariant::TuctosinHeader(toc_header) => {
if toc_header != current_tuctosin_type {
current_tuctosin_type = toc_header;
}
}
GDSIIVariant::Tuctosin(shape) => match current_tuctosin_type {
TuctosinHeader::Boundary => gds2_model.s_boundary.push(shape),
TuctosinHeader::Path => gds2_model.s_path.push(shape),
TuctosinHeader::Sref => gds2_model.s_sref.push(shape),
TuctosinHeader::Aref => gds2_model.s_aref.push(shape),
TuctosinHeader::Text => gds2_model.s_text.push(shape),
TuctosinHeader::Node => gds2_model.s_node.push(shape),
TuctosinHeader::Box => gds2_model.s_box.push(shape),
},
GDSIIVariant::FileEnd => {
// summerize
gds2_model.summerize();
break;
// early return
}
_ => {}
}
}
Ok(gds2_model)
}
Err(_) => Err(GDSIIErrorKind::InvalidGDSII),
}
}
25 changes: 25 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use nom::error::{ErrorKind, ParseError};
use std::fmt::Debug;

#[derive(Debug, PartialEq)]
pub(self) enum ParseGDSIIError<I> {
ParseIntError,
Nom(I, ErrorKind),
}

impl<I> ParseError<I> for ParseGDSIIError<I> {
fn from_error_kind(input: I, kind: ErrorKind) -> Self {
ParseGDSIIError::Nom(input, kind)
}

fn append(_: I, _: ErrorKind, other: Self) -> Self {
other
}
}

type ParseGDIIRes<T, U> = nom::IResult<T, U, ParseGDSIIError<T>>;

mod basic;
mod gds2_parser;
// mod file_tag;
mod variant_parser;
Loading

0 comments on commit 9af5a2f

Please sign in to comment.