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 import java.util.ArrayList;
023 import java.util.Iterator;
024 import java.util.NoSuchElementException;
025
026 public abstract class Tokenizer implements Iterator<Token> {
027
028 /** . */
029 private ArrayList<Token> stack;
030
031 /** . */
032 private int ptr;
033
034 /** . */
035 private int index = 0;
036
037 protected Tokenizer() {
038 this.stack = new ArrayList<Token>();
039 this.ptr = 0;
040 }
041
042 public final boolean hasNext() {
043 if (ptr < stack.size()) {
044 return true;
045 } else {
046 Token next = parse();
047 if (next != null) {
048 stack.add(next);
049 }
050 return next != null;
051 }
052 }
053
054 public final void pushBack(int count) {
055 if (count < 0) {
056 throw new IllegalArgumentException();
057 }
058 if (ptr - count < 0) {
059 throw new IllegalStateException("Trying to push back too many tokens");
060 } else {
061 while (count > 0) {
062 index -= stack.get(--ptr).raw.length();
063 count--;
064 }
065 }
066 }
067
068 public final Token peek() {
069 if (hasNext()) {
070 return stack.get(ptr);
071 } else {
072 return null;
073 }
074 }
075
076 public final Token next() {
077 if (hasNext()) {
078 Token token = stack.get(ptr++);
079 index += token.raw.length();
080 return token;
081 } else {
082 throw new NoSuchElementException();
083 }
084 }
085
086 public final void remove() {
087 throw new UnsupportedOperationException();
088 }
089
090 public final void pushBack() {
091 pushBack(1);
092 }
093
094 protected abstract Token parse();
095
096 public final int getIndex() {
097 return index;
098 }
099
100 }