-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring to prepare for #186 - removing guards logic from MixinsSo…
…lver
- Loading branch information
meri
committed
Apr 14, 2014
1 parent
8c015b8
commit 22cc81d
Showing
16 changed files
with
341 additions
and
170 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/com/github/sommeri/less4j/core/compiler/expressions/GuardValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.github.sommeri.less4j.core.compiler.expressions; | ||
|
||
import com.github.sommeri.less4j.core.compiler.stages.MixinCompilationResult; | ||
import com.github.sommeri.less4j.utils.ArraysUtils.Filter; | ||
|
||
public enum GuardValue { | ||
USE, DO_NOT_USE, USE_IF_DEFAULT, USE_IF_NOT_DEFAULT; | ||
|
||
public Filter<MixinCompilationResult> filter() { | ||
return new DefaultFunctionUseFilter(this); | ||
} | ||
} | ||
|
||
class DefaultFunctionUseFilter implements Filter<MixinCompilationResult> { | ||
|
||
private final GuardValue value; | ||
|
||
public DefaultFunctionUseFilter(GuardValue value) { | ||
super(); | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public boolean accept(MixinCompilationResult t) { | ||
return t.getGuardValue().equals(value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...in/java/com/github/sommeri/less4j/core/compiler/scopes/FullDetachedRulesetDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.github.sommeri.less4j.core.compiler.scopes; | ||
|
||
import com.github.sommeri.less4j.core.ast.DetachedRuleset; | ||
|
||
public class FullDetachedRulesetDefinition { | ||
private final DetachedRuleset detached; | ||
private final IScope bodyScope; | ||
|
||
public FullDetachedRulesetDefinition(DetachedRuleset detached, IScope mixinsBodyScope) { | ||
super(); | ||
this.detached = detached; | ||
this.bodyScope = mixinsBodyScope; | ||
} | ||
|
||
public DetachedRuleset getDetached() { | ||
return detached; | ||
} | ||
|
||
public IScope getScope() { | ||
return bodyScope; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append("FullDetachedDefinition [detached ["); | ||
builder.append(detached.getSourceLine()).append(":").append(detached.getSourceColumn()); | ||
builder.append("], bodyScope="); | ||
builder.append(bodyScope); | ||
builder.append("]"); | ||
return builder.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/com/github/sommeri/less4j/core/compiler/stages/DefaultGuardHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.github.sommeri.less4j.core.compiler.stages; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import com.github.sommeri.less4j.core.ast.MixinReference; | ||
import com.github.sommeri.less4j.core.ast.ReusableStructure; | ||
import com.github.sommeri.less4j.core.compiler.expressions.GuardValue; | ||
import com.github.sommeri.less4j.core.problems.BugHappened; | ||
import com.github.sommeri.less4j.core.problems.ProblemsHandler; | ||
import com.github.sommeri.less4j.utils.ArraysUtils; | ||
|
||
public class DefaultGuardHelper { | ||
|
||
private final ProblemsHandler problemsHandler; | ||
|
||
public DefaultGuardHelper(ProblemsHandler problemsHandler) { | ||
this.problemsHandler = problemsHandler; | ||
} | ||
|
||
public List<MixinCompilationResult> chooseMixinsToBeUsed(List<MixinCompilationResult> compiledMixins, final MixinReference reference) { | ||
// count how many mixins of each kind we encountered | ||
int normalMixinsCnt = ArraysUtils.count(compiledMixins, GuardValue.USE.filter()); | ||
int ifNotCnt = ArraysUtils.count(compiledMixins, GuardValue.USE_IF_NOT_DEFAULT.filter()); | ||
int ifDefaultCnt = ArraysUtils.count(compiledMixins, GuardValue.USE_IF_DEFAULT.filter()); | ||
|
||
//sanity check - could be removed - keeping only for debugging purposes | ||
if (normalMixinsCnt+ifNotCnt+ifDefaultCnt!=compiledMixins.size()) | ||
throw new BugHappened("Unexpected mixin type in compiled mixins list.", reference); | ||
|
||
// We know now that default() value is false. We do not care whether there was some potentional ambiguity or not and return anything that is not default. | ||
if (normalMixinsCnt > 0) { | ||
return keepOnly(compiledMixins, GuardValue.USE, GuardValue.USE_IF_NOT_DEFAULT); | ||
} | ||
|
||
//there are multiple mixins using default() function and nothing else - that is ambiguous (period). | ||
if (ifDefaultCnt+ifNotCnt > 1) { | ||
List<MixinCompilationResult> errorSet = keepOnly(compiledMixins, GuardValue.USE_IF_DEFAULT,GuardValue.USE_IF_NOT_DEFAULT); | ||
problemsHandler.ambiguousDefaultSet(reference, extractOriginalMixins(errorSet)); | ||
//no mixins are going to be used | ||
return Collections.emptyList(); | ||
} | ||
|
||
//now we know that default function returns true | ||
return keepOnly(compiledMixins, GuardValue.USE_IF_DEFAULT); | ||
} | ||
|
||
/** | ||
* Removes all comiled mixins from compiledMixins list with wrong use of default function. | ||
* Warning: Modifies the compiledMixins list. | ||
* @param compiledMixins - list of compiled mixins - will be modified. | ||
* @param kind - types of mixins that are going to stay. | ||
* @return compiledMixins - for convenience | ||
*/ | ||
private List<MixinCompilationResult> keepOnly(List<MixinCompilationResult> compiledMixins, GuardValue... kind) { | ||
Set<GuardValue> expectedUses = ArraysUtils.asSet(kind); | ||
Iterator<MixinCompilationResult> iterator = compiledMixins.iterator(); | ||
while (iterator.hasNext()) { | ||
MixinCompilationResult compiled = iterator.next(); | ||
if (!expectedUses.contains(compiled.getGuardValue())) { | ||
iterator.remove(); | ||
} | ||
} | ||
return compiledMixins; | ||
} | ||
|
||
private List<ReusableStructure> extractOriginalMixins(List<MixinCompilationResult> compiledMixins) { | ||
List<ReusableStructure> result = new ArrayList<ReusableStructure>(); | ||
for (MixinCompilationResult compiled : compiledMixins) { | ||
result.add(compiled.getMixin()); | ||
} | ||
return result; | ||
} | ||
|
||
|
||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/github/sommeri/less4j/core/compiler/stages/MixinCompilationResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.github.sommeri.less4j.core.compiler.stages; | ||
|
||
import java.util.List; | ||
|
||
import com.github.sommeri.less4j.core.ast.ASTCssNode; | ||
import com.github.sommeri.less4j.core.ast.ReusableStructure; | ||
import com.github.sommeri.less4j.core.compiler.expressions.GuardValue; | ||
import com.github.sommeri.less4j.core.compiler.scopes.IScope; | ||
import com.github.sommeri.less4j.utils.ArraysUtils; | ||
|
||
public class MixinCompilationResult { | ||
|
||
private ReusableStructure mixin; | ||
private List<ASTCssNode> replacement; | ||
private IScope returnValues; | ||
private GuardValue guardValue; | ||
|
||
public MixinCompilationResult(ReusableStructure mixin, List<ASTCssNode> replacement, IScope returnValues) { | ||
this.mixin = mixin; | ||
this.replacement = replacement; | ||
this.returnValues = returnValues; | ||
} | ||
|
||
public void setGuardValue(GuardValue guardValue) { | ||
this.guardValue = guardValue; | ||
} | ||
|
||
public GuardValue getGuardValue() { | ||
return guardValue; | ||
} | ||
|
||
public List<ASTCssNode> getReplacement() { | ||
return replacement; | ||
} | ||
|
||
public void setReplacement(List<ASTCssNode> replacement) { | ||
this.replacement = replacement; | ||
} | ||
|
||
public IScope getReturnValues() { | ||
return returnValues; | ||
} | ||
|
||
public void setReturnValues(IScope returnValues) { | ||
this.returnValues = returnValues; | ||
} | ||
|
||
public ReusableStructure getMixin() { | ||
return mixin; | ||
} | ||
|
||
public void setMixin(ReusableStructure mixin) { | ||
this.mixin = mixin; | ||
} | ||
|
||
@Override | ||
protected MixinCompilationResult clone() { | ||
return new MixinCompilationResult(mixin.clone(), ArraysUtils.deeplyClonedList(replacement), returnValues); | ||
} | ||
|
||
} |
Oops, something went wrong.