Created
December 20, 2017 17:51
-
-
Save kizu/6801ee17be625fb1ebfe985d18fe1a61 to your computer and use it in GitHub Desktop.
Getting all keywords and types from CSSTree lexer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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