Skip to content

Instantly share code, notes, and snippets.

@LeonAlvarez
Created August 19, 2019 14:53
Show Gist options
  • Save LeonAlvarez/8f7b024452a30727e850724a39126c08 to your computer and use it in GitHub Desktop.
Save LeonAlvarez/8f7b024452a30727e850724a39126c08 to your computer and use it in GitHub Desktop.
consume string
const consumeAListOfRules = (str, topLevel) => {
let rules = [];
while (str.consume()) {
try {
const rule = consumeStringRule(str, topLevel);
if (rule) {
reules.push(rule)
}
} catch (error) {
return rules;
}
}
}
const consumeStringRule = (str, topLevel) => {
switch (true) {
case str.token instanceof WhitespaceToken:
return;
case (str.token instanceof CDOToken || str.token instanceof CDCToken) && topLevel === 'top-level':
return;
case str.token instanceof EOFToken:
throw new Error('EOF found');
case str.token instanceof AtKeywordToken:
str.reconsume()
return consumeAnAtRule(str);
default:
str.reconsume()
return consumeAQualifiedRule(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment