Skip to content

Instantly share code, notes, and snippets.

@kizu
Created December 20, 2017 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kizu/6801ee17be625fb1ebfe985d18fe1a61 to your computer and use it in GitHub Desktop.
Save kizu/6801ee17be625fb1ebfe985d18fe1a61 to your computer and use it in GitHub Desktop.
Getting all keywords and types from CSSTree lexer
const getPropValues = (prop) => {
const keywords = [];
const types = [];
const searcher = (node) => {
if (!node) return;
if (node.type === 'Keyword' && keywords.indexOf(node.name) === -1) {
keywords.push(node.name);
} else if (node.type === 'Group') {
node.terms.map(term => csstree.grammar.walk(term, { enter: searcher }));
} else if (node.type === 'Type') {
if (types.indexOf(node.name) === -1) {
types.push(node.name);
const typeSource = csstree.lexer.properties[node.name] || csstree.lexer.types[node.name];
if (typeSource && typeSource.syntax) {
csstree.grammar.walk(typeSource.syntax, { enter: searcher });
}
}
}
}
csstree.grammar.walk(csstree.lexer.properties[prop].syntax, {
enter: searcher
});
return {
keywords: keywords,
types: types
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment