Commits

kates committed 4ed5148

parsing comment blocks first

Comments (0)

Files changed (2)

lib/grammar.pegjs

 start
-	= block*
+	= doc*
 
-// block is comment block and function body
+doc
+	= "/**" "\n" body:body? "*/" "\n"* functionBody { return {comment: body};}
 
-block
-	= left:commentBlock right:functionBody "\n"* { return {function: right};}
+body
+	= line:line* { return {tags: line};}
 
-commentBlock
-	= startCommentBlock + endCommentBlock
+line
+	= " "* "*" " "+ "@" tag:ident " " value:ident "\n"+ { return {tag: tag, value: value};}
 
-startCommentBlock
-	= "/**" "\n"
-
-endCommentBlock
-	= "*/" "\n"
 
 functionBody
-	= sig:functionSig "{" "}" { return sig;}
-
-functionSig
-	= function " "+ name:ident "(" args:arglist* ")" { return {name: name, args:args};}
-	/ "var" " "+ name:ident " "? "=" " "? function "(" args:arglist* ")" {return {name:name, args: args};}
-
-function
-	= "function"
-
-arglist
-	= arg:ident ("," " "*)? { return arg;}
+	= "function" " "+ ident "(" ")" "{" "}" "\n"*
 
 ident
-	= chars:[a-zA-Z]+ { return chars.join(''); }
+	= chars:[0-9a-zA-Z]* { return chars.join('');}

test/parser-test.js

 var buster = require("buster");
 var parser = require("../lib/parser").parser;
-//var fs = require('fs');
 
 buster.testCase("Parser", {
-	"can parse function foo()": function() {
-		var code = "/**\n*/\nfunction foo(){}";
+	"comment block": function() {
+		var code = "/**\n * @name Klass\n\n * @param num\n * @param num2\n*/\nfunction Klass(){}";
 		var ast = parser.parse(code);
-		assert.equals(ast[0]['function']['name'], "foo");
-		assert.equals(ast[0]['function']['args'].length, 0);
-	},
-	"can parse function foo(bar)": function() {
-		var code = "/**\n*/\nfunction foo(bar){}";
-		var ast = parser.parse(code);
-		assert.equals(ast[0]['function']['name'], "foo");
-		assert.equals(ast[0]['function']['args'].length, 1);
-		assert.equals(ast[0]['function']['args'][0], 'bar');
-	},
-	"can parse function foo(bar, baz)": function() {
-		var code = "/**\n*/\nfunction foo(bar, baz){}";
-		var ast = parser.parse(code);
-		assert.equals(ast[0]['function']['name'], "foo");
-		assert.equals(ast[0]['function']['args'].length, 2);
-		assert.equals(ast[0]['function']['args'][0], 'bar');
-		assert.equals(ast[0]['function']['args'][1], 'baz');
-	},
-	"can parse var foo = function()": function() {
-		var code = "/**\n*/\nfunction foo(){}";
-		var ast = parser.parse(code);
-		assert.equals(ast[0]['function']['name'], "foo");
-		assert.equals(ast[0]['function']['args'].length, 0);
-	},
-	"can parse var foo = function(bar)": function() {
-		var code = "/**\n*/\nfunction foo(bar){}";
-		var ast = parser.parse(code);
-		assert.equals(ast[0]['function']['name'], "foo");
-		assert.equals(ast[0]['function']['args'].length, 1);
-		assert.equals(ast[0]['function']['args'][0], 'bar');	
-	},
-	"can parse var foo = function(bar, baz)": function() {
-		var code = "/**\n*/\nfunction foo(bar, baz){}";
-		var ast = parser.parse(code);
-		assert.equals(ast[0]['function']['name'], "foo");
-		assert.equals(ast[0]['function']['args'].length, 2);
-		assert.equals(ast[0]['function']['args'][0], 'bar');
-		assert.equals(ast[0]['function']['args'][1], 'baz');
+
+		assert.equals(ast[0]['comment']['tags'].length, 3);
+		assert.equals(ast[0]['comment']['tags'][0]['tag'], 'name')
+		assert.equals(ast[0]['comment']['tags'][1]['tag'], 'param')
 	}
 });