src/Pure/Isar/token.ML
changeset 74373 6e4093927dbb
parent 74175 53e28c438f96
child 74558 44dc1661a5cb
equal deleted inserted replaced
74370:d8dc8fdc46fc 74373:6e4093927dbb
     9   datatype kind =
     9   datatype kind =
    10     (*immediate source*)
    10     (*immediate source*)
    11     Command | Keyword | Ident | Long_Ident | Sym_Ident | Var | Type_Ident | Type_Var | Nat |
    11     Command | Keyword | Ident | Long_Ident | Sym_Ident | Var | Type_Ident | Type_Var | Nat |
    12     Float | Space |
    12     Float | Space |
    13     (*delimited content*)
    13     (*delimited content*)
    14     String | Alt_String | Verbatim | Cartouche | Comment of Comment.kind option |
    14     String | Alt_String | Verbatim | Cartouche |
       
    15     Control of Antiquote.control |
       
    16     Comment of Comment.kind option |
    15     (*special content*)
    17     (*special content*)
    16     Error of string | EOF
    18     Error of string | EOF
       
    19   val control_kind: kind
    17   val str_of_kind: kind -> string
    20   val str_of_kind: kind -> string
    18   type file = {src_path: Path.T, lines: string list, digest: SHA1.digest, pos: Position.T}
    21   type file = {src_path: Path.T, lines: string list, digest: SHA1.digest, pos: Position.T}
    19   type T
    22   type T
    20   type src = T list
    23   type src = T list
    21   type name_value = {name: string, kind: string, print: Proof.context -> Markup.T * xstring}
    24   type name_value = {name: string, kind: string, print: Proof.context -> Markup.T * xstring}
    35   val is_eof: T -> bool
    38   val is_eof: T -> bool
    36   val not_eof: T -> bool
    39   val not_eof: T -> bool
    37   val stopper: T Scan.stopper
    40   val stopper: T Scan.stopper
    38   val kind_of: T -> kind
    41   val kind_of: T -> kind
    39   val is_kind: kind -> T -> bool
    42   val is_kind: kind -> T -> bool
       
    43   val get_control: T -> Antiquote.control option
    40   val is_command: T -> bool
    44   val is_command: T -> bool
    41   val keyword_with: (string -> bool) -> T -> bool
    45   val keyword_with: (string -> bool) -> T -> bool
    42   val is_command_modifier: T -> bool
    46   val is_command_modifier: T -> bool
    43   val ident_with: (string -> bool) -> T -> bool
    47   val ident_with: (string -> bool) -> T -> bool
    44   val is_proper: T -> bool
    48   val is_proper: T -> bool
   116 datatype kind =
   120 datatype kind =
   117   (*immediate source*)
   121   (*immediate source*)
   118   Command | Keyword | Ident | Long_Ident | Sym_Ident | Var | Type_Ident | Type_Var | Nat |
   122   Command | Keyword | Ident | Long_Ident | Sym_Ident | Var | Type_Ident | Type_Var | Nat |
   119   Float | Space |
   123   Float | Space |
   120   (*delimited content*)
   124   (*delimited content*)
   121   String | Alt_String | Verbatim | Cartouche | Comment of Comment.kind option |
   125   String | Alt_String | Verbatim | Cartouche |
       
   126   Control of Antiquote.control |
       
   127   Comment of Comment.kind option |
   122   (*special content*)
   128   (*special content*)
   123   Error of string | EOF;
   129   Error of string | EOF;
       
   130 
       
   131 val control_kind = Control Antiquote.no_control;
       
   132 
       
   133 fun equiv_kind kind kind' =
       
   134   (case (kind, kind') of
       
   135     (Control _, Control _) => true
       
   136   | (Error _, Error _) => true
       
   137   | _ => kind = kind');
   124 
   138 
   125 val str_of_kind =
   139 val str_of_kind =
   126  fn Command => "command"
   140  fn Command => "command"
   127   | Keyword => "keyword"
   141   | Keyword => "keyword"
   128   | Ident => "identifier"
   142   | Ident => "identifier"
   136   | Space => "white space"
   150   | Space => "white space"
   137   | String => "quoted string"
   151   | String => "quoted string"
   138   | Alt_String => "back-quoted string"
   152   | Alt_String => "back-quoted string"
   139   | Verbatim => "verbatim text"
   153   | Verbatim => "verbatim text"
   140   | Cartouche => "text cartouche"
   154   | Cartouche => "text cartouche"
       
   155   | Control _ => "control cartouche"
   141   | Comment NONE => "informal comment"
   156   | Comment NONE => "informal comment"
   142   | Comment (SOME _) => "formal comment"
   157   | Comment (SOME _) => "formal comment"
   143   | Error _ => "bad input"
   158   | Error _ => "bad input"
   144   | EOF => "end-of-input";
   159   | EOF => "end-of-input";
   145 
   160 
   150 val delimited_kind =
   165 val delimited_kind =
   151   (fn String => true
   166   (fn String => true
   152     | Alt_String => true
   167     | Alt_String => true
   153     | Verbatim => true
   168     | Verbatim => true
   154     | Cartouche => true
   169     | Cartouche => true
       
   170     | Control _ => true
   155     | Comment _ => true
   171     | Comment _ => true
   156     | _ => false);
   172     | _ => false);
   157 
   173 
   158 
   174 
   159 (* datatype token *)
   175 (* datatype token *)
   212 
   228 
   213 
   229 
   214 (* kind of token *)
   230 (* kind of token *)
   215 
   231 
   216 fun kind_of (Token (_, (k, _), _)) = k;
   232 fun kind_of (Token (_, (k, _), _)) = k;
   217 fun is_kind k (Token (_, (k', _), _)) = k = k';
   233 fun is_kind k (Token (_, (k', _), _)) = equiv_kind k k';
       
   234 
       
   235 fun get_control tok =
       
   236   (case kind_of tok of Control control => SOME control | _ => NONE);
   218 
   237 
   219 val is_command = is_kind Command;
   238 val is_command = is_kind Command;
   220 
   239 
   221 fun keyword_with pred (Token (_, (Keyword, x), _)) = pred x
   240 fun keyword_with pred (Token (_, (Keyword, x), _)) = pred x
   222   | keyword_with _ _ = false;
   241   | keyword_with _ _ = false;
   302   | Type_Var => (Markup.tvar, "")
   321   | Type_Var => (Markup.tvar, "")
   303   | String => (Markup.string, "")
   322   | String => (Markup.string, "")
   304   | Alt_String => (Markup.alt_string, "")
   323   | Alt_String => (Markup.alt_string, "")
   305   | Verbatim => (Markup.verbatim, "")
   324   | Verbatim => (Markup.verbatim, "")
   306   | Cartouche => (Markup.cartouche, "")
   325   | Cartouche => (Markup.cartouche, "")
       
   326   | Control _ => (Markup.cartouche, "")
   307   | Comment _ => (Markup.comment, "")
   327   | Comment _ => (Markup.comment, "")
   308   | Error msg => (Markup.bad (), msg)
   328   | Error msg => (Markup.bad (), msg)
   309   | _ => (Markup.empty, "");
   329   | _ => (Markup.empty, "");
   310 
   330 
   311 fun keyword_reports tok = map (fn markup => ((pos_of tok, markup), ""));
   331 fun keyword_reports tok = map (fn markup => ((pos_of tok, markup), ""));
   352   (case kind of
   372   (case kind of
   353     String => Symbol_Pos.quote_string_qq x
   373     String => Symbol_Pos.quote_string_qq x
   354   | Alt_String => Symbol_Pos.quote_string_bq x
   374   | Alt_String => Symbol_Pos.quote_string_bq x
   355   | Verbatim => enclose "{*" "*}" x
   375   | Verbatim => enclose "{*" "*}" x
   356   | Cartouche => cartouche x
   376   | Cartouche => cartouche x
       
   377   | Control control => Symbol_Pos.content (Antiquote.control_symbols control)
   357   | Comment NONE => enclose "(*" "*)" x
   378   | Comment NONE => enclose "(*" "*)" x
   358   | EOF => ""
   379   | EOF => ""
   359   | _ => x);
   380   | _ => x);
   360 
   381 
   361 fun print tok = Markup.markups (markups Keyword.empty_keywords tok) (unparse tok);
   382 fun print tok = Markup.markups (markups Keyword.empty_keywords tok) (unparse tok);
   644 
   665 
   645 fun scan_token keywords = !!! "bad input"
   666 fun scan_token keywords = !!! "bad input"
   646   (Symbol_Pos.scan_string_qq err_prefix >> token_range String ||
   667   (Symbol_Pos.scan_string_qq err_prefix >> token_range String ||
   647     Symbol_Pos.scan_string_bq err_prefix >> token_range Alt_String ||
   668     Symbol_Pos.scan_string_bq err_prefix >> token_range Alt_String ||
   648     scan_verbatim >> token_range Verbatim ||
   669     scan_verbatim >> token_range Verbatim ||
   649     scan_cartouche >> token_range Cartouche ||
       
   650     scan_comment >> token_range (Comment NONE) ||
   670     scan_comment >> token_range (Comment NONE) ||
   651     Comment.scan_outer >> (fn (k, ss) => token (Comment (SOME k)) ss) ||
   671     Comment.scan_outer >> (fn (k, ss) => token (Comment (SOME k)) ss) ||
       
   672     scan_cartouche >> token_range Cartouche ||
       
   673     Antiquote.scan_control err_prefix >> (fn control =>
       
   674       token (Control control) (Antiquote.control_symbols control)) ||
   652     scan_space >> token Space ||
   675     scan_space >> token Space ||
   653     (Scan.max token_leq
   676     (Scan.max token_leq
   654       (Scan.max token_leq
   677       (Scan.max token_leq
   655         (Scan.literal (Keyword.major_keywords keywords) >> pair Command)
   678         (Scan.literal (Keyword.major_keywords keywords) >> pair Command)
   656         (Scan.literal (Keyword.minor_keywords keywords) >> pair Keyword))
   679         (Scan.literal (Keyword.minor_keywords keywords) >> pair Keyword))