Skip to content

Commit 3bd1c93

Browse files
author
volker
committed
merge evaluateFunctionsAndExpressions() and replaceVariables() of
SassListItem together to reduced double evaluation of many items
1 parent 7eaeb95 commit 3bd1c93

24 files changed

+69
-185
lines changed

src/main/java/com/inet/sass/parser/ActualArgumentList.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,6 @@ public ActualArgumentList expandVariableArguments() {
155155
return this;
156156
}
157157

158-
public ActualArgumentList replaceVariables(ScssContext context) {
159-
ArgumentList newArgList = arglist.replaceVariables(context);
160-
SassListItem newVarArg = null;
161-
if (hasVariableArguments()) {
162-
newVarArg = variableArgument.replaceVariables(context);
163-
}
164-
return new ActualArgumentList(newArgList, newVarArg);
165-
}
166-
167158
public ActualArgumentList evaluateFunctionsAndExpressions(
168159
ScssContext context, boolean evaluateArithmetics) {
169160
ArgumentList newArgList = arglist.evaluateFunctionsAndExpressions(

src/main/java/com/inet/sass/parser/ArgumentList.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,6 @@ public List<Variable> getNamedVariables() {
5454
return namedVariables;
5555
}
5656

57-
@Override
58-
public ArgumentList replaceVariables(ScssContext context) {
59-
// The actual replacing happens in LexicalUnitImpl, which also
60-
// implements SassListItem.
61-
List<SassListItem> list = new ArrayList<SassListItem>();
62-
for (SassListItem item : this) {
63-
list.add(item.replaceVariables(context));
64-
}
65-
List<Variable> named = new ArrayList<Variable>();
66-
for (Variable var : namedVariables) {
67-
named.add(new Variable(var.getName(), var.getExpr()
68-
.replaceVariables(context), var.isGuarded()));
69-
}
70-
return new ArgumentList(getSeparator(), list, named);
71-
}
72-
7357
@Override
7458
public ArgumentList evaluateFunctionsAndExpressions(ScssContext context,
7559
boolean evaluateArithmetics) {

src/main/java/com/inet/sass/parser/FormalArgumentList.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import java.util.Iterator;
2222
import java.util.List;
2323

24-
import com.inet.sass.ScssContext;
25-
2624
/**
2725
* FormalArgumentList is used for representing the parameter list of a mixin or
2826
* a function definition. Formal arguments are always named and may optionally
@@ -58,24 +56,6 @@ public FormalArgumentList(Collection<Variable> args,
5856
}
5957
}
6058

61-
public FormalArgumentList replaceVariables(ScssContext context) {
62-
ArrayList<Variable> result = new ArrayList<Variable>();
63-
for (final Variable arg : arglist) {
64-
SassListItem expr = arg.getExpr();
65-
if (expr != null) {
66-
expr = expr.replaceVariables(context);
67-
}
68-
if (expr == null) {
69-
Variable var = context.getVariable(arg.getName());
70-
if (var != null) {
71-
expr = var.getExpr();
72-
}
73-
}
74-
result.add(new Variable(arg.getName(), expr));
75-
}
76-
return new FormalArgumentList(result, hasVariableArguments());
77-
}
78-
7959
/**
8060
* Returns a new FormalArgumentList that is obtained from this list by
8161
* replacing all formal arguments with the corresponding actual arguments.

src/main/java/com/inet/sass/parser/Interpolation.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,6 @@ public SassListItem evaluateFunctionsAndExpressions( ScssContext context, boolea
8989
return new StringItem( str );
9090
}
9191

92-
@Override
93-
public SassListItem replaceVariables(ScssContext context) {
94-
SassListItem item = expression.replaceVariables(context);
95-
String str = item.evaluateFunctionsAndExpressions(context, this.evaluateArithmetics).unquotedString();
96-
return new StringItem( str );
97-
}
98-
9992
@Override
10093
public Interpolation updateUrl(String prefix) {
10194
return new Interpolation(expression.updateUrl(prefix), getLineNumber(),

src/main/java/com/inet/sass/parser/LexicalUnitImpl.java

Lines changed: 54 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -645,93 +645,81 @@ public static LexicalUnitImpl createOr( String uri, int line, int column ) {
645645
return new LexicalUnitImpl( uri, line, column, SCSS_OPERATOR_OR );
646646
}
647647

648-
@Override
649-
public SassListItem replaceVariables( ScssContext context ) {
650-
// replace function parameters (if any)
651-
LexicalUnitImpl lui;
652-
ActualArgumentList params = this.params;
653-
if( params != null && (params != (params = params.replaceVariables( context ))) ) {
654-
lui = copy();
655-
lui.params = params;
656-
} else {
657-
lui = this;
658-
}
648+
public boolean containsInterpolation() {
649+
return s != null && s.containsInterpolation();
650+
}
659651

660-
// replace parameters in string value
652+
@Override
653+
public SassListItem evaluateFunctionsAndExpressions( ScssContext context, boolean evaluateArithmetics ) {
661654
switch( type ) {
662655
case SCSS_VARIABLE:
663-
String stringValue = lui.getStringValue();
656+
String stringValue = getStringValue();
664657
Variable var = context.getVariable( stringValue );
665658
if( var != null ) {
666-
return var.getExpr().replaceVariables( context );
659+
return var.getExpr().evaluateFunctionsAndExpressions( context, evaluateArithmetics );
667660
}
668661
varNotResolved = true;
669662
break;
670663
case SCSS_PARENT:
671664
BlockNode parentBlock = context.getParentBlock();
672665
return parentBlock != null ? new StringItem( parentBlock.getSelectors() ) : createNull( uri, line, column );
666+
667+
case SAC_FUNCTION:
668+
case SAC_RGBCOLOR:
669+
String functionName = fname;
670+
if( "calc".equals( functionName ) ) {
671+
return createFunction( uri, line, column, functionName, params.evaluateFunctionsAndExpressions( context, false ) );
672+
}
673+
SCSSFunctionGenerator generator = SCSSFunctionGenerator.getGenerator( functionName );
674+
LexicalUnitImpl copy = this;
675+
if( !"if".equals( functionName ) ) {
676+
copy = createFunction( uri, line, column, functionName, params.evaluateFunctionsAndExpressions( context, true ) );
677+
}
678+
if( generator == null ) {
679+
SassListItem result = copy.replaceCustomFunctions( context );
680+
if( result != null ) {
681+
return result;
682+
}
683+
}
684+
if( generator == null ) {
685+
// log unknown functions
686+
switch( functionName.toLowerCase() ) {
687+
case "brightness":
688+
case "counters":
689+
case "hsl":
690+
case "hsla":
691+
case "linear-gradient":
692+
case "not ":
693+
case "rgba":
694+
case "rotate":
695+
case "scale":
696+
case "translate":
697+
case "translatey":
698+
case "translatex":
699+
case "translatez":
700+
case "url":
701+
case "var":
702+
// ignore well known CSS functions
703+
break;
704+
default:
705+
SCSSErrorHandler.get().warning( "Unknown function: " + functionName );
706+
}
707+
return copy;
708+
}
709+
return generator.compute( context, copy );
710+
673711
default:
674712
StringInterpolationSequence s = this.s;
675713
if( s != null && s.containsInterpolation() ) {
676714
StringInterpolationSequence sis = s.replaceVariables( context );
677715
if( sis != s ) {
678-
LexicalUnitImpl copy = lui.copy();
716+
copy = copy();
679717
copy.s = sis;
680718
return copy;
681719
}
682720
}
683721
}
684-
return lui;
685-
}
686-
687-
public boolean containsInterpolation() {
688-
return s != null && s.containsInterpolation();
689-
}
690-
691-
@Override
692-
public SassListItem evaluateFunctionsAndExpressions( ScssContext context, boolean evaluateArithmetics ) {
693-
String functionName = getFunctionName();
694-
if( params != null && !"calc".equals( functionName ) ) {
695-
SCSSFunctionGenerator generator = SCSSFunctionGenerator.getGenerator( functionName );
696-
LexicalUnitImpl copy = this;
697-
if( !"if".equals( functionName ) ) {
698-
copy = createFunction( uri, line, column, fname, params.evaluateFunctionsAndExpressions( context, true ) );
699-
}
700-
if( generator == null ) {
701-
SassListItem result = copy.replaceCustomFunctions( context );
702-
if( result != null ) {
703-
return result;
704-
}
705-
}
706-
if( generator == null ) {
707-
// log unknown functions
708-
switch( functionName.toLowerCase() ) {
709-
case "brightness":
710-
case "counters":
711-
case "hsl":
712-
case "hsla":
713-
case "linear-gradient":
714-
case "not ":
715-
case "rgba":
716-
case "rotate":
717-
case "scale":
718-
case "translate":
719-
case "translatey":
720-
case "translatex":
721-
case "translatez":
722-
case "url":
723-
case "var":
724-
// ignore well known CSS functions
725-
break;
726-
default:
727-
SCSSErrorHandler.get().warning( "Unknown function: " + functionName );
728-
}
729-
return copy;
730-
}
731-
return generator.compute( context, copy );
732-
} else {
733-
return this;
734-
}
722+
return this;
735723
}
736724

737725
private SassListItem replaceCustomFunctions(ScssContext context) {

src/main/java/com/inet/sass/parser/SassExpression.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,6 @@ public SassListItem evaluateFunctionsAndExpressions( ScssContext context, boolea
220220
return new SassExpression( list );
221221
}
222222

223-
@Override
224-
public SassExpression replaceVariables(ScssContext context) {
225-
List<SassListItem> list = new ArrayList<SassListItem>();
226-
for (SassListItem item : items) {
227-
list.add(item.replaceVariables(context));
228-
}
229-
return new SassExpression(list);
230-
}
231-
232223
@Override
233224
public SassExpression updateUrl(String prefix) {
234225
List<SassListItem> newItems = new ArrayList<SassListItem>(items.size());

src/main/java/com/inet/sass/parser/SassList.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,11 @@ public SassList evaluateFunctionsAndExpressions(ScssContext context,
163163
return new SassList(getSeparator(), list);
164164
}
165165

166-
@Override
167-
public SassList replaceVariables(ScssContext context) {
168-
// The actual replacing happens in LexicalUnitImpl, which also
169-
// implements SassListItem.
170-
return new SassList(getSeparator(), replaceVariables( context, items ) );
171-
}
172-
173166
static List<SassListItem> replaceVariables( ScssContext context, List<SassListItem> items ) {
174167
int size = items.size();
175168
List<SassListItem> list = new ArrayList<SassListItem>( size );
176169
for( int i = 0; i < size; i++ ) {
177-
list.add( items.get( i ).replaceVariables( context ) );
170+
list.add( items.get( i ).evaluateFunctionsAndExpressions( context, true ) );
178171
}
179172
return list;
180173
}

src/main/java/com/inet/sass/parser/SassListItem.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,7 @@ public interface SassListItem extends SourceLocation {
7373
* expressions and functions have been replaced with their evaluated
7474
* values.
7575
*/
76-
public SassListItem evaluateFunctionsAndExpressions(ScssContext context,
77-
boolean evaluateArithmetics);
78-
79-
/**
80-
* Returns a new item that is otherwise equal to this one but all
81-
* occurrences of variables have been replaced by the values in the current
82-
* variable scope. Does not modify this item.
83-
*
84-
* @param context
85-
* the compilation context in which to evaluate functions,
86-
* variables etc.
87-
* @return A SassListItem where all occurrences of variables have been
88-
* replaced by their values.
89-
*/
90-
public SassListItem replaceVariables(ScssContext context);
76+
public SassListItem evaluateFunctionsAndExpressions( ScssContext context, boolean evaluateArithmetics );
9177

9278
/**
9379
* Returns a new item that is obtained from this by updating all url's by,

src/main/java/com/inet/sass/parser/StringItem.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@ public SassListItem evaluateFunctionsAndExpressions(ScssContext context,
6464
return this;
6565
}
6666

67-
@Override
68-
public SassListItem replaceVariables(ScssContext context) {
69-
return this;
70-
}
71-
7267
@Override
7368
public StringItem updateUrl(String prefix) {
7469
// nothing to update

src/main/java/com/inet/sass/tree/DefNode.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public boolean hasVariableArguments() {
6262

6363
@Override
6464
public void replaceVariables(ScssContext context) {
65-
arglist = arglist.replaceVariables(context);
6665
}
6766

6867
public void replacePossibleArguments(ActualArgumentList actualArgumentList) {

0 commit comments

Comments
 (0)