Skip to content

Commit e848f00

Browse files
committed
Merge TemplateReader into TemplateParser.
1 parent 78277ef commit e848f00

File tree

5 files changed

+39
-96
lines changed

5 files changed

+39
-96
lines changed

mustang/TagParser.ooc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ PartialParser: class extends TagParser {
8787
parse: func(tag: String) -> TNode {
8888
//TODO: better template path lookups
8989
templateName := tag substring(1) trim()
90-
template := Template loadFromFile("%s.mustache" format(templateName))
90+
template := Template loadFromPath("%s.mustache" format(templateName))
9191

9292
PartialNode new(template)
9393
}

mustang/Template.ooc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
import io/[Writer, File]
2-
import mustang/[Context, TemplateParser, TemplateReader, Renderer]
2+
import mustang/[Context, TemplateParser, Renderer]
33

44
Template: class {
55
renderer: Renderer
66

7-
loadFromFile: static func(path: String) -> This {
8-
This new(TemplateReader getReaderFromFile(File new(path)))
7+
loadFromPath: static func(path: String) -> This {
8+
new(TemplateParser getParserFromFile(File new(path)))
99
}
1010

11-
loadFromString: static func(text: String) -> This {
12-
This new(TemplateReader new(text))
13-
}
14-
15-
init: func(reader: TemplateReader) { this(reader, "{{", "}}") }
16-
init: func ~customTags(reader: TemplateReader, startTag: String, endTag: String) {
17-
parser := TemplateParser new(reader, startTag, endTag)
11+
init: func(parser: TemplateParser) {
1812
rootNode := parser parse()
1913
renderer = Renderer new(rootNode)
2014
}

mustang/TemplateParser.ooc

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import mustang/[Node, TagParser]
2+
3+
import io/[File, FileReader]
14
import structs/[Stack, List, LinkedList]
2-
import mustang/[TemplateReader, Node, TagParser]
5+
36

47
/**
58
Parses the template text and returns a list of token Nodes.
@@ -15,15 +18,26 @@ import mustang/[TemplateReader, Node, TagParser]
1518
to the NodeList returned at the end of parsing.
1619
*/
1720
TemplateParser: class {
18-
template: TemplateReader
21+
templateText: String
22+
index: Int
23+
1924
startTag, endTag: String
2025
parsers: LinkedList<TagParser>
26+
2127
firstNode, currentNode: TNode
2228
nodeStack: Stack<TNode>
23-
currentBlockName: String
2429
running: Bool
2530

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
2741
parsers = LinkedList<TagParser> new()
2842
nodeStack = Stack<TNode> new()
2943

@@ -65,34 +79,38 @@ TemplateParser: class {
6579
}
6680

6781
parseText: func -> Bool {
68-
if(!template hasNext()) {
82+
if(index >= templateText length()) {
6983
// End of template, stop parsing
7084
return false
7185
}
7286

73-
start := template index()
74-
end := template skipUntil(startTag)
87+
// Find the start of the next tag
88+
indexOfNextTag := templateText indexOf(startTag, index)
7589

76-
if(end == -1) {
90+
if(indexOfNextTag == -1) {
7791
// No more tags to parse, rest of context is plaintext.
78-
text := template range(start)
92+
text := templateText substring(index)
7993
appendNode(TextNode new(text))
8094
return false
8195
}
82-
else if(end != start) {
83-
text := template range(start, end)
96+
else if(indexOfNextTag != index) {
97+
text := templateText substring(index, indexOfNextTag)
8498
appendNode(TextNode new(text))
8599
}
86100

101+
index = indexOfNextTag
102+
87103
return true
88104
}
89105

90106
parseTag: func -> Bool {
91107
// 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
96114

97115
// End of tag block
98116
if(tag first() == '/') {

mustang/TemplateReader.ooc

Lines changed: 0 additions & 69 deletions
This file was deleted.

mustang/mustang.ooc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ main: func(args: ArrayList<String>) -> Int {
1818
}
1919

2020
context := YAMLContext loadFromFile(yamlFile)
21-
template := Template loadFromFile(templateFile)
21+
template := Template loadFromPath(templateFile)
2222
template render(context) println()
2323

2424
return 0

0 commit comments

Comments
 (0)