src/Pure/Isar/outer_parse.ML
author wenzelm
Mon May 03 14:25:56 2010 +0200 (2010-05-03)
changeset 36610 bafd82950e24
parent 35838 c8bd075c4de8
permissions -rw-r--r--
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm@5826
     1
(*  Title:      Pure/Isar/outer_parse.ML
wenzelm@5826
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5826
     3
wenzelm@5826
     4
Generic parsers for Isabelle/Isar outer syntax.
wenzelm@5826
     5
*)
wenzelm@5826
     6
wenzelm@5826
     7
signature OUTER_PARSE =
wenzelm@5826
     8
sig
wenzelm@27886
     9
  type token = OuterLex.token
wenzelm@29310
    10
  type 'a parser = token list -> 'a * token list
wenzelm@30511
    11
  type 'a context_parser = Context.generic * token list -> 'a * (Context.generic * token list)
wenzelm@5826
    12
  val group: string -> (token list -> 'a) -> token list -> 'a
wenzelm@5826
    13
  val !!! : (token list -> 'a) -> token list -> 'a
wenzelm@8581
    14
  val !!!! : (token list -> 'a) -> token list -> 'a
wenzelm@12047
    15
  val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
wenzelm@12047
    16
  val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
wenzelm@12047
    17
  val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b
wenzelm@29310
    18
  val not_eof: token parser
wenzelm@5826
    19
  val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
wenzelm@29310
    20
  val command: string parser
wenzelm@29310
    21
  val keyword: string parser
wenzelm@29310
    22
  val short_ident: string parser
wenzelm@29310
    23
  val long_ident: string parser
wenzelm@29310
    24
  val sym_ident: string parser
wenzelm@29310
    25
  val minus: string parser
wenzelm@29310
    26
  val term_var: string parser
wenzelm@29310
    27
  val type_ident: string parser
wenzelm@29310
    28
  val type_var: string parser
wenzelm@29310
    29
  val number: string parser
wenzelm@29310
    30
  val string: string parser
wenzelm@29310
    31
  val alt_string: string parser
wenzelm@29310
    32
  val verbatim: string parser
wenzelm@29310
    33
  val sync: string parser
wenzelm@29310
    34
  val eof: string parser
wenzelm@29310
    35
  val keyword_with: (string -> bool) -> string parser
wenzelm@29310
    36
  val keyword_ident_or_symbolic: string parser
wenzelm@29310
    37
  val $$$ : string -> string parser
wenzelm@29310
    38
  val reserved: string -> string parser
wenzelm@29310
    39
  val semicolon: string parser
wenzelm@29310
    40
  val underscore: string parser
wenzelm@29310
    41
  val maybe: 'a parser -> 'a option parser
wenzelm@29310
    42
  val tag_name: string parser
wenzelm@29310
    43
  val tags: string list parser
wenzelm@29310
    44
  val opt_unit: unit parser
wenzelm@29310
    45
  val opt_keyword: string -> bool parser
wenzelm@29310
    46
  val begin: string parser
wenzelm@29310
    47
  val opt_begin: bool parser
wenzelm@29310
    48
  val nat: int parser
wenzelm@29310
    49
  val int: int parser
wenzelm@29310
    50
  val enum: string -> 'a parser -> 'a list parser
wenzelm@29310
    51
  val enum1: string -> 'a parser -> 'a list parser
wenzelm@29310
    52
  val and_list: 'a parser -> 'a list parser
wenzelm@29310
    53
  val and_list1: 'a parser -> 'a list parser
wenzelm@30511
    54
  val enum': string -> 'a context_parser -> 'a list context_parser
wenzelm@30511
    55
  val enum1': string -> 'a context_parser -> 'a list context_parser
wenzelm@30511
    56
  val and_list': 'a context_parser -> 'a list context_parser
wenzelm@30511
    57
  val and_list1': 'a context_parser -> 'a list context_parser
wenzelm@29310
    58
  val list: 'a parser -> 'a list parser
wenzelm@29310
    59
  val list1: 'a parser -> 'a list parser
wenzelm@29310
    60
  val name: bstring parser
haftmann@29581
    61
  val binding: binding parser
wenzelm@29310
    62
  val xname: xstring parser
wenzelm@29310
    63
  val text: string parser
wenzelm@29310
    64
  val path: Path.T parser
wenzelm@29310
    65
  val parname: string parser
haftmann@29581
    66
  val parbinding: binding parser
wenzelm@29310
    67
  val sort: string parser
wenzelm@29310
    68
  val arity: (string * string list * string) parser
wenzelm@29310
    69
  val multi_arity: (string list * string list * string) parser
wenzelm@29310
    70
  val type_args: string list parser
wenzelm@35838
    71
  val type_args_constrained: (string * string option) list parser
wenzelm@29310
    72
  val typ_group: string parser
wenzelm@29310
    73
  val typ: string parser
wenzelm@29310
    74
  val mixfix: mixfix parser
wenzelm@29310
    75
  val mixfix': mixfix parser
wenzelm@29310
    76
  val opt_mixfix: mixfix parser
wenzelm@29310
    77
  val opt_mixfix': mixfix parser
wenzelm@29310
    78
  val where_: string parser
wenzelm@29310
    79
  val const: (string * string * mixfix) parser
wenzelm@30339
    80
  val const_binding: (binding * string * mixfix) parser
haftmann@29581
    81
  val params: (binding * string option) list parser
haftmann@29581
    82
  val simple_fixes: (binding * string option) list parser
haftmann@29581
    83
  val fixes: (binding * string option * mixfix) list parser
haftmann@29581
    84
  val for_fixes: (binding * string option * mixfix) list parser
haftmann@29581
    85
  val for_simple_fixes: (binding * string option) list parser
wenzelm@30573
    86
  val ML_source: (Symbol_Pos.text * Position.T) parser
wenzelm@30573
    87
  val doc_source: (Symbol_Pos.text * Position.T) parser
wenzelm@29310
    88
  val term_group: string parser
wenzelm@29310
    89
  val prop_group: string parser
wenzelm@29310
    90
  val term: string parser
wenzelm@29310
    91
  val prop: string parser
wenzelm@29310
    92
  val propp: (string * string list) parser
wenzelm@29310
    93
  val termp: (string * string list) parser
wenzelm@29310
    94
  val target: xstring parser
wenzelm@29310
    95
  val opt_target: xstring option parser
wenzelm@5826
    96
end;
wenzelm@5826
    97
wenzelm@5826
    98
structure OuterParse: OUTER_PARSE =
wenzelm@5826
    99
struct
wenzelm@5826
   100
wenzelm@9131
   101
structure T = OuterLex;
wenzelm@9131
   102
type token = T.token;
wenzelm@5826
   103
wenzelm@29310
   104
type 'a parser = token list -> 'a * token list;
wenzelm@30511
   105
type 'a context_parser = Context.generic * token list -> 'a * (Context.generic * token list);
wenzelm@29310
   106
wenzelm@5826
   107
wenzelm@5826
   108
(** error handling **)
wenzelm@5826
   109
wenzelm@5826
   110
(* group atomic parsers (no cuts!) *)
wenzelm@5826
   111
wenzelm@5826
   112
fun fail_with s = Scan.fail_with
wenzelm@5826
   113
  (fn [] => s ^ " expected (past end-of-file!)"
wenzelm@23789
   114
    | (tok :: _) =>
wenzelm@23789
   115
        (case T.text_of tok of
wenzelm@23789
   116
          (txt, "") => s ^ " expected,\nbut " ^ txt ^ T.pos_of tok ^ " was found"
wenzelm@23789
   117
        | (txt1, txt2) => s ^ " expected,\nbut " ^ txt1 ^ T.pos_of tok ^ " was found:\n" ^ txt2));
wenzelm@5826
   118
wenzelm@5826
   119
fun group s scan = scan || fail_with s;
wenzelm@5826
   120
wenzelm@5826
   121
wenzelm@5877
   122
(* cut *)
wenzelm@5826
   123
wenzelm@8581
   124
fun cut kind scan =
wenzelm@5826
   125
  let
wenzelm@5826
   126
    fun get_pos [] = " (past end-of-file!)"
wenzelm@9131
   127
      | get_pos (tok :: _) = T.pos_of tok;
wenzelm@5826
   128
skalberg@15531
   129
    fun err (toks, NONE) = kind ^ get_pos toks
wenzelm@25625
   130
      | err (toks, SOME msg) =
wenzelm@25625
   131
          if String.isPrefix kind msg then msg
wenzelm@25625
   132
          else 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@27815
   151
fun RESET_VALUE atom = (*required for all primitive parsers*)
wenzelm@27815
   152
  Scan.ahead (Scan.one (K true)) -- atom >> (fn (arg, x) => (T.assign NONE arg; x));
wenzelm@27815
   153
wenzelm@27815
   154
wenzelm@27815
   155
val not_eof = RESET_VALUE (Scan.one T.not_eof);
wenzelm@15703
   156
wenzelm@27753
   157
fun position scan = (Scan.ahead not_eof >> T.position_of) -- scan >> Library.swap;
wenzelm@27886
   158
fun source_position atom = Scan.ahead atom |-- not_eof >> T.source_position_of;
wenzelm@27815
   159
fun inner_syntax atom = Scan.ahead atom |-- not_eof >> T.source_of;
wenzelm@5826
   160
wenzelm@5826
   161
fun kind k =
wenzelm@27815
   162
  group (T.str_of_kind k) (RESET_VALUE (Scan.one (T.is_kind k) >> T.content_of));
wenzelm@5826
   163
wenzelm@9131
   164
val command = kind T.Command;
wenzelm@9131
   165
val keyword = kind T.Keyword;
wenzelm@9131
   166
val short_ident = kind T.Ident;
wenzelm@9131
   167
val long_ident = kind T.LongIdent;
wenzelm@9131
   168
val sym_ident = kind T.SymIdent;
wenzelm@9131
   169
val term_var = kind T.Var;
wenzelm@9131
   170
val type_ident = kind T.TypeIdent;
wenzelm@9131
   171
val type_var = kind T.TypeVar;
wenzelm@9131
   172
val number = kind T.Nat;
wenzelm@9131
   173
val string = kind T.String;
wenzelm@17165
   174
val alt_string = kind T.AltString;
wenzelm@9131
   175
val verbatim = kind T.Verbatim;
wenzelm@9131
   176
val sync = kind T.Sync;
wenzelm@9131
   177
val eof = kind T.EOF;
wenzelm@5826
   178
wenzelm@27815
   179
fun keyword_with pred = RESET_VALUE (Scan.one (T.keyword_with pred) >> T.content_of);
wenzelm@27815
   180
val keyword_ident_or_symbolic = keyword_with T.ident_or_symbolic;
wenzelm@27815
   181
wenzelm@5826
   182
fun $$$ x =
wenzelm@27815
   183
  group (T.str_of_kind T.Keyword ^ " " ^ quote x) (keyword_with (fn y => x = y));
wenzelm@9131
   184
wenzelm@16030
   185
fun reserved x =
wenzelm@27753
   186
  group ("reserved identifier " ^ quote x)
wenzelm@27815
   187
    (RESET_VALUE (Scan.one (T.ident_with (fn y => x = y)) >> T.content_of));
wenzelm@16030
   188
wenzelm@9131
   189
val semicolon = $$$ ";";
wenzelm@5826
   190
wenzelm@15703
   191
val minus = sym_ident :-- (fn "-" => Scan.succeed () | _ => Scan.fail) >> #1;
wenzelm@11792
   192
val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1;
wenzelm@15703
   193
fun maybe scan = underscore >> K NONE || scan >> SOME;
wenzelm@11792
   194
wenzelm@14835
   195
val nat = number >> (#1 o Library.read_int o Symbol.explode);
wenzelm@27815
   196
val int = Scan.optional (minus >> K ~1) 1 -- nat >> op *;
wenzelm@5826
   197
wenzelm@17070
   198
val tag_name = group "tag name" (short_ident || string);
wenzelm@17070
   199
val tags = Scan.repeat ($$$ "%" |-- !!! tag_name);
wenzelm@17070
   200
wenzelm@7930
   201
val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
wenzelm@14646
   202
fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false;
wenzelm@14646
   203
wenzelm@20983
   204
val begin = $$$ "begin";
wenzelm@20983
   205
val opt_begin = Scan.optional (begin >> K true) false;
wenzelm@20961
   206
wenzelm@5826
   207
wenzelm@5826
   208
(* enumerations *)
wenzelm@5826
   209
wenzelm@25999
   210
fun enum1 sep scan = scan ::: Scan.repeat ($$$ sep |-- !!! scan);
wenzelm@5826
   211
fun enum sep scan = enum1 sep scan || Scan.succeed [];
wenzelm@5826
   212
wenzelm@27815
   213
fun enum1' sep scan = scan ::: Scan.repeat (Scan.lift ($$$ sep) |-- scan);
wenzelm@27815
   214
fun enum' sep scan = enum1' sep scan || Scan.succeed [];
wenzelm@5826
   215
wenzelm@6013
   216
fun and_list1 scan = enum1 "and" scan;
wenzelm@6013
   217
fun and_list scan = enum "and" scan;
wenzelm@6013
   218
wenzelm@27815
   219
fun and_list1' scan = enum1' "and" scan;
wenzelm@27815
   220
fun and_list' scan = enum' "and" scan;
wenzelm@27815
   221
wenzelm@27815
   222
fun list1 scan = enum1 "," scan;
wenzelm@27815
   223
fun list scan = enum "," scan;
wenzelm@27815
   224
wenzelm@5826
   225
wenzelm@5960
   226
(* names and text *)
wenzelm@5826
   227
wenzelm@8146
   228
val name = group "name declaration" (short_ident || sym_ident || string || number);
wenzelm@30223
   229
val binding = position name >> Binding.make;
wenzelm@8146
   230
val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number);
wenzelm@8146
   231
val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim);
wenzelm@21858
   232
val path = group "file name/path specification" name >> Path.explode;
wenzelm@6553
   233
wenzelm@18898
   234
val parname = Scan.optional ($$$ "(" |-- name --| $$$ ")") "";
haftmann@28965
   235
val parbinding = Scan.optional ($$$ "(" |-- binding --| $$$ ")") Binding.empty;
wenzelm@18898
   236
wenzelm@6553
   237
wenzelm@6372
   238
(* sorts and arities *)
wenzelm@5826
   239
wenzelm@27753
   240
val sort = group "sort" (inner_syntax xname);
wenzelm@5826
   241
wenzelm@22331
   242
val arity = xname -- ($$$ "::" |-- !!!
wenzelm@22331
   243
  (Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2;
wenzelm@5826
   244
haftmann@25541
   245
val multi_arity = and_list1 xname -- ($$$ "::" |-- !!!
haftmann@25541
   246
  (Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2;
haftmann@25541
   247
wenzelm@5826
   248
wenzelm@5826
   249
(* types *)
wenzelm@5826
   250
wenzelm@27753
   251
val typ_group = group "type"
wenzelm@8146
   252
  (short_ident || long_ident || sym_ident || type_ident || type_var || string || number);
wenzelm@5826
   253
wenzelm@27753
   254
val typ = inner_syntax typ_group;
wenzelm@27753
   255
wenzelm@35838
   256
fun type_arguments arg =
wenzelm@35838
   257
  arg >> single ||
wenzelm@35838
   258
  $$$ "(" |-- !!! (list1 arg --| $$$ ")") ||
wenzelm@5826
   259
  Scan.succeed [];
wenzelm@5826
   260
wenzelm@35838
   261
val type_args = type_arguments type_ident;
wenzelm@35838
   262
val type_args_constrained = type_arguments (type_ident -- Scan.option ($$$ "::" |-- !!! sort));
wenzelm@35838
   263
wenzelm@5826
   264
wenzelm@5826
   265
(* mixfix annotations *)
wenzelm@5826
   266
wenzelm@18669
   267
val mfix = string --
wenzelm@18669
   268
  !!! (Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [] --
wenzelm@18669
   269
    Scan.optional nat Syntax.max_pri) >> (Mixfix o triple2);
wenzelm@18669
   270
wenzelm@35130
   271
val infx = $$$ "infix" |-- !!! (string -- nat >> Infix);
wenzelm@35130
   272
val infxl = $$$ "infixl" |-- !!! (string -- nat >> Infixl);
wenzelm@35130
   273
val infxr = $$$ "infixr" |-- !!! (string -- nat >> Infixr);
wenzelm@5826
   274
wenzelm@18669
   275
val binder = $$$ "binder" |--
wenzelm@18669
   276
  !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
wenzelm@18669
   277
  >> (Binder o triple2);
wenzelm@18669
   278
wenzelm@18669
   279
fun annotation guard fix = $$$ "(" |-- guard (fix --| $$$ ")");
wenzelm@18669
   280
fun opt_annotation guard fix = Scan.optional (annotation guard fix) NoSyn;
wenzelm@18669
   281
wenzelm@18669
   282
val mixfix = annotation !!! (mfix || binder || infxl || infxr || infx);
wenzelm@21609
   283
val mixfix' = annotation I (mfix || binder || infxl || infxr || infx);
wenzelm@18669
   284
val opt_mixfix = opt_annotation !!! (mfix || binder || infxl || infxr || infx);
wenzelm@18669
   285
val opt_mixfix' = opt_annotation I (mfix || binder || infxl || infxr || infx);
wenzelm@5826
   286
wenzelm@5826
   287
wenzelm@18669
   288
(* fixes *)
wenzelm@5826
   289
wenzelm@21400
   290
val where_ = $$$ "where";
wenzelm@21400
   291
wenzelm@18618
   292
val const = name -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1;
wenzelm@30339
   293
val const_binding = binding -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1;
wenzelm@18669
   294
wenzelm@28081
   295
val params = Scan.repeat1 binding -- Scan.option ($$$ "::" |-- !!! typ)
wenzelm@18669
   296
  >> (fn (xs, T) => map (rpair T) xs);
wenzelm@18669
   297
wenzelm@19482
   298
val simple_fixes = and_list1 params >> flat;
wenzelm@18669
   299
wenzelm@18669
   300
val fixes =
wenzelm@28081
   301
  and_list1 (binding -- Scan.option ($$$ "::" |-- typ) -- mixfix >> (single o triple1) ||
wenzelm@19482
   302
    params >> map Syntax.no_syn) >> flat;
wenzelm@5826
   303
wenzelm@19845
   304
val for_fixes = Scan.optional ($$$ "for" |-- !!! fixes) [];
wenzelm@21371
   305
val for_simple_fixes = Scan.optional ($$$ "for" |-- !!! simple_fixes) [];
wenzelm@19845
   306
wenzelm@5826
   307
wenzelm@27877
   308
(* embedded source text *)
wenzelm@27872
   309
wenzelm@27877
   310
val ML_source = source_position (group "ML source" text);
wenzelm@27872
   311
val doc_source = source_position (group "document source" text);
wenzelm@27872
   312
wenzelm@27872
   313
wenzelm@5826
   314
(* terms *)
wenzelm@5826
   315
wenzelm@7477
   316
val trm = short_ident || long_ident || sym_ident || term_var || number || string;
wenzelm@5826
   317
wenzelm@27753
   318
val term_group = group "term" trm;
wenzelm@27753
   319
val prop_group = group "proposition" trm;
wenzelm@27753
   320
wenzelm@27753
   321
val term = inner_syntax term_group;
wenzelm@27753
   322
val prop = inner_syntax prop_group;
wenzelm@5826
   323
wenzelm@5826
   324
wenzelm@6949
   325
(* patterns *)
wenzelm@6935
   326
wenzelm@6949
   327
val is_terms = Scan.repeat1 ($$$ "is" |-- term);
wenzelm@6935
   328
val is_props = Scan.repeat1 ($$$ "is" |-- prop);
wenzelm@6935
   329
wenzelm@19585
   330
val propp = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) [];
wenzelm@6949
   331
val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
wenzelm@6935
   332
wenzelm@6935
   333
wenzelm@22119
   334
(* targets *)
wenzelm@19811
   335
wenzelm@22119
   336
val target = ($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")");
wenzelm@22119
   337
val opt_target = Scan.option target;
wenzelm@12272
   338
wenzelm@12272
   339
end;
wenzelm@30511
   340
wenzelm@30511
   341
type 'a parser = 'a OuterParse.parser;
wenzelm@30511
   342
type 'a context_parser = 'a OuterParse.context_parser;
wenzelm@30511
   343