001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.core.css.lex;
015:
016: import org.itsnat.core.ItsNatException;
017: import java.util.LinkedList;
018: import java.util.List;
019:
020: /**
021: * Una vez creado es de sólo lectura
022: *
023: * @author jmarranz
024: */
025: public abstract class Token {
026: protected int start;
027: protected int end;
028:
029: /** Creates a new instance of Token */
030: public Token(int start) {
031: this .start = start;
032: }
033:
034: public Token() {
035: }
036:
037: public int getStart() {
038: return start;
039: }
040:
041: public int getEnd() {
042: return end;
043: }
044:
045: public static List parse(String code) {
046: Cursor cursor = new Cursor();
047: return parse(code, cursor, false, ' ');
048: }
049:
050: public static List parse(String code, Cursor cursor,
051: boolean checkEndChar, char endChar) {
052: List tokens = new LinkedList();
053: int len = code.length();
054: for (; cursor.getPos() < len; cursor.inc()) {
055: int i = cursor.getPos();
056: char c = code.charAt(i);
057: Token token;
058: if (checkEndChar && (c == endChar))
059: break;
060: if (Space.isSpace(c)) {
061: token = new Space(c, i);
062: } else if (c == ';') {
063: token = new SemiColon(i);
064: } else if (c == ',') {
065: token = new Comma(i);
066: } else if (c == '(') {
067: token = new ParenthesisBlock(code, cursor);
068: } else if (c == '"') {
069: token = new StringDoubleQuote(code, cursor);
070: } else if (c == '\'') {
071: token = new StringSimpleQuote(code, cursor);
072: } else if (c == ':') {
073: token = new Colon(i);
074: } else if (Identifier.isIdentifierStart(c)) {
075: token = new Identifier(code, cursor);
076: } else if (FloatNumber.isFloatNumberStart(c)) {
077: token = new FloatNumber(code, cursor);
078: } else if (HexNumber.isHexNumberStart(c)) {
079: token = new HexNumber(code, cursor);
080: } else
081: throw new ItsNatException("Unexpected char, pos: "
082: + cursor.getPos() + " code: " + code);
083:
084: tokens.add(token);
085: }
086: return tokens;
087: }
088:
089: public boolean equals(Object token) {
090: if (super .equals(token))
091: return true; // identidad de objetos
092: if (!getClass().equals(token.getClass()))
093: return false; // No pueden ser iguales si son de diferente clase
094: return toString().equals(token.toString()); // Mismo tipo y mismo contenido
095: }
096:
097: public int hashCode() {
098: return toString().hashCode();
099: }
100: }
|