From 4f3226b0b7b135df123ff1641b5889236ff9842f Mon Sep 17 00:00:00 2001 From: "N0\\A" Date: Mon, 13 Jul 2026 14:34:14 +0200 Subject: [PATCH] Tree printing --- src/main.odin | 56 ++++++++++++++++++++++++++++++++++++++++++++++++- src/parser.odin | 17 +++++++++++---- test | 2 +- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/main.odin b/src/main.odin index edf2a20..e4c4928 100644 --- a/src/main.odin +++ b/src/main.odin @@ -10,8 +10,62 @@ read_stdin :: proc() -> []string { return strings.split_lines(content) } +print_token :: proc(token: Token, depth: int) { + indent := strings.repeat(" ", depth, context.temp_allocator) + + type_str := "" + switch token.type { + case TokenType.Text: + type_str = "Text" + case TokenType.Italics: + type_str = "Italics" + case TokenType.Bold: + type_str = "Bold" + case TokenType.BreakLine: + type_str = "BreakLine" + case TokenType.Heading: + type_str = "Heading" + case TokenType.Paragraph: + type_str = "Paragraph" + } + + value_str := "" + switch v in token.value { + case string: + value_str = v + case int: + value_str = fmt.aprintf("%d", v, allocator = context.temp_allocator) + case TokenMode: + switch v { + case TokenMode.Start: + value_str = "Start" + case TokenMode.End: + value_str = "End" + case TokenMode.Modeless: + value_str = "Modeless" + } + } + + if value_str != "" { + fmt.printf("%s%s(%s)", indent, type_str, value_str) + } else { + fmt.printf("%s%s", indent, type_str) + } + fmt.println() + + for child in token.children { + print_token(child, depth + 1) + } +} + +print_token_tree :: proc(tokens: [dynamic]Token) { + for token in tokens { + print_token(token, 0) + } +} + main :: proc() { lines := read_stdin() parsed := parse(lines) - fmt.println(parsed) + print_token_tree(parsed) } diff --git a/src/parser.odin b/src/parser.odin index 1713e05..52b4414 100644 --- a/src/parser.odin +++ b/src/parser.odin @@ -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(¤t_token.children, Token{TokenType.BreakLine, TokenMode.Modeless, nil}) continue } + append(¤t_token.children, Token{TokenType.Text, line, nil}) + // lvl, text := parse_header(trimmed) // is_header := lvl > 0 diff --git a/test b/test index 0f4ed1c..c36a7d9 100755 --- a/test +++ b/test @@ -1 +1 @@ -clear && make format && cat README.tlm | make run | ./preview +clear && make format && cat README.tlm | make run # | ./preview