use of com.facebook.buck.model.MacroMatchResult in project buck by facebook.
the class StringWithMacrosTypeCoercer method parse.
private StringWithMacros parse(CellPathResolver cellRoots, ProjectFilesystem filesystem, Path pathRelativeToProjectRoot, String blob) throws CoerceFailedException {
ImmutableList.Builder<Either<String, Macro>> parts = ImmutableList.builder();
// Iterate over all macros found in the string, expanding each found macro.
int lastEnd = 0;
MacroFinder.MacroFinderAutomaton matcher = new MacroFinder.MacroFinderAutomaton(blob);
while (matcher.hasNext()) {
MacroMatchResult matchResult = matcher.next();
// Add everything from the original string since the last match to this one.
if (lastEnd < matchResult.getStartIndex()) {
parts.add(Either.ofLeft(blob.substring(lastEnd, matchResult.getStartIndex())));
}
// Look up the macro coercer that owns this macro name.
String name = matchResult.getMacroType();
Class<? extends Macro> clazz = macros.get(name);
if (clazz == null) {
throw new CoerceFailedException(String.format("expanding %s: no such macro \"%s\"", blob.substring(matchResult.getStartIndex(), matchResult.getEndIndex()), matchResult.getMacroType()));
}
MacroTypeCoercer<? extends Macro> coercer = Preconditions.checkNotNull(coercers.get(clazz));
ImmutableList<String> args = matchResult.getMacroInput();
// Delegate to the macro coercers to parse the macro..
Macro macro;
try {
macro = coercer.coerce(cellRoots, filesystem, pathRelativeToProjectRoot, args);
} catch (CoerceFailedException e) {
throw new CoerceFailedException(String.format("macro \"%s\": %s", name, e.getMessage()), e);
}
parts.add(Either.ofRight(macro));
lastEnd = matchResult.getEndIndex();
}
// Append the remaining part of the original string after the last match.
if (lastEnd < blob.length()) {
parts.add(Either.ofLeft(blob.substring(lastEnd, blob.length())));
}
return StringWithMacros.of(parts.build());
}
Aggregations