~cypheon/kicad2spice

ref: 96e390bc4f914314da98ecdfd7bd775e620563e2 kicad2spice/lib/parse_lib.mly -rw-r--r-- 2.1 KiB
96e390bc — Johann Rudloff Implement correct pin order according to field "Spice_Node_Sequence" 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
%token HEADER
%token COMPDEF_START
%token COMPDEF_END
%token FPLIST_START
%token FPLIST_END
%token DRAW_START
%token DRAW_END
%token ALIAS
%token <string> STRING
%token <string> FIELD
%token NEWLINE
%token EOF

%start <Schematic.CompDef.t list> main

%{

  open Schematic

  let collect_some xs =
    let collect_helper acc = function
        Some x -> x::acc
      | None -> acc in
    List.rev (List.fold_left collect_helper [] xs)
%}

%%

field:
  | _fname=FIELD; list(STRING); NEWLINE;
    {
      ()
    }

ignore_line:
  | list(STRING); NEWLINE; {}

fplist:
  | FPLIST_START; NEWLINE;
    list(ignore_line);
    FPLIST_END; NEWLINE;
    {}

draw_line:
  | typ=STRING;
    args=list(STRING);
    NEWLINE;
  {
    if typ = "X"
    then
      let name = List.nth args 0 in
      let number = List.nth args 1 in
      let posx = int_of_string (List.nth args 2) in
      let posy = int_of_string (List.nth args 3) in
      (*Printf.eprintf "PIN: %s @ %d x %d\n" (String.concat " " args) posx posy;*)
      let p = {Pin.
        name;
        number;
        pos={x=posx; y=posy};
      } in
      Printf.eprintf "pin: %s\n" (Pin.show p);
      Some p
    else (Printf.eprintf "no pin: %s: %s\n" typ (String.concat " " args); None)
  }

aliasline:
  | {[]}
  | ALIAS; aliases=list(STRING); NEWLINE; {aliases}

component:
  | COMPDEF_START;
      name=STRING;
      _reference=STRING;
      _unused=STRING;
      _text_offset=STRING;
      _draw_pinnumber=STRING;
      _draw_pinname=STRING;
      _unit_counts=STRING;
      _units_locked=STRING;
      option_flag=STRING;
    NEWLINE;
    list(field);
    aliases=aliasline;
    option(fplist);
    DRAW_START; NEWLINE;
    draw_pins=list(draw_line);
    DRAW_END; NEWLINE;
    COMPDEF_END; NEWLINE;
    {
      { CompDef.
      name;
      pins = collect_some draw_pins;
      power = String.uppercase_ascii option_flag = "P";
      aliases;
      }
    }

main:
  | HEADER; _version=STRING; NEWLINE;
    compdefs=list(component);
    EOF;
    {
      compdefs
    }
  | error {
    Printf.printf "error j:%d\n" $startpos.pos_lnum;
  invalid_arg "error parsing partslib"
  }