src/Pure/Isar/outer_parse.ML
author haftmann
Thu May 24 08:37:37 2007 +0200 (2007-05-24)
changeset 23086 12320f6e2523
parent 22331 7df6bc8cf0b0
child 23789 1993b865c5ac
permissions -rw-r--r--
tuned Pure/General/name_space.ML
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@22119
    30
  val alt_string: token list -> string * token list
wenzelm@5826
    31
  val verbatim: token list -> string * token list
wenzelm@6860
    32
  val sync: token list -> string * token list
wenzelm@5826
    33
  val eof: token list -> string * token list
wenzelm@15703
    34
  val $$$ : string -> token list -> string * token list
wenzelm@16030
    35
  val reserved : string -> token list -> string * token list
wenzelm@15703
    36
  val semicolon: token list -> string * token list
wenzelm@15703
    37
  val underscore: token list -> string * token list
wenzelm@15703
    38
  val maybe: (token list -> 'a * token list) -> token list -> 'a option * token list
wenzelm@17070
    39
  val tag_name: token list -> string * token list
wenzelm@17070
    40
  val tags: token list -> string list * token list
wenzelm@7930
    41
  val opt_unit: token list -> unit * token list
wenzelm@14646
    42
  val opt_keyword: string -> token list -> bool * token list
wenzelm@20983
    43
  val begin: token list -> string * token list
wenzelm@20961
    44
  val opt_begin: token list -> bool * token list
wenzelm@5826
    45
  val nat: token list -> int * token list
wenzelm@5826
    46
  val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    47
  val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    48
  val list: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    49
  val list1: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@6013
    50
  val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@6013
    51
  val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    52
  val name: token list -> bstring * token list
wenzelm@5826
    53
  val xname: token list -> xstring * token list
wenzelm@5826
    54
  val text: token list -> string * token list
wenzelm@14949
    55
  val path: token list -> Path.T * token list
wenzelm@18898
    56
  val parname: token list -> string * token list
wenzelm@8897
    57
  val sort: token list -> string * token list
wenzelm@22331
    58
  val arity: token list -> (string * string list * string) * token list
wenzelm@5826
    59
  val type_args: token list -> string list * token list
wenzelm@5826
    60
  val typ: token list -> string * token list
wenzelm@18669
    61
  val mixfix: token list -> mixfix * token list
wenzelm@21609
    62
  val mixfix': token list -> mixfix * token list
wenzelm@18669
    63
  val opt_infix: token list -> mixfix * token list
wenzelm@21609
    64
  val opt_infix': token list -> mixfix * token list
wenzelm@18669
    65
  val opt_mixfix: token list -> mixfix * token list
wenzelm@18669
    66
  val opt_mixfix': token list -> mixfix * token list
wenzelm@21400
    67
  val where_: token list -> string * token list
wenzelm@18669
    68
  val const: token list -> (string * string * mixfix) * token list
wenzelm@22119
    69
  val params: token list -> (string * string option) list * token list
wenzelm@19845
    70
  val simple_fixes: token list -> (string * string option) list * token list
wenzelm@18669
    71
  val fixes: token list -> (string * string option * mixfix) list * token list
wenzelm@19845
    72
  val for_fixes: token list -> (string * string option * mixfix) list * token list
wenzelm@21371
    73
  val for_simple_fixes: token list -> (string * string option) list * token list
wenzelm@5826
    74
  val term: token list -> string * token list
wenzelm@5826
    75
  val prop: token list -> string * token list
wenzelm@19585
    76
  val propp: token list -> (string * string list) * token list
wenzelm@6949
    77
  val termp: token list -> (string * string list) * token list
wenzelm@22119
    78
  val keyword_sid: token list -> string * token list
wenzelm@22119
    79
  val args: (string -> bool) -> bool -> token list -> Args.T list * token list
wenzelm@22119
    80
  val args1: (string -> bool) -> bool -> token list -> Args.T list * token list
wenzelm@9131
    81
  val arguments: token list -> Args.T list * token list
wenzelm@22119
    82
  val target: token list -> xstring * token list
wenzelm@22119
    83
  val opt_target: token list -> xstring option * token list
wenzelm@5826
    84
end;
wenzelm@5826
    85
wenzelm@5826
    86
structure OuterParse: OUTER_PARSE =
wenzelm@5826
    87
struct
wenzelm@5826
    88
wenzelm@9131
    89
structure T = OuterLex;
wenzelm@9131
    90
type token = T.token;
wenzelm@5826
    91
wenzelm@5826
    92
wenzelm@5826
    93
(** error handling **)
wenzelm@5826
    94
wenzelm@5826
    95
(* group atomic parsers (no cuts!) *)
wenzelm@5826
    96
wenzelm@5826
    97
fun fail_with s = Scan.fail_with
wenzelm@5826
    98
  (fn [] => s ^ " expected (past end-of-file!)"
wenzelm@9155
    99
    | (tok :: _) => s ^ " expected,\nbut " ^ T.name_of tok ^ T.pos_of tok ^ " was found");
wenzelm@5826
   100
wenzelm@5826
   101
fun group s scan = scan || fail_with s;
wenzelm@5826
   102
wenzelm@5826
   103
wenzelm@5877
   104
(* cut *)
wenzelm@5826
   105
wenzelm@8581
   106
fun cut kind scan =
wenzelm@5826
   107
  let
wenzelm@5826
   108
    fun get_pos [] = " (past end-of-file!)"
wenzelm@9131
   109
      | get_pos (tok :: _) = T.pos_of tok;
wenzelm@5826
   110
skalberg@15531
   111
    fun err (toks, NONE) = kind ^ get_pos toks
skalberg@15531
   112
      | err (toks, SOME msg) = kind ^ get_pos toks ^ ": " ^ msg;
wenzelm@5826
   113
  in Scan.!! err scan end;
wenzelm@5826
   114
wenzelm@8586
   115
fun !!! scan = cut "Outer syntax error" scan;
wenzelm@8586
   116
fun !!!! scan = cut "Corrupted outer syntax in presentation" scan;
wenzelm@8581
   117
wenzelm@5826
   118
wenzelm@5826
   119
wenzelm@5826
   120
(** basic parsers **)
wenzelm@5826
   121
wenzelm@5826
   122
(* utils *)
wenzelm@5826
   123
wenzelm@5826
   124
fun triple1 ((x, y), z) = (x, y, z);
wenzelm@5826
   125
fun triple2 (x, (y, z)) = (x, y, z);
wenzelm@6430
   126
fun triple_swap ((x, y), z) = ((x, z), y);
wenzelm@5826
   127
wenzelm@5826
   128
wenzelm@5826
   129
(* tokens *)
wenzelm@5826
   130
wenzelm@15703
   131
val not_eof = Scan.one T.not_eof;
wenzelm@15703
   132
wenzelm@15703
   133
fun position scan = (Scan.ahead not_eof >> T.position_of) -- scan >> Library.swap;
wenzelm@5826
   134
wenzelm@5826
   135
fun kind k =
wenzelm@9131
   136
  group (T.str_of_kind k) (Scan.one (T.is_kind k) >> T.val_of);
wenzelm@5826
   137
wenzelm@9131
   138
val command = kind T.Command;
wenzelm@9131
   139
val keyword = kind T.Keyword;
wenzelm@9131
   140
val short_ident = kind T.Ident;
wenzelm@9131
   141
val long_ident = kind T.LongIdent;
wenzelm@9131
   142
val sym_ident = kind T.SymIdent;
wenzelm@9131
   143
val term_var = kind T.Var;
wenzelm@9131
   144
val type_ident = kind T.TypeIdent;
wenzelm@9131
   145
val type_var = kind T.TypeVar;
wenzelm@9131
   146
val number = kind T.Nat;
wenzelm@9131
   147
val string = kind T.String;
wenzelm@17165
   148
val alt_string = kind T.AltString;
wenzelm@9131
   149
val verbatim = kind T.Verbatim;
wenzelm@9131
   150
val sync = kind T.Sync;
wenzelm@9131
   151
val eof = kind T.EOF;
wenzelm@5826
   152
wenzelm@5826
   153
fun $$$ x =
wenzelm@9131
   154
  group (T.str_of_kind T.Keyword ^ " " ^ quote x)
wenzelm@9131
   155
    (Scan.one (T.keyword_with (equal x)) >> T.val_of);
wenzelm@9131
   156
wenzelm@16030
   157
fun reserved x =
wenzelm@16030
   158
  group ("Reserved identifier " ^ quote x)
wenzelm@16030
   159
    (Scan.one (T.ident_with (equal x)) >> T.val_of);
wenzelm@16030
   160
wenzelm@9131
   161
val semicolon = $$$ ";";
wenzelm@5826
   162
wenzelm@15703
   163
val minus = sym_ident :-- (fn "-" => Scan.succeed () | _ => Scan.fail) >> #1;
wenzelm@11792
   164
val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1;
wenzelm@15703
   165
fun maybe scan = underscore >> K NONE || scan >> SOME;
wenzelm@11792
   166
wenzelm@14835
   167
val nat = number >> (#1 o Library.read_int o Symbol.explode);
wenzelm@5826
   168
wenzelm@17070
   169
val tag_name = group "tag name" (short_ident || string);
wenzelm@17070
   170
val tags = Scan.repeat ($$$ "%" |-- !!! tag_name);
wenzelm@17070
   171
wenzelm@7930
   172
val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
wenzelm@14646
   173
fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false;
wenzelm@14646
   174
wenzelm@20983
   175
val begin = $$$ "begin";
wenzelm@20983
   176
val opt_begin = Scan.optional (begin >> K true) false;
wenzelm@20961
   177
wenzelm@5826
   178
wenzelm@5826
   179
(* enumerations *)
wenzelm@5826
   180
wenzelm@6003
   181
fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::;
wenzelm@5826
   182
fun enum sep scan = enum1 sep scan || Scan.succeed [];
wenzelm@5826
   183
wenzelm@5826
   184
fun list1 scan = enum1 "," scan;
wenzelm@5826
   185
fun list scan = enum "," scan;
wenzelm@5826
   186
wenzelm@6013
   187
fun and_list1 scan = enum1 "and" scan;
wenzelm@6013
   188
fun and_list scan = enum "and" scan;
wenzelm@6013
   189
wenzelm@5826
   190
wenzelm@5960
   191
(* names and text *)
wenzelm@5826
   192
wenzelm@8146
   193
val name = group "name declaration" (short_ident || sym_ident || string || number);
wenzelm@8146
   194
val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number);
wenzelm@8146
   195
val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim);
wenzelm@21858
   196
val path = group "file name/path specification" name >> Path.explode;
wenzelm@6553
   197
wenzelm@18898
   198
val parname = Scan.optional ($$$ "(" |-- name --| $$$ ")") "";
wenzelm@18898
   199
wenzelm@6553
   200
wenzelm@6372
   201
(* sorts and arities *)
wenzelm@5826
   202
wenzelm@8897
   203
val sort = group "sort" xname;
wenzelm@5826
   204
wenzelm@22331
   205
val arity = xname -- ($$$ "::" |-- !!!
wenzelm@22331
   206
  (Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2;
wenzelm@5826
   207
wenzelm@5826
   208
wenzelm@5826
   209
(* types *)
wenzelm@5826
   210
wenzelm@8146
   211
val typ = group "type"
wenzelm@8146
   212
  (short_ident || long_ident || sym_ident || type_ident || type_var || string || number);
wenzelm@5826
   213
wenzelm@5826
   214
val type_args =
wenzelm@5826
   215
  type_ident >> single ||
wenzelm@5826
   216
  $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
wenzelm@5826
   217
  Scan.succeed [];
wenzelm@5826
   218
wenzelm@5826
   219
wenzelm@5826
   220
(* mixfix annotations *)
wenzelm@5826
   221
wenzelm@18669
   222
val mfix = string --
wenzelm@18669
   223
  !!! (Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [] --
wenzelm@18669
   224
    Scan.optional nat Syntax.max_pri) >> (Mixfix o triple2);
wenzelm@18669
   225
wenzelm@18669
   226
val infx = $$$ "infix" |-- !!! (nat >> Infix || string -- nat >> InfixName);
wenzelm@18669
   227
val infxl = $$$ "infixl" |-- !!! (nat >> Infixl || string -- nat >> InfixlName);
wenzelm@18669
   228
val infxr = $$$ "infixr" |-- !!! (nat >> Infixr || string -- nat >> InfixrName);
wenzelm@5826
   229
wenzelm@18669
   230
val binder = $$$ "binder" |--
wenzelm@18669
   231
  !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
wenzelm@18669
   232
  >> (Binder o triple2);
wenzelm@18669
   233
wenzelm@18669
   234
fun annotation guard fix = $$$ "(" |-- guard (fix --| $$$ ")");
wenzelm@18669
   235
fun opt_annotation guard fix = Scan.optional (annotation guard fix) NoSyn;
wenzelm@18669
   236
wenzelm@18669
   237
val mixfix = annotation !!! (mfix || binder || infxl || infxr || infx);
wenzelm@21609
   238
val mixfix' = annotation I (mfix || binder || infxl || infxr || infx);
wenzelm@18669
   239
val opt_infix = opt_annotation !!! (infxl || infxr || infx);
wenzelm@21609
   240
val opt_infix' = opt_annotation I (infxl || infxr || infx);
wenzelm@18669
   241
val opt_mixfix = opt_annotation !!! (mfix || binder || infxl || infxr || infx);
wenzelm@18669
   242
val opt_mixfix' = opt_annotation I (mfix || binder || infxl || infxr || infx);
wenzelm@5826
   243
wenzelm@5826
   244
wenzelm@18669
   245
(* fixes *)
wenzelm@5826
   246
wenzelm@21400
   247
val where_ = $$$ "where";
wenzelm@21400
   248
wenzelm@18618
   249
val const = name -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1;
wenzelm@18669
   250
wenzelm@18669
   251
val params = Scan.repeat1 name -- Scan.option ($$$ "::" |-- !!! typ)
wenzelm@18669
   252
  >> (fn (xs, T) => map (rpair T) xs);
wenzelm@18669
   253
wenzelm@19482
   254
val simple_fixes = and_list1 params >> flat;
wenzelm@18669
   255
wenzelm@18669
   256
val fixes =
wenzelm@18669
   257
  and_list1 (name -- Scan.option ($$$ "::" |-- typ) -- mixfix >> (single o triple1) ||
wenzelm@19482
   258
    params >> map Syntax.no_syn) >> flat;
wenzelm@5826
   259
wenzelm@19845
   260
val for_fixes = Scan.optional ($$$ "for" |-- !!! fixes) [];
wenzelm@21371
   261
val for_simple_fixes = Scan.optional ($$$ "for" |-- !!! simple_fixes) [];
wenzelm@19845
   262
wenzelm@5826
   263
wenzelm@5826
   264
(* terms *)
wenzelm@5826
   265
wenzelm@7477
   266
val trm = short_ident || long_ident || sym_ident || term_var || number || string;
wenzelm@5826
   267
wenzelm@5826
   268
val term = group "term" trm;
wenzelm@5826
   269
val prop = group "proposition" trm;
wenzelm@5826
   270
wenzelm@5826
   271
wenzelm@6949
   272
(* patterns *)
wenzelm@6935
   273
wenzelm@6949
   274
val is_terms = Scan.repeat1 ($$$ "is" |-- term);
wenzelm@6935
   275
val is_props = Scan.repeat1 ($$$ "is" |-- prop);
wenzelm@6935
   276
wenzelm@19585
   277
val propp = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) [];
wenzelm@6949
   278
val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
wenzelm@6935
   279
wenzelm@6935
   280
wenzelm@5826
   281
(* arguments *)
wenzelm@5826
   282
wenzelm@9131
   283
fun keyword_symid is_symid = Scan.one (T.keyword_with is_symid) >> T.val_of;
wenzelm@9131
   284
val keyword_sid = keyword_symid T.is_sid;
wenzelm@5826
   285
wenzelm@6983
   286
fun atom_arg is_symid blk =
wenzelm@5826
   287
  group "argument"
wenzelm@7477
   288
    (position (short_ident || long_ident || sym_ident || term_var ||
wenzelm@15703
   289
        type_ident || type_var || number) >> Args.mk_ident ||
wenzelm@15703
   290
      position (keyword_symid is_symid) >> Args.mk_keyword ||
wenzelm@15703
   291
      position (string || verbatim) >> Args.mk_string ||
wenzelm@18037
   292
      position alt_string >> Args.mk_alt_string ||
wenzelm@15703
   293
      position (if blk then $$$ "," else Scan.fail) >> Args.mk_keyword);
wenzelm@5826
   294
wenzelm@5877
   295
fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r))
wenzelm@15703
   296
  >> (fn (x, (ys, z)) => Args.mk_keyword x :: ys @ [Args.mk_keyword z]);
wenzelm@5826
   297
wenzelm@6983
   298
fun args is_symid blk x = Scan.optional (args1 is_symid blk) [] x
wenzelm@6983
   299
and args1 is_symid blk x =
wenzelm@5826
   300
  ((Scan.repeat1
wenzelm@6983
   301
    (Scan.repeat1 (atom_arg is_symid blk) ||
wenzelm@6983
   302
      paren_args "(" ")" (args is_symid) ||
wenzelm@19482
   303
      paren_args "[" "]" (args is_symid))) >> flat) x;
wenzelm@5826
   304
wenzelm@9131
   305
val arguments = args T.is_sid false;
wenzelm@9131
   306
wenzelm@5826
   307
wenzelm@22119
   308
(* targets *)
wenzelm@19811
   309
wenzelm@22119
   310
val target = ($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")");
wenzelm@22119
   311
val opt_target = Scan.option target;
wenzelm@12272
   312
wenzelm@12272
   313
end;