aboutsummaryrefslogtreecommitdiffstats
path: root/code/editor/editor_lexer.c
blob: daffdb44a38ce03e9f8e2a51928bc69d53807f41 (plain) (blame)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
NO_WARNINGS_BEGIN

internal inline b32
IsWhiteSpace(rune Character)
{
    return (Character == '\n' || Character == '\r' ||
            Character == ' ' || Character == '\t');
}

internal b32
IsDelimiter(rune Character)
{
    for(s32 Index = 0; Index < ArrayCount(Delimiters); ++Index)
    {
        if(Delimiters[Index] == Character)
        {
            return 1;
        }
    }
    return 0;
}

internal inline b32
IsNilTokenNode(token_node *TokenNode)
{
    return TokenNode == &nil_token_node || TokenNode == NULL;
}

internal inline b32
IsNilToken(token *Token)
{
    return Token == &nil_token || Token == NULL;
}

internal inline void
ParseCStyleComment(rune Buffer[])
{
    // TODO(nasr): handle c style comments
    // couuld be usefull for function information visualiszation
    // so think of a way to link themn to functions and variables?
    // some sort of meta data per thing?
    // and then we can do a visualization if the str8.count of the metadata thing is bigger then 0
    // we should a visualization thing for the thing
    // if the thing is less then 0, we dont do anything?
    
    // TODO(nasr): while doingn this we could also add in some editor specific anotations ?
}

internal inline void
ParseCPPStyleComment(rune Buffer[])
{
    // TODO(nasr):
}

internal inline b32
Is_TokenBreak(rune Character)
{
    return (IsWhiteSpace(Character) || IsDelimiter(Character));
}

internal token_list *
Lex(app_text *Text, arena *Arena, token_list *List)
{
    b32 Initialized = 0;
    s32 Line        = 1;
    s32 Column      = 1;
    
    for(s32 TextIndex = 0; TextIndex < Text->Count; TextIndex++)
    {
        rune Character = Text->Data[TextIndex];
        
        if(Character == '\r' || Character == '\n')
        {
            if(Character == '\r' &&
               (TextIndex + 1 < Text->Count) &&
               Text->Data[TextIndex + 1] == '\n')
            {
                TextIndex++;
            }
            
            ++TextIndex;
            ++Line;
            
            // NOTE(nasr): reset the column to the beginning of the line
            Column = 1;
            continue;
        }
        
        if(IsWhiteSpace(Character))
        {
            ++Column;
            continue;
        }
        
        token_node *TokenNode = PushStructZero(Arena, token_node);
        token      *Token     = PushStructZero(Arena, token);
        TokenNode->Next       = &nil_token_node;
        TokenNode->Previous   = &nil_token_node;
        TokenNode->Token      = Token;
        Token->Line           = Line;
        Token->Column         = Column;
        Token->ByteOffset     = (u64)TextIndex;
        Token->Flags          = FlagNone;
        
        s32 TokenStart = TextIndex;
        s32 TokenEnd   = TextIndex;
        
        if(Character > 126)
        {
            Token->Type = TokenUnwantedChild;
            TokenEnd    = TextIndex + 1;
        }
        else if(IsRuneAlpha(Character))
        {
            {
                while((TextIndex + 1 < Text->Count) &&
                      (IsRuneAlpha(Text->Data[TextIndex + 1]) || IsRuneDigit(Text->Data[TextIndex + 1])))
                {
                    ++TextIndex;
                }
                
                TokenEnd = TextIndex + 1;
            }
            {
                // NOTE(luca): This will never work, since the App->Text is a u32 array, should it be str32 instead?
                str8 Lexeme = S8FromTo((S8Cast{.Data = (u8 *)Text->Data, .Size = (u64)Text->Count}), (u64)TokenStart, (u64)TokenEnd);
                
                // TODO(nasr): handle functions
                if(S8Match(Lexeme, S8("if"), 0))
                    Token->Type = TokenIf;
                else if(S8Match(Lexeme, S8("else"), 0))
                    Token->Type = TokenElse;
                else if(S8Match(Lexeme, S8("return"), 0))
                    Token->Type = TokenReturn;
                else if(S8Match(Lexeme, S8("while"), 0))
                    Token->Type = TokenWhile;
                else if(S8Match(Lexeme, S8("for"), 0))
                    Token->Type = TokenFor;
                else if(S8Match(Lexeme, S8("break"), 0))
                    Token->Type = TokenBreak;
                else if(S8Match(Lexeme, S8("continue"), 0))
                    Token->Type = TokenContinue;
                else
                    Token->Type = TokenIdentifier;
            }
        }
        else if(IsRuneDigit(Character))
        {
            while((TextIndex + 1 < Text->Count) &&
                  IsRuneDigit(Text->Data[TextIndex + 1]))
            {
                ++TextIndex;
            }
            
            TokenEnd    = TextIndex + 1;
            Token->Type = TokenNumber;
        }
        
        else
        {
            rune Next = (TextIndex + 1 < Text->Count) ? Text->Data[TextIndex + 1] : 0;
            
            switch(Character)
            {
                case '=':
                {
                    if(Next == '=')
                    {
                        Token->Type = TokenDoubleEqual;
                        TextIndex++;
                    }
                    else
                    {
                        Token->Type = (token_type)'=';
                    }
                }
                break;
                
                case '>':
                {
                    if(Next == '=')
                    {
                        Token->Type = TokenGreaterEqual;
                        TextIndex++;
                    }
                    else if(Next == '>')
                    {
                        Token->Type = TokenRightShift;
                        TextIndex++;
                    }
                    else
                    {
                        Token->Type = (token_type)'>';
                    }
                }
                break;
                
                case '<':
                {
                    if(Next == '=')
                    {
                        Token->Type = TokenLesserEqual;
                        TextIndex++;
                    }
                    else if(Next == '<')
                    {
                        Token->Type = TokenLeftShift;
                        TextIndex++;
                    }
                    else
                    {
                        Token->Type = (token_type)'<';
                    }
                }
                break;
                
                case '"':
                {
                    while(Text->Data[TextIndex + 1] != '"' && Text->Data[TextIndex + 1] != '\0')
                    {
                        ++TextIndex;
                        if(Text->Data[TextIndex + 1] == '\\')
                            
                            ++TextIndex;  
                    }
                    
                    TokenStart += 1;
                    Token->Type = TokenString;
                }
                break;
                
                default:
                {
                    Token->Type = (token_type)Character;
                }
                break;
            }
        }
        
        TokenEnd = TextIndex + 1;
        
        Token->Lexeme.Data = (u8 *)&Text->Data[TokenStart];
        Token->Lexeme.Size = (u64)(TokenEnd - TokenStart);
        Column += (s32)Token->Lexeme.Size;
        
        //Log("Token: \t%.lu*s\n", Token->Lexeme.Size, Token->Lexeme.Data);
        
        if(!Initialized)
        {
            Initialized   = 1;
            List->Root    = TokenNode;
            List->Current = TokenNode;
        }
        else
        {
            TokenNode->Previous = List->Current;
            List->Current->Next = TokenNode;
            List->Current       = TokenNode;
        }
    }
    
    return List;
}

NO_WARNINGS_END