File tree Expand file tree Collapse file tree 6 files changed +110
-18
lines changed Expand file tree Collapse file tree 6 files changed +110
-18
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ import io/Writer
2
+ import mustang/Context
3
+
1
4
/* *
2
5
Base template node interface.
3
6
*/
4
7
TNode : abstract class {
5
8
next , firstChild : This
6
9
7
- compile : abstract func
10
+ render : abstract func (context : Context , out : Writer )
11
+
8
12
debug : abstract func -> String
9
13
}
10
14
11
15
/* *
12
16
Represents all the plain text in a template file.
13
17
*/
14
18
TextNode : class extends TNode {
15
- offset , length : Int
19
+ text : String
16
20
17
- init : func (= offset , = length ) {}
21
+ init : func (= text ) {}
18
22
19
- compile : func {}
23
+ render : func (context : Context , out : Writer ) {
24
+ out write (text)
25
+ }
20
26
21
- debug : func -> String { "Text: offset=%d length=%d " format (offset, length ) }
27
+ debug : func -> String { "Text: '%s' " format (text ) }
22
28
}
23
29
24
30
VariableNode : class extends TNode {
25
31
variableName : String
26
32
27
33
init : func (= variableName ) {}
28
34
29
- compile : func {}
35
+ render : func (context : Context , out : Writer ) {
36
+ variable := context resolve (variableName)
37
+ out write (variable toString ())
38
+ }
30
39
31
40
debug : func -> String { "Variable: name=%s" format (variableName) }
32
41
}
@@ -36,7 +45,8 @@ SectionNode: class extends TNode {
36
45
37
46
init : func (= name ) {}
38
47
39
- compile : func {}
48
+ render : func (context : Context , out : Writer ) {
49
+ }
40
50
41
51
debug : func -> String { "Section: name=%s" format (name) }
42
52
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -72,19 +72,16 @@ TemplateParser: class {
72
72
73
73
start := template index ()
74
74
end := template skipUntil (startTag)
75
+
75
76
if (end == - 1 ) {
76
77
// 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))
81
80
return false
82
81
}
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))
88
85
}
89
86
90
87
return true
Original file line number Diff line number Diff line change @@ -30,6 +30,13 @@ TemplateReader: class {
30
30
return c
31
31
}
32
32
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
+
33
40
index : func -> Int { index }
34
41
35
42
length : func -> Int { content length () }
Original file line number Diff line number Diff line change @@ -3,11 +3,14 @@ import mustang/Node
3
3
NodeWalker : abstract class {
4
4
root : TNode
5
5
level : Int
6
+ visitChildren : Bool
6
7
7
8
onNode : abstract func (node : TNode )
8
9
9
- walk : func (root : TNode ) {
10
+ walk : func (root : TNode , visitChildren : Bool ) {
10
11
level = - 1
12
+ this visitChildren = visitChildren
13
+
11
14
visit (root)
12
15
}
13
16
@@ -17,7 +20,7 @@ NodeWalker: abstract class {
17
20
18
21
while (c) {
19
22
onNode (c)
20
- if (c firstChild) visit (c firstChild)
23
+ if (visitChildren && c firstChild) visit (c firstChild)
21
24
c = c next
22
25
}
23
26
@@ -26,6 +29,14 @@ NodeWalker: abstract class {
26
29
}
27
30
28
31
NodePrinter : class extends NodeWalker {
32
+ rootNode : TNode
33
+
34
+ init : func (= rootNode ) {}
35
+
36
+ print : func {
37
+ walk (rootNode, true )
38
+ }
39
+
29
40
onNode : func (node : TNode ) {
30
41
"%s%s" format ("--> " times (level), node debug ()) println ()
31
42
}
You can’t perform that action at this time.
0 commit comments