Tree printing
This commit is contained in:
+55
-1
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user