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.cmdline.matcher.tokenizer;
021    
022    import org.crsh.cmdline.Delimiter;
023    
024    import java.util.ArrayList;
025    import java.util.Iterator;
026    import java.util.NoSuchElementException;
027    
028    public class TokenizerImpl extends Tokenizer {
029    
030      /** . */
031      private final CharSequence s;
032    
033      /** . */
034      private int index;
035    
036      /** . */
037      private Delimiter delimiter;
038    
039      public TokenizerImpl(CharSequence s) {
040        this.s = s;
041        this.index = 0;
042        this.delimiter = null;
043      }
044    
045      protected Token parse() {
046        Token token = null;
047        if (index < s.length()) {
048          char c = s.charAt(index);
049          int from = index;
050          while (true) {
051            if (Character.isWhitespace(c)) {
052              index++;
053              if (index < s.length()) {
054                c = s.charAt(index);
055              } else {
056                break;
057              }
058            } else {
059              break;
060            }
061          }
062          if (index > from) {
063            token = new Token.Whitespace(from, s.subSequence(from, index).toString());
064          } else {
065            State state = new State();
066            while (true) {
067              if (Character.isWhitespace(c) && state.escape == Escape.NONE) {
068                break;
069              } else {
070                index++;
071                state.push(c);
072                if (index < s.length()) {
073                  c = s.charAt(index);
074                } else {
075                  break;
076                }
077              }
078            }
079            if (index > from) {
080              switch (state.status) {
081                case INIT: {
082                  token = new Token.Literal.Word(from, s.subSequence(from, index).toString(), state.buffer.toString());
083                  break;
084                }
085                case WORD: {
086                  token = new Token.Literal.Word(from, s.subSequence(from, index).toString(), state.buffer.toString());
087                  break;
088                }
089                case SHORT_OPTION: {
090                  token = new Token.Literal.Option.Short(from, s.subSequence(from, index).toString(), state.buffer.toString());
091                  break;
092                }
093                case LONG_OPTION: {
094                  token = new Token.Literal.Option.Long(from, s.subSequence(from, index).toString(), state.buffer.toString());
095                  break;
096                }
097                default:
098                  throw new AssertionError(state.status);
099              }
100              delimiter = state.escape.delimiter;
101              return token;
102            }
103          }
104        }
105        return token;
106      }
107    
108      public Delimiter getDelimiter() {
109        return delimiter;
110      }
111    }