@Test public void fromString() { final StringTemplate tmpl = StringTemplate.fromString("$1-$0"); assertEquals("${1}-${0}", tmpl.toString()); assertEquals("b-a", tmpl.apply( Stream.of( singletonMap(Any2.<Integer, String>left(0), "a"), singletonMap(Any2.<Integer, String>left(1), "b") ) .map(Map::entrySet) .flatMap(Collection::stream) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))); }
public static StringTemplate fromString(String pattern) { class DescriptiveErrorListener extends BaseErrorListener { public List<String> errors = new ArrayList<>(); @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { errors.add(String.format("%d:%d: %s", line, charPositionInLine, msg)); } } final DescriptiveErrorListener error_listener = new DescriptiveErrorListener(); final StringSubstitutionLexer lexer = new StringSubstitutionLexer(CharStreams.fromString(pattern)); lexer.removeErrorListeners(); lexer.addErrorListener(error_listener); final StringSubstitutionParser parser = new StringSubstitutionParser(new UnbufferedTokenStream(lexer)); parser.removeErrorListeners(); parser.addErrorListener(error_listener); parser.setErrorHandler(new BailErrorStrategy()); final StringSubstitutionParser.ExprContext result = parser.expr(); if (result.exception != null) throw new IllegalArgumentException("errors during parsing: " + pattern, result.exception); else if (!error_listener.errors.isEmpty()) throw new IllegalArgumentException("syntax errors during parsing:\n" + String.join("\n", error_listener.errors.stream().map(s -> " " + s).collect(Collectors.toList()))); return result.s; }
StringTemplate implements Function<Map<Any2<Integer, String>, String>, String> { public static StringTemplate fromString(String pattern) { class DescriptiveErrorListener extends BaseErrorListener { public List<String> errors = new ArrayList<>(); @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { errors.add(String.format("%d:%d: %s", line, charPositionInLine, msg)); } } final DescriptiveErrorListener error_listener = new DescriptiveErrorListener(); final StringSubstitutionLexer lexer = new StringSubstitutionLexer(CharStreams.fromString(pattern)); lexer.removeErrorListeners(); lexer.addErrorListener(error_listener); final StringSubstitutionParser parser = new StringSubstitutionParser(new UnbufferedTokenStream(lexer)); parser.removeErrorListeners(); parser.addErrorListener(error_listener); parser.setErrorHandler(new BailErrorStrategy()); final StringSubstitutionParser.ExprContext result = parser.expr(); if (result.exception != null) throw new IllegalArgumentException("errors during parsing: " + pattern, result.exception); else if (!error_listener.errors.isEmpty()) throw new IllegalArgumentException("syntax errors during parsing:\n" + String.join("\n", error_listener.errors.stream().map(s -> " " + s).collect(Collectors.toList()))); return result.s; } }
StringTemplate implements Function<Map<Any2<Integer, String>, String>, String> { public static StringTemplate fromString(String pattern) { class DescriptiveErrorListener extends BaseErrorListener { public List<String> errors = new ArrayList<>(); @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { errors.add(String.format("%d:%d: %s", line, charPositionInLine, msg)); } } final DescriptiveErrorListener error_listener = new DescriptiveErrorListener(); final StringSubstitutionLexer lexer = new StringSubstitutionLexer(CharStreams.fromString(pattern)); lexer.removeErrorListeners(); lexer.addErrorListener(error_listener); final StringSubstitutionParser parser = new StringSubstitutionParser(new UnbufferedTokenStream(lexer)); parser.removeErrorListeners(); parser.addErrorListener(error_listener); parser.setErrorHandler(new BailErrorStrategy()); final StringSubstitutionParser.ExprContext result = parser.expr(); if (result.exception != null) throw new IllegalArgumentException("errors during parsing: " + pattern, result.exception); else if (!error_listener.errors.isEmpty()) throw new IllegalArgumentException("syntax errors during parsing:\n" + String.join("\n", error_listener.errors.stream().map(s -> " " + s).collect(Collectors.toList()))); return result.s; } StringTemplate(List<Element> elements); }
StringTemplate implements Function<Map<Any2<Integer, String>, String>, String> { public static StringTemplate fromString(String pattern) { class DescriptiveErrorListener extends BaseErrorListener { public List<String> errors = new ArrayList<>(); @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { errors.add(String.format("%d:%d: %s", line, charPositionInLine, msg)); } } final DescriptiveErrorListener error_listener = new DescriptiveErrorListener(); final StringSubstitutionLexer lexer = new StringSubstitutionLexer(CharStreams.fromString(pattern)); lexer.removeErrorListeners(); lexer.addErrorListener(error_listener); final StringSubstitutionParser parser = new StringSubstitutionParser(new UnbufferedTokenStream(lexer)); parser.removeErrorListeners(); parser.addErrorListener(error_listener); parser.setErrorHandler(new BailErrorStrategy()); final StringSubstitutionParser.ExprContext result = parser.expr(); if (result.exception != null) throw new IllegalArgumentException("errors during parsing: " + pattern, result.exception); else if (!error_listener.errors.isEmpty()) throw new IllegalArgumentException("syntax errors during parsing:\n" + String.join("\n", error_listener.errors.stream().map(s -> " " + s).collect(Collectors.toList()))); return result.s; } StringTemplate(List<Element> elements); static StringTemplate fromString(String pattern); @Override String apply(Map<Any2<Integer, String>, String> args); int indexSize(); StringBuilder configString(); @Override String toString(); List<Any2<Integer, String>> getArguments(); }
StringTemplate implements Function<Map<Any2<Integer, String>, String>, String> { public static StringTemplate fromString(String pattern) { class DescriptiveErrorListener extends BaseErrorListener { public List<String> errors = new ArrayList<>(); @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { errors.add(String.format("%d:%d: %s", line, charPositionInLine, msg)); } } final DescriptiveErrorListener error_listener = new DescriptiveErrorListener(); final StringSubstitutionLexer lexer = new StringSubstitutionLexer(CharStreams.fromString(pattern)); lexer.removeErrorListeners(); lexer.addErrorListener(error_listener); final StringSubstitutionParser parser = new StringSubstitutionParser(new UnbufferedTokenStream(lexer)); parser.removeErrorListeners(); parser.addErrorListener(error_listener); parser.setErrorHandler(new BailErrorStrategy()); final StringSubstitutionParser.ExprContext result = parser.expr(); if (result.exception != null) throw new IllegalArgumentException("errors during parsing: " + pattern, result.exception); else if (!error_listener.errors.isEmpty()) throw new IllegalArgumentException("syntax errors during parsing:\n" + String.join("\n", error_listener.errors.stream().map(s -> " " + s).collect(Collectors.toList()))); return result.s; } StringTemplate(List<Element> elements); static StringTemplate fromString(String pattern); @Override String apply(Map<Any2<Integer, String>, String> args); int indexSize(); StringBuilder configString(); @Override String toString(); List<Any2<Integer, String>> getArguments();  }