42 |
42 |
43 |
43 |
44 (* datatype token *) |
44 (* datatype token *) |
45 |
45 |
46 datatype kind = |
46 datatype kind = |
47 Keyword | Ident | String | Space | Comment | Antiq of Antiquote.antiq | EOF; |
47 Keyword | Ident | String | Space | Comment of Comment.kind | Antiq of Antiquote.antiq | EOF; |
48 |
48 |
49 datatype token = Token of Position.range * (kind * string); |
49 datatype token = Token of Position.range * (kind * string); |
50 |
50 |
51 fun pos_of (Token ((pos, _), _)) = pos; |
51 fun pos_of (Token ((pos, _), _)) = pos; |
52 fun end_pos_of (Token ((_, pos), _)) = pos; |
52 fun end_pos_of (Token ((_, pos), _)) = pos; |
58 |
58 |
59 fun kind_of (Token (_, (k, _))) = k; |
59 fun kind_of (Token (_, (k, _))) = k; |
60 fun content_of (Token (_, (_, x))) = x; |
60 fun content_of (Token (_, (_, x))) = x; |
61 |
61 |
62 fun is_proper (Token (_, (Space, _))) = false |
62 fun is_proper (Token (_, (Space, _))) = false |
63 | is_proper (Token (_, (Comment, _))) = false |
63 | is_proper (Token (_, (Comment _, _))) = false |
64 | is_proper _ = true; |
64 | is_proper _ = true; |
65 |
65 |
66 |
66 |
67 (* diagnostics *) |
67 (* diagnostics *) |
68 |
68 |
69 val print_kind = |
69 val print_kind = |
70 fn Keyword => "rail keyword" |
70 fn Keyword => "rail keyword" |
71 | Ident => "identifier" |
71 | Ident => "identifier" |
72 | String => "single-quoted string" |
72 | String => "single-quoted string" |
73 | Space => "white space" |
73 | Space => "white space" |
74 | Comment => "formal comment" |
74 | Comment _ => "formal comment" |
75 | Antiq _ => "antiquotation" |
75 | Antiq _ => "antiquotation" |
76 | EOF => "end-of-input"; |
76 | EOF => "end-of-input"; |
77 |
77 |
78 fun print (Token ((pos, _), (k, x))) = |
78 fun print (Token ((pos, _), (k, x))) = |
79 (if k = EOF then print_kind k else print_kind k ^ " " ^ quote x) ^ |
79 (if k = EOF then print_kind k else print_kind k ^ " " ^ quote x) ^ |
116 |
116 |
117 val err_prefix = "Rail lexical error: "; |
117 val err_prefix = "Rail lexical error: "; |
118 |
118 |
119 val scan_token = |
119 val scan_token = |
120 scan_space >> token Space || |
120 scan_space >> token Space || |
121 Symbol_Pos.scan_comment_cartouche err_prefix >> token Comment || |
121 Comment.scan >> (fn (kind, ss) => token (Comment kind) ss)|| |
122 Antiquote.scan_antiq >> antiq_token || |
122 Antiquote.scan_antiq >> antiq_token || |
123 scan_keyword >> (token Keyword o single) || |
123 scan_keyword >> (token Keyword o single) || |
124 Lexicon.scan_id >> token Ident || |
124 Lexicon.scan_id >> token Ident || |
125 Symbol_Pos.scan_string_q err_prefix >> (fn (pos1, (ss, pos2)) => |
125 Symbol_Pos.scan_string_q err_prefix >> (fn (pos1, (ss, pos2)) => |
126 [Token (Position.range (pos1, pos2), (String, Symbol_Pos.content ss))]); |
126 [Token (Position.range (pos1, pos2), (String, Symbol_Pos.content ss))]); |