From 72231250ed81e10d66bfe70701e64fa5fe50f712 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Wed, 15 Jun 2016 23:09:44 +0200 Subject: Publish --- .../java/com/yahoo/prelude/query/parser/Token.java | 117 +++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 container-search/src/main/java/com/yahoo/prelude/query/parser/Token.java (limited to 'container-search/src/main/java/com/yahoo/prelude/query/parser/Token.java') 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 Jon Bratseth + */ +public class Token { + + public static enum Kind { + EOF(""), + NUMBER(""), + WORD(""), + LETTER(""), + DIGIT(""), + SPACE("\" \""), + NOISE(""), + 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 special token */ + 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 special token */ + 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(); + } + +} -- cgit v1.2.3