src/Pure/Isar/outer_parse.ML
author wenzelm
Fri Jan 13 01:13:08 2006 +0100 (2006-01-13)
changeset 18669 cd6d6baf0411
parent 18618 387d170e4aa9
child 18898 e3d2aa8ba0e1
permissions -rw-r--r--
uniform handling of fixes;
mixfix: added Structure;
wenzelm@5826
     1
(*  Title:      Pure/Isar/outer_parse.ML
wenzelm@5826
     2
    ID:         $Id$
wenzelm@5826
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5826
     4
wenzelm@5826
     5
Generic parsers for Isabelle/Isar outer syntax.
wenzelm@5826
     6
*)
wenzelm@5826
     7
wenzelm@5826
     8
signature OUTER_PARSE =
wenzelm@5826
     9
sig
wenzelm@5826
    10
  type token
wenzelm@5826
    11
  val group: string -> (token list -> 'a) -> token list -> 'a
wenzelm@5826
    12
  val !!! : (token list -> 'a) -> token list -> 'a
wenzelm@8581
    13
  val !!!! : (token list -> 'a) -> token list -> 'a
wenzelm@12047
    14
  val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
wenzelm@12047
    15
  val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
wenzelm@12047
    16
  val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b
wenzelm@15703
    17
  val not_eof: token list -> token * token list
wenzelm@5826
    18
  val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
wenzelm@7026
    19
  val command: token list -> string * token list
wenzelm@5826
    20
  val keyword: token list -> string * token list
wenzelm@5826
    21
  val short_ident: token list -> string * token list
wenzelm@5826
    22
  val long_ident: token list -> string * token list
wenzelm@5826
    23
  val sym_ident: token list -> string * token list
kleing@15963
    24
  val minus: token list -> string * token list
wenzelm@5826
    25
  val term_var: token list -> string * token list
wenzelm@5826
    26
  val type_ident: token list -> string * token list
wenzelm@5826
    27
  val type_var: token list -> string * token list
wenzelm@5826
    28
  val number: token list -> string * token list
wenzelm@5826
    29
  val string: token list -> string * token list
wenzelm@5826
    30
  val verbatim: token list -> string * token list
wenzelm@6860
    31
  val sync: token list -> string * token list
wenzelm@5826
    32
  val eof: token list -> string * token list
wenzelm@15703
    33
  val $$$ : string -> token list -> string * token list
wenzelm@16030
    34
  val reserved : string -> token list -> string * token list
wenzelm@15703
    35
  val semicolon: token list -> string * token list
wenzelm@15703
    36
  val underscore: token list -> string * token list
wenzelm@15703
    37
  val maybe: (token list -> 'a * token list) -> token list -> 'a option * token list
wenzelm@17070
    38
  val tag_name: token list -> string * token list
wenzelm@17070
    39
  val tags: token list -> string list * token list
wenzelm@7930
    40
  val opt_unit: token list -> unit * token list
wenzelm@14646
    41
  val opt_keyword: string -> token list -> bool * token list
wenzelm@5826
    42
  val nat: token list -> int * token list
wenzelm@5826
    43
  val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    44
  val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    45
  val list: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    46
  val list1: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@6013
    47
  val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@6013
    48
  val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    49
  val name: token list -> bstring * token list
wenzelm@5826
    50
  val xname: token list -> xstring * token list
wenzelm@5826
    51
  val text: token list -> string * token list
wenzelm@14949
    52
  val path: token list -> Path.T * token list
wenzelm@8897
    53
  val sort: token list -> string * token list
wenzelm@8897
    54
  val arity: token list -> (string list * string) * token list
wenzelm@5826
    55
  val type_args: token list -> string list * token list
wenzelm@5826
    56
  val typ: token list -> string * token list
wenzelm@18669
    57
  val mixfix: token list -> mixfix * token list
wenzelm@18669
    58
  val opt_infix: token list -> mixfix * token list
wenzelm@18669
    59
  val opt_mixfix: token list -> mixfix * token list
wenzelm@18669
    60
  val opt_infix': token list -> mixfix * token list
wenzelm@18669
    61
  val opt_mixfix': token list -> mixfix * token list
wenzelm@18669
    62
  val const: token list -> (string * string * mixfix) * token list
wenzelm@18669
    63
  val fixes: token list -> (string * string option * mixfix) list * token list
wenzelm@18669
    64
  val simple_fixes: token list -> (string * string option) list * token list
wenzelm@5826
    65
  val term: token list -> string * token list
wenzelm@5826
    66
  val prop: token list -> string * token list
wenzelm@6935
    67
  val propp: token list -> (string * (string list * string list)) * token list
wenzelm@6949
    68
  val termp: token list -> (string * string list) * token list
wenzelm@9131
    69
  val arguments: token list -> Args.T list * token list
wenzelm@15703
    70
  val attribs: token list -> Attrib.src list * token list
wenzelm@15703
    71
  val opt_attribs: token list -> Attrib.src list * token list
wenzelm@15703
    72
  val thm_name: string -> token list -> (bstring * Attrib.src list) * token list
wenzelm@15703
    73
  val opt_thm_name: string -> token list -> (bstring * Attrib.src list) * token list
wenzelm@18618
    74
  val spec: token list -> ((bstring * Attrib.src list) * string list) * token list
wenzelm@18618
    75
  val named_spec: token list -> ((bstring * Attrib.src list) * string list) * token list
wenzelm@15703
    76
  val spec_name: token list -> ((bstring * string) * Attrib.src list) * token list
wenzelm@15703
    77
  val spec_opt_name: token list -> ((bstring * string) * Attrib.src list) * token list
wenzelm@15703
    78
  val xthm: token list -> (thmref * Attrib.src list) * token list
wenzelm@15703
    79
  val xthms1: token list -> (thmref * Attrib.src list) list * token list
wenzelm@17358
    80
  val name_facts: token list ->
wenzelm@17358
    81
    ((bstring * Attrib.src list) * (thmref * Attrib.src list) list) list * token list
wenzelm@18669
    82
  val locale_mixfix: token list -> mixfix * token list
wenzelm@18669
    83
  val locale_fixes: token list -> (string * string option * mixfix) list * token list
ballarin@16736
    84
  val locale_target: token list -> xstring * token list
ballarin@16736
    85
  val opt_locale_target: token list -> xstring option * token list
wenzelm@12272
    86
  val locale_expr: token list -> Locale.expr * token list
wenzelm@12942
    87
  val locale_keyword: token list -> string * token list
wenzelm@18136
    88
  val locale_element: token list -> Element.context Locale.element * token list
wenzelm@18136
    89
  val context_element: token list -> Element.context * token list
wenzelm@5826
    90
  val method: token list -> Method.text * token list
wenzelm@5826
    91
end;
wenzelm@5826
    92
wenzelm@5826
    93
structure OuterParse: OUTER_PARSE =
wenzelm@5826
    94
struct
wenzelm@5826
    95
wenzelm@9131
    96
structure T = OuterLex;
wenzelm@9131
    97
type token = T.token;
wenzelm@5826
    98
wenzelm@5826
    99
wenzelm@5826
   100
(** error handling **)
wenzelm@5826
   101
wenzelm@5826
   102
(* group atomic parsers (no cuts!) *)
wenzelm@5826
   103
wenzelm@5826
   104
fun fail_with s = Scan.fail_with
wenzelm@5826
   105
  (fn [] => s ^ " expected (past end-of-file!)"
wenzelm@9155
   106
    | (tok :: _) => s ^ " expected,\nbut " ^ T.name_of tok ^ T.pos_of tok ^ " was found");
wenzelm@5826
   107
wenzelm@5826
   108
fun group s scan = scan || fail_with s;
wenzelm@5826
   109
wenzelm@5826
   110
wenzelm@5877
   111
(* cut *)
wenzelm@5826
   112
wenzelm@8581
   113
fun cut kind scan =
wenzelm@5826
   114
  let
wenzelm@5826
   115
    fun get_pos [] = " (past end-of-file!)"
wenzelm@9131
   116
      | get_pos (tok :: _) = T.pos_of tok;
wenzelm@5826
   117
skalberg@15531
   118
    fun err (toks, NONE) = kind ^ get_pos toks
skalberg@15531
   119
      | err (toks, SOME msg) = kind ^ get_pos toks ^ ": " ^ msg;
wenzelm@5826
   120
  in Scan.!! err scan end;
wenzelm@5826
   121
wenzelm@8586
   122
fun !!! scan = cut "Outer syntax error" scan;
wenzelm@8586
   123
fun !!!! scan = cut "Corrupted outer syntax in presentation" scan;
wenzelm@8581
   124
wenzelm@5826
   125
wenzelm@5826
   126
wenzelm@5826
   127
(** basic parsers **)
wenzelm@5826
   128
wenzelm@5826
   129
(* utils *)
wenzelm@5826
   130
wenzelm@5826
   131
fun triple1 ((x, y), z) = (x, y, z);
wenzelm@5826
   132
fun triple2 (x, (y, z)) = (x, y, z);
wenzelm@6430
   133
fun triple_swap ((x, y), z) = ((x, z), y);
wenzelm@5826
   134
wenzelm@5826
   135
wenzelm@5826
   136
(* tokens *)
wenzelm@5826
   137
wenzelm@15703
   138
val not_eof = Scan.one T.not_eof;
wenzelm@15703
   139
wenzelm@15703
   140
fun position scan = (Scan.ahead not_eof >> T.position_of) -- scan >> Library.swap;
wenzelm@5826
   141
wenzelm@5826
   142
fun kind k =
wenzelm@9131
   143
  group (T.str_of_kind k) (Scan.one (T.is_kind k) >> T.val_of);
wenzelm@5826
   144
wenzelm@9131
   145
val command = kind T.Command;
wenzelm@9131
   146
val keyword = kind T.Keyword;
wenzelm@9131
   147
val short_ident = kind T.Ident;
wenzelm@9131
   148
val long_ident = kind T.LongIdent;
wenzelm@9131
   149
val sym_ident = kind T.SymIdent;
wenzelm@9131
   150
val term_var = kind T.Var;
wenzelm@9131
   151
val type_ident = kind T.TypeIdent;
wenzelm@9131
   152
val type_var = kind T.TypeVar;
wenzelm@9131
   153
val number = kind T.Nat;
wenzelm@9131
   154
val string = kind T.String;
wenzelm@17165
   155
val alt_string = kind T.AltString;
wenzelm@9131
   156
val verbatim = kind T.Verbatim;
wenzelm@9131
   157
val sync = kind T.Sync;
wenzelm@9131
   158
val eof = kind T.EOF;
wenzelm@5826
   159
wenzelm@5826
   160
fun $$$ x =
wenzelm@9131
   161
  group (T.str_of_kind T.Keyword ^ " " ^ quote x)
wenzelm@9131
   162
    (Scan.one (T.keyword_with (equal x)) >> T.val_of);
wenzelm@9131
   163
wenzelm@16030
   164
fun reserved x =
wenzelm@16030
   165
  group ("Reserved identifier " ^ quote x)
wenzelm@16030
   166
    (Scan.one (T.ident_with (equal x)) >> T.val_of);
wenzelm@16030
   167
wenzelm@9131
   168
val semicolon = $$$ ";";
wenzelm@5826
   169
wenzelm@15703
   170
val minus = sym_ident :-- (fn "-" => Scan.succeed () | _ => Scan.fail) >> #1;
wenzelm@11792
   171
val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1;
wenzelm@15703
   172
fun maybe scan = underscore >> K NONE || scan >> SOME;
wenzelm@11792
   173
wenzelm@14835
   174
val nat = number >> (#1 o Library.read_int o Symbol.explode);
wenzelm@5826
   175
wenzelm@17070
   176
val tag_name = group "tag name" (short_ident || string);
wenzelm@17070
   177
val tags = Scan.repeat ($$$ "%" |-- !!! tag_name);
wenzelm@17070
   178
wenzelm@7930
   179
val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
wenzelm@14646
   180
fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false;
wenzelm@14646
   181
wenzelm@5826
   182
wenzelm@5826
   183
(* enumerations *)
wenzelm@5826
   184
wenzelm@6003
   185
fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::;
wenzelm@5826
   186
fun enum sep scan = enum1 sep scan || Scan.succeed [];
wenzelm@5826
   187
wenzelm@5826
   188
fun list1 scan = enum1 "," scan;
wenzelm@5826
   189
fun list scan = enum "," scan;
wenzelm@5826
   190
wenzelm@6013
   191
fun and_list1 scan = enum1 "and" scan;
wenzelm@6013
   192
fun and_list scan = enum "and" scan;
wenzelm@6013
   193
wenzelm@5826
   194
wenzelm@5960
   195
(* names and text *)
wenzelm@5826
   196
wenzelm@8146
   197
val name = group "name declaration" (short_ident || sym_ident || string || number);
wenzelm@8146
   198
val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number);
wenzelm@8146
   199
val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim);
wenzelm@14949
   200
val path = group "file name/path specification" name >> Path.unpack;
wenzelm@6553
   201
wenzelm@6553
   202
wenzelm@6372
   203
(* sorts and arities *)
wenzelm@5826
   204
wenzelm@8897
   205
val sort = group "sort" xname;
wenzelm@5826
   206
wenzelm@6372
   207
fun gen_arity cod =
wenzelm@7352
   208
  Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- cod;
wenzelm@6372
   209
wenzelm@6372
   210
val arity = gen_arity sort;
wenzelm@5826
   211
wenzelm@5826
   212
wenzelm@5826
   213
(* types *)
wenzelm@5826
   214
wenzelm@8146
   215
val typ = group "type"
wenzelm@8146
   216
  (short_ident || long_ident || sym_ident || type_ident || type_var || string || number);
wenzelm@5826
   217
wenzelm@5826
   218
val type_args =
wenzelm@5826
   219
  type_ident >> single ||
wenzelm@5826
   220
  $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
wenzelm@5826
   221
  Scan.succeed [];
wenzelm@5826
   222
wenzelm@5826
   223
wenzelm@5826
   224
(* mixfix annotations *)
wenzelm@5826
   225
wenzelm@18669
   226
val mfix = string --
wenzelm@18669
   227
  !!! (Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [] --
wenzelm@18669
   228
    Scan.optional nat Syntax.max_pri) >> (Mixfix o triple2);
wenzelm@18669
   229
wenzelm@18669
   230
val infx = $$$ "infix" |-- !!! (nat >> Infix || string -- nat >> InfixName);
wenzelm@18669
   231
val infxl = $$$ "infixl" |-- !!! (nat >> Infixl || string -- nat >> InfixlName);
wenzelm@18669
   232
val infxr = $$$ "infixr" |-- !!! (nat >> Infixr || string -- nat >> InfixrName);
wenzelm@5826
   233
wenzelm@18669
   234
val binder = $$$ "binder" |--
wenzelm@18669
   235
  !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
wenzelm@18669
   236
  >> (Binder o triple2);
wenzelm@18669
   237
wenzelm@18669
   238
fun annotation guard fix = $$$ "(" |-- guard (fix --| $$$ ")");
wenzelm@18669
   239
fun opt_annotation guard fix = Scan.optional (annotation guard fix) NoSyn;
wenzelm@18669
   240
wenzelm@18669
   241
val mixfix = annotation !!! (mfix || binder || infxl || infxr || infx);
wenzelm@18669
   242
val opt_infix = opt_annotation !!! (infxl || infxr || infx);
wenzelm@18669
   243
val opt_mixfix = opt_annotation !!! (mfix || binder || infxl || infxr || infx);
wenzelm@18669
   244
val opt_infix' = opt_annotation I (infxl || infxr || infx);
wenzelm@18669
   245
val opt_mixfix' = opt_annotation I (mfix || binder || infxl || infxr || infx);
wenzelm@5826
   246
wenzelm@5826
   247
wenzelm@18669
   248
(* fixes *)
wenzelm@5826
   249
wenzelm@18618
   250
val const = name -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1;
wenzelm@18669
   251
wenzelm@18669
   252
val params = Scan.repeat1 name -- Scan.option ($$$ "::" |-- !!! typ)
wenzelm@18669
   253
  >> (fn (xs, T) => map (rpair T) xs);
wenzelm@18669
   254
wenzelm@18669
   255
val simple_fixes = and_list1 params >> List.concat;
wenzelm@18669
   256
wenzelm@18669
   257
val fixes =
wenzelm@18669
   258
  and_list1 (name -- Scan.option ($$$ "::" |-- typ) -- mixfix >> (single o triple1) ||
wenzelm@18669
   259
    params >> map Syntax.no_syn) >> List.concat;
wenzelm@5826
   260
wenzelm@5826
   261
wenzelm@5826
   262
(* terms *)
wenzelm@5826
   263
wenzelm@7477
   264
val trm = short_ident || long_ident || sym_ident || term_var || number || string;
wenzelm@5826
   265
wenzelm@5826
   266
val term = group "term" trm;
wenzelm@5826
   267
val prop = group "proposition" trm;
wenzelm@5826
   268
wenzelm@5826
   269
wenzelm@6949
   270
(* patterns *)
wenzelm@6935
   271
wenzelm@6949
   272
val is_terms = Scan.repeat1 ($$$ "is" |-- term);
wenzelm@6935
   273
val is_props = Scan.repeat1 ($$$ "is" |-- prop);
wenzelm@6935
   274
val concl_props = $$$ "concl" |-- !!! is_props;
wenzelm@7418
   275
val any_props = concl_props >> pair [] || is_props -- Scan.optional concl_props [];
wenzelm@6935
   276
wenzelm@6935
   277
val propp = prop -- Scan.optional ($$$ "(" |-- !!! (any_props --| $$$ ")")) ([], []);
wenzelm@12047
   278
val propp' = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) [];
wenzelm@6949
   279
val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
wenzelm@6935
   280
wenzelm@6935
   281
wenzelm@5826
   282
(* arguments *)
wenzelm@5826
   283
wenzelm@9131
   284
fun keyword_symid is_symid = Scan.one (T.keyword_with is_symid) >> T.val_of;
wenzelm@9131
   285
val keyword_sid = keyword_symid T.is_sid;
wenzelm@5826
   286
wenzelm@6983
   287
fun atom_arg is_symid blk =
wenzelm@5826
   288
  group "argument"
wenzelm@7477
   289
    (position (short_ident || long_ident || sym_ident || term_var ||
wenzelm@15703
   290
        type_ident || type_var || number) >> Args.mk_ident ||
wenzelm@15703
   291
      position (keyword_symid is_symid) >> Args.mk_keyword ||
wenzelm@15703
   292
      position (string || verbatim) >> Args.mk_string ||
wenzelm@18037
   293
      position alt_string >> Args.mk_alt_string ||
wenzelm@15703
   294
      position (if blk then $$$ "," else Scan.fail) >> Args.mk_keyword);
wenzelm@5826
   295
wenzelm@5877
   296
fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r))
wenzelm@15703
   297
  >> (fn (x, (ys, z)) => Args.mk_keyword x :: ys @ [Args.mk_keyword z]);
wenzelm@5826
   298
wenzelm@6983
   299
fun args is_symid blk x = Scan.optional (args1 is_symid blk) [] x
wenzelm@6983
   300
and args1 is_symid blk x =
wenzelm@5826
   301
  ((Scan.repeat1
wenzelm@6983
   302
    (Scan.repeat1 (atom_arg is_symid blk) ||
wenzelm@6983
   303
      paren_args "(" ")" (args is_symid) ||
skalberg@15570
   304
      paren_args "[" "]" (args is_symid))) >> List.concat) x;
wenzelm@5826
   305
wenzelm@9131
   306
val arguments = args T.is_sid false;
wenzelm@9131
   307
wenzelm@5826
   308
wenzelm@6372
   309
(* theorem specifications *)
wenzelm@5826
   310
wenzelm@9131
   311
val attrib = position ((keyword_sid || xname) -- !!! arguments) >> Args.src;
wenzelm@5826
   312
val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
wenzelm@5826
   313
val opt_attribs = Scan.optional attribs [];
wenzelm@5826
   314
wenzelm@6398
   315
fun thm_name s = name -- opt_attribs --| $$$ s;
wenzelm@6511
   316
fun opt_thm_name s =
wenzelm@17756
   317
  Scan.optional ((name -- opt_attribs || (attribs >> pair "")) --| $$$ s) ("", []);
wenzelm@5917
   318
wenzelm@18618
   319
val spec = opt_thm_name ":" -- Scan.repeat1 prop;
wenzelm@18618
   320
val named_spec = thm_name ":" -- Scan.repeat1 prop;
wenzelm@18618
   321
wenzelm@6372
   322
val spec_name = thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
wenzelm@6372
   323
val spec_opt_name = opt_thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
wenzelm@6372
   324
berghofe@15456
   325
val thm_sel = $$$ "(" |-- list1
wenzelm@15703
   326
 (nat --| minus -- nat >> PureThy.FromTo ||
wenzelm@15703
   327
  nat --| minus >> PureThy.From ||
wenzelm@15703
   328
  nat >> PureThy.Single) --| $$$ ")";
berghofe@15456
   329
wenzelm@18037
   330
val xthm = (alt_string >> Fact || xname -- thm_sel >> NameSelection || xname >> Name)
wenzelm@18037
   331
  -- opt_attribs;
wenzelm@6372
   332
val xthms1 = Scan.repeat1 xthm;
wenzelm@5826
   333
wenzelm@17358
   334
val name_facts = and_list1 (opt_thm_name "=" -- xthms1);
wenzelm@17358
   335
wenzelm@5826
   336
wenzelm@18136
   337
(* locale and context elements *)
wenzelm@12047
   338
wenzelm@18669
   339
val locale_mixfix = $$$ "(" -- $$$ "structure" -- !!! ($$$ ")") >> K Structure || mixfix;
wenzelm@18669
   340
wenzelm@18669
   341
val locale_fixes =
wenzelm@18669
   342
  and_list1 (name -- Scan.option ($$$ "::" |-- typ) -- locale_mixfix >> (single o triple1) ||
wenzelm@18669
   343
    params >> map Syntax.no_syn) >> List.concat;
wenzelm@18669
   344
wenzelm@12272
   345
local
wenzelm@12272
   346
ballarin@16169
   347
val loc_keyword = $$$ "fixes" || $$$ "constrains" || $$$ "assumes" ||
ballarin@16169
   348
   $$$ "defines" || $$$ "notes" || $$$ "includes";
wenzelm@12272
   349
ballarin@15127
   350
val loc_element =
wenzelm@18669
   351
  $$$ "fixes" |-- !!! locale_fixes >> Element.Fixes ||
ballarin@16169
   352
  $$$ "constrains" |-- !!! (and_list1 (name -- ($$$ "::" |-- typ)))
wenzelm@18136
   353
    >> Element.Constrains ||
ballarin@15127
   354
  $$$ "assumes" |-- !!! (and_list1 (opt_thm_name ":" -- Scan.repeat1 propp))
wenzelm@18136
   355
    >> Element.Assumes ||
ballarin@15127
   356
  $$$ "defines" |-- !!! (and_list1 (opt_thm_name ":" -- propp'))
wenzelm@18136
   357
    >> Element.Defines ||
ballarin@15127
   358
  $$$ "notes" |-- !!! (and_list1 (opt_thm_name "=" -- xthms1))
wenzelm@18136
   359
    >> Element.Notes;
ballarin@15127
   360
wenzelm@12272
   361
fun plus1 scan =
wenzelm@12272
   362
  scan -- Scan.repeat ($$$ "+" |-- Scan.unless loc_keyword (!!! scan)) >> op ::;
wenzelm@12272
   363
wenzelm@18669
   364
val rename = name -- Scan.option mixfix;
ballarin@16102
   365
wenzelm@12268
   366
fun expr2 x = (xname >> Locale.Locale || $$$ "(" |-- !!! (expr0 --| $$$ ")")) x
ballarin@16102
   367
and expr1 x = (expr2 -- Scan.repeat1 (maybe rename) >> Locale.Rename || expr2) x
wenzelm@12272
   368
and expr0 x = (plus1 expr1 >> (fn [e] => e | es => Locale.Merge es)) x;
wenzelm@12268
   369
wenzelm@12272
   370
in
wenzelm@12272
   371
ballarin@16736
   372
val locale_target = ($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")");
ballarin@16736
   373
val opt_locale_target = Scan.option locale_target;
wenzelm@12272
   374
val locale_expr = expr0;
wenzelm@12942
   375
val locale_keyword = loc_keyword;
wenzelm@12047
   376
wenzelm@18136
   377
val locale_element = group "locale element"
wenzelm@18136
   378
  (loc_element >> Locale.Elem || $$$ "includes" |-- !!! locale_expr >> Locale.Expr);
ballarin@15127
   379
wenzelm@18136
   380
val context_element = group "context element" loc_element;
wenzelm@12272
   381
wenzelm@12272
   382
end;
wenzelm@12047
   383
wenzelm@12047
   384
wenzelm@5826
   385
(* proof methods *)
wenzelm@5826
   386
wenzelm@6983
   387
fun is_symid_meth s =
wenzelm@9131
   388
  s <> "|" andalso s <> "?" andalso s <> "+" andalso T.is_sid s;
wenzelm@6983
   389
wenzelm@5826
   390
fun meth4 x =
wenzelm@5877
   391
 (position (xname >> rpair []) >> (Method.Source o Args.src) ||
wenzelm@6983
   392
  $$$ "(" |-- !!! (meth0 --| $$$ ")")) x
wenzelm@5826
   393
and meth3 x =
wenzelm@5826
   394
 (meth4 --| $$$ "?" >> Method.Try ||
wenzelm@5826
   395
  meth4 --| $$$ "+" >> Method.Repeat1 ||
wenzelm@5940
   396
  meth4) x
wenzelm@5940
   397
and meth2 x =
wenzelm@6983
   398
 (position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) ||
wenzelm@5826
   399
  meth3) x
wenzelm@5826
   400
and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
wenzelm@5826
   401
and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;
wenzelm@5826
   402
wenzelm@6558
   403
val method = meth3;
wenzelm@5826
   404
wenzelm@5826
   405
wenzelm@5826
   406
end;