-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsmiles.c
2089 lines (1937 loc) · 41.7 KB
/
smiles.c
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2017-2019 Ben Cornett <ben@lantern.is>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Parses SMILES as specified by the OpenSMILES standard.
*/
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "coho.h"
#define ALIPHATIC_ORGANIC 0x00001
#define AROMATIC 0x00002
#define AROMATIC_ORGANIC 0x00004
#define BOND 0x00008
#define BRACKET_CLOSE 0x00010
#define BRACKET_OPEN 0x00020
#define CHIRALITY 0x00040
#define COLON 0x00080
#define DIGIT 0x00100
#define DOT 0x00200
#define ELEMENT 0x00400
#define HYDROGEN 0x00800
#define MINUS 0x01000
#define PAREN_CLOSE 0x02000
#define PAREN_OPEN 0x04000
#define PERCENT 0x08000
#define PLUS 0x10000
#define WILDCARD 0x20000
struct token {
int type;
int position;
const char *s;
size_t n;
int intval;
int flags;
};
static int atom_class(struct coho_smiles *, struct coho_smiles_atom *);
static int add_atom(struct coho_smiles *, struct coho_smiles_atom *);
static int add_bond(struct coho_smiles *, struct coho_smiles_bond *);
static int add_ringbond(struct coho_smiles *, int, struct coho_smiles_bond *);
static int aliphatic_organic(struct coho_smiles *, struct coho_smiles_atom *);
static int aromatic_organic(struct coho_smiles *, struct coho_smiles_atom *);
static int assign_implicit_hydrogen_count(struct coho_smiles *);
static int atom(struct coho_smiles *, int *);
static int atom_ringbond(struct coho_smiles *, int *);
static int atom_valence(struct coho_smiles *, size_t);
static int bond(struct coho_smiles *, struct coho_smiles_bond *b);
static int bracket_atom(struct coho_smiles *, struct coho_smiles_atom *);
static int charge(struct coho_smiles *, struct coho_smiles_atom *);
static int check_ring_closures(struct coho_smiles *);
static int chirality(struct coho_smiles *, struct coho_smiles_atom *);
static int close_paren(struct coho_smiles *, struct coho_smiles_bond *);
static int dot(struct coho_smiles *);
static int ensure_array_capacities(struct coho_smiles *, size_t);
static void finalize_implicit_bond_order(struct coho_smiles *,
struct coho_smiles_bond *);
static int hydrogen_count(struct coho_smiles *, struct coho_smiles_atom *);
static int integer(struct coho_smiles *, size_t, int *);
static int isotope(struct coho_smiles *, struct coho_smiles_atom *);
static unsigned int lex(struct coho_smiles *, struct token *, int);
static int match(struct coho_smiles *, struct token *, int, unsigned int);
static size_t next_array_cap(size_t);
static int open_paren(struct coho_smiles *, struct coho_smiles_bond *);
static int pop_paren_stack(struct coho_smiles *, int, struct coho_smiles_bond *);
static void push_paren_stack(struct coho_smiles *, int,
struct coho_smiles_bond *);
static int ringbond(struct coho_smiles *, int);
static int round_valence(int, int, int);
static void coho_smiles_atom_init(struct coho_smiles_atom *);
static void coho_smiles_bond_init(struct coho_smiles_bond *);
static void coho_smiles_reinit(struct coho_smiles *, const char *, size_t);
static int symbol(struct coho_smiles *, struct coho_smiles_atom *);
static void tokcpy(char *, struct token *, size_t);
static int wildcard(struct coho_smiles *, struct coho_smiles_atom *);
/*
* Table of standard atom valences.
* <atomic number> <valence>...
*/
static int standard_valences[][4] = {
{5, 3, -1, -1}, /* B */
{6, 4, -1, -1}, /* C */
{7, 3, 5, -1}, /* N */
{8, 2, -1, -1}, /* O */
{9, 1, -1, -1}, /* F */
{15, 3, 5, -1}, /* P */
{16, 2, 4, 6}, /* S */
{17, 1, -1, -1}, /* Cl */
{35, 1, -1, -1}, /* Br */
{53, 1, -1, -1}, /* I */
{-1, -1, -1, -1},
};
void coho_smiles_free(struct coho_smiles *x)
{
free(x->atoms);
free(x->bonds);
free(x->paren_stack);
}
void coho_smiles_init(struct coho_smiles *x)
{
size_t i;
x->smiles = NULL;
x->position = 0;
x->end = 0;
x->error[0] = '\0';
x->error_position = -1;
x->atom_count = 0;
x->bond_count = 0;
x->atoms = NULL;
x->atoms_cap = 0;
x->bonds = NULL;
x->bonds_cap = 0;
x->paren_stack = NULL;
x->paren_stack_cap = 0;
for (i = 0; i < 100; i++)
coho_smiles_bond_init(&x->ring_bonds[i]);
x->open_ring_closures = 0;
}
int coho_smiles_read(struct coho_smiles *x, const char *smiles, size_t sz)
{
struct coho_smiles_bond b;
int anum; /* index of last atom read */
int eos; /* end-of-string flag */
int rc;
size_t end;
enum {
INIT,
ATOM_READ,
BOND_READ,
DOT_READ,
OPEN_PAREN_READ,
CLOSE_PAREN_READ,
} state;
end = sz ? sz : strlen(smiles);
if (sz > INT_MAX) {
strlcpy(x->error, "SMILES too long", sizeof(x->error));
return COHO_NOMEM;
}
coho_smiles_reinit(x, smiles, end);
if (ensure_array_capacities(x, end)) {
return COHO_NOMEM;
}
b.atom0 = -1; /* no previous atom to bond to */
anum = -1;
state = INIT;
for (;;) {
eos = x->position == x->end;
switch (state) {
case INIT:
/* Parsing has just begun. */
if (eos) {
strlcpy(x->error, "empty SMILES",
sizeof(x->error));
goto err;
} else if ((rc = atom_ringbond(x, &anum))) {
if (rc == -1)
goto err;
} else {
strlcpy(x->error, "atom expected",
sizeof(x->error));
goto err;
}
state = ATOM_READ;
break;
case ATOM_READ:
/* An atom has just been read. */
/*
* If there is an open bond to the previous
* atom, complete it.
*/
if (b.atom0 != -1) {
b.atom1 = anum;
finalize_implicit_bond_order(x, &b);
if (add_bond(x, &b) == -1)
goto err;
}
/*
* The atom just read may be bonded to subsequent
* atoms. Store this state in an incomplete bond.
*/
coho_smiles_bond_init(&b);
b.atom0 = anum;
b.is_implicit = 1;
if (eos) {
goto done;
} else if ((rc = atom_ringbond(x, &anum))) {
if (rc == -1)
goto err;
} else if ((rc = bond(x, &b))) {
if (rc == -1)
goto err;
state = BOND_READ;
} else if (dot(x)) {
state = DOT_READ;
} else if (open_paren(x, &b)) {
state = OPEN_PAREN_READ;
} else if ((rc = close_paren(x, &b))) {
if (rc == -1)
goto err;
state = CLOSE_PAREN_READ;
} else {
goto unexpected;
}
break;
case DOT_READ:
/*
* A dot (.) has just been read. An atom is expected.
* If there is a bond to a previous atom awaiting
* completion, it must be cancelled.
*/
/* Invalidate open bond to previous atom. */
b.atom0 = -1;
if ((rc = atom_ringbond(x, &anum))) {
if (rc == -1)
goto err;
} else {
strlcpy(x->error, "atom must follow dot",
sizeof(x->error));
goto err;
}
state = ATOM_READ;
break;
case BOND_READ:
/*
* A bond (-, =, #, etc) has just been read.
* An atom is expected.
*/
if ((rc = atom_ringbond(x, &anum))) {
if (rc == -1)
goto err;
} else {
strlcpy(x->error, "atom must follow bond",
sizeof(x->error));
goto err;
}
state = ATOM_READ;
break;
case OPEN_PAREN_READ:
/*
* An opening parenthesis has just been read
* and the parenthesis stack pushed.
*/
if (eos) {
strlcpy(x->error, "unbalanced parenthesis",
sizeof(x->error));
x->error_position = x->position - 1;
goto err;
} else if ((rc = atom_ringbond(x, &anum))) {
if (rc == -1)
goto err;
state = ATOM_READ;
} else if ((rc = bond(x, &b))) {
if (rc == -1)
goto err;
state = BOND_READ;
} else if (dot(x)) {
state = DOT_READ;
} else {
strlcpy(x->error, "atom, bond, or dot expected",
sizeof(x->error));
goto err;
}
break;
case CLOSE_PAREN_READ:
/*
* A closing parenthesis has just been read
* and the parenthesis stack popped.
*/
if (eos) {
goto done;
} else if ((rc = atom_ringbond(x, &anum))) {
if (rc == -1)
goto err;
state = ATOM_READ;
} else if ((rc = bond(x, &b))) {
if (rc == -1)
goto err;
state = BOND_READ;
} else if (dot(x)) {
state = DOT_READ;
} else if (open_paren(x, &b)) {
state = OPEN_PAREN_READ;
} else if ((rc = close_paren(x, &b))) {
if (rc == -1)
goto err;
state = CLOSE_PAREN_READ;
} else {
goto unexpected;
}
break;
}
}
done:
assert(x->position == x->end);
if (check_ring_closures(x))
goto err;
if (x->paren_stack_count > 0) {
strlcpy(x->error, "unbalanced parenthesis",
sizeof(x->error));
x->error_position = x->paren_stack[0].position;
goto err;
}
if (assign_implicit_hydrogen_count(x))
goto err;
return COHO_OK;
unexpected:
strlcpy(x->error, "unexpected character", sizeof(x->error));
err:
if (x->error_position == -1)
x->error_position = x->position;
return COHO_ERROR;
}
/*
* Parses optional atom class inside a bracket atom (ex: [C:23]).
* If successful, sets a->atom_class and increments a->length.
* Returns 1 if atom class was read, else 0.
* On error, sets x->error and returns -1.
*/
static int atom_class(struct coho_smiles *x, struct coho_smiles_atom *a)
{
struct token t;
int n;
if (!match(x, &t, 1, COLON))
return 0;
a->length += t.n;
if ((n = integer(x, 8, &a->atom_class)) == -1) {
strlcpy(x->error, "atom class too large", sizeof(x->error));
return -1;
} else if (n == 0) {
strlcpy(x->error, "atom class expected", sizeof(x->error));
return -1;
}
a->length += n;
return 1;
}
/*
* Saves a completed atom and returns its index.
*/
static int add_atom(struct coho_smiles *x, struct coho_smiles_atom *a)
{
x->atoms[x->atom_count] = *a;
return x->atom_count++;
}
/*
* Saves a new bond to the bond list and returns its index.
* Returns new length of bond list on success.
* If the bond is already in the list, sets x->error and returns -1.
* Bonds are added so that bond->atom0 < bond->atom1 and the entire bond list
* remains sorted.
*/
static int add_bond(struct coho_smiles *x, struct coho_smiles_bond *bond)
{
size_t i, move;
struct coho_smiles_bond nb, *b;
nb = *bond;
/* Flip so atom0 < atom1 */
if (bond->atom0 > bond->atom1) {
nb.atom0 = bond->atom1;
nb.atom1 = bond->atom0;
if (bond->stereo == COHO_SMILES_BOND_STEREO_UP)
nb.stereo = COHO_SMILES_BOND_STEREO_DOWN;
else if (bond->stereo == COHO_SMILES_BOND_STEREO_DOWN)
nb.stereo = COHO_SMILES_BOND_STEREO_UP;
}
/*
* Find position to insert and check for duplicates.
* Start search from end, since bonds are
* mostly generated in the correct order.
*/
for (i = x->bond_count; i > 0; i--) {
b = &x->bonds[i-1];
if (nb.atom0 > b->atom0)
break;
else if (nb.atom0 < b->atom0)
continue;
else if (nb.atom1 > b->atom1)
break;
else if (nb.atom1 < b->atom1)
continue;
else {
strlcpy(x->error, "duplicate bond", sizeof(x->error));
x->error_position = nb.position;
return -1;
}
}
move = x->bond_count - i; /* # elements to shift */
if (move) {
memmove(x->bonds + i + 1, x->bonds + i,
move * sizeof(x->bonds[0]));
}
x->bonds[i] = nb;
return x->bond_count++;
}
/*
* Adds a ring bond closure.
* If there is already an open ring bond using rnum,
* it is closed and the new bond is added to the bond list.
* Otherwise, a new bond is opened.
* Returns 0 on success.
* On failure, sets x->error and returns -1.
*/
static int add_ringbond(struct coho_smiles *x, int rnum,
struct coho_smiles_bond *b)
{
struct coho_smiles_bond *rb;
assert(rnum < 100);
if (b->order == COHO_SMILES_BOND_UNSPECIFIED)
assert(b->stereo == COHO_SMILES_BOND_STEREO_UNSPECIFIED);
rb = &x->ring_bonds[rnum];
if (rb->atom0 == -1) {
rb->atom0 = b->atom0;
rb->order = b->order;
rb->stereo = b->stereo;
rb->is_implicit = 0;
rb->is_ring = 1;
rb->position = b->position;
rb->length = b->length;
x->open_ring_closures++;
return 0;
}
/* Close the open bond */
if (rb->atom0 == b->atom0) {
strlcpy(x->error, "atom ring-bonded to itself",
sizeof(x->error));
x->error_position = x->atoms[b->atom0].position;
return -1;
}
if (rb->order == COHO_SMILES_BOND_UNSPECIFIED)
rb->order = b->order;
else if (b->order == COHO_SMILES_BOND_UNSPECIFIED)
; /* pass */
else if (rb->order != b->order) {
strlcpy(x->error, "conflicting ring bond orders",
sizeof(x->error));
x->error_position = x->atoms[b->atom0].position;
return -1;
}
if (rb->order == COHO_SMILES_BOND_UNSPECIFIED)
rb->order = COHO_SMILES_BOND_SINGLE;
rb->atom1 = b->atom0;
if (add_bond(x, rb) == -1)
return -1;
coho_smiles_bond_init(rb);
rb->atom0 = -1; ; /* mark slot open again */
x->open_ring_closures--;
return 0;
}
/*
* Matches an aliphatic organic atom (C, N, O, etc.).
* Returns 1 on match, 0 if no match, or -1 on error.
*/
static int aliphatic_organic(struct coho_smiles *x, struct coho_smiles_atom *a)
{
struct token t;
if (!match(x, &t, 0, ALIPHATIC_ORGANIC))
return 0;
coho_smiles_atom_init(a);
a->position = t.position;
a->atomic_number = t.intval;
a->is_organic = 1;
a->length = t.n;
tokcpy(a->symbol, &t, sizeof(a->symbol));
return 1;
}
/*
* Matches an aromatic organic atom (c, n, o, etc.).
* Returns 1 on match, 0 if no match, or -1 on error.
*/
static int aromatic_organic(struct coho_smiles *x,
struct coho_smiles_atom *a)
{
struct token t;
if (!match(x, &t, 0, AROMATIC_ORGANIC))
return 0;
coho_smiles_atom_init(a);
a->position = t.position;
a->atomic_number = t.intval;
a->is_organic = 1;
a->is_aromatic = 1;
a->length = t.n;
tokcpy(a->symbol, &t, sizeof(a->symbol));
return 1;
}
/*
* Assigns implicit hydrogen counts for all atoms that were
* specified using the organic-subset shorthand.
*/
static int assign_implicit_hydrogen_count(struct coho_smiles *x)
{
int i, valence, std;
struct coho_smiles_atom *a;
for (i = 0; i < x->atom_count; i++) {
a = &x->atoms[i];
if (!a->is_organic)
continue;
valence = atom_valence(x, i);
std = round_valence(a->atomic_number, valence, a->is_aromatic);
if (std == -1)
a->implicit_hydrogen_count = 0;
else
a->implicit_hydrogen_count = std - valence;
}
return 0;
}
/*
* Matches an atom or returns 0 if not found.
* If successful, stores the index of the new atom in *anum and returns 1.
* On error, sets x->error and returns -1.
*/
static int atom(struct coho_smiles *x, int *atom_index)
{
struct coho_smiles_atom a;
int rc;
if ((rc = bracket_atom(x, &a)) ||
(rc = aliphatic_organic(x, &a)) ||
(rc = aromatic_organic(x, &a)) ||
(rc = wildcard(x, &a))) {
if (rc == -1)
return -1;
} else {
return 0;
}
*atom_index = add_atom(x, &a);
return 1;
}
/*
* Matches an atom followed by zero or more ringbonds.
* On success, stores the index of the new atom in *anum and returns 1.
* Returns 0 if there is no match.
* On error, sets x->error and returns -1.
*/
static int atom_ringbond(struct coho_smiles *x, int *anum)
{
int rc;
if ((rc = atom(x, anum))) {
if (rc == -1 )
return -1;
} else {
return 0;
}
while ((rc = ringbond(x, *anum)))
if (rc == -1 )
return -1;
return 1;
}
/*
* Computes the valence of an atom by summing the orders
* of its bonds.
* Treats aromatic atoms as a special case in an attempt to
* properly derive implicit hydrogen count.
*/
static int atom_valence(struct coho_smiles *x, size_t idx)
{
int i;
int valence, neighbors;
struct coho_smiles_bond *b;
valence = 0;
neighbors = 0;
for (i = 0; i < x->bond_count; i++) {
b = &x->bonds[i];
if (b->atom0 > (int)idx)
break;
else if (b->atom0 != (int)idx && b->atom1 != (int)idx)
continue;
if (b->order == COHO_SMILES_BOND_SINGLE)
valence += 1;
else if (b->order == COHO_SMILES_BOND_AROMATIC)
valence += 1;
else if (b->order == COHO_SMILES_BOND_DOUBLE)
valence += 2;
else if (b->order == COHO_SMILES_BOND_TRIPLE)
valence += 3;
else if (b->order == COHO_SMILES_BOND_QUAD)
valence += 4;
neighbors += 1;
}
if (x->atoms[idx].is_aromatic && valence == neighbors) {
valence += 1;
}
return valence;
}
/*
* Matches a bond or returns 0 if not found.
* If found, sets fields of *b and returns 1.
* Only sets fields that can be determined by the matching bond
* token (order, stereo, position, and length).
* Clears implicit flag.
* Doesn't set bond atoms.
*/
static int bond(struct coho_smiles *x, struct coho_smiles_bond *b)
{
struct token t;
if (!match(x, &t, 0, BOND))
return 0;
b->order = t.intval;
b->stereo = t.flags;
b->is_implicit = 0;
b->position = t.position;
b->length = t.n;
return 1;
}
/*
* Matches a bracket atom or returns 0 if not found.
* If found, initializes the atom, sets its fields, and returns 1.
* On error, sets x->error and returns -1.
*/
static int bracket_atom(struct coho_smiles *x, struct coho_smiles_atom *a)
{
struct token t;
if (!match(x, &t, 0, BRACKET_OPEN))
return 0;
coho_smiles_atom_init(a);
a->is_bracket = 1;
a->position = t.position;
a->length = t.n;
if (isotope(x, a) == -1)
return -1;
if (symbol(x, a) == 0) {
strlcpy(x->error, "atom symbol expected", sizeof(x->error));
return -1;
}
if (chirality(x, a) == -1)
return -1;
if (hydrogen_count(x, a) == -1)
return -1;
if (charge(x, a) == -1)
return -1;
if (atom_class(x, a) == -1)
return -1;
if (!match(x, &t, 0, BRACKET_CLOSE)) {
strlcpy(x->error, "bracket atom syntax error", sizeof(x->error));
return -1;
}
a->length += t.n;
return 1;
}
/*
* Returns 0 if all rings have been closed.
* Otherwise, sets x->error and returns -1.
*/
static int check_ring_closures(struct coho_smiles *x)
{
size_t i;
if (x->open_ring_closures == 0)
return 0;
strlcpy(x->error, "unclosed ring bond", sizeof(x->error));
for (i = 0; i < 100; i++) {
if (x->ring_bonds[i].atom0 != -1) {
x->error_position = x->ring_bonds[i].position;
break;
}
}
return -1;
}
/*
* Parses optional charge inside a bracket atom.
* If successful, sets a->charge and increments a->length.
* Returns 1 if charge was read, else 0.
* On error, sets x->error and returns -1.
*/
static int charge(struct coho_smiles *x, struct coho_smiles_atom *a)
{
struct token t;
int sign;
int n;
int length;
if (!match(x, &t, 1, PLUS | MINUS))
return 0;
sign = t.intval;
length = t.n;
if ((n = integer(x, 2, &a->charge)) == -1) {
strlcpy(x->error, "charge too large", sizeof(x->error));
return -1;
} else if (n) {
a->charge *= sign;
length += n;
} else {
a->charge = sign;
if (lex(x, &t, 1) & (PLUS | MINUS)) {
if (t.intval == sign) {
x->position += t.n;
a->charge *= 2;
length += t.n;
}
}
}
a->length += length;
return 1;
}
/*
* Parses chirality inside a bracket atom.
* If successful, sets a->chirality and increments a->length.
* Returns 1 if chirality was read, else 0.
* TODO: Currently, this only understands @ and @@.
*/
static int chirality(struct coho_smiles *x, struct coho_smiles_atom *a)
{
struct token t;
if (!match(x, &t, 1, CHIRALITY))
return 0;
tokcpy(a->chirality, &t, sizeof(a->chirality));
a->length += t.n;
return 1;
}
/*
* Matches a closing parenthesis that ends a branch.
* On success, pops the parenthesis stack and returns 1.
* Returns 0 if there was no match.
* On error, sets x->error and returns -1.
*/
static int close_paren(struct coho_smiles *x, struct coho_smiles_bond *b)
{
struct token t;
if (!match(x, &t, 0, PAREN_CLOSE))
return 0;
if (pop_paren_stack(x, t.position, b))
return -1;
return 1;
}
/*
* Matches dot, the no-bond specifier.
* Returns 1 on success, 0 if there was no match.
*/
static int dot(struct coho_smiles *x)
{
struct token t;
return match(x, &t, 0, DOT);
}
static int ensure_array_capacities(struct coho_smiles *x, size_t smiles_length)
{
size_t new_cap;
void *p;
/*
* Maximum required storage is bounded by length of SMILES string.
*/
if (x->atoms_cap >= smiles_length)
return 0;
new_cap = next_array_cap(smiles_length);
#define GROW(name) \
do { \
p = reallocarray(x->name, new_cap, sizeof(x->name[0])); \
if (p == NULL) \
return -1; \
x->name = p; \
x->name##_cap = new_cap; \
} while (0)
GROW(atoms);
GROW(bonds);
GROW(paren_stack);
#undef GROW
return 0;
}
/*
* Sets the order of an implicit bond according to
* the aromaticity of the two atoms.
*/
static void finalize_implicit_bond_order(struct coho_smiles *x,
struct coho_smiles_bond *b)
{
if (!b->is_implicit)
return;
if (x->atoms[b->atom0].is_aromatic && x->atoms[b->atom1].is_aromatic)
b->order = COHO_SMILES_BOND_AROMATIC;
else
b->order = COHO_SMILES_BOND_SINGLE;
}
/*
* Parses hydrogen count inside a bracket atom.
* If successful, sets a->hydrogen_count and increments a->length.
* Returns 1 if hydrogen_count was read, else 0.
*/
static int hydrogen_count(struct coho_smiles *x, struct coho_smiles_atom *a)
{
struct token t;
if (!match(x, &t, 1, HYDROGEN))
return 0;
a->length += t.n;
if (match(x, &t, 1, DIGIT)) {
a->hydrogen_count = t.intval;
a->length += t.n;
} else {
a->hydrogen_count = 1;
}
return 1;
}
/*
* Matches an integer up to maxdigit long.
* On success, stores the integer in *dst and returns number of digits.
* Returns 0 if no digits are available.
* Returns -1 if maxdigit is exceeded.
*/
static int integer(struct coho_smiles *x, size_t maxdigit, int *dst)
{
size_t i;
int n = 0;
int saved = x->position;
struct token t;
for (i = 0; lex(x, &t, 0) & DIGIT; i++) {
if (maxdigit && i == maxdigit) {
x->position = saved;
return -1;
}
x->position += t.n;
n = n * 10 + t.intval;
}
if (i == 0)
return 0;
*dst = n;
return i;
}
/*
* Parses isotope inside a bracket atom.
* If successful, sets a->isotope and increments a->length.
* Returns 1 if isotope was read, else 0.
* On error, returns -1 and sets x->error.
*/
static int isotope(struct coho_smiles *x, struct coho_smiles_atom *a)
{
int n;
if ((n = integer(x, 5, &a->isotope)) == -1) {
strlcpy(x->error, "isotope too large", sizeof(x->error));
return -1;
}
a->length += n;
return 0;
}
/*
* Reads next token and checks if its type is among those requested.
* If so, consumes the token and returns 1.
* If not, returns 0 and the parsing position remains unchanged.
*/
static int match(struct coho_smiles *x, struct token *t, int inbracket,
unsigned int ttype)
{
if (lex(x, t, inbracket) & ttype) {
x->position += t->n;
return 1;
}
return 0;
}
/*
* Returns a new array capacity that is larger than
* its previous capacity.
*/
static size_t next_array_cap(size_t previous_cap)
{
size_t cap = 2 * previous_cap - 1;
while (cap & (cap - 1))
cap = cap & (cap - 1);
return cap;
}
/*
* Matches an opening parenthesis that begins a branch.
* On success, pushes the parenthesis stack and returns 1.
* Returns 0 if there was no match.
*/
static int open_paren(struct coho_smiles *x, struct coho_smiles_bond *b)
{
struct token t;
if (!match(x, &t, 0, PAREN_OPEN))
return 0;
push_paren_stack(x, t.position, b);
return 1;
}
/*
* Pops the parenthesis stack that holds the open bonds to