]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationParser.ml
first check-in of cic_notation
[helm.git] / helm / ocaml / cic_notation / cicNotationParser.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 exception Parse_error of Token.flocation * string
29
30 let grammar = Grammar.gcreate CicNotationLexer.level1_lexer
31
32 let level1 = Grammar.Entry.create grammar "level1"
33
34 let return_term loc term = ()
35
36 (*let fail floc msg =*)
37 (*  let (x, y) = CicAst.loc_of_floc floc in*)
38 (*  failwith (sprintf "Error at characters %d - %d: %s" x y msg)*)
39
40 let int_of_string s =
41   try
42     Pervasives.int_of_string s
43   with Failure _ ->
44     failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
45
46 EXTEND
47   GLOBAL: level1;
48
49   level1: [ [ p = pattern -> () ] ];
50
51   pattern: [ [ p = LIST1 simple_pattern -> () ] ];
52
53   literal: [
54     [ s = SYMBOL -> ()
55     | k = KEYWORD -> ()
56     ]
57   ];
58
59   sep:       [ [ SYMBOL "\\SEP";      sep = literal -> () ] ];
60   row_sep:   [ [ SYMBOL "\\ROWSEP";   sep = literal -> () ] ];
61   field_sep: [ [ SYMBOL "\\FIELDSEP"; sep = literal -> () ] ];
62
63   box_token: [
64     [ SYMBOL "\\HBOX"; p = simple_pattern -> ()
65     | SYMBOL "\\VBOX"; p = simple_pattern -> ()
66     | SYMBOL "\\BREAK" -> ()
67     ]
68   ];
69
70   layout_schemata: [
71     [ SYMBOL "\\ARRAY"; p = simple_pattern; fsep = OPT field_sep;
72       rsep = OPT row_sep ->
73         ()
74     | SYMBOL "\\FRAC"; p1 = simple_pattern; p2 = simple_pattern -> ()
75     | SYMBOL "\\SQRT"; p = simple_pattern -> ()
76     | SYMBOL "\\ROOT"; p1 = simple_pattern; SYMBOL "\\OF";
77       p2 = simple_pattern ->
78         ()
79      (* TODO XXX many issues here:
80       * - "^^" is lexed as two "^" symbols
81       * - "a_b" is lexed as IDENT "a_b" *)
82     | p1 = simple_pattern; SYMBOL "^"; p2 = simple_pattern -> ()
83     | p1 = simple_pattern; SYMBOL "^"; SYMBOL "^"; p2 = simple_pattern -> ()
84     | p1 = simple_pattern; SYMBOL "_"; p2 = simple_pattern -> ()
85     | p1 = simple_pattern; SYMBOL "_"; SYMBOL "_"; p2 = simple_pattern -> ()
86     ]
87   ];
88
89   simple_pattern: [
90     [ SYMBOL "\\LIST0"; p = simple_pattern; sep = OPT sep -> ()
91     | SYMBOL "\\LIST1"; p = simple_pattern; sep = OPT sep -> ()
92     | b = box_token -> ()
93     | id = IDENT -> ()
94     | SYMBOL "\\NUM"; id = IDENT -> ()
95     | SYMBOL "\\IDENT"; id = IDENT -> ()
96     | SYMBOL "\\OPT"; p = simple_pattern -> ()
97     | l = layout_schemata -> ()
98     | SYMBOL "["; p = pattern; SYMBOL "]" -> ()
99     ]
100   ];
101 END
102
103 let exc_located_wrapper f =
104   try
105     f ()
106   with
107   | Stdpp.Exc_located (floc, Stream.Error msg) ->
108       raise (Parse_error (floc, msg))
109   | Stdpp.Exc_located (floc, exn) ->
110       raise (Parse_error (floc, (Printexc.to_string exn)))
111
112 let parse_level1_pattern stream =
113   exc_located_wrapper (fun () -> (Grammar.Entry.parse level1 stream))
114
115 (* vim:set encoding=utf8: *)