summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCMake/Token.h
blob: ca45a319459b4e17f0e46f5229267c03fa4ab762 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/StringView.h>
#include <AK/Vector.h>
#include <LibCMake/Position.h>

namespace CMake {

struct VariableReference {
    StringView value;
    Position start;
    Position end;
};

enum class ControlKeywordType {
    If,
    ElseIf,
    Else,
    EndIf,
    ForEach,
    EndForEach,
    While,
    EndWhile,
    Break,
    Continue,
    Return,
    Macro,
    EndMacro,
    Function,
    EndFunction,
    Block,
    EndBlock,
};

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);

}