All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xapi.source.impl.LexerForWords Maven / Gradle / Ivy

Go to download

This module exists solely to package all other gwt modules into a single uber jar. This makes deploying to non-mavenized targets much easier. Of course, you would be wise to inherit your dependencies individually; the uber jar is intended for projects like collide, which have complex configuration, and adding many jars would be a pain.

The newest version!
package xapi.source.impl;

import xapi.source.api.CharIterator;
import xapi.source.api.Lexer;

public class LexerForWords extends LexerDefault {

  @Override
  protected Lexer consume(char c, CharIterator str) {
    switch (c) {
      case '\r':
        if (str.hasNext() && str.peek()=='\n') {
          c = str.next();//treat \r\n as \n
        }
      case ' ':
      case '\n':
      case '\t':
        // TODO add literals for 0xA, and other nbsp values
        return onWhitespace(c, str);
      default:
        // start a word
        return onWordStart(c, str);
    }
  }

  protected boolean isWhitespace(char c) {
    switch (c) {
      case '\r':
      case '\n':
      case '\t':
      case ' ':
        return true;
      default:
        return false;
    }
  }

  protected Lexer onWordStart(char c, CharIterator str) {
    return onWord(extractWord(c, str), str);
  }

  protected String extractWord(char c, CharIterator str) {
    // Consume everything until we see whitespace again
    StringBuilder b = new StringBuilder().append(c);
    while (str.hasNext()) {
      if (isWhitespace(str.peek())) {
        return b.toString();
      }
      b.append(str.next());
    }
    return b.toString();
  }

  protected Lexer onWord(String word, CharIterator str) {
    if (str.hasNext()) {
      return consume(str);
    }
    return this;
  }

  protected Lexer onWhitespace(char c, CharIterator str) {
    // Default implementation does nothing.
    return super.consume(c, str);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy