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