Keywords { static Lexicon lexicon( Parser<String> wordScanner, Collection<String> keywordNames, StringCase stringCase, final Function<String, ?> defaultMap) { HashMap<String, Object> map = new HashMap<String, Object>(); for (String n : unique(stringCase, keywordNames.toArray(new String[keywordNames.size()]))) { Object value = Tokens.reserved(n); map.put(stringCase.toKey(n), value); } Function<String, Object> keywordMap = stringCase.byKey(map::get); return new Lexicon(keywordMap, wordScanner.map(Lexicon.fallback(keywordMap, defaultMap))); }    }
@Test public void testLexicon_caseSensitive() { List<String> keywords = asList("foo", "Bar"); Lexicon lexicon = Keywords.lexicon( Scanners.IDENTIFIER, keywords, StringCase.CASE_SENSITIVE, TokenizerMaps.IDENTIFIER_FRAGMENT); for (String keyword : keywords) { assertEquals(Tokens.reserved(keyword), lexicon.word(keyword)); } for (String keyword : keywords) { assertEquals(Tokens.reserved(keyword), lexicon.tokenizer.parse(keyword)); } assertEquals(Tokens.identifier("FOO"), lexicon.tokenizer.parse("FOO")); assertEquals(Tokens.identifier("baz"), lexicon.tokenizer.parse("baz")); }
@Test public void testLexicon_caseInsensitive() { List<String> keywords = asList("foo", "Bar"); Lexicon lexicon = Keywords.lexicon( Scanners.IDENTIFIER, keywords, StringCase.CASE_INSENSITIVE, TokenizerMaps.IDENTIFIER_FRAGMENT); for (String keyword : keywords) { assertEquals(Tokens.reserved(keyword), lexicon.word(keyword)); assertEquals(Tokens.reserved(keyword), lexicon.word(keyword.toUpperCase())); } for (String keyword : keywords) { assertEquals(Tokens.reserved(keyword), lexicon.tokenizer.parse(keyword)); assertEquals(Tokens.reserved(keyword), lexicon.tokenizer.parse(keyword.toUpperCase())); } assertEquals(Tokens.identifier("baz"), lexicon.tokenizer.parse("baz")); }