From 464c0eb967626987d9eb0de0e06c68d8bf1890c6 Mon Sep 17 00:00:00 2001 From: Matthew Date: Fri, 22 Aug 2025 07:08:51 +1000 Subject: [PATCH] more work on syntax highlighting --- src/editor/buffer.d | 47 +++++++++++++++++++++----------- test/files/syntax_highlighting.d | 5 +++- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/editor/buffer.d b/src/editor/buffer.d index 59dfec2..b412b32 100644 --- a/src/editor/buffer.d +++ b/src/editor/buffer.d @@ -489,20 +489,24 @@ Tokenize(FlatBuffer* fb) Reset(&tk.arena); tk.tokens = AllocArray!(Token)(&tk.arena, fb.length); - tk.tk_count = 0; + tk.tk_count = 1; bool _macro = false; - Token* prev = null; + bool id_of_line = false; + Token* prev = tk.tokens.ptr; while (true) { + scope(exit) tk.tk_count += 1; Token* t = NextToken(fb); if (t == null) break; switch(t.type) { + case TT.Identifier: 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; case TT.Number: { @@ -521,15 +525,11 @@ Tokenize(FlatBuffer* fb) { tk.buffer[t.start .. t.end] = TS.Char; } break; - case TT.Identifier: - { - tk.buffer[t.start .. t.end] = TS.Identifier; - } break; 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; case TT.LeftBrace: @@ -538,7 +538,7 @@ Tokenize(FlatBuffer* fb) { if (prev.type == TT.RightBracket) { - i64 i = tk.tk_count - 3; + i64 i = tk.tk_count - 2; Token* pt = null; bool is_macro = false; i64 brackets = 1; @@ -559,17 +559,24 @@ Tokenize(FlatBuffer* fb) { brackets += 1; is_macro = true; - break; + continue; } 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; tk.buffer[pt.start .. pt.end] = TS.Function; brackets = 0; Token* ppt = null; - while (i < tk.tk_count - 2) + while (i < tk.tk_count - 1) { ppt = pt; i += 1; @@ -587,7 +594,7 @@ Tokenize(FlatBuffer* fb) 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; tk.buffer[pt.start .. pt.end] = TS.Type; @@ -608,11 +615,15 @@ Tokenize(FlatBuffer* fb) } } goto case TT.Comma; case TT.Semicolon: + { + id_of_line = false; + } goto case TT.Comma; case TT.RightBracket: case TT.RightBrace: case TT.LeftSquareBrace: case TT.RightSquareBrace: case TT.Colon: + case TT.Equals: case TT.Comma: { tk.buffer[t.start .. t.end] = TS.Bracket; @@ -634,7 +645,6 @@ Tokenize(FlatBuffer* fb) case TT.Plus: case TT.Minus: case TT.Slash: - case TT.Equals: { tk.buffer[t.start .. t.end] = TS.Op; } 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* NextToken(FlatBuffer* fb) { @@ -826,8 +843,6 @@ NextToken(FlatBuffer* fb) tk.pos += 1; } } - - tk.tk_count += 1; } return t; diff --git a/test/files/syntax_highlighting.d b/test/files/syntax_highlighting.d index d8a553e..1c96f36 100644 --- a/test/files/syntax_highlighting.d +++ b/test/files/syntax_highlighting.d @@ -40,7 +40,7 @@ Test() int hex = 0x555; int hex2 = 0X555; - int hexp = p+0X5555; + int hexp = p+0X5555-; hexp = p-0x555; string str_literal = r"Teststring"; @@ -55,6 +55,9 @@ Test() int value = WithParameters(z, str); + int h = MacroFunc!(int)(x); + int g = MacroFunc!int(x); + return false; }