@grammar/stylus
Version:
Stylus grammar.
396 lines • 16.4 kB
JavaScript
import $ from './spellu-engine.mjs';
/**
* Stylus -
*/
var grammar$stylus;
(function (grammar$stylus) {
var processors;
(function (processors) {
processors.cst = {};
processors.ast = {};
})(processors = grammar$stylus.processors || (grammar$stylus.processors = {}));
})(grammar$stylus || (grammar$stylus = {}));
(function (grammar$stylus) {
let ElementCombinator;
(function (ElementCombinator) {
ElementCombinator["Descendant"] = " ";
ElementCombinator["Child"] = ">";
ElementCombinator["NextSibling"] = "+";
ElementCombinator["SubsequentSibling"] = "~";
})(ElementCombinator = grammar$stylus.ElementCombinator || (grammar$stylus.ElementCombinator = {}));
let MatchOperator;
(function (MatchOperator) {
MatchOperator["WholeMatch"] = "=";
MatchOperator["PrefixMatch"] = "^=";
MatchOperator["SuffixMatch"] = "$=";
MatchOperator["SubstringMatch"] = "*=";
MatchOperator["Includes"] = "~=";
MatchOperator["DashMatch"] = "|=";
})(MatchOperator = grammar$stylus.MatchOperator || (grammar$stylus.MatchOperator = {}));
})(grammar$stylus || (grammar$stylus = {}));
(function (grammar$stylus) {
let ProcessorSuite;
(function (ProcessorSuite) {
ProcessorSuite["CST"] = "cst";
ProcessorSuite["AST"] = "ast";
})(ProcessorSuite = grammar$stylus.ProcessorSuite || (grammar$stylus.ProcessorSuite = {}));
function selectProcessorSuite(suite) {
const items = grammar$stylus.processors[suite];
return [
items.stylus,
];
}
grammar$stylus.selectProcessorSuite = selectProcessorSuite;
function scan(source, suite = ProcessorSuite.AST, rule, options = {}) {
options.processors = selectProcessorSuite(suite);
return $.scan($.createSource(source), rule ?? grammar$stylus.recipes.stylus.name, options);
}
grammar$stylus.scan = scan;
function tokens(source, options = {}) {
return $.flatten(scan(source, ProcessorSuite.CST, undefined, options));
}
grammar$stylus.tokens = tokens;
})(grammar$stylus || (grammar$stylus = {}));
(function (grammar$stylus) {
const printOptions = {
pretty: false,
};
function print(value, options = {}) {
options = { ...printOptions, ...options };
const printer = new CSSPrinter(options);
return printer.print(value);
}
grammar$stylus.print = print;
class CSSPrinter extends $.TextPrinter {
constructor(options = {}) {
super({
space: options.pretty ? ' ' : '',
lineBreak: options.pretty ? '\n' : '',
indent: options.pretty ? 'tab' : 0,
});
}
print(value) {
this.node(value);
return this.flash();
}
node(...nodes) {
for (const node of nodes) {
const block = this._blocks[node.syntax];
if (!block) {
throw new Error(`syntax "${node.syntax}" is not defined.`);
}
block(this, node);
}
return this;
}
nodeList(list, options) {
const nodes = [...list];
const lastNode = nodes.pop();
for (const node of nodes) {
this.node(node).text(options.separator);
}
if (options.tralingSeparator) {
this.node(lastNode).text(options.separator);
}
else {
this.node(lastNode);
}
return this;
}
_blocks = {
["stylus.StylusDocument" /* StylusDocument */](printer, _) {
printer.node(..._.statements);
},
["stylus.Import" /* Import */](printer, _) {
printer.text('uu');
},
["stylus.Ruleset" /* Ruleset */](printer, _) {
printer.node(..._.selectors);
printer.newLine().indentUp();
printer.node(...(_.items || []));
printer.indentDown();
},
["stylus.Selector" /* Selector */](printer, _) {
_.items.map(_ => printer.node(_));
},
["stylus.Combinator" /* Combinator */](printer, _) {
printer.text(_.combinator);
},
'stylus.token.tag'(printer, _) {
printer.text(_.value);
},
["stylus.Tag" /* Tag */](printer, _) {
printer.text(_.name);
},
["stylus.Id" /* Id */](printer, _) {
printer.text('#', _.name);
},
["stylus.Class" /* Class */](printer, _) {
printer.text('.', _.name);
},
["stylus.Attribute" /* Attribute */](printer, _) {
// printer.text(_.name.value!)
},
["stylus.Declaration" /* Declaration */](printer, _) {
const postfix = _.important ? '!important' : '';
printer.text(_.property.name, ' ')
.nodeList(_.expressions, { separator: ',', tralingSeparator: false })
.newLine();
},
["stylus.Identifier" /* Identifier */](printer, _) {
printer.text(_.name);
},
["stylus.Expression" /* Expression */](printer, _) {
printer.nodeList(_.items, { separator: ' ', tralingSeparator: false });
},
["stylus.Value" /* Value */](printer, _) {
printer.text(_.value);
},
};
}
})(grammar$stylus || (grammar$stylus = {}));
(function (grammar$stylus) {
var recipes;
(function (recipes) {
recipes.stylus = {
name: 'stylus',
spacePattern: /[ \t]*/,
commentRule: 'line-comment',
rules: {
'root': {
parser: 'root-line',
},
'root-line': {
parser: '>>', argument: { parser: 'ruleset' },
},
'ruleset': {
parser: '&', argument: [
{ parser: 'selector-list' },
{ parser: 'block' },
],
},
'block': {
parser: '|', argument: [
{ parser: 'stylus-block' },
{ parser: 'css-block' },
],
},
'stylus-block': {
parser: '&', argument: [
{ parser: '$' },
{
parser: '>>|', argument: [
{ parser: 'ruleset' },
{ parser: 'declaration' },
],
},
],
},
'css-block': {
parser: '&', argument: [
{ parser: '$', optional: true },
{ parser: '?', argument: { syntax: 'stylus.token.css-block-open', pattern: '{' } },
{
parser: '|', argument: [
{ parser: '=' },
{ parser: '>' },
], multiplicity: 0,
},
// TODO
{
parser: '>>|', argument: [
{ parser: 'ruleset' },
{ parser: 'declaration' },
],
},
// TODO
{ parser: '?', argument: { syntax: 'stylus.token.statement-separator', pattern: ';' } },
{ parser: '?', argument: { syntax: 'stylus.token.css-block-close', pattern: '}' } },
],
},
'selector-list': {
parser: '*', argument: [
{ parser: 'selector', options: true },
{ parser: 'selector-separator' },
], options: { allowTrailingSeparator: false },
},
'selector-separator': {
parser: '|', argument: [
{ parser: '?', argument: { syntax: 'stylus.token.comma', pattern: ',' } },
{
parser: '&', argument: [
{ parser: '$' },
{ parser: '=' },
]
},
],
},
'selector': {
parser: '+', argument: [
{ parser: 'selector-item' },
{ parser: 'combinator', optional: true },
],
},
'combinator': {
parser: '|', argument: [
{ parser: '?', argument: { syntax: '>', pattern: '>' } },
{ parser: '?', argument: { syntax: '+', pattern: '+' } },
{ parser: '?', argument: { syntax: '~', pattern: '~' } },
{ parser: '#', argument: { syntax: ' ', pattern: /[ \t]+/, label: ' ' } },
],
},
'selector-item': {
parser: '|', argument: [
{ parser: 'tag' },
],
},
'tag': {
parser: '?', argument: { syntax: 'stylus.token.tag', pattern: /[a-zA-Z][0-9a-zA-Z-:]*/, label: 'TAG' }
},
'declaration': {
parser: '&', argument: [
{ parser: 'property' },
{ parser: '?', argument: { syntax: "stylus.token.colon" /* ColonToken */, pattern: ':' }, optional: true },
{ parser: 'list' },
{ parser: '?', argument: { syntax: "stylus.token.important" /* ImportantToken */, pattern: /!\s*important/ }, optional: true },
],
},
'property': {
parser: 'ident',
},
'list': {
parser: '*', argument: [
{ parser: 'expression' },
{ parser: '?', argument: { syntax: "stylus.token.colon" /* ColonToken */, pattern: ',' } },
],
options: { allowTrailingSeparator: false },
},
'expression': {
parser: 'negation', multiplicity: 1
},
'negation': {
parser: '&', argument: [
{ parser: '?', argument: { syntax: "stylus.token.colon" /* ColonToken */, pattern: 'not' }, optional: true },
{ parser: 'primary' },
],
},
'primary': {
parser: '|', argument: [
{ parser: 'ident' },
{ parser: 'dimension' },
{ parser: 'percentage' },
{ parser: 'number' },
{ parser: 'hash' },
{ parser: 'string' }, // "abc" 'def'
],
},
'ident': {
parser: '?', argument: { syntax: "stylus.token.ident" /* IdentToken */, pattern: /[-]?[a-zA-Z_][a-zA-Z0-9_\-]*/ },
},
'dimension': {
parser: '?', argument: { syntax: "stylus.token.dimension" /* DimensionToken */, pattern: /([.](?:[0-9])+|[0-9]+(?:\.[0-9]+)?)[a-zA-Z]+/ },
},
'percentage': {
parser: '?', argument: { syntax: "stylus.token.percentage" /* PercentageToken */, pattern: /([.](?:[0-9])+|[0-9]+(?:\.[0-9]+)?)%/ },
},
'number': {
parser: '?', argument: { syntax: "stylus.token.number" /* NumberToken */, pattern: /[.](?:[0-9])+|[0-9]+(?:\.[0-9]+)?/ },
},
'hash': {
parser: '?', argument: { syntax: "stylus.token.hash" /* HashToken */, pattern: /[#][a-zA-Z0-9_\-]+/ },
},
'string': {
parser: '&', argument: [
{ parser: '?', argument: { syntax: 'css.token.double-quote', pattern: '"' } },
{ parser: '?', argument: { syntax: 'css.token.string', pattern: /[^"]*/ } },
{ parser: '?', argument: { syntax: 'css.token.double-quote', pattern: '"' } },
],
},
'block-comment': {
parser: '&', argument: [
{ parser: '?', argument: { syntax: 'css.token.open-comment', pattern: '/*' } },
{ parser: '?', argument: { syntax: 'css.token.comment', pattern: /.*(?=\*\/)/ } },
{ parser: '?', argument: { syntax: 'css.token.close-comment', pattern: '*/' } },
], optional: true,
},
'line-comment': {
parser: '?', argument: { syntax: 'stylus.line-comment', pattern: /\/\/.*/ }
},
},
};
})(recipes = grammar$stylus.recipes || (grammar$stylus.recipes = {}));
})(grammar$stylus || (grammar$stylus = {}));
(function (grammar$stylus) {
var processors;
(function (processors) {
processors.cst.stylus = {
recipe: grammar$stylus.recipes.stylus,
parts: {}
};
})(processors = grammar$stylus.processors || (grammar$stylus.processors = {}));
})(grammar$stylus || (grammar$stylus = {}));
(function (grammar$stylus) {
var processors;
(function (processors) {
processors.ast.stylus = {
recipe: grammar$stylus.recipes.stylus,
parts: {
'root': (_) => {
return $.createNode(_, "stylus.StylusDocument" /* StylusDocument */, {
statements: _,
});
},
'root-line': (_) => {
return _;
},
'ruleset': (_) => {
return $.createNode(_, "stylus.Ruleset" /* Ruleset */, {
selectors: _[0],
items: _[1],
});
},
'stylus-block': (_) => {
return _[1];
},
'selector'(_) {
return $.createNode(_, "stylus.Selector" /* Selector */, {
items: _,
});
},
'declaration'(_) {
return $.createNode(_, "stylus.Declaration" /* Declaration */, {
property: _[0],
expressions: _[2],
important: Boolean(_[3]),
});
},
'property'(_) {
return $.createNode(_, "stylus.Identifier" /* Identifier */, {
name: _.value,
token: _,
});
},
'expression'(_) {
return $.createNode(_, "stylus.Expression" /* Expression */, {
items: _,
});
},
'negation'(_) {
return _[1];
},
'primary'(_) {
// return new ast.Value(_.value)
return $.createNode(_, "stylus.Value" /* Value */, {
value: _.value,
token: _,
});
},
'string'(_) {
return _[1];
},
},
};
})(processors = grammar$stylus.processors || (grammar$stylus.processors = {}));
})(grammar$stylus || (grammar$stylus = {}));
export default grammar$stylus;
//# sourceMappingURL=grammar-stylus.mjs.map