|
1 |
| -// Parser.h |
2 | 1 | #ifndef PARSER_H
|
3 | 2 | #define PARSER_H
|
4 | 3 |
|
5 | 4 | #include <QObject>
|
6 | 5 | #include <QList>
|
| 6 | +#include <stdexcept> |
| 7 | +#include <QListIterator> |
7 | 8 |
|
8 | 9 | #include "Token.h"
|
9 | 10 | #include "Node.h"
|
10 | 11 |
|
11 |
| -using namespace Tiny::Data; |
12 |
| - |
13 |
| -namespace Tiny::Parser { |
14 |
| - class Parser : public QObject { |
15 |
| - Q_OBJECT |
16 |
| - public: |
17 |
| - explicit Parser(QObject *parent = nullptr); |
18 |
| - ~Parser() override; |
19 |
| - |
20 |
| - void setTokens(QList<Token> tokens); |
21 |
| - Node* parse(); |
22 |
| - signals: |
23 |
| - void error(QString message); |
24 |
| - private: |
25 |
| - QList<Token> tokens; |
26 |
| - int currentTokenIndex; |
27 |
| - int currentLevel; |
28 |
| - |
29 |
| - void advance(); |
30 |
| - |
31 |
| - void expectAndSkip(Token::TokenType type); |
32 |
| - Node* consume(Token::TokenType tokenType, Tiny::Data::Node::NodeType nodeType); |
33 |
| - |
34 |
| - // rules |
35 |
| - Node* parseStmtSequence(); |
36 |
| - Node* parseStatement(); |
37 |
| - Node* parseIfStatement(); |
38 |
| - Node* parseRepeatStatement(); |
39 |
| - Node* parseAssignStatement(); |
40 |
| - Node* parseReadStatement(); |
41 |
| - Node* parseWriteStatement(); |
42 |
| - Node* parseExp(); |
43 |
| - Node* parseSimpleExp(); |
44 |
| - Node* parseComparisonOp(); |
45 |
| - Node* parseAddop(); |
46 |
| - Node* parseTerm(); |
47 |
| - Node* parseMulop(); |
48 |
| - Node* parseFactor(); |
49 |
| - }; |
50 |
| - |
51 |
| - |
52 |
| -} // namespace Tiny::Parser |
53 |
| - |
54 |
| -#endif // PARSER_H |
| 12 | +namespace Tiny { |
| 13 | +namespace Parser { |
| 14 | + |
| 15 | +/** |
| 16 | + * @class Parser |
| 17 | + * @brief A parser for the TINY language that constructs a syntax tree from a list of tokens. |
| 18 | + * |
| 19 | + * The Parser class takes a list of tokens (produced by the scanner) and attempts to construct a |
| 20 | + * syntax tree according to the TINY language grammar rules. It emits errors or throws exceptions |
| 21 | + * if unexpected tokens or end-of-file conditions occur. |
| 22 | + * |
| 23 | + * Usage: |
| 24 | + * 1. Create a Parser object. |
| 25 | + * 2. Call setTokens() with the token list. |
| 26 | + * 3. Call parse() to parse the entire token list and produce a syntax tree root Node. |
| 27 | + * 4. If parsing is successful, you get a syntax tree. If errors occur, handle them as needed. |
| 28 | + */ |
| 29 | +class Parser : public QObject { |
| 30 | + Q_OBJECT |
| 31 | +public: |
| 32 | + explicit Parser(QObject *parent = nullptr); |
| 33 | + ~Parser() override; |
| 34 | + |
| 35 | + /** |
| 36 | + * @brief Set the token list to be parsed. |
| 37 | + * @param tokens The list of tokens produced by the scanner. |
| 38 | + */ |
| 39 | + void setTokens(QList<Tiny::Data::Token> tokens); |
| 40 | + |
| 41 | + /** |
| 42 | + * @brief Parse the entire set of tokens and build a syntax tree. |
| 43 | + * @return A pointer to the root Node of the syntax tree if successful, or nullptr if empty. |
| 44 | + * @throws std::out_of_range if unexpected end of file (no more tokens) is encountered improperly. |
| 45 | + * @throws std::invalid_argument if a token does not match the expected type. |
| 46 | + */ |
| 47 | + Tiny::Data::Node* parse(); |
| 48 | + |
| 49 | +signals: |
| 50 | + /** |
| 51 | + * @brief Emitted when a parsing error occurs (e.g., unexpected token or EOF). |
| 52 | + * @param message A descriptive error message. |
| 53 | + */ |
| 54 | + void error(QString message); |
| 55 | + |
| 56 | +private: |
| 57 | + QList<Tiny::Data::Token> tokens; ///< The list of tokens to parse. |
| 58 | + QListIterator<Tiny::Data::Token> tokenIterator; ///< Iterator for the token list. |
| 59 | + int currentLevel; ///< Current nesting level for the syntax tree. |
| 60 | + bool hasError; ///< Flag indicating if an error occurred during parsing. |
| 61 | + |
| 62 | + /** |
| 63 | + * @brief Peek at the current token without consuming it. |
| 64 | + * @return The current token. |
| 65 | + * @throws std::out_of_range if no more tokens. |
| 66 | + */ |
| 67 | + Tiny::Data::Token peek(); |
| 68 | + |
| 69 | + void match(Tiny::Data::Token::TokenType expectedType); |
| 70 | + |
| 71 | + void assignLevels(Tiny::Data::Node* node, int baseLevel); |
| 72 | + |
| 73 | + // Grammar rules: |
| 74 | + Tiny::Data::Node* parseStmtSequence(); |
| 75 | + Tiny::Data::Node* parseStatement(); |
| 76 | + Tiny::Data::Node* parseIfStatement(); |
| 77 | + Tiny::Data::Node* parseRepeatStatement(); |
| 78 | + Tiny::Data::Node* parseAssignStatement(); |
| 79 | + Tiny::Data::Node* parseReadStatement(); |
| 80 | + Tiny::Data::Node* parseWriteStatement(); |
| 81 | + Tiny::Data::Node* parseExp(); |
| 82 | + Tiny::Data::Node* parseSimpleExp(); |
| 83 | + Tiny::Data::Node* parseComparisonOp(); |
| 84 | + Tiny::Data::Node* parseAddop(); |
| 85 | + Tiny::Data::Node* parseTerm(); |
| 86 | + Tiny::Data::Node* parseMulop(); |
| 87 | + Tiny::Data::Node* parseFactor(); |
| 88 | +}; |
| 89 | + |
| 90 | +} // namespace Parser |
| 91 | +} // namespace Tiny |
| 92 | + |
| 93 | +#endif // PARSER_H |
0 commit comments