@bablr/language-en-c-comments
Version:
A BABLR language for C-style comments
59 lines (46 loc) • 1.5 kB
JavaScript
import { re, spam as m } from '@bablr/boot';
import { eat, eatMatch } from '@bablr/helpers/grammar';
import { Node, CoveredBy, InjectFrom } from '@bablr/helpers/decorators';
import * as Shared from '@bablr/helpers/productions';
import * as Space from '@bablr/language-en-blank-space';
export const canonicalURL = 'https://bablr.org/languages/core/en/c-comments';
export const dependencies = { Space };
export const grammar = class CCommentsGrammar {
*Trivia() {
while (
(yield eatMatch(m`#: :Space: <*Space /[ \r\n\t]+/ />`)) ||
(yield eatMatch(m`#: <__Comment />`))
);
}
*Comment() {
yield eat(m`<_Any />`, [m`<BlockComment '/*' />`, m`<LineComment '//' />`]);
}
('Comment')
*BlockComment() {
yield eat(m`open: <*Punctuator '/*' { balanced: '*/', balancedSpan: 'Comment:Block' } />`);
yield eatMatch(m`content: <*Content { span: 'Comment:Block' }/>`);
yield eat(m`close: <*Punctuator '*/' { balancer: true } />`);
}
('Comment')
*LineComment() {
yield eat(m`start: <*Punctuator '//' />`);
yield eatMatch(m`content: <*Content { span: 'Comment:Line' } />`);
}
*Content({ state: { span } }) {
if (span === 'Comment:Block') {
yield eatMatch(re`/(\*[^/]|[^*])*/`);
} else if (span === 'Comment:Line') {
yield eatMatch(re`/[^\n]*/`);
} else {
throw new Error();
}
}
(Shared)
*Any() {}
(Shared)
*Punctuator() {}
};