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 9af5a2f commit 1eb6ab0
Show file tree
Hide file tree
Showing 15 changed files with 323 additions and 181 deletions.
7 changes: 5 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use thiserror::Error;
pub enum GDSIIErrorKind {
#[error("Cannot parse the given *.gds")]
InvalidGDSII,
#[error("Cannot open the file at `{0}`")]
InvalidPath(String),
#[error("IO error while opening gds file")]
InvalidPath {
#[from]
source: std::io::Error,
},
}
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
#![allow(dead_code)]

use crate::model::GDSIIModel;
use crate::parser::gds2_parser;

use crate::error::GDSIIErrorKind;

mod error;
mod model;
mod parser;

/// gds2 file path
pub fn parse_gds2<P: AsRef<std::path::Path>>(
file: P,
) -> std::result::Result<GDSIIModel, GDSIIErrorKind> {
let buff = std::fs::read(file)?;
let gds2: GDSIIModel = gds2_parser(&buff)?;
Ok(gds2)
}
1 change: 1 addition & 0 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum GDSIIVariant {
TuctosinEnd,
ModuleEnd,
FileEnd,
Eof, // may include EOF tag in the file
}

/// File header variant in GDSII
Expand Down
19 changes: 13 additions & 6 deletions src/parser/basic.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use super::ParseGDIIRes;
use nom::bytes::streaming::take;
use byteorder::{BigEndian, ByteOrder, LittleEndian};
use nom::bytes::streaming::{tag, take};
use nom::sequence::tuple;

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

pub(super) fn take_type(s: &str) -> ParseGDIIRes<&str, &str> {
take(2usize)(s)
pub(super) fn take_type(s: &[u8]) -> ParseGDIIRes<&[u8], [u8; 2]> {
let (s, d) = take(2usize)(s)?;
Ok((s, [d[0], d[1]]))
}

pub(super) fn end_tag(s: &[u8]) -> ParseGDIIRes<&[u8], &[u8]> {
tag([0x00, 0x00])(s)
}
49 changes: 0 additions & 49 deletions src/parser/gds2_parser.rs

This file was deleted.

58 changes: 55 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use nom::error::{ErrorKind, ParseError};
use crate::error::GDSIIErrorKind;
use crate::model::*;
use nom::{
error::{ErrorKind, ParseError},
multi::many_till,
Finish,
};
use std::fmt::Debug;

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

impl<I> ParseError<I> for ParseGDSIIError<I> {
Expand All @@ -20,6 +27,51 @@ impl<I> ParseError<I> for ParseGDSIIError<I> {
type ParseGDIIRes<T, U> = nom::IResult<T, U, ParseGDSIIError<T>>;

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

use basic::end_tag;
use variant_parser::*;

pub fn gds2_parser(s: &[u8]) -> std::result::Result<GDSIIModel, GDSIIErrorKind> {
match many_till(variant_parser, end_tag)(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),
}
}
Loading

0 comments on commit 1eb6ab0

Please sign in to comment.