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