src/Pure/Syntax/syntax.ML
author paulson <lp15@cam.ac.uk>
Sun, 20 May 2018 18:37:34 +0100
changeset 68239 0764ee22a4d1
parent 67514 6877af8bc18d
child 69002 f0d4b1db0ea0
permissions -rw-r--r--
tidy up of Derivative
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     1
(*  Title:      Pure/Syntax/syntax.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     3
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
     4
Standard Isabelle syntax, based on arbitrary context-free grammars
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
     5
(specified by mixfix declarations).
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
signature SYNTAX =
2383
4127499d9b52 added extend_trfunsT;
wenzelm
parents: 2366
diff changeset
     9
sig
42382
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
    10
  type operations
62897
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
    11
  val install_operations: operations -> theory -> theory
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
    12
  val root: string Config.T
46512
4f9f61f9b535 simplified configuration options for syntax ambiguity;
wenzelm
parents: 46506
diff changeset
    13
  val ambiguity_warning_raw: Config.raw
4f9f61f9b535 simplified configuration options for syntax ambiguity;
wenzelm
parents: 46506
diff changeset
    14
  val ambiguity_warning: bool Config.T
46506
c7faa011bfa7 simplified configuration options for syntax ambiguity;
wenzelm
parents: 46483
diff changeset
    15
  val ambiguity_limit_raw: Config.raw
42268
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
    16
  val ambiguity_limit: int Config.T
59795
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
    17
  val encode_input: Input.source -> XML.tree
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
    18
  val implode_input: Input.source -> string
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
    19
  val read_input_pos: string -> Position.T
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
    20
  val read_input: string -> Input.source
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
    21
  val parse_input: Proof.context -> (XML.tree list -> 'a) ->
55828
42ac3cfb89f6 clarified language markup: added "delimited" property;
wenzelm
parents: 52160
diff changeset
    22
    (bool -> Markup.T) -> (Symbol_Pos.T list * Position.T -> 'a) -> string -> 'a
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    23
  val parse_sort: Proof.context -> string -> sort
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    24
  val parse_typ: Proof.context -> string -> typ
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    25
  val parse_term: Proof.context -> string -> term
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    26
  val parse_prop: Proof.context -> string -> term
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    27
  val unparse_sort: Proof.context -> sort -> Pretty.T
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    28
  val unparse_classrel: Proof.context -> class list -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    29
  val unparse_arity: Proof.context -> arity -> Pretty.T
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    30
  val unparse_typ: Proof.context -> typ -> Pretty.T
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    31
  val unparse_term: Proof.context -> term -> Pretty.T
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
    32
  val check_sort: Proof.context -> sort -> sort
24512
fc4959967b30 added singleton check_typ/term/prop;
wenzelm
parents: 24488
diff changeset
    33
  val check_typ: Proof.context -> typ -> typ
fc4959967b30 added singleton check_typ/term/prop;
wenzelm
parents: 24488
diff changeset
    34
  val check_term: Proof.context -> term -> term
fc4959967b30 added singleton check_typ/term/prop;
wenzelm
parents: 24488
diff changeset
    35
  val check_prop: Proof.context -> term -> term
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
    36
  val check_typs: Proof.context -> typ list -> typ list
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    37
  val check_terms: Proof.context -> term list -> term list
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    38
  val check_props: Proof.context -> term list -> term list
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    39
  val uncheck_sort: Proof.context -> sort -> sort
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    40
  val uncheck_arity: Proof.context -> arity -> arity
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    41
  val uncheck_classrel: Proof.context -> class list -> class list
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    42
  val uncheck_typs: Proof.context -> typ list -> typ list
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    43
  val uncheck_terms: Proof.context -> term list -> term list
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    44
  val read_sort: Proof.context -> string -> sort
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    45
  val read_typ: Proof.context -> string -> typ
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
    46
  val read_term: Proof.context -> string -> term
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
    47
  val read_prop: Proof.context -> string -> term
46989
88b0a8052c75 added Syntax.read_typs;
wenzelm
parents: 46849
diff changeset
    48
  val read_typs: Proof.context -> string list -> typ list
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    49
  val read_terms: Proof.context -> string list -> term list
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    50
  val read_props: Proof.context -> string list -> term list
24709
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
    51
  val read_sort_global: theory -> string -> sort
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
    52
  val read_typ_global: theory -> string -> typ
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
    53
  val read_term_global: theory -> string -> term
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
    54
  val read_prop_global: theory -> string -> term
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    55
  val pretty_term: Proof.context -> term -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    56
  val pretty_typ: Proof.context -> typ -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    57
  val pretty_sort: Proof.context -> sort -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    58
  val pretty_classrel: Proof.context -> class list -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    59
  val pretty_arity: Proof.context -> arity -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    60
  val string_of_term: Proof.context -> term -> string
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    61
  val string_of_typ: Proof.context -> typ -> string
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    62
  val string_of_sort: Proof.context -> sort -> string
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    63
  val string_of_classrel: Proof.context -> class list -> string
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    64
  val string_of_arity: Proof.context -> arity -> string
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    65
  val is_pretty_global: Proof.context -> bool
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    66
  val set_pretty_global: bool -> Proof.context -> Proof.context
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    67
  val init_pretty_global: theory -> Proof.context
61262
7bd1eb4b056e tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents: 59841
diff changeset
    68
  val init_pretty: Context.generic -> Proof.context
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    69
  val pretty_term_global: theory -> term -> Pretty.T
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    70
  val pretty_typ_global: theory -> typ -> Pretty.T
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    71
  val pretty_sort_global: theory -> sort -> Pretty.T
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    72
  val string_of_term_global: theory -> term -> string
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    73
  val string_of_typ_global: theory -> typ -> string
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    74
  val string_of_sort_global: theory -> sort -> string
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
    75
  type syntax
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
    76
  val eq_syntax: syntax * syntax -> bool
45632
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
    77
  val force_syntax: syntax -> unit
42298
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
    78
  val lookup_const: syntax -> string -> string option
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
    79
  val is_keyword: syntax -> string -> bool
42251
050cc12dd985 explicit Syntax.tokenize, Syntax.parse;
wenzelm
parents: 42247
diff changeset
    80
  val tokenize: syntax -> bool -> Symbol_Pos.T list -> Lexicon.token list
45641
20ef9135a9fb removed obsolete argument (cf. 954e9d6782ea);
wenzelm
parents: 45632
diff changeset
    81
  val parse: syntax -> string -> Lexicon.token list -> Parser.parsetree list
42253
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
    82
  val parse_ast_translation: syntax -> string -> (Proof.context -> Ast.ast list -> Ast.ast) option
42255
097c93dcd877 eliminated slightly odd Syntax.rep_syntax;
wenzelm
parents: 42254
diff changeset
    83
  val parse_rules: syntax -> string -> (Ast.ast * Ast.ast) list
42253
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
    84
  val parse_translation: syntax -> string -> (Proof.context -> term list -> term) option
42254
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
    85
  val print_translation: syntax -> string ->
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
    86
    Proof.context -> typ -> term list -> term  (*exception Match*)
42255
097c93dcd877 eliminated slightly odd Syntax.rep_syntax;
wenzelm
parents: 42254
diff changeset
    87
  val print_rules: syntax -> string -> (Ast.ast * Ast.ast) list
42254
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
    88
  val print_ast_translation: syntax -> string ->
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
    89
    Proof.context -> Ast.ast list -> Ast.ast  (*exception Match*)
42255
097c93dcd877 eliminated slightly odd Syntax.rep_syntax;
wenzelm
parents: 42254
diff changeset
    90
  val prtabs: syntax -> Printer.prtabs
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
    91
  type mode
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
    92
  val mode_default: mode
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
    93
  val mode_input: mode
42294
0f4372a2d2e4 simplified Pure syntax bootstrap;
wenzelm
parents: 42293
diff changeset
    94
  val empty_syntax: syntax
45632
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
    95
  val merge_syntax: syntax * syntax -> syntax
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
    96
  val print_gram: syntax -> unit
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
    97
  val print_trans: syntax -> unit
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
    98
  val print_syntax: syntax -> unit
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
    99
  val guess_infix: syntax -> string -> mixfix option
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   100
  datatype 'a trrule =
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   101
    Parse_Rule of 'a * 'a |
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   102
    Print_Rule of 'a * 'a |
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   103
    Parse_Print_Rule of 'a * 'a
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   104
  val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   105
  val update_trfuns:
42224
578a51fae383 discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents: 42223
diff changeset
   106
    (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list *
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   107
    (string * ((Proof.context -> term list -> term) * stamp)) list *
42247
12fe41a92cd5 typed_print_translation: discontinued show_sorts argument;
wenzelm
parents: 42245
diff changeset
   108
    (string * ((Proof.context -> typ -> term list -> term) * stamp)) list *
42224
578a51fae383 discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents: 42223
diff changeset
   109
    (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list -> syntax -> syntax
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   110
  val update_type_gram: bool -> mode -> (string * typ * mixfix) list -> syntax -> syntax
59841
2551ac44150e tuned signature;
wenzelm
parents: 59795
diff changeset
   111
  val update_const_gram: bool -> string list ->
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   112
    mode -> (string * typ * mixfix) list -> syntax -> syntax
42224
578a51fae383 discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents: 42223
diff changeset
   113
  val update_trrules: Ast.ast trrule list -> syntax -> syntax
578a51fae383 discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents: 42223
diff changeset
   114
  val remove_trrules: Ast.ast trrule list -> syntax -> syntax
42476
d0bc1268ef09 clarified auxiliary structure Lexicon.Syntax;
wenzelm
parents: 42408
diff changeset
   115
  val const: string -> term
d0bc1268ef09 clarified auxiliary structure Lexicon.Syntax;
wenzelm
parents: 42408
diff changeset
   116
  val free: string -> term
d0bc1268ef09 clarified auxiliary structure Lexicon.Syntax;
wenzelm
parents: 42408
diff changeset
   117
  val var: indexname -> term
2383
4127499d9b52 added extend_trfunsT;
wenzelm
parents: 2366
diff changeset
   118
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
12094
wenzelm
parents: 12073
diff changeset
   120
structure Syntax: SYNTAX =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   123
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   124
(** inner syntax operations **)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   125
62897
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   126
(* operations *)
42382
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   127
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   128
type operations =
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   129
 {parse_sort: Proof.context -> string -> sort,
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   130
  parse_typ: Proof.context -> string -> typ,
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   131
  parse_term: Proof.context -> string -> term,
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   132
  parse_prop: Proof.context -> string -> term,
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   133
  unparse_sort: Proof.context -> sort -> Pretty.T,
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   134
  unparse_typ: Proof.context -> typ -> Pretty.T,
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   135
  unparse_term: Proof.context -> term -> Pretty.T,
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   136
  check_typs: Proof.context -> typ list -> typ list,
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   137
  check_terms: Proof.context -> term list -> term list,
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   138
  check_props: Proof.context -> term list -> term list,
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   139
  uncheck_typs: Proof.context -> typ list -> typ list,
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   140
  uncheck_terms: Proof.context -> term list -> term list};
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   141
62897
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   142
structure Operations = Theory_Data
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   143
(
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   144
  type T = operations option;
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   145
  val empty = NONE;
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   146
  val extend = I;
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   147
  val merge = merge_options;
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   148
);
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   149
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   150
fun install_operations ops =
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   151
  Operations.map
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   152
    (fn NONE => SOME ops
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   153
      | SOME _ => raise Fail "Inner syntax operations already installed");
42382
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   154
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   155
fun operation which ctxt x =
62897
8093203f0b89 prefer regular context update, to allow continuous editing of Pure;
wenzelm
parents: 62752
diff changeset
   156
  (case Operations.get (Proof_Context.theory_of ctxt) of
42382
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   157
    NONE => raise Fail "Inner syntax operations not installed"
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   158
  | SOME ops => which ops ctxt x);
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   159
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   160
42268
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   161
(* configuration options *)
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   162
64556
851ae0e7b09c more symbols;
wenzelm
parents: 63345
diff changeset
   163
val root = Config.string (Config.declare ("syntax_root", \<^here>) (K (Config.String "any")));
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   164
56438
7f6b2634d853 more source positions;
wenzelm
parents: 56334
diff changeset
   165
val ambiguity_warning_raw =
64556
851ae0e7b09c more symbols;
wenzelm
parents: 63345
diff changeset
   166
  Config.declare ("syntax_ambiguity_warning", \<^here>) (fn _ => Config.Bool true);
46512
4f9f61f9b535 simplified configuration options for syntax ambiguity;
wenzelm
parents: 46506
diff changeset
   167
val ambiguity_warning = Config.bool ambiguity_warning_raw;
42268
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   168
56438
7f6b2634d853 more source positions;
wenzelm
parents: 56334
diff changeset
   169
val ambiguity_limit_raw =
64556
851ae0e7b09c more symbols;
wenzelm
parents: 63345
diff changeset
   170
  Config.declare ("syntax_ambiguity_limit", \<^here>) (fn _ => Config.Int 10);
46506
c7faa011bfa7 simplified configuration options for syntax ambiguity;
wenzelm
parents: 46483
diff changeset
   171
val ambiguity_limit = Config.int ambiguity_limit_raw;
44069
d7c7ec248ef0 make syntax ambiguity warnings a config option
kleing
parents: 43731
diff changeset
   172
42268
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   173
59795
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   174
(* formal input *)
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   175
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   176
fun encode_input source =
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   177
  let
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   178
    val delimited = Input.is_delimited source;
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   179
    val pos = Input.pos_of source;
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   180
    val text = Input.text_of source;
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   181
  in XML.Elem (Markup.input delimited (Position.properties_of pos), [XML.Text text]) end;
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   182
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   183
val implode_input = encode_input #> YXML.string_of;
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   184
55828
42ac3cfb89f6 clarified language markup: added "delimited" property;
wenzelm
parents: 52160
diff changeset
   185
local
42ac3cfb89f6 clarified language markup: added "delimited" property;
wenzelm
parents: 52160
diff changeset
   186
59795
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   187
fun input_range (XML.Elem ((name, props), _)) =
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   188
      if name = Markup.inputN
58978
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58047
diff changeset
   189
      then (Markup.is_delimited props, Position.range_of_properties props)
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58047
diff changeset
   190
      else (false, Position.no_range)
59795
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   191
  | input_range (XML.Text _) = (false, Position.no_range);
46849
36f392239b6a tuned signature;
wenzelm
parents: 46512
diff changeset
   192
59795
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   193
fun input_source tree =
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   194
  let
39555
ccb223a4d49c added XML.content_of convenience -- cover XML.body, which is the general situation;
wenzelm
parents: 39510
diff changeset
   195
    val text = XML.content_of [tree];
59795
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   196
    val (delimited, range) = input_range tree;
59064
a8bcb5a446c8 more abstract type Input.source;
wenzelm
parents: 58993
diff changeset
   197
  in Input.source delimited text range end;
55828
42ac3cfb89f6 clarified language markup: added "delimited" property;
wenzelm
parents: 52160
diff changeset
   198
42ac3cfb89f6 clarified language markup: added "delimited" property;
wenzelm
parents: 52160
diff changeset
   199
in
42ac3cfb89f6 clarified language markup: added "delimited" property;
wenzelm
parents: 52160
diff changeset
   200
59795
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   201
fun read_input_pos str = #1 (#2 (input_range (YXML.parse str handle Fail msg => error msg)));
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   202
59795
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   203
fun read_input str = input_source (YXML.parse str handle Fail msg => error msg);
43731
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43558
diff changeset
   204
59795
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   205
fun parse_input ctxt decode markup parse str =
43731
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43558
diff changeset
   206
  let
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43558
diff changeset
   207
    fun parse_tree tree =
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43558
diff changeset
   208
      let
59795
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   209
        val source = input_source tree;
59064
a8bcb5a446c8 more abstract type Input.source;
wenzelm
parents: 58993
diff changeset
   210
        val syms = Input.source_explode source;
a8bcb5a446c8 more abstract type Input.source;
wenzelm
parents: 58993
diff changeset
   211
        val pos = Input.pos_of source;
a8bcb5a446c8 more abstract type Input.source;
wenzelm
parents: 58993
diff changeset
   212
        val _ = Context_Position.report ctxt pos (markup (Input.is_delimited source));
58978
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 58047
diff changeset
   213
      in parse (syms, pos) end;
43731
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43558
diff changeset
   214
  in
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43558
diff changeset
   215
    (case YXML.parse_body str handle Fail msg => error msg of
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43558
diff changeset
   216
      body as [tree as XML.Elem ((name, _), _)] =>
59795
d453c69596cc clarified input source;
wenzelm
parents: 59064
diff changeset
   217
        if name = Markup.inputN then parse_tree tree else decode body
43731
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43558
diff changeset
   218
    | [tree as XML.Text _] => parse_tree tree
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43558
diff changeset
   219
    | body => decode body)
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43558
diff changeset
   220
  end;
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43558
diff changeset
   221
55828
42ac3cfb89f6 clarified language markup: added "delimited" property;
wenzelm
parents: 52160
diff changeset
   222
end;
42ac3cfb89f6 clarified language markup: added "delimited" property;
wenzelm
parents: 52160
diff changeset
   223
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   224
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   225
(* (un)parsing *)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   226
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   227
val parse_sort = operation #parse_sort;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   228
val parse_typ = operation #parse_typ;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   229
val parse_term = operation #parse_term;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   230
val parse_prop = operation #parse_prop;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   231
val unparse_sort = operation #unparse_sort;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   232
val unparse_typ = operation #unparse_typ;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   233
val unparse_term = operation #unparse_term;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   234
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   235
45429
fd58cbf8cae3 tuned signature;
wenzelm
parents: 45423
diff changeset
   236
(* (un)checking *)
42382
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   237
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   238
val check_typs = operation #check_typs;
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   239
val check_terms = operation #check_terms;
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   240
val check_props = operation #check_props;
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   241
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   242
val check_typ = singleton o check_typs;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   243
val check_term = singleton o check_terms;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   244
val check_prop = singleton o check_props;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   245
42382
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   246
val uncheck_typs = operation #uncheck_typs;
dcd983ee2c29 provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents: 42360
diff changeset
   247
val uncheck_terms = operation #uncheck_terms;
42402
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   248
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   249
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   250
(* derived operations for algebra of sorts *)
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   251
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   252
local
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   253
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   254
fun map_sort f S =
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   255
  (case f (TFree ("", S)) of
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   256
    TFree ("", S') => S'
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   257
  | _ => raise TYPE ("map_sort", [TFree ("", S)], []));
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   258
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   259
in
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   260
c7139609b67d simplified check/uncheck interfaces: result comparison is hardwired by default;
wenzelm
parents: 42383
diff changeset
   261
val check_sort = map_sort o check_typ;
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   262
val uncheck_sort = map_sort o singleton o uncheck_typs;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   263
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   264
end;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   265
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   266
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   267
val uncheck_classrel = map o singleton o uncheck_sort;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   268
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   269
fun unparse_classrel ctxt cs = Pretty.block (flat
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   270
  (separate [Pretty.str " <", Pretty.brk 1] (map (single o unparse_sort ctxt o single) cs)));
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   271
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   272
fun uncheck_arity ctxt (a, Ss, S) =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   273
  let
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   274
    val T = Type (a, replicate (length Ss) dummyT);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   275
    val a' =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   276
      (case singleton (uncheck_typs ctxt) T of
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   277
        Type (a', _) => a'
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   278
      | T => raise TYPE ("uncheck_arity", [T], []));
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   279
    val Ss' = map (uncheck_sort ctxt) Ss;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   280
    val S' = uncheck_sort ctxt S;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   281
  in (a', Ss', S') end;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   282
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   283
fun unparse_arity ctxt (a, Ss, S) =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   284
  let
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   285
    val prtT = unparse_typ ctxt (Type (a, []));
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   286
    val dom =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   287
      if null Ss then []
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   288
      else [Pretty.list "(" ")" (map (unparse_sort ctxt) Ss), Pretty.brk 1];
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   289
  in Pretty.block ([prtT, Pretty.str " ::", Pretty.brk 1] @ dom @ [unparse_sort ctxt S]) end;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   290
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   291
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   292
(* read = parse + check *)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   293
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   294
fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt;
46989
88b0a8052c75 added Syntax.read_typs;
wenzelm
parents: 46849
diff changeset
   295
88b0a8052c75 added Syntax.read_typs;
wenzelm
parents: 46849
diff changeset
   296
fun read_typs ctxt =
58993
302104d8366b prefer independent parallel map where user input is processed -- avoid non-deterministic feedback in error situations;
wenzelm
parents: 58978
diff changeset
   297
  grouped 10 Par_List.map_independent (parse_typ ctxt) #> check_typs ctxt;
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   298
46989
88b0a8052c75 added Syntax.read_typs;
wenzelm
parents: 46849
diff changeset
   299
fun read_terms ctxt =
58993
302104d8366b prefer independent parallel map where user input is processed -- avoid non-deterministic feedback in error situations;
wenzelm
parents: 58978
diff changeset
   300
  grouped 10 Par_List.map_independent (parse_term ctxt) #> check_terms ctxt;
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   301
46989
88b0a8052c75 added Syntax.read_typs;
wenzelm
parents: 46849
diff changeset
   302
fun read_props ctxt =
58993
302104d8366b prefer independent parallel map where user input is processed -- avoid non-deterministic feedback in error situations;
wenzelm
parents: 58978
diff changeset
   303
  grouped 10 Par_List.map_independent (parse_prop ctxt) #> check_props ctxt;
46989
88b0a8052c75 added Syntax.read_typs;
wenzelm
parents: 46849
diff changeset
   304
88b0a8052c75 added Syntax.read_typs;
wenzelm
parents: 46849
diff changeset
   305
val read_typ = singleton o read_typs;
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   306
val read_term = singleton o read_terms;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   307
val read_prop = singleton o read_props;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   308
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42298
diff changeset
   309
val read_sort_global = read_sort o Proof_Context.init_global;
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42298
diff changeset
   310
val read_typ_global = read_typ o Proof_Context.init_global;
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42298
diff changeset
   311
val read_term_global = read_term o Proof_Context.init_global;
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42298
diff changeset
   312
val read_prop_global = read_prop o Proof_Context.init_global;
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   313
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   314
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   315
(* pretty = uncheck + unparse *)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   316
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   317
fun pretty_term ctxt = singleton (uncheck_terms ctxt) #> unparse_term ctxt;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   318
fun pretty_typ ctxt = singleton (uncheck_typs ctxt) #> unparse_typ ctxt;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   319
fun pretty_sort ctxt = uncheck_sort ctxt #> unparse_sort ctxt;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   320
fun pretty_classrel ctxt = uncheck_classrel ctxt #> unparse_classrel ctxt;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   321
fun pretty_arity ctxt = uncheck_arity ctxt #> unparse_arity ctxt;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   322
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   323
val string_of_term = Pretty.string_of oo pretty_term;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   324
val string_of_typ = Pretty.string_of oo pretty_typ;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   325
val string_of_sort = Pretty.string_of oo pretty_sort;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   326
val string_of_classrel = Pretty.string_of oo pretty_classrel;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   327
val string_of_arity = Pretty.string_of oo pretty_arity;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   328
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   329
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   330
(* global pretty printing *)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   331
56438
7f6b2634d853 more source positions;
wenzelm
parents: 56334
diff changeset
   332
val pretty_global =
64556
851ae0e7b09c more symbols;
wenzelm
parents: 63345
diff changeset
   333
  Config.bool (Config.declare ("Syntax.pretty_global", \<^here>) (K (Config.Bool false)));
39508
dfacdb01e1ec simplified some internal flags using Config.T instead of full-blown Proof_Data;
wenzelm
parents: 39507
diff changeset
   334
fun is_pretty_global ctxt = Config.get ctxt pretty_global;
dfacdb01e1ec simplified some internal flags using Config.T instead of full-blown Proof_Data;
wenzelm
parents: 39507
diff changeset
   335
val set_pretty_global = Config.put pretty_global;
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42298
diff changeset
   336
val init_pretty_global = set_pretty_global true o Proof_Context.init_global;
61262
7bd1eb4b056e tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents: 59841
diff changeset
   337
val init_pretty = Context.cases init_pretty_global I;
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   338
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   339
val pretty_term_global = pretty_term o init_pretty_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   340
val pretty_typ_global = pretty_typ o init_pretty_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   341
val pretty_sort_global = pretty_sort o init_pretty_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   342
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   343
val string_of_term_global = string_of_term o init_pretty_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   344
val string_of_typ_global = string_of_typ o init_pretty_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   345
val string_of_sort_global = string_of_sort o init_pretty_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   346
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   347
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   348
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   349
(** tables of translation functions **)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   350
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   351
(* parse (ast) translations *)
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   352
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23631
diff changeset
   353
fun err_dup_trfun name c =
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23631
diff changeset
   354
  error ("More than one " ^ name ^ " for " ^ quote c);
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   355
42253
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   356
fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c);
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   357
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   358
fun remove_trtab trfuns = fold (Symtab.remove Syntax_Ext.eq_trfun) trfuns;
21536
f119c730f509 extend_trtab: allow identical trfuns to be overwritten;
wenzelm
parents: 20784
diff changeset
   359
29004
a5a91f387791 removed Table.extend, NameSpace.extend_table
haftmann
parents: 28904
diff changeset
   360
fun update_trtab name trfuns tab = fold Symtab.update_new trfuns (remove_trtab trfuns tab)
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23631
diff changeset
   361
  handle Symtab.DUP c => err_dup_trfun name c;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   362
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   363
fun merge_trtabs name tab1 tab2 = Symtab.merge Syntax_Ext.eq_trfun (tab1, tab2)
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23631
diff changeset
   364
  handle Symtab.DUP c => err_dup_trfun name c;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   365
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   366
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   367
(* print (ast) translations *)
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   368
42254
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   369
fun apply_tr' tab c ctxt T args =
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   370
  let
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   371
    val fns = map fst (Symtab.lookup_list tab c);
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   372
    fun app_first [] = raise Match
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   373
      | app_first (f :: fs) = f ctxt T args handle Match => app_first fs;
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   374
  in app_first fns end;
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   375
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   376
fun apply_ast_tr' tab c ctxt args =
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   377
  let
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   378
    val fns = map fst (Symtab.lookup_list tab c);
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   379
    fun app_first [] = raise Match
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   380
      | app_first (f :: fs) = f ctxt args handle Match => app_first fs;
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   381
  in app_first fns end;
42253
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   382
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   383
fun update_tr'tab trfuns = fold_rev (Symtab.update_list Syntax_Ext.eq_trfun) trfuns;
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   384
fun remove_tr'tab trfuns = fold (Symtab.remove_list Syntax_Ext.eq_trfun) trfuns;
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   385
fun merge_tr'tabs tab1 tab2 = Symtab.merge_list Syntax_Ext.eq_trfun (tab1, tab2);
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   386
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   387
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   388
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   389
(** tables of translation rules **)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   390
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   391
type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   392
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19375
diff changeset
   393
fun dest_ruletab tab = maps snd (Symtab.dest tab);
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   394
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   395
val update_ruletab = fold_rev (fn r => Symtab.update_list (op =) (Ast.head_of_rule r, r));
18931
427df66052a1 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18921
diff changeset
   396
val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r));
427df66052a1 TableFun: renamed xxx_multi to xxx_list;
wenzelm
parents: 18921
diff changeset
   397
fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   398
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   399
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   400
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   401
(** datatype syntax **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   402
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   403
datatype syntax =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   404
  Syntax of {
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   405
    input: Syntax_Ext.xprod list,
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4618
diff changeset
   406
    lexicon: Scan.lexicon,
45632
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   407
    gram: Parser.gram lazy,
42298
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   408
    consts: string Symtab.table,
2913
ce271fa4d8e2 fixed diagnostic output of print modes;
wenzelm
parents: 2706
diff changeset
   409
    prmodes: string list,
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   410
    parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   411
    parse_ruletab: ruletab,
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   412
    parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table,
42247
12fe41a92cd5 typed_print_translation: discontinued show_sorts argument;
wenzelm
parents: 42245
diff changeset
   413
    print_trtab: ((Proof.context -> typ -> term list -> term) * stamp) list Symtab.table,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   414
    print_ruletab: ruletab,
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   415
    print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   416
    prtabs: Printer.prtabs} * stamp;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   417
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   418
fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2;
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   419
45632
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   420
fun force_syntax (Syntax ({gram, ...}, _)) = ignore (Lazy.force gram);
44802
65c397cc44ec explicit join_syntax ensures command transaction integrity of 'theory';
wenzelm
parents: 44113
diff changeset
   421
42298
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   422
fun lookup_const (Syntax ({consts, ...}, _)) = Symtab.lookup consts;
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   423
fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode;
42251
050cc12dd985 explicit Syntax.tokenize, Syntax.parse;
wenzelm
parents: 42247
diff changeset
   424
fun tokenize (Syntax ({lexicon, ...}, _)) = Lexicon.tokenize lexicon;
45641
20ef9135a9fb removed obsolete argument (cf. 954e9d6782ea);
wenzelm
parents: 45632
diff changeset
   425
fun parse (Syntax ({gram, ...}, _)) = Parser.parse (Lazy.force gram);
14687
e089757b952a added is_keyword;
wenzelm
parents: 14648
diff changeset
   426
42253
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   427
fun parse_ast_translation (Syntax ({parse_ast_trtab, ...}, _)) = lookup_tr parse_ast_trtab;
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   428
fun parse_translation (Syntax ({parse_trtab, ...}, _)) = lookup_tr parse_trtab;
42255
097c93dcd877 eliminated slightly odd Syntax.rep_syntax;
wenzelm
parents: 42254
diff changeset
   429
fun parse_rules (Syntax ({parse_ruletab, ...}, _)) = Symtab.lookup_list parse_ruletab;
42254
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   430
fun print_translation (Syntax ({print_trtab, ...}, _)) = apply_tr' print_trtab;
42255
097c93dcd877 eliminated slightly odd Syntax.rep_syntax;
wenzelm
parents: 42254
diff changeset
   431
fun print_rules (Syntax ({print_ruletab, ...}, _)) = Symtab.lookup_list print_ruletab;
42254
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   432
fun print_ast_translation (Syntax ({print_ast_trtab, ...}, _)) = apply_ast_tr' print_ast_trtab;
42253
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   433
42255
097c93dcd877 eliminated slightly odd Syntax.rep_syntax;
wenzelm
parents: 42254
diff changeset
   434
fun prtabs (Syntax ({prtabs, ...}, _)) = prtabs;
097c93dcd877 eliminated slightly odd Syntax.rep_syntax;
wenzelm
parents: 42254
diff changeset
   435
20784
eece9aaaf352 Syntax.mode;
wenzelm
parents: 20664
diff changeset
   436
type mode = string * bool;
24970
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
   437
val mode_default = ("", true);
37146
f652333bbf8e renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
wenzelm
parents: 36739
diff changeset
   438
val mode_input = (Print_Mode.input, true);
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   439
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   440
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   441
(* empty_syntax *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   442
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   443
val empty_syntax = Syntax
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   444
  ({input = [],
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4618
diff changeset
   445
    lexicon = Scan.empty_lexicon,
45632
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   446
    gram = Lazy.value Parser.empty_gram,
42298
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   447
    consts = Symtab.empty,
2913
ce271fa4d8e2 fixed diagnostic output of print modes;
wenzelm
parents: 2706
diff changeset
   448
    prmodes = [],
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   449
    parse_ast_trtab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   450
    parse_ruletab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   451
    parse_trtab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   452
    print_trtab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   453
    print_ruletab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   454
    print_ast_trtab = Symtab.empty,
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   455
    prtabs = Printer.empty_prtabs}, stamp ());
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   456
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   457
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   458
(* update_syntax *)
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   459
42298
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   460
fun update_const (c, b) tab =
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   461
  if c = "" orelse (b = "" andalso (Lexicon.is_marked c orelse Symtab.defined tab c))
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   462
  then tab
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   463
  else Symtab.update (c, b) tab;
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   464
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   465
fun update_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   466
  let
42268
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   467
    val {input, lexicon, gram, consts = consts1, prmodes, parse_ast_trtab, parse_ruletab,
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   468
      parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs;
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   469
    val Syntax_Ext.Syn_Ext {xprods, consts = consts2, parse_ast_translation,
42268
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   470
      parse_rules, parse_translation, print_translation, print_rules,
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   471
      print_ast_translation} = syn_ext;
36208
74c5e6e3c1d3 update_syntax: add new productions only once, to allow repeated local notation, for example;
wenzelm
parents: 35668
diff changeset
   472
    val new_xprods =
74c5e6e3c1d3 update_syntax: add new productions only once, to allow repeated local notation, for example;
wenzelm
parents: 35668
diff changeset
   473
      if inout then distinct (op =) (filter_out (member (op =) input) xprods) else [];
19546
00d5c7c7ce07 extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents: 19482
diff changeset
   474
    fun if_inout xs = if inout then xs else [];
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   475
  in
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   476
    Syntax
36208
74c5e6e3c1d3 update_syntax: add new productions only once, to allow repeated local notation, for example;
wenzelm
parents: 35668
diff changeset
   477
    ({input = new_xprods @ input,
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   478
      lexicon = fold Scan.extend_lexicon (Syntax_Ext.delims_of new_xprods) lexicon,
67514
6877af8bc18d prefer lazy update;
wenzelm
parents: 67513
diff changeset
   479
      gram = if null new_xprods then gram else Lazy.map (Parser.extend_gram new_xprods) gram,
42298
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   480
      consts = fold update_const consts2 consts1,
42268
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   481
      prmodes = insert (op =) mode prmodes,
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   482
      parse_ast_trtab =
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   483
        update_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   484
      parse_ruletab = update_ruletab (if_inout parse_rules) parse_ruletab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   485
      parse_trtab = update_trtab "parse translation" (if_inout parse_translation) parse_trtab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   486
      print_trtab = update_tr'tab print_translation print_trtab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   487
      print_ruletab = update_ruletab print_rules print_ruletab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   488
      print_ast_trtab = update_tr'tab print_ast_translation print_ast_trtab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   489
      prtabs = Printer.update_prtabs mode xprods prtabs}, stamp ())
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   490
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   491
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   492
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   493
(* remove_syntax *)
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   494
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   495
fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   496
  let
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   497
    val Syntax_Ext.Syn_Ext {xprods, consts = _, parse_ast_translation, parse_rules,
42268
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   498
      parse_translation, print_translation, print_rules, print_ast_translation} = syn_ext;
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   499
    val {input, lexicon, gram, consts, prmodes, parse_ast_trtab, parse_ruletab,
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   500
      parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs;
19300
7689f81f8996 subtract (op =);
wenzelm
parents: 19262
diff changeset
   501
    val input' = if inout then subtract (op =) xprods input else input;
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   502
    val changed = length input <> length input';
19546
00d5c7c7ce07 extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents: 19482
diff changeset
   503
    fun if_inout xs = if inout then xs else [];
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   504
  in
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   505
    Syntax
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   506
    ({input = input',
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   507
      lexicon = if changed then Scan.make_lexicon (Syntax_Ext.delims_of input') else lexicon,
45632
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   508
      gram = if changed then Lazy.value (Parser.make_gram input') else gram,
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   509
      consts = consts,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   510
      prmodes = prmodes,
19546
00d5c7c7ce07 extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents: 19482
diff changeset
   511
      parse_ast_trtab = remove_trtab (if_inout parse_ast_translation) parse_ast_trtab,
00d5c7c7ce07 extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents: 19482
diff changeset
   512
      parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab,
00d5c7c7ce07 extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents: 19482
diff changeset
   513
      parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab,
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   514
      print_trtab = remove_tr'tab print_translation print_trtab,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   515
      print_ruletab = remove_ruletab print_rules print_ruletab,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   516
      print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   517
      prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ())
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   518
  end;
14904
7d8dc92fcb7f removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents: 14798
diff changeset
   519
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   520
45632
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   521
(* merge_syntax *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   522
45632
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   523
fun merge_syntax (Syntax (tabs1, _), Syntax (tabs2, _)) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   524
  let
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   525
    val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
42268
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   526
      prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   527
      parse_trtab = parse_trtab1, print_trtab = print_trtab1, print_ruletab = print_ruletab1,
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   528
      print_ast_trtab = print_ast_trtab1, prtabs = prtabs1} = tabs1;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   529
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   530
    val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
42268
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   531
      prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   532
      parse_trtab = parse_trtab2, print_trtab = print_trtab2, print_ruletab = print_ruletab2,
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   533
      print_ast_trtab = print_ast_trtab2, prtabs = prtabs2} = tabs2;
45632
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   534
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   535
    val (input', gram') =
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   536
      (case subtract (op =) input1 input2 of
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   537
        [] => (input1, gram1)
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   538
      | new_xprods2 =>
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   539
          if subset (op =) (input1, input2) then (input2, gram2)
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   540
          else
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   541
            let
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   542
              val input' = new_xprods2 @ input1;
66167
1bd268ab885c more informative task_statistics;
wenzelm
parents: 64556
diff changeset
   543
              val gram' = Lazy.lazy_name "Syntax.merge_syntax" (fn () => Parser.make_gram input');
45632
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   544
            in (input', gram') end);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   545
  in
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   546
    Syntax
45632
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   547
    ({input = input',
27768
398c64b2acef adapted Scan.extend_lexicon/merge_lexicons;
wenzelm
parents: 27265
diff changeset
   548
      lexicon = Scan.merge_lexicons (lexicon1, lexicon2),
45632
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   549
      gram = gram',
42298
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   550
      consts = Symtab.merge (K true) (consts1, consts2),
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18857
diff changeset
   551
      prmodes = Library.merge (op =) (prmodes1, prmodes2),
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   552
      parse_ast_trtab =
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   553
        merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   554
      parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   555
      parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   556
      print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   557
      print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   558
      print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   559
      prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ())
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   560
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   561
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   562
4887
bbc13af86c16 added trfun_names;
wenzelm
parents: 4703
diff changeset
   563
15759
wenzelm
parents: 15755
diff changeset
   564
(** print syntax **)
wenzelm
parents: 15755
diff changeset
   565
wenzelm
parents: 15755
diff changeset
   566
local
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   567
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   568
fun pretty_strs_qs name strs =
28840
wenzelm
parents: 28413
diff changeset
   569
  Pretty.strs (name :: map quote (sort_strings strs));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   570
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   571
fun pretty_gram (Syntax (tabs, _)) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   572
  let
32784
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 32738
diff changeset
   573
    val {lexicon, prmodes, gram, ...} = tabs;
28375
c879d88d038a eliminated polymorphic equality;
wenzelm
parents: 27889
diff changeset
   574
    val prmodes' = sort_strings (filter_out (fn s => s = "") prmodes);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   575
  in
67513
731b1ad6759a tuned output;
wenzelm
parents: 66167
diff changeset
   576
    [Pretty.block (Pretty.breaks
731b1ad6759a tuned output;
wenzelm
parents: 66167
diff changeset
   577
      (Pretty.str "lexicon:" :: map (Pretty.quote o Pretty.keyword1) (Scan.dest_lexicon lexicon))),
731b1ad6759a tuned output;
wenzelm
parents: 66167
diff changeset
   578
      Pretty.big_list "productions:" (Parser.pretty_gram (Lazy.force gram)),
8720
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   579
      pretty_strs_qs "print modes:" prmodes']
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   580
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   581
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   582
fun pretty_trans (Syntax (tabs, _)) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   583
  let
42298
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   584
    fun pretty_tab name tab =
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   585
      pretty_strs_qs name (sort_strings (Symtab.keys tab));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   586
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   587
    fun pretty_ruletab name tab =
51580
64ef8260dc60 Pretty.item markup for improved readability of lists of items;
wenzelm
parents: 50201
diff changeset
   588
      Pretty.big_list name (map (Pretty.item o single o Ast.pretty_rule) (dest_ruletab tab));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   589
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   590
    val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
42268
01401287c3f7 discontinued user-defined token translations;
wenzelm
parents: 42264
diff changeset
   591
      print_ruletab, print_ast_trtab, ...} = tabs;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   592
  in
42298
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   593
   [pretty_tab "consts:" consts,
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   594
    pretty_tab "parse_ast_translation:" parse_ast_trtab,
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   595
    pretty_ruletab "parse_rules:" parse_ruletab,
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   596
    pretty_tab "parse_translation:" parse_trtab,
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   597
    pretty_tab "print_translation:" print_trtab,
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   598
    pretty_ruletab "print_rules:" print_ruletab,
d622145603ee more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents: 42297
diff changeset
   599
    pretty_tab "print_ast_translation:" print_ast_trtab]
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   600
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   601
15759
wenzelm
parents: 15755
diff changeset
   602
in
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   603
56334
6b3739fee456 some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents: 55829
diff changeset
   604
fun print_gram syn = Pretty.writeln_chunks (pretty_gram syn);
6b3739fee456 some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents: 55829
diff changeset
   605
fun print_trans syn = Pretty.writeln_chunks (pretty_trans syn);
6b3739fee456 some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents: 55829
diff changeset
   606
fun print_syntax syn = Pretty.writeln_chunks (pretty_gram syn @ pretty_trans syn);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   607
15759
wenzelm
parents: 15755
diff changeset
   608
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   609
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   610
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   611
(* reconstructing infixes -- educated guessing *)
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   612
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   613
fun guess_infix (Syntax ({gram, ...}, _)) c =
45632
b23c42b9f78a prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
wenzelm
parents: 45490
diff changeset
   614
  (case Parser.guess_infix_lr (Lazy.force gram) c of
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   615
    SOME (s, l, r, j) => SOME
63345
wenzelm
parents: 62897
diff changeset
   616
     (if l then Infixl (Input.string s, j, Position.no_range)
wenzelm
parents: 62897
diff changeset
   617
      else if r then Infixr (Input.string s, j, Position.no_range)
wenzelm
parents: 62897
diff changeset
   618
      else Infix (Input.string s, j, Position.no_range))
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   619
  | NONE => NONE);
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   620
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   621
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   622
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   623
(** prepare translation rules **)
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   624
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   625
(* rules *)
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   626
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   627
datatype 'a trrule =
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   628
  Parse_Rule of 'a * 'a |
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   629
  Print_Rule of 'a * 'a |
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   630
  Parse_Print_Rule of 'a * 'a;
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   631
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   632
fun map_trrule f (Parse_Rule (x, y)) = Parse_Rule (f x, f y)
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   633
  | map_trrule f (Print_Rule (x, y)) = Print_Rule (f x, f y)
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   634
  | map_trrule f (Parse_Print_Rule (x, y)) = Parse_Print_Rule (f x, f y);
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   635
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   636
fun parse_rule (Parse_Rule pats) = SOME pats
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   637
  | parse_rule (Print_Rule _) = NONE
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   638
  | parse_rule (Parse_Print_Rule pats) = SOME pats;
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   639
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   640
fun print_rule (Parse_Rule _) = NONE
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   641
  | print_rule (Print_Rule pats) = SOME (swap pats)
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   642
  | print_rule (Parse_Print_Rule pats) = SOME (swap pats);
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   643
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   644
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   645
(* check_rules *)
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   646
19262
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   647
local
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   648
42048
afd11ca8e018 support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents: 42044
diff changeset
   649
fun check_rule rule =
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   650
  (case Ast.rule_error rule of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   651
    SOME msg =>
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   652
      error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
42048
afd11ca8e018 support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents: 42044
diff changeset
   653
        Pretty.string_of (Ast.pretty_rule rule))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   654
  | NONE => rule);
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   655
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   656
in
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   657
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   658
fun check_rules rules =
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   659
 (map check_rule (map_filter parse_rule rules),
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   660
  map check_rule (map_filter print_rule rules));
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   661
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   662
end;
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   663
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   664
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   665
19262
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   666
(** modify syntax **)
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   667
52143
36ffe23b25f8 syntax translations always depend on context;
wenzelm
parents: 51612
diff changeset
   668
val update_trfuns = update_syntax mode_default o Syntax_Ext.syn_ext_trfuns;
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   669
35412
b8dead547d9e more uniform treatment of syntax for types vs. consts;
wenzelm
parents: 35394
diff changeset
   670
fun update_type_gram add prmode decls =
b8dead547d9e more uniform treatment of syntax for types vs. consts;
wenzelm
parents: 35394
diff changeset
   671
  (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_types decls);
25387
d9ab1e3a8acb added update_const_gram (avoids duplicates);
wenzelm
parents: 25122
diff changeset
   672
59841
2551ac44150e tuned signature;
wenzelm
parents: 59795
diff changeset
   673
fun update_const_gram add logical_types prmode decls =
2551ac44150e tuned signature;
wenzelm
parents: 59795
diff changeset
   674
  (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_consts logical_types decls);
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   675
52143
36ffe23b25f8 syntax translations always depend on context;
wenzelm
parents: 51612
diff changeset
   676
val update_trrules = update_syntax mode_default o Syntax_Ext.syn_ext_rules o check_rules;
42288
2074b31650e6 discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents: 42287
diff changeset
   677
val remove_trrules = remove_syntax mode_default o Syntax_Ext.syn_ext_rules o check_rules;
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   678
42476
d0bc1268ef09 clarified auxiliary structure Lexicon.Syntax;
wenzelm
parents: 42408
diff changeset
   679
d0bc1268ef09 clarified auxiliary structure Lexicon.Syntax;
wenzelm
parents: 42408
diff changeset
   680
open Lexicon.Syntax;
d0bc1268ef09 clarified auxiliary structure Lexicon.Syntax;
wenzelm
parents: 42408
diff changeset
   681
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   682
end;
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   683