OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library csslib.parser; | 5 library csslib.parser; |
6 | 6 |
7 import 'dart:math' as math; | 7 import 'dart:math' as math; |
8 | 8 |
9 import 'package:source_span/source_span.dart'; | 9 import 'package:source_span/source_span.dart'; |
10 | 10 |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 * : [ONLY | NOT]? S* media_type S* [ AND S* expression ]* | 349 * : [ONLY | NOT]? S* media_type S* [ AND S* expression ]* |
350 * | expression [ AND S* expression ]* | 350 * | expression [ AND S* expression ]* |
351 * media_type | 351 * media_type |
352 * : IDENT | 352 * : IDENT |
353 * expression | 353 * expression |
354 * : '(' S* media_feature S* [ ':' S* expr ]? ')' S* | 354 * : '(' S* media_feature S* [ ':' S* expr ]? ')' S* |
355 * media_feature | 355 * media_feature |
356 * : IDENT | 356 * : IDENT |
357 */ | 357 */ |
358 List<MediaQuery> processMediaQueryList() { | 358 List<MediaQuery> processMediaQueryList() { |
359 var mediaQueries = []; | 359 var mediaQueries = <MediaQuery>[]; |
360 | 360 |
361 bool firstTime = true; | 361 bool firstTime = true; |
362 var mediaQuery; | 362 var mediaQuery; |
363 do { | 363 do { |
364 mediaQuery = processMediaQuery(firstTime == true); | 364 mediaQuery = processMediaQuery(firstTime == true); |
365 if (mediaQuery != null) { | 365 if (mediaQuery != null) { |
366 mediaQueries.add(mediaQuery); | 366 mediaQueries.add(mediaQuery); |
367 firstTime = false; | 367 firstTime = false; |
368 continue; | 368 continue; |
369 } | 369 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 _next(); | 402 _next(); |
403 start = _peekToken.span; | 403 start = _peekToken.span; |
404 } | 404 } |
405 | 405 |
406 var type; | 406 var type; |
407 if (startQuery && unaryOp != TokenKind.MEDIA_OP_AND) { | 407 if (startQuery && unaryOp != TokenKind.MEDIA_OP_AND) { |
408 // Get the media type. | 408 // Get the media type. |
409 if (_peekIdentifier()) type = identifier(); | 409 if (_peekIdentifier()) type = identifier(); |
410 } | 410 } |
411 | 411 |
412 var exprs = []; | 412 var exprs = <MediaExpression>[]; |
413 | 413 |
414 if (unaryOp == -1 || unaryOp == TokenKind.MEDIA_OP_AND) { | 414 if (unaryOp == -1 || unaryOp == TokenKind.MEDIA_OP_AND) { |
415 var andOp = false; | 415 var andOp = false; |
416 while (true) { | 416 while (true) { |
417 var expr = processMediaExpression(andOp); | 417 var expr = processMediaExpression(andOp); |
418 if (expr == null) break; | 418 if (expr == null) break; |
419 | 419 |
420 exprs.add(expr); | 420 exprs.add(expr); |
421 op = _peekToken.text; | 421 op = _peekToken.text; |
422 opLen = op.length; | 422 opLen = op.length; |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
768 * | 768 * |
769 * @mixin IDENT [(args,...)] '{' | 769 * @mixin IDENT [(args,...)] '{' |
770 * [ruleset | property | directive]* | 770 * [ruleset | property | directive]* |
771 * '}' | 771 * '}' |
772 */ | 772 */ |
773 MixinDefinition processMixin() { | 773 MixinDefinition processMixin() { |
774 _next(); | 774 _next(); |
775 | 775 |
776 var name = identifier(); | 776 var name = identifier(); |
777 | 777 |
778 List<VarDefinitionDirective> params = []; | 778 var params = <VarDefinition>[]; |
779 // Any parameters? | 779 // Any parameters? |
780 if (_maybeEat(TokenKind.LPAREN)) { | 780 if (_maybeEat(TokenKind.LPAREN)) { |
781 var mustHaveParam = false; | 781 var mustHaveParam = false; |
782 var keepGoing = true; | 782 var keepGoing = true; |
783 while (keepGoing) { | 783 while (keepGoing) { |
784 var varDef = processVariableOrDirective(mixinParameter: true); | 784 var varDef = processVariableOrDirective(mixinParameter: true); |
785 if (varDef is VarDefinitionDirective || varDef is VarDefinition) { | 785 if (varDef is VarDefinitionDirective || varDef is VarDefinition) { |
786 params.add(varDef); | 786 params.add(varDef); |
787 } else if (mustHaveParam) { | 787 } else if (mustHaveParam) { |
788 _warning("Expecting parameter", _makeSpan(_peekToken.span)); | 788 _warning("Expecting parameter", _makeSpan(_peekToken.span)); |
(...skipping 17 matching lines...) Expand all Loading... |
806 var directive = processDirective(); | 806 var directive = processDirective(); |
807 if (directive != null) { | 807 if (directive != null) { |
808 productions.add(directive); | 808 productions.add(directive); |
809 continue; | 809 continue; |
810 } | 810 } |
811 | 811 |
812 var declGroup = processDeclarations(checkBrace: false); | 812 var declGroup = processDeclarations(checkBrace: false); |
813 if (declGroup.declarations.any((decl) { | 813 if (declGroup.declarations.any((decl) { |
814 return decl is Declaration && decl is! IncludeMixinAtDeclaration; | 814 return decl is Declaration && decl is! IncludeMixinAtDeclaration; |
815 })) { | 815 })) { |
816 var newDecls = []; | 816 var newDecls = <Declaration>[]; |
817 productions.forEach((include) { | 817 productions.forEach((include) { |
818 // If declGroup has items that are declarations then we assume | 818 // If declGroup has items that are declarations then we assume |
819 // this mixin is a declaration mixin not a top-level mixin. | 819 // this mixin is a declaration mixin not a top-level mixin. |
820 if (include is IncludeDirective) { | 820 if (include is IncludeDirective) { |
821 newDecls.add(new IncludeMixinAtDeclaration(include, include.span)); | 821 newDecls.add(new IncludeMixinAtDeclaration(include, include.span)); |
822 } else { | 822 } else { |
823 _warning("Error mixing of top-level vs declarations mixins", | 823 _warning("Error mixing of top-level vs declarations mixins", |
824 _makeSpan(include.span)); | 824 _makeSpan(include.span)); |
825 } | 825 } |
826 }); | 826 }); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
936 * | 936 * |
937 * @include IDENT [(args,...)]; | 937 * @include IDENT [(args,...)]; |
938 */ | 938 */ |
939 _next(); | 939 _next(); |
940 | 940 |
941 var name; | 941 var name; |
942 if (_peekIdentifier()) { | 942 if (_peekIdentifier()) { |
943 name = identifier(); | 943 name = identifier(); |
944 } | 944 } |
945 | 945 |
946 var params = []; | 946 var params = <List<Expression>>[]; |
947 | 947 |
948 // Any parameters? Parameters can be multiple terms per argument e.g., | 948 // Any parameters? Parameters can be multiple terms per argument e.g., |
949 // 3px solid yellow, green is two parameters: | 949 // 3px solid yellow, green is two parameters: |
950 // 1. 3px solid yellow | 950 // 1. 3px solid yellow |
951 // 2. green | 951 // 2. green |
952 // the first has 3 terms and the second has 1 term. | 952 // the first has 3 terms and the second has 1 term. |
953 if (_maybeEat(TokenKind.LPAREN)) { | 953 if (_maybeEat(TokenKind.LPAREN)) { |
954 var terms = []; | 954 var terms = <Expression>[]; |
955 var expr; | 955 var expr; |
956 var keepGoing = true; | 956 var keepGoing = true; |
957 while (keepGoing && (expr = processTerm()) != null) { | 957 while (keepGoing && (expr = processTerm()) != null) { |
958 // VarUsage is returns as a list | 958 // VarUsage is returns as a list |
959 terms.add(expr is List ? expr[0] : expr); | 959 terms.add(expr is List ? expr[0] : expr); |
960 keepGoing = !_peekKind(TokenKind.RPAREN); | 960 keepGoing = !_peekKind(TokenKind.RPAREN); |
961 if (keepGoing) { | 961 if (keepGoing) { |
962 if (_maybeEat(TokenKind.COMMA)) { | 962 if (_maybeEat(TokenKind.COMMA)) { |
963 params.add(terms); | 963 params.add(terms); |
964 terms = []; | 964 terms = []; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1034 messages = oldMessages; | 1034 messages = oldMessages; |
1035 return selGroup; | 1035 return selGroup; |
1036 } | 1036 } |
1037 } | 1037 } |
1038 | 1038 |
1039 DeclarationGroup processDeclarations({bool checkBrace: true}) { | 1039 DeclarationGroup processDeclarations({bool checkBrace: true}) { |
1040 var start = _peekToken.span; | 1040 var start = _peekToken.span; |
1041 | 1041 |
1042 if (checkBrace) _eat(TokenKind.LBRACE); | 1042 if (checkBrace) _eat(TokenKind.LBRACE); |
1043 | 1043 |
1044 List decls = []; | 1044 var decls = <TreeNode>[]; |
1045 List dartStyles = []; // List of latest styles exposed to Dart. | 1045 var dartStyles = []; // List of latest styles exposed to Dart. |
1046 | 1046 |
1047 do { | 1047 do { |
1048 var selectorGroup = _nestedSelector(); | 1048 var selectorGroup = _nestedSelector(); |
1049 while (selectorGroup != null) { | 1049 while (selectorGroup != null) { |
1050 // Nested selector so process as a ruleset. | 1050 // Nested selector so process as a ruleset. |
1051 var ruleset = processRuleSet(selectorGroup); | 1051 var ruleset = processRuleSet(selectorGroup); |
1052 decls.add(ruleset); | 1052 decls.add(ruleset); |
1053 selectorGroup = _nestedSelector(); | 1053 selectorGroup = _nestedSelector(); |
1054 } | 1054 } |
1055 | 1055 |
(...skipping 30 matching lines...) Expand all Loading... |
1086 // Dart style not live, ignore these styles in this Declarations. | 1086 // Dart style not live, ignore these styles in this Declarations. |
1087 decl.dartStyle = null; | 1087 decl.dartStyle = null; |
1088 } | 1088 } |
1089 } | 1089 } |
1090 } | 1090 } |
1091 | 1091 |
1092 return new DeclarationGroup(decls, _makeSpan(start)); | 1092 return new DeclarationGroup(decls, _makeSpan(start)); |
1093 } | 1093 } |
1094 | 1094 |
1095 List<DeclarationGroup> processMarginsDeclarations() { | 1095 List<DeclarationGroup> processMarginsDeclarations() { |
1096 List groups = []; | 1096 var groups = <DeclarationGroup>[]; |
1097 | 1097 |
1098 var start = _peekToken.span; | 1098 var start = _peekToken.span; |
1099 | 1099 |
1100 _eat(TokenKind.LBRACE); | 1100 _eat(TokenKind.LBRACE); |
1101 | 1101 |
1102 List<Declaration> decls = []; | 1102 List<Declaration> decls = []; |
1103 List dartStyles = []; // List of latest styles exposed to Dart. | 1103 List dartStyles = []; // List of latest styles exposed to Dart. |
1104 | 1104 |
1105 do { | 1105 do { |
1106 switch (_peek()) { | 1106 switch (_peek()) { |
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1480 * : [ [ PLUS | '-' | DIMENSION | NUMBER | STRING | IDENT ] S* ]+ | 1480 * : [ [ PLUS | '-' | DIMENSION | NUMBER | STRING | IDENT ] S* ]+ |
1481 * | 1481 * |
1482 * num [0-9]+|[0-9]*\.[0-9]+ | 1482 * num [0-9]+|[0-9]*\.[0-9]+ |
1483 * PLUS '+' | 1483 * PLUS '+' |
1484 * DIMENSION {num}{ident} | 1484 * DIMENSION {num}{ident} |
1485 * NUMBER {num} | 1485 * NUMBER {num} |
1486 */ | 1486 */ |
1487 processSelectorExpression() { | 1487 processSelectorExpression() { |
1488 var start = _peekToken.span; | 1488 var start = _peekToken.span; |
1489 | 1489 |
1490 var expressions = []; | 1490 var expressions = <Expression>[]; |
1491 | 1491 |
1492 Token termToken; | 1492 Token termToken; |
1493 var value; | 1493 var value; |
1494 | 1494 |
1495 var keepParsing = true; | 1495 var keepParsing = true; |
1496 while (keepParsing) { | 1496 while (keepParsing) { |
1497 switch (_peek()) { | 1497 switch (_peek()) { |
1498 case TokenKind.PLUS: | 1498 case TokenKind.PLUS: |
1499 start = _peekToken.span; | 1499 start = _peekToken.span; |
1500 termToken = _next(); | 1500 termToken = _next(); |
(...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2262 case TokenKind.AT: | 2262 case TokenKind.AT: |
2263 if (messages.options.lessSupport) { | 2263 if (messages.options.lessSupport) { |
2264 _next(); | 2264 _next(); |
2265 | 2265 |
2266 var expr = processExpr(); | 2266 var expr = processExpr(); |
2267 if (isChecked && expr.expressions.length > 1) { | 2267 if (isChecked && expr.expressions.length > 1) { |
2268 _error("only @name for Less syntax", _peekToken.span); | 2268 _error("only @name for Less syntax", _peekToken.span); |
2269 } | 2269 } |
2270 | 2270 |
2271 var param = expr.expressions[0]; | 2271 var param = expr.expressions[0]; |
2272 var varUsage = new VarUsage(param.text, [], _makeSpan(start)); | 2272 var varUsage = new VarUsage( |
| 2273 (param as LiteralTerm).text, [], _makeSpan(start)); |
2273 expr.expressions[0] = varUsage; | 2274 expr.expressions[0] = varUsage; |
2274 return expr.expressions; | 2275 return expr.expressions; |
2275 } | 2276 } |
2276 break; | 2277 break; |
2277 } | 2278 } |
2278 | 2279 |
2279 return processDimension(t, value, _makeSpan(start)); | 2280 return processDimension(t, value, _makeSpan(start)); |
2280 } | 2281 } |
2281 | 2282 |
2282 /** Process all dimension units. */ | 2283 /** Process all dimension units. */ |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2536 // (GradientType=0,StartColorStr='#9d8b83', EndColorStr='#847670'); | 2537 // (GradientType=0,StartColorStr='#9d8b83', EndColorStr='#847670'); |
2537 var expr = processExpr(); | 2538 var expr = processExpr(); |
2538 if (!_maybeEat(TokenKind.RPAREN)) { | 2539 if (!_maybeEat(TokenKind.RPAREN)) { |
2539 _error("problem parsing var expected ), ", _peekToken.span); | 2540 _error("problem parsing var expected ), ", _peekToken.span); |
2540 } | 2541 } |
2541 if (isChecked && | 2542 if (isChecked && |
2542 expr.expressions.where((e) => e is OperatorComma).length > 1) { | 2543 expr.expressions.where((e) => e is OperatorComma).length > 1) { |
2543 _error("too many parameters to var()", _peekToken.span); | 2544 _error("too many parameters to var()", _peekToken.span); |
2544 } | 2545 } |
2545 | 2546 |
2546 var paramName = expr.expressions[0].text; | 2547 var paramName = (expr.expressions[0] as LiteralTerm).text; |
2547 | 2548 |
2548 // [0] - var name, [1] - OperatorComma, [2] - default value. | 2549 // [0] - var name, [1] - OperatorComma, [2] - default value. |
2549 var defaultValues = | 2550 var defaultValues = expr.expressions.length >= 3 |
2550 expr.expressions.length >= 3 ? expr.expressions.sublist(2) : []; | 2551 ? expr.expressions.sublist(2) : <Expression>[]; |
2551 return new VarUsage(paramName, defaultValues, _makeSpan(start)); | 2552 return new VarUsage(paramName, defaultValues, _makeSpan(start)); |
2552 default: | 2553 default: |
2553 var expr = processExpr(); | 2554 var expr = processExpr(); |
2554 if (!_maybeEat(TokenKind.RPAREN)) { | 2555 if (!_maybeEat(TokenKind.RPAREN)) { |
2555 _error("problem parsing function expected ), ", _peekToken.span); | 2556 _error("problem parsing function expected ), ", _peekToken.span); |
2556 } | 2557 } |
2557 | 2558 |
2558 return new FunctionTerm(name, name, expr, _makeSpan(start)); | 2559 return new FunctionTerm(name, name, expr, _makeSpan(start)); |
2559 } | 2560 } |
2560 | 2561 |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2746 | 2747 |
2747 if (replace != null && result == null) { | 2748 if (replace != null && result == null) { |
2748 result = new StringBuffer(text.substring(0, i)); | 2749 result = new StringBuffer(text.substring(0, i)); |
2749 } | 2750 } |
2750 | 2751 |
2751 if (result != null) result.write(replace != null ? replace : text[i]); | 2752 if (result != null) result.write(replace != null ? replace : text[i]); |
2752 } | 2753 } |
2753 | 2754 |
2754 return result == null ? text : result.toString(); | 2755 return result == null ? text : result.toString(); |
2755 } | 2756 } |
OLD | NEW |