Root Token
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
TypoML
|
TypoML
|
||||||
|
src.bin
|
||||||
|
|||||||
+4
-8
@@ -10,11 +10,13 @@ read_stdin :: proc() -> []string {
|
|||||||
return strings.split_lines(content)
|
return strings.split_lines(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
print_token :: proc(token: Token, depth: int) {
|
print_token :: proc(token: Token, depth: int = 0) {
|
||||||
indent := strings.repeat(" ", depth, context.temp_allocator)
|
indent := strings.repeat(" ", depth, context.temp_allocator)
|
||||||
|
|
||||||
type_str := ""
|
type_str := ""
|
||||||
switch token.type {
|
switch token.type {
|
||||||
|
case TokenType.Root:
|
||||||
|
type_str = "Root"
|
||||||
case TokenType.Text:
|
case TokenType.Text:
|
||||||
type_str = "Text"
|
type_str = "Text"
|
||||||
case TokenType.Italics:
|
case TokenType.Italics:
|
||||||
@@ -49,14 +51,8 @@ print_token :: proc(token: Token, depth: int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
print_token_tree :: proc(tokens: [dynamic]Token) {
|
|
||||||
for token in tokens {
|
|
||||||
print_token(token, 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
lines := read_stdin()
|
lines := read_stdin()
|
||||||
parsed := parse(lines)
|
parsed := parse(lines)
|
||||||
print_token_tree(parsed)
|
print_token(parsed)
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-11
@@ -10,6 +10,7 @@ TokenType :: enum {
|
|||||||
BreakLine,
|
BreakLine,
|
||||||
Heading,
|
Heading,
|
||||||
Paragraph,
|
Paragraph,
|
||||||
|
Root,
|
||||||
}
|
}
|
||||||
|
|
||||||
TokenValue :: union {
|
TokenValue :: union {
|
||||||
@@ -21,10 +22,10 @@ Token :: struct {
|
|||||||
type: TokenType,
|
type: TokenType,
|
||||||
value: TokenValue,
|
value: TokenValue,
|
||||||
children: [dynamic]Token,
|
children: [dynamic]Token,
|
||||||
|
parent: ^Token,
|
||||||
}
|
}
|
||||||
|
|
||||||
token_tree := [dynamic]Token{}
|
current_token: ^Token
|
||||||
current_token := &token_tree[0]
|
|
||||||
|
|
||||||
replace_text :: proc(text: string, start: int, len_: int, repl: string) -> string {
|
replace_text :: proc(text: string, start: int, len_: int, repl: string) -> string {
|
||||||
builder := strings.builder_make(context.temp_allocator)
|
builder := strings.builder_make(context.temp_allocator)
|
||||||
@@ -49,20 +50,26 @@ parse_header :: proc(line: string) -> (int, string) {
|
|||||||
return lvl, line[lvl + 1:]
|
return lvl, line[lvl + 1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
parse :: proc(lines: []string, allocator := context.allocator) -> [dynamic]Token {
|
parse :: proc(lines: []string, allocator := context.allocator) -> Token {
|
||||||
token_tree := [dynamic]Token{}
|
root := Token{TokenType.Root, nil, nil, nil}
|
||||||
append(&token_tree, Token{TokenType.Paragraph, nil, nil})
|
current_token := &root
|
||||||
current_token := &token_tree[0]
|
append(¤t_token.children, Token{TokenType.Paragraph, nil, nil, current_token})
|
||||||
|
current_token = ¤t_token.children[0]
|
||||||
|
|
||||||
for untrimmed_line in lines {
|
for untrimmed_line, line_index in lines {
|
||||||
line := strings.trim_space(untrimmed_line)
|
line := strings.trim_space(untrimmed_line)
|
||||||
|
|
||||||
if len(line) == 0 {
|
if len(line) == 0 && line_index != len(lines) - 1 {
|
||||||
append(¤t_token.children, Token{TokenType.BreakLine, nil, nil})
|
if current_token.parent != nil {
|
||||||
|
current_token = current_token.parent
|
||||||
|
}
|
||||||
|
|
||||||
|
append(¤t_token.children, Token{TokenType.Paragraph, nil, nil, current_token})
|
||||||
|
current_token = ¤t_token.children[len(current_token.children) - 1]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
append(¤t_token.children, Token{TokenType.Text, line, nil})
|
append(¤t_token.children, Token{TokenType.Text, line, nil, current_token})
|
||||||
|
|
||||||
// lvl, text := parse_header(trimmed)
|
// lvl, text := parse_header(trimmed)
|
||||||
// is_header := lvl > 0
|
// is_header := lvl > 0
|
||||||
@@ -79,5 +86,5 @@ parse :: proc(lines: []string, allocator := context.allocator) -> [dynamic]Token
|
|||||||
// strings.write_string(&builder, "\n")
|
// strings.write_string(&builder, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
return token_tree
|
return root
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user