1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Turns the string of a molecule into a sequence of tokens
//!
//! It picks out bond marks, ring marks, the contents of square brackets, the parentheses of
//! branches, and atom labels.
//! A subscript or superscript mark always takes the next item with it.
//! This keeps the `-` in `H^-` from being read as a bond.
use crate::error::Error;
use crate::node::Kind;
/// One token
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
/// A bond mark
Bond(Kind),
/// A ring mark. circle is the circle of an aromatic ring written with **
Ring {size: usize, circle: bool},
/// Contents of square brackets, without the brackets themselves
Setting(String),
/// Start of a branch or a ring
Open,
/// End of a branch or a ring
Close,
/// Ring closure mark ?
Hook,
/// An atom label
Atom(String),
}
/// A token and its position in the original string
#[derive(Debug, Clone, PartialEq)]
pub struct Located {
pub token: Token,
pub position: usize,
}
/// Characters at which an atom label ends
fn breaks(character: char) -> bool {
matches!(
character,
'-' | '=' | '~' | '<' | '>' | '*' | '(' | ')' | '[' | ']' | '?' | '!' | '\\'
)
}
/// Characters used in notation that cannot be read
fn reserved(character: char) -> Option<&'static str> {
match character {
'!' => Some("部分構造の呼び出し ! はまだ読めません"),
'\\' => Some("読めるコマンドは \\chemfig と \\chemname だけで、どちらも外側に書きます"),
_ => None,
}
}
/// Turns a string into a sequence of tokens
pub fn tokenize(source: &str) -> Result<Vec<Located>, Error> {
let character: Vec<char> = source.chars().collect();
let mut found = Vec::new();
let mut index = 0;
while index < character.len() {
let start = index;
match character[index] {
' ' | '\t' | '\n' | '\r' => {
index += 1;
}
'-' | '=' | '~' | '<' | '>' => {
let (kind, next) = read_bond(&character, index);
found.push(Located {token: Token::Bond(kind), position: start});
index = next;
}
'*' => {
let (token, next) = read_ring(&character, index)?;
found.push(Located {token, position: start});
index = next;
}
'(' => {
found.push(Located {token: Token::Open, position: start});
index += 1;
}
')' => {
found.push(Located {token: Token::Close, position: start});
index += 1;
}
'?' => {
found.push(Located {token: Token::Hook, position: start});
index += 1;
}
'[' => {
let (inside, next) = read_setting(&character, index)?;
found.push(Located {token: Token::Setting(inside), position: start});
index = next;
}
']' => return Err(Error::new("対応する [ のない ] です", start)),
'}' => return Err(Error::new("対応する { のない } です", start)),
other => {
if let Some(reason) = reserved(other) {
return Err(Error::new(reason, start));
}
let (label, next) = read_atom(&character, index)?;
found.push(Located {token: Token::Atom(label), position: start});
index = next;
}
}
}
Ok(found)
}
/// Reads a bond mark. A wedge may be followed by : or |
fn read_bond(character: &[char], at: usize) -> (Kind, usize) {
let next = character.get(at + 1).copied();
match (character[at], next) {
('-', _) => (Kind::Single, at + 1),
('=', _) => (Kind::Double, at + 1),
('~', _) => (Kind::Triple, at + 1),
('>', Some(':')) => (Kind::Hash, at + 2),
('>', Some('|')) => (Kind::Hollow, at + 2),
('>', _) => (Kind::Wedge, at + 1),
('<', Some(':')) => (Kind::HashBack, at + 2),
('<', Some('|')) => (Kind::HollowBack, at + 2),
_ => (Kind::WedgeBack, at + 1),
}
}
/// Reads a ring mark. * is a plain ring, and ** is a ring with a circle drawn inside.
/// The size is written right after it
fn read_ring(character: &[char], at: usize) -> Result<(Token, usize), Error> {
let circle = character.get(at + 1) == Some(&'*');
let mut index = if circle {at + 2} else {at + 1};
let start = index;
let mut digit = String::new();
while let Some(found) = character.get(index) {
if !found.is_ascii_digit() {
break;
}
digit.push(*found);
index += 1;
}
if digit.is_empty() {
return Err(Error::new("環の大きさを書いてください。6 員環なら *6(...) です", start));
}
match digit.parse::<usize>() {
Ok(size) if size >= 3 => Ok((Token::Ring {size, circle}, index)),
Ok(_) => Err(Error::new("環の大きさは 3 以上です", start)),
Err(_) => Err(Error::new("環の大きさが大きすぎます", start)),
}
}
/// Reads the contents of square brackets. It is called at the opening bracket and returns the
/// position just after the closing bracket
fn read_setting(character: &[char], at: usize) -> Result<(String, usize), Error> {
let mut index = at + 1;
let mut inside = String::new();
while index < character.len() {
if character[index] == ']' {
return Ok((inside, index + 1));
}
inside.push(character[index]);
index += 1;
}
Err(Error::new("[ が閉じていません", at))
}
/// Reads an atom label, up to a bond mark or a bracket, or to the end of the string
fn read_atom(character: &[char], at: usize) -> Result<(String, usize), Error> {
let mut index = at;
let mut label = String::new();
while index < character.len() {
match character[index] {
' ' | '\t' | '\n' | '\r' => {
index += 1;
}
'}' => return Err(Error::new("対応する { のない } です", index)),
'{' => {
let (inside, next) = read_group(character, index)?;
label.push('{');
label.push_str(&inside);
label.push('}');
index = next;
}
'_' | '^' => {
label.push(character[index]);
index += 1;
let (inside, next) = read_unit(character, index)?;
label.push_str(&inside);
index = next;
}
other => {
if breaks(other) {
break;
}
label.push(other);
index += 1;
}
}
}
Ok((label, index))
}
/// Reads the contents of braces, counting nested braces too. It is called at the opening brace
/// and returns the position just after the closing brace
fn read_group(character: &[char], at: usize) -> Result<(String, usize), Error> {
let mut index = at + 1;
let mut depth = 1;
let mut inside = String::new();
while index < character.len() {
match character[index] {
'{' => {
depth += 1;
inside.push('{');
}
'}' => {
depth -= 1;
if depth == 0 {
return Ok((inside, index + 1));
}
inside.push('}');
}
other => inside.push(other),
}
index += 1;
}
Err(Error::new("{ が閉じていません", at))
}
/// The one item that a subscript or superscript takes with it.
/// A group in braces, or else a single character
fn read_unit(character: &[char], at: usize) -> Result<(String, usize), Error> {
match character.get(at) {
None => Err(Error::new("_ か ^ の後ろに何もありません", at)),
Some('{') => {
let (inside, next) = read_group(character, at)?;
Ok((format!("{{{inside}}}"), next))
}
Some(other) => Ok((other.to_string(), at + 1)),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn kinds(source: &str) -> Vec<Token> {
tokenize(source).unwrap().into_iter().map(|found| found.token).collect()
}
#[test]
fn splits_a_chain() {
assert_eq!(
kinds("H-O-H"),
vec![
Token::Atom("H".into()),
Token::Bond(Kind::Single),
Token::Atom("O".into()),
Token::Bond(Kind::Single),
Token::Atom("H".into()),
]
);
}
#[test]
fn keeps_the_bracket_contents() {
assert_eq!(
kinds("A-[:30,1.5]B"),
vec![
Token::Atom("A".into()),
Token::Bond(Kind::Single),
Token::Setting(":30,1.5".into()),
Token::Atom("B".into()),
]
);
}
#[test]
fn a_script_takes_the_next_one_with_it() {
// The - after ^ is not a bond
assert_eq!(kinds("H^-"), vec![Token::Atom("H^-".into())]);
assert_eq!(kinds("SO_4^{2-}"), vec![Token::Atom("SO_4^{2-}".into())]);
}
#[test]
fn subscripts_stay_in_the_atom() {
assert_eq!(
kinds("H_2O-H"),
vec![
Token::Atom("H_2O".into()),
Token::Bond(Kind::Single),
Token::Atom("H".into()),
]
);
}
#[test]
fn branches_come_out_as_their_own_tokens() {
assert_eq!(
kinds("C(-H)"),
vec![
Token::Atom("C".into()),
Token::Open,
Token::Bond(Kind::Single),
Token::Atom("H".into()),
Token::Close,
]
);
}
#[test]
fn spaces_fall_away() {
assert_eq!(kinds("H - O"), kinds("H-O"));
}
#[test]
fn double_and_triple_bonds() {
assert_eq!(kinds("O=C=O")[1], Token::Bond(Kind::Double));
assert_eq!(kinds("N~N")[1], Token::Bond(Kind::Triple));
}
#[test]
fn the_six_cram_bonds() {
assert_eq!(kinds("A>B")[1], Token::Bond(Kind::Wedge));
assert_eq!(kinds("A<B")[1], Token::Bond(Kind::WedgeBack));
assert_eq!(kinds("A>:B")[1], Token::Bond(Kind::Hash));
assert_eq!(kinds("A<:B")[1], Token::Bond(Kind::HashBack));
assert_eq!(kinds("A>|B")[1], Token::Bond(Kind::Hollow));
assert_eq!(kinds("A<|B")[1], Token::Bond(Kind::HollowBack));
}
#[test]
fn a_ring_says_its_size() {
assert_eq!(kinds("*6(------)")[0], Token::Ring {size: 6, circle: false});
assert_eq!(kinds("**6(------)")[0], Token::Ring {size: 6, circle: true});
assert_eq!(kinds("*12(-)")[0], Token::Ring {size: 12, circle: false});
}
#[test]
fn a_ring_opens_with_a_bracket() {
let found = kinds("*3(---)");
assert_eq!(found[0], Token::Ring {size: 3, circle: false});
assert_eq!(found[1], Token::Open);
assert_eq!(found[5], Token::Close);
}
#[test]
fn unclosed_brackets_are_reported() {
assert!(tokenize("A-[:30B").is_err());
assert!(tokenize("A_{2").is_err());
assert!(tokenize("A]").is_err());
assert!(tokenize("A}").is_err());
}
#[test]
fn a_ring_without_a_size_is_reported() {
assert!(tokenize("*(---)").is_err());
assert!(tokenize("*2(--)").is_err());
}
#[test]
fn a_hook_is_its_own_token() {
assert_eq!(kinds("A?-B?"), vec![
Token::Atom("A".into()),
Token::Hook,
Token::Bond(Kind::Single),
Token::Atom("B".into()),
Token::Hook,
]);
assert_eq!(kinds("A?[b,=]")[1], Token::Hook);
assert_eq!(kinds("A?[b,=]")[2], Token::Setting("b,=".into()));
}
#[test]
fn the_notations_still_to_come_say_so() {
assert!(tokenize("!{x}").is_err());
}
}