Search in sources :

Example 1 with Macro

use of com.facebook.buck.rules.macros.Macro in project buck by facebook.

the class StringWithMacrosTypeCoercer method traverse.

@Override
public void traverse(StringWithMacros stringWithMacros, Traversal traversal) {
    for (Macro macro : stringWithMacros.getMacros()) {
        MacroTypeCoercer<? extends Macro> coercer = Preconditions.checkNotNull(coercers.get(macro.getClass()));
        traverse(coercer, macro, traversal);
    }
}
Also used : Macro(com.facebook.buck.rules.macros.Macro)

Example 2 with Macro

use of com.facebook.buck.rules.macros.Macro 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());
}
Also used : MacroMatchResult(com.facebook.buck.model.MacroMatchResult) ImmutableList(com.google.common.collect.ImmutableList) Macro(com.facebook.buck.rules.macros.Macro) MacroFinder(com.facebook.buck.model.MacroFinder) Either(com.facebook.buck.model.Either)

Aggregations

Macro (com.facebook.buck.rules.macros.Macro)2 Either (com.facebook.buck.model.Either)1 MacroFinder (com.facebook.buck.model.MacroFinder)1 MacroMatchResult (com.facebook.buck.model.MacroMatchResult)1 ImmutableList (com.google.common.collect.ImmutableList)1