1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:27:34 +00:00

LibCpp: Add the beginning of a C++ parser

This parser will be used by the C++ langauge server to provide better
auto-complete (& maybe also other things in the future).

It is designed to be error tolerant, and keeps track of the position
spans of the AST nodes, which should be useful later for incremental
parsing.
This commit is contained in:
Itamar 2021-01-23 16:47:20 +02:00 committed by Andreas Kling
parent aec9658b4f
commit c96b6987c4
11 changed files with 2298 additions and 9 deletions

View file

@ -96,11 +96,16 @@ namespace Cpp {
__TOKEN(Float) \
__TOKEN(Keyword) \
__TOKEN(KnownType) \
__TOKEN(Identifier)
__TOKEN(Identifier) \
__TOKEN(EOF_TOKEN)
struct Position {
size_t line;
size_t column;
size_t line { 0 };
size_t column { 0 };
bool operator<(const Position&) const;
bool operator>(const Position&) const;
bool operator==(const Position&) const;
};
struct Token {
@ -110,9 +115,9 @@ struct Token {
#undef __TOKEN
};
const char* to_string() const
static const char* type_to_string(Type t)
{
switch (m_type) {
switch (t) {
#define __TOKEN(x) \
case Type::x: \
return #x;
@ -122,6 +127,14 @@ struct Token {
ASSERT_NOT_REACHED();
}
const char* to_string() const
{
return type_to_string(m_type);
}
Position start() const { return m_start; }
Position end() const { return m_end; }
Type type() const { return m_type; }
Type m_type { Type::Unknown };
Position m_start;
Position m_end;