From 4f03c5bf9a4c31e46750cae6769494cfb82e6080 Mon Sep 17 00:00:00 2001 From: "N0\\A" Date: Tue, 14 Jul 2026 14:31:37 +0200 Subject: [PATCH] Ordered lists --- src/parser.odin | 58 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/src/parser.odin b/src/parser.odin index 628b0d8..a422e45 100644 --- a/src/parser.odin +++ b/src/parser.odin @@ -1,5 +1,6 @@ package main +import "core:strconv" import "core:strings" TokenType :: enum { @@ -58,12 +59,8 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token { line := strings.trim_space(untrimmed_line) if len(line) == 0 { - if line_index != len(lines) - 1 { - current_token = push_token(&root, TokenType.Paragraph, nil) - continue - } else { - continue - } + current_token = &root + continue } header_level, text := parse_header(line) @@ -77,8 +74,10 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token { if strings.has_prefix(text, "> ") { if current_token.type != TokenType.Blockquote { current_token = push_token(&root, TokenType.Blockquote, nil) + push_token(current_token, TokenType.Paragraph, nil) } - push_token(current_token, TokenType.Text, text[2:]) + last_p := current_token.children[len(current_token.children) - 1] + push_token(last_p, TokenType.Text, text[2:]) continue } @@ -86,24 +85,45 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token { if current_token.type != TokenType.UnorderedList { current_token = push_token(&root, TokenType.UnorderedList, nil) } - current_token = push_token(current_token, TokenType.ListItem, nil) - push_token(current_token, TokenType.Text, text[2:]) - current_token = current_token.parent + li := push_token(current_token, TokenType.ListItem, nil) + push_token(li, TokenType.Text, text[2:]) continue } + if len(text) >= 3 { + item := 1 + idx := 0 + for idx < len(text) && text[idx] >= '0' && text[idx] <= '9' { + idx += 1 + } + + if idx > 0 && idx + 1 < len(text) && text[idx] == '.' && text[idx + 1] == ' ' { + item, _ = strconv.parse_int(text[:idx]) + text = text[idx + 2:] + + if current_token.type != TokenType.OrderedList { + current_token = push_token(&root, TokenType.OrderedList, item) + } + li := push_token(current_token, TokenType.ListItem, nil) + push_token(li, TokenType.Text, text) + continue + } + } + // LAST-CASE SCENARIO: NORMAL TEXT - if current_token.parent == nil { - current_token = push_token(&root, TokenType.Paragraph, nil) + if current_token.type == .Blockquote { + last_p := current_token.children[len(current_token.children) - 1] + push_token(last_p, TokenType.Text, line) + } else if current_token.type == .UnorderedList || current_token.type == .OrderedList { + last_li := current_token.children[len(current_token.children) - 1] + push_token(last_li, TokenType.Text, line) + } else { + if current_token.type != .Paragraph { + current_token = push_token(&root, TokenType.Paragraph, nil) + } + push_token(current_token, TokenType.Text, line) } - - #partial switch current_token.parent.type { - case TokenType.Blockquote: - current_token = current_token.parent - } - - push_token(current_token, TokenType.Text, line) } return root