glsl-token-macros
Extract macro preprocessor statements from an array of GLSL tokens
Last updated 5 years ago by hughsk .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install glsl-token-macros 
SYNC missed versions from official npm registry.

glsl-token-macros

stable

Extract macro preprocessor statements from an array of GLSL tokens from glsl-tokenizer.

Parses out the macro name, arguments (if present) and contents.

Usage

NPM

macros = tokenMacros(tokens)

Given an array of tokens produced by glsl-tokenizer, return an array of macro data in the following format:

{
  name: 'MACRO_NAME',
  args: ['a', 'b'],
  value: '(a * 0.5 + 0.5)',
  index: 2,
  token: {
    type: 'preprocessor',
    data: '#define MACRO_NAME(a, b) (a * 0.5 + 0.5)'
  }
}

macro.name

The macro name. For example, #define HELLO_WORLD would result in HELLO_WORLD.

macro.args

The arguments supplied to the macro. If the macro has no arguments, this will be false. Note that 0 arguments will result in an empty array. For example:

const tokenMacros = require('glsl-token-macros')
const toString = require('glsl-token-string')
const tokenize = require('glsl-tokenizer')
const src = `
#define HELLO_WORLD a
#define LOREM_IPSUM() a
#define BOGAN_IPSUM(a) (a * 0.5 + 0.5)
#define SOME_OTHERS(a, b) (a * 0.5 + b)
`

const tokens = tokenize(src)
const macros = tokenMacros(tokens)

console.log(macros[0].args) // false
console.log(macros[1].args) // []
console.log(macros[2].args) // ["a"]
console.log(macros[3].args) // ["a", "b"]

macro.value

The macro value. If no value is supplied, this will be an empty string. For example:

const tokenMacros = require('glsl-token-macros')
const toString = require('glsl-token-string')
const tokenize = require('glsl-tokenizer')
const src = `
#define HELLO_WORLD
#define LOREM_IPSUM() a
#define BOGAN_IPSUM(a)    (a * 0.5 + 0.5)
#define SOME_OTHERS(a, b) (a * 0.5 + b)
`

const tokens = tokenize(src)
const macros = tokenMacros(tokens)

console.log(macros[0].value) // ""
console.log(macros[1].value) // "a"
console.log(macros[2].value) // "(a * 0.5 + 0.5)"
console.log(macros[3].value) // "(a * 0.5 + b)"

macro.token

The original preprocessor token object from the token list.

macro.index

The index of the original preprocessor token object within the token list.

Contributing

See stackgl/contributing for details.

License

MIT. See LICENSE.md for details.

Current Tags

  • 1.0.0                                ...           latest (5 years ago)

1 Versions

  • 1.0.0                                ...           5 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (0)
None
Dev Dependencies (3)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |