Permalink
Browse files

An example to find out token distribution.

  • Loading branch information...
1 parent 3d9465d commit 25af36448e0b29a5a265fcfb0b93be81aa185464 @ariya committed Jul 18, 2012
Showing with 33 additions and 0 deletions.
  1. +33 −0 examples/tokendist.js
View
33 examples/tokendist.js
@@ -0,0 +1,33 @@
+/*jslint node:true */
+var fs = require('fs'),
+ esprima = require('../esprima'),
+ files = process.argv.splice(2),
+ histogram,
+ type;
+
+histogram = {
+ Boolean: 0,
+ Identifier: 0,
+ Keyword: 0,
+ Null: 0,
+ Numeric: 0,
+ Punctuator: 0,
+ RegularExpression: 0,
+ String: 0
+};
+
+files.forEach(function (filename) {
+ 'use strict';
+ var content = fs.readFileSync(filename, 'utf-8'),
+ tokens = esprima.parse(content, { tokens: true }).tokens;
+
+ tokens.forEach(function (token) {
+ histogram[token.type] += 1;
+ });
+});
+
+for (type in histogram) {
+ if (histogram.hasOwnProperty(type)) {
+ console.log(type, histogram[type]);
+ }
+}

0 comments on commit 25af364

Please sign in to comment.