package yokohama.unit.ast;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import yokohama.unit.util.Lists;
import yokohama.unit.util.Pair;

@RequiredArgsConstructor
public class ChoiceCollectVisitor extends 
        StreamVisitorTemplate<Pair<List<Ident>, List<List<Expr>>>> {
    final List<Table> tables;

    @Override
    public Stream<Pair<List<Ident>, List<List<Expr>>>> visitChoiceBinding(ChoiceBinding choiceBinding) {
        List<Ident> idents = Arrays.asList(choiceBinding.getName());
        List<List<Expr>> ess = Lists.map(
                choiceBinding.getChoices(),
                choice -> Arrays.asList(choice));
        return Stream.of(new Pair<>(idents, ess));
    }
}

@@ -66,20 +66,112 @@
 va.util.
-List
+HashMap;%0D%0Aimport java.util.List;%0D%0Aimport java.util.Optional;%0D%0Aimport java.util.stream.Collectors
 ;%0D%0Aimpor
@@ -759,16 +759,16 @@
 ces(),%0D%0A
-
         
@@ -868,12 +868,2740 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    @Override%0D%0A    public Stream%3CPair%3CList%3CIdent%3E, List%3CList%3CExpr%3E%3E%3E%3E visitTableBinding(%0D%0A            TableBinding tableBinding) %7B%0D%0A        List%3CIdent%3E idents = tableBinding.getIdents();%0D%0A        String name = tableBinding.getName();%0D%0A        List%3CList%3CExpr%3E%3E ess;%0D%0A        switch (tableBinding.getType()) %7B%0D%0A            case INLINE:%0D%0A                Optional%3CTable%3E tableOpt =%0D%0A                        tables.stream()%0D%0A                                .filter(table -%3E table.getName().equals(name))%0D%0A                                .findFirst();%0D%0A                if (tableOpt.isPresent()) %7B%0D%0A                    Table table = tableOpt.get();%0D%0A                    List%3CIdent%3E header = table.getHeader();%0D%0A                    ess = table.getRows().stream()%0D%0A                            .map(row -%3E%0D%0A                                    Pair.zip(header, row.getCells())%0D%0A                                            .stream()%0D%0A                                            .collect(%0D%0A                                                    () -%3E new HashMap%3CIdent, Cell%3E(),%0D%0A                                                    (m, p) -%3E m.put(%0D%0A                                                            p.getFirst(),%0D%0A                                                            p.getSecond()),%0D%0A                                                    (m1, m2) -%3E m1.putAll(m2)))%0D%0A                            .map(m -%3E %7B%0D%0A                                return header.stream()%0D%0A                                        .map(ident -%3E m.get(ident))%0D%0A                                        .map(cell -%3E %7B%0D%0A                                            return cell.accept(%0D%0A                                                    expr -%3E expr.getExpr(),%0D%0A                                                    pred -%3E %7B%0D%0A                                                        throw new AstException(%0D%0A                                                                %22invalid reference to predicate cell%22,%0D%0A                                                                tableBinding.getSpan());%0D%0A                                                    %7D);%0D%0A                                        %7D)%0D%0A                                        .collect(Collectors.toList());%0D%0A                            %7D)%0D%0A                            .collect(Collectors.toList());%0D%0A                %7D else %7B%0D%0A                    throw new AstException(%0D%0A                            %22no table named '%22 + name + %22' not found%22,%0D%0A                            tableBinding.getSpan());%0D%0A                %7D%0D%0A                break;%0D%0A            default:%0D%0A                throw new UnsupportedOperationException();%0D%0A        %7D%0D%0A        return Stream.of(new Pair%3C%3E(idents, ess));%0D%0A    %7D%0D
 %0A%7D%0D%0A

Implement visitTableBinding