aboutsummaryrefslogtreecommitdiffstats
path: root/parser.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'parser.hpp')
-rw-r--r--parser.hpp54
1 files changed, 44 insertions, 10 deletions
diff --git a/parser.hpp b/parser.hpp
index 65f2686..2ecf6f8 100644
--- a/parser.hpp
+++ b/parser.hpp
@@ -23,7 +23,6 @@
#define PARSER_HPP
#include "ast.hpp"
-#include "state.hpp"
#include <deque>
#include <optional>
@@ -33,27 +32,62 @@
class Parser {
public:
enum Error {
- None,
- ExpectedProcedure,
+ ExpectedProcedureCallOpen,
ExpectedIdentifier,
ExpectedProcedureCallClose,
ExpectedArgumentList,
UnknownIdentifier,
- InvalidOperator,
+ InvalidExpression,
InvalidOperand,
+ InvalidCondition,
+ InvalidThenBranch,
+ InvalidElseBranch,
+ InvalidInitializer,
+ InvalidAssignValue,
+ InvalidArgumentName,
+ InvalidLambdaBody
};
- void addString(const std::string& str);
+ /**
+ * Appends the given string to the end of the text queue.
+ * Call parse() after adding text to produce the AST.
+ */
+ void addString(const std::string&);
- void consumeWhitespace();
- std::optional<std::string> consumeIdentifier();
- std::optional<std::variant<int, double>> consumeLiteralNumber();
+ /**
+ * Attempts to parse all text in the text queue, stopping if there is an
+ * error.
+ * @return An iterable collection of produced AST nodes.
+ */
+ std::deque<AST::Node *> parse();
+
+ std::string describeErrors() noexcept;
- AST::Node *parse(CompilerState&);
+ bool hasErrors() const noexcept {
+ return !errors.empty();
+ }
private:
std::deque<char> text;
- Error lastError = None;
+ std::deque<Error> errors;
+
+ /**
+ * Advances through the text queue until a non-whitespace character is
+ * found.
+ */
+ void consumeWhitespace() noexcept;
+ /**
+ * Attempts to consume an identifier from the text queue.
+ * @return A string containing the identifier if one is found.
+ */
+ std::optional<std::string> consumeIdentifier() noexcept;
+ /**
+ * Attempts to consume a literal number from the text queue.
+ * @return The number if one is found: an integer if there is no decimal
+ * point; otherwise, a double.
+ * TODO Ensure and add noexcept.
+ */
+ std::optional<std::variant<int, double>> consumeLiteralNumber();
AST::Node *parseExpression();
AST::Node *parseProcedureCall();