src/Pure/Isar/token.ML
changeset 55033 8e8243975860
parent 54520 cee77d2e9582
child 55103 57d87ec3da4c
equal deleted inserted replaced
55032:b49366215417 55033:8e8243975860
     6 
     6 
     7 signature TOKEN =
     7 signature TOKEN =
     8 sig
     8 sig
     9   datatype kind =
     9   datatype kind =
    10     Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar |
    10     Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar |
    11     Nat | Float | String | AltString | Verbatim | Space | Comment | InternalValue |
    11     Nat | Float | String | AltString | Verbatim | Cartouche | Space | Comment | InternalValue |
    12     Error of string | Sync | EOF
    12     Error of string | Sync | EOF
    13   type file = {src_path: Path.T, text: string, pos: Position.T}
    13   type file = {src_path: Path.T, text: string, pos: Position.T}
    14   datatype value =
    14   datatype value =
    15     Text of string | Typ of typ | Term of term | Fact of thm list |
    15     Text of string | Typ of typ | Term of term | Fact of thm list |
    16     Attribute of morphism -> attribute | Files of file Exn.result list
    16     Attribute of morphism -> attribute | Files of file Exn.result list
    98 
    98 
    99 (* datatype token *)
    99 (* datatype token *)
   100 
   100 
   101 datatype kind =
   101 datatype kind =
   102   Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar |
   102   Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar |
   103   Nat | Float | String | AltString | Verbatim | Space | Comment | InternalValue |
   103   Nat | Float | String | AltString | Verbatim | Cartouche | Space | Comment | InternalValue |
   104   Error of string | Sync | EOF;
   104   Error of string | Sync | EOF;
   105 
   105 
   106 datatype T = Token of (Symbol_Pos.text * Position.range) * (kind * string) * slot;
   106 datatype T = Token of (Symbol_Pos.text * Position.range) * (kind * string) * slot;
   107 
   107 
   108 val str_of_kind =
   108 val str_of_kind =
   117   | Nat => "natural number"
   117   | Nat => "natural number"
   118   | Float => "floating-point number"
   118   | Float => "floating-point number"
   119   | String => "string"
   119   | String => "string"
   120   | AltString => "back-quoted string"
   120   | AltString => "back-quoted string"
   121   | Verbatim => "verbatim text"
   121   | Verbatim => "verbatim text"
       
   122   | Cartouche => "cartouche"
   122   | Space => "white space"
   123   | Space => "white space"
   123   | Comment => "comment text"
   124   | Comment => "comment text"
   124   | InternalValue => "internal value"
   125   | InternalValue => "internal value"
   125   | Error _ => "bad input"
   126   | Error _ => "bad input"
   126   | Sync => "sync marker"
   127   | Sync => "sync marker"
   219 fun unparse (Token (_, (kind, x), _)) =
   220 fun unparse (Token (_, (kind, x), _)) =
   220   (case kind of
   221   (case kind of
   221     String => Symbol_Pos.quote_string_qq x
   222     String => Symbol_Pos.quote_string_qq x
   222   | AltString => Symbol_Pos.quote_string_bq x
   223   | AltString => Symbol_Pos.quote_string_bq x
   223   | Verbatim => enclose "{*" "*}" x
   224   | Verbatim => enclose "{*" "*}" x
       
   225   | Cartouche => cartouche x
   224   | Comment => enclose "(*" "*)" x
   226   | Comment => enclose "(*" "*)" x
   225   | Sync => ""
   227   | Sync => ""
   226   | EOF => ""
   228   | EOF => ""
   227   | _ => x);
   229   | _ => x);
   228 
   230 
   298 fun !!! msg = Symbol_Pos.!!! (fn () => err_prefix ^ msg);
   300 fun !!! msg = Symbol_Pos.!!! (fn () => err_prefix ^ msg);
   299 
   301 
   300 
   302 
   301 (* scan symbolic idents *)
   303 (* scan symbolic idents *)
   302 
   304 
   303 val is_sym_char = member (op =) (raw_explode "!#$%&*+-/<=>?@^_|~");
       
   304 
       
   305 val scan_symid =
   305 val scan_symid =
   306   Scan.many1 (is_sym_char o Symbol_Pos.symbol) ||
   306   Scan.many1 (Symbol.is_symbolic_char o Symbol_Pos.symbol) ||
   307   Scan.one (Symbol.is_symbolic o Symbol_Pos.symbol) >> single;
   307   Scan.one (Symbol.is_symbolic o Symbol_Pos.symbol) >> single;
   308 
   308 
   309 fun is_symid str =
   309 fun is_symid str =
   310   (case try Symbol.explode str of
   310   (case try Symbol.explode str of
   311     SOME [s] => Symbol.is_symbolic s orelse is_sym_char s
   311     SOME [s] => Symbol.is_symbolic s orelse Symbol.is_symbolic_char s
   312   | SOME ss => forall is_sym_char ss
   312   | SOME ss => forall Symbol.is_symbolic_char ss
   313   | _ => false);
   313   | _ => false);
   314 
   314 
   315 fun ident_or_symbolic "begin" = false
   315 fun ident_or_symbolic "begin" = false
   316   | ident_or_symbolic ":" = true
   316   | ident_or_symbolic ":" = true
   317   | ident_or_symbolic "::" = true
   317   | ident_or_symbolic "::" = true
   331 
   331 
   332 val recover_verbatim =
   332 val recover_verbatim =
   333   $$$ "{" @@@ $$$ "*" @@@ (Scan.repeat scan_verb >> flat);
   333   $$$ "{" @@@ $$$ "*" @@@ (Scan.repeat scan_verb >> flat);
   334 
   334 
   335 
   335 
       
   336 (* scan cartouche *)
       
   337 
       
   338 val scan_cartouche =
       
   339   Symbol_Pos.scan_pos -- (Symbol_Pos.scan_cartouche_body !!! -- Symbol_Pos.scan_pos);
       
   340 
       
   341 
   336 (* scan space *)
   342 (* scan space *)
   337 
   343 
   338 fun space_symbol (s, _) = Symbol.is_blank s andalso s <> "\n";
   344 fun space_symbol (s, _) = Symbol.is_blank s andalso s <> "\n";
   339 
   345 
   340 val scan_space =
   346 val scan_space =
   365 
   371 
   366 fun scan (lex1, lex2) = !!! "bad input"
   372 fun scan (lex1, lex2) = !!! "bad input"
   367   (Symbol_Pos.scan_string_qq err_prefix >> token_range String ||
   373   (Symbol_Pos.scan_string_qq err_prefix >> token_range String ||
   368     Symbol_Pos.scan_string_bq err_prefix >> token_range AltString ||
   374     Symbol_Pos.scan_string_bq err_prefix >> token_range AltString ||
   369     scan_verbatim >> token_range Verbatim ||
   375     scan_verbatim >> token_range Verbatim ||
       
   376     scan_cartouche >> token_range Cartouche ||
   370     scan_comment >> token_range Comment ||
   377     scan_comment >> token_range Comment ||
   371     scan_space >> token Space ||
   378     scan_space >> token Space ||
   372     Scan.one (Symbol.is_sync o Symbol_Pos.symbol) >> (token Sync o single) ||
   379     Scan.one (Symbol.is_sync o Symbol_Pos.symbol) >> (token Sync o single) ||
   373     (Scan.max token_leq
   380     (Scan.max token_leq
   374       (Scan.max token_leq
   381       (Scan.max token_leq
   385 
   392 
   386 fun recover msg =
   393 fun recover msg =
   387   (Symbol_Pos.recover_string_qq ||
   394   (Symbol_Pos.recover_string_qq ||
   388     Symbol_Pos.recover_string_bq ||
   395     Symbol_Pos.recover_string_bq ||
   389     recover_verbatim ||
   396     recover_verbatim ||
       
   397     Symbol_Pos.recover_cartouche ||
   390     Symbol_Pos.recover_comment ||
   398     Symbol_Pos.recover_comment ||
   391     Scan.one (Symbol.is_regular o Symbol_Pos.symbol) >> single)
   399     Scan.one (Symbol.is_regular o Symbol_Pos.symbol) >> single)
   392   >> (single o token (Error msg));
   400   >> (single o token (Error msg));
   393 
   401 
   394 in
   402 in