~cypheon/kicad2spice

482cfdc38b63e9d1ef5bc7ac24081178fc7b221a — Johann Rudloff 4 years ago f7dc604
Parse extra fields of components.
4 files changed, 32 insertions(+), 7 deletions(-)

M lib/parser.ml
M lib/sch_legacy.mly
M lib/sch_legacy_ast.ml
M lib/schematic.ml
M lib/parser.ml => lib/parser.ml +1 -1
@@ 49,7 49,7 @@ let resolve_item compdefs = let open Sch_legacy_ast in function
  | ParserItem.Comp c ->
      let mangled = mangle_compname c.name in
      match (List.find_opt (fun c -> c.Schematic.CompDef.name = mangled) compdefs) with
        | Some part -> Schematic.Item.Comp {name=c.name; part;ref=c.ref;pos=c.pos;transform=c.transform;value=c.value;}
        | Some part -> Schematic.Item.Comp {name=c.name; part;ref=c.ref;pos=c.pos;transform=c.transform;value=c.value;fields=c.fields;}
        | None -> failwith ("unknown component: " ^ mangled)

let resolve compdefs (version, items) =

M lib/sch_legacy.mly => lib/sch_legacy.mly +12 -6
@@ 32,17 32,23 @@ and comp_parse_pos name ref lines = match lines with
  | _ -> failwith "unexpected EOF, expecting \"P\" line"
and comp_parse_fields name ref pos lines = match lines with
  | ("F"::"0"::_)::rest -> comp_parse_fields name ref pos rest
  | ("F"::"1"::value::_)::rest -> comp_parse_extra1 name ref pos value rest
  | ("F"::"1"::value::_)::rest -> comp_parse_extra1 name ref pos value (Fields.empty) rest
  | _ -> failwith "unexpected EOF, expecting \"F\" line"
and comp_parse_extra1 name ref pos value lines = match lines with
  | ("F"::_)::rest -> comp_parse_extra1 name ref pos value rest
  | ("\t"::_)::rest -> comp_parse_extra2 name ref pos value rest
and comp_parse_extra1 name ref pos value fields lines = match lines with
  | ("F"::tokens)::rest ->
      let updated_fields = if List.length tokens >= 10 then
        let f_name = List.nth tokens 9 in
        let f_value = List.nth tokens 1 in
        (Fields.add f_name f_value fields)
      else fields in
      comp_parse_extra1 name ref pos value updated_fields rest
  | ("\t"::_)::rest -> comp_parse_extra2 name ref pos value fields rest
  | _ -> failwith "unexpected EOF, expecting indented line"
and comp_parse_extra2 name ref pos value lines = match lines with
and comp_parse_extra2 name ref pos value fields lines = match lines with
  | ("\t"::a::b::c::d::[])::[] -> let
  a,b,c,d = (int_of_string a, int_of_string b, int_of_string c, int_of_string d)
  in {ParserComp.
    name; ref; value; pos; transform={a;b;c;d}
    name; ref; value; pos; transform={a;b;c;d}; fields;
    }
  | _ -> failwith "unexpected EOF, expecting indented line"


M lib/sch_legacy_ast.ml => lib/sch_legacy_ast.ml +1 -0
@@ 6,6 6,7 @@ module ParserComp = struct
    name : string;
    ref : string;
    value : string;
    fields : string Fields.t;
    pos : point2i;
    transform : mat22i;
  }

M lib/schematic.ml => lib/schematic.ml +18 -0
@@ 1,5 1,22 @@
include Geometry

module Fields = struct
  include Map.Make(String)

  let pp pval fmt m = let open Format in
    pp_print_string fmt "{Fields.t";
    pp_open_hbox fmt ();
    let first = ref true in
    Seq.iter (fun (k,v) ->
      if not !first then pp_print_string fmt ","; first := false;
      pp_print_break fmt 1 2;
      fprintf fmt "\"%s\" => %a" k pval v;
    )
    (to_seq m);
    pp_close_box fmt ();
    pp_print_string fmt "}"
end

module Wire = struct
  type t = {
    startp : point2i;


@@ 33,6 50,7 @@ module Comp = struct
    name : string;
    ref : string;
    value : string;
    fields : string Fields.t;
    pos : point2i;
    transform : mat22i;
    part : CompDef.t;