Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: lib/parser.dart

Issue 1407333002: Added beginning support for calc however, the expression is not fully parsed into the AST. (Closed) Base URL: https://github.com/dart-lang/csslib.git@master
Patch Set: Update CHANGELOG.md Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « CHANGELOG.md ('k') | lib/src/css_printer.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 2170 matching lines...) Expand 10 before | Expand all | Expand 10 after
2181 _error('Expecting a positive number', _makeSpan(start)); 2181 _error('Expecting a positive number', _makeSpan(start));
2182 } 2182 }
2183 2183
2184 _eat(TokenKind.RBRACK); 2184 _eat(TokenKind.RBRACK);
2185 2185
2186 return new ItemTerm(term.value, term.text, _makeSpan(start)); 2186 return new ItemTerm(term.value, term.text, _makeSpan(start));
2187 case TokenKind.IDENTIFIER: 2187 case TokenKind.IDENTIFIER:
2188 var nameValue = identifier(); // Snarf up the ident we'll remap, maybe. 2188 var nameValue = identifier(); // Snarf up the ident we'll remap, maybe.
2189 2189
2190 if (!ieFilter && _maybeEat(TokenKind.LPAREN)) { 2190 if (!ieFilter && _maybeEat(TokenKind.LPAREN)) {
2191 var calc = processCalc(nameValue);
2192 if (calc != null) return calc;
2191 // FUNCTION 2193 // FUNCTION
2192 return processFunction(nameValue); 2194 return processFunction(nameValue);
2193 } 2195 }
2194 if (ieFilter) { 2196 if (ieFilter) {
2195 if (_maybeEat(TokenKind.COLON) && 2197 if (_maybeEat(TokenKind.COLON) &&
2196 nameValue.name.toLowerCase() == 'progid') { 2198 nameValue.name.toLowerCase() == 'progid') {
2197 // IE filter:progid: 2199 // IE filter:progid:
2198 return processIEFilter(start); 2200 return processIEFilter(start);
2199 } else { 2201 } else {
2200 // Handle filter:<name> where name is any filter e.g., alpha, chroma , 2202 // Handle filter:<name> where name is any filter e.g., alpha, chroma ,
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
2435 startAfterProgidColon.start.offset, _peekToken.start); 2437 startAfterProgidColon.start.offset, _peekToken.start);
2436 return new LiteralTerm(tok.text, tok.text, tok.span); 2438 return new LiteralTerm(tok.text, tok.text, tok.span);
2437 } 2439 }
2438 break; 2440 break;
2439 default: 2441 default:
2440 _eat(_peek()); 2442 _eat(_peek());
2441 } 2443 }
2442 } 2444 }
2443 } 2445 }
2444 2446
2447 // TODO(terry): Hack to gobble up the calc expression as a string looking
2448 // for the matching RPAREN the expression is not parsed into the
2449 // AST.
2450 //
2451 // grammar should be:
2452 //
2453 // <calc()> = calc( <calc-sum> )
2454 // <calc-sum> = <calc-product> [ [ '+' | '-' ] <calc-product> ]*
2455 // <calc-product> = <calc-value> [ '*' <calc-value> | '/' <number> ]*
2456 // <calc-value> = <number> | <dimension> | <percentage> | ( <calc-sum> )
2457 //
2458 String processCalcExpression() {
2459 var inString = tokenizer._inString;
2460 tokenizer._inString = false;
2461
2462 // Gobble up everything until we hit our stop token.
2463 var stringValue = new StringBuffer();
2464 var left = 1;
2465 var matchingParens = false;
2466 while (_peek() != TokenKind.END_OF_FILE && !matchingParens) {
2467 var token = _peek();
2468 if (token == TokenKind.LPAREN)
2469 left++;
2470 else if (token == TokenKind.RPAREN)
2471 left--;
2472
2473 matchingParens = left == 0;
2474 if (!matchingParens) stringValue.write(_next().text);
2475 }
2476
2477 if (!matchingParens) {
2478 _error("problem parsing function expected ), ", _peekToken.span);
2479 }
2480
2481 tokenizer._inString = inString;
2482
2483 return stringValue.toString();
2484 }
2485
2486 CalcTerm processCalc(Identifier func) {
2487 var start = _peekToken.span;
2488
2489 var name = func.name;
2490 if (name == 'calc') {
2491 // TODO(terry): Implement expression parsing properly.
2492 String expression = processCalcExpression();
2493 var calcExpr = new LiteralTerm(expression, expression, _makeSpan(start));
2494
2495 if (!_maybeEat(TokenKind.RPAREN)) {
2496 _error("problem parsing function expected ), ", _peekToken.span);
2497 }
2498
2499 return new CalcTerm(name, name, calcExpr, _makeSpan(start));
2500 }
2501
2502 return null;
2503 }
2504
2445 // Function grammar: 2505 // Function grammar:
2446 // 2506 //
2447 // function: IDENT '(' expr ')' 2507 // function: IDENT '(' expr ')'
2448 // 2508 //
2449 processFunction(Identifier func) { 2509 processFunction(Identifier func) {
2450 var start = _peekToken.span; 2510 var start = _peekToken.span;
2451 2511
2452 var name = func.name; 2512 var name = func.name;
2453 2513
2454 switch (name) { 2514 switch (name) {
2455 case 'url': 2515 case 'url':
2456 // URI term sucks up everything inside of quotes(' or ") or between pare ns 2516 // URI term sucks up everything inside of quotes(' or ") or between pare ns
2457 var urlParam = processQuotedString(true); 2517 var urlParam = processQuotedString(true);
2458 2518
2459 // TODO(terry): Better error messge and checking for mismatched quotes. 2519 // TODO(terry): Better error messge and checking for mismatched quotes.
2460 if (_peek() == TokenKind.END_OF_FILE) { 2520 if (_peek() == TokenKind.END_OF_FILE) {
2461 _error("problem parsing URI", _peekToken.span); 2521 _error("problem parsing URI", _peekToken.span);
2462 } 2522 }
2463 2523
2464 if (_peek() == TokenKind.RPAREN) { 2524 if (_peek() == TokenKind.RPAREN) {
2465 _next(); 2525 _next();
2466 } 2526 }
2467 2527
2468 return new UriTerm(urlParam, _makeSpan(start)); 2528 return new UriTerm(urlParam, _makeSpan(start));
2469 case 'calc':
2470 // TODO(terry): Implement expression handling...
2471 break;
2472 case 'var': 2529 case 'var':
2473 // TODO(terry): Consider handling var in IE specific filter/progid. Thi s 2530 // TODO(terry): Consider handling var in IE specific filter/progid. Thi s
2474 // will require parsing entire IE specific syntax e.g., 2531 // will require parsing entire IE specific syntax e.g.,
2475 // param = value or progid:com_id, etc. for example: 2532 // param = value or progid:com_id, etc. for example:
2476 // 2533 //
2477 // var-blur: Blur(Add = 0, Direction = 225, Strength = 10); 2534 // var-blur: Blur(Add = 0, Direction = 225, Strength = 10);
2478 // var-gradient: progid:DXImageTransform.Microsoft.gradient" 2535 // var-gradient: progid:DXImageTransform.Microsoft.gradient"
2479 // (GradientType=0,StartColorStr='#9d8b83', EndColorStr='#847670'); 2536 // (GradientType=0,StartColorStr='#9d8b83', EndColorStr='#847670');
2480 var expr = processExpr(); 2537 var expr = processExpr();
2481 if (!_maybeEat(TokenKind.RPAREN)) { 2538 if (!_maybeEat(TokenKind.RPAREN)) {
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
2689 2746
2690 if (replace != null && result == null) { 2747 if (replace != null && result == null) {
2691 result = new StringBuffer(text.substring(0, i)); 2748 result = new StringBuffer(text.substring(0, i));
2692 } 2749 }
2693 2750
2694 if (result != null) result.write(replace != null ? replace : text[i]); 2751 if (result != null) result.write(replace != null ? replace : text[i]);
2695 } 2752 }
2696 2753
2697 return result == null ? text : result.toString(); 2754 return result == null ? text : result.toString();
2698 } 2755 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | lib/src/css_printer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698