Browse Source

Parse mixin application

master
Dylan Baker 5 years ago
parent
commit
8919a55213
3 changed files with 31 additions and 6 deletions
  1. 2
    2
      src/ast.ts
  2. 10
    4
      src/parser.ts
  3. 19
    0
      src/tests/parser.test.ts

+ 2
- 2
src/ast.ts View File

@@ -257,11 +257,11 @@ export namespace AST {
257 257
 
258 258
   export class RuleSet extends Node {
259 259
     public selectors: Selector[];
260
-    public children: (RuleSet | Rule)[];
260
+    public children: (Application | Rule | RuleSet)[];
261 261
 
262 262
     public constructor(
263 263
       selectors: Selector[],
264
-      children: (RuleSet | Rule)[] = []
264
+      children: (Application | Rule | RuleSet)[] = []
265 265
     ) {
266 266
       super();
267 267
       this.selectors = selectors;

+ 10
- 4
src/parser.ts View File

@@ -244,7 +244,7 @@ export default class Parser {
244 244
       selectors.push(selector);
245 245
     }
246 246
 
247
-    const children: (AST.Rule | AST.RuleSet)[] = [];
247
+    const children: (AST.Application | AST.Rule | AST.RuleSet)[] = [];
248 248
 
249 249
     while (
250 250
       [TokenTypes.PROPERTY, TokenTypes.LPAREN].includes(
@@ -262,9 +262,15 @@ export default class Parser {
262 262
 
263 263
         children.push(new AST.Rule(property, value));
264 264
       } else if (this.currentToken().type === TokenTypes.LPAREN) {
265
-        const ruleSet = this.ruleSet(selectors);
266
-        if (ruleSet instanceof ParserError) return ruleSet;
267
-        children.push(ruleSet);
265
+        if (this.nextToken().type === TokenTypes.LITERAL) {
266
+          const ruleSet = this.ruleSet(selectors);
267
+          if (ruleSet instanceof ParserError) return ruleSet;
268
+          children.push(ruleSet);
269
+        } else {
270
+          const application = this.application();
271
+          if (application instanceof ParserError) return application;
272
+          children.push(application);
273
+        }
268 274
       }
269 275
     }
270 276
 

+ 19
- 0
src/tests/parser.test.ts View File

@@ -337,3 +337,22 @@ test('combine rules and children', (t) => {
337 337
     );
338 338
   }
339 339
 });
340
+
341
+test('parse mixin application', (t) => {
342
+  t.plan(2);
343
+  const result = parse('(div (@border #000000))');
344
+  t.false(result instanceof ParserError);
345
+  if (!(result instanceof LexerError) && !(result instanceof ParserError)) {
346
+    t.deepEqual(
347
+      result.tree[0],
348
+      new AST.RuleSet(
349
+        [new AST.Selector(literalToken('div'))],
350
+        [
351
+          new AST.Application(functionName('border'), [
352
+            literalNode('#000000'),
353
+          ]),
354
+        ]
355
+      )
356
+    )
357
+  }
358
+});

Loading…
Cancel
Save