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