Operators { static Lexicon lexicon(final Collection<String> operatorNames) { final Map<String, Object> operators = new HashMap<String, Object>(); final String[] ops = sort(operatorNames.toArray(new String[operatorNames.size()])); final Parser<?>[] lexers = new Parser<?>[ops.length]; for (int i = 0; i < ops.length; i++) { String s = ops[i]; Parser<?> scanner = s.length() == 1 ? Scanners.isChar(s.charAt(0)) : Scanners.string(s); Object value = Tokens.reserved(s); operators.put(s, value); lexers[i] = scanner.retn(value); } return new Lexicon(operators::get, Parsers.or(lexers)); }    }
@Test public void testLexicon() { List<String> ops = Arrays.asList( "++", "--", "+", "-", "+=", "-=", "+++", "=", "==", "===", "!", "!=", "<", "<=", ">", ">=", "<>"); Lexicon lexicon = Operators.lexicon(ops); for (String op : ops) { assertEquals(Tokens.reserved(op), lexicon.word(op)); } for (String op : ops) { assertEquals(Tokens.reserved(op), lexicon.tokenizer.parse(op)); } }