~cypheon/kicad2spice

ref: 96e390bc4f914314da98ecdfd7bd775e620563e2 kicad2spice/lib/lex_lib.mll -rw-r--r-- 1.6 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
{
  open Lexing
  open Parse_lib
  exception SyntaxError of string

  let is_at_bol lexbuf =
    lexbuf.lex_start_p.pos_cnum = lexbuf.lex_start_p.pos_bol

}

let space = [' ' '\t']

rule read =
  parse
  | "EESchema-LIBRARY Version " { HEADER }
  | "DEF" { if is_at_bol lexbuf then COMPDEF_START else STRING (lexeme lexbuf) }
  | "ENDDEF" { if is_at_bol lexbuf then COMPDEF_END else STRING (lexeme lexbuf) }
  | "$FPLIST" { if is_at_bol lexbuf then FPLIST_START else STRING (lexeme lexbuf) }
  | "$ENDFPLIST" { if is_at_bol lexbuf then FPLIST_END else STRING (lexeme lexbuf) }
  | "DRAW" { if is_at_bol lexbuf then DRAW_START else STRING (lexeme lexbuf) }
  | "ENDDRAW" { if is_at_bol lexbuf then DRAW_END else STRING (lexeme lexbuf) }
  | "ALIAS" { if is_at_bol lexbuf then ALIAS else STRING (lexeme lexbuf) }
  | 'F' (_ # space) ' ' { FIELD (lexeme lexbuf) }
  | '\n' { new_line lexbuf; NEWLINE }
  | space { read lexbuf }
  | [ ^ '"' ' ' '\t' '\n' ]+ {
    if is_at_bol lexbuf && (lexeme_char lexbuf 0) = '#'
    then read_comment lexbuf
    else STRING (lexeme lexbuf)
  }
  | '"' { read_string (Buffer.create 17) lexbuf }
  | eof { EOF }
  | _ { failwith (Printf.sprintf "unexpected character: line %d:%d" lexbuf.lex_curr_p.pos_lnum (lexbuf.lex_curr_p.pos_cnum - lexbuf.lex_curr_p.pos_bol + 1) )}

and read_string buf =
  parse
  | '"' { STRING (Buffer.contents buf) }
  | '\\' '"'
    {
      Buffer.add_char buf '\\';
      read_string buf lexbuf
    }
  | [^ '"' '\\' '\n']+
    { Buffer.add_string buf (lexeme lexbuf);
      read_string buf lexbuf
    }
and read_comment =
  parse
  | [ ^ '\n' ]* '\n' { new_line lexbuf; read lexbuf }