void error(const char *message, const char *param) { token* t = (token*)tokens->elements[index]; printf("In file \"%s\": line %d; token: %s(%s)\n", current_file, t->line-1, token_type_str(t->id), t->data); printf("Parser error: %s%s\n", message, param); exit(EXIT_FAILURE); }
ASTError::ASTError(const std::string &msg, const NyanToken &token) : ParserError{"", token.line, token.line_offset} { std::ostringstream builder; builder << msg << ": " << token_type_str(token.type); this->msg = builder.str(); }
std::string Token::str() const { std::ostringstream builder; builder << "(" << this->location.get_line() << ":" << this->location.get_line_offset() << ": " << token_type_str(this->type); if (this->value.size() > 0) { builder << " '" << this->value << "'"; } builder << ")"; return builder.str(); }
/* * Remember where the current open bracket is * so that the indentation can check if the depth is correct. */ void Impl::track_brackets(token_type type, int token_start) { // opening brackets if (type == token_type::LPAREN or type == token_type::LANGLE or type == token_type::LBRACKET or type == token_type::LBRACE) { // Track bracket type and indentation. // The position after the ( is exactly the expected indent // for hanging brackets. this->brackets.emplace( type, token_start + 1 ); this->possibly_hanging = true; return; } // closing brackets else if (type == token_type::RPAREN or type == token_type::RANGLE or type == token_type::RBRACKET or type == token_type::RBRACE) { if (this->brackets.empty()) { throw this->error("unexpected closing bracket, " "as no opening one is known"); } Bracket &matching_open_bracket = this->brackets.top(); // test if bracket actually matches if (not matching_open_bracket.matches(type)) { std::ostringstream builder; builder << "non-matching bracket: expected '" << matching_open_bracket.matching_type_str() << "' but got '" << token_type_str(type) << "'"; throw this->error(builder.str()); } if (not matching_open_bracket.closing_indent_ok(token_start)) { std::ostringstream builder; builder << "wrong indentation of bracket: expected " << matching_open_bracket.get_closing_indent() << " indentation spaces (it is currently at " << token_start << " spaces)"; throw this->error(builder.str()); } this->bracketcloseindent_expected = false; this->brackets.pop(); } // newline directly after opening bracket // means regular indentation has to follow // and the bracket pair doesn't hang. else if (not this->brackets.empty() and this->possibly_hanging and type == token_type::ENDLINE) { // the bracket is followed by a newline directly, // thus is not hanging. this->brackets.top().doesnt_hang( this->previous_indent ); } else if (not this->brackets.empty() and this->bracketcloseindent_expected) { std::ostringstream builder; builder << ("expected closing bracket or content " "at indentation with ") << this->brackets.top().get_content_indent() << " spaces (you start at " << token_start << " spaces)"; throw this->error(builder.str()); } this->possibly_hanging = false; }