~ubuntu-branches/ubuntu/trusty/sexplib310/trusty

« back to all changes in this revision

Viewing changes to lib/parser_with_layout.mly

  • Committer: Package Import Robot
  • Author(s): Stéphane Glondu
  • Date: 2013-12-03 21:36:45 UTC
  • mfrom: (11.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20131203213645-h1if1c6hxual8p11
Tags: 109.20.00-2
* Team upload
* Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%{
 
2
  (** Parser: Grammar Specification for Parsing S-expressions *)
 
3
  (* compare to parser.mly *)
 
4
  (*  *) (* C inside Ocaml comment to satisfy cr *)
 
5
 
 
6
  open Lexing
 
7
 
 
8
  let parse_failure what =
 
9
    let pos = Parsing.symbol_start_pos () in
 
10
    let msg =
 
11
      Printf.sprintf "Sexplib.Parser: failed to parse line %d char %d: %s"
 
12
        pos.pos_lnum (pos.pos_cnum - pos.pos_bol) what in
 
13
    failwith msg
 
14
 
 
15
  module With_pos = struct
 
16
 
 
17
    open Type_with_layout.Parsed
 
18
 
 
19
    let coerce = Src_pos.Absolute.of_lexing
 
20
 
 
21
    let start_pos () = coerce (Parsing.symbol_start_pos ())
 
22
 
 
23
    let end_pos () =
 
24
      let p = Parsing.symbol_end_pos () in
 
25
      coerce { p with Lexing.pos_cnum = p.Lexing.pos_cnum - 1 }
 
26
 
 
27
    let atom (x, y) =
 
28
      let (pos, y) =
 
29
        match y with
 
30
        | None -> (start_pos (), None)
 
31
        | Some (pos, x) -> (coerce pos, Some x)
 
32
      in
 
33
      Atom (pos, x, y)
 
34
 
 
35
    let list ts = List (start_pos (), ts, end_pos ())
 
36
 
 
37
    let sexp    x = Sexp    x
 
38
    let comment x = Comment x
 
39
 
 
40
    let sexp_comment cs t = Sexp_comment (start_pos (), cs, t)
 
41
 
 
42
    let plain_comment (x, pos_opt) =
 
43
      let pos =
 
44
        match pos_opt with
 
45
        | None -> start_pos ()
 
46
        | Some pos -> coerce pos
 
47
      in
 
48
      Plain_comment (pos, x)
 
49
 
 
50
  end
 
51
 
 
52
%}
 
53
 
 
54
%token <string * (Lexing.position * string) option> STRING
 
55
%token <string * Lexing.position option> COMMENT
 
56
%token LPAREN RPAREN EOF HASH_SEMI
 
57
 
 
58
%start sexp
 
59
%type <Type_with_layout.t_or_comment> sexp
 
60
 
 
61
%start sexp_opt
 
62
%type <Type_with_layout.t_or_comment option> sexp_opt
 
63
 
 
64
%start sexps
 
65
%type <Type_with_layout.t_or_comment list> sexps
 
66
 
 
67
%start sexps_abs
 
68
%type <Type_with_layout.Parsed.t_or_comment list> sexps_abs
 
69
 
 
70
%start rev_sexps
 
71
%type <Type_with_layout.t_or_comment list> rev_sexps
 
72
 
 
73
%%
 
74
 
 
75
sexp_but_no_comment_abs
 
76
  : STRING { With_pos.atom $1 }
 
77
  | LPAREN rev_sexps_abs RPAREN { With_pos.list (List.rev $2) }
 
78
  | error { parse_failure "sexp" }
 
79
 
 
80
comment_abs
 
81
  : COMMENT { With_pos.plain_comment $1 }
 
82
  | HASH_SEMI rev_comments_abs sexp_but_no_comment_abs { With_pos.sexp_comment (List.rev $2) $3 }
 
83
 
 
84
rev_comments_abs
 
85
  : /* nothing */ { [] }
 
86
  | rev_comments_abs comment_abs { $2 :: $1 }
 
87
 
 
88
sexp_abs
 
89
  : sexp_but_no_comment_abs { With_pos.sexp $1 }
 
90
  | comment_abs { With_pos.comment $1 }
 
91
 
 
92
rev_sexps_abs
 
93
  : /* empty */ { [] }
 
94
  | rev_sexps_abs sexp_abs { $2 :: $1 }
 
95
 
 
96
sexp
 
97
  : sexp_abs { Type_with_layout.relativize $1 }
 
98
 
 
99
sexp_opt
 
100
  : sexp { Some $1 }
 
101
  | EOF { None }
 
102
 
 
103
rev_sexps_aux
 
104
  : sexp { [$1] }
 
105
  | rev_sexps_aux sexp { $2 :: $1 }
 
106
 
 
107
rev_sexps
 
108
  : rev_sexps_aux EOF { $1 }
 
109
  | EOF { [] }
 
110
 
 
111
sexps
 
112
  : rev_sexps_aux EOF { List.rev $1 }
 
113
  | EOF { [] }
 
114
 
 
115
/* for debugging positions */
 
116
sexps_abs
 
117
  : rev_sexps_abs EOF { List.rev $1 }
 
118