src/Pure/Syntax/syntax.ML
changeset 40959 49765c1104d4
parent 39555 ccb223a4d49c
child 41377 390c53904220
equal deleted inserted replaced
40958:755f8fe7ced9 40959:49765c1104d4
   115   val ambiguity_level: int Config.T
   115   val ambiguity_level: int Config.T
   116   val ambiguity_limit: int Config.T
   116   val ambiguity_limit: int Config.T
   117   val standard_parse_term: (term -> string option) ->
   117   val standard_parse_term: (term -> string option) ->
   118     (((string * int) * sort) list -> string * int -> Term.sort) ->
   118     (((string * int) * sort) list -> string * int -> Term.sort) ->
   119     (string -> bool * string) -> (string -> string option) -> Proof.context ->
   119     (string -> bool * string) -> (string -> string option) -> Proof.context ->
   120     (string -> bool) -> syntax -> typ -> Symbol_Pos.T list * Position.T -> term
   120     syntax -> string -> Symbol_Pos.T list * Position.T -> term
   121   val standard_parse_typ: Proof.context -> syntax ->
   121   val standard_parse_typ: Proof.context -> syntax ->
   122     ((indexname * sort) list -> indexname -> sort) -> Symbol_Pos.T list * Position.T -> typ
   122     ((indexname * sort) list -> indexname -> sort) -> Symbol_Pos.T list * Position.T -> typ
   123   val standard_parse_sort: Proof.context -> syntax -> Symbol_Pos.T list * Position.T -> sort
   123   val standard_parse_sort: Proof.context -> syntax -> Symbol_Pos.T list * Position.T -> sort
   124   datatype 'a trrule =
   124   datatype 'a trrule =
   125     ParseRule of 'a * 'a |
   125     ParseRule of 'a * 'a |
   147   val extend_tokentrfuns: (string * string * (Proof.context -> string -> Pretty.T)) list ->
   147   val extend_tokentrfuns: (string * string * (Proof.context -> string -> Pretty.T)) list ->
   148     syntax -> syntax
   148     syntax -> syntax
   149   val update_type_gram: bool -> mode -> (string * typ * mixfix) list -> syntax -> syntax
   149   val update_type_gram: bool -> mode -> (string * typ * mixfix) list -> syntax -> syntax
   150   val update_const_gram: bool -> (string -> bool) ->
   150   val update_const_gram: bool -> (string -> bool) ->
   151     mode -> (string * typ * mixfix) list -> syntax -> syntax
   151     mode -> (string * typ * mixfix) list -> syntax -> syntax
   152   val update_trrules: Proof.context -> (string -> bool) -> syntax ->
   152   val update_trrules: Proof.context -> syntax -> (string * string) trrule list -> syntax -> syntax
   153     (string * string) trrule list -> syntax -> syntax
   153   val remove_trrules: Proof.context -> syntax -> (string * string) trrule list -> syntax -> syntax
   154   val remove_trrules: Proof.context -> (string -> bool) -> syntax ->
       
   155     (string * string) trrule list -> syntax -> syntax
       
   156   val update_trrules_i: ast trrule list -> syntax -> syntax
   154   val update_trrules_i: ast trrule list -> syntax -> syntax
   157   val remove_trrules_i: ast trrule list -> syntax -> syntax
   155   val remove_trrules_i: ast trrule list -> syntax -> syntax
   158 end;
   156 end;
   159 
   157 
   160 structure Syntax: SYNTAX =
   158 structure Syntax: SYNTAX =
   700 val ambiguity_limit =
   698 val ambiguity_limit =
   701   Config.int (Config.declare "syntax_ambiguity_limit" (fn _ => Config.Int 10));
   699   Config.int (Config.declare "syntax_ambiguity_limit" (fn _ => Config.Int 10));
   702 
   700 
   703 fun ambiguity_msg pos = "Parse error: ambiguous syntax" ^ Position.str_of pos;
   701 fun ambiguity_msg pos = "Parse error: ambiguous syntax" ^ Position.str_of pos;
   704 
   702 
   705 fun read_asts ctxt is_logtype (Syntax (tabs, _)) xids root (syms, pos) =
   703 fun read_asts ctxt (Syntax (tabs, _)) xids root (syms, pos) =
   706   let
   704   let
   707     val {lexicon, gram, parse_ast_trtab, ...} = tabs;
   705     val {lexicon, gram, parse_ast_trtab, ...} = tabs;
   708     val toks = Lexicon.tokenize lexicon xids syms;
   706     val toks = Lexicon.tokenize lexicon xids syms;
   709     val _ = List.app (Lexicon.report_token ctxt) toks;
   707     val _ = List.app (Lexicon.report_token ctxt) toks;
   710 
   708 
   711     val root' = if root <> "prop" andalso is_logtype root then Syn_Ext.logic else root;
   709     val pts = Parser.parse ctxt gram root (filter Lexicon.is_proper toks)
   712     val pts = Parser.parse ctxt gram root' (filter Lexicon.is_proper toks)
       
   713       handle ERROR msg =>
   710       handle ERROR msg =>
   714         error (msg ^
   711         error (msg ^
   715           implode (map (Markup.markup Markup.report o Lexicon.reported_token_range ctxt) toks));
   712           implode (map (Markup.markup Markup.report o Lexicon.reported_token_range ctxt) toks));
   716     val len = length pts;
   713     val len = length pts;
   717 
   714 
   731   end;
   728   end;
   732 
   729 
   733 
   730 
   734 (* read *)
   731 (* read *)
   735 
   732 
   736 fun read ctxt is_logtype (syn as Syntax (tabs, _)) ty inp =
   733 fun read ctxt (syn as Syntax (tabs, _)) root inp =
   737   let
   734   let
   738     val {parse_ruletab, parse_trtab, ...} = tabs;
   735     val {parse_ruletab, parse_trtab, ...} = tabs;
   739     val asts = read_asts ctxt is_logtype syn false (Syn_Ext.typ_to_nonterm ty) inp;
   736     val asts = read_asts ctxt syn false root inp;
   740   in
   737   in
   741     Syn_Trans.asts_to_terms ctxt (lookup_tr parse_trtab)
   738     Syn_Trans.asts_to_terms ctxt (lookup_tr parse_trtab)
   742       (map (Ast.normalize_ast (Symtab.lookup_list parse_ruletab)) asts)
   739       (map (Ast.normalize_ast (Symtab.lookup_list parse_ruletab)) asts)
   743   end;
   740   end;
   744 
   741 
   776           (("Ambiguous input, " ^ string_of_int len ^ " terms are type correct" ^
   773           (("Ambiguous input, " ^ string_of_int len ^ " terms are type correct" ^
   777             (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") ::
   774             (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") ::
   778             map show_term (take limit results)))
   775             map show_term (take limit results)))
   779       end;
   776       end;
   780 
   777 
   781 fun standard_parse_term check get_sort map_const map_free ctxt is_logtype syn ty (syms, pos) =
   778 fun standard_parse_term check get_sort map_const map_free ctxt syn root (syms, pos) =
   782   read ctxt is_logtype syn ty (syms, pos)
   779   read ctxt syn root (syms, pos)
   783   |> map (Type_Ext.decode_term get_sort map_const map_free)
   780   |> map (Type_Ext.decode_term get_sort map_const map_free)
   784   |> disambig ctxt check;
   781   |> disambig ctxt check;
   785 
   782 
   786 
   783 
   787 (* read types *)
   784 (* read types *)
   788 
   785 
   789 fun standard_parse_typ ctxt syn get_sort (syms, pos) =
   786 fun standard_parse_typ ctxt syn get_sort (syms, pos) =
   790   (case read ctxt (K false) syn Syn_Ext.typeT (syms, pos) of
   787   (case read ctxt syn (Syn_Ext.typ_to_nonterm Syn_Ext.typeT) (syms, pos) of
   791     [t] => Type_Ext.typ_of_term (get_sort (Type_Ext.term_sorts t)) t
   788     [t] => Type_Ext.typ_of_term (get_sort (Type_Ext.term_sorts t)) t
   792   | _ => error (ambiguity_msg pos));
   789   | _ => error (ambiguity_msg pos));
   793 
   790 
   794 
   791 
   795 (* read sorts *)
   792 (* read sorts *)
   796 
   793 
   797 fun standard_parse_sort ctxt syn (syms, pos) =
   794 fun standard_parse_sort ctxt syn (syms, pos) =
   798   (case read ctxt (K false) syn Type_Ext.sortT (syms, pos) of
   795   (case read ctxt syn (Syn_Ext.typ_to_nonterm Type_Ext.sortT) (syms, pos) of
   799     [t] => Type_Ext.sort_of_term t
   796     [t] => Type_Ext.sort_of_term t
   800   | _ => error (ambiguity_msg pos));
   797   | _ => error (ambiguity_msg pos));
   801 
   798 
   802 
   799 
   803 
   800 
   830     SOME msg =>
   827     SOME msg =>
   831       error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
   828       error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
   832         Ast.str_of_ast lhs ^ "  ->  " ^ Ast.str_of_ast rhs)
   829         Ast.str_of_ast lhs ^ "  ->  " ^ Ast.str_of_ast rhs)
   833   | NONE => rule);
   830   | NONE => rule);
   834 
   831 
   835 fun read_pattern ctxt is_logtype syn (root, str) =
   832 fun read_pattern ctxt syn (root, str) =
   836   let
   833   let
   837     fun constify (ast as Ast.Constant _) = ast
   834     fun constify (ast as Ast.Constant _) = ast
   838       | constify (ast as Ast.Variable x) =
   835       | constify (ast as Ast.Variable x) =
   839           if is_const syn x orelse Long_Name.is_qualified x then Ast.Constant x
   836           if is_const syn x orelse Long_Name.is_qualified x then Ast.Constant x
   840           else ast
   837           else ast
   841       | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
   838       | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
   842 
   839 
   843     val (syms, pos) = read_token str;
   840     val (syms, pos) = read_token str;
   844   in
   841   in
   845     (case read_asts ctxt is_logtype syn true root (syms, pos) of
   842     (case read_asts ctxt syn true root (syms, pos) of
   846       [ast] => constify ast
   843       [ast] => constify ast
   847     | _ => error (ambiguity_msg pos))
   844     | _ => error (ambiguity_msg pos))
   848   end;
   845   end;
   849 
   846 
   850 fun prep_rules rd_pat raw_rules =
   847 fun prep_rules rd_pat raw_rules =
   855 
   852 
   856 in
   853 in
   857 
   854 
   858 val cert_rules = prep_rules I;
   855 val cert_rules = prep_rules I;
   859 
   856 
   860 fun read_rules ctxt is_logtype syn =
   857 fun read_rules ctxt syn = prep_rules (read_pattern ctxt syn);
   861   prep_rules (read_pattern ctxt is_logtype syn);
       
   862 
   858 
   863 end;
   859 end;
   864 
   860 
   865 
   861 
   866 
   862 
   907   (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_types decls);
   903   (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_types decls);
   908 
   904 
   909 fun update_const_gram add is_logtype prmode decls =
   905 fun update_const_gram add is_logtype prmode decls =
   910   (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_consts is_logtype decls);
   906   (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_consts is_logtype decls);
   911 
   907 
   912 fun update_trrules ctxt is_logtype syn =
   908 fun update_trrules ctxt syn =
   913   ext_syntax Syn_Ext.syn_ext_rules o read_rules ctxt is_logtype syn;
   909   ext_syntax Syn_Ext.syn_ext_rules o read_rules ctxt syn;
   914 
   910 
   915 fun remove_trrules ctxt is_logtype syn =
   911 fun remove_trrules ctxt syn =
   916   remove_syntax mode_default o Syn_Ext.syn_ext_rules o read_rules ctxt is_logtype syn;
   912   remove_syntax mode_default o Syn_Ext.syn_ext_rules o read_rules ctxt syn;
   917 
   913 
   918 val update_trrules_i = ext_syntax Syn_Ext.syn_ext_rules o cert_rules;
   914 val update_trrules_i = ext_syntax Syn_Ext.syn_ext_rules o cert_rules;
   919 val remove_trrules_i = remove_syntax mode_default o Syn_Ext.syn_ext_rules o cert_rules;
   915 val remove_trrules_i = remove_syntax mode_default o Syn_Ext.syn_ext_rules o cert_rules;
   920 
   916 
   921 
   917