001 /*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.cli.impl.tokenizer;
021
022 public abstract class Token {
023
024
025 public final static class Whitespace extends Token {
026
027 public Whitespace(int index, String raw) {
028 super(index, raw);
029 }
030
031 @Override
032 public boolean equals(Object obj) {
033 if (obj == this) {
034 return true;
035 }
036 if (obj instanceof Whitespace) {
037 Whitespace that = (Whitespace)obj;
038 return super.equals(obj) && index == that.index;
039 }
040 return false;
041 }
042
043 @Override
044 public String toString() {
045 return "Token.Whitespace[index=" + index + ",raw=" + raw + "]";
046 }
047 }
048
049 public abstract static class Literal extends Token {
050
051 public abstract static class Option extends Literal {
052
053 /** . */
054 private final String name;
055
056 public final String getName() {
057 return name;
058 }
059
060 Option(int index, String raw, String value, String name) {
061 super(index, raw, value);
062 this.name = name;
063 }
064
065 public final static class Short extends Option {
066 public Short(int index, String raw, String value) {
067 super(index, raw, value, value.substring(1));
068 }
069 public Short(int index, String value) {
070 this(index, value, value);
071 }
072 }
073
074 public final static class Long extends Option {
075 public Long(int index, String value) {
076 this(index, value, value);
077 }
078 public Long(int index, String raw, String value) {
079 super(index, raw, value, value.substring(2));
080 }
081 }
082 }
083
084 public final static class Word extends Literal {
085 public Word(int index, String raw, String value) {
086 super(index, raw, value);
087 }
088
089 public Word(int index, String value) {
090 super(index, value);
091 }
092 }
093
094 /** . */
095 final String value;
096
097 Literal(int index, String value) {
098 this(index, value, value);
099 }
100
101 Literal(int index, String raw, String value) {
102 super(index, raw);
103
104 if (value == null) {
105 throw new NullPointerException();
106 }
107
108 //
109 this.value = value;
110 }
111
112 public String getValue() {
113 return value;
114 }
115
116 @Override
117 public boolean equals(Object obj) {
118 if (obj == this) {
119 return true;
120 }
121 if (obj.getClass().equals(getClass())) {
122 Literal that = (Literal)obj;
123 return super.equals(obj) && index == that.index && value.equals(that.value);
124 }
125 return false;
126 }
127
128 @Override
129 public String toString() {
130 return getClass().getSimpleName() + "[index=" + index + ",raw=" + raw + ",value=" + value + "]";
131 }
132 }
133
134 /** The index in the containing sequence. */
135 final int index;
136
137 /** . */
138 final String raw;
139
140 Token(int index, String raw) {
141
142 if (index < 0) {
143 throw new IllegalArgumentException();
144 }
145 if (raw == null) {
146 throw new NullPointerException();
147 }
148
149 //
150 this.index = index;
151 this.raw = raw;
152 }
153
154 /**
155 * Returns the raw text.
156 *
157 * @return the raw text
158 */
159 public String getRaw() {
160 return raw;
161 }
162
163 /**
164 * Returns the from index is the containing string.
165 *
166 * @return the from index
167 */
168 public int getFrom() {
169 return index;
170 }
171
172 /**
173 * Returns the to index in the containing string.
174 *
175 * @return the to index
176 */
177 public int getTo() {
178 return index + raw.length();
179 }
180
181 @Override
182 public boolean equals(Object obj) {
183 if (obj == this) {
184 return true;
185 }
186 if (obj instanceof Token) {
187 Token that = (Token)obj;
188 return index == that.index && raw.equals(that.raw);
189 }
190 return false;
191 }
192 }