Commit | Line | Data |
---|---|---|
feec80c3 C |
1 | %{ |
2 | (***************************************************************************** | |
3 | * Sexplib * | |
4 | * * | |
5 | * Copyright (C) 2005- Jane Street Holding, LLC * | |
6 | * Contact: opensource@janestreet.com * | |
7 | * WWW: http://www.janestreet.com/ocaml * | |
8 | * Author: Markus Mottl * | |
9 | * * | |
10 | * This library is free software; you can redistribute it and/or * | |
11 | * modify it under the terms of the GNU Lesser General Public * | |
12 | * License as published by the Free Software Foundation; either * | |
13 | * version 2 of the License, or (at your option) any later version. * | |
14 | * * | |
15 | * This library is distributed in the hope that it will be useful, * | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * | |
18 | * Lesser General Public License for more details. * | |
19 | * * | |
20 | * You should have received a copy of the GNU Lesser General Public * | |
21 | * License along with this library; if not, write to the Free Software * | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
23 | * * | |
24 | *****************************************************************************) | |
25 | ||
26 | (** Parser: Grammar Specification for Parsing S-expressions *) | |
27 | ||
28 | open Lexing | |
29 | ||
30 | let parse_failure what = | |
31 | let pos = symbol_start_pos () in | |
32 | let msg = | |
33 | Printf.sprintf "Sexplib.Parser: failed to parse line %d char %d: %s" | |
34 | pos.pos_lnum (pos.pos_cnum - pos.pos_bol) what in | |
35 | failwith msg | |
36 | %} | |
37 | ||
38 | %token <string> STRING | |
39 | %token LPAREN RPAREN EOF | |
40 | ||
41 | %start sexp | |
42 | %type <Type.t> sexp | |
43 | ||
44 | %start sexp_opt | |
45 | %type <Type.t option> sexp_opt | |
46 | ||
47 | %start sexps | |
48 | %type <Type.t list> sexps | |
49 | ||
50 | %start rev_sexps | |
51 | %type <Type.t list> rev_sexps | |
52 | ||
53 | %% | |
54 | ||
55 | sexp | |
56 | : STRING { Type.Atom $1 } | |
57 | | LPAREN RPAREN { Type.List [] } | |
58 | | LPAREN rev_sexps_aux RPAREN { Type.List (List.rev $2) } | |
59 | | error { parse_failure "sexp" } | |
60 | ||
61 | sexp_opt | |
62 | : sexp { Some $1 } | |
63 | | EOF { None } | |
64 | ||
65 | rev_sexps_aux | |
66 | : sexp { [$1] } | |
67 | | rev_sexps_aux sexp { $2 :: $1 } | |
68 | ||
69 | rev_sexps | |
70 | : rev_sexps_aux { $1 } | |
71 | | EOF { [] } | |
72 | ||
73 | sexps | |
74 | : rev_sexps_aux { List.rev $1 } | |
75 | | EOF { [] } |