UNPKG

@bablr/language-en-es6

Version:
93 lines (75 loc) 2.6 kB
import { spam as m } from '@bablr/boot'; import { CoveredBy, Node } from '@bablr/helpers/decorators'; import { o, eat, eatMatch, fail } from '@bablr/helpers/grammar'; export const mixin = (Base) => class ES6ClassGrammar extends Base { @Node *ClassDeclaration() { yield eat(m`sigilToken: <*Keyword 'class' />`); yield eat(m`name$: <Identifier />`); if (yield eatMatch(m`extendsToken: <*Keyword 'extends' />`, o({}), o({ bind: true }))) { yield eat(m`superClass$: <__Expression>`); } else { yield eat(m`superClass$: null`); } yield eat(m`body: <ClassBody />`); } @CoveredBy('Expression') @Node *ClassExpression() { yield eat(m`sigilToken: <*Keyword 'class' />`); yield eatMatch(m`name$: <Identifier />`); if (yield eatMatch(m`extendsToken: <*Keyword 'extends' />`)) { yield eat(m`superClass$: <__Expression>`); } else { yield eat(m`superClass$: null`); } yield eat(m`body: <ClassBody />`); } @Node *ClassBody() { yield eat(m`openToken: <*Punctuator '{' { balanced: '}' } />`); while (yield eatMatch(m`members[]: <__ClassMember />`)) {} yield eat(m`closeToken: <*Punctuator '}' { balancer: true } />`); } *ClassMember() { yield eat(m`<ClassMethod />`); } @CoveredBy('ClassMember') @Node *ClassMethod() { yield eatMatch(m`staticToken: <*Keyword 'static' />`); let acc, a, gen; acc = yield eatMatch(m`kindToken: <*Keyword /get|set/ />`); if (!acc) { a = yield eatMatch(m`asyncToken: <*Keyword 'async' />`); gen = yield eatMatch(m`starToken: <*Punctuator '*' />`); } else { yield eatMatch(m`asyncToken: null`); yield eatMatch(m`starToken: null`); } if (a && gen) yield fail(); yield eatMatch(m`id: <Identifier />`, o({}), o({ bind: true })); yield eat(m`openParamsToken: <*Punctuator '(' { balanced: ')' } />`); yield eat( m`params[]: <_List />`, o({ element: m`<Identifier />`, allowTrailingSeparator: false, separator: m`separatorTokens[]: <*Punctuator ',' />`, }), ); yield eat(m`closeParamsToken: <*Punctuator ')' { balancer: true } />`); yield eat(m`body: <BlockStatement />`); } @CoveredBy('Expression') @Node *SuperExpression() { yield eat(m`sigilToken: <*Keyword 'super' />`); } @CoveredBy('Expression') @Node *ThisExpression() { yield eat(m`sigilToken: <*Keyword 'this' />`); } };