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