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