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