src/Pure/Isar/outer_parse.ML
changeset 9131 cd17637c917f
parent 9037 91cbae314c84
child 9155 adfa40218e06
equal deleted inserted replaced
9130:ff8789b49d2e 9131:cd17637c917f
    11   type token
    11   type token
    12   val group: string -> (token list -> 'a) -> token list -> 'a
    12   val group: string -> (token list -> 'a) -> token list -> 'a
    13   val !!! : (token list -> 'a) -> token list -> 'a
    13   val !!! : (token list -> 'a) -> token list -> 'a
    14   val !!!! : (token list -> 'a) -> token list -> 'a
    14   val !!!! : (token list -> 'a) -> token list -> 'a
    15   val $$$ : string -> token list -> string * token list
    15   val $$$ : string -> token list -> string * token list
       
    16   val semicolon: token list -> string * token list
    16   val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
    17   val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
    17   val command: token list -> string * token list
    18   val command: token list -> string * token list
    18   val keyword: token list -> string * token list
    19   val keyword: token list -> string * token list
    19   val short_ident: token list -> string * token list
    20   val short_ident: token list -> string * token list
    20   val long_ident: token list -> string * token list
    21   val long_ident: token list -> string * token list
    54   val const: token list -> (bstring * string * Syntax.mixfix) * token list
    55   val const: token list -> (bstring * string * Syntax.mixfix) * token list
    55   val term: token list -> string * token list
    56   val term: token list -> string * token list
    56   val prop: token list -> string * token list
    57   val prop: token list -> string * token list
    57   val propp: token list -> (string * (string list * string list)) * token list
    58   val propp: token list -> (string * (string list * string list)) * token list
    58   val termp: token list -> (string * string list) * token list
    59   val termp: token list -> (string * string list) * token list
       
    60   val arguments: token list -> Args.T list * token list
    59   val attribs: token list -> Args.src list * token list
    61   val attribs: token list -> Args.src list * token list
    60   val opt_attribs: token list -> Args.src list * token list
    62   val opt_attribs: token list -> Args.src list * token list
    61   val thm_name: string -> token list -> (bstring * Args.src list) * token list
    63   val thm_name: string -> token list -> (bstring * Args.src list) * token list
    62   val opt_thm_name: string -> token list -> (bstring * Args.src list) * token list
    64   val opt_thm_name: string -> token list -> (bstring * Args.src list) * token list
    63   val spec_name: token list -> ((bstring * string) * Args.src list) * token list
    65   val spec_name: token list -> ((bstring * string) * Args.src list) * token list
    71 end;
    73 end;
    72 
    74 
    73 structure OuterParse: OUTER_PARSE =
    75 structure OuterParse: OUTER_PARSE =
    74 struct
    76 struct
    75 
    77 
    76 type token = OuterLex.token;
    78 structure T = OuterLex;
       
    79 type token = T.token;
    77 
    80 
    78 
    81 
    79 (** error handling **)
    82 (** error handling **)
    80 
    83 
    81 (* group atomic parsers (no cuts!) *)
    84 (* group atomic parsers (no cuts!) *)
    82 
    85 
    83 fun fail_with s = Scan.fail_with
    86 fun fail_with s = Scan.fail_with
    84   (fn [] => s ^ " expected (past end-of-file!)"
    87   (fn [] => s ^ " expected (past end-of-file!)"
    85     | (tok :: _) => s ^ " expected,\nbut " ^ OuterLex.name_of tok ^ " " ^
    88     | (tok :: _) => s ^ " expected,\nbut " ^ T.name_of tok ^ " " ^
    86       quote (OuterLex.val_of tok) ^ OuterLex.pos_of tok ^ " was found");
    89       quote (T.val_of tok) ^ T.pos_of tok ^ " was found");
    87 
    90 
    88 fun group s scan = scan || fail_with s;
    91 fun group s scan = scan || fail_with s;
    89 
    92 
    90 
    93 
    91 (* cut *)
    94 (* cut *)
    92 
    95 
    93 fun cut kind scan =
    96 fun cut kind scan =
    94   let
    97   let
    95     fun get_pos [] = " (past end-of-file!)"
    98     fun get_pos [] = " (past end-of-file!)"
    96       | get_pos (tok :: _) = OuterLex.pos_of tok;
    99       | get_pos (tok :: _) = T.pos_of tok;
    97 
   100 
    98     fun err (toks, None) = kind ^ get_pos toks
   101     fun err (toks, None) = kind ^ get_pos toks
    99       | err (toks, Some msg) = kind ^ get_pos toks ^ ": " ^ msg;
   102       | err (toks, Some msg) = kind ^ get_pos toks ^ ": " ^ msg;
   100   in Scan.!! err scan end;
   103   in Scan.!! err scan end;
   101 
   104 
   114 
   117 
   115 
   118 
   116 (* tokens *)
   119 (* tokens *)
   117 
   120 
   118 fun position scan =
   121 fun position scan =
   119   (Scan.ahead (Scan.one OuterLex.not_eof) >> OuterLex.position_of) -- scan >> Library.swap;
   122   (Scan.ahead (Scan.one T.not_eof) >> T.position_of) -- scan >> Library.swap;
   120 
   123 
   121 fun kind k =
   124 fun kind k =
   122   group (OuterLex.str_of_kind k)
   125   group (T.str_of_kind k) (Scan.one (T.is_kind k) >> T.val_of);
   123     (Scan.one (OuterLex.is_kind k) >> OuterLex.val_of);
   126 
   124 
   127 val command = kind T.Command;
   125 val command = kind OuterLex.Command;
   128 val keyword = kind T.Keyword;
   126 val keyword = kind OuterLex.Keyword;
   129 val short_ident = kind T.Ident;
   127 val short_ident = kind OuterLex.Ident;
   130 val long_ident = kind T.LongIdent;
   128 val long_ident = kind OuterLex.LongIdent;
   131 val sym_ident = kind T.SymIdent;
   129 val sym_ident = kind OuterLex.SymIdent;
   132 val term_var = kind T.Var;
   130 val term_var = kind OuterLex.Var;
   133 val type_ident = kind T.TypeIdent;
   131 val type_ident = kind OuterLex.TypeIdent;
   134 val type_var = kind T.TypeVar;
   132 val type_var = kind OuterLex.TypeVar;
   135 val number = kind T.Nat;
   133 val number = kind OuterLex.Nat;
   136 val string = kind T.String;
   134 val string = kind OuterLex.String;
   137 val verbatim = kind T.Verbatim;
   135 val verbatim = kind OuterLex.Verbatim;
   138 val sync = kind T.Sync;
   136 val sync = kind OuterLex.Sync;
   139 val eof = kind T.EOF;
   137 val eof = kind OuterLex.EOF;
       
   138 
   140 
   139 fun $$$ x =
   141 fun $$$ x =
   140   group (OuterLex.str_of_kind OuterLex.Keyword ^ " " ^ quote x)
   142   group (T.str_of_kind T.Keyword ^ " " ^ quote x)
   141     (Scan.one (OuterLex.keyword_with (equal x)) >> OuterLex.val_of);
   143     (Scan.one (T.keyword_with (equal x)) >> T.val_of);
       
   144 
       
   145 val semicolon = $$$ ";";
   142 
   146 
   143 val nat = number >> (fst o Term.read_int o Symbol.explode);
   147 val nat = number >> (fst o Term.read_int o Symbol.explode);
   144 
   148 
   145 val not_eof = Scan.one OuterLex.not_eof;
   149 val not_eof = Scan.one T.not_eof;
   146 
   150 
   147 val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
   151 val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
   148 
   152 
   149 
   153 
   150 (* enumerations *)
   154 (* enumerations *)
   249 val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
   253 val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
   250 
   254 
   251 
   255 
   252 (* arguments *)
   256 (* arguments *)
   253 
   257 
   254 fun keyword_symid is_symid = Scan.one (OuterLex.keyword_with is_symid) >> OuterLex.val_of;
   258 fun keyword_symid is_symid = Scan.one (T.keyword_with is_symid) >> T.val_of;
   255 val keyword_sid = keyword_symid OuterLex.is_sid;
   259 val keyword_sid = keyword_symid T.is_sid;
   256 
   260 
   257 fun atom_arg is_symid blk =
   261 fun atom_arg is_symid blk =
   258   group "argument"
   262   group "argument"
   259     (position (short_ident || long_ident || sym_ident || term_var ||
   263     (position (short_ident || long_ident || sym_ident || term_var ||
   260         type_ident || type_var || number) >> Args.ident ||
   264         type_ident || type_var || number) >> Args.ident ||
   270   ((Scan.repeat1
   274   ((Scan.repeat1
   271     (Scan.repeat1 (atom_arg is_symid blk) ||
   275     (Scan.repeat1 (atom_arg is_symid blk) ||
   272       paren_args "(" ")" (args is_symid) ||
   276       paren_args "(" ")" (args is_symid) ||
   273       paren_args "[" "]" (args is_symid))) >> flat) x;
   277       paren_args "[" "]" (args is_symid))) >> flat) x;
   274 
   278 
       
   279 val arguments = args T.is_sid false;
       
   280 
   275 
   281 
   276 (* theorem specifications *)
   282 (* theorem specifications *)
   277 
   283 
   278 val attrib = position ((keyword_sid || xname) -- !!! (args OuterLex.is_sid false)) >> Args.src;
   284 val attrib = position ((keyword_sid || xname) -- !!! arguments) >> Args.src;
   279 val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
   285 val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
   280 val opt_attribs = Scan.optional attribs [];
   286 val opt_attribs = Scan.optional attribs [];
   281 
   287 
   282 fun thm_name s = name -- opt_attribs --| $$$ s;
   288 fun thm_name s = name -- opt_attribs --| $$$ s;
   283 fun opt_thm_name s =
   289 fun opt_thm_name s =
   291 
   297 
   292 
   298 
   293 (* proof methods *)
   299 (* proof methods *)
   294 
   300 
   295 fun is_symid_meth s =
   301 fun is_symid_meth s =
   296   s <> "|" andalso s <> "?" andalso s <> "+" andalso OuterLex.is_sid s;
   302   s <> "|" andalso s <> "?" andalso s <> "+" andalso T.is_sid s;
   297 
   303 
   298 fun meth4 x =
   304 fun meth4 x =
   299  (position (xname >> rpair []) >> (Method.Source o Args.src) ||
   305  (position (xname >> rpair []) >> (Method.Source o Args.src) ||
   300   $$$ "(" |-- !!! (meth0 --| $$$ ")")) x
   306   $$$ "(" |-- !!! (meth0 --| $$$ ")")) x
   301 and meth3 x =
   307 and meth3 x =