kates avatar kates committed 2c36d71

parsing function signature

Comments (0)

Files changed (2)

lib/grammar.pegjs

 	= doc*
 
 doc
-	= whitespace* "/**" whitespace* body:body? whitespace* "*/" whitespace* functionBody { return {comment: body};}
+	= whitespace* "/**" whitespace* body:body? whitespace* "*/" whitespace* func:functionBody { return {comment: body, function:func};}
 
 body
 	= lines:line* { var tags = []; for (var i=0; i<lines.length; i++){ if (lines[i] != '') { tags.push(lines[i]);}} return {tags: tags};}
 line
 	= " "* "*" whitespace+ comment:comment? { return comment;}
 
-ignoreLine
-	= " "* "*" whitespace+
-
 comment
 	= "@" tag:ident " "+ value:ident whitespace+ { return {tag: tag, value: value};}
 
 functionBody
-	= "function" " "+ ident "(" ")" "{" "}" whitespace*
+	= "function" " "+ name:ident "(" args:arglist* ")" "{" "}" whitespace* { return {name: name, args:args};}
+
+arglist
+	= arg:ident ("," " "*)? { return arg;}
 
 whitespace
 	= " "
 	/ "\n"
 
 ident
-	= chars:[0-9a-zA-Z]* { return chars.join('');}
+	= chars:[0-9a-zA-Z]+ { return chars.join('');}

test/parser-test.js

 
 buster.testCase("Parser", {
 	"comment block 1": function() {
-		var code = "/**\n * @name Klass\n * @param num\n * @param num2\n*/\nfunction Klass(){}";
+		var code = "/**\n * @name Klass\n * @param num\n * @param num2\n*/\nfunction Klass(num,num2){}";
 		var ast = parser.parse(code);
 
 		assert.equals(ast[0]['comment']['tags'].length, 3);
 		assert.equals(ast[0]['comment']['tags'][0]['tag'], 'name');
+		assert.equals(ast[0]['comment']['tags'][0]['value'], 'Klass');
 		assert.equals(ast[0]['comment']['tags'][1]['tag'], 'param');
+		assert.equals(ast[0]['comment']['tags'][1]['value'], 'num');
+		assert.equals(ast[0]['comment']['tags'][2]['tag'], 'param');
+		assert.equals(ast[0]['comment']['tags'][2]['value'], 'num2');
+
+		assert.equals(ast[0]['function']['name'], 'Klass');
+		assert.equals(ast[0]['function']['args'].length, 2);
+		assert.equals(ast[0]['function']['args'][0], 'num');
+		assert.equals(ast[0]['function']['args'][1], 'num2');
 	},
 	"comment block 2": function() {
-		var code = "/**\n * @name Klass\n *\n * @param num\n * @param num2\n*/\nfunction Klass(){}";
+		var code = "/**\n * @name Klass\n *\n * @param num\n * @param num2\n*/\nfunction Klass(num, num2){}";
 		var ast = parser.parse(code);
-		console.log(ast[0]['comment']);
+		//console.log(ast[0]['function'])
 		assert.equals(ast[0]['comment']['tags'].length, 3);
 		assert.equals(ast[0]['comment']['tags'][0]['tag'], 'name');
+		assert.equals(ast[0]['comment']['tags'][0]['value'], 'Klass');
 		assert.equals(ast[0]['comment']['tags'][1]['tag'], 'param');
+		assert.equals(ast[0]['comment']['tags'][1]['value'], 'num');
+		assert.equals(ast[0]['comment']['tags'][2]['tag'], 'param');
+		assert.equals(ast[0]['comment']['tags'][2]['value'], 'num2');
+
+		assert.equals(ast[0]['function']['name'], 'Klass');
+		assert.equals(ast[0]['function']['args'].length, 2);
+		assert.equals(ast[0]['function']['args'][0], 'num');
+		assert.equals(ast[0]['function']['args'][1], 'num2');
 	}
 });
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.