]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/content_pres/cicNotationLexer.ml
more push/pop to avoid confusion with imperative data structures employed by
[helm.git] / helm / software / components / content_pres / cicNotationLexer.ml
index 00696cc00d2cec8b36e4a3a4a1c8d45000331a52..f80821a5f1b6562cb77f7fe55f0bb7d62ffc0d42 100644 (file)
@@ -31,6 +31,11 @@ exception Error of int * int * string
 
 let regexp number = xml_digit+
 let regexp utf8_blank = " " | "\r\n" | "\n" | "\t" | [160] (* this is a nbsp *)
+let regexp floatwithunit = 
+  [ '0' - '9' ] + ["."] [ '0' - '9' ] + ([ 'a' - 'z' ] + | "" )
+let regexp color = "#" [ '0' - '9' 'a' - 'f' 'A' - 'F' ] [ '0' - '9' 'a' - 'f'
+'A' - 'F' ] [ '0' - '9' 'a' - 'f' 'A' - 'F' ] [ '0' - '9' 'a' - 'f' 'A' - 'F' ]
+[ '0' - '9' 'a' - 'f' 'A' - 'F' ] [ '0' - '9' 'a' - 'f' 'A' - 'F' ]
 
   (* ZACK: breaks unicode's binder followed by an ascii letter without blank *)
 (* let regexp ident_letter = xml_letter *)
@@ -98,11 +103,11 @@ let level1_keywords =
     "break";
     "list0"; "list1"; "sep";
     "opt";
-    "term"; "ident"; "number"
+    "term"; "ident"; "number"; "mstyle"
   ] @ level1_layouts
 
 let level2_meta_keywords =
-  [ "if"; "then"; "else";
+  [ "if"; "then"; "elCicNotationParser.se";
     "fold"; "left"; "right"; "rec";
     "fail";
     "default";
@@ -110,18 +115,25 @@ let level2_meta_keywords =
   ]
 
   (* (string, unit) Hashtbl.t, to exploit multiple bindings *)
-let level2_ast_keywords = Hashtbl.create 23
-let _ =
-  List.iter (fun k -> Hashtbl.add level2_ast_keywords k ())
+let initial_level2_ast_keywords () = Hashtbl.create 23;;
+
+let level2_ast_keywords = ref (initial_level2_ast_keywords ())
+
+let initialize_keywords () =
+  List.iter (fun k -> Hashtbl.add !level2_ast_keywords k ())
   [ "CProp"; "Prop"; "Type"; "Set"; "let"; "match";
   "with"; "in"; "and"; "to"; "as"; "on"; "return"; "done" ]
+;;
+
+let _ = initialize_keywords ();;
 
-let add_level2_ast_keyword k = Hashtbl.add level2_ast_keywords k ()
-let remove_level2_ast_keyword k = Hashtbl.remove level2_ast_keywords k
+let add_level2_ast_keyword k = Hashtbl.add !level2_ast_keywords k ()
+let remove_level2_ast_keyword k = Hashtbl.remove !level2_ast_keywords k
 
   (* (string, int) Hashtbl.t, with multiple bindings.
    * int is the unicode codepoint *)
 let ligatures = Hashtbl.create 23
+
 let _ =
   List.iter
     (fun (ligature, symbol) -> Hashtbl.add ligatures ligature symbol)
@@ -290,7 +302,7 @@ and level2_ast_token =
   | placeholder -> return lexbuf ("PLACEHOLDER", "")
   | ident ->
       let lexeme = Ulexing.utf8_lexeme lexbuf in
-      if Hashtbl.mem level2_ast_keywords lexeme then
+      if Hashtbl.mem !level2_ast_keywords lexeme then
         return lexbuf ("", lexeme)
       else
         return lexbuf ("IDENT", lexeme)
@@ -333,6 +345,9 @@ and level1_pattern_token =
          else
            return lexbuf ("IDENT", s)
        end
+  | color -> return lexbuf ("COLOR", Ulexing.utf8_lexeme lexbuf)
+  | floatwithunit -> 
+      return lexbuf ("FLOATWITHUNIT", Ulexing.utf8_lexeme lexbuf)
   | tex_token -> return lexbuf (expand_macro lexbuf)
   | qkeyword ->
       return lexbuf ("QKEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf))
@@ -346,9 +361,18 @@ let level2_ast_token = ligatures_token level2_ast_token
 
 (* API implementation *)
 
-let level1_pattern_lexer = mk_lexer level1_pattern_token
-let level2_ast_lexer = mk_lexer level2_ast_token
-let level2_meta_lexer = mk_lexer level2_meta_token
+let initial_level1_pattern_lexer () = mk_lexer level1_pattern_token
+let initial_level2_ast_lexer () = mk_lexer level2_ast_token
+let initial_level2_meta_lexer () = mk_lexer level2_meta_token
+
+
+let level1_pattern_lexer_ref = ref (initial_level1_pattern_lexer ())
+let level2_ast_lexer_ref = ref (initial_level2_ast_lexer ())
+let level2_meta_lexer_ref = ref (initial_level2_meta_lexer ())
+
+let level1_pattern_lexer () = !level1_pattern_lexer_ref
+let level2_ast_lexer () = !level2_ast_lexer_ref
+let level2_meta_lexer () = !level2_meta_lexer_ref 
 
 let lookup_ligatures lexeme =
   try
@@ -356,4 +380,29 @@ let lookup_ligatures lexeme =
     then [ Utf8Macro.expand (String.sub lexeme 1 (String.length lexeme - 1)) ]
     else List.rev (Hashtbl.find_all ligatures lexeme)
   with Invalid_argument _ | Utf8Macro.Macro_not_found _ -> []
+;;
+
+let history = ref [];;
+
+let push () =
+  history :=
+    (!level2_ast_keywords,!level1_pattern_lexer_ref,
+     !level2_ast_lexer_ref,!level2_meta_lexer_ref) :: !history;
+  level2_ast_keywords := initial_level2_ast_keywords ();
+  initialize_keywords ();
+  level1_pattern_lexer_ref := initial_level1_pattern_lexer ();
+  level2_ast_lexer_ref := initial_level2_ast_lexer ();
+  level2_meta_lexer_ref := initial_level2_meta_lexer ();
+;;
+
+let pop () =
+  match !history with
+  | [] -> assert false
+  | (kwd,pl,al,ml) :: tl -> 
+      level2_ast_keywords := kwd;
+      level1_pattern_lexer_ref := pl;
+      level2_ast_lexer_ref := al;
+      level2_meta_lexer_ref := ml;
+      history := tl
+;;