aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/query/parser/Token.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /container-search/src/main/java/com/yahoo/prelude/query/parser/Token.java
Publish
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude/query/parser/Token.java')
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/query/parser/Token.java117
1 files changed, 117 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/parser/Token.java b/container-search/src/main/java/com/yahoo/prelude/query/parser/Token.java
new file mode 100644
index 00000000000..27ad26279e7
--- /dev/null
+++ b/container-search/src/main/java/com/yahoo/prelude/query/parser/Token.java
@@ -0,0 +1,117 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.prelude.query.parser;
+
+
+import com.yahoo.prelude.query.Substring;
+
+/**
+ * A query token.
+ *
+ * @author <a href="mailto:bratseth@yahoo-inc.com">Jon Bratseth</a>
+ */
+public class Token {
+
+ public static enum Kind {
+ EOF("<EOF>"),
+ NUMBER("<NUMBER>"),
+ WORD("<WORD>"),
+ LETTER("<LETTER>"),
+ DIGIT("<DIGIT>"),
+ SPACE("\" \""),
+ NOISE("<NOISE>"),
+ LATINSIGN("<LATINSIGN>"),
+ QUOTE("\"\\\"\""),
+ MINUS("\"-\""),
+ PLUS("\"+\""),
+ DOT("\".\""),
+ COMMA("\",\""),
+ COLON("\":\""),
+ LBRACE("\"(\""),
+ RBRACE("\")\""),
+ LSQUAREBRACKET("\"[\""),
+ RSQUAREBRACKET("\"]\""),
+ SEMICOLON("\";\""),
+ GREATER("\">\""),
+ SMALLER("\"<\""),
+ EXCLAMATION("\"!\""),
+ UNDERSCORE("\"_\""),
+ HAT("\"^\""),
+ STAR("\"*\""),
+ DOLLAR("\"$\""),
+ DEFAULT("");
+
+ public final String image;
+
+ private Kind(String image) {
+ this.image = image;
+ }
+ }
+
+ /** The raw substring causing this token, never null */
+ public final Substring substring;
+
+ public final Token.Kind kind;
+
+ /** Lowercase image */
+ public final String image;
+
+ /** True if this is a <i>special token</i> */
+ private final boolean special;
+
+ /** Crates a token which fails to know its origin (as a substring). Do not use, except for testing. */
+ public Token(Token.Kind kind, String image) {
+ this(kind,image,false,null);
+ }
+
+ public Token(Token.Kind kind, String image, Substring substring) {
+ this(kind,image,false,substring);
+ }
+
+ public Token(Token.Kind kind, String image, boolean special, Substring substring) {
+ this.kind = kind;
+ this.image = image;
+ this.special = special;
+ this.substring = substring;
+ }
+
+ /** Returns whether this is a <i>special token</i> */
+ public boolean isSpecial() { return special; }
+
+ public String toString() { return image; }
+
+ public boolean equals(Object object) {
+ if (this == object) {
+ return true;
+ }
+ if (object == null) {
+ return false;
+ }
+ if (object.getClass() != this.getClass()) {
+ return false;
+ }
+
+ Token other = (Token) object;
+
+ if (this.kind != other.kind) {
+ return false;
+ }
+ if (!(this.image.equals(other.image))) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Returns the substring containing the image ins original form (including casing),
+ * as well as all the text surrounding the token
+ *
+ * @return the image in original casing, never null
+ */
+ public Substring getSubstring() { return substring; }
+
+ public int hashCode() {
+ return image.hashCode() ^ kind.hashCode();
+ }
+
+}