QueryPrologLexer { public static List<Token> lex(String input) { final List<Token> result = new ArrayList<>(); for (int i = 0; i < input.length();) { char c = input.charAt(i); switch (c) { case '#': result.add(HASH_TOKEN); String comment = readComment(input, i); i += comment.length() + 1; result.add(new Token(TokenType.COMMENT, comment)); break; case 'p': case 'P': result.add(PREFIX_KEYWORD_TOKEN); String prefix = readPrefix(input, i); result.add(new Token(TokenType.PREFIX, prefix.trim())); i = i + prefix.length() + 7; break; case 'b': case 'B': result.add(BASE_KEYWORD_TOKEN); i += 4; break; case '<': result.add(LBRACKET_TOKEN); String iri = readIRI(input, i); result.add(new Token(TokenType.IRI, iri)); result.add(RBRACKET_TOKEN); i += iri.length() + 2; break; default: if (Character.isWhitespace(c)) { i++; } else { String restOfQuery = input.substring(i); result.add(new Token(TokenType.REST_OF_QUERY, restOfQuery)); i += restOfQuery.length(); } break; } } return result; }  static List<Token> lex(String input); static Token getRestOfQueryToken(String input);  }
@Test public void testLexEmptyString() { List<Token> tokens = QueryPrologLexer.lex(""); assertNotNull(tokens); assertEquals(0, tokens.size()); }
@Test public void testLexNoProlog1() { List<Token> tokens = QueryPrologLexer.lex("SELECT * WHERE {?s ?p ?o} "); assertNotNull(tokens); assertEquals(1, tokens.size()); Token t = tokens.get(0); assertTrue(t.getType().equals(TokenType.REST_OF_QUERY)); }
@Test public void testLexWithComment() { List<Token> tokens = QueryPrologLexer.lex("# COMMENT \nSELECT * WHERE {?s ?p ?o} "); assertNotNull(tokens); assertEquals(3, tokens.size()); assertEquals(" COMMENT \n", tokens.get(1).s); Token t = tokens.get(tokens.size() - 1); assertTrue(t.getType().equals(TokenType.REST_OF_QUERY)); assertEquals("SELECT * WHERE {?s ?p ?o} ", t.s); }
@Test public void testLexWithComment_WindowsLinebreak() { List<Token> tokens = QueryPrologLexer.lex("# COMMENT \r\nSELECT * WHERE {?s ?p ?o} "); assertNotNull(tokens); Token t = tokens.get(tokens.size() - 1); assertTrue(t.getType().equals(TokenType.REST_OF_QUERY)); assertEquals("SELECT * WHERE {?s ?p ?o} ", t.s); }
@Test public void testLexWithComment_NoSpaceBeforeQuery() { List<Token> tokens = QueryPrologLexer.lex("# COMMENT \nSELECT * WHERE {?s ?p ?o} "); assertNotNull(tokens); Token t = tokens.get(tokens.size() - 1); assertTrue(t.getType().equals(TokenType.REST_OF_QUERY)); assertEquals("SELECT * WHERE {?s ?p ?o} ", t.s); }
@Test public void testLexWithBaseAndComment() { List<Token> tokens = QueryPrologLexer.lex("BASE <foobar> # COMMENT \nSELECT * WHERE {?s ?p ?o} "); assertNotNull(tokens); Token t = tokens.get(tokens.size() - 1); assertTrue(t.getType().equals(TokenType.REST_OF_QUERY)); assertEquals("SELECT * WHERE {?s ?p ?o} ", t.s); }
@Test public void testLexSyntaxError() { try { List<Token> tokens = QueryPrologLexer .lex("BASE <foobar # missing closing bracket \nSELECT * WHERE {?s ?p ?o} "); } catch (Exception e) { fail("malformed query should not make lexer fail"); } }