swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 1 | #pragma once |
2 | |||||
3 | #include <QString> | ||||
4 | |||||
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 5 | #include "Token.h" |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 6 | #include "AstNode.h" |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 7 | #include "Function.h" |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 8 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 9 | struct ParsePos |
10 | { | ||||
11 | int line = 0, | ||||
12 | pos = 0, | ||||
13 | lineOffset = 0; | ||||
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 14 | |
15 | operator QString(); | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 16 | }; |
17 | |||||
18 | class ParseResult | ||||
19 | { | ||||
20 | public: | ||||
21 | ParseResult() = default; | ||||
22 | ParseResult(bool okay); | ||||
23 | explicit ParseResult(int status, ParsePos pos = {}); | ||||
24 | ParseResult(int status, QString message, ParsePos pos = {}); | ||||
25 | |||||
26 | enum | ||||
27 | { | ||||
28 | NO_MATCH, | ||||
29 | COMPLETE, | ||||
30 | INCOMPLETE, | ||||
31 | }; | ||||
32 | |||||
33 | operator bool() const; | ||||
34 | |||||
35 | ParsePos pos() const; | ||||
36 | QString message() const; | ||||
37 | int status() const; | ||||
38 | |||||
39 | private: | ||||
40 | int _status = COMPLETE; | ||||
41 | QString _message = ""; | ||||
42 | ParsePos _pos; | ||||
43 | }; | ||||
44 | |||||
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 45 | class Parser |
46 | { | ||||
47 | public: | ||||
48 | explicit Parser(QString input); | ||||
49 | |||||
50 | QChar peek(); | ||||
51 | QChar get(); | ||||
52 | bool atEnd(); | ||||
53 | |||||
54 | void skip(); | ||||
55 | |||||
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 56 | QString line(int n) const; |
57 | |||||
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 58 | template <typename T> |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 59 | ParseResult parseSymbol(T *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 60 | |
61 | template <typename T> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 62 | ParseResult parseIdentifier(T *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 63 | |
64 | template <typename T> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 65 | ParseResult parseNumber(T *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 66 | |
67 | template <typename T> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 68 | ParseResult parseVariable(T *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 69 | |
70 | template <typename T> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 71 | ParseResult parseParens(T *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 72 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 73 | ParseResult parseFunction(AstNode *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 74 | |
75 | template <typename T> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 76 | ParseResult parseMany(QList<T> *list); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 77 | |
78 | template <typename T> | ||||
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 79 | ParseResult parseString(QList<T> *list); |
80 | |||||
81 | template <typename T> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 82 | ParseResult parseOne(T *node); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 83 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 84 | ParseResult parseSentence(Sentence *sentence); |
85 | ParseResult parseFunctionDefinition(Function *function); | ||||
86 | |||||
87 | ParsePos save() const; | ||||
88 | void reset(ParsePos pos); | ||||
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 89 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 90 | private: |
91 | int _pos = 0; | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 92 | int _line = 1; |
93 | int _offset = 1; | ||||
94 | |||||
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 95 | QString _input; |
96 | }; | ||||
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 97 | |
98 | template <> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 99 | ParseResult Parser::parseOne<Token>(Token *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 100 | |
101 | template <> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 102 | ParseResult Parser::parseOne<AstNode>(AstNode *node); |