/
thomas-king
/
SyntacticAnalyzer
Обзор
Документация
Войти
/
thomas-king
/
SyntacticAnalyzer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
lib/Models/Token.cpp
142 строки
3 KB
Ace Rodstin
Implement SyntacticAnalyzer.
06 апр 2023, 09:52
06 апр 2023, 09:52
2d58f4b
Код
Авторство
О чём код?
// // Token.cpp // TokensParser // // Created by Ace Rodstin on 4/1/23. // Copyright © 2023 Ace Rodstin. All rights reserved. // #include "Models/Token.h" #include <sstream> using namespace ace; Token::Token(Kind kind) { this->kind = kind; } Token::Token(string identifier) { this->kind = Kind::identifier; this->identifier = identifier; } Token::Token(Keyword keyword) { this->kind = Kind::keyword; this->keyword = keyword; } Token::Token(Punctuator punctuator) { this->kind = Kind::punctuator; this->punctuator = punctuator; } Token::Token(Operator op) { this->kind = Kind::_operator; this->_operator = op; } Token::Token(Literal literal) { this->kind = Kind::literal; this->literal = literal; } Token::Kind Token::getKind() const { return kind; } string Token::getIdentifier() const { return identifier.value(); } Keyword Token::getKeyword() const { return keyword.value(); } Punctuator Token::getPunctuator() const { return punctuator.value(); } Operator Token::getOperator() const { return _operator.value(); } Literal Token::getLiteral() const { return literal.value(); } string Token::toString() const { stringstream stream; stream << "Token(kind: " << description(kind) << ", identifier: " << description(identifier) << ", keyword: " << description(keyword) << ", punctuator: " << description(punctuator) << ", operator: " << description(_operator) << ", literal: " << description(literal) << ")"; return stream.str(); } bool Token::operator ==(const Token& other) const { auto lhs = *this; auto rhs = other; return lhs.kind == rhs.kind && lhs.identifier == rhs.identifier && lhs.keyword == rhs.keyword && lhs.punctuator == rhs.punctuator && lhs._operator == rhs._operator && lhs.literal == rhs.literal; } string Token::description(Kind kind) const { string description; switch (kind) { case Kind::keyword: description = "keyword"; break; case Kind::identifier: description = "identifier"; break; case Kind::punctuator: description = "punctuator"; break; case Kind::_operator: description = "operator"; break; case Kind::literal: description = "literal"; break; case Kind::endOfInput: description = "endOfInput"; break; case Kind::unknown: description = "unknown"; break; } return description; } string Token::description(optional<string> identifier) const { if (identifier.has_value()) { return "\"" + identifier.value() + "\""; } else { return "null"; } } string Token::description(optional<Literal> literal) const { if (literal.has_value()) { return literal.value().toString(); } else { return "null"; } }