more work on syntax highlighting

This commit is contained in:
Matthew 2025-08-22 07:08:51 +10:00
parent f578883908
commit 464c0eb967
2 changed files with 35 additions and 17 deletions

View File

@ -489,20 +489,24 @@ Tokenize(FlatBuffer* fb)
Reset(&tk.arena); Reset(&tk.arena);
tk.tokens = AllocArray!(Token)(&tk.arena, fb.length); tk.tokens = AllocArray!(Token)(&tk.arena, fb.length);
tk.tk_count = 0; tk.tk_count = 1;
bool _macro = false; bool _macro = false;
Token* prev = null; bool id_of_line = false;
Token* prev = tk.tokens.ptr;
while (true) while (true)
{ {
scope(exit) tk.tk_count += 1;
Token* t = NextToken(fb); Token* t = NextToken(fb);
if (t == null) break; if (t == null) break;
switch(t.type) switch(t.type)
{ {
case TT.Identifier:
case TT.Keyword: case TT.Keyword:
{ {
tk.buffer[t.start .. t.end] = TS.Keyword; TokenStyle ts = t.type == TT.Keyword ? TS.Keyword : TS.Identifier;
tk.buffer[t.start .. t.end] = ts;
} break; } break;
case TT.Number: case TT.Number:
{ {
@ -521,15 +525,11 @@ Tokenize(FlatBuffer* fb)
{ {
tk.buffer[t.start .. t.end] = TS.Char; tk.buffer[t.start .. t.end] = TS.Char;
} break; } break;
case TT.Identifier:
{
tk.buffer[t.start .. t.end] = TS.Identifier;
} break;
case TT.LeftBracket: case TT.LeftBracket:
{ {
if (prev != null && _macro && prev.type == TT.Exclamation) if (prev != null && _macro && prev.type != TT.Exclamation)
{ {
_macro = false;
} }
} goto case TT.Comma; } goto case TT.Comma;
case TT.LeftBrace: case TT.LeftBrace:
@ -538,7 +538,7 @@ Tokenize(FlatBuffer* fb)
{ {
if (prev.type == TT.RightBracket) if (prev.type == TT.RightBracket)
{ {
i64 i = tk.tk_count - 3; i64 i = tk.tk_count - 2;
Token* pt = null; Token* pt = null;
bool is_macro = false; bool is_macro = false;
i64 brackets = 1; i64 brackets = 1;
@ -559,17 +559,24 @@ Tokenize(FlatBuffer* fb)
{ {
brackets += 1; brackets += 1;
is_macro = true; is_macro = true;
break; continue;
} }
if (pt.type == TT.Identifier && brackets == 0) if (pt.type == TT.Identifier && brackets == 0)
{ {
if (i >= 0)
{
Token* tmp = tk.tokens.ptr + i;
tmp.type = TT.Type;
tk.buffer[tmp.start .. tmp.end] = TS.Type;
}
pt.type = TT.Function; pt.type = TT.Function;
tk.buffer[pt.start .. pt.end] = TS.Function; tk.buffer[pt.start .. pt.end] = TS.Function;
brackets = 0; brackets = 0;
Token* ppt = null; Token* ppt = null;
while (i < tk.tk_count - 2) while (i < tk.tk_count - 1)
{ {
ppt = pt; ppt = pt;
i += 1; i += 1;
@ -587,7 +594,7 @@ Tokenize(FlatBuffer* fb)
continue; continue;
} }
if (!is_macro && brackets < 2 && id_keyword && (ppt.type == TT.LeftBracket || ppt.type == TT.Comma)) if (((!is_macro && brackets < 2) || (is_macro && brackets == 3)) && id_keyword && (ppt.type == TT.LeftBracket || ppt.type == TT.Comma))
{ {
pt.type = TT.Type; pt.type = TT.Type;
tk.buffer[pt.start .. pt.end] = TS.Type; tk.buffer[pt.start .. pt.end] = TS.Type;
@ -608,11 +615,15 @@ Tokenize(FlatBuffer* fb)
} }
} goto case TT.Comma; } goto case TT.Comma;
case TT.Semicolon: case TT.Semicolon:
{
id_of_line = false;
} goto case TT.Comma;
case TT.RightBracket: case TT.RightBracket:
case TT.RightBrace: case TT.RightBrace:
case TT.LeftSquareBrace: case TT.LeftSquareBrace:
case TT.RightSquareBrace: case TT.RightSquareBrace:
case TT.Colon: case TT.Colon:
case TT.Equals:
case TT.Comma: case TT.Comma:
{ {
tk.buffer[t.start .. t.end] = TS.Bracket; tk.buffer[t.start .. t.end] = TS.Bracket;
@ -634,7 +645,6 @@ Tokenize(FlatBuffer* fb)
case TT.Plus: case TT.Plus:
case TT.Minus: case TT.Minus:
case TT.Slash: case TT.Slash:
case TT.Equals:
{ {
tk.buffer[t.start .. t.end] = TS.Op; tk.buffer[t.start .. t.end] = TS.Op;
} break; } break;
@ -646,6 +656,13 @@ Tokenize(FlatBuffer* fb)
} }
} }
Token*
PrevToken(FlatBuffer* fb, i64 prev)
{
i64 c = cast(i64)(fb.tk.tk_count);
return (c-prev) > 0 ? fb.tk.tokens.ptr + (c-prev) : fb.tk.tokens.ptr + 0;
}
Token* Token*
NextToken(FlatBuffer* fb) NextToken(FlatBuffer* fb)
{ {
@ -826,8 +843,6 @@ NextToken(FlatBuffer* fb)
tk.pos += 1; tk.pos += 1;
} }
} }
tk.tk_count += 1;
} }
return t; return t;

View File

@ -40,7 +40,7 @@ Test()
int hex = 0x555; int hex = 0x555;
int hex2 = 0X555; int hex2 = 0X555;
int hexp = p+0X5555; int hexp = p+0X5555-;
hexp = p-0x555; hexp = p-0x555;
string str_literal = r"Teststring"; string str_literal = r"Teststring";
@ -55,6 +55,9 @@ Test()
int value = WithParameters(z, str); int value = WithParameters(z, str);
int h = MacroFunc!(int)(x);
int g = MacroFunc!int(x);
return false; return false;
} }