Permalink
Browse files

Little cleanup

Just some minor changes.
  • Loading branch information...
1 parent 6679b24 commit 3905b81abe0b16a6bf7fb5f9da89695a57250d61 Ryan Patterson committed Jan 10, 2012
Showing with 31 additions and 30 deletions.
  1. +13 −8 jsgrep
  2. +18 −22 lib/jsgrep.js
View
21 jsgrep
@@ -1,10 +1,11 @@
#!/usr/bin/env node
+var Narcissus = require('narcissus/main');
+var _ = require('underscore');
var fs = require('fs');
var path = require('path');
-var _ = require('underscore');
-var Narcissus = require('narcissus/main');
+var jsgrep = require('./lib/jsgrep.js');
/// Used to print the matching snippet rather than the first line of it.
const PRINT_FIRST_LINE = 'first-line';
@@ -126,10 +127,14 @@ function usage(hasError) {
}
})();
-var patternsAst = [];
-for (var i = 0; i < config.patterns.length; i++) {
- patternsAst[i] =
- require('./lib/jsgrep.js').Matcher.compilePattern(config.patterns[i]);
+try {
+ var patternsAst = [];
+ for (var i = 0; i < config.patterns.length; i++) {
+ patternsAst[i] = jsgrep.Matcher.compilePattern(config.patterns[i]);
+ }
+} catch(e) {
+ console.error(e.name + ": " + e.message);
+ process.exit(1);
}
if (config.dumpAst) {
@@ -156,12 +161,12 @@ function doFile(filename) {
try {
var ast = Narcissus.parser.parse(source, filename, 1);
} catch(e) {
- console.warn(e.message);
+ console.warn(e.name + ": " + e.message);
return;
}
try {
- require('./lib/jsgrep.js').jsgrep({
+ jsgrep.jsgrep({
source: ast,
patterns: patternsAst,
matchScript: config.matchScript,
View
@@ -90,7 +90,7 @@ function tokenString(tt) {
* - patterns: Array of AST or String. List of patterns to match with.
* - pattern: AST or String. Single pattern to match with.
* - matchScript: String. Path to a file, to be evaluated as a match script.
- * - strictMatches: Boolean. Controls strict matching.
+ * - strictMatches: Boolean. Controls strict matching (not for matchScript).
* - callback: Function. Called with (node, metavariables) when any pattern
* matches an AST node.
*/
@@ -99,7 +99,7 @@ var jsgrep = exports.jsgrep = function(options) {
if (!ast) {
ast = fs.readFileSync(options.filename).toString();
}
- if (ast instanceof String) {
+ if (typeof ast === 'string') {
ast = Narcissus.parser.parse(ast, options.filename, 1);
}
@@ -118,34 +118,30 @@ var jsgrep = exports.jsgrep = function(options) {
}
matchFn = eval("(function custom_matcher(jsgrep) {" + matchScript + "})");
+ try {
+ matchFn(matcher);
+ } catch(e) {
+ if (e instanceof Error) {
+ throw {
+ message: 'Matcher threw exception\n' + e.stack
+ };
+ } else {
+ throw e;
+ }
+ }
} else {
var patterns = options.patterns || [ options.pattern ];
for (var i = 0; i < patterns.length; i++) {
- if (patterns[i] instanceof String) {
- patterns[i] = Narcissus.parser.parse(patterns[i], 'pattern', 0);
+ if (typeof patterns[i] === 'string') {
+ patterns[i] = Matcher.compilePattern(patterns[i]);
}
}
matcher.options.strictMatches = options.strictMatches;
-
- var matchFn = function(matcher) {
- _.each(patterns, function(pattern) {
- matcher.find(pattern);
- });
- };
- }
-
- try {
- matchFn(matcher);
- } catch(e) {
- if (e instanceof Error) {
- throw {
- message: 'Matcher threw exception\n' + e.stack
- };
- } else {
- throw e;
- }
+ _.each(patterns, function(pattern) {
+ matcher.find(pattern);
+ });
}
}

0 comments on commit 3905b81

Please sign in to comment.