forked from bokeh/bokeh
-
Notifications
You must be signed in to change notification settings - Fork 11
/
scanner.js
134 lines (117 loc) · 3.66 KB
/
scanner.js
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
131
132
133
134
(function() {
var Scanner, StringScanner, trim;
StringScanner = require("strscan").StringScanner;
trim = require("./util").trim;
module.exports = Scanner = (function() {
Scanner.modePatterns = {
data: /(.*?)(<%%|<%\s*(\#)|<%(([=-])?)|\n|$)/,
code: /(.*?)((((:|(->|=>))\s*))?%>|\n|$)/,
comment: /(.*?)(%>|\n|$)/
};
Scanner.dedentablePattern = /^(end|when|else|catch|finally)(?:\W|$)/;
Scanner.scan = function(source) {
var scanner, tokens;
tokens = [];
scanner = new Scanner(source);
while (!scanner.done) {
scanner.scan(function(token) {
return tokens.push(token);
});
}
return tokens;
};
function Scanner(source) {
this.source = source.replace(/\r\n?/g, "\n");
this.scanner = new StringScanner(this.source);
this.mode = "data";
this.buffer = "";
this.lineNo = 1;
this.done = false;
}
Scanner.prototype.scan = function(callback) {
if (this.done) {
return callback();
} else if (this.scanner.hasTerminated()) {
this.done = true;
switch (this.mode) {
case "data":
return callback(["printString", this.flush()]);
case "code":
return callback(["fail", "unexpected end of template"]);
}
} else {
this.advance();
switch (this.mode) {
case "data":
return this.scanData(callback);
case "code":
return this.scanCode(callback);
case "comment":
return this.scanComment(callback);
}
}
};
Scanner.prototype.advance = function() {
this.scanner.scanUntil(Scanner.modePatterns[this.mode]);
this.buffer += this.scanner.getCapture(0);
this.tail = this.scanner.getCapture(1);
this.comment = this.scanner.getCapture(2);
this.directive = this.scanner.getCapture(4);
return this.arrow = this.scanner.getCapture(5);
};
Scanner.prototype.scanData = function(callback) {
if (this.tail === "<%%") {
this.buffer += "<%";
return this.scan(callback);
} else if (this.tail === "\n") {
this.buffer += this.tail;
this.lineNo++;
return this.scan(callback);
} else if (this.tail) {
callback(["printString", this.flush()]);
if (this.comment) {
return this.mode = "comment";
} else {
this.mode = "code";
return callback([
"beginCode", {
print: this.directive != null,
safe: this.directive === "-"
}
]);
}
}
};
Scanner.prototype.scanCode = function(callback) {
var code;
if (this.tail === "\n") {
return callback(["fail", "unexpected newline in code block"]);
} else if (this.tail) {
this.mode = "data";
code = trim(this.flush());
if (this.arrow) code += " " + this.arrow;
if (this.isDedentable(code)) callback(["dedent"]);
callback(["recordCode", code]);
if (this.directive) return callback(["indent", this.arrow]);
}
};
Scanner.prototype.scanComment = function(callback) {
if (this.tail === "\n") {
return callback(["fail", "unexpected newline in code block"]);
} else if (this.tail) {
this.mode = "data";
return this.buffer = "";
}
};
Scanner.prototype.flush = function() {
var buffer;
buffer = this.buffer;
this.buffer = "";
return buffer;
};
Scanner.prototype.isDedentable = function(code) {
return code.match(Scanner.dedentablePattern);
};
return Scanner;
})();
}).call(this);