src/Pure/Isar/outer_lex.ML
author wenzelm
Thu Feb 10 20:54:18 2000 +0100 (2000-02-10 ago)
changeset 8231 fa93309ff27e
parent 7902 10fd5d922c97
child 8580 e79ee31d3936
permissions -rw-r--r--
symid: include single symbolic char;
wenzelm@5825
     1
(*  Title:      Pure/Isar/outer_lex.ML
wenzelm@5825
     2
    ID:         $Id$
wenzelm@5825
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5825
     4
wenzelm@5825
     5
Outer lexical syntax for Isabelle/Isar.
wenzelm@5825
     6
*)
wenzelm@5825
     7
wenzelm@5825
     8
signature OUTER_LEX =
wenzelm@5825
     9
sig
wenzelm@5825
    10
  datatype token_kind =
wenzelm@7477
    11
    Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar |
wenzelm@7682
    12
    Nat | String | Verbatim | Space | Comment | Sync | EOF
wenzelm@5825
    13
  type token
wenzelm@5825
    14
  val str_of_kind: token_kind -> string
wenzelm@5825
    15
  val stopper: token * (token -> bool)
wenzelm@6859
    16
  val not_sync: token -> bool
wenzelm@5825
    17
  val not_eof: token -> bool
wenzelm@5825
    18
  val position_of: token -> Position.T
wenzelm@5825
    19
  val pos_of: token -> string
wenzelm@5825
    20
  val is_kind: token_kind -> token -> bool
wenzelm@7026
    21
  val keyword_with: (string -> bool) -> token -> bool
wenzelm@5825
    22
  val name_of: token -> string
wenzelm@5825
    23
  val is_proper: token -> bool
wenzelm@7902
    24
  val is_indent: token -> bool
wenzelm@5825
    25
  val val_of: token -> string
wenzelm@5876
    26
  val is_sid: string -> bool
wenzelm@7026
    27
  val scan: (Scan.lexicon * Scan.lexicon) ->
wenzelm@5825
    28
    Position.T * Symbol.symbol list -> token * (Position.T * Symbol.symbol list)
wenzelm@7026
    29
  val source: bool -> (unit -> (Scan.lexicon * Scan.lexicon)) ->
wenzelm@7026
    30
    Position.T -> (Symbol.symbol, 'a) Source.source ->
wenzelm@7682
    31
    (token, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
wenzelm@5825
    32
end;
wenzelm@5825
    33
wenzelm@5825
    34
structure OuterLex: OUTER_LEX =
wenzelm@5825
    35
struct
wenzelm@5825
    36
wenzelm@5825
    37
wenzelm@5825
    38
(** tokens **)
wenzelm@5825
    39
wenzelm@5825
    40
(* datatype token *)
wenzelm@5825
    41
wenzelm@5825
    42
datatype token_kind =
wenzelm@7477
    43
  Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar |
wenzelm@7682
    44
  Nat | String | Verbatim | Space | Comment | Sync | EOF;
wenzelm@5825
    45
wenzelm@5825
    46
datatype token = Token of Position.T * (token_kind * string);
wenzelm@5825
    47
wenzelm@5825
    48
val str_of_kind =
wenzelm@7026
    49
 fn Command => "command"
wenzelm@7026
    50
  | Keyword => "keyword"
wenzelm@5825
    51
  | Ident => "identifier"
wenzelm@5825
    52
  | LongIdent => "long identifier"
wenzelm@5825
    53
  | SymIdent => "symbolic identifier"
wenzelm@5825
    54
  | Var => "schematic variable"
wenzelm@5825
    55
  | TypeIdent => "type variable"
wenzelm@5825
    56
  | TypeVar => "schematic type variable"
wenzelm@5825
    57
  | Nat => "number"
wenzelm@5825
    58
  | String => "string"
wenzelm@5825
    59
  | Verbatim => "verbatim text"
wenzelm@7682
    60
  | Space => "white space"
wenzelm@7682
    61
  | Comment => "comment text"
wenzelm@6859
    62
  | Sync => "sync marker"
wenzelm@5825
    63
  | EOF => "end-of-file";
wenzelm@5825
    64
wenzelm@5825
    65
wenzelm@6859
    66
(* sync token *)
wenzelm@6859
    67
wenzelm@6859
    68
fun not_sync (Token (_, (Sync, _))) = false
wenzelm@6859
    69
  | not_sync _ = true;
wenzelm@6859
    70
wenzelm@6859
    71
wenzelm@5825
    72
(* eof token *)
wenzelm@5825
    73
wenzelm@5825
    74
val eof = Token (Position.none, (EOF, ""));
wenzelm@5825
    75
wenzelm@5825
    76
fun is_eof (Token (_, (EOF, _))) = true
wenzelm@5825
    77
  | is_eof _ = false;
wenzelm@5825
    78
wenzelm@5825
    79
val stopper = (eof, is_eof);
wenzelm@5825
    80
val not_eof = not o is_eof;
wenzelm@5825
    81
wenzelm@5825
    82
wenzelm@5825
    83
(* get position *)
wenzelm@5825
    84
wenzelm@5825
    85
fun position_of (Token (pos, _)) = pos;
wenzelm@5825
    86
val pos_of = Position.str_of o position_of;
wenzelm@5825
    87
wenzelm@5825
    88
wenzelm@5825
    89
(* kind of token *)
wenzelm@5825
    90
wenzelm@5825
    91
fun is_kind k (Token (_, (k', _))) = k = k';
wenzelm@5825
    92
wenzelm@7026
    93
fun keyword_with pred (Token (_, (Keyword, x))) = pred x
wenzelm@7026
    94
  | keyword_with _ _ = false;
wenzelm@5825
    95
wenzelm@5825
    96
fun name_of (Token (_, (k, _))) = str_of_kind k;
wenzelm@5825
    97
wenzelm@7682
    98
fun is_proper (Token (_, (Space, _))) = false
wenzelm@7682
    99
  | is_proper (Token (_, (Comment, _))) = false
wenzelm@5825
   100
  | is_proper _ = true;
wenzelm@5825
   101
wenzelm@7902
   102
(*indentations; note that space tokens obey lines*)
wenzelm@7902
   103
fun is_indent (Token (_, (Space, s))) =
wenzelm@7902
   104
      let val n = size s in n > 0 andalso String.substring (s, n - 1, 1) <> "\n" end
wenzelm@7902
   105
  | is_indent _ = false;
wenzelm@7902
   106
wenzelm@5825
   107
wenzelm@5825
   108
(* value of token *)
wenzelm@5825
   109
wenzelm@5825
   110
fun val_of (Token (_, (_, x))) = x;
wenzelm@5825
   111
wenzelm@5825
   112
fun token_leq (Token (_, (_, x)), Token (_, (_, x'))) = x <= x';
wenzelm@5825
   113
wenzelm@5825
   114
wenzelm@5825
   115
wenzelm@5825
   116
(** scanners **)
wenzelm@5825
   117
wenzelm@5825
   118
fun change_prompt scan = Scan.prompt "# " scan;
wenzelm@5825
   119
wenzelm@5825
   120
wenzelm@5825
   121
(* diagnostics *)
wenzelm@5825
   122
wenzelm@5825
   123
fun lex_err msg ((pos, cs), _) = "Outer lexical error" ^ Position.str_of pos ^ ": " ^ msg cs;
wenzelm@5825
   124
wenzelm@5825
   125
wenzelm@5825
   126
(* line numbering *)
wenzelm@5825
   127
wenzelm@5825
   128
fun incr_line scan = Scan.depend (fn pos => scan >> pair (Position.inc pos));
wenzelm@5825
   129
val keep_line = Scan.lift;
wenzelm@5825
   130
wenzelm@5825
   131
val scan_blank =
wenzelm@5825
   132
  incr_line ($$ "\n") ||
wenzelm@5825
   133
  keep_line (Scan.one Symbol.is_blank);
wenzelm@5825
   134
wenzelm@5825
   135
wenzelm@5825
   136
(* scan symbolic idents *)
wenzelm@5825
   137
wenzelm@5825
   138
val sym_chars = explode "!#$%&*+-/:<=>?@^_`|~";
wenzelm@5825
   139
fun is_sym_char s = s mem sym_chars;
wenzelm@5825
   140
wenzelm@8231
   141
val scan_symid =
wenzelm@8231
   142
  Scan.any1 is_sym_char >> implode ||
wenzelm@8231
   143
  Scan.one Symbol.is_symbolic;
wenzelm@5825
   144
wenzelm@8231
   145
fun is_symid str =
wenzelm@8231
   146
  (case try Symbol.explode str of
wenzelm@8231
   147
    Some [s] => Symbol.is_symbolic s orelse is_sym_char s
wenzelm@8231
   148
  | Some ss => forall is_sym_char ss
wenzelm@8231
   149
  | _ => false);
wenzelm@8231
   150
wenzelm@5876
   151
val is_sid = is_symid orf Syntax.is_identifier;
wenzelm@5825
   152
wenzelm@5825
   153
wenzelm@5825
   154
(* scan strings *)
wenzelm@5825
   155
wenzelm@5825
   156
val scan_str =
wenzelm@7682
   157
  scan_blank ||
wenzelm@6859
   158
  keep_line ($$ "\\" |-- Scan.one (Symbol.not_sync andf Symbol.not_eof)) ||
wenzelm@6859
   159
  keep_line (Scan.one (not_equal "\\" andf not_equal "\"" andf
wenzelm@6859
   160
    Symbol.not_sync andf Symbol.not_eof));
wenzelm@5825
   161
wenzelm@5825
   162
val scan_string =
wenzelm@5825
   163
  keep_line ($$ "\"") |--
wenzelm@5825
   164
    !! (lex_err (K "missing quote at end of string"))
wenzelm@5825
   165
      (change_prompt ((Scan.repeat scan_str >> implode) --| keep_line ($$ "\"")));
wenzelm@5825
   166
wenzelm@5825
   167
wenzelm@5825
   168
(* scan verbatim text *)
wenzelm@5825
   169
wenzelm@5825
   170
val scan_verb =
wenzelm@5825
   171
  scan_blank ||
wenzelm@6743
   172
  keep_line ($$ "*" --| Scan.ahead (Scan.one (not_equal "}"))) ||
wenzelm@6859
   173
  keep_line (Scan.one (not_equal "*" andf Symbol.not_sync andf Symbol.not_eof));
wenzelm@5825
   174
wenzelm@5825
   175
val scan_verbatim =
wenzelm@6743
   176
  keep_line ($$ "{" -- $$ "*") |--
wenzelm@5825
   177
    !! (lex_err (K "missing end of verbatim text"))
wenzelm@6743
   178
      (change_prompt ((Scan.repeat scan_verb >> implode) --| keep_line ($$ "*" -- $$ "}")));
wenzelm@5825
   179
wenzelm@5825
   180
wenzelm@5825
   181
(* scan space *)
wenzelm@5825
   182
wenzelm@5825
   183
val is_space = Symbol.is_blank andf not_equal "\n";
wenzelm@5825
   184
wenzelm@5825
   185
val scan_space =
wenzelm@7682
   186
  (keep_line (Scan.any1 is_space) -- Scan.optional (incr_line ($$ "\n")) "" ||
wenzelm@7682
   187
    keep_line (Scan.any is_space) -- incr_line ($$ "\n")) >> (fn (cs, c) => implode cs ^ c);
wenzelm@5825
   188
wenzelm@5825
   189
wenzelm@5825
   190
(* scan nested comments *)
wenzelm@5825
   191
wenzelm@5825
   192
val scan_cmt =
wenzelm@5825
   193
  Scan.lift scan_blank ||
wenzelm@5825
   194
  Scan.depend (fn d => keep_line ($$ "(" ^^ $$ "*") >> pair (d + 1)) ||
wenzelm@5825
   195
  Scan.depend (fn 0 => Scan.fail | d => keep_line ($$ "*" ^^ $$ ")") >> pair (d - 1)) ||
wenzelm@5825
   196
  Scan.lift (keep_line ($$ "*" --| Scan.ahead (Scan.one (not_equal ")")))) ||
wenzelm@6859
   197
  Scan.lift (keep_line (Scan.one (not_equal "*" andf Symbol.not_sync andf Symbol.not_eof)));
wenzelm@5825
   198
wenzelm@5825
   199
val scan_comment =
wenzelm@5825
   200
  keep_line ($$ "(" -- $$ "*") |--
wenzelm@5825
   201
    !! (lex_err (K "missing end of comment"))
wenzelm@5825
   202
      (change_prompt
wenzelm@7682
   203
        (Scan.pass 0 (Scan.repeat scan_cmt >> implode) --| keep_line ($$ "*" -- $$ ")")));
wenzelm@5825
   204
wenzelm@5825
   205
wenzelm@5825
   206
(* scan token *)
wenzelm@5825
   207
wenzelm@7026
   208
fun scan (lex1, lex2) (pos, cs) =
wenzelm@5825
   209
  let
wenzelm@5825
   210
    fun token k x = Token (pos, (k, x));
wenzelm@6859
   211
    fun sync _ = token Sync Symbol.sync;
wenzelm@5825
   212
wenzelm@5825
   213
    val scanner =
wenzelm@5825
   214
      scan_string >> token String ||
wenzelm@5825
   215
      scan_verbatim >> token Verbatim ||
wenzelm@7682
   216
      scan_space >> token Space ||
wenzelm@7682
   217
      scan_comment >> token Comment ||
wenzelm@6859
   218
      keep_line (Scan.one Symbol.is_sync >> sync) ||
wenzelm@5825
   219
      keep_line (Scan.max token_leq
wenzelm@7026
   220
        (Scan.max token_leq
wenzelm@7026
   221
          (Scan.literal lex1 >> (token Keyword o implode))
wenzelm@7026
   222
          (Scan.literal lex2 >> (token Command o implode)))
wenzelm@5825
   223
        (Syntax.scan_longid >> token LongIdent ||
wenzelm@5825
   224
          Syntax.scan_id >> token Ident ||
wenzelm@5825
   225
          Syntax.scan_var >> token Var ||
wenzelm@5825
   226
          Syntax.scan_tid >> token TypeIdent ||
wenzelm@5825
   227
          Syntax.scan_tvar >> token TypeVar ||
wenzelm@5825
   228
          Syntax.scan_nat >> token Nat ||
wenzelm@5825
   229
          scan_symid >> token SymIdent));
wenzelm@6859
   230
  in !! (lex_err (fn cs => "bad input " ^ quote (Symbol.beginning cs))) scanner (pos, cs) end;
wenzelm@5825
   231
wenzelm@5825
   232
wenzelm@5825
   233
(* source of (proper) tokens *)
wenzelm@5825
   234
wenzelm@6859
   235
val is_junk = (not o Symbol.is_blank) andf Symbol.not_sync andf Symbol.not_eof;
wenzelm@6859
   236
fun recover xs = keep_line (Scan.any1 is_junk) xs;
wenzelm@5825
   237
wenzelm@5825
   238
fun source do_recover get_lex pos src =
wenzelm@5825
   239
  Source.source' pos Symbol.stopper (Scan.bulk (fn xs => scan (get_lex ()) xs))
wenzelm@7682
   240
    (if do_recover then Some recover else None) src;
wenzelm@5825
   241
wenzelm@5825
   242
wenzelm@5825
   243
end;