blob: 102a3c330c122facdd1a26da0ebdbe56d1553bb3 [file] [log] [blame]
swissChilic71acc62021-12-07 08:03:37 -08001#pragma once
2
3#include <QString>
4
swissChili682e7bc2021-12-07 09:04:54 -08005#include "Token.h"
swissChilic71acc62021-12-07 08:03:37 -08006#include "AstNode.h"
swissChili8a581c62021-12-07 13:29:21 -08007#include "Function.h"
swissChilic71acc62021-12-07 08:03:37 -08008
swissChili847a78c2021-12-09 17:44:52 -08009struct ParsePos
10{
11 int line = 0,
12 pos = 0,
13 lineOffset = 0;
swissChili323883d2022-02-20 16:35:23 -080014
15 operator QString();
swissChili847a78c2021-12-09 17:44:52 -080016};
17
18class ParseResult
19{
20public:
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
39private:
40 int _status = COMPLETE;
41 QString _message = "";
42 ParsePos _pos;
43};
44
swissChilic71acc62021-12-07 08:03:37 -080045class Parser
46{
47public:
48 explicit Parser(QString input);
49
50 QChar peek();
51 QChar get();
52 bool atEnd();
53
54 void skip();
55
swissChili323883d2022-02-20 16:35:23 -080056 QString line(int n) const;
57
swissChili682e7bc2021-12-07 09:04:54 -080058 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080059 ParseResult parseSymbol(T *node);
swissChili682e7bc2021-12-07 09:04:54 -080060
61 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080062 ParseResult parseIdentifier(T *node);
swissChili682e7bc2021-12-07 09:04:54 -080063
64 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080065 ParseResult parseNumber(T *node);
swissChili682e7bc2021-12-07 09:04:54 -080066
67 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080068 ParseResult parseVariable(T *node);
swissChili682e7bc2021-12-07 09:04:54 -080069
70 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080071 ParseResult parseParens(T *node);
swissChili682e7bc2021-12-07 09:04:54 -080072
swissChili847a78c2021-12-09 17:44:52 -080073 ParseResult parseFunction(AstNode *node);
swissChili682e7bc2021-12-07 09:04:54 -080074
75 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080076 ParseResult parseMany(QList<T> *list);
swissChili682e7bc2021-12-07 09:04:54 -080077
78 template <typename T>
swissChili918557c2022-02-20 20:16:34 -080079 ParseResult parseString(QList<T> *list);
80
81 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080082 ParseResult parseOne(T *node);
swissChilic71acc62021-12-07 08:03:37 -080083
swissChili847a78c2021-12-09 17:44:52 -080084 ParseResult parseSentence(Sentence *sentence);
85 ParseResult parseFunctionDefinition(Function *function);
86
87 ParsePos save() const;
88 void reset(ParsePos pos);
swissChili8a581c62021-12-07 13:29:21 -080089
swissChilic71acc62021-12-07 08:03:37 -080090private:
91 int _pos = 0;
swissChili847a78c2021-12-09 17:44:52 -080092 int _line = 1;
93 int _offset = 1;
94
swissChilic71acc62021-12-07 08:03:37 -080095 QString _input;
96};
swissChili682e7bc2021-12-07 09:04:54 -080097
98template <>
swissChili847a78c2021-12-09 17:44:52 -080099ParseResult Parser::parseOne<Token>(Token *node);
swissChili682e7bc2021-12-07 09:04:54 -0800100
101template <>
swissChili847a78c2021-12-09 17:44:52 -0800102ParseResult Parser::parseOne<AstNode>(AstNode *node);