src/Pure/ML/ml_lex.ML
author wenzelm
Wed, 10 Jun 2009 11:12:40 +0200
changeset 31543 5bef6c7cc819
parent 31474 0ae32184bde0
child 37195 e87d305a4490
permissions -rw-r--r--
allow Isabelle symbols within low-level ML source;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ML/ml_lex.ML
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
     3
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
     4
Lexical syntax for SML.
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
     5
*)
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
     6
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
     7
signature ML_LEX =
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
     8
sig
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
     9
  datatype token_kind =
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    10
    Keyword | Ident | LongIdent | TypeVar | Word | Int | Real | Char | String |
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    11
    Space | Comment | Error of string | EOF
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    12
  eqtype token
27732
8dbf5761a24a abstract type Scan.stopper;
wenzelm
parents: 24596
diff changeset
    13
  val stopper: token Scan.stopper
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    14
  val is_regular: token -> bool
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    15
  val is_improper: token -> bool
30683
e8ac1f9d9469 datatype antiquote: maintain original Position.range, which is eventually attached to the resulting ML tokens;
wenzelm
parents: 30645
diff changeset
    16
  val set_range: Position.range -> token -> token
30636
bd8813d7774d ML_Lex.pos_of: regular position;
wenzelm
parents: 30614
diff changeset
    17
  val pos_of: token -> Position.T
31474
0ae32184bde0 export end_pos_of;
wenzelm
parents: 31426
diff changeset
    18
  val end_pos_of: token -> Position.T
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    19
  val kind_of: token -> token_kind
27817
78cae5cca09e renamed ML_Lex.val_of to content_of;
wenzelm
parents: 27799
diff changeset
    20
  val content_of: token -> string
31426
5c9dbd511510 tuned signature;
wenzelm
parents: 31332
diff changeset
    21
  val check_content_of: token -> string
31332
9639a6d4d714 added flatten;
wenzelm
parents: 30683
diff changeset
    22
  val flatten: token list -> string
30614
845bcfd3e9cd report markup for ML tokens;
wenzelm
parents: 30600
diff changeset
    23
  val report_token: token -> unit
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    24
  val keywords: string list
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    25
  val source: (Symbol.symbol, 'a) Source.source ->
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
    26
    (token, (Symbol_Pos.T, Position.T * (Symbol.symbol, 'a) Source.source)
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
    27
      Source.source) Source.source
30591
6c9c00ea4ae6 added tokenize;
wenzelm
parents: 30573
diff changeset
    28
  val tokenize: string -> token list
30645
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
    29
  val read_antiq: Symbol_Pos.T list * Position.T -> token Antiquote.antiquote list
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    30
end;
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    31
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    32
structure ML_Lex: ML_LEX =
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    33
struct
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    34
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    35
(** tokens **)
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    36
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    37
(* datatype token *)
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    38
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    39
datatype token_kind =
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    40
  Keyword | Ident | LongIdent | TypeVar | Word | Int | Real | Char | String |
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    41
  Space | Comment | Error of string | EOF;
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    42
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
    43
datatype token = Token of Position.range * (token_kind * string);
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
    44
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
    45
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
    46
(* position *)
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
    47
30683
e8ac1f9d9469 datatype antiquote: maintain original Position.range, which is eventually attached to the resulting ML tokens;
wenzelm
parents: 30645
diff changeset
    48
fun set_range range (Token (_, x)) = Token (range, x);
e8ac1f9d9469 datatype antiquote: maintain original Position.range, which is eventually attached to the resulting ML tokens;
wenzelm
parents: 30645
diff changeset
    49
30636
bd8813d7774d ML_Lex.pos_of: regular position;
wenzelm
parents: 30614
diff changeset
    50
fun pos_of (Token ((pos, _), _)) = pos;
bd8813d7774d ML_Lex.pos_of: regular position;
wenzelm
parents: 30614
diff changeset
    51
fun end_pos_of (Token ((_, pos), _)) = pos;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    52
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    53
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    54
(* control tokens *)
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    55
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
    56
fun mk_eof pos = Token ((pos, Position.none), (EOF, ""));
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
    57
val eof = mk_eof Position.none;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    58
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    59
fun is_eof (Token (_, (EOF, _))) = true
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    60
  | is_eof _ = false;
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    61
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
    62
val stopper =
30636
bd8813d7774d ML_Lex.pos_of: regular position;
wenzelm
parents: 30614
diff changeset
    63
  Scan.stopper (fn [] => eof | toks => mk_eof (end_pos_of (List.last toks))) is_eof;
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
    64
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
    65
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
    66
(* token content *)
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
    67
31426
5c9dbd511510 tuned signature;
wenzelm
parents: 31332
diff changeset
    68
fun kind_of (Token (_, (k, _))) = k;
5c9dbd511510 tuned signature;
wenzelm
parents: 31332
diff changeset
    69
27817
78cae5cca09e renamed ML_Lex.val_of to content_of;
wenzelm
parents: 27799
diff changeset
    70
fun content_of (Token (_, (_, x))) = x;
78cae5cca09e renamed ML_Lex.val_of to content_of;
wenzelm
parents: 27799
diff changeset
    71
fun token_leq (tok, tok') = content_of tok <= content_of tok';
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
    72
31426
5c9dbd511510 tuned signature;
wenzelm
parents: 31332
diff changeset
    73
fun check_content_of tok =
30636
bd8813d7774d ML_Lex.pos_of: regular position;
wenzelm
parents: 30614
diff changeset
    74
  (case kind_of tok of
30645
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
    75
    Error msg => error msg
31426
5c9dbd511510 tuned signature;
wenzelm
parents: 31332
diff changeset
    76
  | _ => content_of tok);
30636
bd8813d7774d ML_Lex.pos_of: regular position;
wenzelm
parents: 30614
diff changeset
    77
31426
5c9dbd511510 tuned signature;
wenzelm
parents: 31332
diff changeset
    78
val flatten = implode o map (Symbol.escape o check_content_of);
31332
9639a6d4d714 added flatten;
wenzelm
parents: 30683
diff changeset
    79
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    80
fun is_regular (Token (_, (Error _, _))) = false
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    81
  | is_regular (Token (_, (EOF, _))) = false
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    82
  | is_regular _ = true;
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    83
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    84
fun is_improper (Token (_, (Space, _))) = true
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    85
  | is_improper (Token (_, (Comment, _))) = true
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    86
  | is_improper _ = false;
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    87
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
    88
30614
845bcfd3e9cd report markup for ML tokens;
wenzelm
parents: 30600
diff changeset
    89
(* markup *)
845bcfd3e9cd report markup for ML tokens;
wenzelm
parents: 30600
diff changeset
    90
30645
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
    91
val token_kind_markup =
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
    92
 fn Keyword   => Markup.ML_keyword
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
    93
  | Ident     => Markup.ML_ident
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
    94
  | LongIdent => Markup.ML_ident
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
    95
  | TypeVar   => Markup.ML_tvar
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
    96
  | Word      => Markup.ML_numeral
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
    97
  | Int       => Markup.ML_numeral
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
    98
  | Real      => Markup.ML_numeral
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
    99
  | Char      => Markup.ML_char
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   100
  | String    => Markup.ML_string
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   101
  | Space     => Markup.none
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   102
  | Comment   => Markup.ML_comment
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   103
  | Error _   => Markup.ML_malformed
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   104
  | EOF       => Markup.none;
30614
845bcfd3e9cd report markup for ML tokens;
wenzelm
parents: 30600
diff changeset
   105
30645
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   106
fun report_token (Token ((pos, _), (kind, _))) =
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   107
  Position.report (token_kind_markup kind) pos;
30614
845bcfd3e9cd report markup for ML tokens;
wenzelm
parents: 30600
diff changeset
   108
845bcfd3e9cd report markup for ML tokens;
wenzelm
parents: 30600
diff changeset
   109
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   110
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   111
(** scanners **)
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   112
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
   113
open Basic_Symbol_Pos;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   114
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
   115
fun !!! msg = Symbol_Pos.!!! ("SML lexical error: " ^ msg);
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   116
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   117
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   118
(* blanks *)
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   119
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   120
val scan_blank = Scan.one (Symbol.is_ascii_blank o symbol);
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   121
val scan_blanks1 = Scan.repeat1 scan_blank;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   122
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   123
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   124
(* keywords *)
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   125
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   126
val keywords = ["#", "(", ")", ",", "->", "...", ":", ":>", ";", "=",
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   127
  "=>", "[", "]", "_", "{", "|", "}", "abstype", "and", "andalso", "as",
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   128
  "case", "datatype", "do", "else", "end", "eqtype", "exception", "fn",
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   129
  "fun", "functor", "handle", "if", "in", "include", "infix", "infixr",
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   130
  "let", "local", "nonfix", "of", "op", "open", "orelse", "raise", "rec",
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   131
  "sharing", "sig", "signature", "struct", "structure", "then", "type",
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   132
  "val", "where", "while", "with", "withtype"];
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   133
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   134
val lex = Scan.make_lexicon (map explode keywords);
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   135
fun scan_keyword x = Scan.literal lex x;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   136
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   137
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   138
(* identifiers *)
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   139
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   140
local
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   141
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   142
val scan_letdigs =
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   143
  Scan.many ((Symbol.is_ascii_letter orf Symbol.is_ascii_digit orf Symbol.is_ascii_quasi) o symbol);
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   144
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   145
val scan_alphanumeric = Scan.one (Symbol.is_ascii_letter o symbol) -- scan_letdigs >> op ::;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   146
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   147
val scan_symbolic = Scan.many1 (member (op =) (explode "!#$%&*+-/:<=>?@\\^`|~") o symbol);
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   148
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   149
in
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   150
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   151
val scan_ident = scan_alphanumeric || scan_symbolic;
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   152
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   153
val scan_longident =
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   154
  (Scan.repeat1 (scan_alphanumeric @@@ $$$ ".") >> flat) @@@ (scan_ident || $$$ "=");
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   155
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   156
val scan_typevar = $$$ "'" @@@ scan_letdigs;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   157
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   158
end;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   159
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   160
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   161
(* numerals *)
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   162
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   163
local
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   164
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   165
val scan_dec = Scan.many1 (Symbol.is_ascii_digit o symbol);
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   166
val scan_hex = Scan.many1 (Symbol.is_ascii_hex o symbol);
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   167
val scan_sign = Scan.optional ($$$ "~") [];
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   168
val scan_decint = scan_sign @@@ scan_dec;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   169
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   170
in
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   171
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   172
val scan_word =
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   173
  $$$ "0" @@@ $$$ "w" @@@ $$$ "x" @@@ scan_hex ||
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   174
  $$$ "0" @@@ $$$ "w" @@@ scan_dec;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   175
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   176
val scan_int = scan_sign @@@ ($$$ "0" @@@ $$$ "x" @@@ scan_hex || scan_dec);
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   177
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   178
val scan_exp = ($$$ "E" || $$$ "e") @@@ scan_decint;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   179
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   180
val scan_real =
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   181
  scan_decint @@@ $$$ "." @@@ scan_dec @@@ Scan.optional scan_exp [] ||
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   182
  scan_decint @@@ scan_exp;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   183
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   184
end;
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   185
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   186
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   187
(* chars and strings *)
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   188
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   189
local
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   190
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   191
val scan_escape =
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   192
  Scan.one (member (op =) (explode "\"\\abtnvfr") o symbol) >> single ||
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   193
  $$$ "^" @@@ (Scan.one (fn (s, _) => ord "@" <= ord s andalso ord s <= ord "_") >> single) ||
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   194
  Scan.one (Symbol.is_ascii_digit o symbol) --
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   195
    Scan.one (Symbol.is_ascii_digit o symbol) --
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   196
    Scan.one (Symbol.is_ascii_digit o symbol) >> (fn ((a, b), c) => [a, b, c]);
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   197
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   198
val scan_str =
30600
de241396389c allow non-printable symbols within string tokens;
wenzelm
parents: 30593
diff changeset
   199
  Scan.one (fn (s, _) => Symbol.is_regular s andalso s <> "\"" andalso s <> "\\" andalso
de241396389c allow non-printable symbols within string tokens;
wenzelm
parents: 30593
diff changeset
   200
    (not (Symbol.is_char s) orelse Symbol.is_printable s)) >> single ||
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   201
  $$$ "\\" @@@ !!! "bad escape character in string" scan_escape;
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   202
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   203
val scan_gap = $$$ "\\" @@@ scan_blanks1 @@@ $$$ "\\";
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   204
val scan_gaps = Scan.repeat scan_gap >> flat;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   205
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   206
in
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   207
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   208
val scan_char =
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   209
  $$$ "#" @@@ $$$ "\"" @@@ scan_gaps @@@ scan_str @@@ scan_gaps @@@ $$$ "\"";
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   210
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   211
val scan_string =
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   212
  $$$ "\"" @@@ !!! "missing quote at end of string"
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   213
    ((Scan.repeat (scan_gap || scan_str) >> flat) @@@ $$$ "\"");
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   214
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   215
end;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   216
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   217
30645
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   218
(* scan tokens *)
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   219
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   220
local
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   221
30593
495672058d97 added scan_antiq;
wenzelm
parents: 30591
diff changeset
   222
fun token k ss = Token (Symbol_Pos.range ss, (k, Symbol_Pos.content ss));
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   223
30593
495672058d97 added scan_antiq;
wenzelm
parents: 30591
diff changeset
   224
val scan_ml =
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   225
 (scan_char >> token Char ||
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   226
  scan_string >> token String ||
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   227
  scan_blanks1 >> token Space ||
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
   228
  Symbol_Pos.scan_comment !!! >> token Comment ||
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   229
  Scan.max token_leq
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   230
   (scan_keyword >> token Keyword)
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   231
   (scan_word >> token Word ||
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   232
    scan_real >> token Real ||
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   233
    scan_int >> token Int ||
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   234
    scan_longident >> token LongIdent ||
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   235
    scan_ident >> token Ident ||
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   236
    scan_typevar >> token TypeVar));
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   237
30645
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   238
val scan_antiq = Antiquote.scan || scan_ml >> Antiquote.Text;
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   239
27772
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   240
fun recover msg =
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   241
  Scan.many (((not o Symbol.is_blank) andf Symbol.is_regular) o symbol)
b933c997eab3 improved position handling due to SymbolPos.T;
wenzelm
parents: 27732
diff changeset
   242
  >> (fn cs => [token (Error msg) cs]);
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   243
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   244
in
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   245
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   246
fun source src =
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
   247
  Symbol_Pos.source (Position.line 1) src
30593
495672058d97 added scan_antiq;
wenzelm
parents: 30591
diff changeset
   248
  |> Source.source Symbol_Pos.stopper (Scan.bulk (!!! "bad input" scan_ml)) (SOME (false, recover));
30591
6c9c00ea4ae6 added tokenize;
wenzelm
parents: 30573
diff changeset
   249
31543
5bef6c7cc819 allow Isabelle symbols within low-level ML source;
wenzelm
parents: 31474
diff changeset
   250
val tokenize =
5bef6c7cc819 allow Isabelle symbols within low-level ML source;
wenzelm
parents: 31474
diff changeset
   251
  Source.of_string #>
5bef6c7cc819 allow Isabelle symbols within low-level ML source;
wenzelm
parents: 31474
diff changeset
   252
  Symbol.source {do_recover = true} #>
5bef6c7cc819 allow Isabelle symbols within low-level ML source;
wenzelm
parents: 31474
diff changeset
   253
  source #>
5bef6c7cc819 allow Isabelle symbols within low-level ML source;
wenzelm
parents: 31474
diff changeset
   254
  Source.exhaust;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   255
30645
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   256
fun read_antiq (syms, pos) =
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   257
  (Source.of_list syms
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   258
    |> Source.source Symbol_Pos.stopper (Scan.bulk (!!! "bad input" scan_antiq))
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   259
      (SOME (false, fn msg => recover msg >> map Antiquote.Text))
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   260
    |> Source.exhaust
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   261
    |> tap (List.app (Antiquote.report report_token))
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   262
    |> tap Antiquote.check_nesting
31426
5c9dbd511510 tuned signature;
wenzelm
parents: 31332
diff changeset
   263
    |> tap (List.app (fn Antiquote.Text tok => ignore (check_content_of tok) | _ => ())))
30645
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   264
  handle ERROR msg =>
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   265
    cat_error msg ("The error(s) above occurred in ML source" ^ Position.str_of pos);
e7943202d177 added read_antiq, with improved error reporting;
wenzelm
parents: 30636
diff changeset
   266
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   267
end;
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   268
24596
f1333a841b26 removed obsolete Selector token;
wenzelm
parents: 24579
diff changeset
   269
end;
24579
852fc50927b1 Lexical syntax for SML.
wenzelm
parents:
diff changeset
   270