CSSScanner.java 19.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
/* CSSScanner.java -- A parser for CSS stylesheets
   Copyright (C) 2006 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package gnu.javax.swing.text.html.css;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * A tokenizer for CSS stylesheets. This is based on the scanner definition
 * from:
 *
 * http://www.w3.org/TR/CSS21/syndata.html#tokenization
 *
 * @author Roman Kennke (kennke@aicas.com)
 */
// TODO: Maybe implement more restrictive scanner:
// http://www.w3.org/TR/CSS21/grammar.html#q2
class CSSScanner
{

  // The tokens. This list is taken from:
  // http://www.w3.org/TR/CSS21/syndata.html#tokenization
  static final int IDENT = 1;
  static final int ATKEYWORD = 2;
  static final int STRING = 3;
  static final int INVALID = 4;
  static final int HASH = 5;
  static final int NUMBER = 6;
  static final int PERCENTAGE = 7;
  static final int DIMENSION = 8;
  static final int URI = 9;
  static final int UNICODE_RANGE = 10;
  static final int CDO = 11;
  static final int CDC = 12;
  static final int SEMICOLON = 13;
  static final int CURLY_LEFT = 14;
  static final int CURLY_RIGHT = 15;
  static final int PAREN_LEFT = 16;
  static final int PAREN_RIGHT = 17;
  static final int BRACE_LEFT = 16;
  static final int BRACE_RIGHT = 17;
  static final int S = 18;
  static final int COMMENT = 19;
  static final int FUNCTION = 20;
  static final int INCLUDES = 21;
  static final int DASHMATCH = 22;
  static final int DELIM = 23;

  // Additional tokens defined for convenience.
  static final int EOF = -1;

  /**
   * The input source.
   */
  private Reader in;

  /**
   * The parse buffer.
   */
  char[] parseBuffer;

  /**
   * The end index in the parseBuffer of the current token.
   */
  int tokenEnd;

  /**
   * The lookahead 'buffer'.
   */
  private int[] lookahead;

  CSSScanner(Reader r)
  {
    lookahead = new int[2];
    lookahead[0] = -1;
    lookahead[1] = -1;
    parseBuffer = new char[2048];
    in = r;
  }

  /**
   * Fetches the next token. The actual character data is in the parseBuffer
   * afterwards with the tokenStart at index 0 and the tokenEnd field
   * pointing to the end of the token.
   *
   * @return the next token
   */
  int nextToken()
    throws IOException
  {
    tokenEnd = 0;
    int token = -1;
    int next = read();
    if (next != -1)
      {
        switch (next)
        {
          case ';':
            parseBuffer[0] = (char) next;
            tokenEnd = 1;
            token = SEMICOLON;
            break;
          case '{':
            parseBuffer[0] = (char) next;
            tokenEnd = 1;
            token = CURLY_LEFT;
            break;
          case '}':
            parseBuffer[0] = (char) next;
            tokenEnd = 1;
            token = CURLY_RIGHT;
            break;
          case '(':
            parseBuffer[0] = (char) next;
            tokenEnd = 1;
            token = PAREN_LEFT;
            break;
          case ')':
            parseBuffer[0] = (char) next;
            tokenEnd = 1;
            token = PAREN_RIGHT;
            break;
          case '[':
            parseBuffer[0] = (char) next;
            tokenEnd = 1;
            token = BRACE_LEFT;
            break;
          case ']':
            parseBuffer[0] = (char) next;
            tokenEnd = 1;
            token = BRACE_RIGHT;
            break;
          case '@':
            parseBuffer[0] = (char) next;
            tokenEnd = 1;
            readIdent();
            token = ATKEYWORD;
            break;
          case '#':
            parseBuffer[0] = (char) next;
            tokenEnd = 1;
            readName();
            token = HASH;
            break;
          case '\'':
          case '"':
            lookahead[0] = next;
            readString();
            token = STRING;
            break;
          case ' ':
          case '\t':
          case '\r':
          case '\n':
          case '\f':
            lookahead[0] = next;
            readWhitespace();
            token = S;
            break;
            // FIXME: Detecting an URI involves several characters lookahead.
//          case 'u':
//            lookahead[0] = ch;
//            readURI();
//            token = URI;
//            break;
          case '<':
            parseBuffer[0] = (char) next;
            parseBuffer[1] = (char) read();
            parseBuffer[2] = (char) read();
            parseBuffer[3] = (char) read();
            if (parseBuffer[1] == '!' && parseBuffer[2] == '-'
              && parseBuffer[3] == '-')
              {
                token = CDO;
                tokenEnd = 4;
              }
            else
              throw new CSSLexicalException("expected CDO token");
            break;
          case '/':
            lookahead[0] = next;
            readComment();
            token = COMMENT;
            break;
          case '~':
            parseBuffer[0] = (char) next;
            parseBuffer[1] = (char) read();
            if (parseBuffer[1] == '=')
              token = INCLUDES;
            else
              throw new CSSLexicalException("expected INCLUDES token");
            break;
          case '|':
            parseBuffer[0] = (char) next;
            parseBuffer[1] = (char) read();
            if (parseBuffer[1] == '=')
              token = DASHMATCH;
            else
              throw new CSSLexicalException("expected DASHMATCH token");
            break;
          case '-':
            int ch2 = read();
            if (ch2 == '-')
              {
                int ch3 = read();
                if (ch3 == '>')
                  {
                    parseBuffer[0] = (char) next;
                    parseBuffer[1] = (char) ch2;
                    parseBuffer[2] = (char) ch3;
                    tokenEnd = 3;
                    token = CDC;
                  }
                else
                  throw new CSSLexicalException("expected CDC token");
              }
            else
              {
                lookahead[0] = next;
                lookahead[1] = ch2;
                readIdent();
                int ch3 = read();
                if (ch3 == -1 || ch3 != '(')
                  {
                    lookahead[0] = ch3;
                    token = IDENT;
                  }
                else
                  {
                    parseBuffer[tokenEnd] = (char) ch3;
                    tokenEnd++;
                    token = FUNCTION;
                  }
              }
            break;
          case '0':
          case '1':
          case '2':
          case '3':
          case '4':
          case '5':
          case '6':
          case '7':
          case '8':
          case '9':
            lookahead[0] = next;
            readNum();
            int ch3 = read();
            if (ch3 == '%')
              {
                parseBuffer[tokenEnd] = (char) ch3;
                tokenEnd++;
                token = PERCENTAGE;
              }
            else if (ch3 == -1 || (! (ch3 == '_'
                                      || (ch3 >= 'a' && ch3 <= 'z')
                                      || (ch3 >= 'A' && ch3 <= 'Z')
                                      || ch3 == '\\' || ch3 > 177)))
              {
                lookahead[0] = ch3;
                token = NUMBER;
              }
            else
              {
                lookahead[0] = ch3;
                readIdent();
                token = DIMENSION;
              }
            break;
          default:
            // Handle IDENT that don't begin with '-'.
            if (next == '_' || (next >= 'a' && next <= 'z')
                || (next >= 'A' && next <= 'Z') || next == '\\' || next > 177)
              {
                lookahead[0] = next;
                readIdent();
                int ch4 = read();
                if (ch4 == -1 || ch4 != '(')
                  {
                    lookahead[0] = ch4;
                    token = IDENT;
                  }
                else
                  {
                    parseBuffer[tokenEnd] = (char) ch4;
                    tokenEnd++;
                    token = FUNCTION;
                  }
              }
            else
              {
                parseBuffer[0] = (char) next;
                tokenEnd = 1;
                token = DELIM;
              }
          break;
        }
      }
    return token;
  }

  String currentTokenString()
  {
    return new String(parseBuffer, 0, tokenEnd);
  }

  /**
   * Reads one character from the input stream or from the lookahead
   * buffer, if it contains one character.
   *
   * @return the next character
   *
   * @throws IOException if problems occur on the input source
   */
  private int read()
    throws IOException
  {
    int ret;
    if (lookahead[0] != -1)
      {
        ret = lookahead[0];
        lookahead[0] = -1;
      }
    else if (lookahead[1] != -1)
      {
        ret = lookahead[1];
        lookahead[1] = -1;
      }
    else
      {
        ret = in.read();
      }
    return ret;
  }

  /**
   * Reads and identifier.
   *
   * @throws IOException if something goes wrong in the input source or if
   *         the lexical analyser fails to read an identifier
   */
  private void readIdent()
    throws IOException
  {
    int ch1 = read();
    // Read possibly leading '-'.
    if (ch1 == '-')
      {
        parseBuffer[tokenEnd] = (char) ch1;
        tokenEnd++;
        ch1 = read();
      }
    // What follows must be '_' or a-z or A-Z or nonascii (>177) or an
    // escape.
    if (ch1 == '_' || (ch1 >= 'a' && ch1 <= 'z')
        || (ch1 >= 'A' && ch1 <= 'Z') || ch1 > 177)
      {
        parseBuffer[tokenEnd] = (char) ch1;
        tokenEnd++;
      }
    else if (ch1 == '\\')
      {
        // Try to read an escape.
        lookahead[0] = ch1;
        readEscape();
      }
    else
      throw new CSSLexicalException("First character of identifier incorrect");

    // Read any number of [_a-zA-Z0-9-] chars.
    int ch = read();
    while (ch != -1 && (ch == '_' || ch == '-' || (ch >= 'a' && ch <= 'z')
           || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')))
      {
        parseBuffer[tokenEnd] = (char) ch;
        tokenEnd++;
        ch = read();
      }

    // Push back last read character since it doesn't belong to the IDENT.
    lookahead[0] = ch;
  }

  /**
   * Reads an escape.
   *
   * @throws IOException if something goes wrong in the input source or if
   *         the lexical analyser fails to read an escape
   */
  private void readEscape()
    throws IOException
  {
    int ch = read();
    if (ch != -1 && ch == '\\')
      {
        parseBuffer[tokenEnd] = (char) ch;
        tokenEnd++;
        ch = read();
        if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f'))
          {
            // Read unicode escape.
            // Zero to five 0-9a-f chars can follow.
            int hexcount = 0;
            ch = read();
            while (((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f'))
                   && hexcount < 5)
              {
                parseBuffer[tokenEnd] = (char) ch;
                tokenEnd++;
                hexcount++;
                ch = read();
              }
            // Now we can have a \r\n or any whitespace character following.
            if (ch == '\r')
              {
                parseBuffer[tokenEnd] = (char) ch;
                tokenEnd++;
                ch = read();
                if (ch == '\n')
                  {
                    parseBuffer[tokenEnd] = (char) ch;
                    tokenEnd++;
                  }
                else
                  {
                    lookahead[0] = ch;
                  }
              }
            else if (ch == ' ' || ch == '\n' || ch == '\f' || ch == '\t')
              {
                parseBuffer[tokenEnd] = (char) ch;
                tokenEnd++;
              }
            else
              {
                lookahead[0] = ch;
              }
          }
        else if (ch != '\n' && ch != '\r' && ch != '\f')
          {
            parseBuffer[tokenEnd] = (char) ch;
            tokenEnd++;
          }
        else
          throw new CSSLexicalException("Can't read escape");
      }
    else
      throw new CSSLexicalException("Escape must start with '\\'");
    
  }

  private void readName()
    throws IOException
  {
    // Read first name character.
    int ch = read();
    if (ch != -1 && (ch == '_' || ch == '-' || (ch >= 'a' && ch <= 'z')
           || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')))
      {
        parseBuffer[tokenEnd] = (char) ch;
        tokenEnd++;
      }
    else
      throw new CSSLexicalException("Invalid name");

    // Read any number (at least one) of [_a-zA-Z0-9-] chars.
    ch = read();
    while (ch != -1 && (ch == '_' || ch == '-' || (ch >= 'a' && ch <= 'z')
           || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')))
      {
        parseBuffer[tokenEnd] = (char) ch;
        tokenEnd++;
        ch = read();
      }

    // Push back last read character since it doesn't belong to the IDENT.
    lookahead[0] = ch;
  }

  /**
   * Reads in a string.
   *
   * @throws IOException
   */
  private void readString()
    throws IOException
  {
    int ch1 = read();
    if (ch1 != -1 && (ch1 == '\'' || ch1 == '\"'))
      {
        parseBuffer[tokenEnd] = (char) ch1;
        tokenEnd++;

        // Read any number of chars until we hit another chc1 char.
        // Reject newlines, except if prefixed with \.
        int ch = read();
        while (ch != -1 && ch != ch1)
          {
            // Every non-newline and non-\ char should be ok.
            if (ch != '\n' && ch != '\r' && ch != '\f' && ch != '\\')
              {
                parseBuffer[tokenEnd] = (char) ch;
                tokenEnd++;
              }
            // Ok when followed by newline or as part of escape.
            else if (ch == '\\')
              {
                int ch2 = read();
                if (ch2 == '\n' || ch2 == '\r')
                  {
                    parseBuffer[tokenEnd] = (char) ch;
                    parseBuffer[tokenEnd + 1] = (char) ch2;
                    tokenEnd += 2;
                  }
                else
                  {
                    // Try to parse an escape.
                    lookahead[0] = ch;
                    lookahead[1] = ch2;
                    readEscape();
                  }
              }
            else
              throw new CSSLexicalException("Invalid string");

            ch = read();
          }
        if (ch != -1)
          {
            // Push the final char on the buffer.
            parseBuffer[tokenEnd] = (char) ch;
            tokenEnd++;
          }
        else
          throw new CSSLexicalException("Unterminated string");
      }
    else
      throw new CSSLexicalException("Invalid string");
  }

  /**
   * Reads a chunk of whitespace.
   *
   * @throws IOException
   */
  private void readWhitespace()
    throws IOException
  {
    int ch = read();
    while (ch != -1 && (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'
           || ch == '\f'))
      {
        parseBuffer[tokenEnd] = (char) ch;
        tokenEnd++;
        ch = read();
      }
    // Push back last character read.
    lookahead[0] = ch;
    
  }

  private void readURI()
    throws IOException
  {
    // FIXME: Implement.
  }

  /**
   * Reads a comment block.
   *
   * @throws IOException
   */
  private void readComment()
    throws IOException
  {
    // First we need a / and a *
    int ch = read();
    if (ch != -1 && ch == '/')
      {
        parseBuffer[tokenEnd] = (char) ch;
        tokenEnd++;
        ch = read();
        if (ch != -1 && ch == '*')
          {
            parseBuffer[tokenEnd] = (char) ch;
            tokenEnd++;
            ch = read();
            parseBuffer[tokenEnd] = (char) ch;
            tokenEnd++;
            boolean finished = false;
            int lastChar = ch;
            ch = read();
            while (! finished && ch != -1)
              {
                if (lastChar == '*' && ch == '/')
                  finished = true;
                parseBuffer[tokenEnd] = (char) ch;
                tokenEnd++;
                lastChar = ch;
                ch = read();
              }
          }
      }
    if (ch == -1)
      throw new CSSLexicalException("Unterminated comment");
    
    // Push back last character read.
    lookahead[0] = ch;
  }

  /**
   * Reads a number.
   *
   * @throws IOException
   */
  private void readNum()
    throws IOException
  {
    boolean hadDot = false;
    // First char must be number or .
    int ch = read();
    if (ch != -1 && ((ch >= '0' && ch <= '9') || ch == '.'))
      {
        if (ch == '.')
          hadDot = true;
        parseBuffer[tokenEnd] = (char) ch;
        tokenEnd++;
        // Now read in any number of digits afterwards, and maybe one dot,
        // if we hadn't one already.
        ch = read();
        while (ch != -1 && ((ch >= '0' && ch <= '9')
                            || (ch == '.' && ! hadDot)))
          {
            if (ch == '.')
              hadDot = true;
            parseBuffer[tokenEnd] = (char) ch;
            tokenEnd++;
            ch = read();
          }                            
      }
    else
      throw new CSSLexicalException("Invalid number");

    // Check if we haven't accidentally finished with a dot.
    if (parseBuffer[tokenEnd - 1] == '.')
      throw new CSSLexicalException("Invalid number");

    // Push back last character read.
    lookahead[0] = ch;
  }

  /**
   * For testing, we read in the default.css in javax/swing/text/html
   *
   * @param args
   */
  public static void main(String[] args)
  {
    try
      {
        String name = "/javax/swing/text/html/default.css";
        InputStream in = CSSScanner.class.getResourceAsStream(name);
        BufferedInputStream bin = new BufferedInputStream(in);
        InputStreamReader r = new InputStreamReader(bin);
        CSSScanner s = new CSSScanner(r);
        int token;
        do
          {
            token = s.nextToken();
            System.out.println("token: " + token + ": "
                               + s.currentTokenString());
          } while (token != -1);
      }
    catch (IOException ex)
      {
        ex.printStackTrace();
      }
  }
}