Skip to content

Commit

Permalink
Better debug to strings for structures and added scope to expressions.
Browse files Browse the repository at this point in the history
Related to #186
  • Loading branch information
jurcovicovam committed Jun 21, 2014
1 parent 4928723 commit 6bf9ad3
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/github/sommeri/less4j/core/ast/ASTCssNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,14 @@ public void configureParentToAllChilds() {
}
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(this.getClass().getSimpleName()).append(" (");
builder.append(getSourceLine()).append(":").append(getSourceColumn());
builder.append(")");

return builder.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class DetachedRulesetReference extends ASTCssNode {

public DetachedRulesetReference(HiddenTokenAwareTree token, Variable variable) {
super(token);
this.variable = variable;
}

public Variable getVariable() {
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/com/github/sommeri/less4j/core/ast/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,36 @@
import java.util.Collections;
import java.util.List;

import com.github.sommeri.less4j.core.ast.annotations.NotAstProperty;
import com.github.sommeri.less4j.core.compiler.scopes.IScope;
import com.github.sommeri.less4j.core.parser.HiddenTokenAwareTree;

public abstract class Expression extends ASTCssNode {
public abstract class Expression extends ASTCssNode implements IScopeAware {

private IScope ownerScope;

public Expression(HiddenTokenAwareTree token) {
super(token);
}

@Override
@NotAstProperty
public IScope getScope() {
return ownerScope;
}

@Override
@NotAstProperty
public void setScope(IScope scope) {
this.ownerScope = scope;
}

@Override
@NotAstProperty
public boolean hasScope() {
return ownerScope!=null;
}

public List<Expression> splitByComma() {
return Collections.singletonList(this);
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/github/sommeri/less4j/core/ast/IScopeAware.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.sommeri.less4j.core.ast;

import com.github.sommeri.less4j.core.ast.annotations.NotAstProperty;
import com.github.sommeri.less4j.core.compiler.scopes.IScope;

/**
* Contains scope associated with the node. It would be much more cleaner if
* the solution would keep ast completely independent from scope. That being said,
* I did not found such clean solution that would not be also fragile or too
* complicated or required too much boiler plate like code.
*
*/
public interface IScopeAware {

@NotAstProperty
IScope getScope();

@NotAstProperty
void setScope(IScope scope);

@NotAstProperty
boolean hasScope();

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.github.sommeri.less4j.core.ast.ListExpressionOperator.Operator;
import com.github.sommeri.less4j.core.ast.annotations.NotAstProperty;
import com.github.sommeri.less4j.core.compiler.scopes.IScope;
import com.github.sommeri.less4j.core.parser.HiddenTokenAwareTree;
import com.github.sommeri.less4j.utils.ArraysUtils;

Expand All @@ -20,6 +21,11 @@ public ListExpression(HiddenTokenAwareTree token, List<Expression> expressions,
this.operator = operator;
}

public ListExpression(HiddenTokenAwareTree token, List<Expression> expressions, ListExpressionOperator operator, IScope scope) {
this(token, expressions, operator);
setScope(scope);
}

public ListExpressionOperator getOperator() {
return operator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public IScope firstChild() {
public Expression getValue(Variable variable) {
return getValue(variable.getName());
}

public Expression getValue(String name) {
Expression value = getLocalValue(name);

if (value == null && hasParent())
value = getParent().getValue(name);

return value;
}

Expand Down Expand Up @@ -62,7 +62,6 @@ public boolean seesLocalDataOf(IScope otherScope) {
return getParent().seesLocalDataOf(otherScope);
}


public IScope getRootScope() {
if (!hasParent())
return this;
Expand Down Expand Up @@ -119,7 +118,13 @@ public String toFullName() {

private String toSimpleName() {
List<String> names = getNames();
return "" + getType() + names;
if (names!=null && !names.isEmpty())
return getType() + names;

if (ScopeFactory.DEFAULT!=getType())
return getType() + "[" + getOwner().toString() + "]";

return getType();
}

public String toLongString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Iterator;

import com.github.sommeri.less4j.core.problems.BugHappened;

public class IteratedScope {

Expand All @@ -24,6 +25,8 @@ public IteratedScope getNextChild() {
do {
if (childsIterator.hasNext())
child = childsIterator.next();
else
throw new BugHappened("Child scope was expected but missing", getScope().getOwner());
} while (!child.isPresentInAst());

return new IteratedScope(child);
Expand Down

0 comments on commit 6bf9ad3

Please sign in to comment.