1
+ import mustang/[Node, TagParser]
2
+
3
+ import io/[File, FileReader]
1
4
import structs/[Stack, List, LinkedList]
2
- import mustang/[TemplateReader, Node, TagParser]
5
+
3
6
4
7
/* *
5
8
Parses the template text and returns a list of token Nodes.
@@ -15,15 +18,26 @@ import mustang/[TemplateReader, Node, TagParser]
15
18
to the NodeList returned at the end of parsing.
16
19
*/
17
20
TemplateParser : class {
18
- template : TemplateReader
21
+ templateText : String
22
+ index : Int
23
+
19
24
startTag , endTag : String
20
25
parsers : LinkedList <TagParser >
26
+
21
27
firstNode , currentNode : TNode
22
28
nodeStack : Stack <TNode >
23
- currentBlockName : String
24
29
running : Bool
25
30
26
- init : func (= template , = startTag , = endTag ) {
31
+ getParserFromFile : static func (file : File ) -> This {
32
+ size := file size ()
33
+ buffer : String = gc_malloc (size + 1 )
34
+ FileReader new (file) read (buffer, 0 , size)
35
+ buffer[size] = '\0 ' ;
36
+ return new (buffer, "{{" , "}}" ) // TODO: un-hardcode
37
+ }
38
+
39
+ init : func (= templateText , = startTag , = endTag ) {
40
+ index = 0
27
41
parsers = LinkedList< TagParser> new ()
28
42
nodeStack = Stack< TNode> new ()
29
43
@@ -65,34 +79,38 @@ TemplateParser: class {
65
79
}
66
80
67
81
parseText : func -> Bool {
68
- if (! template hasNext ()) {
82
+ if (index >= templateText length ()) {
69
83
// End of template, stop parsing
70
84
return false
71
85
}
72
86
73
- start := template index ()
74
- end := template skipUntil (startTag)
87
+ // Find the start of the next tag
88
+ indexOfNextTag := templateText indexOf (startTag, index )
75
89
76
- if (end == - 1 ) {
90
+ if (indexOfNextTag == - 1 ) {
77
91
// No more tags to parse, rest of context is plaintext.
78
- text := template range (start )
92
+ text := templateText substring (index )
79
93
appendNode (TextNode new (text))
80
94
return false
81
95
}
82
- else if (end ! = start ) {
83
- text := template range (start, end )
96
+ else if (indexOfNextTag ! = index ) {
97
+ text := templateText substring (index, indexOfNextTag )
84
98
appendNode (TextNode new (text))
85
99
}
86
100
101
+ index = indexOfNextTag
102
+
87
103
return true
88
104
}
89
105
90
106
parseTag : func -> Bool {
91
107
// Read the tag from the template
92
- template skip (startTag length ()) // opening mustaches {{
93
- tag := template readUntil (endTag) // tag body
94
- tag = tag trim ()
95
- template skip (endTag length ()) // closing mustaches }}
108
+ // TODO: probably best we do this in a regex.
109
+ // as of now triple mustaches don't parse right.
110
+ index += startTag length () // skip open tag
111
+ indexOfEndTag := templateText indexOf (endTag, index) // get index of end tag
112
+ tag := templateText substring (index, indexOfEndTag) trim () // substring out the tag content
113
+ index = indexOfEndTag + endTag length () // advance past end tag
96
114
97
115
// End of tag block
98
116
if (tag first () == '/' ) {
0 commit comments