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 | |||||
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 39 | operator QString() const; |
40 | |||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 41 | private: |
42 | int _status = COMPLETE; | ||||
43 | QString _message = ""; | ||||
44 | ParsePos _pos; | ||||
45 | }; | ||||
46 | |||||
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 47 | class Parser |
48 | { | ||||
49 | public: | ||||
50 | explicit Parser(QString input); | ||||
51 | |||||
52 | QChar peek(); | ||||
53 | QChar get(); | ||||
54 | bool atEnd(); | ||||
55 | |||||
56 | void skip(); | ||||
57 | |||||
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 58 | QString line(int n) const; |
59 | |||||
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 60 | template <typename T> |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 61 | ParseResult parseSymbol(T *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 62 | |
63 | template <typename T> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 64 | ParseResult parseIdentifier(T *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 65 | |
66 | template <typename T> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 67 | ParseResult parseNumber(T *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 68 | |
69 | template <typename T> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 70 | ParseResult parseVariable(T *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 71 | |
72 | template <typename T> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 73 | ParseResult parseParens(T *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 74 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 75 | ParseResult parseFunction(AstNode *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 76 | |
77 | template <typename T> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 78 | ParseResult parseMany(QList<T> *list); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 79 | |
80 | template <typename T> | ||||
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 81 | ParseResult parseString(QList<T> *list); |
82 | |||||
83 | template <typename T> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 84 | ParseResult parseOne(T *node); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 85 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 86 | ParseResult parseSentence(Sentence *sentence); |
87 | ParseResult parseFunctionDefinition(Function *function); | ||||
88 | |||||
89 | ParsePos save() const; | ||||
90 | void reset(ParsePos pos); | ||||
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 91 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 92 | private: |
93 | int _pos = 0; | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 94 | int _line = 1; |
95 | int _offset = 1; | ||||
96 | |||||
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 97 | QString _input; |
98 | }; | ||||
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 99 | |
100 | template <> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 101 | ParseResult Parser::parseOne<Token>(Token *node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 102 | |
103 | template <> | ||||
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 104 | ParseResult Parser::parseOne<AstNode>(AstNode *node); |