mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 08:07:44 +00:00
LibCMake: Introduce a CMake lexer
This commit is contained in:
parent
ca4cc03269
commit
3d8cc2257f
8 changed files with 581 additions and 0 deletions
102
Userland/Libraries/LibCMake/Token.h
Normal file
102
Userland/Libraries/LibCMake/Token.h
Normal file
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
namespace CMake {
|
||||
|
||||
struct Position {
|
||||
size_t line { 0 };
|
||||
size_t column { 0 };
|
||||
};
|
||||
|
||||
struct VariableReference {
|
||||
StringView value;
|
||||
Position start;
|
||||
Position end;
|
||||
};
|
||||
|
||||
enum class ControlKeywordType {
|
||||
If,
|
||||
ElseIf,
|
||||
Else,
|
||||
EndIf,
|
||||
ForEach,
|
||||
EndForEach,
|
||||
While,
|
||||
EndWhile,
|
||||
Break,
|
||||
Continue,
|
||||
Macro,
|
||||
EndMacro,
|
||||
Function,
|
||||
EndFunction,
|
||||
};
|
||||
|
||||
struct Token {
|
||||
enum class Type {
|
||||
BracketComment,
|
||||
LineComment,
|
||||
Identifier,
|
||||
ControlKeyword,
|
||||
OpenParen,
|
||||
CloseParen,
|
||||
BracketArgument,
|
||||
QuotedArgument,
|
||||
UnquotedArgument,
|
||||
Garbage,
|
||||
|
||||
// These are elements inside argument tokens
|
||||
VariableReference,
|
||||
};
|
||||
|
||||
Type type;
|
||||
StringView value;
|
||||
|
||||
Position start;
|
||||
Position end;
|
||||
|
||||
// Type-specific
|
||||
Optional<ControlKeywordType> control_keyword {};
|
||||
Vector<VariableReference> variable_references {};
|
||||
};
|
||||
|
||||
static constexpr StringView to_string(Token::Type type)
|
||||
{
|
||||
switch (type) {
|
||||
case Token::Type::BracketComment:
|
||||
return "BracketComment"sv;
|
||||
case Token::Type::LineComment:
|
||||
return "LineComment"sv;
|
||||
case Token::Type::Identifier:
|
||||
return "Identifier"sv;
|
||||
case Token::Type::ControlKeyword:
|
||||
return "ControlKeyword"sv;
|
||||
case Token::Type::OpenParen:
|
||||
return "OpenParen"sv;
|
||||
case Token::Type::CloseParen:
|
||||
return "CloseParen"sv;
|
||||
case Token::Type::BracketArgument:
|
||||
return "BracketArgument"sv;
|
||||
case Token::Type::QuotedArgument:
|
||||
return "QuotedArgument"sv;
|
||||
case Token::Type::UnquotedArgument:
|
||||
return "UnquotedArgument"sv;
|
||||
case Token::Type::Garbage:
|
||||
return "Garbage"sv;
|
||||
case Token::Type::VariableReference:
|
||||
return "VariableReference"sv;
|
||||
}
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
Optional<ControlKeywordType> control_keyword_from_string(StringView value);
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue