]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/grafite_parser/grafiteParser.ml
Huge reorganization of matita and ocaml.
[helm.git] / helm / ocaml / grafite_parser / grafiteParser.ml
index 5b9cea37a8cf9aac01fd186e7b214cd25735af3b..d32eb32656313b90df47f9eef3c0c170f88594e0 100644 (file)
@@ -27,10 +27,17 @@ open Printf
 
 module Ast = CicNotationPt
 
+type 'a localized_option =
+   LSome of 'a
+ | LNone of Token.flocation
+
 type statement =
-  (CicNotationPt.term, CicNotationPt.term, CicNotationPt.term GrafiteAst.reduction,
-   CicNotationPt.obj, string)
-    GrafiteAst.statement
+ include_paths:string list ->
+ LexiconEngine.status ->
+  LexiconEngine.status *
+  (CicNotationPt.term, CicNotationPt.term,
+   CicNotationPt.term GrafiteAst.reduction, CicNotationPt.obj, string)
+    GrafiteAst.statement localized_option
 
 let grammar = CicNotationParser.level2_ast_grammar
 
@@ -353,7 +360,7 @@ EXTEND
         if (try ignore (UriManager.uri_of_string uri); true
             with UriManager.IllFormedUri _ -> false)
         then
-          GrafiteAst.Ident_alias (id, uri)
+          LexiconAst.Ident_alias (id, uri)
         else 
           raise
            (HExtlib.Localized (loc, CicNotationParser.Parse_error (sprintf "Not a valid uri: %s" uri)))
@@ -366,14 +373,14 @@ EXTEND
         let instance =
           match instance with Some i -> i | None -> 0
         in
-        GrafiteAst.Symbol_alias (symbol, instance, dsc)
+        LexiconAst.Symbol_alias (symbol, instance, dsc)
     | IDENT "num";
       instance = OPT [ LPAREN; IDENT "instance"; n = int; RPAREN -> n ];
       SYMBOL "="; dsc = QSTRING ->
         let instance =
           match instance with Some i -> i | None -> 0
         in
-        GrafiteAst.Number_alias (instance, dsc)
+        LexiconAst.Number_alias (instance, dsc)
     ]
   ];
   argument: [
@@ -439,7 +446,12 @@ EXTEND
         (s, args, t)
     ]
   ];
-  command: [ [
+  
+  include_command: [ [
+      IDENT "include" ; path = QSTRING -> loc,path
+   ]];
+
+  grafite_command: [ [
       IDENT "set"; n = QSTRING; v = QSTRING ->
         GrafiteAst.Set (loc, n, v)
     | IDENT "drop" -> GrafiteAst.Drop loc
@@ -480,27 +492,23 @@ EXTEND
         GrafiteAst.Obj (loc, Ast.Inductive (params, ind_types))
     | IDENT "coercion" ; suri = URI ->
         GrafiteAst.Coercion (loc, UriManager.uri_of_string suri, true)
-    | IDENT "alias" ; spec = alias_spec ->
-        GrafiteAst.Alias (loc, spec)
     | IDENT "record" ; (params,name,ty,fields) = record_spec ->
         GrafiteAst.Obj (loc, Ast.Record (params,name,ty,fields))
-    | IDENT "include" ; path = QSTRING ->
-        GrafiteAst.Include (loc,path)
     | IDENT "default" ; what = QSTRING ; uris = LIST1 URI ->
        let uris = List.map UriManager.uri_of_string uris in
         GrafiteAst.Default (loc,what,uris)
+  ]];
+  lexicon_command: [ [
+      IDENT "alias" ; spec = alias_spec ->
+        LexiconAst.Alias (loc, spec)
     | IDENT "notation"; (dir, l1, assoc, prec, l2) = notation ->
-        GrafiteAst.Notation (loc, dir, l1, assoc, prec, l2)
+        LexiconAst.Notation (loc, dir, l1, assoc, prec, l2)
     | IDENT "interpretation"; id = QSTRING;
       (symbol, args, l3) = interpretation ->
-        GrafiteAst.Interpretation (loc, id, (symbol, args), l3)
-
-    | IDENT "dump" -> GrafiteAst.Dump loc
-    | IDENT "render"; u = URI ->
-        GrafiteAst.Render (loc, UriManager.uri_of_string u)
+        LexiconAst.Interpretation (loc, id, (symbol, args), l3)
   ]];
   executable: [
-    [ cmd = command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
+    [ cmd = grafite_command; SYMBOL "." -> GrafiteAst.Command (loc, cmd)
     | tac = tactical; punct = punctuation_tactical ->
         GrafiteAst.Tactical (loc, tac, Some punct)
     | punct = punctuation_tactical -> GrafiteAst.Tactical (loc, punct, None)
@@ -515,8 +523,25 @@ EXTEND
     ]
   ];
   statement: [
-    [ ex = executable -> GrafiteAst.Executable (loc,ex)
-    | com = comment -> GrafiteAst.Comment (loc, com)
+    [ ex = executable ->
+       fun ~include_paths status -> status,LSome(GrafiteAst.Executable (loc,ex))
+    | com = comment ->
+       fun ~include_paths status -> status,LSome (GrafiteAst.Comment (loc, com))
+    | (iloc,fname) = include_command ; SYMBOL "."  ->
+       fun ~include_paths status ->
+        let path = DependenciesParser.baseuri_of_script ~include_paths fname in
+        let status =
+         LexiconEngine.eval_command status (LexiconAst.Include (iloc,path))
+        in
+         status,
+          LSome
+          (GrafiteAst.Executable
+           (loc,GrafiteAst.Command
+            (loc,GrafiteAst.Include (iloc,path))))
+    | scom = lexicon_command ; SYMBOL "." ->
+       fun ~include_paths status ->
+        let status = LexiconEngine.eval_command status scom in
+         status,LNone loc
     | EOI -> raise End_of_file
     ]
   ];
@@ -536,22 +561,3 @@ let exc_located_wrapper f =
 let parse_statement lexbuf =
   exc_located_wrapper
     (fun () -> (Grammar.Entry.parse statement (Obj.magic lexbuf)))
-
-let parse_dependencies lexbuf = 
-  let tok_stream,_ =
-    CicNotationLexer.level2_ast_lexer.Token.tok_func (Obj.magic lexbuf)
-  in
-  let rec parse acc = 
-    (parser
-    | [< '("URI", u) >] ->
-        parse (GrafiteAst.UriDep (UriManager.uri_of_string u) :: acc)
-    | [< '("IDENT", "include"); '("QSTRING", fname) >] ->
-        parse (GrafiteAst.IncludeDep fname :: acc)
-    | [< '("IDENT", "set"); '("QSTRING", "baseuri"); '("QSTRING", baseuri) >] ->
-        parse (GrafiteAst.BaseuriDep baseuri :: acc)
-    | [< '("EOI", _) >] -> acc
-    | [< 'tok >] -> parse acc
-    | [<  >] -> acc) tok_stream
-  in
-  List.rev (parse [])
-