Skip to content

Commit

Permalink
traversal for semantic analyses
Browse files Browse the repository at this point in the history
  • Loading branch information
thejohnfreeman committed Apr 8, 2012
1 parent a9a7567 commit 552a969
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 71 deletions.
12 changes: 8 additions & 4 deletions lib/documenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ define(function (require) {

var SymbolTable = require("./ast/symtab");
var Parser = require("./parser/parser");
var markConstructors = require("./sema/constructor");
var markdownify = require("./sema/markdown");
var Generator = require("./generator/generator");

var Documenter = function Documenter(config) {
Expand Down Expand Up @@ -38,8 +36,14 @@ define(function (require) {
};

Documenter.prototype.flush = function flush() {
markConstructors(this.symtab);
markdownify(this.symtab);
/* Semantic analysis. */
[
"markConstructors",
"copyDeclsToFiles",
"markdownify",
].forEach(function (pass) {
require("./sema/" + pass)(this.symtab);
}, this);

console.log(this.symtab.toPrettyString());

Expand Down
30 changes: 0 additions & 30 deletions lib/sema/constructor.js

This file was deleted.

26 changes: 26 additions & 0 deletions lib/sema/copyDeclsToFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
define(function (require) {

var assert = require("assert");
var traverse = require("./traverse");

var visitDecl = function visitDecl(decl) {
assert.ok(decl.doclet, "expected doclet");
/* Decls that are mentioned but never documented, e.g. a class
* prototype, will have no location information. */
var file = decl.doclet.loc.file;
if (file) file.decls.push(decl);
};

var copyToFileVisitor = {
visitDecl : visitDecl,
visitScope : visitDecl
};

var copyDeclsToFiles = function copyDeclsToFiles(symtab) {
traverse(symtab.globalScope, copyToFileVisitor);
};

return copyDeclsToFiles;

});

24 changes: 24 additions & 0 deletions lib/sema/markConstructors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
define(function (require) {

var assert = require("assert");
var traverse = require("./traverse");

var markConstructorVisitor = {
visitScope : function (scope) {
/** Any scope with a prototype must be a constructor. */
if (scope.decls.hasOwnProperty("prototype") || scope.classDoclet) {
scope.doclet.setKind("constructor");
}
}
};

var markConstructors = function markConstructors(symtab) {
assert.ok(!symtab.globalScope.decls.prototype,
"do not change the prototype of the global object");
traverse(symtab.globalScope, markConstructorVisitor);
};

return markConstructors;

});

33 changes: 0 additions & 33 deletions lib/sema/markdown.js

This file was deleted.

29 changes: 29 additions & 0 deletions lib/sema/markdownify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
define(function (require) {

var markdown = require("markdown").markdown;
var traverse = require("./traverse");

var markdownifyDoclet = function markdownifyDoclet(doclet) {
/* TODO: Resolve references. */
doclet.markdown = markdown.toHTML(doclet.description);
};

var markdownifyVisitor = {
visitDecl : function visitDecl(decl) {
markdownifyDoclet(decl.doclet);
},
visitScope : function visitScope(scope) {
this.visitDecl(scope);
if (scope.classDoclet) markdownifyDoclet(scope.classDoclet);
}
};

var markdownify = function markdownify(symtab) {
/* TODO: Files. */
traverse(symtab.globalScope, markdownifyVisitor);
};

return markdownify;

});

36 changes: 36 additions & 0 deletions lib/sema/traverse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
define(function (require) {

var assert = require("assert");
var Scope = require("../ast/scope");

var defaultVisitor = {
visitDecl : function () {},
visitScope : function () {}
};

var traverse = function traverse(scope, visitor) {
Object.keys(defaultVisitor).forEach(function (key) {
if (!visitor[key]) visitor[key] = defaultVisitor[key];
});

traverseScope(scope, visitor);
};

var traverseScope = function traverseScope(scope, visitor) {
visitor.visitScope(scope);

Object.keys(scope.decls).forEach(function (name) {
var decl = scope.decls[name];
if (decl instanceof Scope) {
traverseScope(decl, visitor);
} else {
assert.ok(decl instanceof Decl, "expected only declarations");
visitor.visitDecl(decl);
}
});
};

return traverse;

});

8 changes: 4 additions & 4 deletions test/tests/inference/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ define(function (require) {

var q = require("qunit");

var markConstructors = require("lib/sema/constructor");
var markConstructors = require("lib/sema/markConstructors");

var parse = require("../common/parse");

Expand All @@ -19,11 +19,11 @@ define(function (require) {
});

q.test("kind", function () {
q.expect(1);
q.expect(2);
q.strictEqual(this.Foo1.doclet.kind, "constructor",
"inferred constructor kind for decl with a prototype");
//q.strictEqual(this.Foo2.doclet.kind, "constructor",
//"inferred constructor kind for decl with a class doclet");
q.strictEqual(this.Foo2.doclet.kind, "constructor",
"inferred constructor kind for decl with a class doclet");
});

});
Expand Down

0 comments on commit 552a969

Please sign in to comment.