aboutsummaryrefslogtreecommitdiff
path: root/lib/xixanta/src/parser.rs
blob: 8429c2ea1e8dbd2336abb3d24dccc442f1f67014 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
use crate::errors::ParseError;
use crate::node::{NodeBodyType, NodeType, OperationType, PNode, PString};
use crate::opcodes::{CONTROL_FUNCTIONS, INSTRUCTIONS};
use std::cmp::Ordering;
use std::io::{self, BufRead, Read};

/// The Parser struct holds basic data for the current parsing session.
#[derive(Default)]
pub struct Parser {
    // The current line number.
    line: usize,

    // The current column number.
    column: usize,

    // The offset of the string being evaluated as a "line". Note that this can
    // vary wildly because on recursive expression parsing the line might be
    // slightly different. This property allows us to have a proper value for
    // each iteration.
    offset: usize,

    /// The nodes that have been evaluated for the current parsing session. You
    /// can count on this vector to be filled after calling
    /// `parser::Parser::parse`. Note that it's a vector of vectors to handle
    /// statements which contain a block (e.g. '.macro'). In the end of the
    /// parsing session, though, this vector of vectors is guaranteed to have
    /// len() == 1, which means that there's only one code "layer" in the end
    /// because the other blocks have been closed (or forced to be closed on bad
    /// .end placements).
    nodes: Vec<Vec<PNode>>,

    /// This is a stack of node types that we are expecting for closing the
    /// currently open inner block. This is done this way to guarantee that end
    /// statements (e.g. '.endproc') go with their respective start ones (e.g.
    /// '.proc'), and they don't close another block.
    bodies: Vec<NodeType>,
}

impl Parser {
    /// Parse the input from the given `reader`. You can then access the results
    /// from the `nodes` field. Otherwise, a vector of ParseError's might be
    /// returned.
    pub fn parse(&mut self, reader: impl Read) -> Result<(), Vec<ParseError>> {
        let mut errors = Vec::new();

        self.nodes.push(vec![]);

        for line in io::BufReader::new(reader).lines() {
            match line {
                Ok(l) => {
                    if let Err(err) = self.parse_line(l.as_str()) {
                        errors.push(err);
                    }
                }
                Err(_) => errors.push(self.parser_error("could not get line")),
            }
            self.line += 1;
        }

        // Are there any more statements which are begging for a closing
        // statement? If so, then there's something wrong.
        if !self.bodies.is_empty() {
            errors.push(
                self.parser_error(
                    format!(
                        "expecting a '{}' but there are no more statements",
                        self.bodies.last().unwrap()
                    )
                    .as_str(),
                ),
            );
        }

        // Truncate the nodes level to 1, just in case there were any errors on
        // not closing a .macro statement or something similar.
        self.nodes.truncate(1);

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    pub fn nodes(&self) -> Vec<PNode> {
        // println!("{:#?}", self.nodes);
        self.nodes.first().unwrap().to_vec()
    }

    // Parse a single `line` and push the parsed nodes into `self.nodes`.
    fn parse_line(&mut self, line: &str) -> Result<(), ParseError> {
        self.column = 0;
        self.offset = 0;

        // Skip until the first non-whitespace character. If that's not
        // possible, then it's an empty line and we can return early.
        if !self.skip_whitespace(line) {
            return Ok(());
        }

        // Let's pin point the last character we need to care for parsing. This
        // can be either the start position of an inline comment (i.e. ';'), or
        // the real line end.
        let end = if let Some(comment) = line.find(';') {
            comment
        } else {
            line.len()
        };

        // It's safe to trim the end of the resulting string. Moreover, doing so
        // can already show lines which are actually empty (e.g. a line which
        // simply contains a comment). If this is the case, just return an empty
        // node.
        let mut l = line.get(self.column..end).unwrap_or_default().trim_end();
        if l.is_empty() {
            return Ok(());
        }

        // Fetch the first element of the line, which we will call it an
        // "identifier" but might be a label or a statement. The label might be
        // followed by more code. Hence, push it first, then fetch the next
        // identifier and finally fall through.
        self.offset = 0;
        let (mut id, mut nt) = self.parse_identifier(l)?;
        if nt == NodeType::Label {
            self.nodes.last_mut().unwrap().push(PNode {
                node_type: nt,
                value: id,
                left: None,
                right: None,
                args: None,
            });

            self.skip_whitespace(l);

            // Is it the label alone? If so return early.
            l = line.get(self.column..end).unwrap_or_default().trim_end();
            if l.is_empty() {
                return Ok(());
            }

            // The label is followed by a statement. Let's parse the identifier
            // for it and fall through.
            self.offset = 0;
            (id, nt) = self.parse_identifier(l)?;
            if nt == NodeType::Label {
                return Err(self.parser_error("cannot have multiple labels at the same location"));
            }

            self.skip_whitespace(l);
        }

        self.parse_statement(l, id)
    }

    // Given a `line` parses an identifier if possible. This identifier is not
    // necessary an "identifier" per se, but rather a first identifier-like
    // string which can be used to determine which kind of expression we are
    // dealing with. Returns a PString representing this identifier on success,
    // plus a hint on whether the identifier belongs to a label or not.
    fn parse_identifier(&mut self, line: &str) -> Result<(PString, NodeType), ParseError> {
        let start = self.column;
        let base_offset = self.offset;
        let mut nt = NodeType::Value;

        // For the general case we just need to iterate until a whitespace
        // character or an inline comment is found. Then our PString object is
        // merely whatever is on the column..self.column range. Note that we
        // need this iteration to be peekable so we can look ahead. This is
        // interesting for detecting identifiers which are scoped (e.g.
        // "Scope::Identifier").
        let mut chars = line
            .get(self.offset..)
            .unwrap_or_default()
            .chars()
            .peekable();
        while let Some(c) = chars.next() {
            // Check for characters that end an identifier.
            if c.is_whitespace() || c == ':' || c == '(' || c == ')' || c == '=' {
                // This next match looks scarier than what it actually is. To
                // sum things up, the ':' character is quite troublesome, since
                // it can mean three things depending on the context.
                //
                //  1. A label in the form of "label:". In this case there is no
                //     "next" character.
                //  2. A scope operator in "Scope::Variable". In this case we
                //     should swallow up both colon characters and continue
                //     parsing the identifier.
                //  3. A relative label in "jmp :-" or "jmp :+". In this case we
                //     want to exhaust the character stream
                match chars.peek() {
                    Some(pc) => {
                        if c == ':' {
                            let next = *pc;
                            match next {
                                // Scope operator (e.g. "Scope::Variable").
                                ':' => {
                                    chars.next();
                                    self.next();
                                    self.next();
                                    continue;
                                }
                                // Relative label (e.g. "jmp :+" or "jmp :-").
                                '+' | '-' => {
                                    // Relative labels start with ':' *always*.
                                    // Hence, if there was something behind it,
                                    // there is something wrong (e.g. "Bad:+").
                                    if base_offset != self.offset {
                                        return Err(ParseError {
                                        line: self.line,
                                        message: "you cannot have a relative label inside of an identifier".to_string(),
                                    });
                                    }

                                    // Skip the ':' character for the next loop.
                                    self.next();

                                    // Let's exhaust the stream of characters.
                                    let mut size = 0;
                                    for cc in chars.by_ref() {
                                        //  Only four levels of relative jumps
                                        //  are allowed. This is a rather random
                                        //  number, but if you are dealing with
                                        //  more than two levels of relative
                                        //  jumps you are already screwed, so we
                                        //  are actually quite generous here for
                                        //  what it's most probably just
                                        //  spaghetti code.
                                        size += 1;
                                        if size > 4 {
                                            return Err(ParseError {
                                            line: self.line,
                                            message: "you can only jump to a maximum of four relative labels".to_string(),
                                        });
                                        }

                                        // You cannot mix '+'/'-' characters
                                        // with others.
                                        if cc != next {
                                            let msg =
                                                if next == '+' { "forward" } else { "backward" };
                                            return Err(ParseError {
                                                line: self.line,
                                                message: format!(
                                                "{} relative label can only have '{}' characters",
                                                msg, next
                                            ),
                                            });
                                        }
                                        self.next();
                                    }
                                }
                                // Regular label (e.g. "label:").
                                _ => nt = NodeType::Label,
                            }
                        }
                    }
                    // If there are no characters left, check if it was a regular label.
                    None => {
                        if c == ':' {
                            nt = NodeType::Label;
                        }
                    }
                }

                // The value of the identifier is whatever we have picked up
                // along the parsing.
                let value = String::from(line.get(base_offset..self.offset).unwrap_or("").trim());

                // The end of the identifier range has to be shortened for
                // regular labels because we want to ignore to extra ':'
                // character in the end.
                let end = match nt {
                    NodeType::Label => {
                        self.next();
                        self.column - 1
                    }
                    _ => self.column,
                };

                // And we are done.
                return Ok((
                    PString {
                        value,
                        line: self.line,
                        start,
                        end,
                    },
                    nt,
                ));
            }

            self.next();
        }

        // The line is merely the identifier (e.g. instruction with implied
        // addressing).
        let id = String::from(line.get(base_offset..).unwrap_or_default().trim());
        Ok((
            PString {
                value: id,
                line: self.line,
                start,
                end: self.column,
            },
            NodeType::Value,
        ))
    }

    // Parse the top-level statement as found on the given `line` which has a
    // leading `id` positioned-string which may be an identifier.
    fn parse_statement(&mut self, line: &str, id: PString) -> Result<(), ParseError> {
        // There are only two top-level statements: instructions and
        // assignments. Other kinds of expressions can also be used in the
        // middle of assignments or instructions, and so they have to be handled
        // as common expressions. Whether expressions make sense at the
        // different levels is something to be figured out by the assembler.
        match INSTRUCTIONS.get(&id.value) {
            Some(_) => self.parse_instruction(line, id),
            None => {
                if line.contains('=') {
                    self.parse_assignment(line, id)
                } else {
                    self.parse_other(line, id)
                }
            }
        }
    }

    // Parse the given `line` as an instruction being identified by `id`.
    fn parse_instruction(&mut self, line: &str, id: PString) -> Result<(), ParseError> {
        let mut paren = 0;

        // After the initial instruction identifier (e.g. `lda`), there might be
        // an undefined white space. Let's skip it now.
        self.skip_whitespace(line);

        // After skipping the identifier, are we actually on a weird assignment
        // scenario? (e.g. `lda = #42`). If so, then complain on the programmer
        // using a reserved instruction mnemonic as a variable name.
        if line.chars().nth(self.offset).unwrap_or_default() == '=' {
            return Err(self.parser_error(
                format!(
                    "cannot use the reserved mnemonic '{}' as a variable name",
                    id.value
                )
                .as_str(),
            ));
        }

        // If the line at the current point starts with an open paren, then we
        // assume that the indirect addressing mode is being used. If this is
        // the case, then the left arm is actually what's inside of the
        // parenthesis. Otherwise we have to grab until the very end.
        let indirect = line.chars().nth(self.offset).unwrap_or(',') == '(';
        let l = if indirect {
            // Skip the '(' character and skip whitespaces.
            self.next();
            self.skip_whitespace(line);

            // Now let's find the matching paren for the one that opened the
            // indirect addressing mode, and that's the end of our left arm for
            // this instruction.
            paren = self.find_matching_paren(line, self.offset)?;
            line.get(self.offset..paren).unwrap_or_default()
        } else {
            line.get(self.offset..).unwrap_or_default()
        };

        // Is there a left arm at all? If so, then parse it now but considering
        // the trimmed `l` variable, which is a bit special when using indirect
        // addressing mode.
        self.offset = 0;
        let mut left = if l.is_empty() {
            None
        } else {
            Some(Box::new(self.parse_left_arm(l)?))
        };

        // Skip whitespace until the possible right arm. Notice that both
        // `paren` on indirect addressing mode, and `parse_left_arm` have set
        // the "cursor" just after any possible comma. Hence, if there's
        // anything left, then it's the right arm which might have leading
        // spaces.
        self.skip_whitespace(l);

        // At this point, if there is nothing there, then we have no right arm.
        // Otherwise we have to parse the expression.
        let mut right_str = l.get(self.offset..).unwrap_or_default();
        let mut right = if right_str.is_empty() {
            None
        } else {
            self.offset = 0;
            Some(Box::new(self.parse_expression(right_str)?))
        };

        // If we were in indirect addressing mode, then there's some juggling we
        // have to do for the parsed expressions. This is the most complex part
        // from this function, as it will mutate the `left` and the `right`
        // nodes in subtle ways. But this is better than having things in a
        // different function because the rest of the code is pretty much the
        // same.
        if indirect {
            // In indirect addressing mode there's *always* a left arm.
            if left.is_none() {
                return Err(self.parser_error("empty indirect addressing"));
            }

            // Now, here's the trick: if there's something left after the
            // parenthesis, then we have the right arm there. Note that the
            // Indirection node cannot have a right arm at the same time. If
            // this is the case, we are going to freak out now.
            right_str = line.get(paren..).unwrap_or_default();
            if !(right_str.is_empty() || right_str == ")") && right.is_some() {
                return Err(self.parser_error("bad indirect addressing"));
            }

            // The left arm from an indirect addressing mode is actually the
            // indirection itself.
            left = Some(Box::new(PNode {
                node_type: NodeType::Indirection,
                value: PString::default(),
                left,
                right,
                args: None,
            }));

            // Do we have anything as a right arm?
            right = if right_str.is_empty() || right_str == ")" {
                None
            } else {
                self.offset = 0;

                // Skip any possible leading space on ")   ,".
                self.next();
                self.skip_whitespace(right_str);

                // Skip any possible leading space after the comma.
                self.next();
                self.skip_whitespace(right_str);

                // And finally parse the right arm for the global instruction.
                Some(Box::new(self.parse_expression(right_str)?))
            };
        }

        // We can push the resulting parsed expressions.
        self.nodes.last_mut().unwrap().push(PNode {
            node_type: NodeType::Instruction,
            value: id,
            left,
            right,
            args: None,
        });

        Ok(())
    }

    // Parse the given `line` as an assignment statement which declares a
    // variable at `id`.
    fn parse_assignment(&mut self, line: &str, id: PString) -> Result<(), ParseError> {
        // Notice that `parse_identifier` pretty much swallows any kind of
        // identifier without doing any sanity checks. Now it's the time to do
        // so.
        if let Err(msg) = id.is_valid_identifier(false) {
            return Err(self.parser_error(&msg));
        }

        // Skip whitespaces and make sure that we have a '=' sign.
        self.skip_whitespace(line);
        if line.chars().nth(self.offset).unwrap_or_default() != '=' {
            return Err(self.parser_error(format!("unknown instruction '{}'", id.value).as_str()));
        }

        // Skip the '=' sign and any possible whitespaces.
        self.next();
        self.skip_whitespace(line);

        // Parse the expression on the right side of the assignment.
        let rest = line.get(self.offset..).unwrap_or("").trim_end();
        if rest.is_empty() {
            return Err(self.parser_error("incomplete assignment"));
        };
        self.offset = 0;
        let left = self.parse_expression(rest)?;

        // And push the node.
        self.nodes.last_mut().unwrap().push(PNode {
            node_type: NodeType::Assignment,
            value: id,
            left: Some(Box::new(left)),
            right: None,
            args: None,
        });

        Ok(())
    }

    // Parse statements which are neither an instruction nor an assignment. This
    // includes stuff like control statements.
    fn parse_other(&mut self, line: &str, id: PString) -> Result<(), ParseError> {
        let node = self.parse_expression_with_identifier(id, line)?;
        let node_type = node.node_type.clone();
        let body_type = node.body_type();

        // The given statement might be a start/end one (e.g. '.proc' and
        // '.endproc'). In these cases there are some things to handle besides
        // pushing the node into the current level of nodes.
        match body_type {
            NodeBodyType::Starts => {
                self.bodies.push(node_type.closing_type().unwrap());
                self.nodes.last_mut().unwrap().push(node);
                self.nodes.push(vec![]);
            }
            NodeBodyType::Ends => {
                // Pop out which start statement was last seen and check that it
                // makes sense to the end statement we are parsing now.
                let expected_close = match self.bodies.pop() {
                    Some(ec) => ec,
                    None => {
                        return Err(
                            self.parser_error(format!("unexpected '{}'", node_type).as_str())
                        )
                    }
                };
                if node_type != expected_close {
                    return Err(self.parser_error(
                        format!("expecting '{}', found '{}'", expected_close, node_type).as_str(),
                    ));
                }

                // Note that empty bodies are possible. This is left
                // to the caller (e.g. assembler) to decide whether
                // it makes sense or not.
                let nodes = self.nodes.pop().unwrap();
                self.nodes.last_mut().unwrap().last_mut().unwrap().right = Some(Box::new(PNode {
                    node_type: NodeType::ControlBody,
                    value: PString::default(),
                    left: None,
                    right: None,
                    args: Some(nodes),
                }));
                self.nodes.last_mut().unwrap().push(node);
            }
            NodeBodyType::None => self.nodes.last_mut().unwrap().push(node),
        }

        Ok(())
    }

    // Parse any possible arguments for the given `line`. The offset is supposed
    // to be at a point where arguments might appear, either between parens or
    // not.
    fn parse_arguments(&mut self, line: &str) -> Result<Vec<PNode>, ParseError> {
        // Skip any possible whitespace before the optional opening paren.
        self.skip_whitespace(line);

        // Scope the end of the argument list. If the arguments are enclosed on
        // parenthesis, take that into account, otherwise we will parse until
        // the end of the cleaned line.
        let paren = line.chars().nth(self.offset).unwrap_or_default() == '(';
        let end = if paren {
            // We have a parenthesis. Skip it.
            self.next();
            self.skip_whitespace(line);

            // The end is actually the matching paren for the current opening
            // one.
            self.find_matching_paren(line, self.offset)?
        } else {
            line.len()
        };

        let mut args = Vec::new();
        let trimmed_str = line.get(..end).unwrap_or_default().trim_end();

        // Having an infinite loop with `break`s inside is admittedly not the
        // cleanest thing ever, but it does its job.
        loop {
            // This looks scarier than it actually is. It first finds the end of
            // the argument. Then it calculates a diff on trimming the end or
            // not. This diff will be used to re-adjust the column after the
            // argument is parsed, so we skip any final spaces.
            let (arg_end, comma) = self.find_left_end(trimmed_str)?;
            let arg_untrimmed = line.get(self.offset..arg_end).unwrap_or_default();
            let arg = arg_untrimmed.trim_end();
            let diff = arg_untrimmed.len() - arg.len();

            // Do we actually have an argument. If not then this is the end of
            // our loop.
            if arg.is_empty() {
                break;
            }

            // Parse the argument, which is trimmed down from the line and hence
            // the offset needs to be reset.
            self.offset = 0;
            args.push(self.parse_expression(arg)?);

            // After the parsing is done for the current argument, move both
            // `self.offset` and `self.column` right after the end of the
            // current argument.
            self.offset = arg_end;
            self.column += diff;

            // Was the argument ended by a comma? If so there are more arguments
            // to be parsed. Otherwise we can break the loop if it wasn't
            // catched for whatever reason by the previous check.
            if comma {
                // Skip the comma character and any leading white spaces for the
                // next argument.
                self.next();
                self.skip_whitespace(line);
            } else {
                break;
            }
        }

        Ok(args)
    }

    // Parse the left arm from an instruction and leave `offset` and `column`
    // past the end of it.
    fn parse_left_arm(&mut self, line: &str) -> Result<PNode, ParseError> {
        let start_column = self.column;

        // We track the start value of the offset and we will keep track of the
        // movement of it on `end`. This allows us to preserve the value on
        // inner calls that might modify the offset value.
        let (end, comma) = self.find_left_end(line)?;

        // Set the offset to 0 since we are constraining the string to be
        // parsed.
        let str = line.get(..end).unwrap_or_default().trim_end();
        self.offset = 0;

        // Parse the expression that we can get from the current offset to the
        // computed end.
        let expr = self.parse_expression(str);

        // Set the `offset` and `column` to the end of the line that is shared
        // with the caller.
        self.offset = end;
        self.column = start_column + end;

        // If there was a comma, then the caller expects this function to move
        // both `offset` and `column` past it.
        if comma {
            self.next();
        }
        expr
    }

    // Find the end position for a "left arm"-like expression. That is, there
    // might be opening/closing parenthesis which need to be balanced. On
    // success it returns the index from within the given `line`, and a boolean
    // which is set to true/false on whether a comma was found.
    fn find_left_end(&self, line: &str) -> Result<(usize, bool), ParseError> {
        let mut idx = self.offset;
        let mut parens = 0;
        let mut comma = false;

        for c in line.get(self.offset..).unwrap_or_default().chars() {
            if c == ',' {
                if parens == 0 {
                    comma = true;
                    break;
                }
            } else if c == '(' {
                parens += 1;
            } else if c == ')' {
                parens -= 1;
            }

            idx += 1;

            if parens < 0 {
                return Err(self.parser_error("too many closing parenthesis"));
            }
        }
        if parens > 0 {
            return Err(self.parser_error("unclosed parenthesis"));
        }

        Ok((idx, comma))
    }

    // Finds the matching parenthesis which closes the parenthesis that was just
    // opened. The `init` index point to the next character after the opening
    // paren from the given `line`.
    fn find_matching_paren(&self, line: &str, init: usize) -> Result<usize, ParseError> {
        let mut idx = init;
        let mut parens = 1;

        for c in line.get(init..).unwrap_or_default().chars() {
            if c == '(' {
                parens += 1;
            } else if c == ')' {
                parens -= 1;
            }

            match parens.cmp(&0) {
                Ordering::Equal => return Ok(idx),
                Ordering::Less => return Err(self.parser_error("too many closing parenthesis")),
                Ordering::Greater => {}
            }

            idx += 1;
        }
        if parens > 0 {
            return Err(self.parser_error("unclosed parenthesis"));
        }

        Ok(idx)
    }

    // Parse the expression under `line`. Indeces such as `self.column` and
    // `self.offset` are assumed to be correct at this point for the given
    // `line` (e.g. the line might not be a full line but rather a limited range
    // and the offset has been set accordingly). Returns a new node for the
    // expression at hand.
    fn parse_expression(&mut self, line: &str) -> Result<PNode, ParseError> {
        // Cases where fetching an "identifier" is really not needed.
        let first = line.chars().next().unwrap_or_default();
        if first == '(' {
            // This is an expression under paranthesis. Get those out of the way
            // and call `parse_expression` again but for the expression inside.

            // Skip '(' character and whitespace characters in between.
            self.next();
            self.skip_whitespace(line);

            // Extract what's inside of the enclosing parenthesis.
            let paren = self.find_matching_paren(line, self.offset)?;
            let l = line.get(self.offset..paren).unwrap_or_default();

            // And return what you can parse from the inner expression.
            self.offset = 0;
            return self.parse_expression(l);
        } else if let Some(node_type) = self.get_unary_from_line(line) {
            // This is a unary operation. Just parse the right side and return
            // early.

            // Skip operator and whitespaces.
            let start = self.column;
            self.next();
            self.skip_whitespace(line);

            // Fetch the right side of the operator.
            let right_str = line.get(self.offset..).unwrap_or_default().trim_end();
            self.offset = 0;
            let right = self.parse_expression(right_str)?;

            return Ok(PNode {
                node_type,
                value: PString {
                    value: String::from(""),
                    line: self.line,
                    start,
                    end: right.value.end,
                },
                left: None,
                right: Some(Box::new(right)),
                args: None,
            });
        }

        // Now that we have changed specific cases where detecting an
        // "identifier" is not really relevant, let's fetch the "identifier" and
        // parse the expression with that into consideration.
        let (id, nt) = self.parse_identifier(line)?;

        if nt == NodeType::Label {
            Err(self.parser_error("not expecting a label defined here"))
        } else {
            self.parse_expression_with_identifier(id, line)
        }
    }

    // Returns the unary operator type which is at the beginning of the line if
    // it exists, otherwise returns None.
    fn get_unary_from_line(&mut self, line: &str) -> Option<NodeType> {
        match line.chars().nth(0).unwrap_or_default() {
            '+' => Some(NodeType::Operation(OperationType::UnaryPositive)),
            '-' => Some(NodeType::Operation(OperationType::UnaryNegative)),
            '~' => Some(NodeType::Operation(OperationType::BitwiseNot)),
            '<' => Some(NodeType::Operation(OperationType::LoByte)),
            '>' => Some(NodeType::Operation(OperationType::HiByte)),
            _ => None,
        }
    }

    // Get the operator from the first two characters of the given line. If
    // there's no operator, then None is returned for the first element of the
    // Ok tuple. Moreover, the second item on the tuple contains the amount of
    // characters that made up the operator.
    fn get_operation_from_line(
        &mut self,
        line: &str,
    ) -> Result<(Option<NodeType>, usize), ParseError> {
        let first = line.chars().nth(0).unwrap_or_default();
        let second = line.chars().nth(1).unwrap_or_default();

        match first {
            '+' => Ok((Some(NodeType::Operation(OperationType::Add)), 1)),
            '-' => Ok((Some(NodeType::Operation(OperationType::Sub)), 1)),
            '*' => Ok((Some(NodeType::Operation(OperationType::Mul)), 1)),
            '/' => Ok((Some(NodeType::Operation(OperationType::Div)), 1)),
            '^' => Ok((Some(NodeType::Operation(OperationType::Xor)), 1)),
            '|' => Ok((Some(NodeType::Operation(OperationType::Or)), 1)),
            '&' => Ok((Some(NodeType::Operation(OperationType::And)), 1)),
            '<' => match second {
                '<' => Ok((Some(NodeType::Operation(OperationType::Lshift)), 2)),
                _ => Err(self.parser_error(format!("unknown operator '<{}'", second).as_str())),
            },
            '>' => match second {
                '>' => Ok((Some(NodeType::Operation(OperationType::Rshift)), 2)),
                _ => Err(self.parser_error(format!("unknown operator '>{}'", second).as_str())),
            },
            _ => Ok((None, 0)),
        }
    }

    // Parse the expression under `line` by taking into consideration that a
    // part of it has already been parsed and evaluated as the given `id`.
    // Indeces such as `self.column` and `self.offset` are assumed to be correct
    // at this point. Returns a new node for the expression at hand.
    fn parse_expression_with_identifier(
        &mut self,
        id: PString,
        line: &str,
    ) -> Result<PNode, ParseError> {
        // Reaching this condition is usually a bad sign, but there is so many
        // ways in which it could go wrong, that an `assert!` wouldn't be fair
        // either. Hence, just error out.
        if id.is_empty() {
            return Err(self.parser_error("invalid identifier"));
        }

        if id.value.starts_with(".") {
            self.parse_control(id, line)
        } else if line.starts_with('$') || line.starts_with('#') || line.starts_with('%') {
            self.parse_literal(id, line)
        } else if line.starts_with('\'') {
            self.parse_char(id)
        } else {
            // Skip any whitespace after our identifier.
            self.skip_whitespace(line);
            let l = line.get(self.offset..).unwrap_or_default().trim_end();

            // The line might start with a binary operator. This would mean that
            // the "id" is actually just the left node of a binary operation and
            // that we just need to parse the right arm.
            let (op, op_size) = self.get_operation_from_line(l)?;
            if let Some(node_type) = op {
                // The left node of the operator is simply the "identifier" that
                // came to us.
                let left = Some(Box::new(PNode {
                    node_type: NodeType::Value,
                    value: id.clone(),
                    left: None,
                    right: None,
                    args: None,
                }));

                // Fetch a trimmed version of the string that comes after the
                // operator.
                self.offset += op_size;
                self.column += op_size;
                self.skip_whitespace(line);
                let right_str = line.get(self.offset..).unwrap_or_default().trim_end();

                // The right node of the operation will be the parsed expression
                // of the string we just got right of the operator.
                self.offset = 0;
                let right = self.parse_expression(right_str)?;

                return Ok(PNode {
                    node_type,
                    value: PString {
                        value: String::from(""),
                        line: id.line,
                        start: id.start,
                        end: right.value.end,
                    },
                    left,
                    right: Some(Box::new(right)),
                    args: None,
                });
            }

            // Ok, so the line did not indicate any sort of operation. That
            // being said, if the rest of the line still contains stuff, it
            // might as well be a macro call. Try to process it as such.
            if !l.is_empty() {
                let args = self.parse_arguments(line)?;
                return Ok(PNode {
                    node_type: NodeType::Call,
                    value: id,
                    left: None,
                    right: None,
                    args: if args.is_empty() { None } else { Some(args) },
                });
            }

            // Blindly return the identifier as a PNode. This might be either a
            // value as-is, or a macro call which we can't make sense at the
            // moment. Eitherway, let the assembler decide.
            Ok(PNode {
                node_type: NodeType::Value,
                value: id,
                left: None,
                right: None,
                args: None,
            })
        }
    }

    // Returns a NodeType::Control node with whatever could be parsed
    // considering the given `id` and rest of the `line`.
    fn parse_control(&mut self, id: PString, line: &str) -> Result<PNode, ParseError> {
        let mut left = None;

        // Ensure that this is a function that we know of. In the past this was
        // not done and it brought too many problems that made the more
        // "abstract" way of handling this just too complicated.
        let control = match CONTROL_FUNCTIONS.get(&id.value.to_lowercase()) {
            Some(control) => control,
            None => {
                return Err(self.parser_error(format!("unknown function '{}'", id.value).as_str()))
            }
        };

        // If this control function has an identifier (e.g. `.macro
        // Identifier(args...)`), let's parse it now.
        if control.has_identifier {
            self.skip_whitespace(line);
            left = Some(Box::new(PNode {
                node_type: NodeType::Value,
                value: self.parse_identifier(line)?.0,
                left: None,
                right: None,
                args: None,
            }));
        }

        // At this point we reached the arguments (i.e. any identifier required
        // by the control function has already been parsed and set in `left`).
        // Then, just parse the arguments and ensure that it matches the amount
        // required by the function.
        let args = self.parse_arguments(line)?;
        if let Some(args_required) = control.required_args {
            if args.len() != args_required {
                return Err(self.parser_error(
                    format!("wrong number of arguments for function '{}'", id.value).as_str(),
                ));
            }
        }

        Ok(PNode {
            node_type: NodeType::Control(control.control_type.clone()),
            value: id,
            left,
            right: None,
            args: if args.is_empty() { None } else { Some(args) },
        })
    }

    // Returns a NodeType::Literal node with whatever could be parsed
    // considering the given `id` and rest of the `line`.
    fn parse_literal(&mut self, id: PString, line: &str) -> Result<PNode, ParseError> {
        // Force the column to point to the literal character just in case
        // of expressions like '#.hibyte'. Then skip whitespaces for super
        // ugly statements such as '# 20'. This is ugly but we should permit
        // it. A later linter can yell at a programmer for this.
        self.column = id.start;
        self.offset = 0;
        self.next();
        self.skip_whitespace(line);

        // With this, just fetch the inner expression and return the literal
        // node.
        let inner = line.get(self.offset..).unwrap_or("");
        self.offset = 0;
        let left = self.parse_expression(inner)?;

        Ok(PNode {
            node_type: NodeType::Literal,
            value: id,
            left: Some(Box::new(left)),
            right: None,
            args: None,
        })
    }

    // Returns a NodeType::Literal node with the given `id` parsed as a
    // character literal. This literal will have on the left node a value which
    // is the character transformed into a decimal value.
    fn parse_char(&mut self, id: PString) -> Result<PNode, ParseError> {
        if id.value.len() != 3 {
            return Err(self.parser_error("bad character literal"));
        }

        let mut chars = id.value.chars();
        let del = chars.next().unwrap();
        let ch = chars.next().unwrap();

        if !ch.is_ascii_alphanumeric() {
            return Err(self.parser_error(
                "only alphanumeric ASCII characters are allowed on character literals",
            ));
        }
        if del != '\'' || chars.next().unwrap() != '\'' {
            return Err(self.parser_error("bad character literal"));
        }

        Ok(PNode {
            node_type: NodeType::Literal,
            value: id.clone(),
            left: Some(Box::new(PNode {
                node_type: NodeType::Value,
                value: PString {
                    value: (ch as u8).to_string(),
                    line: id.line,
                    start: id.start + 1,
                    end: id.end - 1,
                },
                left: None,
                right: None,
                args: None,
            })),
            right: None,
            args: None,
        })
    }

    // Returns a new ParseError by using the current line.
    fn parser_error(&self, msg: &str) -> ParseError {
        ParseError {
            message: String::from(msg),
            line: self.line,
        }
    }

    // Advances `self.column` and `self.offset` until a non-whitespace character
    // is found. Note that the initial index is bound to `self.offset`. Returns
    // false if the line can be skipped entirely, true otherwise.
    fn skip_whitespace(&mut self, line: &str) -> bool {
        if line.is_empty() {
            return false;
        }

        for c in line.get(self.offset..).unwrap_or("").chars() {
            if !c.is_whitespace() {
                if c == ';' {
                    return false;
                }
                return true;
            }

            self.next();
        }

        true
    }

    // Increment `self.column` and `self.offset` by one.
    fn next(&mut self) {
        self.column += 1;
        self.offset += 1;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::node::ControlType;

    fn assert_one_valid(parser: &mut Parser, line: &str) {
        assert!(parser.parse(line.as_bytes()).is_ok());
        assert!(parser.nodes.len() == 1);
    }

    fn assert_node(node: &PNode, nt: NodeType, line: &str, value: &str) {
        assert_eq!(node.node_type, nt);
        assert_eq!(
            node.value.value.as_str(),
            line.get(node.value.start..node.value.end).unwrap()
        );
        assert_eq!(node.value.value.as_str(), value);
    }

    // Empty

    #[test]
    fn empty_line() {
        let mut parser = Parser::default();
        assert!(parser.parse("".as_bytes()).is_ok());
        assert_eq!(parser.nodes.last().unwrap().len(), 0);
    }

    #[test]
    fn spaced_line() {
        let mut parser = Parser::default();
        assert!(parser.parse("   ".as_bytes()).is_ok());
        assert_eq!(parser.nodes.last().unwrap().len(), 0);
    }

    #[test]
    fn just_a_comment_line() {
        for line in vec![";; This is a comment", "    ;; Comment"].into_iter() {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());
            assert_eq!(parser.nodes.last().unwrap().len(), 0);
        }
    }

    // Labels

    #[test]
    fn anonymous_label() {
        let mut parser = Parser::default();
        assert!(parser.parse(":".as_bytes()).is_ok());

        let mut nodes = parser.nodes.last().unwrap();
        assert_eq!(nodes.len(), 1);
        assert!(nodes.first().unwrap().value.value.is_empty());
        assert_eq!(nodes.first().unwrap().value.start, 0);
        assert_eq!(nodes.first().unwrap().value.end, 0);

        parser = Parser::default();
        assert!(parser.parse("  :".as_bytes()).is_ok());

        nodes = parser.nodes.last().unwrap();
        assert_eq!(nodes.len(), 1);
        assert!(nodes.first().unwrap().value.value.is_empty());
        assert_eq!(nodes.first().unwrap().value.start, 2);
        assert_eq!(nodes.first().unwrap().value.end, 2);
    }

    #[test]
    fn named_label() {
        let mut parser = Parser::default();
        assert!(parser.parse("label:".as_bytes()).is_ok());

        let mut nodes = parser.nodes.last().unwrap();
        assert_eq!(nodes.len(), 1);
        assert_eq!(nodes.first().unwrap().value.value, "label");
        assert_eq!(nodes.first().unwrap().value.start, 0);
        assert_eq!(nodes.first().unwrap().value.end, 5);

        parser = Parser::default();
        assert!(parser.parse("  label:".as_bytes()).is_ok());

        nodes = parser.nodes.last().unwrap();
        assert_eq!(nodes.len(), 1);
        assert_eq!(nodes.first().unwrap().value.value, "label");
        assert_eq!(nodes.first().unwrap().value.start, 2);
        assert_eq!(nodes.first().unwrap().value.end, 7);
    }

    #[test]
    fn label_with_instruction() {
        let line = "label: dex";

        let mut parser = Parser::default();
        assert!(parser.parse(line.as_bytes()).is_ok());

        let nodes = parser.nodes();
        assert_eq!(nodes.len(), 2);

        // Label.
        assert_eq!(nodes.first().unwrap().value.value, "label");
        assert_eq!(nodes.first().unwrap().value.start, 0);
        assert_eq!(nodes.first().unwrap().value.end, 5);

        // Instruction
        assert_node(nodes.last().unwrap(), NodeType::Instruction, line, "dex")
    }

    // Literals

    #[test]
    fn parse_pound_literal() {
        for line in vec!["#20", " #20 ", "  #20   ; Comment", "  label:   # 20"].into_iter() {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_eq!(node.node_type, NodeType::Literal);
            assert!(node.right.is_none());
            assert!(node.args.is_none());

            let left = node.left.clone().unwrap();
            assert_eq!(left.node_type, NodeType::Value);
            assert_eq!(left.value.value, "20");
            assert_eq!(line.get(left.value.start..left.value.end).unwrap(), "20");
        }
    }

    #[test]
    fn parse_compound_literal() {
        let line = "#$20";
        let mut parser = Parser::default();
        assert!(parser.parse(line.as_bytes()).is_ok());

        let node = parser.nodes.last().unwrap().last().unwrap();
        assert_eq!(node.node_type, NodeType::Literal);
        assert!(node.right.is_none());
        assert!(node.args.is_none());

        let inner = node.left.clone().unwrap();
        assert_eq!(inner.node_type, NodeType::Literal);
        assert_eq!(inner.value.value, "$20");
        assert_eq!(line.get(inner.value.start..inner.value.end).unwrap(), "$20");

        let innerinner = inner.left.clone().unwrap();
        assert_eq!(innerinner.node_type, NodeType::Value);
        assert_eq!(innerinner.value.value, "20");
        assert_eq!(
            line.get(innerinner.value.start..innerinner.value.end)
                .unwrap(),
            "20"
        );
    }

    #[test]
    fn parse_variable_in_literal() {
        let line = "#Variable";
        let mut parser = Parser::default();
        assert!(parser.parse(line.as_bytes()).is_ok());

        let node = parser.nodes.last().unwrap().last().unwrap();
        assert_eq!(node.node_type, NodeType::Literal);
        assert!(node.right.is_none());
        assert!(node.args.is_none());

        let inner = node.left.clone().unwrap();
        assert_eq!(inner.node_type, NodeType::Value);
        assert_eq!(inner.value.value, "Variable");
        assert_eq!(
            line.get(inner.value.start..inner.value.end).unwrap(),
            "Variable"
        );
    }

    #[test]
    fn parse_paren_expression() {
        let line = "ldx #(Variable)";
        let mut parser = Parser::default();
        assert!(parser.parse(line.as_bytes()).is_ok());

        let instr = parser.nodes.last().unwrap().last().unwrap();
        assert_eq!(instr.node_type, NodeType::Instruction);
        assert!(instr.right.is_none());
        assert!(instr.args.is_none());

        let node = instr.left.clone().unwrap();
        assert_eq!(node.node_type, NodeType::Literal);
        assert!(node.right.is_none());
        assert!(node.args.is_none());

        let inner = node.left.clone().unwrap();
        assert_eq!(inner.node_type, NodeType::Value);
        assert_eq!(inner.value.value, "Variable");
        assert_eq!(
            line.get(inner.value.start..inner.value.end).unwrap(),
            "Variable"
        );
    }

    #[test]
    fn parse_bad_literals() {
        for line in vec!["#", "#%", "$"].into_iter() {
            let mut parser = Parser::default();
            let err = parser.parse(line.as_bytes()).unwrap_err();

            assert_eq!(err.first().unwrap().message, "invalid identifier");
        }
    }

    // Regular instructions.

    #[test]
    fn instruction_with_implied() {
        for line in vec![
            "dex",
            "    dex",
            " dex  ",
            "  dex   ; Comment",
            "  label:   dex",
        ]
        .into_iter()
        {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line, "dex");
            assert!(node.left.is_none());
            assert!(node.right.is_none());
            assert!(node.args.is_none());
        }
    }

    #[test]
    fn instruction_with_implied_explicit() {
        for line in vec!["inc a", "    inc a", " inc  a  "].into_iter() {
            let mut parser = Parser::default();
            assert_one_valid(&mut parser, line);

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line, "inc");
            assert!(node.right.is_none());
            assert!(node.args.is_none());

            assert_node(&node.left.clone().unwrap(), NodeType::Value, line, "a");
        }
    }

    #[test]
    fn instruction_with_zeropage() {
        for line in vec!["inc $20", "    inc   $20", " inc  $20  "].into_iter() {
            let mut parser = Parser::default();
            assert_one_valid(&mut parser, line);

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line, "inc");
            assert!(node.right.is_none());
            assert!(node.args.is_none());

            assert_node(&node.left.clone().unwrap(), NodeType::Literal, line, "$20");
        }
    }

    #[test]
    fn instruction_with_immediate() {
        for line in vec!["adc #$20", "  adc #$20  ", "  adc   #$20  "].into_iter() {
            let mut parser = Parser::default();
            assert_one_valid(&mut parser, line);

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line, "adc");
            assert!(node.right.is_none());
            assert!(node.args.is_none());

            assert_node(&node.left.clone().unwrap(), NodeType::Literal, line, "#$20");
        }
    }

    #[test]
    fn instruction_with_absolute() {
        for line in vec!["inc $2002", "    inc   $2002", " inc  $2002  "].into_iter() {
            let mut parser = Parser::default();
            assert_one_valid(&mut parser, line);

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line, "inc");
            assert!(node.right.is_none());
            assert!(node.args.is_none());

            assert_node(
                &node.left.clone().unwrap(),
                NodeType::Literal,
                line,
                "$2002",
            );
        }
    }

    #[test]
    fn instruction_with_absolute_x() {
        for line in vec![
            "inc $2002, x",
            "    inc   $2002, x",
            " inc  $2002,    x  ",
            " label:   inc $2002,   x  ; Comment",
        ]
        .into_iter()
        {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line, "inc");
            assert!(node.args.is_none());

            assert_node(
                &node.left.clone().unwrap(),
                NodeType::Literal,
                line,
                "$2002",
            );
            assert_node(&node.right.clone().unwrap(), NodeType::Value, line, "x");
        }
    }

    #[test]
    fn indirect_addressing_bare() {
        for line in vec![
            "lda ($2000)",
            "    lda   ( $2000 )  ; Comment",
            "  : lda (   $2000)",
            "lda($2000)",
        ]
        .into_iter()
        {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line, "lda");
            assert!(node.right.is_none());

            let left = node.left.clone().unwrap();
            assert_eq!(left.node_type, NodeType::Indirection);
            assert_node(&left.left.unwrap(), NodeType::Literal, line, "$2000");
            assert!(left.right.is_none());
        }
    }

    #[test]
    fn indirect_addressing_x() {
        for line in vec![
            "lda ($20, x)",
            "    lda ($20,   x)",
            " lda   ($20,x)  ",
            "  : lda ($20  ,   x) ; Comment",
            "  lda (  $20  ,   x  )  ",
        ]
        .into_iter()
        {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line, "lda");
            assert!(node.right.is_none());

            let left = node.left.clone().unwrap();
            assert_eq!(left.node_type, NodeType::Indirection);
            assert_node(&left.left.unwrap(), NodeType::Literal, line, "$20");
            assert_node(&left.right.unwrap(), NodeType::Value, line, "x");
        }
    }

    #[test]
    fn bad_indirect_addressing_x() {
        let mut parser = Parser::default();

        let err = parser.parse("lda (Variable, x), y".as_bytes()).unwrap_err();
        assert_eq!(err.first().unwrap().message, "bad indirect addressing");
    }

    #[test]
    fn indirect_addressing_y() {
        for line in vec!["lda ($20), y"].into_iter() {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line, "lda");

            let left = node.left.clone().unwrap();
            assert_eq!(left.node_type, NodeType::Indirection);
            assert_node(&left.left.unwrap(), NodeType::Literal, line, "$20");
            assert!(left.right.is_none());

            let right = node.right.clone().unwrap();
            assert_node(&right, NodeType::Value, line, "y");
        }
    }

    #[test]
    fn variable_in_instruction() {
        let line = "lda Variable, x";
        let mut parser = Parser::default();
        assert!(parser.parse(line.as_bytes()).is_ok());

        let node = parser.nodes.last().unwrap().last().unwrap();
        assert_node(node, NodeType::Instruction, line, "lda");
        assert!(node.args.is_none());

        assert_node(
            &node.left.clone().unwrap(),
            NodeType::Value,
            line,
            "Variable",
        );
        assert_node(&node.right.clone().unwrap(), NodeType::Value, line, "x");
    }

    #[test]
    fn variable_literal_in_instruction() {
        let line = "lda #Variable, x";
        let mut parser = Parser::default();
        assert!(parser.parse(line.as_bytes()).is_ok());

        let node = parser.nodes.last().unwrap().last().unwrap();
        assert_node(node, NodeType::Instruction, line, "lda");
        assert!(node.args.is_none());

        assert_node(
            &node.left.clone().unwrap(),
            NodeType::Literal,
            line,
            "#Variable",
        );
        assert_node(&node.right.clone().unwrap(), NodeType::Value, line, "x");
    }

    #[test]
    fn scoped_variable_literal_in_instruction() {
        for var in vec!["Scope::Variable", "Scope::Inner::Variable"].into_iter() {
            let line = format!("lda #{}", var);
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line.as_str(), "lda");
            assert!(node.right.is_none());
            assert!(node.args.is_none());

            assert_node(
                &node.left.clone().unwrap(),
                NodeType::Literal,
                line.as_str(),
                format!("#{}", var).as_str(),
            );
        }
    }

    #[test]
    fn bad_variable_scoping() {
        let mut parser = Parser::default();

        let err = parser.parse("adc #One:Variable".as_bytes()).unwrap_err();
        assert_eq!(
            err.first().unwrap().message,
            "not expecting a label defined here"
        );
    }

    #[test]
    fn reserved_mnemonic_name() {
        let mut parser = Parser::default();

        let err = parser.parse("lda = $10".as_bytes()).unwrap_err();
        assert_eq!(
            err.first().unwrap().message,
            "cannot use the reserved mnemonic 'lda' as a variable name"
        );
    }

    #[test]
    fn relative_labels() {
        for label in vec![":+", ":++", ":+++ ", ":++++", ":-", ":--", ":---", ":----"].into_iter() {
            let line = format!("jmp {}", label);
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line.as_str(), "jmp");
            assert!(node.right.is_none());
            assert!(node.args.is_none());

            assert_node(
                &node.left.clone().unwrap(),
                NodeType::Value,
                line.as_str(),
                label.trim(),
            );
        }
    }

    #[test]
    fn bad_relative_labels() {
        let mut parser = Parser::default();

        let mut line = "jmp :+++++";
        let mut err = parser.parse(line.as_bytes()).unwrap_err();
        assert_eq!(
            err.first().unwrap().message,
            "you can only jump to a maximum of four relative labels"
        );

        line = "jmp :+-";
        err = parser.parse(line.as_bytes()).unwrap_err();
        assert_eq!(
            err.first().unwrap().message,
            "forward relative label can only have '+' characters"
        );

        line = "jmp :-+-";
        err = parser.parse(line.as_bytes()).unwrap_err();
        assert_eq!(
            err.first().unwrap().message,
            "backward relative label can only have '-' characters"
        );

        line = "jmp Identifier:++";
        err = parser.parse(line.as_bytes()).unwrap_err();
        assert_eq!(
            err.first().unwrap().message,
            "you cannot have a relative label inside of an identifier"
        );
    }

    // Assignments

    #[test]
    fn bad_assignments() {
        let mut parser = Parser::default();

        let mut err = parser.parse("abc = $10".as_bytes()).unwrap_err();
        assert_eq!(
            err.first().unwrap().message,
            "cannot use names which are valid hexadecimal values such as 'abc'"
        );

        parser = Parser::default();
        err = parser.parse("var =".as_bytes()).unwrap_err();
        assert_eq!(err.first().unwrap().message, "incomplete assignment");

        parser = Parser::default();
        err = parser.parse("var =   ".as_bytes()).unwrap_err();
        assert_eq!(err.first().unwrap().message, "incomplete assignment");

        parser = Parser::default();
        err = parser.parse("var =   ; Comment".as_bytes()).unwrap_err();
        assert_eq!(err.first().unwrap().message, "incomplete assignment");
    }

    // Constant expressions

    #[test]
    fn constant_expression_test() {
        let line = "ldx #(4 * NUM_SPRITES)";
        let mut parser = Parser::default();
        assert!(parser.parse(line.as_bytes()).is_ok());

        let node = parser.nodes.last().unwrap().last().unwrap();
        assert_node(node, NodeType::Instruction, line, "ldx");
        assert!(node.args.is_none());
        assert!(node.right.is_none());

        let literal = &node.left.clone().unwrap();
        assert_node(literal, NodeType::Literal, line, "#");

        let op = &literal.left.clone().unwrap();
        assert_eq!(op.node_type, NodeType::Operation(OperationType::Mul));
        assert_node(&op.left.clone().unwrap(), NodeType::Value, line, "4");
        assert_node(
            &op.right.clone().unwrap(),
            NodeType::Value,
            line,
            "NUM_SPRITES",
        );
    }

    #[test]
    fn unary_operator_test() {
        let line = "ldx #<NUM_SPRITES";
        let mut parser = Parser::default();
        assert!(parser.parse(line.as_bytes()).is_ok());

        let node = parser.nodes.last().unwrap().last().unwrap();
        assert_node(node, NodeType::Instruction, line, "ldx");
        assert!(node.args.is_none());
        assert!(node.right.is_none());

        let literal = &node.left.clone().unwrap();
        assert_node(literal, NodeType::Literal, line, "#<NUM_SPRITES");

        let op = &literal.left.clone().unwrap();
        assert_eq!(op.node_type, NodeType::Operation(OperationType::LoByte));
        assert!(op.left.is_none());
        assert_node(
            &op.right.clone().unwrap(),
            NodeType::Value,
            line,
            "NUM_SPRITES",
        );
    }

    // Control statements.

    #[test]
    fn parse_control_no_args() {
        for line in vec![".byte", "    .byte", " label: .byte   ; Comment"].into_iter() {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Control(ControlType::Byte), line, ".byte");
            assert!(node.left.is_none());
            assert!(node.right.is_none());
            assert!(node.args.is_none());
        }
    }

    #[test]
    fn parse_control_one_arg() {
        for line in vec![
            ".hibyte $2000",
            "    .hibyte $2000",
            " label: .hibyte $2000   ; Comment",
            "  .hibyte($2000)",
            "  .hibyte (  $2000  )",
        ]
        .into_iter()
        {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(
                node,
                NodeType::Control(ControlType::Hibyte),
                line,
                ".hibyte",
            );
            assert!(node.left.is_none());
            assert!(node.right.is_none());

            let args = node.args.clone().unwrap();
            assert_eq!(args.len(), 1);
            assert_node(args.first().unwrap(), NodeType::Literal, line, "$2000");
        }
    }

    #[test]
    fn parse_control_multiple_args() {
        for line in vec![
            ".byte $10, $20",
            "    .byte $10, $20",
            " label: .byte $10, $20   ; Comment",
            "  .byte($10, $20)",
            "  .byte (  $10 , $20  )",
        ]
        .into_iter()
        {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Control(ControlType::Byte), line, ".byte");
            assert!(node.left.is_none());
            assert!(node.right.is_none());

            let args = node.args.clone().unwrap();
            assert_eq!(args.len(), 2);
            assert_node(args.first().unwrap(), NodeType::Literal, line, "$10");
            assert_node(args.last().unwrap(), NodeType::Literal, line, "$20");
        }
    }

    #[test]
    fn parse_byte_with_character_literals() {
        let line = ".byte 'N', 'E', 'S', $1A";
        let mut parser = Parser::default();
        assert!(parser.parse(line.as_bytes()).is_ok());

        let args = parser
            .nodes
            .last()
            .unwrap()
            .first()
            .unwrap()
            .args
            .clone()
            .unwrap();
        assert_eq!(args.len(), 4);

        let mut it = args.into_iter();

        let mut cur = it.next().unwrap();
        let mut left = cur.left.as_ref().unwrap();
        assert_node(&cur, NodeType::Literal, line, "'N'");
        assert_eq!(&left.node_type, &NodeType::Value);
        assert_eq!(&left.value.value, "78");
        assert_eq!(line.get(left.value.start..left.value.end).unwrap(), "N");

        cur = it.next().unwrap();
        left = cur.left.as_ref().unwrap();
        assert_node(&cur, NodeType::Literal, line, "'E'");
        assert_eq!(&left.node_type, &NodeType::Value);
        assert_eq!(&left.value.value, "69");
        assert_eq!(line.get(left.value.start..left.value.end).unwrap(), "E");

        cur = it.next().unwrap();
        left = cur.left.as_ref().unwrap();
        assert_node(&cur, NodeType::Literal, line, "'S'");
        assert_eq!(&left.node_type, &NodeType::Value);
        assert_eq!(&left.value.value, "83");
        assert_eq!(line.get(left.value.start..left.value.end).unwrap(), "S");
    }

    #[test]
    fn parse_control_id_no_args() {
        for line in vec![
            ".scope Scope",
            "    .scope Scope",
            " label: .scope Scope   ; Comment",
            "  .scope   Scope",
        ]
        .into_iter()
        {
            // Add `.endscope` or it will error out on an unclosed macro
            // statement.
            let real = String::from(line) + "\n.endscope";

            let mut parser = Parser::default();
            assert!(parser.parse(real.as_bytes()).is_ok());

            let nodes = parser.nodes();
            let node = &nodes[nodes.len() - 2];
            assert_node(
                node,
                NodeType::Control(ControlType::StartScope),
                line,
                ".scope",
            );
            assert!(node.right.is_some());
            assert!(node.args.is_none());

            let left = node.left.clone().unwrap();
            assert_node(&left, NodeType::Value, line, "Scope");
        }
    }

    #[test]
    fn parse_control_id_one_arg() {
        for line in vec![
            ".macro Macro(arg1)",
            ".macro Macro arg1 ",
            "    .macro   Macro(arg1)",
            " label: .macro Macro(arg1)   ; Comment",
            "  .macro   Macro  ( arg1 )",
        ]
        .into_iter()
        {
            // Add `.endmacro` or it will error out on an unclosed macro
            // statement.
            let real = String::from(line) + "\n.endmacro";

            let mut parser = Parser::default();
            assert!(parser.parse(real.as_bytes()).is_ok());

            let nodes = parser.nodes();
            let node = &nodes[nodes.len() - 2];
            assert_node(
                node,
                NodeType::Control(ControlType::StartMacro),
                line,
                ".macro",
            );
            assert!(node.right.is_some());

            let left = node.left.clone().unwrap();
            assert_node(&left, NodeType::Value, line, "Macro");

            let args = node.args.clone().unwrap();
            assert_eq!(args.len(), 1);
            assert_node(args.first().unwrap(), NodeType::Value, line, "arg1");
        }
    }

    #[test]
    fn parse_control_id_multiple_args() {
        for line in vec![
            ".macro Macro(arg1, arg2)",
            ".macro Macro arg1, arg2 ",
            "    .macro   Macro(arg1, arg2)",
            " label: .macro Macro(arg1, arg2)   ; Comment",
            "  .macro   Macro  ( arg1 , arg2 )",
        ]
        .into_iter()
        {
            // Add `.endmacro` or it will error out on an unclosed macro
            // statement.
            let real = String::from(line) + "\n.endmacro";

            let mut parser = Parser::default();
            assert!(parser.parse(real.as_bytes()).is_ok());

            let nodes = parser.nodes();
            let node = &nodes[nodes.len() - 2];
            assert_node(
                node,
                NodeType::Control(ControlType::StartMacro),
                line,
                ".macro",
            );
            assert!(node.right.is_some());

            let left = node.left.clone().unwrap();
            assert_node(&left, NodeType::Value, line, "Macro");

            let args = node.args.clone().unwrap();
            assert_eq!(args.len(), 2);
            assert_node(args.first().unwrap(), NodeType::Value, line, "arg1");
            assert_node(args.last().unwrap(), NodeType::Value, line, "arg2");
        }
    }

    #[test]
    fn parse_control_unclosed() {
        let mut parser = Parser::default();
        let err = parser.parse(".macro MACRO".as_bytes()).unwrap_err();

        assert_eq!(
            err.first().unwrap().message,
            "expecting a 'control function (.endmacro)' but there are no more statements"
        );
    }

    #[test]
    fn parse_control_too_many_closes() {
        let mut parser = Parser::default();
        let err = parser.parse(".endmacro".as_bytes()).unwrap_err();

        assert_eq!(
            err.first().unwrap().message,
            "unexpected 'control function (.endmacro)'"
        );
    }

    #[test]
    fn parse_control_wrong_close() {
        let code = r#".scope Scope
.macro Macro
.endscope
.endmacro"#;
        let mut parser = Parser::default();
        let err = parser.parse(code.as_bytes()).unwrap_err();

        assert_eq!(
            err.first().unwrap().message,
            "expecting 'control function (.endmacro)', found 'control function (.endscope)'"
        );
        assert_eq!(
            err.last().unwrap().message,
            "expecting 'control function (.endscope)', found 'control function (.endmacro)'"
        );
    }

    #[test]
    fn parse_control_body() {
        let code = r#".macro MACRO
nop
inc $20
.endmacro"#;
        let mut parser = Parser::default();
        assert!(parser.parse(code.as_bytes()).is_ok());

        let nodes = parser.nodes();
        assert_eq!(nodes.len(), 2); // .macro and .endmacro

        let node = nodes.first().unwrap();
        assert_node(
            node,
            NodeType::Control(ControlType::StartMacro),
            code,
            ".macro",
        );

        let left = node.left.clone().unwrap();
        assert_node(&left, NodeType::Value, code, "MACRO");

        let right = node.right.clone().unwrap();
        assert_node(&right, NodeType::ControlBody, code, "");

        let inner = right.args.unwrap();
        assert_node(inner.first().unwrap(), NodeType::Instruction, "nop", "nop");
        assert_node(
            inner.last().unwrap(),
            NodeType::Instruction,
            "inc $20",
            "inc",
        );
    }

    #[test]
    fn parse_control_bad_number_args() {
        for line in vec![".hibyte", ".hibyte($20, $22)"].into_iter() {
            let mut parser = Parser::default();
            let err = parser.parse(line.as_bytes()).unwrap_err();

            assert_eq!(
                err.first().unwrap().message,
                "wrong number of arguments for function '.hibyte'"
            );
        }
    }

    #[test]
    fn parse_control_in_instructions() {
        for line in vec!["lda #.hibyte($2010)", "  label:   lda #.hibyte   $2010   "].into_iter() {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line, "lda");
            assert!(node.right.is_none());
            assert!(node.args.is_none());

            let left = node.left.clone().unwrap();
            assert_node(&left, NodeType::Literal, line, "#.hibyte");
            assert!(left.right.is_none());
            assert!(left.args.is_none());

            let control = left.left.clone().unwrap();
            assert_node(
                &control,
                NodeType::Control(ControlType::Hibyte),
                line,
                ".hibyte",
            );
            assert!(control.left.is_none());
            assert!(control.right.is_none());

            let args = control.args.clone().unwrap();
            assert_eq!(args.len(), 1);
            assert_node(args.first().unwrap(), NodeType::Literal, line, "$2010");
        }
    }

    #[test]
    fn parse_control_in_indirect_x_instructions() {
        for line in vec![
            "lda (#.hibyte($2010), x)",
            " label:   lda (#.hibyte ( $2010 ) , x)",
        ]
        .into_iter()
        {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line, "lda");
            assert!(node.right.is_none());
            assert!(node.args.is_none());

            let ind = node.left.clone().unwrap();
            assert_eq!(ind.node_type, NodeType::Indirection);
            assert!(ind.args.is_none());

            let left = ind.left.clone().unwrap();
            assert_node(&left, NodeType::Literal, line, "#.hibyte");
            assert!(left.right.is_none());
            assert!(left.args.is_none());

            let control = left.left.clone().unwrap();
            assert_node(
                &control,
                NodeType::Control(ControlType::Hibyte),
                line,
                ".hibyte",
            );
            assert!(control.left.is_none());
            assert!(control.right.is_none());

            let args = control.args.clone().unwrap();
            assert_eq!(args.len(), 1);
            assert_node(args.first().unwrap(), NodeType::Literal, line, "$2010");

            let right = ind.right.clone().unwrap();
            assert_node(&right, NodeType::Value, line, "x");
        }
    }

    #[test]
    fn parse_control_in_indirect_y_instructions() {
        for line in vec![
            "lda (#.hibyte($2010)), y",
            "  label:   lda ( #.hibyte( $2010  ) )  , y",
        ]
        .into_iter()
        {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Instruction, line, "lda");
            assert!(node.args.is_none());

            let ind = node.left.clone().unwrap();
            assert_eq!(ind.node_type, NodeType::Indirection);
            assert!(ind.right.is_none());
            assert!(ind.args.is_none());

            let left = ind.left.clone().unwrap();
            assert_node(&left, NodeType::Literal, line, "#.hibyte");
            assert!(left.right.is_none());
            assert!(left.args.is_none());

            let control = left.left.clone().unwrap();
            assert_node(
                &control,
                NodeType::Control(ControlType::Hibyte),
                line,
                ".hibyte",
            );
            assert!(control.left.is_none());
            assert!(control.right.is_none());

            let args = control.args.clone().unwrap();
            assert_eq!(args.len(), 1);
            assert_node(args.first().unwrap(), NodeType::Literal, line, "$2010");

            let right = node.right.clone().unwrap();
            assert_node(&right, NodeType::Value, line, "y");
        }
    }

    #[test]
    fn parse_control_in_assignments() {
        for line in vec![
            "lala = #.hibyte($2010)",
            "  lala   =     #.hibyte($2010)",
            "label:  lala   =     #.hibyte($2010)  ; comment",
        ]
        .into_iter()
        {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Assignment, line, "lala");
            assert!(node.right.is_none());
            assert!(node.args.is_none());

            let left = node.left.clone().unwrap();
            assert_node(&left, NodeType::Literal, line, "#.hibyte");
            assert!(left.right.is_none());
            assert!(left.args.is_none());

            let control = left.left.clone().unwrap();
            assert_node(
                &control,
                NodeType::Control(ControlType::Hibyte),
                line,
                ".hibyte",
            );
            assert!(control.left.is_none());
            assert!(control.right.is_none());

            let args = control.args.clone().unwrap();
            assert_eq!(args.len(), 1);
            assert_node(args.first().unwrap(), NodeType::Literal, line, "$2010");
        }
    }

    #[test]
    fn parse_unknown_control() {
        let mut parser = Parser::default();
        let mut err = parser.parse(".".as_bytes()).unwrap_err();
        assert_eq!(err.first().unwrap().message, "unknown function '.'");

        parser = Parser::default();
        err = parser.parse(".whatever".as_bytes()).unwrap_err();
        assert_eq!(err.first().unwrap().message, "unknown function '.whatever'");
    }

    // Macro calls.

    #[test]
    fn parse_macro_call_no_args_variable_lookalike() {
        for line in vec![
            "MACRO_CALL",
            " MACRO_CALL ",
            " label:  MACRO_CALL  ; comment",
        ]
        .into_iter()
        {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Value, line, "MACRO_CALL");
            assert!(node.left.is_none());
            assert!(node.right.is_none());
            assert!(node.args.is_none());
        }
    }

    #[test]
    fn parse_macro_call_no_args() {
        for line in vec![
            "MACRO_CALL()",
            " MACRO_CALL() ",
            " MACRO_CALL () ",
            " MACRO_CALL (  ) ",
            " label:  MACRO_CALL ()  ; comment",
        ]
        .into_iter()
        {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Call, line, "MACRO_CALL");
            assert!(node.left.is_none());
            assert!(node.right.is_none());
            assert!(node.args.is_none());
        }
    }

    #[test]
    fn parse_macro_call_one_arg() {
        for line in vec![
            "MACRO_CALL(arg1)",
            "MACRO_CALL arg1 ",
            "    MACRO_CALL (arg1)",
            " label: MACRO_CALL( arg1 )   ; Comment",
        ]
        .into_iter()
        {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Call, line, "MACRO_CALL");
            assert!(node.left.is_none());
            assert!(node.right.is_none());

            let args = node.args.clone().unwrap();
            assert_eq!(args.len(), 1);
            assert_node(args.first().unwrap(), NodeType::Value, line, "arg1");
        }
    }

    #[test]
    fn parse_macro_call_multiple_args() {
        for line in vec![
            "MACRO_CALL(arg1, arg2)",
            "MACRO_CALL arg1, arg2 ",
            "    MACRO_CALL (arg1,arg2)",
            " label: MACRO_CALL( arg1 , arg2 )   ; Comment",
        ]
        .into_iter()
        {
            let mut parser = Parser::default();
            assert!(parser.parse(line.as_bytes()).is_ok());

            let node = parser.nodes.last().unwrap().last().unwrap();
            assert_node(node, NodeType::Call, line, "MACRO_CALL");
            assert!(node.left.is_none());
            assert!(node.right.is_none());

            let args = node.args.clone().unwrap();
            assert_eq!(args.len(), 2);
            assert_node(args.first().unwrap(), NodeType::Value, line, "arg1");
            assert_node(args.last().unwrap(), NodeType::Value, line, "arg2");
        }
    }
}