Added @class and @global declaration and will now make a warning if class or global is not declared.

This commit is contained in:
Martin Carlberg committed 2013-01-14 18:36:39 +01:00
1 parent 77d66fa0c9
commit e78f211e8a
4 files changed
+102 -7

No files matched your search

+6
View File
@@ -61,6 +61,12 @@ CPLog(@"Got some class: %@", inst);
@todo document KVC usage.
*/
@class CPString
@class CPException
@global CPInvalidArgumentException
@implementation CPObject
{
Class isa;
+76 -5
View File
@@ -33,6 +33,16 @@ Scope.prototype.compiler = function()
return this.compiler;
}
Scope.prototype.rootScope = function()
{
return this.prev ? this.prev.rootScope() : this;
}
Scope.prototype.isRootScope = function()
{
return !this.prev;
}
Scope.prototype.currentClassName = function()
{
return this.classDef ? this.classDef.className : this.prev ? this.prev.currentClassName() : null;
@@ -56,7 +66,7 @@ Scope.prototype.getIvarForCurrentClass = function(/* String */ ivarName)
return null;
}
Scope.prototype.getLvar = function(/* String */ lvarName)
Scope.prototype.getLvar = function(/* String */ lvarName, /* BOOL */ stopAtMethod)
{
if (this.vars)
{
@@ -68,8 +78,8 @@ Scope.prototype.getLvar = function(/* String */ lvarName)
var prev = this.prev;
// Stop at the method declaration
if (prev && !this.methodtype)
return prev.getLvar(lvarName);
if (prev && (!stopAtMethod || !this.methodtype))
return prev.getLvar(lvarName, stopAtMethod);
return null;
}
@@ -90,8 +100,22 @@ Scope.prototype.copyAddedSelfToIvarsToParent = function()
}
}
Scope.prototype.addMaybeWarning = function(warning)
{
var rootScope = this.rootScope();
(rootScope._maybeWarnings || (rootScope._maybeWarnings = [])).push(warning);
}
Scope.prototype.maybeWarnings = function()
{
return this.rootScope()._maybeWarnings;
}
var currentCompilerFlags = "";
var reservedIdentifiers = exports.acorn.makePredicate("self _cmd undefined localStorage arguments");
var ObjJAcornCompiler = function(/*String*/ aString, /*CFURL*/ aURL, /*unsigned*/ flags, /*unsigned*/ pass)
{
this.source = aString;
@@ -312,6 +336,14 @@ Program: function(node, st, c) {
c(node.body[i], st, "Statement");
}
CONCAT(st.compiler.jsBuffer,st.compiler.source.substring(st.compiler.lastPos, node.end));
// Check maybe warnings
var maybeWarnings = st.maybeWarnings();
if (maybeWarnings) for (var i = 0; i < maybeWarnings.length; i++) {
var maybeWarning = maybeWarnings[i];
if (!st.getLvar(maybeWarning.identifier) && (typeof global[maybeWarning.identifier] === "undefined" && !st.compiler.getClassDef(maybeWarning.identifier))) {
st.compiler.addWarning(maybeWarning.message);
}
}
},
Function: function(node, scope, c) {
var inner = new Scope(scope);
@@ -359,6 +391,15 @@ VariableDeclaration: function(node, scope, c) {
}
}
},
AssignmentExpression: function(node, st, c) {
var saveAssignment = st.assignment;
st.assignment = true;
c(node.left, st, "Expression");
st.assignment = saveAssignment;
c(node.right, st, "Expression");
if (st.isRootScope() && node.left.type === "Identifier" && !st.getLvar(node.left.name))
st.vars[node.left.name] = {type: "global", node: node.left};
},
MemberExpression: function(node, st, c) {
c(node.object, st, "Expression");
st.secondMemberExpression = !node.computed;
@@ -390,7 +431,8 @@ ClassDeclarationStatement: function(node, st, c) {
// First we declare the class
if (node.superclassname)
{
if (st.compiler.getClassDef(className))
classDef = st.compiler.getClassDef(className);
if (classDef && classDef.ivars) // Must have ivars dictionary to be a real declaration. Without it is a "@class" declaration
throw new SyntaxError(st.compiler.error_message("Duplicate class " + className, node.classname));
if (!st.compiler.getClassDef(node.superclassname.name))
throw new SyntaxError(st.compiler.error_message("Can't find superclass " + node.superclassname.name, node.superclassname));
@@ -678,7 +720,7 @@ Identifier: function(node, st, c) {
if (st.currentMethodType() === "-" && !st.secondMemberExpression)
{
var identifier = node.name,
lvar = st.getLvar(identifier),
lvar = st.getLvar(identifier, true), // Stop looking at method
ivar = st.compiler.getIvarForClass(identifier, st);
if (ivar)
@@ -699,6 +741,17 @@ Identifier: function(node, st, c) {
((st.addedSelfToIvars || (st.addedSelfToIvars = Object.create(null)))[identifier] || (st.addedSelfToIvars[identifier] = [])).push({node: node, index: compiler.jsBuffer.atoms.length});
CONCAT(compiler.jsBuffer, "self.");
}
} else {
if (!reservedIdentifiers(identifier) && !st.getLvar(identifier) && typeof global[identifier] === "undefined" && !st.compiler.getClassDef(identifier)) {
var message;
if (st.assignment) {
message = createMessage("Creating global variable inside function or method'" + identifier + "'", node, st.compiler.source);
st.vars[identifier] = {type: "global", node: node};
} else
message = createMessage("Using unknown class or uninitialized global variable '" + identifier + "'", node, st.compiler.source);
st.addMaybeWarning({identifier: identifier, message: message});
}
}
}
},
@@ -732,5 +785,23 @@ PreprocessStatement: function(node, st, c) {
CONCAT(st.compiler.jsBuffer, st.compiler.source.substring(st.compiler.lastPos, node.start));
st.compiler.lastPos = node.start;
CONCAT(st.compiler.jsBuffer, "//");
},
ClassStatement: function(node, st, c) {
CONCAT(st.compiler.jsBuffer, st.compiler.source.substring(st.compiler.lastPos, node.start));
st.compiler.lastPos = node.start;
CONCAT(st.compiler.jsBuffer, "//");
var className = node.id.name;
if (!st.compiler.getClassDef(className)) {
classDef = {"className": className};
st.compiler.classDefs[className] = classDef;
}
st.vars[node.id.name] = {type: "class", node: node.id};
},
GlobalStatement: function(node, st, c) {
CONCAT(st.compiler.jsBuffer, st.compiler.source.substring(st.compiler.lastPos, node.start));
st.compiler.lastPos = node.start;
CONCAT(st.compiler.jsBuffer, "//");
print("GlobalStatement: " + node.id.name);
st.rootScope().vars[node.id.name] = {type: "global", node: node.id};
}
});
+18 -2
View File
@@ -273,7 +273,7 @@ if (!exports.acorn) {
var _implementation = {keyword: "implementation"}, _outlet = {keyword: "outlet"}, _accessors = {keyword: "accessors"};
var _end = {keyword: "end"}, _import = {keyword: "import", afterImport: true};
var _action = {keyword: "action"}, _selector = {keyword: "selector"};
var _action = {keyword: "action"}, _selector = {keyword: "selector"}, _class = {keyword: "class"}, _global = {keyword: "global"};
// Objective-J keywords
@@ -302,7 +302,7 @@ if (!exports.acorn) {
// Map Objective-J "@" keyword names to token types.
var objJAtKeywordTypes = {"implementation": _implementation, "outlet": _outlet, "accessors": _accessors, "end": _end,
"import": _import, "action": _action, "selector": _selector};
"import": _import, "action": _action, "selector": _selector, "class": _class, "global": _global};
// Punctuation token types. Again, the `type` property is purely for debugging.
@@ -388,6 +388,8 @@ if (!exports.acorn) {
return new Function("str", f);
}
exports.makePredicate = makePredicate;
// The ECMAScript 3 reserved word list.
var isReservedWord3 = makePredicate("abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile");
@@ -1443,6 +1445,20 @@ if (!exports.acorn) {
next();
return finishNode(node, "PreprocessStatement");
// This is a Objective-J statement
case _class:
next();
node.id = parseIdent(false);
print("Class: " + node.id.name);
return finishNode(node, "ClassStatement");
// This is a Objective-J statement
case _global:
next();
node.id = parseIdent(false);
print("Global: " + node.id.name);
return finishNode(node, "GlobalStatement");
// If the statement does not start with a statement keyword or a
// brace, it's an ExpressionStatement or LabeledStatement. We
// simply start parsing an expression, and afterwards, if the
+2
View File
@@ -203,6 +203,8 @@ if (!exports.acorn) {
exports.MethodDeclarationStatement = ignore;
exports.PreprocessStatement = ignore;
exports.ClassStatement = ignore;
exports.GlobalStatement = ignore;
exports.MethodDeclarationStatement = function(node, st, c) {
c(node.body, st, "Statement");