bash-parser
Advanced tools
Comparing version 0.3.2 to 0.4.0
{ | ||
"name": "bash-parser", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"main": "src/index.js", | ||
@@ -10,6 +10,7 @@ "description": "Standard compliant bash parser", | ||
"scripts": { | ||
"test": "ava --no-babel && xo --ignore grammar.js --ignore 'packages/**/*'", | ||
"build": "jison src/grammar.jison > grammar.js", | ||
"cover-test": "nyc ava --no-babel && xo --ignore grammar.js --ignore 'packages/**/*'", | ||
"build-site": "browserify docs/index.js -o docs/bundle.js -t [ babelify --presets [ es2015 ] ] -t uglifyify" | ||
"doc": "doctoc", | ||
"test": "ava --no-babel && xo --ignore **/built-grammar.js --ignore 'packages/**/*'", | ||
"build": "mgb ./src/modes posix && mgb ./src/modes bash", | ||
"cover-test": "nyc ava --no-babel && xo --ignore **/built-grammar.js --ignore 'packages/**/*'", | ||
"cover-publish": "nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
@@ -22,23 +23,15 @@ "keywords": [], | ||
"ava": "^0.15.2", | ||
"babel-polyfill": "^6.13.0", | ||
"babel-preset-es2015": "^6.14.0", | ||
"babelify": "^7.3.0", | ||
"bash-ast-traverser": "file:packages/bash-ast-traverser", | ||
"browserify": "^13.1.0", | ||
"coveralls": "^2.11.11", | ||
"jison": "^0.4.17", | ||
"doctoc": "^1.2.0", | ||
"json5": "^0.5.0", | ||
"mode-grammar-builder": "0.0.0", | ||
"nyc": "^7.0.0", | ||
"rus-diff": "^1.1.0", | ||
"tree-walk": "^0.4.0", | ||
"uglifyify": "^3.0.2", | ||
"xo": "^0.16.0" | ||
}, | ||
"files": [ | ||
"src", | ||
"grammar.js" | ||
"src" | ||
], | ||
"nyc": { | ||
"exclude": [ | ||
"grammar.js", | ||
"src/modes/posix/built-grammar.js", | ||
"test/_utils.js" | ||
@@ -48,13 +41,24 @@ ] | ||
"dependencies": { | ||
"array-last": "^1.1.1", | ||
"babylon": "^6.9.1", | ||
"compose-function": "^3.0.3", | ||
"curry": "^1.2.0", | ||
"deep-freeze": "0.0.1", | ||
"filter-iterator": "0.0.1", | ||
"filter-obj": "^1.1.0", | ||
"has-own-property": "^0.1.0", | ||
"identity-function": "^1.0.0", | ||
"iterable-lookahead": "^1.0.0", | ||
"iterable-transform-replace": "^1.1.1", | ||
"magic-string": "^0.16.0", | ||
"map-iterable": "^1.0.1", | ||
"map-obj": "^2.0.0", | ||
"object-pairs": "^0.1.0", | ||
"object-values": "^1.0.0", | ||
"reverse-arguments": "^1.0.0", | ||
"shell-quote-word": "^1.0.1", | ||
"to-pascal-case": "^1.0.0", | ||
"transform-spread-iterable": "^1.1.0", | ||
"unescape-js": "^1.0.5" | ||
} | ||
} |
# bash-parser | ||
Parses bash source code to produce an AST | ||
[![Travis Build Status](https://img.shields.io/travis/vorpaljs/bash-parser.svg)](http://travis-ci.org/vorpaljs/bash-parser) | ||
[![Coveralls](https://img.shields.io/coveralls/vorpaljs/bash-parser.svg?maxAge=2592000)](https://coveralls.io/github/vorpaljs/bash-parser) | ||
[![NPM module](https://img.shields.io/npm/v/bash-parser.svg)](https://npmjs.org/package/bash-parser) | ||
[![NPM downloads](https://img.shields.io/npm/dt/bash-parser.svg)](https://npmjs.org/package/bash-parser) | ||
[![Try online](https://img.shields.io/badge/try_it-online!-yellow.svg)](https://vorpaljs.github.io/bash-parser/) | ||
# Installation | ||
```bash | ||
npm install --save bash-parser | ||
``` | ||
## Usage | ||
```js | ||
const parse = require('bash-parser'); | ||
const ast = parse('echo foo>file.txt'); | ||
``` | ||
`ast` is of the following form: | ||
```js | ||
{ | ||
type: 'list', | ||
andOrs: [{ | ||
type: 'andOr', | ||
left: [{ | ||
type: 'simple_command', | ||
name: 'echo', | ||
suffix: { | ||
type: 'cmd_suffix', | ||
list: [ | ||
'foo', | ||
{ | ||
type: 'io_redirect', | ||
op: '>', | ||
file: 'file.txt' | ||
} | ||
] | ||
} | ||
}] | ||
}] | ||
} | ||
``` | ||
# Related projects | ||
* [cash](https://github.com/dthree/cash) - This parser should become the parser used by `cash` (and also [vorpal](https://github.com/dthree/vorpal)) | ||
* [nsh](https://github.com/piranna/nsh) - This parser should become the parser used by `nsh` | ||
* [js-shell-parse](https://github.com/grncdr/js-shell-parse) - bash-parser was born as a fork of `js-shell-parse`, but was rewritten to use a `jison` grammar | ||
* [jison](https://github.com/zaach/jison) - Bison in JavaScript. | ||
# License | ||
The MIT License (MIT) | ||
Copyright (c) 2016 vorpaljs | ||
> See [repo root readme](https://github.com/vorpaljs/bash-parser) |
'use strict'; | ||
// const walk = require('tree-walk'); | ||
// const assert = require('assert'); | ||
// const traverse = require('bash-ast-traverser'); | ||
const Parser = require('../grammar.js').Parser; | ||
const posixShellLexer = require('./posix-shell-lexer'); | ||
const astBuilder = require('./ast-builder'); | ||
/* eslint-disable camelcase */ | ||
/* | ||
## options | ||
const shellLexer = require('./shell-lexer'); | ||
const utils = require('./utils'); | ||
* insertLOC: Boolean = false - whether to track line and column information for tokens | ||
*/ | ||
// preload all modes to have them browserified | ||
const modes = { | ||
'bash': require('./modes/bash'), | ||
'posix': require('./modes/posix'), | ||
'word-expansion': require('./modes/word-expansion') | ||
}; | ||
function loadPlugin(name) { | ||
const modePlugin = modes[name]; | ||
if (modePlugin.inherits) { | ||
return modePlugin.init(loadPlugin(modePlugin.inherits), utils); | ||
} | ||
return modePlugin.init(null, utils); | ||
} | ||
module.exports = function parse(sourceCode, options) { | ||
try { | ||
options = options || {}; | ||
options.mode = options.mode || 'posix'; | ||
const mode = loadPlugin(options.mode); | ||
const Parser = mode.grammar.Parser; | ||
const astBuilder = mode.astBuilder; | ||
const parser = new Parser(); | ||
parser.lexer = posixShellLexer(options); | ||
parser.lexer = shellLexer(mode, options); | ||
parser.yy = astBuilder(options); | ||
const ast = parser.parse(sourceCode); | ||
/* | ||
walk.preorder(ast, (value, key, parent) => { | ||
if (value !== null && typeof value === 'object') { | ||
const fixtureFolder = `${__dirname}/../test/fixtures`; | ||
const json = require('json5'); | ||
const {writeFileSync} = require('fs'); | ||
console.log(Object.getPrototypeOf(value)); | ||
} | ||
}); | ||
traverse(ast, { | ||
simple_command(node) { | ||
if (node.name.text !== '') { | ||
const expectAliasCheck = | ||
node.name.maybeSimpleCommandName || | ||
node.name.text.indexOf('$') !== -1 || | ||
node.name.text[0].match(/[0-9]/); | ||
assert.ok(expectAliasCheck, `expected simple_command name ${JSON.stringify(node, null, 2)}`); | ||
} | ||
delete node.name.maybeSimpleCommandName; | ||
}, | ||
defaultMethod(node) { | ||
assert.ok(!node.maybeSimpleCommandName, `simple_command name not expected ${JSON.stringify(node, null, 2)}`); | ||
delete node.maybeSimpleCommandName; | ||
} | ||
}); | ||
const fileName = require('node-uuid').v4(); | ||
const filePath = `${fixtureFolder}/${fileName}.js`; | ||
writeFileSync(filePath, 'module.exports = ' + json.stringify({ | ||
sourceCode, result: ast | ||
}, null, '\t')); | ||
*/ | ||
@@ -49,0 +47,0 @@ return ast; |
@@ -86,4 +86,4 @@ /* parser generated by jison 0.4.17 */ | ||
switch (yystate) { | ||
case 1: case 4: | ||
return $$[$0-2] | ||
case 1: | ||
return yy.checkAsync($$[$0-2], $$[$0-1]) | ||
break; | ||
@@ -93,4 +93,7 @@ case 2: case 3: | ||
break; | ||
case 4: | ||
return yy.checkAsync($$[$0-2], $$[$0-3]) | ||
break; | ||
case 5: | ||
this.$ = yy.listAppend($$[$0-2], $$[$0]); | ||
this.$ = yy.listAppend($$[$0-2], $$[$0], $$[$0-1]); | ||
break; | ||
@@ -125,6 +128,6 @@ case 6: | ||
case 28: case 29: | ||
this.$ = $$[$0-1]; | ||
this.$ = yy.checkAsync($$[$0-1], $$[$0]); | ||
break; | ||
case 30: | ||
this.$ = yy.termAppend($$[$0-2], $$[$0]); | ||
this.$ = yy.termAppend($$[$0-2], $$[$0], $$[$0-1]); | ||
break; | ||
@@ -222,3 +225,3 @@ case 31: | ||
case 71: | ||
this.$ =yy.command($$[$0], {text:"", type:"word"}); | ||
this.$ =yy.commandAssignment($$[$0]); | ||
break; | ||
@@ -225,0 +228,0 @@ case 72: |
@@ -9,3 +9,3 @@ /* eslint-disable max-lines */ | ||
'list separator EOF', | ||
' return $list ' | ||
' return yy.checkAsync($list, $separator)' | ||
], | ||
@@ -22,3 +22,3 @@ [ | ||
'separator list separator EOF', | ||
' return $list ' | ||
' return yy.checkAsync($list, $separator)' | ||
] | ||
@@ -29,3 +29,3 @@ ], | ||
'list separator and_or', | ||
'$$ = yy.listAppend($list, $and_or);' | ||
'$$ = yy.listAppend($list, $and_or, $separator);' | ||
], | ||
@@ -103,7 +103,7 @@ [ | ||
'term separator', | ||
'$$ = $term;' | ||
'$$ = yy.checkAsync($term, $separator);' | ||
], | ||
[ | ||
'NEWLINE_LIST term separator', | ||
'$$ = $term;' | ||
'$$ = yy.checkAsync($term, $separator);' | ||
] | ||
@@ -114,3 +114,3 @@ ], | ||
'term separator and_or', | ||
'$$ = yy.termAppend($term, $and_or);' | ||
'$$ = yy.termAppend($term, $and_or, $separator);' | ||
], | ||
@@ -302,3 +302,3 @@ [ | ||
'cmd_prefix', | ||
'$$ =yy.command($cmd_prefix, {text:"", type:"word"});' | ||
'$$ =yy.commandAssignment($cmd_prefix);' | ||
], | ||
@@ -305,0 +305,0 @@ [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
131736
7
61
3652
21
4
4
1
+ Addedarray-last@^1.1.1
+ Addedcurry@^1.2.0
+ Addeddeep-freeze@0.0.1
+ Addedfilter-iterator@0.0.1
+ Addedfilter-obj@^1.1.0
+ Addedidentity-function@^1.0.0
+ Addedmap-obj@^2.0.0
+ Addedreverse-arguments@^1.0.0
+ Addedto-pascal-case@^1.0.0
+ Addedarray-last@1.3.0(transitive)
+ Addeddeep-freeze@0.0.1(transitive)
+ Addedfilter-iterator@0.0.1(transitive)
+ Addedfilter-obj@1.1.0(transitive)
+ Addedidentity-function@1.0.0(transitive)
+ Addedis-number@4.0.0(transitive)
+ Addediterable-transform-replace@1.2.0(transitive)
+ Addedmap-obj@2.0.0(transitive)
+ Addedreverse-arguments@1.0.0(transitive)
+ Addedto-no-case@1.0.2(transitive)
+ Addedto-pascal-case@1.0.0(transitive)
+ Addedto-space-case@1.0.0(transitive)
+ Addedtransform-spread-iterable@1.4.1(transitive)