src/Pure/Syntax/syntax.ML
author oheimb
Wed Oct 27 11:15:35 1999 +0200 (1999-10-27)
changeset 7944 cc1930ad1a88
parent 7025 afbd8241797b
child 8720 840c75ab2a7f
permissions -rw-r--r--
symbols in (error) messages now consistently with single backslash
     1 (*  Title:      Pure/Syntax/syntax.ML
     2     ID:         $Id$
     3     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     4 
     5 Root of Isabelle's syntax module.
     6 *)
     7 
     8 signature BASIC_SYNTAX =
     9 sig
    10   include AST0
    11   include SYN_TRANS0
    12   include MIXFIX0
    13   include PRINTER0
    14 end;
    15 
    16 signature SYNTAX =
    17 sig
    18   include TOKEN_TRANS0
    19   include AST1
    20   include LEXICON0
    21   include SYN_EXT0
    22   include TYPE_EXT0
    23   include SYN_TRANS1
    24   include MIXFIX1
    25   include PRINTER0
    26   datatype 'a trrule =
    27     ParseRule of 'a * 'a |
    28     PrintRule of 'a * 'a |
    29     ParsePrintRule of 'a * 'a
    30   type syntax
    31   val extend_log_types: syntax -> string list -> syntax
    32   val extend_type_gram: syntax -> (string * int * mixfix) list -> syntax
    33   val extend_const_gram: syntax -> string * bool -> (string * typ * mixfix) list -> syntax
    34   val extend_consts: syntax -> string list -> syntax
    35   val extend_trfuns: syntax ->
    36     (string * (ast list -> ast)) list *
    37     (string * (term list -> term)) list *
    38     (string * (term list -> term)) list *
    39     (string * (ast list -> ast)) list -> syntax
    40   val extend_trfunsT: syntax -> (string * (bool -> typ -> term list -> term)) list -> syntax
    41   val extend_tokentrfuns: syntax -> (string * string * (string -> string * real)) list -> syntax
    42   val extend_trrules: syntax -> (string * string) trrule list -> syntax
    43   val extend_trrules_i: syntax -> ast trrule list -> syntax
    44   val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
    45   val merge_syntaxes: syntax -> syntax -> syntax
    46   val type_syn: syntax
    47   val pure_syn: syntax
    48   val print_gram: syntax -> unit
    49   val print_trans: syntax -> unit
    50   val print_syntax: syntax -> unit
    51   val test_read: syntax -> string -> string -> unit
    52   val read: syntax -> typ -> string -> term list
    53   val read_typ: syntax -> ((indexname * sort) list -> indexname -> sort) -> string -> typ
    54   val simple_read_typ: string -> typ
    55   val pretty_term: syntax -> bool -> term -> Pretty.T
    56   val pretty_typ: syntax -> typ -> Pretty.T
    57   val pretty_sort: syntax -> sort -> Pretty.T
    58   val simple_str_of_sort: sort -> string
    59   val simple_string_of_typ: typ -> string
    60   val simple_pprint_typ: typ -> pprint_args -> unit
    61   val ambiguity_level: int ref
    62 end;
    63 
    64 structure Syntax : SYNTAX =
    65 struct
    66 
    67 
    68 (** tables of translation functions **)
    69 
    70 fun mk_trfun (c, f) = (c, (f, stamp ()));
    71 fun eq_trfuns ((c1:string, (_, s1:stamp)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
    72 
    73 
    74 (* parse (ast) translations *)
    75 
    76 fun lookup_tr tab c = apsome fst (Symtab.lookup (tab, c));
    77 
    78 fun err_dup_trfuns name cs =
    79   error ("More than one " ^ name ^ " for " ^ commas_quote cs);
    80 
    81 fun extend_trtab tab trfuns name =
    82   Symtab.extend (tab, map mk_trfun trfuns)
    83     handle Symtab.DUPS cs => err_dup_trfuns name cs;
    84 
    85 fun merge_trtabs tab1 tab2 name =
    86   Symtab.merge eq_snd (tab1, tab2)
    87     handle Symtab.DUPS cs => err_dup_trfuns name cs;
    88 
    89 
    90 (* print (ast) translations *)
    91 
    92 fun lookup_tr' tab c = map fst (Symtab.lookup_multi (tab, c));
    93 
    94 fun extend_tr'tab tab trfuns =
    95   generic_extend eq_trfuns Symtab.dest_multi Symtab.make_multi tab (map mk_trfun trfuns);
    96 
    97 fun merge_tr'tabs tabs = generic_merge eq_trfuns Symtab.dest_multi Symtab.make_multi tabs;
    98 
    99 
   100 
   101 (** tables of token translation functions **)
   102 
   103 fun lookup_tokentr tabs modes =
   104   let val trs = gen_distinct eq_fst (flat (map (assocs tabs) (modes @ [""])))
   105   in fn c => apsome fst (assoc (trs, c)) end;
   106 
   107 fun merge_tokentrtabs tabs1 tabs2 =
   108   let
   109     fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
   110 
   111     fun name (s, _) = implode (tl (Symbol.explode s));
   112 
   113     fun merge mode =
   114       let
   115         val trs1 = assocs tabs1 mode;
   116         val trs2 = assocs tabs2 mode;
   117         val trs = gen_distinct eq_tr (trs1 @ trs2);
   118       in
   119         (case gen_duplicates eq_fst trs of
   120           [] => (mode, trs)
   121         | dups => error ("More than one token translation function in mode " ^
   122             quote mode ^ " for " ^ commas_quote (map name dups)))
   123       end;
   124   in
   125     map merge (distinct (map fst (tabs1 @ tabs2)))
   126   end;
   127 
   128 fun extend_tokentrtab tabs tokentrs =
   129   let
   130     fun ins_tokentr (ts, (m, c, f)) =
   131       overwrite (ts, (m, ("_" ^ c, (f, stamp ())) :: assocs ts m));
   132   in
   133     merge_tokentrtabs tabs (foldl ins_tokentr ([], tokentrs))
   134   end;
   135 
   136 
   137 
   138 (** tables of translation rules **)
   139 
   140 type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
   141 
   142 fun dest_ruletab tab = flat (map snd (Symtab.dest tab));
   143 
   144 
   145 (* lookup_ruletab *)
   146 
   147 fun lookup_ruletab tab =
   148   if Symtab.is_empty tab then None
   149   else Some (fn a => Symtab.lookup_multi (tab, a));
   150 
   151 
   152 (* empty, extend, merge ruletabs *)
   153 
   154 fun extend_ruletab tab rules =
   155   generic_extend (op =) Symtab.dest_multi Symtab.make_multi tab
   156     (map (fn r => (Ast.head_of_rule r, r)) (distinct rules));
   157 
   158 fun merge_ruletabs tab1 tab2 =
   159   generic_merge (op =) Symtab.dest_multi Symtab.make_multi tab1 tab2;
   160 
   161 
   162 
   163 (** datatype syntax **)
   164 
   165 datatype syntax =
   166   Syntax of {
   167     lexicon: Scan.lexicon,
   168     logtypes: string list,
   169     gram: Parser.gram,
   170     consts: string list,
   171     prmodes: string list,
   172     parse_ast_trtab: ((Ast.ast list -> Ast.ast) * stamp) Symtab.table,
   173     parse_ruletab: ruletab,
   174     parse_trtab: ((term list -> term) * stamp) Symtab.table,
   175     print_trtab: ((bool -> typ -> term list -> term) * stamp) list Symtab.table,
   176     print_ruletab: ruletab,
   177     print_ast_trtab: ((Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
   178     tokentrtab: (string * (string * ((string -> string * real) * stamp)) list) list,
   179     prtabs: Printer.prtabs}
   180 
   181 
   182 (* empty_syntax *)
   183 
   184 val empty_syntax =
   185   Syntax {
   186     lexicon = Scan.empty_lexicon,
   187     logtypes = [],
   188     gram = Parser.empty_gram,
   189     consts = [],
   190     prmodes = [],
   191     parse_ast_trtab = Symtab.empty,
   192     parse_ruletab = Symtab.empty,
   193     parse_trtab = Symtab.empty,
   194     print_trtab = Symtab.empty,
   195     print_ruletab = Symtab.empty,
   196     print_ast_trtab = Symtab.empty,
   197     tokentrtab = [],
   198     prtabs = Printer.empty_prtabs}
   199 
   200 
   201 (* extend_syntax *)
   202 
   203 fun extend_syntax (mode, inout) (Syntax tabs) syn_ext =
   204   let
   205     val {lexicon, logtypes = logtypes1, gram, consts = consts1, prmodes = prmodes1,
   206       parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
   207       print_ast_trtab, tokentrtab, prtabs} = tabs;
   208     val SynExt.SynExt {logtypes = logtypes2, xprods, consts = consts2, prmodes = prmodes2,
   209       parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
   210       print_ast_translation, token_translation} = syn_ext;
   211   in
   212     Syntax {
   213       lexicon = if inout then Scan.extend_lexicon lexicon (SynExt.delims_of xprods) else lexicon,
   214       logtypes = extend_list logtypes1 logtypes2,
   215       gram = if inout then Parser.extend_gram gram xprods else gram,
   216       consts = consts2 union consts1,
   217       prmodes = (mode ins prmodes2) union prmodes1,
   218       parse_ast_trtab =
   219         extend_trtab parse_ast_trtab parse_ast_translation "parse ast translation",
   220       parse_ruletab = extend_ruletab parse_ruletab parse_rules,
   221       parse_trtab = extend_trtab parse_trtab parse_translation "parse translation",
   222       print_trtab = extend_tr'tab print_trtab print_translation,
   223       print_ruletab = extend_ruletab print_ruletab print_rules,
   224       print_ast_trtab = extend_tr'tab print_ast_trtab print_ast_translation,
   225       tokentrtab = extend_tokentrtab tokentrtab token_translation,
   226       prtabs = Printer.extend_prtabs prtabs mode xprods}
   227   end;
   228 
   229 
   230 (* merge_syntaxes *)
   231 
   232 fun merge_syntaxes (Syntax tabs1) (Syntax tabs2) =
   233   let
   234     val {lexicon = lexicon1, logtypes = logtypes1, gram = gram1, consts = consts1,
   235       prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
   236       parse_trtab = parse_trtab1, print_trtab = print_trtab1,
   237       print_ruletab = print_ruletab1, print_ast_trtab = print_ast_trtab1,
   238       tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
   239 
   240     val {lexicon = lexicon2, logtypes = logtypes2, gram = gram2, consts = consts2,
   241       prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
   242       parse_trtab = parse_trtab2, print_trtab = print_trtab2,
   243       print_ruletab = print_ruletab2, print_ast_trtab = print_ast_trtab2,
   244       tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
   245   in
   246     Syntax {
   247       lexicon = Scan.merge_lexicons lexicon1 lexicon2,
   248       logtypes = merge_lists logtypes1 logtypes2,
   249       gram = Parser.merge_grams gram1 gram2,
   250       consts = merge_lists consts1 consts2,
   251       prmodes = merge_lists prmodes1 prmodes2,
   252       parse_ast_trtab =
   253         merge_trtabs parse_ast_trtab1 parse_ast_trtab2 "parse ast translation",
   254       parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
   255       parse_trtab = merge_trtabs parse_trtab1 parse_trtab2 "parse translation",
   256       print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
   257       print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
   258       print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
   259       tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2,
   260       prtabs = Printer.merge_prtabs prtabs1 prtabs2}
   261   end;
   262 
   263 
   264 (* type_syn *)
   265 
   266 val type_syn = extend_syntax ("", true) empty_syntax TypeExt.type_ext;
   267 val pure_syn = extend_syntax ("", true) type_syn SynExt.pure_ext;
   268 
   269 
   270 
   271 (** inspect syntax **)
   272 
   273 fun pretty_strs_qs name strs =
   274   Pretty.strs (name :: map quote (sort_strings strs));
   275 
   276 
   277 (* print_gram *)
   278 
   279 fun print_gram (Syntax tabs) =
   280   let
   281     val {lexicon, logtypes, prmodes, gram, prtabs, ...} = tabs;
   282     val prmodes' = sort_strings (filter_out (equal "") prmodes);
   283   in
   284     Pretty.writeln (pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon));
   285     Pretty.writeln (Pretty.strs ("logtypes:" :: logtypes));
   286     Pretty.writeln (Pretty.big_list "prods:" (Parser.pretty_gram gram));
   287     Pretty.writeln (pretty_strs_qs "print modes:" prmodes')
   288   end;
   289 
   290 
   291 (* print_trans *)
   292 
   293 fun print_trans (Syntax tabs) =
   294   let
   295     fun pretty_trtab name tab =
   296       pretty_strs_qs name (Symtab.keys tab);
   297 
   298     fun pretty_ruletab name tab =
   299       Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
   300 
   301     fun pretty_tokentr (mode, trs) = Pretty.strs (quote mode ^ ":" :: map fst trs);
   302 
   303     val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
   304       print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs;
   305   in
   306     Pretty.writeln (pretty_strs_qs "consts:" consts);
   307     Pretty.writeln (pretty_trtab "parse_ast_translation:" parse_ast_trtab);
   308     Pretty.writeln (pretty_ruletab "parse_rules:" parse_ruletab);
   309     Pretty.writeln (pretty_trtab "parse_translation:" parse_trtab);
   310     Pretty.writeln (pretty_trtab "print_translation:" print_trtab);
   311     Pretty.writeln (pretty_ruletab "print_rules:" print_ruletab);
   312     Pretty.writeln (pretty_trtab "print_ast_translation:" print_ast_trtab);
   313     Pretty.writeln (Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab))
   314   end;
   315 
   316 
   317 (* print_syntax *)
   318 
   319 fun print_syntax syn = (print_gram syn; print_trans syn);
   320 
   321 
   322 
   323 (** read **)
   324 
   325 (* test_read *)
   326 
   327 fun test_read (Syntax tabs) root str =
   328   let
   329     val {lexicon, gram, parse_ast_trtab, parse_ruletab, ...} = tabs;
   330 
   331     val chars = Symbol.explode str;
   332     val toks = Lexicon.tokenize lexicon false chars;
   333     val _ = writeln ("tokens: " ^ space_implode " " (map Lexicon.display_token toks));
   334 
   335     fun show_pt pt =
   336       let
   337         val raw_ast = SynTrans.pt_to_ast (K None) pt;
   338         val _ = writeln ("raw: " ^ Ast.str_of_ast raw_ast);
   339         val pre_ast = SynTrans.pt_to_ast (lookup_tr parse_ast_trtab) pt;
   340         val _ = Ast.normalize true true (lookup_ruletab parse_ruletab) pre_ast;
   341       in () end;
   342   in seq show_pt (Parser.parse gram root toks) end;
   343 
   344 
   345 (* read_ast *)
   346 
   347 val ambiguity_level = ref 1;
   348 
   349 fun read_asts (Syntax tabs) xids root str =
   350   let
   351     val {lexicon, gram, parse_ast_trtab, logtypes, ...} = tabs;
   352     val root' = if root mem logtypes then SynExt.logic else root;
   353     val chars = Symbol.explode str;
   354     val pts = Parser.parse gram root' (Lexicon.tokenize lexicon xids chars);
   355 
   356     fun show_pt pt = warning (Pretty.string_of 
   357 			(Ast.pretty_ast (SynTrans.pt_to_ast (K None) pt)));
   358   in
   359     if length pts > ! ambiguity_level then
   360       (warning ("Ambiguous input " ^ quote str);
   361        warning "produces the following parse trees:";
   362        seq show_pt pts)
   363     else ();
   364     map (SynTrans.pt_to_ast (lookup_tr parse_ast_trtab)) pts
   365   end;
   366 
   367 
   368 (* read *)
   369 
   370 fun read (syn as Syntax tabs) ty str =
   371   let
   372     val {parse_ruletab, parse_trtab, ...} = tabs;
   373     val asts = read_asts syn false (SynExt.typ_to_nonterm ty) str;
   374   in
   375     map (SynTrans.ast_to_term (lookup_tr parse_trtab))
   376       (map (Ast.normalize_ast (lookup_ruletab parse_ruletab)) asts)
   377   end;
   378 
   379 
   380 (* read types *)
   381 
   382 fun read_typ syn get_sort str =
   383   (case read syn SynExt.typeT str of
   384     [t] => TypeExt.typ_of_term (get_sort (TypeExt.raw_term_sorts t)) t
   385   | _ => error "read_typ: ambiguous type syntax");
   386 
   387 fun simple_read_typ str =
   388   let fun get_sort env xi = if_none (assoc (env, xi)) [] in
   389     read_typ type_syn get_sort str
   390   end;
   391 
   392 
   393 
   394 (** prepare translation rules **)
   395 
   396 datatype 'a trrule =
   397   ParseRule of 'a * 'a |
   398   PrintRule of 'a * 'a |
   399   ParsePrintRule of 'a * 'a;
   400 
   401 fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y)
   402   | map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y)
   403   | map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y);
   404 
   405 fun parse_rule (ParseRule pats) = Some pats
   406   | parse_rule (PrintRule _) = None
   407   | parse_rule (ParsePrintRule pats) = Some pats;
   408 
   409 fun print_rule (ParseRule _) = None
   410   | print_rule (PrintRule pats) = Some (swap pats)
   411   | print_rule (ParsePrintRule pats) = Some (swap pats);
   412 
   413 
   414 fun check_rule (rule as (lhs, rhs)) =
   415   (case Ast.rule_error rule of
   416     Some msg =>
   417       error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
   418         Ast.str_of_ast lhs ^ "  ->  " ^ Ast.str_of_ast rhs)
   419   | None => rule);
   420 
   421 
   422 fun read_pattern syn (root, str) =
   423   let
   424     val Syntax {consts, ...} = syn;
   425 
   426     fun constify (ast as Ast.Constant _) = ast
   427       | constify (ast as Ast.Variable x) =
   428           if x mem consts orelse NameSpace.qualified x then Ast.Constant x
   429           else ast
   430       | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
   431   in
   432     (case read_asts syn true root str of
   433       [ast] => constify ast
   434     | _ => error ("Syntactically ambiguous input: " ^ quote str))
   435   end handle ERROR =>
   436     error ("The error(s) above occurred in translation pattern " ^
   437       quote str);
   438 
   439 
   440 fun prep_rules rd_pat raw_rules =
   441   let val rules = map (map_trrule rd_pat) raw_rules in
   442     (map check_rule (mapfilter parse_rule rules),
   443       map check_rule (mapfilter print_rule rules))
   444   end
   445 
   446 
   447 
   448 (** pretty terms, typs, sorts **)
   449 
   450 val print_mode = print_mode;
   451 fun pretty_t t_to_ast prt_t (syn as Syntax tabs) curried t =
   452   let
   453     val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs;
   454     val ast = t_to_ast (lookup_tr' print_trtab) t;
   455   in
   456     prt_t curried prtabs (lookup_tr' print_ast_trtab)
   457       (lookup_tokentr tokentrtab (! print_mode))
   458       (Ast.normalize_ast (lookup_ruletab print_ruletab) ast)
   459   end;
   460 
   461 val pretty_term = pretty_t Printer.term_to_ast Printer.pretty_term_ast;
   462 fun pretty_typ syn = pretty_t Printer.typ_to_ast Printer.pretty_typ_ast syn false;
   463 fun pretty_sort syn = pretty_t Printer.sort_to_ast Printer.pretty_typ_ast syn false;
   464 
   465 val simple_str_of_sort = Pretty.str_of o pretty_sort type_syn;
   466 val simple_string_of_typ = Pretty.string_of o (pretty_typ type_syn);
   467 val simple_pprint_typ = Pretty.pprint o Pretty.quote o (pretty_typ type_syn);
   468 
   469 
   470 
   471 (** extend syntax (external interfaces) **)
   472 
   473 fun ext_syntax mk_syn_ext prmode (syn as Syntax {logtypes, ...}) decls =
   474   extend_syntax prmode syn (mk_syn_ext logtypes decls);
   475 
   476 
   477 fun extend_log_types syn logtypes =
   478   extend_syntax ("", true) syn (SynExt.syn_ext_logtypes logtypes);
   479 
   480 val extend_type_gram = ext_syntax Mixfix.syn_ext_types ("", true);
   481 
   482 fun extend_const_gram syn prmode = ext_syntax Mixfix.syn_ext_consts prmode syn;
   483 
   484 val extend_consts = ext_syntax SynExt.syn_ext_const_names ("", true);
   485 
   486 val extend_trfuns = ext_syntax SynExt.syn_ext_trfuns ("", true);
   487 
   488 val extend_trfunsT = ext_syntax SynExt.syn_ext_trfunsT ("", true);
   489 
   490 val extend_tokentrfuns = ext_syntax SynExt.syn_ext_tokentrfuns ("", true);
   491 
   492 fun extend_trrules syn rules =
   493   ext_syntax SynExt.syn_ext_rules ("", true) syn (prep_rules (read_pattern syn) rules);
   494 
   495 fun extend_trrules_i syn rules =
   496   ext_syntax SynExt.syn_ext_rules ("", true) syn (prep_rules I rules);
   497 
   498 
   499 
   500 (** export parts of internal Syntax structures **)
   501 
   502 open TokenTrans Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
   503 
   504 
   505 end;
   506 
   507 
   508 structure BasicSyntax: BASIC_SYNTAX = Syntax;
   509 open BasicSyntax;