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