-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtm-parser.js
More file actions
130 lines (127 loc) · 3.2 KB
/
Copy pathtm-parser.js
File metadata and controls
130 lines (127 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var tree = [],
debug = function (msg) {
console.log('DEBUG: ' + msg)
},
error = function (errorMessage) {
console.log('ERROR: ' + errorMessage)
throw errorMessage
}
function parse(input) {
var state = 'whitespace', /* comment, token, whitespace, string */
i = 0,
c,
len = input.length,
prev = null,
next = null,
stack = [],
isInsideString = false,
isInsideMLString = false,
lineIndent = '',
insideLineIndent = false,
multilineIndent = '',
token = null;
tree.splice(0, tree.length)
stack.push(tree)
function invalidSyntax() {
error('Syntax error at ' + input.substring(0, i))
}
function inWhiteSpace() {
if(!isInsideString) {
endToken();
(state != 'comment') ? state = 'whitespace' : null
} else {
appendToToken(c)
}
}
function startList() {
if(state == 'comment') return
endToken()
var newNode = []
if(stack.length<1) invalidSyntax()
stack[stack.length-1].push(newNode)
stack.push(newNode)
}
function endList() {
if(state == 'comment') return;
endToken()
stack.pop()
}
function appendToToken(c) {
if(state == 'whitespace') {
token = []
state = 'token'
}
if(state == 'token' && c) token.push(c)
}
function endToken() {
if(state == 'token') {
stack[stack.length-1].push(token.join(''))
token = []
state = 'whitespace'
isInsideString = false
isInsideMLString = false
}
}
function startComment() {
endToken()
state = 'comment'
}
function endComment() {
state = 'whitespace'
}
function inString(c) {
if(state == 'token') endToken(); else {
isInsideString = true
appendToToken()
}
}
function startMultiLineString(c) {
endToken()
isInsideMLString = true
multilineIndent = lineIndent
}
function stopMultiLineString(c) {
endToken()
isInsideMLString = false
// Fix indents
var multilineToken = stack[stack.length-1][stack[stack.length-1].length-1].split('\n').map(function (line) {
if(line.indexOf(multilineIndent) == 0) {
return line.substring(multilineIndent.length)
} else {
return line
}
})
stack[stack.length-1][stack[stack.length-1].length-1] = multilineToken.join('\n')
}
for(; i<len; i++) {
c = input[i];
next = (i<len-1) ? input[i+1] : null;
(c == '>' && prev == '%') ? stopMultiLineString(c) :
(isInsideMLString && (c == '(' || c == ')' || c == '\\' || c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\'' || c == '"')) ? appendToToken(c) :
(c == '*' && prev == '/') ? startComment() :
(c == '/' && prev == '*') ? endComment() :
(c == ' ' || c == '\t' || c == '\n' || c == '\r') ? inWhiteSpace(c) :
(c == '(' && prev != '\\') ? startList() :
(c == ')' && prev != '\\') ? endList() :
(c == '\'' && prev != '\\') ? inString(c) :
(c == '%' && prev == '<') ? startMultiLineString(c) :
(c == '/' && next == '*') ? null :
(c == '*' && next == '/') ? null :
(c == '\\' && prev != '\\') ? null :
(c == '<' && next == '%') ? null :
(c == '%' && next == '>') ? null :
appendToToken(c);
if(c == '\n') {
lineIndent = ''
insideLineIndent = true
} else if(insideLineIndent && c == ' ' || c == '\t') {
lineIndent += c
} else {
insideLineIndent = false
}
prev = c
}
return tree
}
module.exports.parse = parse
module.exports.tree = tree