forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsunlight.javascript.js
183 lines (153 loc) · 4.85 KB
/
sunlight.javascript.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
(function(sunlight, undefined){
if (sunlight === undefined || sunlight["registerLanguage"] === undefined) {
throw "Include sunlight.js before including language files";
}
sunlight.registerLanguage("javascript", {
keywords: [
//keywords
"break", "case", "catch", "continue", "default", "delete", "do",
"else", "finally", "for", "function", "if", "in", "instanceof",
"new", "return", "switch", "this", "throw", "try", "typeof",
"var", "void", "while", "with",
//literals
"true", "false", "null"
],
customTokens: {
reservedWord: {
values: [
"abstract", "boolean", "byte", "char", "class", "const", "debugger", "double",
"enum", "export", "extends", "final", "float", "goto", "implements", "import",
"int", "interface", "long", "native", "package", "private", "protected", "public",
"short", "static", "super", "synchronized", "throws", "transient", "volatile"
],
boundary: "\\b"
},
globalVariable: {
values: ["NaN", "Infinity", "undefined"],
boundary: "\\b"
},
globalFunction: {
values: ["encodeURI", "encodeURIComponent", "decodeURI", "decodeURIComponent", "parseInt", "parseFloat", "isNaN", "isFinite", "eval"],
boundary: "\\b"
},
globalObject: {
values: [
"Math", "JSON",
"XMLHttpRequest", "XDomainRequest", "ActiveXObject",
"Boolean", "Date", "Array", "Image", "Function", "Object", "Number", "RegExp", "String"
],
boundary: "\\b"
}
},
scopes: {
string: [ ["\"", "\"", sunlight.util.escapeSequences.concat(["\\\""])], ["'", "'", sunlight.util.escapeSequences.concat(["\\\'", "\\\\"])] ],
comment: [ ["//", "\n", null, true], ["/*", "*/"] ]
},
customParseRules: [
//regex literal
function(context) {
var peek = context.reader.peek(),
isValid,
regexLiteral = "/",
line = context.reader.getLine(),
column = context.reader.getColumn(),
charClass = false,
peek2,
next;
if (context.reader.current() !== "/" || peek === "/" || peek === "*") {
//doesn't start with a / or starts with // (comment) or /* (multi line comment)
return null;
}
isValid = function() {
var previousNonWsToken = context.token(context.count() - 1),
previousToken = null;
if (context.defaultData.text !== "") {
previousToken = context.createToken("default", context.defaultData.text);
}
if (!previousToken) {
previousToken = previousNonWsToken;
}
//first token of the string
if (previousToken === undefined) {
return true;
}
//since JavaScript doesn't require statement terminators, if the previous token was whitespace and contained a newline, then we're good
if (previousToken.name === "default" && previousToken.value.indexOf("\n") > -1) {
return true;
}
if (sunlight.util.contains(["keyword", "ident", "number"], previousNonWsToken.name)) {
return false;
}
if (previousNonWsToken.name === "punctuation" && !sunlight.util.contains(["(", "{", "[", ",", ";"], previousNonWsToken.value)) {
return false;
}
return true;
}();
if (!isValid) {
return null;
}
//read the regex literal
while (context.reader.peek() !== context.reader.EOF) {
peek2 = context.reader.peek(2);
if (peek2 === "\\/" || peek2 === "\\\\") {
//escaped backslash or escaped forward slash
regexLiteral += context.reader.read(2);
continue;
}
if (peek2 === "\\[" || peek2 === "\\]") {
regexLiteral += context.reader.read(2);
continue;
} else if (next === "[") {
charClass = true;
} else if (next === "]") {
charClass = false;
}
regexLiteral += (next = context.reader.read());
if (next === "/" && !charClass) {
break;
}
}
//read the regex modifiers
//only "g", "i" and "m" are allowed, but for the sake of simplicity we'll just say any alphabetical character is valid
while (context.reader.peek() !== context.reader.EOF) {
if (!/[A-Za-z]/.test(context.reader.peek())) {
break;
}
regexLiteral += context.reader.read();
}
return context.createToken("regexLiteral", regexLiteral, line, column);
}
],
identFirstLetter: /[$A-Za-z_]/,
identAfterFirstLetter: /[\w\$]/,
namedIdentRules: {
follows: [
[{ token: "keyword", values: ["function"] }, sunlight.util.whitespace]
]
},
operators: [
//arithmetic
"++", "+=", "+",
"--", "-=", "-",
"*=", "*",
"/=", "/",
"%=", "%",
//boolean
"&&", "||",
//bitwise
"|=", "|",
"&=", "&",
"^=", "^",
">>>=", ">>>", ">>=", ">>",
"<<=", "<<",
//inequality
"<=", "<",
">=", ">",
"===", "==", "!==", "!=",
//unary
"!", "~",
//other
"?", ":", ".", "="
]
});
}(this["Sunlight"]));