diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..825fc67 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +.DS_Store +npm-debug.log diff --git a/.npmignore b/.npmignore index c2658d7..9daeafb 100644 --- a/.npmignore +++ b/.npmignore @@ -1 +1 @@ -node_modules/ +test diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..74efbbf --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,10 @@ +The MIT License (MIT) +===================== + +Copyright (c) 2014 [Chris Dickinson](http://github.com/chrisdickinson) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 981da96..2c3036e 100644 --- a/README.md +++ b/README.md @@ -1,46 +1,68 @@ # glsl-deparser -```javascript +![](http://img.shields.io/badge/stability-stable-green.svg?style=flat) +![](http://img.shields.io/npm/v/glsl-deparser.svg?style=flat) +![](http://img.shields.io/npm/dm/glsl-deparser.svg?style=flat) +![](http://img.shields.io/npm/l/glsl-deparser.svg?style=flat) -var Path = require('path') +Transform the AST output from [glsl-parser](http://github.com/stackgl/glsl-parser) +into strings. + +Only operates on top-level statements emitted by `glsl-parser`, so the code it +emits is executable by WebGL. + +## API + +### stream = require('glsl-deparser/stream')(opts) + +Creates a `readable`/`writable` stream. + +The following options are available: + +* `whitespace`: passing this as `false` will ensure that only syntactically + significatn whitespace will be emitted. (It'll behave like a poor man's + minifier). Defaults to `true`. +* `indent`: assuming that `whitespace` is enabled, use the `indent` string + to indent the deparsed GLSL. Defaults to `' '`. -var tokenizer = require('glsl-tokenizer')() - , parser = require('glsl-parser') - , deparser = require('glsl-deparser') +``` javascript +var tokenizer = require('glsl-tokenizer/stream') +var parser = require('glsl-parser/stream') +var deparser = require('glsl-deparser/stream') process.stdin - .pipe(tokenizer) + .pipe(tokenizer()) .pipe(parser()) - .pipe(deparser()) // <-- deparser! + .pipe(deparser()) .pipe(process.stdout) process.stdin.resume() - ``` -transform a stream of [glsl-parser](https://github.com/chrisdickinson/glsl-parser) AST nodes -into strings. - -only operates on top-level statements emitted by `glsl-parser`, so the code it emits is executable -by webgl. +### string = require('glsl-deparser/direct')(ast, opts) -# api +Takes an AST produced by [glsl-parser](http://github.com/stackgl/glsl-parser) +and returns the deparsed GLSL. Accepts the same options listed above. -### deparser(whitespace_enabled=true, tab_text=' ') +``` javascript +var tokenize = require('glsl-tokenizer/string') +var parse = require('glsl-parser/direct') +var deparse = require('glsl-deparse/direct') -Creates a `readable`/`writable` stream. +function reformat(inputSrc) { + var tokens = tokenize(inputSrc) + var ast = parse(tokens) + var outputSrc = deparse(ast) -If no args are provided, `whitespace` is assumed to be enabled, and the tab text will be `' '`. - -If you pass `false` for the first arg, only syntactically significant whitespace will be emitted (it'll behave like a poor man's minifier). - -If you pass `true` and tab text, that tab text will be used to indent code. + return outputSrc +} +``` -# note +## Note -the big caveat is that preprocessor if statements (`#if*`, `#endif`) won't work unless -each branch produces a parseable tree. +The big caveat is that preprocessor if statements (`#if*`, `#endif`) won't work +unless each branch produces a parseable tree. -# license +## License -MIT +MIT. See [LICENSE.md](LICENSE.md) diff --git a/direct.js b/direct.js new file mode 100644 index 0000000..c499bd2 --- /dev/null +++ b/direct.js @@ -0,0 +1,15 @@ +var Deparser = require('./lib/index') + +module.exports = deparseDirect + +function deparseDirect(ast, opts) { + var deparser = Deparser(opts) + var children = ast.children + var output = [] + + for (var i = 0; i < children.length; i++) { + output.push(deparser(children[i])) + } + + return output.join('') +} diff --git a/lib/index.js b/lib/index.js index 3fdd263..4376506 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,7 +1,6 @@ module.exports = deparse_stream -var through = require('through') - , language = require('cssauron-glsl') +var language = require('cssauron-glsl') , WSManager = require('./ws') var types = @@ -53,19 +52,24 @@ var needs_semicolon = { var output = [] , ws -function deparse_stream(with_whitespace, indent) { - with_whitespace = with_whitespace === undefined ? true : with_whitespace +function deparse_stream(opts, indent) { + if (typeof opts === 'boolean') opts = { whitespace: opts } + opts = opts || {} - var stream = through(recv, end) - , whitespace = new WSManager(with_whitespace, indent || ' ') + var wsc = 'whitespace' in opts ? opts.whitespace : true + var whitespace = new WSManager(wsc, opts.indent || indent || ' ') + handler.parseable = language(':root > *') - stream.parseable = language(':root > *') - - return stream + return handler + + function handler(node) { + if (node === null) return null + return recv(node) + } function recv(node) { - if(!stream.parseable(node)) return + if(!handler.parseable(node)) return '' // reuse the old array. output.length = 0 @@ -74,11 +78,7 @@ function deparse_stream(with_whitespace, indent) { deparse(node) - stream.queue(output.join('')) - } - - function end() { - stream.queue(null) + return output.join('') } } @@ -198,7 +198,7 @@ function deparse_expr(node) { } function deparse_forloop(node) { - var is_stmtlist = node.children[3].type === 'stmtlist' + var is_stmtlist = node.children[3].type === 'stmtlist' output.push('for(') deparse(node.children[0]) @@ -243,7 +243,7 @@ function deparse_functionargs(node) { output.push(',') output.push(ws.optional(' ')) } - } + } } function deparse_ident(node) { @@ -303,7 +303,7 @@ function deparse_if(node) { !is_if_stmt && ws.dedent() output.push(ws.optional('\n')) } - } + } } function deparse_keyword(node) { @@ -377,11 +377,11 @@ function deparse_stmt(node) { function deparse_stmtlist(node) { var has_parent = node.parent !== null - + if(has_parent) { output.push('{') ws.indent() - output.push(ws.optional('\n')) + output.push(ws.optional('\n')) } for(var i = 0, len = node.children.length; i < len; ++i) { @@ -448,7 +448,7 @@ function deparse_whileloop(node) { function deparse_call(node) { var len = node.children.length , len_minus_one = len - 1 - + deparse(node.children[0]) output.push('(') for(var i = 1; i < len; ++i) { @@ -458,7 +458,7 @@ function deparse_call(node) { output.push(ws.optional(' ')) } } - output.push(')') + output.push(')') } function deparse_operator(node) { diff --git a/package.json b/package.json index 81fbbd5..565bb1f 100644 --- a/package.json +++ b/package.json @@ -2,17 +2,23 @@ "name": "glsl-deparser", "version": "1.0.0", "description": "through stream that translates glsl-parser AST nodes into working glsl code", - "main": "index.js", + "main": "stream.js", "directories": { "test": "test" }, "dependencies": { "cssauron-glsl": "X.X.X", - "through": "~1.1.2" + "through2": "^0.6.3" + }, + "devDependencies": { + "glsl-parser": "git://github.com/stackgl/glsl-parser#2.0.0", + "glsl-tokenizer": "git://github.com/stackgl/glsl-tokenizer#2.0.0", + "new-from": "0.0.3", + "tap-spec": "^1.0.1", + "tape": "^3.0.2" }, - "devDependencies": {}, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node test/index | tap-spec" }, "repository": { "type": "git", diff --git a/stream.js b/stream.js new file mode 100644 index 0000000..1fee7f2 --- /dev/null +++ b/stream.js @@ -0,0 +1,20 @@ +var Deparser = require('./lib/index') +var through = require('through2').obj + +module.exports = DeparseStream + +function DeparseStream(opts) { + var deparser = Deparser(opts) + var stream = through(write, flush) + + return stream + + function write(data, _, next) { + this.push(deparser(data)) + next() + } + + function flush() { + this.push(null) + } +} diff --git a/test/index.js b/test/index.js index fa41a24..d7a63a6 100644 --- a/test/index.js +++ b/test/index.js @@ -1,29 +1,120 @@ -var cssauron_config = { - tag: 'type' -, parent: 'parent' -, children: 'children' -} - -var fs = require('fs') - , Path = require('path') - -var tokenizer = require('glsl-tokenizer')() - , parser = require('glsl-parser') - , lang = require('cssauron')(cssauron_config) - , deparser = require('../index') - , path = Path.join(__dirname, 'working.glsl') - , parse_stream = parser(select) - , is_global_variable = lang(':root > stmt > decl decllist ident') - , is_global_function = lang(':root > stmt > decl function > ident') - , is_struct = lang(':root > stmt > struct > ident') - , lang - -function select(x) { - return is_global_variable(x) || is_global_function(x) || is_struct(x) -} - -fs.createReadStream(path) - .pipe(tokenizer) - .pipe(parser()) - .pipe(deparser(true)) - .pipe(process.stdout) +var TokenStream = require('glsl-tokenizer/stream') +var TokenString = require('glsl-tokenizer/string') +var ParseDirect = require('glsl-parser/direct') +var ParseStream = require('glsl-parser/stream') +var DeparseStream = require('../stream') +var DeparseDirect = require('../direct') + +var from = require('new-from') +var test = require('tape') +var path = require('path') +var fs = require('fs') + +var file = path.join(__dirname, 'working.glsl') + +test('sanity check', function(t) { + var deparser = DeparseStream({ whitespace: true }) + var tokenizer = TokenStream() + var parser = ParseStream() + var buffer1 = '' + + t.plan(6) + + fs.createReadStream(file) + .pipe(tokenizer) + .pipe(parser) + .pipe(deparser) + .on('data', function(d) { buffer1 += d }) + .once('end', function() { + var parser = ParseStream() + var ast1 = parser.program + var tokenizer = TokenStream() + var buffer2 = '' + + t.pass('worked the first time') + + from([buffer1]) + .pipe(tokenizer) + .pipe(parser) + .once('end', function() { + var ast2 = parser.program + t.deepEqual(ast1, ast2, 'AST of original source and deparsed source match') + }) + .pipe(DeparseStream({ whitespace: true })) + .on('data', function(d) { buffer2 += d }) + .once('end', function() { + t.pass('worked the second time') + t.ok(buffer1, 'first buffer has content') + t.ok(buffer2, 'second buffer has content') + t.equal(buffer1, buffer2, 'deparse of matching ASTs matches') + }) + }) +}) + +test('deparsing a single program object', function(t) { + var src = fs.readFileSync(file) + var tokens = TokenString(src) + var ast = ParseDirect(tokens) + var programStream = from(ast.children, { objectMode: true }) + var buffer1 = '' + + programStream + .pipe(DeparseStream(true)) + .on('data', function(d) { buffer1 += d }) + .once('end', function() { + var buffer2 = '' + + fs.createReadStream(file) + .pipe(TokenStream()) + .pipe(ParseStream()) + .pipe(DeparseStream(true)) + .on('data', function(d) { buffer2 += d }) + .once('end', function() { + t.ok(buffer1, 'single object buffer has content') + t.ok(buffer2, 'whole tree buffer has content') + t.equal(buffer1, buffer2, 'single object and whole tree streams match') + t.end() + }) + }) +}) + +test('whitespace flag alters output', function(t) { + var src = fs.readFileSync(file) + var tokens = TokenString(src) + var ast = ParseDirect(tokens) + var programStream1 = from(ast.children, { objectMode: true }) + var programStream2 = from(ast.children, { objectMode: true }) + var buffer1 = '' + var buffer2 = '' + + programStream1 + .pipe(DeparseStream(true)) + .on('data', function(d) { buffer1 += d }) + .once('end', function() { + programStream2 + .pipe(DeparseStream(false)) + .on('data', function(d) { buffer2 += d }) + .once('end', function() { + t.notEqual(buffer1, buffer2, 'buffers are not equal') + t.end() + }) + }) +}) + +test('direct deparsing vs stream deparsing', function(t) { + var src = fs.readFileSync(file) + var tokens = TokenString(src) + var ast = ParseDirect(tokens) + var buffer1 = DeparseDirect(ast, true, '\t') + var buffer2 = '' + + fs.createReadStream(file) + .pipe(TokenStream()) + .pipe(ParseStream()) + .pipe(DeparseStream(true, '\t')) + .on('data', function(d) { buffer2 += d }) + .once('end', function() { + t.equal(buffer1, buffer2, 'direct deparser and stream deparser yield the same results') + t.end() + }) +})