Code coverage report for lib/parser/commands/grep.js

Statements: 100% (51 / 51)      Branches: 95% (19 / 20)      Functions: 100% (7 / 7)      Lines: 100% (48 / 48)      Ignored: none     

All files » lib/parser/commands/ » grep.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183                                      1 1 1 1 1   1 1 1 1 1   1                                                                                                         1                             1                                             1   1         1   1 1 1 76 76 76 76   1   1   1 72 72 72 72       1   106 67   39         1 40 40       40 1   40 18 22 19 3 1   2     1 1  
/*
grep:
Matcher Selection:
arguments:
- ["E","--extended-regexp","Interpret PATTERN as an extended regular expression"]
- ["F","--fixed-strings","Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched."]
- ["G","--basic-regexp","Interpret PATTERN as a basic regular expression (BRE, see below).  This is the default."]
- ["P","--perl-regexp","display $ at end of each line"]
Matching Control:
arguments:
- ["e PATTERN","--regexp=PATTERN","Use PATTERN as the pattern.  This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-)."]
- ["f FILE","--file=FILE","Obtain patterns from FILE, one per line.  The empty file contains zero patterns, and therefore matches nothing."]
- ["i","--ignore-case","Ignore case distinctions in both the PATTERN and the input files."]
- ["v","--invert-match","Invert the sense of matching, to select non-matching lines."]
- ["w","--word-regexp"," Select only those lines containing matches that form whole words.  The test is that the matching substring must either be at the beginning of the line, or preceded by a non-
word constituent character.  Similarly, it must be either at the end of the line or followed by a non-word constituent character.  Word-constituent characters  are  letters,
digits, and the underscore."]
- ["x","--line-regexp","Select only those matches that exactly match the whole line."]
*/
var __extends = this.__extends || function (d, b) {
    for (var p in b) Eif (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var $ = require("../utils/optionsParser");
var parserModule = require("../utils/parserData");
var common = require("../utils/init");
var sanitizer = require("../utils/sanitizer");
var GraphModule = require("../../common/graph");
 
var selectors = {
    patternType: {
        name: "pattern type",
        description: "define the pattern to filter",
        options: {
            extRegex: {
                name: "extended regexp",
                option: "E",
                type: 'option',
                description: 'use pattern as an extended regular expression'
            },
            fixedStrings: {
                name: "fixed strings",
                option: "F",
                type: 'option',
                description: 'use pattern as a set of expressions separated by lines'
            },
            basicRegex: {
                name: "basic regexp",
                option: null,
                type: 'option',
                description: 'use pattern as a basic regular expression',
                default: true
            }
        }
    },
    match: {
        name: "match",
        description: "",
        options: {
            default: {
                name: "default",
                option: null,
                type: 'option',
                description: 'do not force the pattern to filter complete words or lines',
                default: true
            },
            word: {
                name: "whole word",
                option: "F",
                type: 'option',
                description: 'force the pattern to filter complete words'
            },
            line: {
                name: "whole line",
                option: null,
                type: 'option',
                description: 'force the pattern to filter complete lines'
            }
        }
    }
};
 
var flags = {
    ignoreCase: {
        name: "ignore case",
        option: 'T',
        description: "print TAB characters like ^I",
        active: false
    },
    invertMatch: {
        name: "invert match",
        option: 'E',
        description: "print $ after each line",
        active: false
    }
};
 
var optionsParser = {
    shortOptions: {
        E: $.select(selectors.patternType, selectors.patternType.options.extRegex),
        F: $.select(selectors.patternType, selectors.patternType.options.fixedStrings),
        G: $.select(selectors.patternType, selectors.patternType.options.basicRegex),
        i: $.switchOn(flags.ignoreCase),
        //P  :  $.select(selectors.patternType, patternTypeSelector.perlRegex),
        v: $.switchOn(flags.invertMatch),
        x: $.select(selectors.match, selectors.match.options.line),
        w: $.selectIfUnselected(selectors.match.name, selectors.match.options.word.name, selectors.match.options.line.name),
        y: $.switchOn(flags.ignoreCase)
    },
    longOptions: {
        "extended-regexp": $.sameAs("E"),
        "fixed-strings": $.sameAs("F"),
        "basic-regexp": $.sameAs("G"),
        "perl-regexp": $.sameAs("P"),
        "ignore-case": $.sameAs("i"),
        "invert-match": $.sameAs("v"),
        "word-regexp": $.sameAs("w"),
        "line-regexp": $.sameAs("x")
    }
};
$.generate(optionsParser);
 
var config = {
    selectors: selectors,
    flags: flags
};
 
var grepCommandData = new parserModule.ParserData(config);
 
var GrepComponent = (function (_super) {
    __extends(GrepComponent, _super);
    function GrepComponent() {
        _super.apply(this, arguments);
        this.exec = "grep";
        this.files = [];
        this.pattern = null;
    }
    return GrepComponent;
})(GraphModule.CommandComponent);
exports.GrepComponent = GrepComponent;
 
function defaultComponentData() {
    var graph = new GrepComponent();
    graph.selectors = grepCommandData.componentSelectors;
    graph.flags = grepCommandData.componentFlags;
    return graph;
}
;
 
exports.parseCommand = common.commonParseCommand(optionsParser, defaultComponentData, {
    string: function (component, str) {
        if (component.pattern === null) {
            component.pattern = str || "";
        } else {
            return "continue";
        }
    }
});
 
exports.parseComponent = common.commonParseComponent(grepCommandData.flagOptions, grepCommandData.selectorOptions, null, function (component, exec, flags, files) {
    var pattern = component.pattern || "";
    pattern = sanitizer.sanitizeArgument(pattern);
 
    //console.error(pattern + " - " + files.length );
    //console.error(!!pattern + " - " + !!files.length );
    if (flags) {
        exec = exec.concat(flags);
    }
    if (pattern && files.length) {
        return exec.concat(pattern, files).join(' ');
    } else if (pattern) {
        return exec.concat(pattern, files).join(' ');
    } else if (files.length) {
        return exec.concat('""', files).join(' ');
    } else
        return exec.join(' ');
});
 
exports.VisualSelectorOptions = grepCommandData.visualSelectorOptions;
exports.componentClass = GrepComponent;