Skip to content

Instantly share code, notes, and snippets.

@LeonAlvarez
Created August 19, 2019 14:25
Show Gist options
  • Save LeonAlvarez/1f25e71ec4db10c4b5e09b53627b3aa7 to your computer and use it in GitHub Desktop.
Save LeonAlvarez/1f25e71ec4db10c4b5e09b53627b3aa7 to your computer and use it in GitHub Desktop.
consume string rule
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