Skip to content

Commit d12a160

Browse files
committed
Expose sync interface, add docs/tests
1 parent 9a186d2 commit d12a160

File tree

11 files changed

+5487
-116
lines changed

11 files changed

+5487
-116
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.DS_Store
3+
npm-debug.log

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node_modules/
1+
test

LICENSE.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
The MIT License (MIT)
2+
=====================
3+
4+
Copyright (c) 2014 [Chris Dickinson](http://github.com/chrisdickinson)
5+
6+
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:
7+
8+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9+
10+
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.

README.md

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,83 @@
1-
glsl-parser
2-
===========
1+
# glsl-parser
32

4-
a through stream that takes tokens from [glsl-tokenizer](https://github.com/chrisdickinson/glsl-tokenizer) and turns them into
3+
![](http://img.shields.io/badge/stability-stable-green.svg?style=flat)
4+
![](http://img.shields.io/npm/v/glsl-parser.svg?style=flat)
5+
![](http://img.shields.io/npm/dm/glsl-parser.svg?style=flat)
6+
![](http://img.shields.io/npm/l/glsl-parser.svg?style=flat)
7+
8+
A GLSL parser that takes tokens from
9+
[glsl-tokenizer](http://github.com/stackgl/glsl-tokenizer) and turns them into
510
an AST.
611

7-
```javascript
12+
May either be used as a stream
13+
14+
## API
815

9-
var tokenizer = require('glsl-tokenizer')()
10-
, fs = require('fs')
11-
, parser = require('./index')
16+
### `stream = require('glsl-parser/stream')`
1217

13-
var num = 0
18+
Creates a GLSL parser stream, which emits nodes as they're parsed.
19+
20+
``` javascript
21+
var TokenStream = require('glsl-tokenizer/stream')
22+
var ParseStream = require('glsl-parser/stream')
23+
var fs = require('fs')
1424

1525
fs.createReadStream('test.glsl')
16-
.pipe(tokenizer)
17-
.pipe(parser())
26+
.pipe(TokenStream())
27+
.pipe(ParseStream())
1828
.on('data', function(x) {
1929
console.log('ast of', x.type)
2030
})
21-
2231
```
2332

24-
similar to [JSONStream](https://github.com/dominictarr/JSONStream), you may pass selectors
25-
into the constructor to match only AST elements at that level. viable selectors are strings
26-
and regexen, and they'll be matched against the emitted node's `type`.
33+
### `ast = stream.program`
2734

28-
nodes
29-
-----
35+
The full program's AST, which will be updated with each incoming token.
3036

31-
```
37+
### `ast = require('glsl-parser/direct')(tokens)`
3238

33-
stmtlist
34-
stmt
35-
struct
36-
function
37-
functionargs
38-
decl
39-
decllist
40-
forloop
41-
whileloop
42-
if
43-
expr
44-
precision
45-
comment
46-
preprocessor
47-
keyword
48-
ident
49-
return
50-
continue
51-
break
52-
discard
53-
do-while
54-
binary
55-
ternary
56-
unary
39+
Synchronously parses an array of tokens from `glsl-tokenizer`.
5740

58-
```
41+
``` javascript
42+
var TokenString = require('glsl-tokenizer/string')
43+
var ParseTokens = require('glsl-parser/direct')
44+
var fs = require('fs')
5945

60-
legal & caveats
61-
===============
46+
var src = fs.readFileSync('test.glsl', 'utf8')
47+
var tokens = TokenString(src)
48+
var ast = ParseTokens(tokens)
49+
50+
console.log(ast)
51+
```
6252

63-
known bugs
64-
----------
53+
## Nodes
54+
55+
* `stmtlist`
56+
* `stmt`
57+
* `struct`
58+
* `function`
59+
* `functionargs`
60+
* `decl`
61+
* `decllist`
62+
* `forloop`
63+
* `whileloop`
64+
* `if`
65+
* `expr`
66+
* `precision`
67+
* `comment`
68+
* `preprocessor`
69+
* `keyword`
70+
* `ident`
71+
* `return`
72+
* `continue`
73+
* `break`
74+
* `discard`
75+
* `do-while`
76+
* `binary`
77+
* `ternary`
78+
* `unary`
79+
80+
## Known Issues
6581

6682
* because i am not smart enough to write a fully streaming parser, the current parser "cheats" a bit when it encounters a `expr` node! it actually waits until it has all the tokens it needs to build a tree for a given expression, then builds it and emits the constituent child nodes in the expected order. the `expr` parsing is heavily influenced by [crockford's tdop article](http://javascript.crockford.com/tdop/tdop.html). the rest of the parser is heavily influenced by fever dreams.
6783

@@ -73,7 +89,6 @@ might not be the case -- it might be a user-defined constructor starting a state
7389
if you've got unhygenic macros in your code, move the #if / #endifs to statement level, and have them surround
7490
wholly parseable code. this sucks, and i am sorry.
7591

76-
license
77-
-------
92+
## License
7893

79-
MIT
94+
MIT, see [LICENSE.md](LICENSE.md) for more details.

direct.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var parse = require('./lib/index')
2+
3+
module.exports = parseArray
4+
5+
function parseArray(tokens) {
6+
var parser = parse()
7+
8+
for (var i = 0; i < tokens.length; i++) {
9+
parser(tokens[i])
10+
}
11+
12+
return parser(null)
13+
}

0 commit comments

Comments
 (0)