Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sync API, docs/tests #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.DS_Store
npm-debug.log
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node_modules/
test
10 changes: 10 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
74 changes: 48 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions direct.js
Original file line number Diff line number Diff line change
@@ -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('')
}
44 changes: 22 additions & 22 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -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
Expand All @@ -74,11 +78,7 @@ function deparse_stream(with_whitespace, indent) {

deparse(node)

stream.queue(output.join(''))
}

function end() {
stream.queue(null)
return output.join('')
}
}

Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -243,7 +243,7 @@ function deparse_functionargs(node) {
output.push(',')
output.push(ws.optional(' '))
}
}
}
}

function deparse_ident(node) {
Expand Down Expand Up @@ -303,7 +303,7 @@ function deparse_if(node) {
!is_if_stmt && ws.dedent()
output.push(ws.optional('\n'))
}
}
}
}

function deparse_keyword(node) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -458,7 +458,7 @@ function deparse_call(node) {
output.push(ws.optional(' '))
}
}
output.push(')')
output.push(')')
}

function deparse_operator(node) {
Expand Down
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions stream.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading