summaryrefslogtreecommitdiffstats
path: root/virt-df/virt_df_lvm2_lexer.mll
blob: 2dbe7e59cc3bdfb463d43ec528b076bd6fe3eaad (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
(* 'df' command for virtual domains.
   (C) Copyright 2007-2008 Richard W.M. Jones, Red Hat Inc.
   http://libvirt.org/

   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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *)

(* Scanner for LVM2 metadata.
 * ocamllex tutorial:
 * http://plus.kaist.ac.kr/~shoh/ocaml/ocamllex-ocamlyacc/ocamllex-tutorial/
 *)

{
  open Printf
  open Lexing

  open Virt_df
  open Virt_df_lvm2_parser

  (* Temporary buffer used for parsing strings, etc. *)
  let tmp = Buffer.create 80

  exception Error of string
}

let digit = ['0'-'9']
let alpha = ['a'-'z' 'A'-'Z']
let alphau = ['a'-'z' 'A'-'Z' '_']
let alnum = ['a'-'z' 'A'-'Z' '0'-'9']
let alnumu = ['a'-'z' 'A'-'Z' '0'-'9' '_']
let ident = alphau alnumu*

let whitespace = [' ' '\t' '\r' '\n']+

let escaped_char = '\\' _

rule token = parse
  (* ignore whitespace and comments *)
  | whitespace
  | '#' [^ '\n']*
      { token lexbuf }

  (* scan single character tokens *)
  | '{'  { LBRACE }
  | '}'  { RBRACE }
  | '['  { LSQUARE }
  | ']'  { RSQUARE }
  | '='  { EQ }
  | ','  { COMMA }

  (* strings - see LVM2/lib/config/config.c *)
  | '"'
      {
	Buffer.reset tmp;
	STRING (dq_string lexbuf)
      }
  | '\''
      {
	Buffer.reset tmp;
	STRING (dq_string lexbuf)
      }

  (* floats *)
  | ('-'? digit+ '.' digit*) as f
      {
	let f = float_of_string f in
	FLOAT f
      }

  (* integers *)
  | ('-'? digit+) as i
      {
	let i = Int64.of_string i in
	INT i
      }

  (* identifiers *)
  | ident as id
      { IDENT id }

  (* end of file *)
  | eof
      { EOF }

  | _ as c
      { raise (Error (sprintf "%c: invalid character in input" c)) }

and dq_string = parse
  | '"'
      { Buffer.contents tmp }
  | escaped_char as str
      { Buffer.add_char tmp str.[1]; dq_string lexbuf }
  | eof
      { raise (Error "unterminated string in metadata") }
  | _ as c
      { Buffer.add_char tmp c; dq_string lexbuf }

and q_string = parse
  | '\''
      { Buffer.contents tmp }
  | escaped_char as str
      { Buffer.add_char tmp str.[1]; q_string lexbuf }
  | eof
      { raise (Error "unterminated string in metadata") }
  | _ as c
      { Buffer.add_char tmp c; q_string lexbuf }

{
  (* Demonstration of how to wrap the token function
     with extra debugging statements:
  let token lexbuf =
    try
      let r = token lexbuf in
      if debug then
	eprintf "Lexer: token returned is %s\n"
	  (match r with
	   | LBRACE -> "LBRACE"
	   | RBRACE -> "RBRACE"
	   | LSQUARE -> "LSQUARE"
	   | RSQUARE -> "RSQUARE"
	   | EQ -> "EQ"
	   | COMMA -> "COMMA"
	   | STRING s -> sprintf "STRING(%S)" s
	   | INT i -> sprintf "INT(%Ld)" i
	   | FLOAT f -> sprintf "FLOAT(%g)" f
	   | IDENT s -> sprintf "IDENT(%s)" s
           | EOF -> "EOF");
      r
    with
      exn ->
	prerr_endline (Printexc.to_string exn);
	raise exn
  *)

  (* Lex and parse input.
   *
   * Return the parsed metadata structure if everything went to plan.
   * Raises [Error msg] if there was some parsing problem.
   *)
  let rec parse_lvm2_metadata_from_string str =
    let lexbuf = Lexing.from_string str in
    parse_lvm2_metadata lexbuf
  and parse_lvm2_metadata_from_channel chan =
    let lexbuf = Lexing.from_channel chan in
    parse_lvm2_metadata lexbuf
  and parse_lvm2_metadata lexbuf =
    try
      input token lexbuf
    with
    | Error _ as exn -> raise exn
    | Parsing.Parse_error -> raise (Error "Parse error")
    | exn -> raise (Error ("Exception: " ^ Printexc.to_string exn))
}