forked from gracelang/minigrace
-
Notifications
You must be signed in to change notification settings - Fork 3
/
xmodule.grace
518 lines (475 loc) · 20 KB
/
xmodule.grace
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
dialect "standard"
import "io" as io
import "sys" as sys
import "util" as util
import "mirror" as mirror
import "errormessages" as errormessages
import "unixFilePath" as filePath
import "shasum" as shasum
import "regularExpression" as regex
import "intrinsic" as intrinsic
import "basic" as basic
import "buildinfo" as buildinfo
use basic.open
def CheckerFailure = Exception.refine "CheckerFailure"
def DialectError is public = Exception.refine "DialectError"
//must correspond to what is defined in "dialect"
def gctCache = dictionary.empty
def keyCompare = { a, b -> a.key.compare(b.key) }
def currentDialect is public = object {
var name is public := "standard"
var moduleObject is public // will be assigned if the dialect is loaded
var hasParseChecker is public := false
var hasAstChecker is public := false
var hasAtStart is public := false
var hasAtEnd is public := false
}
// TODO: this partially duplicates information in the moduleNode; integrate
type RangeSuggestions = interface {
line -> Number
posStart -> Number
posEnd -> Number
suggestions
}
def externalModules is public = dictionary.empty // dialect, & direct imports
def allModules = dictionary.empty // includes above and transitive imports
type ModuleRecord = interface { // a record describing an external module
path -> filePath.FilePath // the path to the source file
sha -> String // the SHA256 checksum of the source
jsFile -> filePath.FilePath // the corresponding .js object file
importsChecked -> Boolean // have transitive imports been checked?
checked // sets importsChecked
}
class filePath (fp) sha (sum) jsFile (jsf) checked (c) -> ModuleRecord {
def path is public = fp // the path to the source file
def sha is public = sum // the SHA256 checksum of the source
def jsFile is public = jsf // the corresponding .js object file
var importsChecked is readable := c
// have transitive imports been checked?
method checked { importsChecked := true }
}
method checkDialect(moduleObject) {
def dialectNode = moduleObject.theDialect
def dmn = dialectNode.moduleName
currentDialect.name := dmn
if (dmn == "none") then { return }
util.log 50 verbose "checking dialect \"{dmn}\" used by module {moduleObject.name}"
checkExternalModule(dialectNode)
def dialectGct = gctDictionaryFor(dialectNode.moduleName)
def dialectScope = dialectGct.at "self" ifAbsent {
EnvironmentException.raise "dialect \"{dmn}\"'s gct does not have a 'self' entry"
}.first
def dAttributes = dialectGct.at "scope:{dialectScope}"
if (dAttributes.anySatisfy { each -> each.startsWith "thisDialect" }) then {
util.log 45 verbose "loading dialect \"{dmn}\" for checkers."
try {
def dobj = mirror.loadModule(dialectNode.path)
currentDialect.moduleObject := dobj
if (mirror.reflect(dobj).methodNames.contains "thisDialect") then {
def mths = mirror.reflect(dobj.thisDialect).methods
for (mths) do { m ->
if (m.name == "parseChecker(_)") then {
currentDialect.hasParseChecker := true
}
if (m.name == "astChecker(_)") then {
currentDialect.hasAstChecker := true
}
if (m.name == "atEnd(_)") then {
currentDialect.hasAtEnd := true
}
if (m.name == "atStart(_)") then {
currentDialect.hasAtStart := true
}
}
}
} catch { e:Exception ->
util.setPosition(dialectNode.line, 1)
e.printBacktrace
errormessages.error "Dialect error: dialect \"{dmn}\" failed to load.\n{e}."
atRange(dialectNode)
}
} else {
util.log 45 verbose "not loading dialect \"{dmn}\" because it does not define 'thisDialect'"
}
}
method doParseCheck(moduleNode) {
if (currentDialect.hasParseChecker.not) then { return }
try {
currentDialect.moduleObject.thisDialect.parseChecker(moduleNode)
} catch { e:CheckerFailure | DialectError | errormessages.SyntaxError ->
reportDialectError(e)
} catch { e:Exception -> // some unknown Grace exception
printBacktrace (e) asFarAs "thisDialect.parseChecker"
errormessages.error("Unexpected exception raised by parse checker for " ++
"dialect '{currentDialect.name}'.\n{e.exception}: {e.message}")
}
}
method doAstCheck(moduleNode) {
if (currentDialect.hasAstChecker.not) then { return }
try {
currentDialect.moduleObject.thisDialect.astChecker(moduleNode)
} catch { e:CheckerFailure | DialectError | errormessages.SyntaxError ->
reportDialectError(e)
} catch { e:Exception -> // some unknown Grace exception
printBacktrace (e) asFarAs "thisDialect.astChecker"
errormessages.error("Unexpected exception raised by AST checker for " ++
"dialect '{currentDialect.name}'.\n{e.exception}: {e.message}")
}
}
method reportDialectError(ex) {
match (ex.data)
case { rs:RangeSuggestions ->
errormessages.error "Dialect {currentDialect.name}: {ex.message}."
atRange(rs)
withSuggestions(rs.suggestions)
} case { r:Range -> // inlcudes AstNode
errormessages.error "Dialect {currentDialect.name}: {ex.message}."
atRange(r)
} case { p:Position ->
errormessages.error "Dialect {currentDialect.name}: {ex.message}."
atPosition(p.line, p.column)
} else {
errormessages.error "Dialect {currentDialect.name}: {ex.message}."
atLine(util.linenum)
}
}
method printBacktrace(exceptionPacket) asFarAs (methodName) {
def ex = exceptionPacket.exception
def msg = exceptionPacket.message
def lineNr = exceptionPacket.line
def mod = exceptionPacket.moduleName
io.error.write "{ex}: {msg}\n"
def bt = exceptionPacket.backtrace
bt.reversed.do { frameDescription ->
io.error.write " requested from {frameDescription}\n"
if (frameDescription.contains(methodName)) then { return }
}
}
method checkExternalModule(node) {
// Used by identifierresolution to check that node, representing a
// dialect or import statement, refers to a module that exisits.
// Checks that node.moduleName can be found on node.path, and that a
// compiled version exists; creates the compiled version if necessary.
// If an existing, up-to-date, compiled version was found, then check
// its imports; if we had to compile it, then the recursive compile has
// already checked its imports.
def moduleName = node.moduleName
if (externalModules.containsKey(moduleName)) then {
errormessages.syntaxError "multiple imports of {moduleName}"
atRange (node.range)
}
if (intrinsic.inBrowser) then {
if (compiledModuleExistsInBrowser(moduleName)) then {
return
} else {
errormessages.error "Please \"Run\" module {moduleName} before importing it."
atRange(node.range)
}
}
util.log 50 verbose "checking module \"{moduleName}\" used by {util.modname}"
def moduleInfo = findOrBuildCompiledModule(moduleName, node.path, node.range)
externalModules.at (moduleName) put (moduleInfo)
allModules.at(moduleName) put (moduleInfo)
checkTransitiveImports(moduleInfo, node)
}
method compiledModuleExistsInBrowser(name) {
native "js" code ‹
if (typeof window[graceModuleName(var_name._value)] === "function") {
return GraceTrue;
} else {
return GraceFalse;
}›
}
method findOrBuildCompiledModule(moduleName, modulePath, sourceRange) -> ModuleRecord
is confidential {
// Returns a record desribing the compiled module for moduleName.
// Creates the .js file if necessary.
// modulePath is the whole string from the dialect or import, potentially
// containing "/" characters; moduleName is the name after the final "/"
allModules.at(moduleName) ifAbsent {
def graceFile = findGraceFile(modulePath) otherwise { m ->
def rm = errormessages.readableStringFrom(m)
errormessages.error "I can't find {modulePath}; tried {rm}."
atRange (sourceRange)
}
def sourceSHA = shasum.sha256OfFile(graceFile)
def thisCompiler = buildinfo.gitgeneration
def jsFileName = filePath.withBase(graceFile.base).setExtension ".js"
def jsFile = findJsFile(jsFileName) suchThat { f ->
file(f) createdBy(thisCompiler) withSHA(sourceSHA)
} otherwise { m ->
def rm = errormessages.readableStringFrom(m)
util.log 50 verbose("I can't find {jsFileName} with SHA {sourceSHA} " ++
"compiled by {thisCompiler}; tried {rm}")
def objectFile = compileModule (moduleName) inFile (graceFile)
atRange (sourceRange)
if (objectFile.exists.not) then {
errormessages.error("I just compiled {graceFile}, " ++
"but {objectFile} does not exist") atRange (sourceRange)
}
def newModuleRecord = filePath (graceFile) sha (sourceSHA) jsFile (objectFile) checked (true)
allModules.at(moduleName) put (newModuleRecord)
return newModuleRecord
}
util.log 55 verbose "found compiled module \"{moduleName}\" in {jsFile}"
def newModuleRecord = filePath (graceFile) sha (sourceSHA) jsFile (jsFile) checked (false)
allModules.at(moduleName) put (newModuleRecord)
newModuleRecord
}
}
method findGraceFile (modulePath) otherwise (action) -> filePath.FilePath
is confidential {
var candidate := filePath.fromString(modulePath).setExtension ".grace"
def directoryPrefix = candidate.directory
if (directoryPrefix.startsWith "/") then {
if (candidate.exists) then { return candidate }
action.apply([candidate])
}
candidate.setDirectory(util.sourceDir ++ directoryPrefix)
if (candidate.exists) then { return candidate }
def rejects = list [ candidate ]
def locations = filePath.split(sys.environ.at "GRACE_MODULE_PATH")
locations.do { each ->
candidate := candidate.copy.setDirectory(each ++ directoryPrefix)
if (candidate.exists) then { return candidate }
rejects.add(candidate)
}
action.apply(rejects)
}
method findJsFile (jsFileName:filePath.FilePath) suchThat (p:Predicate1) otherwise (action) {
// returns a file with the same base as jsFileName, on a directory in
// util.sourceDir, util.outDir, or GRACE_MODULE_PATH
if ((jsFileName.exists) && {p.apply(jsFileName)}) then { return jsFileName }
var candidate := jsFileName.copy
def directoryPrefix = candidate.directory
candidate.setDirectory(util.outDir)
if ((candidate.exists) && {p.apply(candidate)}) then { return candidate }
def rejects = list [ candidate ]
candidate := candidate.copy.setDirectory(util.outDir ++ directoryPrefix)
if ((candidate.exists) && {p.apply(candidate)}) then { return candidate }
rejects.add(candidate)
if (util.sourceDir ≠ util.outDir) then {
candidate := candidate.copy.setDirectory(util.sourceDir ++ directoryPrefix)
if ((candidate.exists) && {p.apply(candidate)}) then { return candidate }
rejects.add(candidate)
}
def locations = filePath.split(sys.environ.at "GRACE_MODULE_PATH")
def od = util.outDir
if (locations.contains(od).not) then {
locations.add(od)
}
locations.do { each ->
if (directoryPrefix ≠ "./") then {
candidate := candidate.copy.setDirectory(each ++ directoryPrefix)
if ((candidate.exists) && {p.apply(candidate)}) then { return candidate }
rejects.add(candidate)
}
candidate := candidate.copy.setDirectory(each)
if ((candidate.exists) && {p.apply(candidate)}) then { return candidate }
rejects.add(candidate)
}
action.apply(rejects)
}
method file(f) createdBy(thisCompiler) withSHA(sourceSHA) {
// was f created by thisCompiler from a Grace file with sourceSHA?
def jsStream = io.open(f, "r")
try {
def originalSHA = extract "sha256" from (jsStream)
if (originalSHA ≠ sourceSHA) then { return false }
def usedCompiler = extract "minigraceGeneration" from (jsStream)
if (usedCompiler ≠ thisCompiler) then { return false }
return true
} catch { ex:EnvironmentException ->
util.log (util.defaultVerbosity) verbose (ex.message)
return false
} finally {
jsStream.close
}
}
method extract (item) from (stream) {
var maxLines := 10 // look in first 10 lines of js file
def sought = regex.fromString "gracecode_.*_{item} = \"(\\w+)\""
while { stream.eof.not && (maxLines > 0) } do {
def line = stream.getline
def matches = sought.allMatches(line)
if (matches.isEmpty.not) then {
return matches.first.group 1
}
maxLines := maxLines - 1
}
EnvironmentException.raise "Can't find {item} in JS file {stream.pathname}"
}
method checkTransitiveImports(moduleRecord, node) {
if (moduleRecord.importsChecked) then { return }
moduleRecord.checked
def modulePath = moduleRecord.path
def moduleName = modulePath.base
def gctDict = gctDictionaryFor(moduleName)
util.log 50 verbose "checking modules imported by \"{moduleName}\""
def importedModules = gctDict.at "modules" // includes the dialect
def m = util.modname
importedModules.do { eachImport ->
if (m == eachImport) then {
errormessages.error("cyclic import detected — '{m}' is imported "
++ "by '{moduleName}', which is imported by '{m}'")
atRange(node.range)
}
if (intrinsic.inBrowser) then {
if (compiledModuleExistsInBrowser(eachImport)) then {
return
} else {
errormessages.error "Please \"Run\" module {eachImport} before importing it."
atRange(node.range)
}
}
def moduleInfo = findOrBuildCompiledModule(eachImport, eachImport, node.range)
checkTransitiveImports(moduleInfo, node)
}
}
method compileModule (nm) inFile (sourceFile) atRange (sourceRange) is confidential {
// Compiles module with name nm located in sourceFile.
// Returns the filePath containing the compiled code.
if (util.recurse.not) then {
errormessages.error "Please compile module {nm} before using it."
atRange (sourceRange)
}
if (intrinsic.inBrowser) then {
errormessages.error "Please \"Run\" module {nm} before using it."
atRange (sourceRange)
}
var cmd
if (sys.argv.first.contains "/") then {
cmd := io.realpath(sys.argv.first)
} else {
cmd := io.realpath "{sys.execPath}/{sys.argv.first}"
}
cmd := "\"{cmd}\""
cmd := cmd.replace "compiler-js" with "minigrace-js"
if (util.verbosity != util.defaultVerbosity) then {
cmd := cmd ++ " --verbose {util.verbosity}"
}
var outputDirectory
if (util.dirFlag) then {
cmd := cmd ++ " --dir " ++ util.outDir
outputDirectory := util.outDir
} else {
outputDirectory := sourceFile.directory
}
if (false != util.vtag) then {
cmd := cmd ++ " --vtag " ++ util.vtag
}
if (false != util.gracelibPath) then {
cmd := cmd ++ " --gracelib " ++ util.gracelibPath
}
cmd := cmd ++ util.commandLineExtensions
if (util.defaultTarget ≠ util.target) then {
cmd := cmd ++ " --target {util.target}"
}
cmd := cmd ++ " --make \"{sourceFile}\""
util.log (util.defaultVerbosity - 1) verbose "executing sub-compile of {sourceFile}"
def exitCode = io.spawn("bash", ["-c", cmd]).status
if (exitCode != 0) then {
errormessages.error "Failed to compile imported module {nm} ({exitCode})."
atRange (sourceRange)
}
filePath.withDirectory(outputDirectory) base(sourceFile.base) extension ".js"
}
method gctDictionaryFor(moduleName) {
// used by identifierresolution to discover the names defined by moduleName,
// as well as here (in xmodule) to find transitive imports of moduleName
gctCache.at(moduleName) ifAbsent {
gctDictionaryFrom(extractGctFor(moduleName)) for(moduleName)
}
}
method gctDictionaryFrom(gctList) for(moduleName) is confidential {
def gctDict = dictionary.empty
var key := ""
for (gctList) do { line ->
if (line.size > 0) then {
if (line.first ≠ " ") then {
key := line.substringFrom 1 to (line.size-1) // dropping the ":"
gctDict.at(key) put(list [ ])
} else {
gctDict.at(key).addLast(line.substringFrom 2)
}
}
}
gctCache.at(moduleName) put(gctDict)
gctDict
}
method extractGctFor(moduleName) is confidential {
// Extracts the gct information for moduleName from an external resource
// Returns the gct information as a collection of Strings.
if (intrinsic.inBrowser) then { return extractGctFromCache(moduleName) }
def jsFile = allModules.at(moduleName).jsFile
try {
extractGctFor(moduleName) fromJsFile(jsFile)
} catch {ex:EnvironmentException ->
EnvironmentException.raise ("failed to find gct for {moduleName} " ++
"in {jsFile}")
}
}
method extractGctFor(moduleName) fromJsFile(filepath) is confidential {
// Looks in filepath, containing the compiled code for moduleName.
// Returns the gct information as a collection of Strings.
def jsStream = io.open(filepath, "r")
var maxLines := 10 // look in first 10 lines of js file
while { jsStream.eof.not && (maxLines > 0) } do {
def line = jsStream.getline
if (line.startsWith " gctCache[") then {
jsStream.close
return splitJsString(line)
}
maxLines := maxLines - 1
}
jsStream.close
EnvironmentException.raise "Can't find gct string in JS file {filepath}"
}
method splitJsString(jsLine:String) is confidential {
// jsLine is a line of javascript like
// gctCache["xmodule"] = "objects:\nconfidential:\n CheckerFailure\n ..."
// Evaluates the string on the rhs of the = sign, splits into lines,
// and returns a (Grace) list containing those lines as (Grace) strings.
native "js" code ‹
var arg = var_jsLine._value;
var keyStr = "\"] = ";
var keyStart = arg.indexOf(keyStr);
var stringLit = arg.substr(keyStart + keyStr.length);
var gctString = eval(stringLit);
var jsStringArray = gctString.split("\n");
result = GraceList([]);
for (var ix = 0, len = jsStringArray.length ; ix < len; ix++) {
callmethod(result, "add(1)", [1],
new GraceString (jsStringArray[ix]));
}›
}
method extractGctFromCache(module) {
// When running in the browser, returns a Grace list containing
// the contents of the cached gct information for module
native "js" code ‹var gctString = gctCache[var_module._value];
var jsStringArray = gctString.split("\n");
result = GraceList([]);
for (var ix = 0, len = jsStringArray.length ; ix < len; ix++) {
callmethod(result, "add(1)", [1],
new GraceString (jsStringArray[ix]));
}›
}
method writeGCT(modname, dict) {
if (util.extensions.containsKey "gctfile") then {
def fp = io.open("{util.outDir}{modname}.gct", "w")
dict.bindings.sortedBy(keyCompare).do { b ->
fp.write "{b.key}:\n"
b.value.do { v -> fp.write " {v}\n" }
}
fp.close
}
gctCache.at(modname) put(dict)
}
method gctAsString(gctDict) {
var ret := ""
gctDict.bindings.sortedBy(keyCompare).do { b ->
ret := ret ++ "{b.key}:\n"
b.value.do { v -> ret := ret ++ " {v}\n" }
}
ret
}