Skip to content

Commit

Permalink
add comment parsing feature
Browse files Browse the repository at this point in the history
  • Loading branch information
59naga committed Apr 24, 2016
1 parent 6097358 commit b8b5c31
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
39 changes: 28 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ function parse (s, env, opts) {
'(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')*'
].join('|'), 'g');
var match = filter(s.match(chunker), Boolean);
var commented = false;

if (!match) return [];
if (!env) env = {};
if (!opts) opts = {};
return map(match, function (s) {
return map(match, function (s, j) {
if (commented) {
return;
}
if (RegExp('^' + CONTROL + '$').test(s)) {
return { op: s };
}
Expand All @@ -80,14 +84,13 @@ function parse (s, env, opts) {
var DS = '$';
var BS = opts.escape || '\\';
var quote = false;
var varname = false;
var esc = false;
var out = '';
var isGlob = false;

for (var i = 0, len = s.length; i < len; i++) {
var c = s.charAt(i);
isGlob = isGlob || (!quote && (c === '*' || c === '?'))
isGlob = isGlob || (!quote && (c === '*' || c === '?'));
if (esc) {
out += c;
esc = false;
Expand All @@ -113,18 +116,25 @@ function parse (s, env, opts) {
out += parseEnvVar();
}
else {
out += c
out += c;
}
}
}
else if (c === DQ || c === SQ) {
quote = c
quote = c;
}
else if (RegExp('^' + CONTROL + '$').test(c)) {
return { op: s };
}
else if (RegExp('^#$').test(c)) {
commented = true;
if (out.length){
return [out, { comment: s.slice(i+1) + match.slice(j+1).join(' ') }];
}
return [{ comment: s.slice(i+1) + match.slice(j+1).join(' ') }];
}
else if (c === BS) {
esc = true
esc = true;
}
else if (c === DS) {
out += parseEnvVar();
Expand All @@ -134,14 +144,14 @@ function parse (s, env, opts) {

if (isGlob) return {op: 'glob', pattern: out};

return out
return out;

function parseEnvVar() {
i += 1;
var varend, varname;
//debugger
if (s.charAt(i) === '{') {
i += 1
i += 1;
if (s.charAt(i) === '}') {
throw new Error("Bad substitution: " + s.substr(i - 2, 3));
}
Expand All @@ -162,13 +172,20 @@ function parse (s, env, opts) {
varname = s.substr(i);
i = s.length;
} else {
varname = s.substr(i, varend.index)
varname = s.substr(i, varend.index);
i += varend.index - 1;
}
}
return getVar(null, '', varname);
}
});
})
// finalize parsed aruments
.reduce(function(prev, arg){
if (arg === undefined){
return prev;
}
return prev.concat(arg);
},[]);

function getVar (_, pre, key) {
var r = typeof env === 'function' ? env(key) : env[key];
Expand All @@ -179,4 +196,4 @@ function parse (s, env, opts) {
}
else return pre + r;
}
};
}
14 changes: 14 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ output:
[ 'beep', { op: '||' }, 'boop', { op: '>' }, '/byte' ]
```

## parsing shell comment

``` js
var parse = require('shell-quote').parse;
var xs = parse('beep > boop # > kaboom');
console.dir(xs);
```

output:

```
[ 'beep', { op: '>' }, 'boop', { comment: '> kaboom' } ]
```

# methods

``` js
Expand Down
14 changes: 14 additions & 0 deletions test/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var test = require('tape');
var parse = require('../').parse;

test('comment', function (t) {
t.same(parse('beep#boop'), [ 'beep', { comment: 'boop' } ]);
t.same(parse('beep #boop'), [ 'beep', { comment: 'boop' } ]);
t.same(parse('beep # boop'), [ 'beep', { comment: 'boop' } ]);
t.same(parse('beep # > boop'), [ 'beep', { comment: '> boop' } ]);
t.same(parse('beep # "> boop"'), [ 'beep', { comment: '"> boop"' } ]);
t.same(parse('beep "#"'), [ 'beep', '#' ]);
t.same(parse('beep #"#"#'), [ 'beep', { comment: '"#"#' } ]);
t.same(parse('beep > boop # > foo'), [ 'beep', {op: '>'}, 'boop', { comment: '> foo' } ]);
t.end();
});

0 comments on commit b8b5c31

Please sign in to comment.