Tree printing

This commit is contained in:
2026-07-13 14:34:14 +02:00
parent 36e95f2568
commit 4f3226b0b7
3 changed files with 69 additions and 6 deletions
+13 -4
View File
@@ -9,11 +9,13 @@ TokenType :: enum {
Bold,
BreakLine,
Heading,
Paragraph,
}
TokenMode :: enum {
Start,
End,
Modeless,
}
TokenValue :: union {
@@ -23,12 +25,13 @@ TokenValue :: union {
}
Token :: struct {
type: TokenType,
value: TokenValue,
child: [dynamic]Token,
type: TokenType,
value: TokenValue,
children: [dynamic]Token,
}
token_tree := [dynamic]Token{}
current_token := &token_tree[0]
replace_text :: proc(text: string, start: int, len_: int, repl: string) -> string {
builder := strings.builder_make(context.temp_allocator)
@@ -54,14 +57,20 @@ parse_header :: proc(line: string) -> (int, string) {
}
parse :: proc(lines: []string, allocator := context.allocator) -> [dynamic]Token {
token_tree := [dynamic]Token{}
append(&token_tree, Token{TokenType.Paragraph, TokenMode.Start, nil})
current_token := &token_tree[0]
for untrimmed_line in lines {
line := strings.trim_space(untrimmed_line)
if len(line) == 0 {
append(&token_tree, Token{TokenType.BreakLine, TokenMode.Start, nil})
append(&current_token.children, Token{TokenType.BreakLine, TokenMode.Modeless, nil})
continue
}
append(&current_token.children, Token{TokenType.Text, line, nil})
// lvl, text := parse_header(trimmed)
// is_header := lvl > 0