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