Skip to content

Commit 11ebb1f

Browse files
committed
First stab at getting nodes to render.
1 parent 27540a9 commit 11ebb1f

File tree

6 files changed

+110
-18
lines changed

6 files changed

+110
-18
lines changed

mustang/Context.ooc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import structs/[HashMap, List]
2+
3+
Value: abstract class {
4+
typeName: abstract func -> String
5+
toString: abstract func -> String
6+
}
7+
8+
StringValue: class extends Value {
9+
value: String
10+
11+
init: func(=value) {}
12+
13+
typeName: func -> String { "String" }
14+
15+
toString: func -> String { value }
16+
}
17+
18+
ListValue: class <T> extends Value {
19+
list: List<T>
20+
21+
init: func(=list) {}
22+
23+
typeName: func -> String { "List" }
24+
toString: func -> String { "List size=%d" format(list size()) }
25+
26+
list: func -> List<T> { list }
27+
}
28+
29+
Context: abstract class {
30+
resolve: abstract func(name: String) -> Value
31+
}
32+
33+
MockContext: class extends Context {
34+
data: HashMap<Value>
35+
36+
init: func(=data) {}
37+
38+
resolve: func(name: String) -> Value {
39+
data[name]
40+
}
41+
}

mustang/Node.ooc

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1+
import io/Writer
2+
import mustang/Context
3+
14
/**
25
Base template node interface.
36
*/
47
TNode: abstract class {
58
next, firstChild: This
69

7-
compile: abstract func
10+
render: abstract func(context: Context, out: Writer)
11+
812
debug: abstract func -> String
913
}
1014

1115
/**
1216
Represents all the plain text in a template file.
1317
*/
1418
TextNode: class extends TNode {
15-
offset, length: Int
19+
text: String
1620

17-
init: func(=offset, =length) {}
21+
init: func(=text) {}
1822

19-
compile: func {}
23+
render: func(context: Context, out: Writer) {
24+
out write(text)
25+
}
2026

21-
debug: func -> String { "Text: offset=%d length=%d" format(offset, length) }
27+
debug: func -> String { "Text: '%s'" format(text) }
2228
}
2329

2430
VariableNode: class extends TNode {
2531
variableName: String
2632

2733
init: func(=variableName) {}
2834

29-
compile: func {}
35+
render: func(context: Context, out: Writer) {
36+
variable := context resolve(variableName)
37+
out write(variable toString())
38+
}
3039

3140
debug: func -> String { "Variable: name=%s" format(variableName) }
3241
}
@@ -36,7 +45,8 @@ SectionNode: class extends TNode {
3645

3746
init: func(=name) {}
3847

39-
compile: func {}
48+
render: func(context: Context, out: Writer) {
49+
}
4050

4151
debug: func -> String { "Section: name=%s" format(name) }
4252
}

mustang/Renderer.ooc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import io/Writer
2+
import text/Buffer
3+
import mustang/[Node, Walker, Context]
4+
5+
6+
Renderer: class extends NodeWalker {
7+
rootNode: TNode
8+
context: Context
9+
output: Writer
10+
11+
init: func(=rootNode, =context) {}
12+
13+
onNode: func(node: TNode) {
14+
node render(context, output)
15+
}
16+
17+
render: func(output: Writer) {
18+
this output = output
19+
walk(rootNode, false)
20+
}
21+
render: func ~toString -> String {
22+
buffer := Buffer new(1000)
23+
render(BufferWriter new(buffer))
24+
return buffer toString()
25+
}
26+
}

mustang/TemplateParser.ooc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,16 @@ TemplateParser: class {
7272

7373
start := template index()
7474
end := template skipUntil(startTag)
75+
7576
if(end == -1) {
7677
// No more tags to parse, rest of context is plaintext.
77-
length := template length() - start
78-
if(length) {
79-
appendNode(TextNode new(start, template length() - start))
80-
}
78+
text := template range(start)
79+
appendNode(TextNode new(text))
8180
return false
8281
}
83-
else {
84-
length := end - start
85-
if(length) {
86-
appendNode(TextNode new(start, end - start))
87-
}
82+
else if(end != start) {
83+
text := template range(start, end)
84+
appendNode(TextNode new(text))
8885
}
8986

9087
return true

mustang/TemplateReader.ooc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ TemplateReader: class {
3030
return c
3131
}
3232

33+
range: func(start: Int, end: Int) -> String {
34+
content substring(start, end)
35+
}
36+
range: func ~untilEnd(start: Int) -> String {
37+
content substring(start)
38+
}
39+
3340
index: func -> Int { index }
3441

3542
length: func -> Int { content length() }

mustang/Walker.ooc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import mustang/Node
33
NodeWalker: abstract class {
44
root: TNode
55
level: Int
6+
visitChildren: Bool
67

78
onNode: abstract func(node: TNode)
89

9-
walk: func(root: TNode) {
10+
walk: func(root: TNode, visitChildren: Bool) {
1011
level = -1
12+
this visitChildren = visitChildren
13+
1114
visit(root)
1215
}
1316

@@ -17,7 +20,7 @@ NodeWalker: abstract class {
1720

1821
while(c) {
1922
onNode(c)
20-
if(c firstChild) visit(c firstChild)
23+
if(visitChildren && c firstChild) visit(c firstChild)
2124
c = c next
2225
}
2326

@@ -26,6 +29,14 @@ NodeWalker: abstract class {
2629
}
2730

2831
NodePrinter: class extends NodeWalker {
32+
rootNode: TNode
33+
34+
init: func(=rootNode) {}
35+
36+
print: func {
37+
walk(rootNode, true)
38+
}
39+
2940
onNode: func(node: TNode) {
3041
"%s%s" format("--> " times(level), node debug()) println()
3142
}

0 commit comments

Comments
 (0)