summaryrefslogtreecommitdiff
path: root/src/tparse/tree.go
blob: 417580a500f400c246159b550d34c6fdddd462e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package tparse

// Node represents a group of nodes or a directive
type Node struct {
	SubNodes []Node

	Dir Directive
}

// Directive represents a block or single directive
type Directive struct {
	Type string
	Data string

	Param Paramaters
}

// Paramaters represents a set of paramaters for a directive
type Paramaters struct {
	In  []string
	Out []string
}

func handleCode(tokens *[]Token, start int) (Node, int) {
	out := Node{}

	return out, start
}

func handleBlock(tokens *[]Token, start int) (Node, int) {
	var out Node
	var tmp Node

	l := len(*tokens)

	if start >= l {
		panic((*tokens)[l-1])
	}

	for ; start < l; start++ {
		t := (*tokens)[start]
		switch t.Type {
		case LINESEP:
			if t.Data == ";" {
				tmp, start = handleCode(tokens, start+1)
			}
			break
		case DELIMIT:
			if t.Data == "/;" {
				tmp, start = handleCode(tokens, start+1)
			}
			break
		default:
			panic(t)
		}
		out.SubNodes = append(out.SubNodes, tmp)
	}

	return out, start
}

func handlePre(tokens *[]Token, start int) (Node, int) {
	out := Node{}

	return out, start
}

// CreateTree takes a series of tokens and converts them into an AST
func CreateTree(tokens *[]Token, start int) Node {
	out := Node{}
	out.Dir = Directive{Type: "root"}

	var tmp Node

	for i, t := range *tokens {
		switch t.Type {
		case LINESEP:
			if t.Data == ";" {
				tmp, i = handleCode(tokens, i)
			} else if t.Data == ":" {
				tmp, i = handlePre(tokens, i)
			}
			break
		case DELIMIT:
			if t.Data == "/;" {
				tmp, i = handleCode(tokens, i)
			} else if t.Data == "/:" {
				tmp, i = handlePre(tokens, i)
			}
			break
		}
		out.SubNodes = append(out.SubNodes, tmp)
	}

	return out
}