File: dot_lexer.mll

package info (click to toggle)
js-of-ocaml 5.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 32,020 kB
  • sloc: ml: 91,250; javascript: 57,289; ansic: 315; makefile: 271; lisp: 23; sh: 6; perl: 4
file content (151 lines) | stat: -rw-r--r-- 3,768 bytes parent folder | download | duplicates (11)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
(* Graph viewer
 * Copyright (C) 2010 Jérôme Vouillon
 * Laboratoire PPS - CNRS Université Paris Diderot
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)

{

type graph_type = Graph | Digraph | Unknown
let graph_type = ref Unknown

let set_graph_type v = if !graph_type = Unknown then graph_type := v

let buffer = Buffer.create 256

let html_nest = ref 1

let reset () = graph_type := Unknown

}

let letter = ['A'-'Z' 'a'-'z' '_' '\128'-'\255']
let digit = ['0'-'9']
let name = letter (letter | digit) *
let number = '-'? ((digit+ ('.' digit*)?) | ('.' digit+))
let id = name | number

rule token =
  parse
    "/*"
      { comment lexbuf }
  | "//" [^ '\n']*
  | "#" [^ '\n']*
  | [' ' '\t' '\r' '\n']+
      { token lexbuf }
  | "->"
      { if !graph_type = Digraph then
          Dot_parser.EDGEOP
        else
          exit 1 (*XXX*) }
  | "--"
      { if !graph_type = Graph then
          Dot_parser.EDGEOP
        else
          exit 1 (*XXX*) }
  | name
      {
        let s = Lexing.lexeme lexbuf in
        match String.lowercase s with
          "node"     -> Dot_parser.NODE
        | "edge"     -> Dot_parser.EDGE
        | "graph"    -> set_graph_type Graph; Dot_parser.GRAPH
        | "digraph"  -> set_graph_type Digraph; Dot_parser.DIGRAPH
        | "strict"   -> Dot_parser.STRICT
        | "subgraph" -> Dot_parser.SUBGRAPH
        | _          -> Dot_parser.ATOM s }
  | number
      { Dot_parser.ATOM (Lexing.lexeme lexbuf) }
  | '"'
      { qstring lexbuf }
  | '<'
      { Buffer.add_char buffer '<';
        html_nest := 1;
        hstring lexbuf }
  | '{'
      { Dot_parser.LBRACE }
  | '}'
      { Dot_parser.RBRACE }
  | '['
      { Dot_parser.LBRACKET }
  | ']'
      { Dot_parser.RBRACKET }
  | '='
      { Dot_parser.EQUAL }
  | ':'
      { Dot_parser.COLON }
  | ';'
      { Dot_parser.SEMI }
  | ','
      { Dot_parser.COMMA }
  | '+'
      { Dot_parser.PLUS }
  | _
      { Format.eprintf "%s@." (Lexing.lexeme lexbuf); exit 1 (*XXX*) }
  | eof
      { Dot_parser.EOF }

and comment =
  parse
    [^'*']*
      { comment lexbuf }
  | '*'+ [^ '*' '/']*
      { comment lexbuf }
  | '*'+ '/'
      { token lexbuf }
(*
  | eof
*)

and qstring =
  parse
    '"'
      { let s = Buffer.contents buffer in
        Buffer.clear buffer;
        Dot_parser.QATOM s }
  | "\\\""
      { Buffer.add_string buffer "\\\"";
        qstring lexbuf }
  | "\\\n"
      { qstring lexbuf }
  | '\n'
      { Buffer.add_char buffer '\n';
        qstring lexbuf }
  | [^ '"' '\\']+ | '\\' [^'\n']
      { Buffer.add_string buffer (Lexing.lexeme lexbuf);
        qstring lexbuf }
(*
   | _
   | eof
*)

and hstring =
  parse
    '>'
      { decr html_nest;
        if !html_nest > 0 then hstring lexbuf else begin
          Buffer.add_char buffer '>';
          let s = Buffer.contents buffer in
          Buffer.clear buffer;
          Dot_parser.QATOM s
        end }
  | '<'
      { incr html_nest;
        Buffer.add_char buffer '<';
        hstring lexbuf }
  | [^ '<' '>'] +
      { Buffer.add_string buffer (Lexing.lexeme lexbuf);
        hstring lexbuf }