blob: d0a348d60520800dc864e67e9925856b19882bc6 [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
swissChilid85daa92022-02-24 15:29:02 -080039 operator QString() const;
40
swissChili847a78c2021-12-09 17:44:52 -080041private:
42 int _status = COMPLETE;
43 QString _message = "";
44 ParsePos _pos;
45};
46
swissChilic71acc62021-12-07 08:03:37 -080047class Parser
48{
49public:
50 explicit Parser(QString input);
51
52 QChar peek();
53 QChar get();
54 bool atEnd();
55
56 void skip();
57
swissChili323883d2022-02-20 16:35:23 -080058 QString line(int n) const;
59
swissChili682e7bc2021-12-07 09:04:54 -080060 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080061 ParseResult parseSymbol(T *node);
swissChili682e7bc2021-12-07 09:04:54 -080062
63 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080064 ParseResult parseIdentifier(T *node);
swissChili682e7bc2021-12-07 09:04:54 -080065
66 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080067 ParseResult parseNumber(T *node);
swissChili682e7bc2021-12-07 09:04:54 -080068
69 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080070 ParseResult parseVariable(T *node);
swissChili682e7bc2021-12-07 09:04:54 -080071
72 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080073 ParseResult parseParens(T *node);
swissChili682e7bc2021-12-07 09:04:54 -080074
swissChili847a78c2021-12-09 17:44:52 -080075 ParseResult parseFunction(AstNode *node);
swissChili682e7bc2021-12-07 09:04:54 -080076
77 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080078 ParseResult parseMany(QList<T> *list);
swissChili682e7bc2021-12-07 09:04:54 -080079
80 template <typename T>
swissChili918557c2022-02-20 20:16:34 -080081 ParseResult parseString(QList<T> *list);
82
83 template <typename T>
swissChili847a78c2021-12-09 17:44:52 -080084 ParseResult parseOne(T *node);
swissChilic71acc62021-12-07 08:03:37 -080085
swissChili847a78c2021-12-09 17:44:52 -080086 ParseResult parseSentence(Sentence *sentence);
87 ParseResult parseFunctionDefinition(Function *function);
88
89 ParsePos save() const;
90 void reset(ParsePos pos);
swissChili8a581c62021-12-07 13:29:21 -080091
swissChilic71acc62021-12-07 08:03:37 -080092private:
93 int _pos = 0;
swissChili847a78c2021-12-09 17:44:52 -080094 int _line = 1;
95 int _offset = 1;
96
swissChilic71acc62021-12-07 08:03:37 -080097 QString _input;
98};
swissChili682e7bc2021-12-07 09:04:54 -080099
100template <>
swissChili847a78c2021-12-09 17:44:52 -0800101ParseResult Parser::parseOne<Token>(Token *node);
swissChili682e7bc2021-12-07 09:04:54 -0800102
103template <>
swissChili847a78c2021-12-09 17:44:52 -0800104ParseResult Parser::parseOne<AstNode>(AstNode *node);