-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathcodescan.js
More file actions
117 lines (110 loc) · 2.84 KB
/
Copy pathcodescan.js
File metadata and controls
117 lines (110 loc) · 2.84 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
///////////////////////////////////////////////////////////////////////////
// SIMPLISTIC CODE SCANNING
// This textual code scan just scans coffeescript and javascript code,
// looking for object names and
///////////////////////////////////////////////////////////////////////////
function stripCSMultiline(code) {
// Really simple heuristic for coffeescript:
// Just scan the multiline quote and comment delimeters, and
// omit parts of the code within those when scanning.
var parts = code.split(/(###|\/\/\/|"""|'''|#|\n)/);
var keep = [],
inside = null,
hadline = false,
ignoreline = false;
for (var j = 0; j < parts.length; ++j) {
var part = parts[j];
if (ignoreline) {
if (part === '\n') {
ignoreline = false;
} else {
continue;
}
}
if (inside) {
if (part === '\n' && !hadline) {
hadline = true;
keep.push(part);
}
if (part === inside) {
hadline = false;
part = null;
}
continue;
}
if (part === '#') {
ignoreline = true;
continue;
}
if (/^(?:###|"""|'''|\/\/\/)$/.test(part)) {
inside = part;
continue;
}
keep.push(part);
}
return keep.join('');
}
function coffeeObjects(code) {
code = stripCSMultiline(code);
var lines = code.split('\n');
var result = [];
for (var j = 0; j < lines.length; ++j) {
var match = /^\s*(\w+)\s*=\s*new\s+(\w+)(.*)$/.exec(lines[j]);
if (match) {
result.push({
name: match[1],
class: match[2],
args: (match[3] || '').replace(/\((.*)\)\s*/, '$1').split(',').map(
function(t) { return t.trim(); })
});
}
}
return result;
}
function jsObjects(code) {
var lines = code.split('\n');
var result = [];
for (var j = 0; j < lines.length; ++j) {
var match = /^\s*(\w+)\s*=\s*new\s+(\w+)(.*)$/.exec(lines[j]);
if (match) {
result.push({
name: match[1],
class: match[2],
args: (match[3] || '').replace(/\((.*)\)\s*/, '$1').split(',').map(
function(t) { return t.trim(); })
});
}
}
return result;
}
function scanObjects(language, code) {
var objects;
if (/coffee/.test(language)) {
objects = coffeeObjects(code);
} else if (/js|javascript/.test(language)) {
objects = jsObjects(code);
} else {
return [];
}
var deduped = {};
for (var j = 0; j < objects.length; ++j) {
var obj = objects[j];
if (/^(?:Turtle|Sprite|Piano|Webcam|Pencil)$/.test(obj.class) &&
!(obj.name in deduped)) {
deduped[obj.name] = obj;
}
}
var result = [];
for (var n in deduped) {
var obj = deduped[n];
result.push({
label: obj.class + ' ' + obj.name,
name: obj.name
});
}
return result;
}
window.pencilcode.codescan = {
scanObjects: scanObjects
};
module.exports = window.pencilcode.codescan;