package mrtim.sasscompiler;

import mrtim.sasscompiler.grammar.SassLexer;
import mrtim.sasscompiler.grammar.SassParser.Selector_combinationContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;

public class SelectorCombinationVisitor extends BaseVisitor<String> {

    @Override
    public String visitSelector_combination(Selector_combinationContext ctx) {
        StringBuffer buffer = new StringBuffer();
        for (int i=0; i<ctx.getChildCount(); i++) {
            ParseTree child = ctx.getChild(i);
            if (child.getChildCount() > 0) {
                if (i > 0) {
                    buffer.append(" ");
                }
                buffer.append(visit(child));
            }
        }
        return buffer.toString();
    }

    @Override
    public String visitTerminal(TerminalNode node) {
        switch (node.getSymbol().getType()) {
            case SassLexer.COMMENT:
                return "";
            case SassLexer.COMMA:
                return ", ";
            default:
                return node.getText();
        }
    }

    @Override
    protected String aggregateResult(String aggregate, String nextResult) {
        if (aggregate == null) {
            aggregate = "";
        }
        if (nextResult == null) {
            nextResult = "";
        }
        return aggregate + nextResult;
    }
}

@@ -141,16 +141,132 @@
 ontext;%0A
+import mrtim.sasscompiler.grammar.SassParser.Simple_selectorContext;%0Aimport org.antlr.v4.runtime.ParserRuleContext;%0A
 import o
@@ -896,32 +896,594 @@
 tring();%0A    %7D%0A%0A
+    @Override%0A    public String visitSimple_selector(Simple_selectorContext ctx) %7B%0A        int prevEnd = -1;%0A        StringBuffer buffer = new StringBuffer();%0A        for (int i=0; i%3Cctx.getChildCount(); i++) %7B%0A            ParserRuleContext child = (ParserRuleContext) ctx.getChild(i);%0A            if (prevEnd %3E -1 && child.getStart().getStartIndex() %3E prevEnd+1) %7B%0A                buffer.append(%22 %22);%0A            %7D%0A            prevEnd = child.getStop().getStopIndex();%0A            buffer.append(visit(child));%0A        %7D%0A        return buffer.toString();%0A    %7D%0A%0A
     @Override%0A  

Check for non-adjacent selector elements to deal with descendant combinators