src/Pure/Syntax/syntax.ML
changeset 15755 50ac97ebe7d8
parent 15574 b1d1b5bfc464
child 15759 144c9f9a8ade
     1.1 --- a/src/Pure/Syntax/syntax.ML	Sat Apr 16 18:58:30 2005 +0200
     1.2 +++ b/src/Pure/Syntax/syntax.ML	Sat Apr 16 18:58:55 2005 +0200
     1.3 @@ -23,36 +23,37 @@
     1.4    include SYN_TRANS1
     1.5    include MIXFIX1
     1.6    include PRINTER0
     1.7 -  val extend_trtab: ('a * stamp) Symtab.table -> (string * 'a) list -> string
     1.8 -    -> ('a * stamp) Symtab.table
     1.9 -  val merge_trtabs: ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table -> string
    1.10 -    -> ('a * stamp) Symtab.table
    1.11 +  val extend_trtab: string -> (string * ('a * stamp)) list ->
    1.12 +    ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table
    1.13 +  val merge_trtabs: string -> ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table ->
    1.14 +    ('a * stamp) Symtab.table
    1.15    val merge_tr'tabs: ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
    1.16      -> ('a * stamp) list Symtab.table
    1.17 -  val extend_tr'tab: ('a * stamp) list Symtab.table -> (string * 'a) list
    1.18 -    -> ('a * stamp) list Symtab.table
    1.19 +  val extend_tr'tab: (string * ('a * stamp)) list ->
    1.20 +    ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
    1.21    datatype 'a trrule =
    1.22      ParseRule of 'a * 'a |
    1.23      PrintRule of 'a * 'a |
    1.24      ParsePrintRule of 'a * 'a
    1.25    type syntax
    1.26    val is_keyword: syntax -> string -> bool
    1.27 +  val default_mode: string * bool
    1.28    val extend_type_gram: (string * int * mixfix) list -> syntax -> syntax
    1.29    val extend_const_gram: (string -> bool) ->
    1.30      string * bool -> (string * typ * mixfix) list -> syntax -> syntax
    1.31    val extend_consts: string list -> syntax -> syntax
    1.32    val extend_trfuns:
    1.33 -    (string * (ast list -> ast)) list *
    1.34 -    (string * (term list -> term)) list *
    1.35 -    (string * (bool -> typ -> term list -> term)) list *
    1.36 -    (string * (ast list -> ast)) list -> syntax -> syntax
    1.37 -  val extend_tokentrfuns:
    1.38 -    (string * string * (string -> string * real)) list -> syntax -> syntax
    1.39 +    (string * ((ast list -> ast) * stamp)) list *
    1.40 +    (string * ((term list -> term) * stamp)) list *
    1.41 +    (string * ((bool -> typ -> term list -> term) * stamp)) list *
    1.42 +    (string * ((ast list -> ast) * stamp)) list -> syntax -> syntax
    1.43 +  val extend_tokentrfuns: (string * string * (string -> string * real)) list -> syntax -> syntax
    1.44    val extend_trrules_i: ast trrule list -> syntax -> syntax
    1.45    val extend_trrules: (string -> bool) -> syntax ->
    1.46      (string * string) trrule list -> syntax -> syntax
    1.47 +  val remove_const_gram: (string -> bool) ->
    1.48 +    string * bool -> (string * typ * mixfix) list -> syntax -> syntax
    1.49    val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
    1.50 -  val default_mode: string * bool
    1.51    val merge_syntaxes: syntax -> syntax -> syntax
    1.52    val type_syn: syntax
    1.53    val pure_syn: syntax
    1.54 @@ -74,14 +75,8 @@
    1.55  structure Syntax: SYNTAX =
    1.56  struct
    1.57  
    1.58 -val ambiguity_is_error = ref false
    1.59 -
    1.60  (** tables of translation functions **)
    1.61  
    1.62 -fun mk_trfun (c, f) = (c, (f, stamp ()));
    1.63 -fun eq_trfuns ((_, s1:stamp), (_, s2)) = s1 = s2;
    1.64 -
    1.65 -
    1.66  (* parse (ast) translations *)
    1.67  
    1.68  fun lookup_tr tab c = Option.map fst (Symtab.lookup (tab, c));
    1.69 @@ -89,20 +84,28 @@
    1.70  fun err_dup_trfuns name cs =
    1.71    error ("More than one " ^ name ^ " for " ^ commas_quote cs);
    1.72  
    1.73 -fun extend_trtab tab trfuns name =
    1.74 -  Symtab.extend (tab, map mk_trfun trfuns)
    1.75 -    handle Symtab.DUPS cs => err_dup_trfuns name cs;
    1.76 +fun extend_trtab name trfuns tab = Symtab.extend (tab, trfuns)
    1.77 +  handle Symtab.DUPS cs => err_dup_trfuns name cs;
    1.78  
    1.79 -fun merge_trtabs tab1 tab2 name =
    1.80 -  Symtab.merge eq_snd (tab1, tab2)
    1.81 -    handle Symtab.DUPS cs => err_dup_trfuns name cs;
    1.82 +fun remove_trtab trfuns = trfuns |> fold (fn (c, (_, s: stamp)) => fn tab =>
    1.83 +  if Option.map snd (Symtab.lookup (tab, c)) = SOME s
    1.84 +  then Symtab.delete c tab
    1.85 +  else tab);
    1.86 +
    1.87 +fun merge_trtabs name tab1 tab2 = Symtab.merge SynExt.eq_trfun (tab1, tab2)
    1.88 +  handle Symtab.DUPS cs => err_dup_trfuns name cs;
    1.89  
    1.90  
    1.91  (* print (ast) translations *)
    1.92  
    1.93  fun lookup_tr' tab c = map fst (Symtab.lookup_multi (tab, c));
    1.94 -fun extend_tr'tab tab trfuns = foldr Symtab.update_multi tab (map mk_trfun trfuns);
    1.95 -fun merge_tr'tabs tab1 tab2 = Symtab.merge_multi' eq_trfuns (tab1, tab2);
    1.96 +
    1.97 +fun extend_tr'tab trfuns tab = foldr Symtab.update_multi tab trfuns;
    1.98 +
    1.99 +fun remove_tr'tab trfuns = trfuns |> fold (fn (c, tr) =>
   1.100 +  Symtab.map_entry c (gen_remove SynExt.eq_trfun tr));
   1.101 +
   1.102 +fun merge_tr'tabs tab1 tab2 = Symtab.merge_multi' SynExt.eq_trfun (tab1, tab2);
   1.103  
   1.104  
   1.105  
   1.106 @@ -133,7 +136,7 @@
   1.107      map merge (distinct (map fst (tabs1 @ tabs2)))
   1.108    end;
   1.109  
   1.110 -fun extend_tokentrtab tabs tokentrs =
   1.111 +fun extend_tokentrtab tokentrs tabs =
   1.112    let
   1.113      fun ins_tokentr (ts, (m, c, f)) =
   1.114        overwrite (ts, (m, ("_" ^ c, (f, stamp ())) :: assocs ts m));
   1.115 @@ -153,9 +156,11 @@
   1.116  
   1.117  (* empty, extend, merge ruletabs *)
   1.118  
   1.119 -fun extend_ruletab tab rules =
   1.120 +fun extend_ruletab rules tab =
   1.121    foldr Symtab.update_multi tab (map (fn r => (Ast.head_of_rule r, r)) rules);
   1.122  
   1.123 +val remove_ruletab = fold (fn r => Symtab.map_entry (Ast.head_of_rule r) (remove r));
   1.124 +
   1.125  fun merge_ruletabs tab1 tab2 = Symtab.merge_multi' (op =) (tab1, tab2);
   1.126  
   1.127  
   1.128 @@ -164,6 +169,7 @@
   1.129  
   1.130  datatype syntax =
   1.131    Syntax of {
   1.132 +    input: SynExt.xprod list,
   1.133      lexicon: Scan.lexicon,
   1.134      gram: Parser.gram,
   1.135      consts: string list,
   1.136 @@ -179,11 +185,14 @@
   1.137  
   1.138  fun is_keyword (Syntax {lexicon, ...}) = Scan.is_literal lexicon o Symbol.explode;
   1.139  
   1.140 +val default_mode = ("", true);
   1.141 +
   1.142  
   1.143  (* empty_syntax *)
   1.144  
   1.145  val empty_syntax =
   1.146    Syntax {
   1.147 +    input = [],
   1.148      lexicon = Scan.empty_lexicon,
   1.149      gram = Parser.empty_gram,
   1.150      consts = [],
   1.151 @@ -195,14 +204,14 @@
   1.152      print_ruletab = Symtab.empty,
   1.153      print_ast_trtab = Symtab.empty,
   1.154      tokentrtab = [],
   1.155 -    prtabs = Printer.empty_prtabs}
   1.156 +    prtabs = Printer.empty_prtabs};
   1.157  
   1.158  
   1.159  (* extend_syntax *)
   1.160  
   1.161  fun extend_syntax (mode, inout) syn_ext (Syntax tabs) =
   1.162    let
   1.163 -    val {lexicon, gram, consts = consts1, prmodes = prmodes1,
   1.164 +    val {input, lexicon, gram, consts = consts1, prmodes = prmodes1,
   1.165        parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
   1.166        print_ast_trtab, tokentrtab, prtabs} = tabs;
   1.167      val SynExt.SynExt {xprods, consts = consts2, prmodes = prmodes2,
   1.168 @@ -210,49 +219,78 @@
   1.169        print_ast_translation, token_translation} = syn_ext;
   1.170    in
   1.171      Syntax {
   1.172 +      input = if inout then xprods @ input else input,
   1.173        lexicon = if inout then Scan.extend_lexicon lexicon (SynExt.delims_of xprods) else lexicon,
   1.174        gram = if inout then Parser.extend_gram gram xprods else gram,
   1.175 -      consts = consts2 @ consts1,
   1.176 -      prmodes = (mode ins_string prmodes2) union_string prmodes1,
   1.177 +      consts = merge_lists' consts1 consts2,
   1.178 +      prmodes = mode ins_string (merge_lists' prmodes1 prmodes2),
   1.179        parse_ast_trtab =
   1.180 -        extend_trtab parse_ast_trtab parse_ast_translation "parse ast translation",
   1.181 -      parse_ruletab = extend_ruletab parse_ruletab parse_rules,
   1.182 -      parse_trtab = extend_trtab parse_trtab parse_translation "parse translation",
   1.183 -      print_trtab = extend_tr'tab print_trtab print_translation,
   1.184 -      print_ruletab = extend_ruletab print_ruletab print_rules,
   1.185 -      print_ast_trtab = extend_tr'tab print_ast_trtab print_ast_translation,
   1.186 -      tokentrtab = extend_tokentrtab tokentrtab token_translation,
   1.187 -      prtabs = Printer.extend_prtabs prtabs mode xprods}
   1.188 +        extend_trtab "parse ast translation" parse_ast_translation parse_ast_trtab,
   1.189 +      parse_ruletab = extend_ruletab parse_rules parse_ruletab,
   1.190 +      parse_trtab = extend_trtab "parse translation" parse_translation parse_trtab,
   1.191 +      print_trtab = extend_tr'tab print_translation print_trtab,
   1.192 +      print_ruletab = extend_ruletab print_rules print_ruletab,
   1.193 +      print_ast_trtab = extend_tr'tab print_ast_translation print_ast_trtab,
   1.194 +      tokentrtab = extend_tokentrtab token_translation tokentrtab,
   1.195 +      prtabs = Printer.extend_prtabs mode xprods prtabs}
   1.196    end;
   1.197  
   1.198 -val default_mode = ("", true);
   1.199 +
   1.200 +(* remove_syntax *)
   1.201 +
   1.202 +fun remove_syntax (mode, inout) syn_ext (Syntax tabs) =
   1.203 +  let
   1.204 +    val SynExt.SynExt {xprods, consts = _, prmodes = _,
   1.205 +      parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
   1.206 +      print_ast_translation, token_translation = _} = syn_ext;
   1.207 +    val {input, lexicon, gram, consts, prmodes,
   1.208 +      parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
   1.209 +      print_ast_trtab, tokentrtab, prtabs} = tabs;
   1.210 +    val input' = if inout then fold remove xprods input else input;
   1.211 +  in
   1.212 +    Syntax {
   1.213 +      input = input',
   1.214 +      lexicon = if inout then Scan.make_lexicon (SynExt.delims_of input') else lexicon,
   1.215 +      gram = if inout then Parser.make_gram input' else gram,
   1.216 +      consts = consts,
   1.217 +      prmodes = prmodes,
   1.218 +      parse_ast_trtab = remove_trtab parse_ast_translation parse_ast_trtab,
   1.219 +      parse_ruletab = remove_ruletab parse_rules parse_ruletab,
   1.220 +      parse_trtab = remove_trtab parse_translation parse_trtab,
   1.221 +      print_trtab = remove_tr'tab print_translation print_trtab,
   1.222 +      print_ruletab = remove_ruletab print_rules print_ruletab,
   1.223 +      print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
   1.224 +      tokentrtab = tokentrtab,
   1.225 +      prtabs = Printer.remove_prtabs mode xprods prtabs}
   1.226 +  end;
   1.227  
   1.228  
   1.229  (* merge_syntaxes *)
   1.230  
   1.231  fun merge_syntaxes (Syntax tabs1) (Syntax tabs2) =
   1.232    let
   1.233 -    val {lexicon = lexicon1, gram = gram1, consts = consts1, prmodes = prmodes1,
   1.234 -      parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
   1.235 -      parse_trtab = parse_trtab1, print_trtab = print_trtab1,
   1.236 -      print_ruletab = print_ruletab1, print_ast_trtab = print_ast_trtab1,
   1.237 -      tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
   1.238 +    val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
   1.239 +      prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1,
   1.240 +      parse_ruletab = parse_ruletab1, parse_trtab = parse_trtab1,
   1.241 +      print_trtab = print_trtab1, print_ruletab = print_ruletab1,
   1.242 +      print_ast_trtab = print_ast_trtab1, tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
   1.243  
   1.244 -    val {lexicon = lexicon2, gram = gram2, consts = consts2, prmodes = prmodes2,
   1.245 -      parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
   1.246 -      parse_trtab = parse_trtab2, print_trtab = print_trtab2,
   1.247 -      print_ruletab = print_ruletab2, print_ast_trtab = print_ast_trtab2,
   1.248 -      tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
   1.249 +    val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
   1.250 +      prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2,
   1.251 +      parse_ruletab = parse_ruletab2, parse_trtab = parse_trtab2,
   1.252 +      print_trtab = print_trtab2, print_ruletab = print_ruletab2,
   1.253 +      print_ast_trtab = print_ast_trtab2, tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
   1.254    in
   1.255      Syntax {
   1.256 +      input = merge_lists' input1 input2,
   1.257        lexicon = Scan.merge_lexicons lexicon1 lexicon2,
   1.258        gram = Parser.merge_grams gram1 gram2,
   1.259        consts = unique_strings (sort_strings (consts1 @ consts2)),
   1.260        prmodes = merge_lists prmodes1 prmodes2,
   1.261        parse_ast_trtab =
   1.262 -        merge_trtabs parse_ast_trtab1 parse_ast_trtab2 "parse ast translation",
   1.263 +        merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
   1.264        parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
   1.265 -      parse_trtab = merge_trtabs parse_trtab1 parse_trtab2 "parse translation",
   1.266 +      parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
   1.267        print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
   1.268        print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
   1.269        print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
   1.270 @@ -326,6 +364,7 @@
   1.271  (* read_ast *)
   1.272  
   1.273  val ambiguity_level = ref 1;
   1.274 +val ambiguity_is_error = ref false
   1.275  
   1.276  fun read_asts is_logtype (Syntax tabs) xids root str =
   1.277    let
   1.278 @@ -468,15 +507,16 @@
   1.279    ext_syntax' (K SynExt.syn_ext_rules) (K false) default_mode
   1.280      (prep_rules (read_pattern is_logtype syn) rules);
   1.281  
   1.282 +fun remove_const_gram is_logtype prmode decls =
   1.283 +  remove_syntax prmode (Mixfix.syn_ext_consts is_logtype decls);
   1.284 +
   1.285  
   1.286  
   1.287  (** export parts of internal Syntax structures **)
   1.288  
   1.289  open TokenTrans Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
   1.290  
   1.291 -
   1.292  end;
   1.293  
   1.294 -
   1.295  structure BasicSyntax: BASIC_SYNTAX = Syntax;
   1.296  open BasicSyntax;