1 (* 'df' command for virtual domains.
2 (C) Copyright 2007-2008 Richard W.M. Jones, Red Hat Inc.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version,
9 with the OCaml linking exception described in ../COPYING.LIB.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 (* Scanner for LVM2 metadata.
23 * http://plus.kaist.ac.kr/~shoh/ocaml/ocamllex-ocamlyacc/ocamllex-tutorial/
31 open Diskimage_lvm2_parser
33 (* Temporary buffer used for parsing strings, etc. *)
34 let tmp = Buffer.create 80
36 exception Error of string
40 let alpha = ['a'-'z' 'A'-'Z']
41 let alphau = ['a'-'z' 'A'-'Z' '_']
42 let alnum = ['a'-'z' 'A'-'Z' '0'-'9']
43 let alnumu = ['a'-'z' 'A'-'Z' '0'-'9' '_']
44 let ident = alphau alnumu*
46 let whitespace = [' ' '\t' '\r' '\n']+
48 let escaped_char = '\\' _
51 (* ignore whitespace and comments *)
56 (* scan single character tokens *)
64 (* strings - see LVM2/lib/config/config.c *)
68 STRING (dq_string lexbuf)
73 STRING (dq_string lexbuf)
77 | ('-'? digit+ '.' digit*) as f
79 let f = float_of_string f in
86 let i = Int63.of_string i in
99 { raise (Error (sprintf "%c: invalid character in input" c)) }
101 and dq_string = parse
103 { Buffer.contents tmp }
104 | escaped_char as str
105 { Buffer.add_char tmp str.[1]; dq_string lexbuf }
107 { raise (Error "unterminated string in metadata") }
109 { Buffer.add_char tmp c; dq_string lexbuf }
113 { Buffer.contents tmp }
114 | escaped_char as str
115 { Buffer.add_char tmp str.[1]; q_string lexbuf }
117 { raise (Error "unterminated string in metadata") }
119 { Buffer.add_char tmp c; q_string lexbuf }
122 (* Demonstration of how to wrap the token function
123 with extra debugging statements:
126 let r = token lexbuf in
128 eprintf "Lexer: token returned is %s\n"
132 | LSQUARE -> "LSQUARE"
133 | RSQUARE -> "RSQUARE"
136 | STRING s -> sprintf "STRING(%S)" s
137 | INT i -> sprintf "INT(%Ld)" i
138 | FLOAT f -> sprintf "FLOAT(%g)" f
139 | IDENT s -> sprintf "IDENT(%s)" s
144 prerr_endline (Printexc.to_string exn);
148 (* Lex and parse input.
150 * Return the parsed metadata structure if everything went to plan.
151 * Raises [Error msg] if there was some parsing problem.
153 let rec parse_lvm2_metadata_from_string str =
154 let lexbuf = Lexing.from_string str in
155 parse_lvm2_metadata lexbuf
156 and parse_lvm2_metadata_from_channel chan =
157 let lexbuf = Lexing.from_channel chan in
158 parse_lvm2_metadata lexbuf
159 and parse_lvm2_metadata lexbuf =
163 | Error _ as exn -> raise exn
164 | Parsing.Parse_error -> raise (Error "Parse error")
165 | exn -> raise (Error ("Exception: " ^ Printexc.to_string exn))