拆分token结果

import Scanner from './scaner'
export default function (tpStr) {
const tokens = []
const ScannerInit = new Scanner(tpStr)
let words;
while (!ScannerInit.eos()) {
words = ScannerInit.scanUtil('{{')
if (words !== '') {
tokens.push(['text', words])
}
ScannerInit.scan('{{')
words = ScannerInit.scanUtil('}}')
if (words !== '') {
if (words[0] === '#') {
tokens.push(['#', words.substring(1)])
} else if (words[0] === '/') {
tokens.push(['/', words.substring(1)])
} else {
tokens.push(['text', words])
}
}
ScannerInit.scan('}}')
}
console.log(tokens)
}
export default class Scanner {
constructor(tpStr) {
console.log('render函数里 调用Scanner')
this.tpStr = tpStr
this.pos = 0
this.tail = tpStr
}
scan(tag) {
if (this.tail.indexOf(tag) === 0) {
this.pos += tag.length
this.tail = this.tpStr.substring(this.pos)
}
}
scanUtil(stopTag) {
const pos_backup = this.pos
while (this.tail.indexOf(stopTag) !== 0 && !this.eos()) {
this.pos++
this.tail = this.tpStr.substr(this.pos)
}
return this.tpStr.substring(pos_backup, this.pos)
}
eos() {
return this.pos >= this.tpStr.length
}
}
import parseTp2Tokens from './parseTp2Tokens'
window.TpEngine = {
render(tpStr, data) {
console.log('render函数被调用,命令Scanner工作')
parseTp2Tokens(tpStr)
}
}
const tmpStr = `<div>
<ol>
{{#students}}
<li>
学生{{item.name}}的爱好<ol>
{{#item.hobbies}}
<li>
{{.}}
</li>
{{/item.hobbies}}
</ol>
</li>
{{/students}}
</ol>
</div>`
TpEngine.render(tmpStr)