src/Pure/Isar/parse.ML
author wenzelm
Sat Jul 23 16:37:17 2011 +0200 (2011-07-23)
changeset 43947 9b00f09f7721
parent 43775 b361c7d184e7
child 44357 5f5649ac8235
permissions -rw-r--r--
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm@36949
     1
(*  Title:      Pure/Isar/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@36949
     7
signature PARSE =
wenzelm@5826
     8
sig
wenzelm@36959
     9
  type 'a parser = Token.T list -> 'a * Token.T list
wenzelm@36959
    10
  type 'a context_parser = Context.generic * Token.T list -> 'a * (Context.generic * Token.T list)
wenzelm@36959
    11
  val group: string -> (Token.T list -> 'a) -> Token.T list -> 'a
wenzelm@36959
    12
  val !!! : (Token.T list -> 'a) -> Token.T list -> 'a
wenzelm@36959
    13
  val !!!! : (Token.T list -> 'a) -> Token.T 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@36959
    17
  val not_eof: Token.T parser
wenzelm@42326
    18
  val position: 'a parser -> ('a * Position.T) parser
wenzelm@42657
    19
  val source_position: 'a parser -> (Symbol_Pos.text * Position.T) parser
wenzelm@42326
    20
  val inner_syntax: 'a parser -> string parser
wenzelm@29310
    21
  val command: string parser
wenzelm@29310
    22
  val keyword: string parser
wenzelm@29310
    23
  val short_ident: string parser
wenzelm@29310
    24
  val long_ident: string parser
wenzelm@29310
    25
  val sym_ident: string parser
wenzelm@29310
    26
  val minus: string parser
wenzelm@29310
    27
  val term_var: string parser
wenzelm@29310
    28
  val type_ident: string parser
wenzelm@29310
    29
  val type_var: string parser
wenzelm@29310
    30
  val number: string parser
wenzelm@40290
    31
  val float_number: string parser
wenzelm@29310
    32
  val string: string parser
wenzelm@29310
    33
  val alt_string: string parser
wenzelm@29310
    34
  val verbatim: string parser
wenzelm@29310
    35
  val sync: string parser
wenzelm@29310
    36
  val eof: string parser
wenzelm@29310
    37
  val keyword_with: (string -> bool) -> string parser
wenzelm@29310
    38
  val keyword_ident_or_symbolic: string parser
wenzelm@29310
    39
  val $$$ : string -> string parser
wenzelm@29310
    40
  val reserved: string -> string parser
wenzelm@29310
    41
  val semicolon: string parser
wenzelm@29310
    42
  val underscore: string parser
wenzelm@29310
    43
  val maybe: 'a parser -> 'a option parser
wenzelm@29310
    44
  val tag_name: string parser
wenzelm@29310
    45
  val tags: string list parser
wenzelm@29310
    46
  val opt_unit: unit parser
wenzelm@29310
    47
  val opt_keyword: string -> bool parser
wenzelm@29310
    48
  val begin: string parser
wenzelm@29310
    49
  val opt_begin: bool parser
wenzelm@29310
    50
  val nat: int parser
wenzelm@29310
    51
  val int: int parser
wenzelm@40290
    52
  val real: real parser
wenzelm@29310
    53
  val enum: string -> 'a parser -> 'a list parser
wenzelm@29310
    54
  val enum1: string -> 'a parser -> 'a list parser
wenzelm@29310
    55
  val and_list: 'a parser -> 'a list parser
wenzelm@29310
    56
  val and_list1: 'a parser -> 'a list parser
wenzelm@30511
    57
  val enum': string -> 'a context_parser -> 'a list context_parser
wenzelm@30511
    58
  val enum1': string -> 'a context_parser -> 'a list context_parser
wenzelm@30511
    59
  val and_list': 'a context_parser -> 'a list context_parser
wenzelm@30511
    60
  val and_list1': 'a context_parser -> 'a list context_parser
wenzelm@29310
    61
  val list: 'a parser -> 'a list parser
wenzelm@29310
    62
  val list1: 'a parser -> 'a list parser
wenzelm@43775
    63
  val properties: Properties.T parser
wenzelm@29310
    64
  val name: bstring parser
haftmann@29581
    65
  val binding: binding parser
wenzelm@29310
    66
  val xname: xstring parser
wenzelm@29310
    67
  val text: string parser
wenzelm@29310
    68
  val path: Path.T parser
wenzelm@40800
    69
  val liberal_name: xstring parser
wenzelm@29310
    70
  val parname: string parser
haftmann@29581
    71
  val parbinding: binding parser
wenzelm@29310
    72
  val sort: string parser
wenzelm@29310
    73
  val arity: (string * string list * string) parser
wenzelm@29310
    74
  val multi_arity: (string list * string list * string) parser
wenzelm@29310
    75
  val type_args: string list parser
wenzelm@35838
    76
  val type_args_constrained: (string * string option) list parser
wenzelm@29310
    77
  val typ_group: string parser
wenzelm@29310
    78
  val typ: string parser
wenzelm@29310
    79
  val mixfix: mixfix parser
wenzelm@29310
    80
  val mixfix': mixfix parser
wenzelm@29310
    81
  val opt_mixfix: mixfix parser
wenzelm@29310
    82
  val opt_mixfix': mixfix parser
wenzelm@29310
    83
  val where_: string parser
wenzelm@42299
    84
  val const_decl: (string * string * mixfix) parser
wenzelm@30339
    85
  val const_binding: (binding * string * mixfix) parser
haftmann@29581
    86
  val params: (binding * string option) list parser
haftmann@29581
    87
  val simple_fixes: (binding * string option) list parser
haftmann@29581
    88
  val fixes: (binding * string option * mixfix) list parser
haftmann@29581
    89
  val for_fixes: (binding * string option * mixfix) list parser
haftmann@29581
    90
  val for_simple_fixes: (binding * string option) list parser
wenzelm@30573
    91
  val ML_source: (Symbol_Pos.text * Position.T) parser
wenzelm@30573
    92
  val doc_source: (Symbol_Pos.text * Position.T) parser
wenzelm@29310
    93
  val term_group: string parser
wenzelm@29310
    94
  val prop_group: string parser
wenzelm@29310
    95
  val term: string parser
wenzelm@29310
    96
  val prop: string parser
wenzelm@42300
    97
  val type_const: string parser
wenzelm@42300
    98
  val const: string parser
wenzelm@40793
    99
  val literal_fact: string parser
wenzelm@29310
   100
  val propp: (string * string list) parser
wenzelm@29310
   101
  val termp: (string * string list) parser
wenzelm@29310
   102
  val target: xstring parser
wenzelm@29310
   103
  val opt_target: xstring option parser
wenzelm@5826
   104
end;
wenzelm@5826
   105
wenzelm@36949
   106
structure Parse: PARSE =
wenzelm@5826
   107
struct
wenzelm@5826
   108
wenzelm@36959
   109
type 'a parser = Token.T list -> 'a * Token.T list;
wenzelm@36959
   110
type 'a context_parser = Context.generic * Token.T list -> 'a * (Context.generic * Token.T list);
wenzelm@29310
   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@43947
   118
  (fn [] => (fn () => s ^ " expected (past end-of-file!)")
wenzelm@42519
   119
    | tok :: _ =>
wenzelm@43947
   120
        (fn () =>
wenzelm@43947
   121
          (case Token.text_of tok of
wenzelm@43947
   122
            (txt, "") =>
wenzelm@43947
   123
              s ^ " expected,\nbut " ^ txt ^ Token.pos_of tok ^ " was found"
wenzelm@43947
   124
          | (txt1, txt2) =>
wenzelm@43947
   125
              s ^ " expected,\nbut " ^ txt1 ^ Token.pos_of tok ^ " was found:\n" ^ txt2)));
wenzelm@5826
   126
wenzelm@5826
   127
fun group s scan = scan || fail_with s;
wenzelm@5826
   128
wenzelm@5826
   129
wenzelm@5877
   130
(* cut *)
wenzelm@5826
   131
wenzelm@8581
   132
fun cut kind scan =
wenzelm@5826
   133
  let
wenzelm@5826
   134
    fun get_pos [] = " (past end-of-file!)"
wenzelm@36959
   135
      | get_pos (tok :: _) = Token.pos_of tok;
wenzelm@5826
   136
wenzelm@43947
   137
    fun err (toks, NONE) = (fn () => kind ^ get_pos toks)
wenzelm@25625
   138
      | err (toks, SOME msg) =
wenzelm@43947
   139
          (fn () =>
wenzelm@43947
   140
            let val s = msg () in
wenzelm@43947
   141
              if String.isPrefix kind s then s
wenzelm@43947
   142
              else kind ^ get_pos toks ^ ": " ^ s
wenzelm@43947
   143
            end);
wenzelm@5826
   144
  in Scan.!! err scan end;
wenzelm@5826
   145
wenzelm@8586
   146
fun !!! scan = cut "Outer syntax error" scan;
wenzelm@8586
   147
fun !!!! scan = cut "Corrupted outer syntax in presentation" scan;
wenzelm@8581
   148
wenzelm@5826
   149
wenzelm@5826
   150
wenzelm@5826
   151
(** basic parsers **)
wenzelm@5826
   152
wenzelm@5826
   153
(* utils *)
wenzelm@5826
   154
wenzelm@5826
   155
fun triple1 ((x, y), z) = (x, y, z);
wenzelm@5826
   156
fun triple2 (x, (y, z)) = (x, y, z);
wenzelm@6430
   157
fun triple_swap ((x, y), z) = ((x, z), y);
wenzelm@5826
   158
wenzelm@5826
   159
wenzelm@5826
   160
(* tokens *)
wenzelm@5826
   161
wenzelm@27815
   162
fun RESET_VALUE atom = (*required for all primitive parsers*)
wenzelm@36959
   163
  Scan.ahead (Scan.one (K true)) -- atom >> (fn (arg, x) => (Token.assign NONE arg; x));
wenzelm@27815
   164
wenzelm@27815
   165
wenzelm@36959
   166
val not_eof = RESET_VALUE (Scan.one Token.not_eof);
wenzelm@15703
   167
wenzelm@36959
   168
fun position scan = (Scan.ahead not_eof >> Token.position_of) -- scan >> Library.swap;
wenzelm@36959
   169
fun source_position atom = Scan.ahead atom |-- not_eof >> Token.source_position_of;
wenzelm@36959
   170
fun inner_syntax atom = Scan.ahead atom |-- not_eof >> Token.source_of;
wenzelm@5826
   171
wenzelm@5826
   172
fun kind k =
wenzelm@36959
   173
  group (Token.str_of_kind k) (RESET_VALUE (Scan.one (Token.is_kind k) >> Token.content_of));
wenzelm@5826
   174
wenzelm@36959
   175
val command = kind Token.Command;
wenzelm@36959
   176
val keyword = kind Token.Keyword;
wenzelm@36959
   177
val short_ident = kind Token.Ident;
wenzelm@36959
   178
val long_ident = kind Token.LongIdent;
wenzelm@36959
   179
val sym_ident = kind Token.SymIdent;
wenzelm@36959
   180
val term_var = kind Token.Var;
wenzelm@36959
   181
val type_ident = kind Token.TypeIdent;
wenzelm@36959
   182
val type_var = kind Token.TypeVar;
wenzelm@36959
   183
val number = kind Token.Nat;
wenzelm@40290
   184
val float_number = kind Token.Float;
wenzelm@36959
   185
val string = kind Token.String;
wenzelm@36959
   186
val alt_string = kind Token.AltString;
wenzelm@36959
   187
val verbatim = kind Token.Verbatim;
wenzelm@36959
   188
val sync = kind Token.Sync;
wenzelm@36959
   189
val eof = kind Token.EOF;
wenzelm@5826
   190
wenzelm@36959
   191
fun keyword_with pred = RESET_VALUE (Scan.one (Token.keyword_with pred) >> Token.content_of);
wenzelm@36959
   192
val keyword_ident_or_symbolic = keyword_with Token.ident_or_symbolic;
wenzelm@27815
   193
wenzelm@5826
   194
fun $$$ x =
wenzelm@36959
   195
  group (Token.str_of_kind Token.Keyword ^ " " ^ quote x) (keyword_with (fn y => x = y));
wenzelm@9131
   196
wenzelm@16030
   197
fun reserved x =
wenzelm@27753
   198
  group ("reserved identifier " ^ quote x)
wenzelm@36959
   199
    (RESET_VALUE (Scan.one (Token.ident_with (fn y => x = y)) >> Token.content_of));
wenzelm@16030
   200
wenzelm@9131
   201
val semicolon = $$$ ";";
wenzelm@5826
   202
wenzelm@15703
   203
val minus = sym_ident :-- (fn "-" => Scan.succeed () | _ => Scan.fail) >> #1;
wenzelm@11792
   204
val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1;
wenzelm@15703
   205
fun maybe scan = underscore >> K NONE || scan >> SOME;
wenzelm@11792
   206
wenzelm@14835
   207
val nat = number >> (#1 o Library.read_int o Symbol.explode);
wenzelm@27815
   208
val int = Scan.optional (minus >> K ~1) 1 -- nat >> op *;
wenzelm@40296
   209
val real = float_number >> (the o Real.fromString) || int >> Real.fromInt;
wenzelm@5826
   210
wenzelm@17070
   211
val tag_name = group "tag name" (short_ident || string);
wenzelm@17070
   212
val tags = Scan.repeat ($$$ "%" |-- !!! tag_name);
wenzelm@17070
   213
wenzelm@7930
   214
val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
wenzelm@14646
   215
fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false;
wenzelm@14646
   216
wenzelm@20983
   217
val begin = $$$ "begin";
wenzelm@20983
   218
val opt_begin = Scan.optional (begin >> K true) false;
wenzelm@20961
   219
wenzelm@5826
   220
wenzelm@5826
   221
(* enumerations *)
wenzelm@5826
   222
wenzelm@25999
   223
fun enum1 sep scan = scan ::: Scan.repeat ($$$ sep |-- !!! scan);
wenzelm@5826
   224
fun enum sep scan = enum1 sep scan || Scan.succeed [];
wenzelm@5826
   225
wenzelm@27815
   226
fun enum1' sep scan = scan ::: Scan.repeat (Scan.lift ($$$ sep) |-- scan);
wenzelm@27815
   227
fun enum' sep scan = enum1' sep scan || Scan.succeed [];
wenzelm@5826
   228
wenzelm@6013
   229
fun and_list1 scan = enum1 "and" scan;
wenzelm@6013
   230
fun and_list scan = enum "and" scan;
wenzelm@6013
   231
wenzelm@27815
   232
fun and_list1' scan = enum1' "and" scan;
wenzelm@27815
   233
fun and_list' scan = enum' "and" scan;
wenzelm@27815
   234
wenzelm@27815
   235
fun list1 scan = enum1 "," scan;
wenzelm@27815
   236
fun list scan = enum "," scan;
wenzelm@27815
   237
wenzelm@43775
   238
val properties = $$$ "(" |-- !!! (list (string -- ($$$ "=" |-- string)) --| $$$ ")");
wenzelm@43775
   239
wenzelm@5826
   240
wenzelm@5960
   241
(* names and text *)
wenzelm@5826
   242
wenzelm@8146
   243
val name = group "name declaration" (short_ident || sym_ident || string || number);
wenzelm@30223
   244
val binding = position name >> Binding.make;
wenzelm@8146
   245
val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number);
wenzelm@8146
   246
val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim);
wenzelm@21858
   247
val path = group "file name/path specification" name >> Path.explode;
wenzelm@6553
   248
wenzelm@40800
   249
val liberal_name = keyword_ident_or_symbolic || xname;
wenzelm@40800
   250
wenzelm@18898
   251
val parname = Scan.optional ($$$ "(" |-- name --| $$$ ")") "";
haftmann@28965
   252
val parbinding = Scan.optional ($$$ "(" |-- binding --| $$$ ")") Binding.empty;
wenzelm@18898
   253
wenzelm@6553
   254
wenzelm@6372
   255
(* sorts and arities *)
wenzelm@5826
   256
wenzelm@27753
   257
val sort = group "sort" (inner_syntax xname);
wenzelm@5826
   258
wenzelm@22331
   259
val arity = xname -- ($$$ "::" |-- !!!
wenzelm@22331
   260
  (Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2;
wenzelm@5826
   261
haftmann@25541
   262
val multi_arity = and_list1 xname -- ($$$ "::" |-- !!!
haftmann@25541
   263
  (Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2;
haftmann@25541
   264
wenzelm@5826
   265
wenzelm@5826
   266
(* types *)
wenzelm@5826
   267
wenzelm@27753
   268
val typ_group = group "type"
wenzelm@8146
   269
  (short_ident || long_ident || sym_ident || type_ident || type_var || string || number);
wenzelm@5826
   270
wenzelm@27753
   271
val typ = inner_syntax typ_group;
wenzelm@27753
   272
wenzelm@35838
   273
fun type_arguments arg =
wenzelm@35838
   274
  arg >> single ||
wenzelm@35838
   275
  $$$ "(" |-- !!! (list1 arg --| $$$ ")") ||
wenzelm@5826
   276
  Scan.succeed [];
wenzelm@5826
   277
wenzelm@35838
   278
val type_args = type_arguments type_ident;
wenzelm@35838
   279
val type_args_constrained = type_arguments (type_ident -- Scan.option ($$$ "::" |-- !!! sort));
wenzelm@35838
   280
wenzelm@5826
   281
wenzelm@5826
   282
(* mixfix annotations *)
wenzelm@5826
   283
wenzelm@18669
   284
val mfix = string --
wenzelm@18669
   285
  !!! (Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [] --
wenzelm@42297
   286
    Scan.optional nat 1000) >> (Mixfix o triple2);
wenzelm@18669
   287
wenzelm@35130
   288
val infx = $$$ "infix" |-- !!! (string -- nat >> Infix);
wenzelm@35130
   289
val infxl = $$$ "infixl" |-- !!! (string -- nat >> Infixl);
wenzelm@35130
   290
val infxr = $$$ "infixr" |-- !!! (string -- nat >> Infixr);
wenzelm@5826
   291
wenzelm@18669
   292
val binder = $$$ "binder" |--
wenzelm@18669
   293
  !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
wenzelm@18669
   294
  >> (Binder o triple2);
wenzelm@18669
   295
wenzelm@18669
   296
fun annotation guard fix = $$$ "(" |-- guard (fix --| $$$ ")");
wenzelm@18669
   297
fun opt_annotation guard fix = Scan.optional (annotation guard fix) NoSyn;
wenzelm@18669
   298
wenzelm@18669
   299
val mixfix = annotation !!! (mfix || binder || infxl || infxr || infx);
wenzelm@21609
   300
val mixfix' = annotation I (mfix || binder || infxl || infxr || infx);
wenzelm@18669
   301
val opt_mixfix = opt_annotation !!! (mfix || binder || infxl || infxr || infx);
wenzelm@18669
   302
val opt_mixfix' = opt_annotation I (mfix || binder || infxl || infxr || infx);
wenzelm@5826
   303
wenzelm@5826
   304
wenzelm@18669
   305
(* fixes *)
wenzelm@5826
   306
wenzelm@21400
   307
val where_ = $$$ "where";
wenzelm@21400
   308
wenzelm@42299
   309
val const_decl = name -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1;
wenzelm@30339
   310
val const_binding = binding -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1;
wenzelm@18669
   311
wenzelm@28081
   312
val params = Scan.repeat1 binding -- Scan.option ($$$ "::" |-- !!! typ)
wenzelm@18669
   313
  >> (fn (xs, T) => map (rpair T) xs);
wenzelm@18669
   314
wenzelm@19482
   315
val simple_fixes = and_list1 params >> flat;
wenzelm@18669
   316
wenzelm@18669
   317
val fixes =
wenzelm@28081
   318
  and_list1 (binding -- Scan.option ($$$ "::" |-- typ) -- mixfix >> (single o triple1) ||
wenzelm@42287
   319
    params >> map (fn (x, y) => (x, y, NoSyn))) >> flat;
wenzelm@5826
   320
wenzelm@19845
   321
val for_fixes = Scan.optional ($$$ "for" |-- !!! fixes) [];
wenzelm@21371
   322
val for_simple_fixes = Scan.optional ($$$ "for" |-- !!! simple_fixes) [];
wenzelm@19845
   323
wenzelm@5826
   324
wenzelm@27877
   325
(* embedded source text *)
wenzelm@27872
   326
wenzelm@27877
   327
val ML_source = source_position (group "ML source" text);
wenzelm@27872
   328
val doc_source = source_position (group "document source" text);
wenzelm@27872
   329
wenzelm@27872
   330
wenzelm@5826
   331
(* terms *)
wenzelm@5826
   332
wenzelm@40793
   333
val tm = short_ident || long_ident || sym_ident || term_var || number || string;
wenzelm@5826
   334
wenzelm@40793
   335
val term_group = group "term" tm;
wenzelm@40793
   336
val prop_group = group "proposition" tm;
wenzelm@27753
   337
wenzelm@27753
   338
val term = inner_syntax term_group;
wenzelm@27753
   339
val prop = inner_syntax prop_group;
wenzelm@5826
   340
wenzelm@42300
   341
val type_const = inner_syntax (group "type constructor" xname);
wenzelm@42300
   342
val const = inner_syntax (group "constant" xname);
wenzelm@42300
   343
wenzelm@40793
   344
val literal_fact = inner_syntax (group "literal fact" alt_string);
wenzelm@40793
   345
wenzelm@5826
   346
wenzelm@6949
   347
(* patterns *)
wenzelm@6935
   348
wenzelm@6949
   349
val is_terms = Scan.repeat1 ($$$ "is" |-- term);
wenzelm@6935
   350
val is_props = Scan.repeat1 ($$$ "is" |-- prop);
wenzelm@6935
   351
wenzelm@19585
   352
val propp = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) [];
wenzelm@6949
   353
val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
wenzelm@6935
   354
wenzelm@6935
   355
wenzelm@22119
   356
(* targets *)
wenzelm@19811
   357
wenzelm@22119
   358
val target = ($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")");
wenzelm@22119
   359
val opt_target = Scan.option target;
wenzelm@12272
   360
wenzelm@12272
   361
end;
wenzelm@30511
   362
wenzelm@36949
   363
type 'a parser = 'a Parse.parser;
wenzelm@36949
   364
type 'a context_parser = 'a Parse.context_parser;
wenzelm@30511
   365