StructuredQuery { public static Node parse(String query) { ArrayList<StructuredLexer.Token> tokens; try { tokens = StructuredLexer.tokens(query); } catch (Exception e) { e.printStackTrace(); return new Node("text", ""); } TokenStream stream = new TokenStream(tokens); ArrayList<Node> arguments = parseArgumentList(stream); if (arguments.size() == 0) { return new Node("text", ""); } else if (arguments.size() == 1) { return arguments.get(0); } else { return new Node("root", arguments, 0); } }  static Token parseParameterTerm(TokenStream tokens); static NodeParameters parseParameters(TokenStream tokens); static Node parseOperator(TokenStream tokens); static Node parseQuotedTerms(TokenStream tokens); static Node parseTerm(TokenStream tokens); static Node parseUnrestricted(TokenStream tokens); static ArrayList<Node> parseFieldList(TokenStream tokens); static Node nodeWithOptionalExtentOr(String operator, Node child, ArrayList<Node> orFields); static Node parseRestricted(TokenStream tokens); static Node parseArgument(TokenStream tokens); static ArrayList<Node> parseArgumentList(TokenStream tokens); static Node parse(String query); static Set<String> findQueryTerms(Node queryTree); static Set<String> findQueryTerms(Node queryTree, Set<String> operators);  }
@Test public void testFieldCombinationParseCommas() { String query = "a.b,c"; Node result = StructuredQuery.parse(query); assertEquals( "#inside( #text:a() #extentor( #field:b() #field:c() ) )", result.toString()); }
@Test public void testParensParse() { String query = "a.(b) a.(b,c)"; Node result = StructuredQuery.parse(query); assertEquals( "#root( #smoothinside( #text:a() #field:b() ) #smoothinside( #text:a() #extentor( #field:b() #field:c() ) ) )", result.toString()); }
@Test public void testParameterDoubles() { String query = "#combine:0=1.2:3.4=5( a b )"; Node result = StructuredQuery.parse(query); assertEquals("#combine:0=1.2:3.4=5( #text:a() #text:b() )", result.toString()); }
@Test public void testPrettyPrinter() { String query = "#combine:0=1.2:3.4=5( #a( d e ) b )"; Node result1 = StructuredQuery.parse(query); String prettyQuery = result1.toPrettyString(); Node result2 = StructuredQuery.parse(prettyQuery); assert result1.equals(result2); }
@Test public void testText() { String query = "hello"; Node result = StructuredQuery.parse(query); assertEquals("#text:hello()", result.toString()); }
@Test public void testTextOperator() { String query = "#text:hello()"; Node result = StructuredQuery.parse(query); assertEquals("#text:hello()", result.toString()); }
@Test public void testSimpleCombine() { String query = "#combine( hello )"; Node result = StructuredQuery.parse(query); assertEquals("#combine( #text:hello() )", result.toString()); }
@Test public void testSimpleTextCombine() { String query = "#combine( #text:hello() )"; Node result = StructuredQuery.parse(query); assertEquals("#combine( #text:hello() )", result.toString()); }
@Test public void testSimpleParse() { String query = "#combine( #bm25(a) b )"; Node tree = createQuery(); Node result = StructuredQuery.parse(query); assertEquals(tree.toString(), result.toString()); }
@Test public void testFieldParse() { String query = "#combine( a.b c.d @/e/ @/f. h/.g )"; Node result = StructuredQuery.parse(query); assertEquals( "#combine( #inside( #text:a() #field:b() ) #inside( #text:c() #field:d() ) #text:e() #inside( #text:f. h() #field:g() ) )", result.toString()); }
@Test public void testFieldCombinationParse() { String query = "a.b.c"; Node result = StructuredQuery.parse(query); assertEquals( "#inside( #inside( #text:a() #field:b() ) #field:c() )", result.toString()); }
@Test public void testFieldWindow() { String query = "#1(a b).c"; Node result = StructuredQuery.parse(query); assertEquals( "#inside( #1( #text:a() #text:b() ) #field:c() )", result.toString()); }
@Test public void testFieldSmoothWindow() { String query = "#1(a b).(c)"; Node result = StructuredQuery.parse(query); assertEquals( "#smoothinside( #1( #text:a() #text:b() ) #field:c() )", result.toString()); }