| 1 | %{ |
| 2 | (* File: parser.mly |
| 3 | |
| 4 | Copyright (C) 2005- |
| 5 | |
| 6 | Jane Street Holding, LLC |
| 7 | Author: Markus Mottl |
| 8 | email: mmottl@janestcapital.com |
| 9 | WWW: http://www.janestcapital.com/ocaml |
| 10 | |
| 11 | This library is free software; you can redistribute it and/or |
| 12 | modify it under the terms of the GNU Lesser General Public |
| 13 | License as published by the Free Software Foundation; either |
| 14 | version 2 of the License, or (at your option) any later version. |
| 15 | |
| 16 | This library is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | Lesser General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU Lesser General Public |
| 22 | License along with this library; if not, write to the Free Software |
| 23 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 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 { [] } |