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