Skip to content

Commit

Permalink
Parser: separate declaration from implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wpmed92 committed Aug 13, 2024
1 parent 1511589 commit 6e3f63b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
23 changes: 6 additions & 17 deletions include/Parser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,19 @@ enum ParserErrorKind {
};

struct ParserError {
ParserError() : kind(ParserErrorKind::None) {}
ParserError(ParserErrorKind kind, const std::string &msg) : kind(kind), msg(msg) {}
ParserError();
ParserError(ParserErrorKind kind, const std::string &msg);

ParserErrorKind kind;
std::string msg;

bool none() { return kind == ParserErrorKind::None; }
bool none();
};

class Parser {

public:
Parser(std::vector<std::unique_ptr<Token>> &tokens)
: tokenStream(tokens), cursor(-1), curToken(nullptr), parsingLhsExpression(false) {
advanceToken();
}

Parser(std::vector<std::unique_ptr<Token>> &tokens);
std::unique_ptr<TranslationUnit> parseTranslationUnit();
std::unique_ptr<ExternalDeclaration> parseExternalDeclaration();
std::unique_ptr<FunctionDeclaration> parseFunctionDeclaration();
Expand Down Expand Up @@ -108,15 +104,8 @@ class Parser {
makeMatrixType(std::vector<std::unique_ptr<TypeQualifier>>, TypeKind, int,
int);

void savePosition() {
savedPositions.push_back(cursor);
}

void rollbackPosition() {
cursor = savedPositions.back() - 1;
savedPositions.pop_back();
advanceToken();
}
void savePosition();
void rollbackPosition();
};

} // namespace parser
Expand Down
27 changes: 27 additions & 0 deletions lib/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ using namespace ast;

namespace parser {

Parser::Parser(std::vector<std::unique_ptr<Token>> &tokens)
: tokenStream(tokens), cursor(-1), curToken(nullptr), parsingLhsExpression(false) {
advanceToken();
}

ParserError::ParserError() : kind(ParserErrorKind::None) {

}

ParserError::ParserError(ParserErrorKind kind, const std::string &msg) : kind(kind), msg(msg) {

}

bool ParserError::none() {
return kind == ParserErrorKind::None;
}

std::map<BinaryOperator, int> Parser::binopPrecedence = {
{BinaryOperator::LogOr, 10}, {BinaryOperator::LogXor, 20},
{BinaryOperator::LogAnd, 30}, {BinaryOperator::BitIor, 40},
Expand Down Expand Up @@ -1503,6 +1520,16 @@ void Parser::reportError(ParserErrorKind kind, const std::string &msg) {
error = ParserError(kind, msg);
}

void Parser::savePosition() {
savedPositions.push_back(cursor);
}

void Parser::rollbackPosition() {
cursor = savedPositions.back() - 1;
savedPositions.pop_back();
advanceToken();
}

}; // namespace parser

}; // namespace shaderpulse

0 comments on commit 6e3f63b

Please sign in to comment.