mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:27:35 +00:00
LibCpp: Parse type qualifiers
This commit is contained in:
parent
71a123148a
commit
5a7abb8363
4 changed files with 34 additions and 5 deletions
|
@ -82,7 +82,10 @@ void Type::dump(size_t indent) const
|
||||||
{
|
{
|
||||||
ASTNode::dump(indent);
|
ASTNode::dump(indent);
|
||||||
print_indent(indent + 1);
|
print_indent(indent + 1);
|
||||||
outln("{}", m_name);
|
String qualifiers_string;
|
||||||
|
if(!m_qualifiers.is_empty())
|
||||||
|
qualifiers_string = String::formatted("[{}] ", String::join(" ", m_qualifiers));
|
||||||
|
outln("{}{}", qualifiers_string, m_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Parameter::dump(size_t indent) const
|
void Parameter::dump(size_t indent) const
|
||||||
|
|
|
@ -226,6 +226,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
StringView m_name;
|
StringView m_name;
|
||||||
|
Vector<StringView> m_qualifiers;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Pointer : public Type {
|
class Pointer : public Type {
|
||||||
|
|
|
@ -24,6 +24,9 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#undef CPP_DEBUG
|
||||||
|
#define CPP_DEBUG 1
|
||||||
|
|
||||||
#ifdef CPP_DEBUG
|
#ifdef CPP_DEBUG
|
||||||
# define DEBUG_SPAM
|
# define DEBUG_SPAM
|
||||||
#endif
|
#endif
|
||||||
|
@ -226,9 +229,11 @@ NonnullRefPtr<BlockStatement> Parser::parse_block_statement(ASTNode& parent)
|
||||||
|
|
||||||
bool Parser::match_variable_declaration()
|
bool Parser::match_variable_declaration()
|
||||||
{
|
{
|
||||||
|
SCOPE_LOGGER();
|
||||||
save_state();
|
save_state();
|
||||||
ScopeGuard state_guard = [this] { load_state(); };
|
ScopeGuard state_guard = [this] { load_state(); };
|
||||||
|
|
||||||
|
parse_type_qualifiers();
|
||||||
// Type
|
// Type
|
||||||
if (!peek(Token::Type::KnownType).has_value() && !peek(Token::Type::Identifier).has_value())
|
if (!peek(Token::Type::KnownType).has_value() && !peek(Token::Type::Identifier).has_value())
|
||||||
return false;
|
return false;
|
||||||
|
@ -255,12 +260,12 @@ NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration(ASTNode& p
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
SCOPE_LOGGER();
|
||||||
auto var = create_ast_node<VariableDeclaration>(parent, position(), {});
|
auto var = create_ast_node<VariableDeclaration>(parent, position(), {});
|
||||||
auto type_token = consume();
|
if (!match_variable_declaration()) {
|
||||||
if (type_token.type() != Token::Type::KnownType && type_token.type() != Token::Type::Identifier) {
|
|
||||||
error("unexpected token for variable type");
|
error("unexpected token for variable type");
|
||||||
var->set_end(type_token.end());
|
var->set_end(position());
|
||||||
return var;
|
return var;
|
||||||
}
|
}
|
||||||
|
var->m_type = parse_type(var);
|
||||||
auto identifier_token = consume(Token::Type::Identifier);
|
auto identifier_token = consume(Token::Type::Identifier);
|
||||||
RefPtr<Expression> initial_value;
|
RefPtr<Expression> initial_value;
|
||||||
|
|
||||||
|
@ -270,7 +275,6 @@ NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration(ASTNode& p
|
||||||
}
|
}
|
||||||
|
|
||||||
var->set_end(position());
|
var->set_end(position());
|
||||||
var->m_type = create_ast_node<Type>(var, type_token.m_start, type_token.m_end, text_of_token(type_token));
|
|
||||||
var->m_name = text_of_token(identifier_token);
|
var->m_name = text_of_token(identifier_token);
|
||||||
var->m_initial_value = move(initial_value);
|
var->m_initial_value = move(initial_value);
|
||||||
|
|
||||||
|
@ -983,8 +987,10 @@ bool Parser::match_boolean_literal()
|
||||||
NonnullRefPtr<Type> Parser::parse_type(ASTNode& parent)
|
NonnullRefPtr<Type> Parser::parse_type(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
SCOPE_LOGGER();
|
||||||
|
auto qualifiers = parse_type_qualifiers();
|
||||||
auto token = consume();
|
auto token = consume();
|
||||||
auto type = create_ast_node<Type>(parent, token.start(), token.end(), text_of_token(token));
|
auto type = create_ast_node<Type>(parent, token.start(), token.end(), text_of_token(token));
|
||||||
|
type->m_qualifiers = move(qualifiers);
|
||||||
if (token.type() != Token::Type::KnownType && token.type() != Token::Type::Identifier) {
|
if (token.type() != Token::Type::KnownType && token.type() != Token::Type::Identifier) {
|
||||||
error(String::formatted("unexpected token for type: {}", token.to_string()));
|
error(String::formatted("unexpected token for type: {}", token.to_string()));
|
||||||
return type;
|
return type;
|
||||||
|
@ -1033,4 +1039,22 @@ NonnullRefPtr<IfStatement> Parser::parse_if_statement(ASTNode& parent)
|
||||||
}
|
}
|
||||||
return if_statement;
|
return if_statement;
|
||||||
}
|
}
|
||||||
|
Vector<StringView> Parser::parse_type_qualifiers()
|
||||||
|
{
|
||||||
|
SCOPE_LOGGER();
|
||||||
|
Vector<StringView> qualifiers;
|
||||||
|
while (!done()) {
|
||||||
|
auto token = peek();
|
||||||
|
if (token.type() != Token::Type::Keyword)
|
||||||
|
break;
|
||||||
|
auto text = text_of_token(token);
|
||||||
|
if (text == "static" || text == "const") {
|
||||||
|
qualifiers.append(text);
|
||||||
|
consume();
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return qualifiers;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -160,6 +160,7 @@ private:
|
||||||
RefPtr<TranslationUnit> m_root_node;
|
RefPtr<TranslationUnit> m_root_node;
|
||||||
NonnullRefPtrVector<ASTNode> m_nodes;
|
NonnullRefPtrVector<ASTNode> m_nodes;
|
||||||
Vector<String> m_errors;
|
Vector<String> m_errors;
|
||||||
|
Vector<StringView> parse_type_qualifiers();
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue