tcalc 0.2.0
 
Loading...
Searching...
No Matches
parser.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include <cstddef>
15#include <string_view>
16
17#include "tcalc/ast/node.hpp"
18#include "tcalc/common.hpp"
19#include "tcalc/error.hpp"
20#include "tcalc/token.hpp"
21#include "tcalc/tokenizer.hpp"
22
23namespace tcalc::ast {
24
29class TCALC_PUBLIC ParserContext
30{
31private:
32 token::Tokenizer _tokenizer;
33 token::Token _current;
34
35public:
42 static error::Result<ParserContext> create(std::string_view input);
43
44 ~ParserContext() = default;
45
51 [[nodiscard]] TCALC_INLINE auto& tokenizer() const noexcept
52 {
53 return _tokenizer;
54 }
55
61 [[nodiscard]] TCALC_INLINE auto& current() const noexcept { return _current; }
62
70
77
78private:
86 : _tokenizer{ tokenizer }
87 , _current{ std::move(current) }
88 {
89 }
90};
91
96class TCALC_PUBLIC Parser
97{
98public:
99 Parser() = default;
100 ~Parser() = default;
101
108 error::Result<NodePtr<>> parse(std::string_view input);
109
116 error::Result<NodePtr<>> next_program(ParserContext& ctx);
117
124 error::Result<NodePtr<>> next_statement(ParserContext& ctx);
125
132 error::Result<NodePtr<>> next_expr(ParserContext& ctx);
133
141 error::Result<NodePtr<>> next_prio_term(ParserContext& ctx, std::size_t prio);
142
150
157 error::Result<NodePtr<>> next_assign(ParserContext& ctx);
158
165 error::Result<NodePtr<>> next_factor(ParserContext& ctx);
166
173 error::Result<NodePtr<>> next_idref(ParserContext& ctx);
174
181 error::Result<NodePtr<>> next_fdef(ParserContext& ctx);
182
189 error::Result<NodePtr<>> next_import(ParserContext& ctx);
190};
191
192}
Wrapper around the tokenizer to provide a context for parsing.
Definition parser.hpp:30
TCALC_INLINE auto & current() const noexcept
Get current token.
Definition parser.hpp:61
TCALC_INLINE auto & tokenizer() const noexcept
Get tokenizer.
Definition parser.hpp:51
AST parser.
Definition parser.hpp:97
Tokenize the input string into tokens.
Definition tokenizer.hpp:30
tcalc common header.
tcalc error definition.
_TCALC_EXPECTED_NS::expected< T, Error > Result
Result type.
Definition error.hpp:153
Base class for AST nodes.
Token structure.
Definition token.hpp:60
Input token definition.
TokenType
Token type, some values are ASCII code of the corresponding character.
Definition token.hpp:25
Tokenize the input string into tokens.