src/Pure/Syntax/syntax.ML
author wenzelm
Wed Dec 12 23:36:07 2012 +0100 (2012-12-12 ago)
changeset 50499 f496b2b7bafb
parent 50201 c26369c9eda6
child 51580 64ef8260dc60
permissions -rw-r--r--
rendering of selected dialog_result as active_result_color, depending on dynamic command status in output panel, but not static popups etc.;
wenzelm@18
     1
(*  Title:      Pure/Syntax/syntax.ML
clasohm@0
     2
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@18
     3
wenzelm@24263
     4
Standard Isabelle syntax, based on arbitrary context-free grammars
wenzelm@24263
     5
(specified by mixfix declarations).
clasohm@0
     6
*)
clasohm@0
     7
clasohm@0
     8
signature SYNTAX =
wenzelm@2383
     9
sig
wenzelm@42382
    10
  type operations
wenzelm@42382
    11
  val install_operations: operations -> unit
wenzelm@42288
    12
  val root: string Config.T
wenzelm@46512
    13
  val ambiguity_warning_raw: Config.raw
wenzelm@46512
    14
  val ambiguity_warning: bool Config.T
wenzelm@46506
    15
  val ambiguity_limit_raw: Config.raw
wenzelm@42268
    16
  val ambiguity_limit: int Config.T
wenzelm@30573
    17
  val read_token: string -> Symbol_Pos.T list * Position.T
wenzelm@46849
    18
  val read_token_pos: string -> Position.T
wenzelm@43731
    19
  val parse_token: Proof.context -> (XML.tree list -> 'a) ->
wenzelm@43731
    20
    Markup.T -> (Symbol_Pos.T list * Position.T -> 'a) -> string -> 'a
wenzelm@24263
    21
  val parse_sort: Proof.context -> string -> sort
wenzelm@24263
    22
  val parse_typ: Proof.context -> string -> typ
wenzelm@24263
    23
  val parse_term: Proof.context -> string -> term
wenzelm@24263
    24
  val parse_prop: Proof.context -> string -> term
wenzelm@24768
    25
  val unparse_sort: Proof.context -> sort -> Pretty.T
wenzelm@24923
    26
  val unparse_classrel: Proof.context -> class list -> Pretty.T
wenzelm@24923
    27
  val unparse_arity: Proof.context -> arity -> Pretty.T
wenzelm@24768
    28
  val unparse_typ: Proof.context -> typ -> Pretty.T
wenzelm@24768
    29
  val unparse_term: Proof.context -> term -> Pretty.T
wenzelm@24488
    30
  val check_sort: Proof.context -> sort -> sort
wenzelm@24512
    31
  val check_typ: Proof.context -> typ -> typ
wenzelm@24512
    32
  val check_term: Proof.context -> term -> term
wenzelm@24512
    33
  val check_prop: Proof.context -> term -> term
wenzelm@24488
    34
  val check_typs: Proof.context -> typ list -> typ list
wenzelm@24263
    35
  val check_terms: Proof.context -> term list -> term list
wenzelm@24263
    36
  val check_props: Proof.context -> term list -> term list
wenzelm@24923
    37
  val uncheck_sort: Proof.context -> sort -> sort
wenzelm@24923
    38
  val uncheck_arity: Proof.context -> arity -> arity
wenzelm@24923
    39
  val uncheck_classrel: Proof.context -> class list -> class list
wenzelm@24768
    40
  val uncheck_typs: Proof.context -> typ list -> typ list
wenzelm@24768
    41
  val uncheck_terms: Proof.context -> term list -> term list
wenzelm@24263
    42
  val read_sort: Proof.context -> string -> sort
wenzelm@24263
    43
  val read_typ: Proof.context -> string -> typ
wenzelm@24488
    44
  val read_term: Proof.context -> string -> term
wenzelm@24488
    45
  val read_prop: Proof.context -> string -> term
wenzelm@46989
    46
  val read_typs: Proof.context -> string list -> typ list
wenzelm@24263
    47
  val read_terms: Proof.context -> string list -> term list
wenzelm@24263
    48
  val read_props: Proof.context -> string list -> term list
wenzelm@24709
    49
  val read_sort_global: theory -> string -> sort
wenzelm@24709
    50
  val read_typ_global: theory -> string -> typ
wenzelm@24709
    51
  val read_term_global: theory -> string -> term
wenzelm@24709
    52
  val read_prop_global: theory -> string -> term
wenzelm@24923
    53
  val pretty_term: Proof.context -> term -> Pretty.T
wenzelm@24923
    54
  val pretty_typ: Proof.context -> typ -> Pretty.T
wenzelm@24923
    55
  val pretty_sort: Proof.context -> sort -> Pretty.T
wenzelm@24923
    56
  val pretty_classrel: Proof.context -> class list -> Pretty.T
wenzelm@24923
    57
  val pretty_arity: Proof.context -> arity -> Pretty.T
wenzelm@24923
    58
  val string_of_term: Proof.context -> term -> string
wenzelm@24923
    59
  val string_of_typ: Proof.context -> typ -> string
wenzelm@24923
    60
  val string_of_sort: Proof.context -> sort -> string
wenzelm@24923
    61
  val string_of_classrel: Proof.context -> class list -> string
wenzelm@24923
    62
  val string_of_arity: Proof.context -> arity -> string
wenzelm@26951
    63
  val is_pretty_global: Proof.context -> bool
wenzelm@26951
    64
  val set_pretty_global: bool -> Proof.context -> Proof.context
wenzelm@26951
    65
  val init_pretty_global: theory -> Proof.context
wenzelm@42383
    66
  val init_pretty: Context.pretty -> Proof.context
wenzelm@26951
    67
  val pretty_term_global: theory -> term -> Pretty.T
wenzelm@26951
    68
  val pretty_typ_global: theory -> typ -> Pretty.T
wenzelm@26951
    69
  val pretty_sort_global: theory -> sort -> Pretty.T
wenzelm@26951
    70
  val string_of_term_global: theory -> term -> string
wenzelm@26951
    71
  val string_of_typ_global: theory -> typ -> string
wenzelm@26951
    72
  val string_of_sort_global: theory -> sort -> string
wenzelm@39135
    73
  type syntax
wenzelm@39135
    74
  val eq_syntax: syntax * syntax -> bool
wenzelm@45632
    75
  val force_syntax: syntax -> unit
wenzelm@42298
    76
  val lookup_const: syntax -> string -> string option
wenzelm@39135
    77
  val is_keyword: syntax -> string -> bool
wenzelm@42251
    78
  val tokenize: syntax -> bool -> Symbol_Pos.T list -> Lexicon.token list
wenzelm@45641
    79
  val parse: syntax -> string -> Lexicon.token list -> Parser.parsetree list
wenzelm@42253
    80
  val parse_ast_translation: syntax -> string -> (Proof.context -> Ast.ast list -> Ast.ast) option
wenzelm@42255
    81
  val parse_rules: syntax -> string -> (Ast.ast * Ast.ast) list
wenzelm@42253
    82
  val parse_translation: syntax -> string -> (Proof.context -> term list -> term) option
wenzelm@42254
    83
  val print_translation: syntax -> string ->
wenzelm@42254
    84
    Proof.context -> typ -> term list -> term  (*exception Match*)
wenzelm@42255
    85
  val print_rules: syntax -> string -> (Ast.ast * Ast.ast) list
wenzelm@42254
    86
  val print_ast_translation: syntax -> string ->
wenzelm@42254
    87
    Proof.context -> Ast.ast list -> Ast.ast  (*exception Match*)
wenzelm@42255
    88
  val prtabs: syntax -> Printer.prtabs
wenzelm@39135
    89
  type mode
wenzelm@39135
    90
  val mode_default: mode
wenzelm@39135
    91
  val mode_input: mode
wenzelm@42294
    92
  val empty_syntax: syntax
wenzelm@45632
    93
  val merge_syntax: syntax * syntax -> syntax
wenzelm@42294
    94
  val token_markers: string list
wenzelm@39135
    95
  val basic_nonterms: string list
wenzelm@39135
    96
  val print_gram: syntax -> unit
wenzelm@39135
    97
  val print_trans: syntax -> unit
wenzelm@39135
    98
  val print_syntax: syntax -> unit
wenzelm@39135
    99
  val guess_infix: syntax -> string -> mixfix option
wenzelm@39135
   100
  datatype 'a trrule =
wenzelm@42204
   101
    Parse_Rule of 'a * 'a |
wenzelm@42204
   102
    Print_Rule of 'a * 'a |
wenzelm@42204
   103
    Parse_Print_Rule of 'a * 'a
wenzelm@39135
   104
  val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
wenzelm@39135
   105
  val update_trfuns:
wenzelm@42224
   106
    (string * ((Ast.ast list -> Ast.ast) * stamp)) list *
wenzelm@39135
   107
    (string * ((term list -> term) * stamp)) list *
wenzelm@42247
   108
    (string * ((typ -> term list -> term) * stamp)) list *
wenzelm@42224
   109
    (string * ((Ast.ast list -> Ast.ast) * stamp)) list -> syntax -> syntax
wenzelm@39135
   110
  val update_advanced_trfuns:
wenzelm@42224
   111
    (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list *
wenzelm@39135
   112
    (string * ((Proof.context -> term list -> term) * stamp)) list *
wenzelm@42247
   113
    (string * ((Proof.context -> typ -> term list -> term) * stamp)) list *
wenzelm@42224
   114
    (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list -> syntax -> syntax
wenzelm@39135
   115
  val update_type_gram: bool -> mode -> (string * typ * mixfix) list -> syntax -> syntax
wenzelm@39135
   116
  val update_const_gram: bool -> (string -> bool) ->
wenzelm@39135
   117
    mode -> (string * typ * mixfix) list -> syntax -> syntax
wenzelm@42224
   118
  val update_trrules: Ast.ast trrule list -> syntax -> syntax
wenzelm@42224
   119
  val remove_trrules: Ast.ast trrule list -> syntax -> syntax
wenzelm@42476
   120
  val const: string -> term
wenzelm@42476
   121
  val free: string -> term
wenzelm@42476
   122
  val var: indexname -> term
wenzelm@2383
   123
end;
clasohm@0
   124
wenzelm@12094
   125
structure Syntax: SYNTAX =
clasohm@0
   126
struct
clasohm@0
   127
wenzelm@42288
   128
wenzelm@39135
   129
(** inner syntax operations **)
wenzelm@39135
   130
wenzelm@42382
   131
(* back-patched operations *)
wenzelm@42382
   132
wenzelm@42382
   133
type operations =
wenzelm@42382
   134
 {parse_sort: Proof.context -> string -> sort,
wenzelm@42382
   135
  parse_typ: Proof.context -> string -> typ,
wenzelm@42382
   136
  parse_term: Proof.context -> string -> term,
wenzelm@42382
   137
  parse_prop: Proof.context -> string -> term,
wenzelm@42382
   138
  unparse_sort: Proof.context -> sort -> Pretty.T,
wenzelm@42382
   139
  unparse_typ: Proof.context -> typ -> Pretty.T,
wenzelm@42382
   140
  unparse_term: Proof.context -> term -> Pretty.T,
wenzelm@42382
   141
  check_typs: Proof.context -> typ list -> typ list,
wenzelm@42382
   142
  check_terms: Proof.context -> term list -> term list,
wenzelm@42382
   143
  check_props: Proof.context -> term list -> term list,
wenzelm@42382
   144
  uncheck_typs: Proof.context -> typ list -> typ list,
wenzelm@42382
   145
  uncheck_terms: Proof.context -> term list -> term list};
wenzelm@42382
   146
wenzelm@42382
   147
val operations: operations Single_Assignment.var = Single_Assignment.var "Syntax.operations";
wenzelm@42382
   148
fun install_operations ops = Single_Assignment.assign operations ops;
wenzelm@42382
   149
wenzelm@42382
   150
fun operation which ctxt x =
wenzelm@42382
   151
  (case Single_Assignment.peek operations of
wenzelm@42382
   152
    NONE => raise Fail "Inner syntax operations not installed"
wenzelm@42382
   153
  | SOME ops => which ops ctxt x);
wenzelm@42382
   154
wenzelm@42382
   155
wenzelm@42268
   156
(* configuration options *)
wenzelm@42268
   157
wenzelm@42293
   158
val root = Config.string (Config.declare "syntax_root" (K (Config.String "any")));
wenzelm@42288
   159
wenzelm@46512
   160
val ambiguity_warning_raw = Config.declare "syntax_ambiguity_warning" (fn _ => Config.Bool true);
wenzelm@46512
   161
val ambiguity_warning = Config.bool ambiguity_warning_raw;
wenzelm@42268
   162
wenzelm@46506
   163
val ambiguity_limit_raw = Config.declare "syntax_ambiguity_limit" (fn _ => Config.Int 10);
wenzelm@46506
   164
val ambiguity_limit = Config.int ambiguity_limit_raw;
kleing@44069
   165
wenzelm@42268
   166
wenzelm@43731
   167
(* outer syntax token -- with optional YXML content *)
wenzelm@39135
   168
wenzelm@46849
   169
fun token_position (XML.Elem ((name, props), _)) =
wenzelm@50201
   170
      if name = Markup.tokenN then Position.of_properties props
wenzelm@46849
   171
      else Position.none
wenzelm@46849
   172
  | token_position (XML.Text _) = Position.none;
wenzelm@46849
   173
wenzelm@43731
   174
fun explode_token tree =
wenzelm@39135
   175
  let
wenzelm@39555
   176
    val text = XML.content_of [tree];
wenzelm@46849
   177
    val pos = token_position tree;
wenzelm@39135
   178
  in (Symbol_Pos.explode (text, pos), pos) end;
wenzelm@39135
   179
wenzelm@43731
   180
fun read_token str = explode_token (YXML.parse str handle Fail msg => error msg);
wenzelm@46849
   181
fun read_token_pos str = token_position (YXML.parse str handle Fail msg => error msg);
wenzelm@43731
   182
wenzelm@43731
   183
fun parse_token ctxt decode markup parse str =
wenzelm@43731
   184
  let
wenzelm@43731
   185
    fun parse_tree tree =
wenzelm@43731
   186
      let
wenzelm@43731
   187
        val (syms, pos) = explode_token tree;
wenzelm@43731
   188
        val _ = Context_Position.report ctxt pos markup;
wenzelm@43731
   189
      in parse (syms, pos) end;
wenzelm@43731
   190
  in
wenzelm@43731
   191
    (case YXML.parse_body str handle Fail msg => error msg of
wenzelm@43731
   192
      body as [tree as XML.Elem ((name, _), _)] =>
wenzelm@50201
   193
        if name = Markup.tokenN then parse_tree tree else decode body
wenzelm@43731
   194
    | [tree as XML.Text _] => parse_tree tree
wenzelm@43731
   195
    | body => decode body)
wenzelm@43731
   196
  end;
wenzelm@43731
   197
wenzelm@39135
   198
wenzelm@39135
   199
(* (un)parsing *)
wenzelm@39135
   200
wenzelm@39135
   201
val parse_sort = operation #parse_sort;
wenzelm@39135
   202
val parse_typ = operation #parse_typ;
wenzelm@39135
   203
val parse_term = operation #parse_term;
wenzelm@39135
   204
val parse_prop = operation #parse_prop;
wenzelm@39135
   205
val unparse_sort = operation #unparse_sort;
wenzelm@39135
   206
val unparse_typ = operation #unparse_typ;
wenzelm@39135
   207
val unparse_term = operation #unparse_term;
wenzelm@39135
   208
wenzelm@39135
   209
wenzelm@45429
   210
(* (un)checking *)
wenzelm@42382
   211
wenzelm@42382
   212
val check_typs = operation #check_typs;
wenzelm@42382
   213
val check_terms = operation #check_terms;
wenzelm@42382
   214
val check_props = operation #check_props;
wenzelm@39135
   215
wenzelm@39135
   216
val check_typ = singleton o check_typs;
wenzelm@39135
   217
val check_term = singleton o check_terms;
wenzelm@39135
   218
val check_prop = singleton o check_props;
wenzelm@39135
   219
wenzelm@42382
   220
val uncheck_typs = operation #uncheck_typs;
wenzelm@42382
   221
val uncheck_terms = operation #uncheck_terms;
wenzelm@42402
   222
wenzelm@42402
   223
wenzelm@42402
   224
(* derived operations for algebra of sorts *)
wenzelm@42402
   225
wenzelm@42402
   226
local
wenzelm@42402
   227
wenzelm@42402
   228
fun map_sort f S =
wenzelm@42402
   229
  (case f (TFree ("", S)) of
wenzelm@42402
   230
    TFree ("", S') => S'
wenzelm@42402
   231
  | _ => raise TYPE ("map_sort", [TFree ("", S)], []));
wenzelm@42402
   232
wenzelm@42402
   233
in
wenzelm@42402
   234
wenzelm@42402
   235
val check_sort = map_sort o check_typ;
wenzelm@39135
   236
val uncheck_sort = map_sort o singleton o uncheck_typs;
wenzelm@39135
   237
wenzelm@39135
   238
end;
wenzelm@39135
   239
wenzelm@39135
   240
wenzelm@39135
   241
val uncheck_classrel = map o singleton o uncheck_sort;
wenzelm@39135
   242
wenzelm@39135
   243
fun unparse_classrel ctxt cs = Pretty.block (flat
wenzelm@39135
   244
  (separate [Pretty.str " <", Pretty.brk 1] (map (single o unparse_sort ctxt o single) cs)));
wenzelm@39135
   245
wenzelm@39135
   246
fun uncheck_arity ctxt (a, Ss, S) =
wenzelm@39135
   247
  let
wenzelm@39135
   248
    val T = Type (a, replicate (length Ss) dummyT);
wenzelm@39135
   249
    val a' =
wenzelm@39135
   250
      (case singleton (uncheck_typs ctxt) T of
wenzelm@39135
   251
        Type (a', _) => a'
wenzelm@39135
   252
      | T => raise TYPE ("uncheck_arity", [T], []));
wenzelm@39135
   253
    val Ss' = map (uncheck_sort ctxt) Ss;
wenzelm@39135
   254
    val S' = uncheck_sort ctxt S;
wenzelm@39135
   255
  in (a', Ss', S') end;
wenzelm@39135
   256
wenzelm@39135
   257
fun unparse_arity ctxt (a, Ss, S) =
wenzelm@39135
   258
  let
wenzelm@39135
   259
    val prtT = unparse_typ ctxt (Type (a, []));
wenzelm@39135
   260
    val dom =
wenzelm@39135
   261
      if null Ss then []
wenzelm@39135
   262
      else [Pretty.list "(" ")" (map (unparse_sort ctxt) Ss), Pretty.brk 1];
wenzelm@39135
   263
  in Pretty.block ([prtT, Pretty.str " ::", Pretty.brk 1] @ dom @ [unparse_sort ctxt S]) end;
wenzelm@39135
   264
wenzelm@39135
   265
wenzelm@39135
   266
(* read = parse + check *)
wenzelm@39135
   267
wenzelm@39135
   268
fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt;
wenzelm@46989
   269
wenzelm@46989
   270
fun read_typs ctxt =
wenzelm@46989
   271
  grouped 10 (Par_List.map_name "Syntax.read_typs") (parse_typ ctxt) #> check_typs ctxt;
wenzelm@39135
   272
wenzelm@46989
   273
fun read_terms ctxt =
wenzelm@46989
   274
  grouped 10 (Par_List.map_name "Syntax.read_terms") (parse_term ctxt) #> check_terms ctxt;
wenzelm@39135
   275
wenzelm@46989
   276
fun read_props ctxt =
wenzelm@46989
   277
  grouped 10 (Par_List.map_name "Syntax.read_props") (parse_prop ctxt) #> check_props ctxt;
wenzelm@46989
   278
wenzelm@46989
   279
val read_typ = singleton o read_typs;
wenzelm@39135
   280
val read_term = singleton o read_terms;
wenzelm@39135
   281
val read_prop = singleton o read_props;
wenzelm@39135
   282
wenzelm@42360
   283
val read_sort_global = read_sort o Proof_Context.init_global;
wenzelm@42360
   284
val read_typ_global = read_typ o Proof_Context.init_global;
wenzelm@42360
   285
val read_term_global = read_term o Proof_Context.init_global;
wenzelm@42360
   286
val read_prop_global = read_prop o Proof_Context.init_global;
wenzelm@39135
   287
wenzelm@39135
   288
wenzelm@39135
   289
(* pretty = uncheck + unparse *)
wenzelm@39135
   290
wenzelm@39135
   291
fun pretty_term ctxt = singleton (uncheck_terms ctxt) #> unparse_term ctxt;
wenzelm@39135
   292
fun pretty_typ ctxt = singleton (uncheck_typs ctxt) #> unparse_typ ctxt;
wenzelm@39135
   293
fun pretty_sort ctxt = uncheck_sort ctxt #> unparse_sort ctxt;
wenzelm@39135
   294
fun pretty_classrel ctxt = uncheck_classrel ctxt #> unparse_classrel ctxt;
wenzelm@39135
   295
fun pretty_arity ctxt = uncheck_arity ctxt #> unparse_arity ctxt;
wenzelm@39135
   296
wenzelm@39135
   297
val string_of_term = Pretty.string_of oo pretty_term;
wenzelm@39135
   298
val string_of_typ = Pretty.string_of oo pretty_typ;
wenzelm@39135
   299
val string_of_sort = Pretty.string_of oo pretty_sort;
wenzelm@39135
   300
val string_of_classrel = Pretty.string_of oo pretty_classrel;
wenzelm@39135
   301
val string_of_arity = Pretty.string_of oo pretty_arity;
wenzelm@39135
   302
wenzelm@39135
   303
wenzelm@39135
   304
(* global pretty printing *)
wenzelm@39135
   305
wenzelm@39508
   306
val pretty_global = Config.bool (Config.declare "Syntax.pretty_global" (K (Config.Bool false)));
wenzelm@39508
   307
fun is_pretty_global ctxt = Config.get ctxt pretty_global;
wenzelm@39508
   308
val set_pretty_global = Config.put pretty_global;
wenzelm@42360
   309
val init_pretty_global = set_pretty_global true o Proof_Context.init_global;
wenzelm@42383
   310
val init_pretty = Context.pretty_context init_pretty_global;
wenzelm@39135
   311
wenzelm@39135
   312
val pretty_term_global = pretty_term o init_pretty_global;
wenzelm@39135
   313
val pretty_typ_global = pretty_typ o init_pretty_global;
wenzelm@39135
   314
val pretty_sort_global = pretty_sort o init_pretty_global;
wenzelm@39135
   315
wenzelm@39135
   316
val string_of_term_global = string_of_term o init_pretty_global;
wenzelm@39135
   317
val string_of_typ_global = string_of_typ o init_pretty_global;
wenzelm@39135
   318
val string_of_sort_global = string_of_sort o init_pretty_global;
wenzelm@39135
   319
wenzelm@39135
   320
wenzelm@39135
   321
wenzelm@237
   322
(** tables of translation functions **)
wenzelm@237
   323
wenzelm@5692
   324
(* parse (ast) translations *)
wenzelm@5692
   325
wenzelm@23655
   326
fun err_dup_trfun name c =
wenzelm@23655
   327
  error ("More than one " ^ name ^ " for " ^ quote c);
wenzelm@237
   328
wenzelm@42253
   329
fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c);
wenzelm@42253
   330
wenzelm@42288
   331
fun remove_trtab trfuns = fold (Symtab.remove Syntax_Ext.eq_trfun) trfuns;
wenzelm@21536
   332
haftmann@29004
   333
fun update_trtab name trfuns tab = fold Symtab.update_new trfuns (remove_trtab trfuns tab)
wenzelm@23655
   334
  handle Symtab.DUP c => err_dup_trfun name c;
wenzelm@237
   335
wenzelm@42288
   336
fun merge_trtabs name tab1 tab2 = Symtab.merge Syntax_Ext.eq_trfun (tab1, tab2)
wenzelm@23655
   337
  handle Symtab.DUP c => err_dup_trfun name c;
wenzelm@237
   338
wenzelm@237
   339
wenzelm@5692
   340
(* print (ast) translations *)
wenzelm@5692
   341
wenzelm@42254
   342
fun apply_tr' tab c ctxt T args =
wenzelm@42254
   343
  let
wenzelm@42254
   344
    val fns = map fst (Symtab.lookup_list tab c);
wenzelm@42254
   345
    fun app_first [] = raise Match
wenzelm@42254
   346
      | app_first (f :: fs) = f ctxt T args handle Match => app_first fs;
wenzelm@42254
   347
  in app_first fns end;
wenzelm@42254
   348
wenzelm@42254
   349
fun apply_ast_tr' tab c ctxt args =
wenzelm@42254
   350
  let
wenzelm@42254
   351
    val fns = map fst (Symtab.lookup_list tab c);
wenzelm@42254
   352
    fun app_first [] = raise Match
wenzelm@42254
   353
      | app_first (f :: fs) = f ctxt args handle Match => app_first fs;
wenzelm@42254
   354
  in app_first fns end;
wenzelm@42253
   355
wenzelm@42288
   356
fun update_tr'tab trfuns = fold_rev (Symtab.update_list Syntax_Ext.eq_trfun) trfuns;
wenzelm@42288
   357
fun remove_tr'tab trfuns = fold (Symtab.remove_list Syntax_Ext.eq_trfun) trfuns;
wenzelm@42288
   358
fun merge_tr'tabs tab1 tab2 = Symtab.merge_list Syntax_Ext.eq_trfun (tab1, tab2);
wenzelm@5692
   359
wenzelm@5692
   360
wenzelm@237
   361
wenzelm@237
   362
(** tables of translation rules **)
wenzelm@237
   363
wenzelm@5692
   364
type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
wenzelm@237
   365
wenzelm@19482
   366
fun dest_ruletab tab = maps snd (Symtab.dest tab);
wenzelm@237
   367
wenzelm@25394
   368
val update_ruletab = fold_rev (fn r => Symtab.update_list (op =) (Ast.head_of_rule r, r));
wenzelm@18931
   369
val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r));
wenzelm@18931
   370
fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2);
clasohm@0
   371
clasohm@0
   372
clasohm@0
   373
clasohm@0
   374
(** datatype syntax **)
clasohm@0
   375
wenzelm@237
   376
datatype syntax =
wenzelm@237
   377
  Syntax of {
wenzelm@42288
   378
    input: Syntax_Ext.xprod list,
wenzelm@4703
   379
    lexicon: Scan.lexicon,
wenzelm@45632
   380
    gram: Parser.gram lazy,
wenzelm@42298
   381
    consts: string Symtab.table,
wenzelm@2913
   382
    prmodes: string list,
wenzelm@21772
   383
    parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table,
wenzelm@237
   384
    parse_ruletab: ruletab,
wenzelm@21772
   385
    parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table,
wenzelm@42247
   386
    print_trtab: ((Proof.context -> typ -> term list -> term) * stamp) list Symtab.table,
wenzelm@237
   387
    print_ruletab: ruletab,
wenzelm@21772
   388
    print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
wenzelm@17079
   389
    prtabs: Printer.prtabs} * stamp;
clasohm@0
   390
wenzelm@17079
   391
fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2;
wenzelm@17079
   392
wenzelm@45632
   393
fun force_syntax (Syntax ({gram, ...}, _)) = ignore (Lazy.force gram);
wenzelm@44802
   394
wenzelm@42298
   395
fun lookup_const (Syntax ({consts, ...}, _)) = Symtab.lookup consts;
wenzelm@17079
   396
fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode;
wenzelm@42251
   397
fun tokenize (Syntax ({lexicon, ...}, _)) = Lexicon.tokenize lexicon;
wenzelm@45641
   398
fun parse (Syntax ({gram, ...}, _)) = Parser.parse (Lazy.force gram);
wenzelm@14687
   399
wenzelm@42253
   400
fun parse_ast_translation (Syntax ({parse_ast_trtab, ...}, _)) = lookup_tr parse_ast_trtab;
wenzelm@42253
   401
fun parse_translation (Syntax ({parse_trtab, ...}, _)) = lookup_tr parse_trtab;
wenzelm@42255
   402
fun parse_rules (Syntax ({parse_ruletab, ...}, _)) = Symtab.lookup_list parse_ruletab;
wenzelm@42254
   403
fun print_translation (Syntax ({print_trtab, ...}, _)) = apply_tr' print_trtab;
wenzelm@42255
   404
fun print_rules (Syntax ({print_ruletab, ...}, _)) = Symtab.lookup_list print_ruletab;
wenzelm@42254
   405
fun print_ast_translation (Syntax ({print_ast_trtab, ...}, _)) = apply_ast_tr' print_ast_trtab;
wenzelm@42253
   406
wenzelm@42255
   407
fun prtabs (Syntax ({prtabs, ...}, _)) = prtabs;
wenzelm@42255
   408
wenzelm@20784
   409
type mode = string * bool;
wenzelm@24970
   410
val mode_default = ("", true);
wenzelm@37146
   411
val mode_input = (Print_Mode.input, true);
wenzelm@15755
   412
wenzelm@18
   413
wenzelm@237
   414
(* empty_syntax *)
wenzelm@18
   415
wenzelm@17079
   416
val empty_syntax = Syntax
wenzelm@17079
   417
  ({input = [],
wenzelm@4703
   418
    lexicon = Scan.empty_lexicon,
wenzelm@45632
   419
    gram = Lazy.value Parser.empty_gram,
wenzelm@42298
   420
    consts = Symtab.empty,
wenzelm@2913
   421
    prmodes = [],
wenzelm@5692
   422
    parse_ast_trtab = Symtab.empty,
wenzelm@5692
   423
    parse_ruletab = Symtab.empty,
wenzelm@5692
   424
    parse_trtab = Symtab.empty,
wenzelm@5692
   425
    print_trtab = Symtab.empty,
wenzelm@5692
   426
    print_ruletab = Symtab.empty,
wenzelm@5692
   427
    print_ast_trtab = Symtab.empty,
wenzelm@17079
   428
    prtabs = Printer.empty_prtabs}, stamp ());
wenzelm@167
   429
wenzelm@167
   430
wenzelm@25394
   431
(* update_syntax *)
wenzelm@167
   432
wenzelm@42298
   433
fun update_const (c, b) tab =
wenzelm@42298
   434
  if c = "" orelse (b = "" andalso (Lexicon.is_marked c orelse Symtab.defined tab c))
wenzelm@42298
   435
  then tab
wenzelm@42298
   436
  else Symtab.update (c, b) tab;
wenzelm@42298
   437
wenzelm@25394
   438
fun update_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
wenzelm@167
   439
  let
wenzelm@42268
   440
    val {input, lexicon, gram, consts = consts1, prmodes, parse_ast_trtab, parse_ruletab,
wenzelm@42268
   441
      parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs;
wenzelm@42288
   442
    val Syntax_Ext.Syn_Ext {xprods, consts = consts2, parse_ast_translation,
wenzelm@42268
   443
      parse_rules, parse_translation, print_translation, print_rules,
wenzelm@42268
   444
      print_ast_translation} = syn_ext;
wenzelm@36208
   445
    val new_xprods =
wenzelm@36208
   446
      if inout then distinct (op =) (filter_out (member (op =) input) xprods) else [];
wenzelm@19546
   447
    fun if_inout xs = if inout then xs else [];
wenzelm@167
   448
  in
wenzelm@17079
   449
    Syntax
wenzelm@36208
   450
    ({input = new_xprods @ input,
wenzelm@42288
   451
      lexicon = fold Scan.extend_lexicon (Syntax_Ext.delims_of new_xprods) lexicon,
wenzelm@45632
   452
      gram = Lazy.value (Parser.extend_gram new_xprods (Lazy.force gram)),
wenzelm@42298
   453
      consts = fold update_const consts2 consts1,
wenzelm@42268
   454
      prmodes = insert (op =) mode prmodes,
wenzelm@167
   455
      parse_ast_trtab =
wenzelm@25394
   456
        update_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab,
wenzelm@25394
   457
      parse_ruletab = update_ruletab (if_inout parse_rules) parse_ruletab,
wenzelm@25394
   458
      parse_trtab = update_trtab "parse translation" (if_inout parse_translation) parse_trtab,
wenzelm@25394
   459
      print_trtab = update_tr'tab print_translation print_trtab,
wenzelm@25394
   460
      print_ruletab = update_ruletab print_rules print_ruletab,
wenzelm@25394
   461
      print_ast_trtab = update_tr'tab print_ast_translation print_ast_trtab,
wenzelm@25394
   462
      prtabs = Printer.update_prtabs mode xprods prtabs}, stamp ())
wenzelm@18
   463
  end;
wenzelm@18
   464
wenzelm@15755
   465
wenzelm@15755
   466
(* remove_syntax *)
wenzelm@15755
   467
wenzelm@17079
   468
fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
wenzelm@15755
   469
  let
wenzelm@42288
   470
    val Syntax_Ext.Syn_Ext {xprods, consts = _, parse_ast_translation, parse_rules,
wenzelm@42268
   471
      parse_translation, print_translation, print_rules, print_ast_translation} = syn_ext;
wenzelm@42268
   472
    val {input, lexicon, gram, consts, prmodes, parse_ast_trtab, parse_ruletab,
wenzelm@42268
   473
      parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs;
wenzelm@19300
   474
    val input' = if inout then subtract (op =) xprods input else input;
wenzelm@25394
   475
    val changed = length input <> length input';
wenzelm@19546
   476
    fun if_inout xs = if inout then xs else [];
wenzelm@15755
   477
  in
wenzelm@17079
   478
    Syntax
wenzelm@17079
   479
    ({input = input',
wenzelm@42288
   480
      lexicon = if changed then Scan.make_lexicon (Syntax_Ext.delims_of input') else lexicon,
wenzelm@45632
   481
      gram = if changed then Lazy.value (Parser.make_gram input') else gram,
wenzelm@15755
   482
      consts = consts,
wenzelm@15755
   483
      prmodes = prmodes,
wenzelm@19546
   484
      parse_ast_trtab = remove_trtab (if_inout parse_ast_translation) parse_ast_trtab,
wenzelm@19546
   485
      parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab,
wenzelm@19546
   486
      parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab,
wenzelm@15755
   487
      print_trtab = remove_tr'tab print_translation print_trtab,
wenzelm@15755
   488
      print_ruletab = remove_ruletab print_rules print_ruletab,
wenzelm@15755
   489
      print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
wenzelm@17079
   490
      prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ())
wenzelm@15755
   491
  end;
wenzelm@14904
   492
wenzelm@18
   493
wenzelm@45632
   494
(* merge_syntax *)
clasohm@0
   495
wenzelm@45632
   496
fun merge_syntax (Syntax (tabs1, _), Syntax (tabs2, _)) =
clasohm@0
   497
  let
wenzelm@15755
   498
    val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
wenzelm@42268
   499
      prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
wenzelm@42268
   500
      parse_trtab = parse_trtab1, print_trtab = print_trtab1, print_ruletab = print_ruletab1,
wenzelm@42268
   501
      print_ast_trtab = print_ast_trtab1, prtabs = prtabs1} = tabs1;
wenzelm@237
   502
wenzelm@15755
   503
    val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
wenzelm@42268
   504
      prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
wenzelm@42268
   505
      parse_trtab = parse_trtab2, print_trtab = print_trtab2, print_ruletab = print_ruletab2,
wenzelm@42268
   506
      print_ast_trtab = print_ast_trtab2, prtabs = prtabs2} = tabs2;
wenzelm@45632
   507
wenzelm@45632
   508
    val (input', gram') =
wenzelm@45632
   509
      (case subtract (op =) input1 input2 of
wenzelm@45632
   510
        [] => (input1, gram1)
wenzelm@45632
   511
      | new_xprods2 =>
wenzelm@45632
   512
          if subset (op =) (input1, input2) then (input2, gram2)
wenzelm@45632
   513
          else
wenzelm@45632
   514
            let
wenzelm@45632
   515
              val input' = new_xprods2 @ input1;
wenzelm@45632
   516
              val gram' = Lazy.lazy (fn () => Parser.make_gram input');
wenzelm@45632
   517
            in (input', gram') end);
clasohm@0
   518
  in
wenzelm@17079
   519
    Syntax
wenzelm@45632
   520
    ({input = input',
wenzelm@27768
   521
      lexicon = Scan.merge_lexicons (lexicon1, lexicon2),
wenzelm@45632
   522
      gram = gram',
wenzelm@42298
   523
      consts = Symtab.merge (K true) (consts1, consts2),
wenzelm@18921
   524
      prmodes = Library.merge (op =) (prmodes1, prmodes2),
wenzelm@237
   525
      parse_ast_trtab =
wenzelm@15755
   526
        merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
wenzelm@237
   527
      parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
wenzelm@15755
   528
      parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
wenzelm@5692
   529
      print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
wenzelm@237
   530
      print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
wenzelm@5692
   531
      print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
wenzelm@17079
   532
      prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ())
clasohm@0
   533
  end;
clasohm@0
   534
clasohm@0
   535
wenzelm@18720
   536
(* basic syntax *)
wenzelm@260
   537
wenzelm@42294
   538
val token_markers =
wenzelm@42408
   539
  ["_tfree", "_tvar", "_free", "_bound", "_loose", "_var", "_numeral", "_inner_string"];
wenzelm@260
   540
wenzelm@18720
   541
val basic_nonterms =
wenzelm@49674
   542
  Lexicon.terminals @ ["logic", "type", "types", "sort", "classes",
wenzelm@42293
   543
    "args", "cargs", "pttrn", "pttrns", "idt", "idts", "aprop", "asms",
wenzelm@46236
   544
    "any", "prop'", "num_const", "float_const", "xnum_const", "num_position",
wenzelm@49674
   545
    "float_position", "xnum_position", "index", "struct", "tid_position",
wenzelm@49674
   546
    "tvar_position", "id_position", "longid_position", "str_position",
wenzelm@49674
   547
    "type_name", "class_name"];
wenzelm@18720
   548
clasohm@0
   549
wenzelm@4887
   550
wenzelm@15759
   551
(** print syntax **)
wenzelm@15759
   552
wenzelm@15759
   553
local
clasohm@0
   554
wenzelm@260
   555
fun pretty_strs_qs name strs =
wenzelm@28840
   556
  Pretty.strs (name :: map quote (sort_strings strs));
clasohm@0
   557
wenzelm@17079
   558
fun pretty_gram (Syntax (tabs, _)) =
clasohm@0
   559
  let
wenzelm@32784
   560
    val {lexicon, prmodes, gram, ...} = tabs;
wenzelm@28375
   561
    val prmodes' = sort_strings (filter_out (fn s => s = "") prmodes);
clasohm@0
   562
  in
wenzelm@8720
   563
    [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon),
wenzelm@45632
   564
      Pretty.big_list "prods:" (Parser.pretty_gram (Lazy.force gram)),
wenzelm@8720
   565
      pretty_strs_qs "print modes:" prmodes']
clasohm@0
   566
  end;
clasohm@0
   567
wenzelm@17079
   568
fun pretty_trans (Syntax (tabs, _)) =
clasohm@0
   569
  let
wenzelm@42298
   570
    fun pretty_tab name tab =
wenzelm@42298
   571
      pretty_strs_qs name (sort_strings (Symtab.keys tab));
clasohm@0
   572
wenzelm@260
   573
    fun pretty_ruletab name tab =
wenzelm@5692
   574
      Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
clasohm@0
   575
wenzelm@237
   576
    val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
wenzelm@42268
   577
      print_ruletab, print_ast_trtab, ...} = tabs;
clasohm@0
   578
  in
wenzelm@42298
   579
   [pretty_tab "consts:" consts,
wenzelm@42298
   580
    pretty_tab "parse_ast_translation:" parse_ast_trtab,
wenzelm@42298
   581
    pretty_ruletab "parse_rules:" parse_ruletab,
wenzelm@42298
   582
    pretty_tab "parse_translation:" parse_trtab,
wenzelm@42298
   583
    pretty_tab "print_translation:" print_trtab,
wenzelm@42298
   584
    pretty_ruletab "print_rules:" print_ruletab,
wenzelm@42298
   585
    pretty_tab "print_ast_translation:" print_ast_trtab]
clasohm@0
   586
  end;
clasohm@0
   587
wenzelm@15759
   588
in
clasohm@0
   589
wenzelm@15759
   590
fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn));
wenzelm@15759
   591
fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn));
wenzelm@15759
   592
fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn));
clasohm@0
   593
wenzelm@15759
   594
end;
clasohm@0
   595
clasohm@0
   596
wenzelm@26951
   597
(* reconstructing infixes -- educated guessing *)
wenzelm@26951
   598
wenzelm@26951
   599
fun guess_infix (Syntax ({gram, ...}, _)) c =
wenzelm@45632
   600
  (case Parser.guess_infix_lr (Lazy.force gram) c of
wenzelm@26951
   601
    SOME (s, l, r, j) => SOME
wenzelm@35130
   602
     (if l then Mixfix.Infixl (s, j)
wenzelm@35130
   603
      else if r then Mixfix.Infixr (s, j)
wenzelm@35130
   604
      else Mixfix.Infix (s, j))
wenzelm@26951
   605
  | NONE => NONE);
wenzelm@26951
   606
wenzelm@26951
   607
clasohm@0
   608
wenzelm@1158
   609
(** prepare translation rules **)
wenzelm@1158
   610
wenzelm@42204
   611
(* rules *)
wenzelm@42204
   612
wenzelm@1158
   613
datatype 'a trrule =
wenzelm@42204
   614
  Parse_Rule of 'a * 'a |
wenzelm@42204
   615
  Print_Rule of 'a * 'a |
wenzelm@42204
   616
  Parse_Print_Rule of 'a * 'a;
wenzelm@888
   617
wenzelm@42204
   618
fun map_trrule f (Parse_Rule (x, y)) = Parse_Rule (f x, f y)
wenzelm@42204
   619
  | map_trrule f (Print_Rule (x, y)) = Print_Rule (f x, f y)
wenzelm@42204
   620
  | map_trrule f (Parse_Print_Rule (x, y)) = Parse_Print_Rule (f x, f y);
wenzelm@1158
   621
wenzelm@42204
   622
fun parse_rule (Parse_Rule pats) = SOME pats
wenzelm@42204
   623
  | parse_rule (Print_Rule _) = NONE
wenzelm@42204
   624
  | parse_rule (Parse_Print_Rule pats) = SOME pats;
wenzelm@1158
   625
wenzelm@42204
   626
fun print_rule (Parse_Rule _) = NONE
wenzelm@42204
   627
  | print_rule (Print_Rule pats) = SOME (swap pats)
wenzelm@42204
   628
  | print_rule (Parse_Print_Rule pats) = SOME (swap pats);
wenzelm@1158
   629
wenzelm@1158
   630
wenzelm@42204
   631
(* check_rules *)
wenzelm@42204
   632
wenzelm@19262
   633
local
wenzelm@19262
   634
wenzelm@42048
   635
fun check_rule rule =
wenzelm@5692
   636
  (case Ast.rule_error rule of
skalberg@15531
   637
    SOME msg =>
wenzelm@1158
   638
      error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
wenzelm@42048
   639
        Pretty.string_of (Ast.pretty_rule rule))
skalberg@15531
   640
  | NONE => rule);
wenzelm@888
   641
wenzelm@42204
   642
in
wenzelm@42204
   643
wenzelm@42204
   644
fun check_rules rules =
wenzelm@42204
   645
 (map check_rule (map_filter parse_rule rules),
wenzelm@42204
   646
  map check_rule (map_filter print_rule rules));
wenzelm@42204
   647
wenzelm@42204
   648
end;
wenzelm@42204
   649
wenzelm@42204
   650
wenzelm@18
   651
wenzelm@19262
   652
(** modify syntax **)
wenzelm@383
   653
wenzelm@25394
   654
fun ext_syntax f decls = update_syntax mode_default (f decls);
wenzelm@383
   655
wenzelm@42288
   656
val update_trfuns = ext_syntax Syntax_Ext.syn_ext_trfuns;
wenzelm@42288
   657
val update_advanced_trfuns = ext_syntax Syntax_Ext.syn_ext_advanced_trfuns;
wenzelm@5692
   658
wenzelm@35412
   659
fun update_type_gram add prmode decls =
wenzelm@35412
   660
  (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_types decls);
wenzelm@25387
   661
wenzelm@35412
   662
fun update_const_gram add is_logtype prmode decls =
wenzelm@35412
   663
  (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_consts is_logtype decls);
wenzelm@15755
   664
wenzelm@42288
   665
val update_trrules = ext_syntax Syntax_Ext.syn_ext_rules o check_rules;
wenzelm@42288
   666
val remove_trrules = remove_syntax mode_default o Syntax_Ext.syn_ext_rules o check_rules;
wenzelm@5692
   667
wenzelm@42476
   668
wenzelm@42476
   669
open Lexicon.Syntax;
wenzelm@42476
   670
clasohm@0
   671
end;
wenzelm@5692
   672