src/Pure/Syntax/syntax.ML
author wenzelm
Wed, 06 Apr 2011 17:00:40 +0200
changeset 42254 f427c9890c46
parent 42253 c539d3c9c023
child 42255 097c93dcd877
permissions -rw-r--r--
more abstract print translation;
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
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
     8
signature BASIC_SYNTAX =
2383
4127499d9b52 added extend_trfunsT;
wenzelm
parents: 2366
diff changeset
     9
sig
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    10
  include SYN_TRANS0
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    11
  include MIXFIX0
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    12
  include PRINTER0
2383
4127499d9b52 added extend_trfunsT;
wenzelm
parents: 2366
diff changeset
    13
end;
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    14
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
signature SYNTAX =
2383
4127499d9b52 added extend_trfunsT;
wenzelm
parents: 2366
diff changeset
    16
sig
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
  include LEXICON0
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    18
  include SYN_EXT0
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
  include TYPE_EXT0
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    20
  include SYN_TRANS1
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    21
  include MIXFIX1
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
  include PRINTER0
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 30364
diff changeset
    23
  val read_token: string -> Symbol_Pos.T list * Position.T
38238
43c13eb0d842 proper context for Syntax.parse_token;
wenzelm
parents: 38237
diff changeset
    24
  val parse_token: Proof.context -> Markup.T -> string -> Symbol_Pos.T list * Position.T
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    25
  val parse_sort: Proof.context -> string -> sort
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    26
  val parse_typ: Proof.context -> string -> typ
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    27
  val parse_term: Proof.context -> string -> term
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    28
  val parse_prop: Proof.context -> string -> term
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    29
  val unparse_sort: Proof.context -> sort -> Pretty.T
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    30
  val unparse_classrel: Proof.context -> class list -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    31
  val unparse_arity: Proof.context -> arity -> Pretty.T
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    32
  val unparse_typ: Proof.context -> typ -> Pretty.T
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    33
  val unparse_term: Proof.context -> term -> Pretty.T
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    34
  val install_operations:
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    35
   {parse_sort: Proof.context -> string -> sort,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    36
    parse_typ: Proof.context -> string -> typ,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    37
    parse_term: Proof.context -> string -> term,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    38
    parse_prop: Proof.context -> string -> term,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    39
    unparse_sort: Proof.context -> sort -> Pretty.T,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    40
    unparse_typ: Proof.context -> typ -> Pretty.T,
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    41
    unparse_term: Proof.context -> term -> Pretty.T} -> unit
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    42
  val print_checks: Proof.context -> unit
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    43
  val add_typ_check: int -> string ->
25060
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
    44
    (typ list -> Proof.context -> (typ list * Proof.context) option) ->
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    45
    Context.generic -> Context.generic
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    46
  val add_term_check: int -> string ->
25060
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
    47
    (term list -> Proof.context -> (term list * Proof.context) option) ->
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    48
    Context.generic -> Context.generic
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    49
  val add_typ_uncheck: int -> string ->
25060
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
    50
    (typ list -> Proof.context -> (typ list * Proof.context) option) ->
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    51
    Context.generic -> Context.generic
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    52
  val add_term_uncheck: int -> string ->
25060
17c313217998 Syntax.(un)check: explicit result option;
wenzelm
parents: 25043
diff changeset
    53
    (term list -> Proof.context -> (term list * Proof.context) option) ->
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    54
    Context.generic -> Context.generic
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
    55
  val check_sort: Proof.context -> sort -> sort
24512
fc4959967b30 added singleton check_typ/term/prop;
wenzelm
parents: 24488
diff changeset
    56
  val check_typ: Proof.context -> typ -> typ
fc4959967b30 added singleton check_typ/term/prop;
wenzelm
parents: 24488
diff changeset
    57
  val check_term: Proof.context -> term -> term
fc4959967b30 added singleton check_typ/term/prop;
wenzelm
parents: 24488
diff changeset
    58
  val check_prop: Proof.context -> term -> term
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
    59
  val check_typs: Proof.context -> typ list -> typ list
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    60
  val check_terms: Proof.context -> term list -> term list
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    61
  val check_props: Proof.context -> term list -> term list
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    62
  val uncheck_sort: Proof.context -> sort -> sort
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    63
  val uncheck_arity: Proof.context -> arity -> arity
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    64
  val uncheck_classrel: Proof.context -> class list -> class list
24768
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    65
  val uncheck_typs: Proof.context -> typ list -> typ list
123e219b66c2 added unparse interfaces (still unused);
wenzelm
parents: 24709
diff changeset
    66
  val uncheck_terms: Proof.context -> term list -> term list
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    67
  val read_sort: Proof.context -> string -> sort
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    68
  val read_typ: Proof.context -> string -> typ
24488
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
    69
  val read_term: Proof.context -> string -> term
646e782ba8ff turned type_check into separate typ/term_check;
wenzelm
parents: 24372
diff changeset
    70
  val read_prop: Proof.context -> string -> term
24263
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    71
  val read_terms: Proof.context -> string list -> term list
aff00d8b2e32 added generic wrapper for parse/read functions;
wenzelm
parents: 24247
diff changeset
    72
  val read_props: Proof.context -> string list -> term list
24709
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
    73
  val read_sort_global: theory -> string -> sort
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
    74
  val read_typ_global: theory -> string -> typ
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
    75
  val read_term_global: theory -> string -> term
ecfb9dcb6c4c removed redundant global_parse operations;
wenzelm
parents: 24680
diff changeset
    76
  val read_prop_global: theory -> string -> term
24923
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    77
  val pretty_term: Proof.context -> term -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    78
  val pretty_typ: Proof.context -> typ -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    79
  val pretty_sort: Proof.context -> sort -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    80
  val pretty_classrel: Proof.context -> class list -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    81
  val pretty_arity: Proof.context -> arity -> Pretty.T
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    82
  val string_of_term: Proof.context -> term -> string
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    83
  val string_of_typ: Proof.context -> typ -> string
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    84
  val string_of_sort: Proof.context -> sort -> string
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    85
  val string_of_classrel: Proof.context -> class list -> string
9e095546cdac generic Syntax.pretty/string_of operations;
wenzelm
parents: 24768
diff changeset
    86
  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
    87
  val is_pretty_global: Proof.context -> bool
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    88
  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
    89
  val init_pretty_global: theory -> Proof.context
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    90
  val pretty_term_global: theory -> term -> Pretty.T
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    91
  val pretty_typ_global: theory -> typ -> Pretty.T
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    92
  val pretty_sort_global: theory -> sort -> Pretty.T
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    93
  val string_of_term_global: theory -> term -> string
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    94
  val string_of_typ_global: theory -> typ -> string
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    95
  val string_of_sort_global: theory -> sort -> string
24970
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
    96
  val pp: Proof.context -> Pretty.pp
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
    97
  val pp_global: theory -> Pretty.pp
42245
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42242
diff changeset
    98
  val lookup_tokentr:
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42242
diff changeset
    99
    (string * (string * ((Proof.context -> string -> Pretty.T) * stamp)) list) list ->
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42242
diff changeset
   100
      string list -> string -> (Proof.context -> string -> Pretty.T) option
42242
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   101
  type ruletab
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   102
  type syntax
42242
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   103
  val rep_syntax: syntax ->
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   104
   {input: Syn_Ext.xprod list,
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   105
    lexicon: Scan.lexicon,
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   106
    gram: Parser.gram,
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   107
    consts: string list,
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   108
    prmodes: string list,
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   109
    parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table,
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   110
    parse_ruletab: ruletab,
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   111
    parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table,
42247
12fe41a92cd5 typed_print_translation: discontinued show_sorts argument;
wenzelm
parents: 42245
diff changeset
   112
    print_trtab: ((Proof.context -> typ -> term list -> term) * stamp) list Symtab.table,
42242
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   113
    print_ruletab: ruletab,
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   114
    print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   115
    tokentrtab: (string * (string * ((Proof.context -> string -> Pretty.T) * stamp)) list) list,
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   116
    prtabs: Printer.prtabs}
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   117
  val eq_syntax: syntax * syntax -> bool
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   118
  val is_keyword: syntax -> string -> bool
42251
050cc12dd985 explicit Syntax.tokenize, Syntax.parse;
wenzelm
parents: 42247
diff changeset
   119
  val tokenize: syntax -> bool -> Symbol_Pos.T list -> Lexicon.token list
050cc12dd985 explicit Syntax.tokenize, Syntax.parse;
wenzelm
parents: 42247
diff changeset
   120
  val parse: Proof.context -> syntax -> string -> Lexicon.token list -> Parser.parsetree list
42253
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   121
  val parse_ast_translation: syntax -> string -> (Proof.context -> Ast.ast list -> Ast.ast) option
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   122
  val parse_translation: syntax -> string -> (Proof.context -> term list -> term) option
42254
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   123
  val print_translation: syntax -> string ->
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   124
    Proof.context -> typ -> term list -> term  (*exception Match*)
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   125
  val print_ast_translation: syntax -> string ->
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   126
    Proof.context -> Ast.ast list -> Ast.ast  (*exception Match*)
42253
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   127
  val token_translation: syntax -> string list ->
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   128
    string -> (Proof.context -> string -> Pretty.T) option
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   129
  type mode
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   130
  val mode_default: mode
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   131
  val mode_input: mode
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   132
  val merge_syntaxes: syntax -> syntax -> syntax
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   133
  val basic_syntax: syntax
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   134
  val basic_nonterms: string list
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   135
  val print_gram: syntax -> unit
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   136
  val print_trans: syntax -> unit
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   137
  val print_syntax: syntax -> unit
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   138
  val guess_infix: syntax -> string -> mixfix option
42056
160a630b2c7e enable inner syntax source positions by default (controlled via configuration option);
wenzelm
parents: 42048
diff changeset
   139
  val positions_raw: Config.raw
160a630b2c7e enable inner syntax source positions by default (controlled via configuration option);
wenzelm
parents: 42048
diff changeset
   140
  val positions: bool Config.T
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   141
  val ambiguity_enabled: bool Config.T
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
   142
  val ambiguity_level_raw: Config.raw
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   143
  val ambiguity_level: int Config.T
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   144
  val ambiguity_limit: int Config.T
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   145
  datatype 'a trrule =
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   146
    Parse_Rule of 'a * 'a |
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   147
    Print_Rule of 'a * 'a |
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   148
    Parse_Print_Rule of 'a * 'a
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   149
  val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   150
  val is_const: syntax -> string -> bool
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   151
  val update_trfuns:
42224
578a51fae383 discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents: 42223
diff changeset
   152
    (string * ((Ast.ast list -> Ast.ast) * stamp)) list *
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   153
    (string * ((term list -> term) * stamp)) list *
42247
12fe41a92cd5 typed_print_translation: discontinued show_sorts argument;
wenzelm
parents: 42245
diff changeset
   154
    (string * ((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
   155
    (string * ((Ast.ast list -> Ast.ast) * stamp)) list -> syntax -> syntax
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   156
  val update_advanced_trfuns:
42224
578a51fae383 discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents: 42223
diff changeset
   157
    (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list *
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   158
    (string * ((Proof.context -> term list -> term) * stamp)) list *
42247
12fe41a92cd5 typed_print_translation: discontinued show_sorts argument;
wenzelm
parents: 42245
diff changeset
   159
    (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
   160
    (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
   161
  val extend_tokentrfuns: (string * string * (Proof.context -> string -> Pretty.T)) list ->
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   162
    syntax -> syntax
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   163
  val update_type_gram: bool -> mode -> (string * typ * mixfix) list -> syntax -> syntax
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   164
  val update_const_gram: bool -> (string -> bool) ->
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   165
    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
   166
  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
   167
  val remove_trrules: Ast.ast trrule list -> syntax -> syntax
2383
4127499d9b52 added extend_trfunsT;
wenzelm
parents: 2366
diff changeset
   168
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
12094
wenzelm
parents: 12073
diff changeset
   170
structure Syntax: SYNTAX =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   171
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   172
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   173
(** inner syntax operations **)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   174
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   175
(* read token -- with optional YXML encoding of position *)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   176
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   177
fun read_token str =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   178
  let
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   179
    val tree = YXML.parse str handle Fail msg => error msg;
39555
ccb223a4d49c added XML.content_of convenience -- cover XML.body, which is the general situation;
wenzelm
parents: 39510
diff changeset
   180
    val text = XML.content_of [tree];
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   181
    val pos =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   182
      (case tree of
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   183
        XML.Elem ((name, props), _) =>
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   184
          if name = Markup.tokenN then Position.of_properties props
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   185
          else Position.none
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   186
      | XML.Text _ => Position.none);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   187
  in (Symbol_Pos.explode (text, pos), pos) end;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   188
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   189
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   190
(* (un)parsing *)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   191
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   192
fun parse_token ctxt markup str =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   193
  let
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   194
    val (syms, pos) = read_token str;
39507
839873937ddd tuned signature of (Context_)Position.report variants;
wenzelm
parents: 39288
diff changeset
   195
    val _ = Context_Position.report ctxt pos markup;
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   196
  in (syms, pos) end;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   197
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   198
local
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   199
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   200
type operations =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   201
 {parse_sort: Proof.context -> string -> sort,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   202
  parse_typ: Proof.context -> string -> typ,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   203
  parse_term: Proof.context -> string -> term,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   204
  parse_prop: Proof.context -> string -> term,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   205
  unparse_sort: Proof.context -> sort -> Pretty.T,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   206
  unparse_typ: Proof.context -> typ -> Pretty.T,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   207
  unparse_term: Proof.context -> term -> Pretty.T};
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   208
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   209
val operations: operations Single_Assignment.var = Single_Assignment.var "Syntax.operations";
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   210
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   211
fun operation which ctxt x =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   212
  (case Single_Assignment.peek operations of
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   213
    NONE => raise Fail "Inner syntax operations not installed"
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   214
  | SOME ops => which ops ctxt x);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   215
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   216
in
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   217
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   218
val parse_sort = operation #parse_sort;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   219
val parse_typ = operation #parse_typ;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   220
val parse_term = operation #parse_term;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   221
val parse_prop = operation #parse_prop;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   222
val unparse_sort = operation #unparse_sort;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   223
val unparse_typ = operation #unparse_typ;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   224
val unparse_term = operation #unparse_term;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   225
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   226
fun install_operations ops = Single_Assignment.assign operations ops;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   227
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   228
end;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   229
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   230
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   231
(* context-sensitive (un)checking *)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   232
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   233
local
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
type key = int * bool;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   236
type 'a check = 'a list -> Proof.context -> ('a list * Proof.context) option;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   237
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   238
structure Checks = Generic_Data
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   239
(
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   240
  type T =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   241
    ((key * ((string * typ check) * stamp) list) list *
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   242
     (key * ((string * term check) * stamp) list) list);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   243
  val empty = ([], []);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   244
  val extend = I;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   245
  fun merge ((typ_checks1, term_checks1), (typ_checks2, term_checks2)) : T =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   246
    (AList.join (op =) (K (Library.merge (eq_snd (op =)))) (typ_checks1, typ_checks2),
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   247
     AList.join (op =) (K (Library.merge (eq_snd (op =)))) (term_checks1, term_checks2));
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   248
);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   249
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   250
fun gen_add which (key: key) name f =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   251
  Checks.map (which (AList.map_default op = (key, []) (cons ((name, f), stamp ()))));
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   252
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   253
fun check_stage fs = perhaps_loop (perhaps_apply (map uncurry fs));
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   254
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   255
fun gen_check which uncheck ctxt0 xs0 =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   256
  let
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   257
    val funs = which (Checks.get (Context.Proof ctxt0))
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   258
      |> map_filter (fn ((i, u), fs) => if uncheck = u then SOME (i, map (snd o fst) fs) else NONE)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   259
      |> Library.sort (int_ord o pairself fst) |> map snd
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   260
      |> not uncheck ? map rev;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   261
    val check_all = perhaps_apply (map check_stage funs);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   262
  in #1 (perhaps check_all (xs0, ctxt0)) end;
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
fun map_sort f S =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   265
  (case f (TFree ("", S)) of
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   266
    TFree ("", S') => S'
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   267
  | _ => raise TYPE ("map_sort", [TFree ("", S)], []));
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
in
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   270
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   271
fun print_checks ctxt =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   272
  let
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   273
    fun split_checks checks =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   274
      List.partition (fn ((_, un), _) => not un) checks
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   275
      |> pairself (map (fn ((i, _), fs) => (i, map (fst o fst) fs))
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   276
          #> sort (int_ord o pairself fst));
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   277
    fun pretty_checks kind checks =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   278
      checks |> map (fn (i, names) => Pretty.block
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   279
        [Pretty.str (kind ^ " (stage " ^ signed_string_of_int i ^ "):"),
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   280
          Pretty.brk 1, Pretty.strs names]);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   281
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   282
    val (typs, terms) = Checks.get (Context.Proof ctxt);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   283
    val (typ_checks, typ_unchecks) = split_checks typs;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   284
    val (term_checks, term_unchecks) = split_checks terms;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   285
  in
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   286
    pretty_checks "typ_checks" typ_checks @
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   287
    pretty_checks "term_checks" term_checks @
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   288
    pretty_checks "typ_unchecks" typ_unchecks @
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   289
    pretty_checks "term_unchecks" term_unchecks
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   290
  end |> Pretty.chunks |> Pretty.writeln;
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
fun add_typ_check stage = gen_add apfst (stage, false);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   293
fun add_term_check stage = gen_add apsnd (stage, false);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   294
fun add_typ_uncheck stage = gen_add apfst (stage, true);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   295
fun add_term_uncheck stage = gen_add apsnd (stage, true);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   296
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   297
val check_typs = gen_check fst false;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   298
val check_terms = gen_check snd false;
39288
f1ae2493d93f eliminated aliases of Type.constraint;
wenzelm
parents: 39168
diff changeset
   299
fun check_props ctxt = map (Type.constraint propT) #> check_terms ctxt;
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   300
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   301
val check_typ = singleton o check_typs;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   302
val check_term = singleton o check_terms;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   303
val check_prop = singleton o check_props;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   304
val check_sort = map_sort o check_typ;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   305
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   306
val uncheck_typs = gen_check fst true;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   307
val uncheck_terms = gen_check snd true;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   308
val uncheck_sort = map_sort o singleton o uncheck_typs;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   309
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   310
end;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   311
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   312
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   313
(* derived operations for classrel and arity *)
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
val uncheck_classrel = map o singleton o uncheck_sort;
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 unparse_classrel ctxt cs = Pretty.block (flat
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   318
  (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
   319
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   320
fun uncheck_arity ctxt (a, Ss, S) =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   321
  let
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   322
    val T = Type (a, replicate (length Ss) dummyT);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   323
    val a' =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   324
      (case singleton (uncheck_typs ctxt) T of
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   325
        Type (a', _) => a'
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   326
      | T => raise TYPE ("uncheck_arity", [T], []));
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   327
    val Ss' = map (uncheck_sort ctxt) Ss;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   328
    val S' = uncheck_sort ctxt S;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   329
  in (a', Ss', S') end;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   330
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   331
fun unparse_arity ctxt (a, Ss, S) =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   332
  let
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   333
    val prtT = unparse_typ ctxt (Type (a, []));
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   334
    val dom =
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   335
      if null Ss then []
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   336
      else [Pretty.list "(" ")" (map (unparse_sort ctxt) Ss), Pretty.brk 1];
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   337
  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
   338
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   339
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   340
(* read = parse + check *)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   341
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   342
fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   343
fun read_typ ctxt = parse_typ ctxt #> singleton (check_typs ctxt);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   344
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   345
fun read_terms ctxt = map (parse_term ctxt) #> check_terms ctxt;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   346
fun read_props ctxt = map (parse_prop ctxt) #> check_props ctxt;
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
val read_term = singleton o read_terms;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   349
val read_prop = singleton o read_props;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   350
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   351
val read_sort_global = read_sort o ProofContext.init_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   352
val read_typ_global = read_typ o ProofContext.init_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   353
val read_term_global = read_term o ProofContext.init_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   354
val read_prop_global = read_prop o ProofContext.init_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   355
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   356
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   357
(* pretty = uncheck + unparse *)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   358
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   359
fun pretty_term ctxt = singleton (uncheck_terms ctxt) #> unparse_term ctxt;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   360
fun pretty_typ ctxt = singleton (uncheck_typs ctxt) #> unparse_typ ctxt;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   361
fun pretty_sort ctxt = uncheck_sort ctxt #> unparse_sort ctxt;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   362
fun pretty_classrel ctxt = uncheck_classrel ctxt #> unparse_classrel ctxt;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   363
fun pretty_arity ctxt = uncheck_arity ctxt #> unparse_arity ctxt;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   364
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   365
val string_of_term = Pretty.string_of oo pretty_term;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   366
val string_of_typ = Pretty.string_of oo pretty_typ;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   367
val string_of_sort = Pretty.string_of oo pretty_sort;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   368
val string_of_classrel = Pretty.string_of oo pretty_classrel;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   369
val string_of_arity = Pretty.string_of oo pretty_arity;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   370
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   371
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   372
(* global pretty printing *)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   373
39508
dfacdb01e1ec simplified some internal flags using Config.T instead of full-blown Proof_Data;
wenzelm
parents: 39507
diff changeset
   374
val pretty_global = Config.bool (Config.declare "Syntax.pretty_global" (K (Config.Bool false)));
dfacdb01e1ec simplified some internal flags using Config.T instead of full-blown Proof_Data;
wenzelm
parents: 39507
diff changeset
   375
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
   376
val set_pretty_global = Config.put pretty_global;
39135
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   377
val init_pretty_global = set_pretty_global true o ProofContext.init_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   378
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   379
val pretty_term_global = pretty_term o init_pretty_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   380
val pretty_typ_global = pretty_typ o init_pretty_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   381
val pretty_sort_global = pretty_sort o init_pretty_global;
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   382
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   383
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
   384
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
   385
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
   386
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   387
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   388
(* pp operations -- deferred evaluation *)
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   389
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   390
fun pp ctxt = Pretty.pp
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   391
 (fn x => pretty_term ctxt x,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   392
  fn x => pretty_typ ctxt x,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   393
  fn x => pretty_sort ctxt x,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   394
  fn x => pretty_classrel ctxt x,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   395
  fn x => pretty_arity ctxt x);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   396
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   397
fun pp_global thy = Pretty.pp
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   398
 (fn x => pretty_term_global thy x,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   399
  fn x => pretty_typ_global thy x,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   400
  fn x => pretty_sort_global thy x,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   401
  fn x => pretty_classrel (init_pretty_global thy) x,
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   402
  fn x => pretty_arity (init_pretty_global thy) x);
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   403
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   404
6f5f64542405 structure Syntax: define "interfaces" before actual implementations;
wenzelm
parents: 39126
diff changeset
   405
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   406
(** tables of translation functions **)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   407
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   408
(* parse (ast) translations *)
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   409
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23631
diff changeset
   410
fun err_dup_trfun name c =
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23631
diff changeset
   411
  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
   412
42253
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   413
fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c);
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   414
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   415
fun remove_trtab trfuns = fold (Symtab.remove Syn_Ext.eq_trfun) trfuns;
21536
f119c730f509 extend_trtab: allow identical trfuns to be overwritten;
wenzelm
parents: 20784
diff changeset
   416
29004
a5a91f387791 removed Table.extend, NameSpace.extend_table
haftmann
parents: 28904
diff changeset
   417
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
   418
  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
   419
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   420
fun merge_trtabs name tab1 tab2 = Symtab.merge Syn_Ext.eq_trfun (tab1, tab2)
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 23631
diff changeset
   421
  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
   422
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   423
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   424
(* print (ast) translations *)
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   425
42254
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   426
fun apply_tr' tab c ctxt T args =
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   427
  let
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   428
    val fns = map fst (Symtab.lookup_list tab c);
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   429
    fun app_first [] = raise Match
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   430
      | app_first (f :: fs) = f ctxt T args handle Match => app_first fs;
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   431
  in app_first fns end;
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   432
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   433
fun apply_ast_tr' tab c ctxt args =
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   434
  let
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   435
    val fns = map fst (Symtab.lookup_list tab c);
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   436
    fun app_first [] = raise Match
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   437
      | app_first (f :: fs) = f ctxt args handle Match => app_first fs;
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   438
  in app_first fns end;
42253
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   439
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   440
fun update_tr'tab trfuns = fold_rev (Symtab.update_list Syn_Ext.eq_trfun) trfuns;
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   441
fun remove_tr'tab trfuns = fold (Symtab.remove_list Syn_Ext.eq_trfun) trfuns;
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   442
fun merge_tr'tabs tab1 tab2 = Symtab.merge_list Syn_Ext.eq_trfun (tab1, tab2);
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   443
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   444
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   445
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   446
(** tables of token translation functions **)
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   447
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   448
fun lookup_tokentr tabs modes =
42245
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42242
diff changeset
   449
  let val trs =
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42242
diff changeset
   450
    distinct (eq_fst (op = : string * string -> bool))
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42242
diff changeset
   451
      (maps (these o AList.lookup (op =) tabs) (modes @ [""]))
17314
04e21a27c0ad introduces some modern-style AList operations
haftmann
parents: 17221
diff changeset
   452
  in fn c => Option.map fst (AList.lookup (op =) trs c) end;
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   453
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   454
fun merge_tokentrtabs tabs1 tabs2 =
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   455
  let
42245
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42242
diff changeset
   456
    fun eq_tr ((c1, (_, s1: stamp)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   457
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4618
diff changeset
   458
    fun name (s, _) = implode (tl (Symbol.explode s));
2706
91a640a91c6e improved err msg;
wenzelm
parents: 2700
diff changeset
   459
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   460
    fun merge mode =
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   461
      let
17213
ba65f3e5653c fixed ins_tokentr: AList.default;
wenzelm
parents: 17192
diff changeset
   462
        val trs1 = these (AList.lookup (op =) tabs1 mode);
ba65f3e5653c fixed ins_tokentr: AList.default;
wenzelm
parents: 17192
diff changeset
   463
        val trs2 = these (AList.lookup (op =) tabs2 mode);
19046
bc5c6c9b114e removed distinct, renamed gen_distinct to distinct;
wenzelm
parents: 18977
diff changeset
   464
        val trs = distinct eq_tr (trs1 @ trs2);
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   465
      in
18964
67f572e03236 renamed gen_duplicates to duplicates;
wenzelm
parents: 18931
diff changeset
   466
        (case duplicates (eq_fst (op =)) trs of
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   467
          [] => (mode, trs)
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   468
        | dups => error ("More than one token translation function in mode " ^
2706
91a640a91c6e improved err msg;
wenzelm
parents: 2700
diff changeset
   469
            quote mode ^ " for " ^ commas_quote (map name dups)))
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   470
      end;
19046
bc5c6c9b114e removed distinct, renamed gen_distinct to distinct;
wenzelm
parents: 18977
diff changeset
   471
  in map merge (distinct (op =) (map fst (tabs1 @ tabs2))) end;
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   472
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   473
fun extend_tokentrtab tokentrs tabs =
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   474
  let
17213
ba65f3e5653c fixed ins_tokentr: AList.default;
wenzelm
parents: 17192
diff changeset
   475
    fun ins_tokentr (m, c, f) =
ba65f3e5653c fixed ins_tokentr: AList.default;
wenzelm
parents: 17192
diff changeset
   476
      AList.default (op =) (m, [])
ba65f3e5653c fixed ins_tokentr: AList.default;
wenzelm
parents: 17192
diff changeset
   477
      #> AList.map_entry (op =) m (cons ("_" ^ c, (f, stamp ())));
15759
wenzelm
parents: 15755
diff changeset
   478
  in merge_tokentrtabs tabs (fold ins_tokentr tokentrs []) end;
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   479
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   480
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   481
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   482
(** tables of translation rules **)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   483
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   484
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
   485
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19375
diff changeset
   486
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
   487
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   488
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   489
(* empty, update, merge ruletabs *)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   490
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   491
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
   492
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
   493
fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   494
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   495
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   496
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   497
(** datatype syntax **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   498
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   499
datatype syntax =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   500
  Syntax of {
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   501
    input: Syn_Ext.xprod list,
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4618
diff changeset
   502
    lexicon: Scan.lexicon,
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   503
    gram: Parser.gram,
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   504
    consts: string list,
2913
ce271fa4d8e2 fixed diagnostic output of print modes;
wenzelm
parents: 2706
diff changeset
   505
    prmodes: string list,
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   506
    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
   507
    parse_ruletab: ruletab,
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   508
    parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table,
42247
12fe41a92cd5 typed_print_translation: discontinued show_sorts argument;
wenzelm
parents: 42245
diff changeset
   509
    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
   510
    print_ruletab: ruletab,
21772
7c7ade4f537b advanced translation functions: Proof.context;
wenzelm
parents: 21731
diff changeset
   511
    print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
26704
51ee753cc2e3 token translations: context dependent, result Pretty.T;
wenzelm
parents: 26684
diff changeset
   512
    tokentrtab: (string * (string * ((Proof.context -> string -> Pretty.T) * stamp)) list) list,
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   513
    prtabs: Printer.prtabs} * stamp;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   514
42242
39261908e12f moved decode/parse operations to standard_syntax.ML;
wenzelm
parents: 42225
diff changeset
   515
fun rep_syntax (Syntax (tabs, _)) = tabs;
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   516
fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2;
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   517
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   518
fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode;
42251
050cc12dd985 explicit Syntax.tokenize, Syntax.parse;
wenzelm
parents: 42247
diff changeset
   519
fun tokenize (Syntax ({lexicon, ...}, _)) = Lexicon.tokenize lexicon;
050cc12dd985 explicit Syntax.tokenize, Syntax.parse;
wenzelm
parents: 42247
diff changeset
   520
fun parse ctxt (Syntax ({gram, ...}, _)) = Parser.parse ctxt gram;
14687
e089757b952a added is_keyword;
wenzelm
parents: 14648
diff changeset
   521
42253
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   522
fun parse_ast_translation (Syntax ({parse_ast_trtab, ...}, _)) = lookup_tr parse_ast_trtab;
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   523
fun parse_translation (Syntax ({parse_trtab, ...}, _)) = lookup_tr parse_trtab;
42254
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   524
fun print_translation (Syntax ({print_trtab, ...}, _)) = apply_tr' print_trtab;
f427c9890c46 more abstract print translation;
wenzelm
parents: 42253
diff changeset
   525
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
   526
fun token_translation (Syntax ({tokentrtab, ...}, _)) = lookup_tokentr tokentrtab;
c539d3c9c023 more abstract syntax translation;
wenzelm
parents: 42251
diff changeset
   527
20784
eece9aaaf352 Syntax.mode;
wenzelm
parents: 20664
diff changeset
   528
type mode = string * bool;
24970
050afeec89a7 renamed Syntax.XXX_mode to Syntax.mode_XXX;
wenzelm
parents: 24923
diff changeset
   529
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
   530
val mode_input = (Print_Mode.input, true);
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   531
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   532
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   533
(* empty_syntax *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   534
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   535
val empty_syntax = Syntax
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   536
  ({input = [],
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4618
diff changeset
   537
    lexicon = Scan.empty_lexicon,
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   538
    gram = Parser.empty_gram,
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   539
    consts = [],
2913
ce271fa4d8e2 fixed diagnostic output of print modes;
wenzelm
parents: 2706
diff changeset
   540
    prmodes = [],
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   541
    parse_ast_trtab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   542
    parse_ruletab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   543
    parse_trtab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   544
    print_trtab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   545
    print_ruletab = Symtab.empty,
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   546
    print_ast_trtab = Symtab.empty,
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   547
    tokentrtab = [],
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   548
    prtabs = Printer.empty_prtabs}, stamp ());
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   549
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   550
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   551
(* update_syntax *)
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   552
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   553
fun update_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   554
  let
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   555
    val {input, lexicon, gram, consts = consts1, prmodes = prmodes1,
2366
a163d2be1bb5 added chartrans;
wenzelm
parents: 2287
diff changeset
   556
      parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   557
      print_ast_trtab, tokentrtab, prtabs} = tabs;
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   558
    val Syn_Ext.Syn_Ext {xprods, consts = consts2, prmodes = prmodes2,
2913
ce271fa4d8e2 fixed diagnostic output of print modes;
wenzelm
parents: 2706
diff changeset
   559
      parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   560
      print_ast_translation, token_translation} = syn_ext;
36208
74c5e6e3c1d3 update_syntax: add new productions only once, to allow repeated local notation, for example;
wenzelm
parents: 35668
diff changeset
   561
    val new_xprods =
74c5e6e3c1d3 update_syntax: add new productions only once, to allow repeated local notation, for example;
wenzelm
parents: 35668
diff changeset
   562
      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
   563
    fun if_inout xs = if inout then xs else [];
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   564
  in
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   565
    Syntax
36208
74c5e6e3c1d3 update_syntax: add new productions only once, to allow repeated local notation, for example;
wenzelm
parents: 35668
diff changeset
   566
    ({input = new_xprods @ input,
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   567
      lexicon = fold Scan.extend_lexicon (Syn_Ext.delims_of new_xprods) lexicon,
37684
d123b1e08856 standard argument order;
wenzelm
parents: 37216
diff changeset
   568
      gram = Parser.extend_gram new_xprods gram,
35429
afa8cf9e63d8 authentic syntax for classes and type constructors;
wenzelm
parents: 35412
diff changeset
   569
      consts = Library.merge (op =) (consts1, filter_out Lexicon.is_marked consts2),
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18857
diff changeset
   570
      prmodes = insert (op =) mode (Library.merge (op =) (prmodes1, prmodes2)),
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   571
      parse_ast_trtab =
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   572
        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
   573
      parse_ruletab = update_ruletab (if_inout parse_rules) parse_ruletab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   574
      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
   575
      print_trtab = update_tr'tab print_translation print_trtab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   576
      print_ruletab = update_ruletab print_rules print_ruletab,
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   577
      print_ast_trtab = update_tr'tab print_ast_translation print_ast_trtab,
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   578
      tokentrtab = extend_tokentrtab token_translation tokentrtab,
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   579
      prtabs = Printer.update_prtabs mode xprods prtabs}, stamp ())
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   580
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   581
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   582
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   583
(* remove_syntax *)
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   584
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   585
fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   586
  let
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   587
    val Syn_Ext.Syn_Ext {xprods, consts = _, prmodes = _,
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   588
      parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   589
      print_ast_translation, token_translation = _} = syn_ext;
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   590
    val {input, lexicon, gram, consts, prmodes,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   591
      parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   592
      print_ast_trtab, tokentrtab, prtabs} = tabs;
19300
7689f81f8996 subtract (op =);
wenzelm
parents: 19262
diff changeset
   593
    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
   594
    val changed = length input <> length input';
19546
00d5c7c7ce07 extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents: 19482
diff changeset
   595
    fun if_inout xs = if inout then xs else [];
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   596
  in
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   597
    Syntax
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   598
    ({input = input',
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   599
      lexicon = if changed then Scan.make_lexicon (Syn_Ext.delims_of input') else lexicon,
42217
1a2a53d03c31 misc tuning and clarification;
wenzelm
parents: 42205
diff changeset
   600
      gram = if changed then Parser.extend_gram input' Parser.empty_gram else gram,
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   601
      consts = consts,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   602
      prmodes = prmodes,
19546
00d5c7c7ce07 extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents: 19482
diff changeset
   603
      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
   604
      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
   605
      parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab,
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   606
      print_trtab = remove_tr'tab print_translation print_trtab,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   607
      print_ruletab = remove_ruletab print_rules print_ruletab,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   608
      print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   609
      tokentrtab = tokentrtab,
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   610
      prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ())
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   611
  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
   612
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   613
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   614
(* merge_syntaxes *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   615
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   616
fun merge_syntaxes (Syntax (tabs1, _)) (Syntax (tabs2, _)) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   617
  let
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   618
    val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   619
      prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   620
      parse_ruletab = parse_ruletab1, parse_trtab = parse_trtab1,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   621
      print_trtab = print_trtab1, print_ruletab = print_ruletab1,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   622
      print_ast_trtab = print_ast_trtab1, tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   623
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   624
    val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   625
      prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   626
      parse_ruletab = parse_ruletab2, parse_trtab = parse_trtab2,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   627
      print_trtab = print_trtab2, print_ruletab = print_ruletab2,
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   628
      print_ast_trtab = print_ast_trtab2, tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   629
  in
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   630
    Syntax
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18857
diff changeset
   631
    ({input = Library.merge (op =) (input1, input2),
27768
398c64b2acef adapted Scan.extend_lexicon/merge_lexicons;
wenzelm
parents: 27265
diff changeset
   632
      lexicon = Scan.merge_lexicons (lexicon1, lexicon2),
37684
d123b1e08856 standard argument order;
wenzelm
parents: 37216
diff changeset
   633
      gram = Parser.merge_gram (gram1, gram2),
18428
4059413acbc1 sort_distinct;
wenzelm
parents: 17496
diff changeset
   634
      consts = sort_distinct string_ord (consts1 @ consts2),
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18857
diff changeset
   635
      prmodes = Library.merge (op =) (prmodes1, prmodes2),
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   636
      parse_ast_trtab =
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   637
        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
   638
      parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   639
      parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   640
      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
   641
      print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   642
      print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
2700
80e6b48c5204 added token translation support;
wenzelm
parents: 2585
diff changeset
   643
      tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2,
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   644
      prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ())
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   645
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   646
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   647
18720
dd1ebba12151 added basic syntax;
wenzelm
parents: 18678
diff changeset
   648
(* basic syntax *)
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   649
35668
69e1740fbfb1 simplified Syntax.basic_syntax (again);
wenzelm
parents: 35429
diff changeset
   650
val basic_syntax =
17168
e7951b191048 removed obsolete type_syn;
wenzelm
parents: 17079
diff changeset
   651
  empty_syntax
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   652
  |> update_syntax mode_default Type_Ext.type_ext
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   653
  |> update_syntax mode_default Syn_Ext.pure_ext;
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   654
18720
dd1ebba12151 added basic syntax;
wenzelm
parents: 18678
diff changeset
   655
val basic_nonterms =
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   656
  (Lexicon.terminals @ [Syn_Ext.logic, "type", "types", "sort", "classes",
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   657
    Syn_Ext.args, Syn_Ext.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   658
    "asms", Syn_Ext.any, Syn_Ext.sprop, "num_const", "float_const", "index",
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   659
    "struct", "id_position", "longid_position", "type_name", "class_name"]);
18720
dd1ebba12151 added basic syntax;
wenzelm
parents: 18678
diff changeset
   660
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   661
4887
bbc13af86c16 added trfun_names;
wenzelm
parents: 4703
diff changeset
   662
15759
wenzelm
parents: 15755
diff changeset
   663
(** print syntax **)
wenzelm
parents: 15755
diff changeset
   664
wenzelm
parents: 15755
diff changeset
   665
local
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   666
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   667
fun pretty_strs_qs name strs =
28840
wenzelm
parents: 28413
diff changeset
   668
  Pretty.strs (name :: map quote (sort_strings strs));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   669
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   670
fun pretty_gram (Syntax (tabs, _)) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   671
  let
32784
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 32738
diff changeset
   672
    val {lexicon, prmodes, gram, ...} = tabs;
28375
c879d88d038a eliminated polymorphic equality;
wenzelm
parents: 27889
diff changeset
   673
    val prmodes' = sort_strings (filter_out (fn s => s = "") prmodes);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   674
  in
8720
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   675
    [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon),
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   676
      Pretty.big_list "prods:" (Parser.pretty_gram gram),
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   677
      pretty_strs_qs "print modes:" prmodes']
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   678
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   679
17079
ce9663987126 added eq_syntax;
wenzelm
parents: 16716
diff changeset
   680
fun pretty_trans (Syntax (tabs, _)) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   681
  let
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   682
    fun pretty_trtab name tab =
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   683
      pretty_strs_qs name (Symtab.keys tab);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   684
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   685
    fun pretty_ruletab name tab =
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   686
      Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   687
28840
wenzelm
parents: 28413
diff changeset
   688
    fun pretty_tokentr (mode, trs) = Pretty.strs (quote mode ^ ":" :: map fst trs);
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4618
diff changeset
   689
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   690
    val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4618
diff changeset
   691
      print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   692
  in
8720
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   693
    [pretty_strs_qs "consts:" consts,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   694
      pretty_trtab "parse_ast_translation:" parse_ast_trtab,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   695
      pretty_ruletab "parse_rules:" parse_ruletab,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   696
      pretty_trtab "parse_translation:" parse_trtab,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   697
      pretty_trtab "print_translation:" print_trtab,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   698
      pretty_ruletab "print_rules:" print_ruletab,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   699
      pretty_trtab "print_ast_translation:" print_ast_trtab,
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 7944
diff changeset
   700
      Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab)]
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   701
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   702
15759
wenzelm
parents: 15755
diff changeset
   703
in
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   704
15759
wenzelm
parents: 15755
diff changeset
   705
fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn));
wenzelm
parents: 15755
diff changeset
   706
fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn));
wenzelm
parents: 15755
diff changeset
   707
fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   708
15759
wenzelm
parents: 15755
diff changeset
   709
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   710
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   711
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   712
(* reconstructing infixes -- educated guessing *)
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   713
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   714
fun guess_infix (Syntax ({gram, ...}, _)) c =
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   715
  (case Parser.guess_infix_lr gram c of
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   716
    SOME (s, l, r, j) => SOME
35130
0991c84e8dcf renamed InfixName to Infix etc.;
wenzelm
parents: 35111
diff changeset
   717
     (if l then Mixfix.Infixl (s, j)
0991c84e8dcf renamed InfixName to Infix etc.;
wenzelm
parents: 35111
diff changeset
   718
      else if r then Mixfix.Infixr (s, j)
0991c84e8dcf renamed InfixName to Infix etc.;
wenzelm
parents: 35111
diff changeset
   719
      else Mixfix.Infix (s, j))
26951
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   720
  | NONE => NONE);
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   721
030e4a818b39 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26704
diff changeset
   722
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   723
42223
098c86e53153 more precise propagation of reports/results through some inner syntax layers;
wenzelm
parents: 42217
diff changeset
   724
(** read **)  (* FIXME rename!? *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   725
42223
098c86e53153 more precise propagation of reports/results through some inner syntax layers;
wenzelm
parents: 42217
diff changeset
   726
(* configuration options *)
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   727
42056
160a630b2c7e enable inner syntax source positions by default (controlled via configuration option);
wenzelm
parents: 42048
diff changeset
   728
val positions_raw = Config.declare "syntax_positions" (fn _ => Config.Bool true);
160a630b2c7e enable inner syntax source positions by default (controlled via configuration option);
wenzelm
parents: 42048
diff changeset
   729
val positions = Config.bool positions_raw;
160a630b2c7e enable inner syntax source positions by default (controlled via configuration option);
wenzelm
parents: 42048
diff changeset
   730
39126
ee117c5b3b75 configuration options Syntax.ambiguity_enabled (inverse of former Syntax.ambiguity_is_error), Syntax.ambiguity_level (with Isar attribute "syntax_ambiguity_level"), Syntax.ambiguity_limit;
wenzelm
parents: 38831
diff changeset
   731
val ambiguity_enabled =
ee117c5b3b75 configuration options Syntax.ambiguity_enabled (inverse of former Syntax.ambiguity_is_error), Syntax.ambiguity_level (with Isar attribute "syntax_ambiguity_level"), Syntax.ambiguity_limit;
wenzelm
parents: 38831
diff changeset
   732
  Config.bool (Config.declare "syntax_ambiguity_enabled" (fn _ => Config.Bool true));
ee117c5b3b75 configuration options Syntax.ambiguity_enabled (inverse of former Syntax.ambiguity_is_error), Syntax.ambiguity_level (with Isar attribute "syntax_ambiguity_level"), Syntax.ambiguity_limit;
wenzelm
parents: 38831
diff changeset
   733
39163
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
   734
val ambiguity_level_raw = Config.declare "syntax_ambiguity_level" (fn _ => Config.Int 1);
4d701c0388c3 more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents: 39137
diff changeset
   735
val ambiguity_level = Config.int ambiguity_level_raw;
39126
ee117c5b3b75 configuration options Syntax.ambiguity_enabled (inverse of former Syntax.ambiguity_is_error), Syntax.ambiguity_level (with Isar attribute "syntax_ambiguity_level"), Syntax.ambiguity_limit;
wenzelm
parents: 38831
diff changeset
   736
ee117c5b3b75 configuration options Syntax.ambiguity_enabled (inverse of former Syntax.ambiguity_is_error), Syntax.ambiguity_level (with Isar attribute "syntax_ambiguity_level"), Syntax.ambiguity_limit;
wenzelm
parents: 38831
diff changeset
   737
val ambiguity_limit =
ee117c5b3b75 configuration options Syntax.ambiguity_enabled (inverse of former Syntax.ambiguity_is_error), Syntax.ambiguity_level (with Isar attribute "syntax_ambiguity_level"), Syntax.ambiguity_limit;
wenzelm
parents: 38831
diff changeset
   738
  Config.int (Config.declare "syntax_ambiguity_limit" (fn _ => Config.Int 10));
882
b118d1ea0dfd moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents: 865
diff changeset
   739
8894
0281bde335ca added read_sort;
wenzelm
parents: 8720
diff changeset
   740
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   741
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   742
(** prepare translation rules **)
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   743
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   744
(* rules *)
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   745
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   746
datatype 'a trrule =
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   747
  Parse_Rule of 'a * 'a |
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   748
  Print_Rule of 'a * 'a |
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   749
  Parse_Print_Rule of 'a * 'a;
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   750
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   751
fun map_trrule f (Parse_Rule (x, y)) = Parse_Rule (f x, f y)
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   752
  | map_trrule f (Print_Rule (x, y)) = Print_Rule (f x, f y)
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   753
  | 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
   754
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   755
fun parse_rule (Parse_Rule pats) = SOME pats
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   756
  | parse_rule (Print_Rule _) = NONE
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   757
  | parse_rule (Parse_Print_Rule pats) = SOME pats;
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   758
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   759
fun print_rule (Parse_Rule _) = NONE
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   760
  | print_rule (Print_Rule pats) = SOME (swap pats)
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   761
  | print_rule (Parse_Print_Rule pats) = SOME (swap pats);
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   762
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   763
35111
18cd034922ba added ML antiquotation @{syntax_const};
wenzelm
parents: 33957
diff changeset
   764
fun is_const (Syntax ({consts, ...}, _)) c = member (op =) consts c;
18cd034922ba added ML antiquotation @{syntax_const};
wenzelm
parents: 33957
diff changeset
   765
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   766
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   767
(* check_rules *)
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   768
19262
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   769
local
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   770
42048
afd11ca8e018 support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents: 42044
diff changeset
   771
fun check_rule rule =
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   772
  (case Ast.rule_error rule of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   773
    SOME msg =>
1158
96804ce95516 added extend_trrules_i;
wenzelm
parents: 1147
diff changeset
   774
      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
   775
        Pretty.string_of (Ast.pretty_rule rule))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   776
  | NONE => rule);
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   777
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   778
in
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   779
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   780
fun check_rules rules =
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   781
 (map check_rule (map_filter parse_rule rules),
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   782
  map check_rule (map_filter print_rule rules));
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   783
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   784
end;
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   785
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   786
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   787
19262
b98b48496819 added remove_trrules(_i);
wenzelm
parents: 19046
diff changeset
   788
(** modify syntax **)
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   789
25394
db25c98f32e1 syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents: 25387
diff changeset
   790
fun ext_syntax f decls = update_syntax mode_default (f decls);
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   791
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   792
val update_trfuns = ext_syntax Syn_Ext.syn_ext_trfuns;
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   793
val update_advanced_trfuns = ext_syntax Syn_Ext.syn_ext_advanced_trfuns;
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   794
val extend_tokentrfuns = ext_syntax Syn_Ext.syn_ext_tokentrfuns;
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   795
35412
b8dead547d9e more uniform treatment of syntax for types vs. consts;
wenzelm
parents: 35394
diff changeset
   796
fun update_type_gram add prmode decls =
b8dead547d9e more uniform treatment of syntax for types vs. consts;
wenzelm
parents: 35394
diff changeset
   797
  (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
   798
35412
b8dead547d9e more uniform treatment of syntax for types vs. consts;
wenzelm
parents: 35394
diff changeset
   799
fun update_const_gram add is_logtype prmode decls =
b8dead547d9e more uniform treatment of syntax for types vs. consts;
wenzelm
parents: 35394
diff changeset
   800
  (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_consts is_logtype decls);
15755
50ac97ebe7d8 expect translations functions to be stamped already;
wenzelm
parents: 15574
diff changeset
   801
42204
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   802
val update_trrules = ext_syntax Syn_Ext.syn_ext_rules o check_rules;
b3277168c1e7 added Position.reports convenience;
wenzelm
parents: 42134
diff changeset
   803
val remove_trrules = remove_syntax mode_default o Syn_Ext.syn_ext_rules o check_rules;
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   804
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   805
15833
78109c7012ed removed token_trans.ML (some content moved to syn_ext.ML);
wenzelm
parents: 15759
diff changeset
   806
(*export parts of internal Syntax structures*)
42251
050cc12dd985 explicit Syntax.tokenize, Syntax.parse;
wenzelm
parents: 42247
diff changeset
   807
val syntax_tokenize = tokenize;
42225
cf48af306290 discontinued special treatment of structure Parser -- directly accessible;
wenzelm
parents: 42224
diff changeset
   808
open Lexicon Syn_Ext Type_Ext Syn_Trans Mixfix Printer;
42251
050cc12dd985 explicit Syntax.tokenize, Syntax.parse;
wenzelm
parents: 42247
diff changeset
   809
val tokenize = syntax_tokenize;
2366
a163d2be1bb5 added chartrans;
wenzelm
parents: 2287
diff changeset
   810
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   811
end;
5692
2e873c5f0e2c no open;
wenzelm
parents: 4887
diff changeset
   812
35130
0991c84e8dcf renamed InfixName to Infix etc.;
wenzelm
parents: 35111
diff changeset
   813
structure Basic_Syntax: BASIC_SYNTAX = Syntax;
0991c84e8dcf renamed InfixName to Infix etc.;
wenzelm
parents: 35111
diff changeset
   814
open Basic_Syntax;
23923
8c10f3515633 hide internal structures (again);
wenzelm
parents: 23660
diff changeset
   815
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   816
forget_structure "Syn_Ext";
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37146
diff changeset
   817
forget_structure "Type_Ext";
26684
wenzelm
parents: 26678
diff changeset
   818
forget_structure "Mixfix";
42245
29e3967550d5 moved unparse material to syntax_phases.ML;
wenzelm
parents: 42242
diff changeset
   819