Skip to content
This repository was archived by the owner on Feb 23, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion src/com/google/common/css/SourceCodeLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.css.compiler.ast.CssNode;
import com.google.common.primitives.Ints;

import java.util.Iterator;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -198,6 +199,37 @@ public static SourceCodeLocation getUnknownLocation() {
return result;
}

/**
* Returns a new SourceCodeLocation which covers everything between the
* beginning of the first location and the end of the second location.
*/
public static SourceCodeLocation merge(SourceCodeLocation beginLocation,
SourceCodeLocation endLocation) {
Preconditions.checkArgument(beginLocation.sourceCode.equals(endLocation.sourceCode));
return new SourceCodeLocation(beginLocation.sourceCode,
beginLocation.getBeginCharacterIndex(),
beginLocation.getBeginLineNumber(),
beginLocation.getBeginIndexInLine(),
endLocation.getEndCharacterIndex(),
endLocation.getEndLineNumber(),
endLocation.getEndIndexInLine());
}

public static SourceCodeLocation merge(
Iterable<? extends CssNode> locations) {
Iterator<? extends CssNode> i = locations.iterator();
if (!i.hasNext()) {
return getUnknownLocation();
}
SourceCodeLocation loc = i.next().getSourceCodeLocation();
while (i.hasNext()) {
SourceCodeLocation iLoc = i.next().getSourceCodeLocation();
if (iLoc == null || iLoc.isUnknown()) continue;
loc = merge(loc, iLoc);
}
return loc;
}

private final SourceCode sourceCode;

/**
Expand Down Expand Up @@ -241,10 +273,18 @@ public int getBeginCharacterIndex() {
return begin.getCharacterIndex();
}

/**
* The index of the line that contains the first character of the node. Indexes start at 1; 0
* means the location is not known.
*/
public int getBeginLineNumber() {
return begin.getLineNumber();
}

/**
* The index of the column that contains the first character of the node. Indexes start at 1; 0
* means the location is not known.
*/
public int getBeginIndexInLine() {
return begin.getIndexInLine();
}
Expand All @@ -253,10 +293,18 @@ public int getEndCharacterIndex() {
return end.getCharacterIndex();
}

/**
* The index of the line that contains the last character of the node. Indexes start at 1; 0
* means the location is not known.
*/
public int getEndLineNumber() {
return end.getLineNumber();
}

/**
* The index of the column that comes after the last character of the node. Indexes start at 1; 0
* means the location is not known.
*/
public int getEndIndexInLine() {
return end.getIndexInLine();
}
Expand Down Expand Up @@ -325,4 +373,15 @@ public int compareTo(SourceCodeLocation o) {
}
return end.compareTo(o.end);
}

@Override
public String toString() {
return String.format(
"%s: line %d, col %d -> line %d, col %d",
sourceCode.getFileName(),
begin.getLineNumber(),
begin.getIndexInLine(),
end.getLineNumber(),
end.getIndexInLine());
}
}
101 changes: 48 additions & 53 deletions src/com/google/common/css/compiler/ast/CssDeclarationNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.base.Preconditions;

import com.google.common.css.SourceCodeLocation;
import java.util.List;

import javax.annotation.Nullable;
Expand All @@ -39,83 +40,77 @@ public class CssDeclarationNode extends CssNode {
*/
private boolean hasStarHack;

/**
* Constructor of a node representing a CSS declaration.
*
* @param propertyName
*/
/** Constructor of a node representing a CSS declaration. */
public CssDeclarationNode(CssPropertyNode propertyName) {
this(propertyName, new CssPropertyValueNode());
this(propertyName, new CssPropertyValueNode(), null /* sourceCodeLocation */);
}

/**
* Constructor of a node representing a CSS declaration.
*
* @param propertyName
* @param comments
*/
public CssDeclarationNode(CssPropertyNode propertyName,
List<CssCommentNode> comments) {
this(propertyName, new CssPropertyValueNode(), comments);
/** Constructor of a node representing a CSS declaration. */
public CssDeclarationNode(
CssPropertyNode propertyName, @Nullable SourceCodeLocation sourceCodeLocation) {
this(propertyName, new CssPropertyValueNode(), sourceCodeLocation);
}

/**
* Constructor of a node representing a CSS declaration.
*
* @param propertyName
* @param propertyValue
*/
public CssDeclarationNode(CssPropertyNode propertyName,
CssPropertyValueNode propertyValue) {
this(propertyName, propertyValue, null);
/** Constructor of a node representing a CSS declaration. */
public CssDeclarationNode(CssPropertyNode propertyName, List<CssCommentNode> comments) {
this(propertyName, comments, null /* sourceCodeLocation */);
}

/**
* Constructor of a node representing a CSS declaration.
*
* @param propertyName
* @param propertyValue
* @param comments
*/
public CssDeclarationNode(CssPropertyNode propertyName,
CssPropertyValueNode propertyValue,
@Nullable List<CssCommentNode> comments) {
this(propertyName, propertyValue, comments, false);
/** Constructor of a node representing a CSS declaration. */
public CssDeclarationNode(
CssPropertyNode propertyName,
List<CssCommentNode> comments,
@Nullable SourceCodeLocation sourceCodeLocation) {
this(propertyName, new CssPropertyValueNode(), comments, sourceCodeLocation);
}

/**
* Constructor of a node representing a CSS declaration.
*
* @param propertyName
* @param propertyValue
* @param comments
* @param hasStarHack
*/
public CssDeclarationNode(CssPropertyNode propertyName,
CssPropertyValueNode propertyValue,
@Nullable List<CssCommentNode> comments,
boolean hasStarHack) {
super(null, comments, null);
/** Constructor of a node representing a CSS declaration. */
public CssDeclarationNode(
CssPropertyNode propertyName,
CssPropertyValueNode propertyValue,
@Nullable SourceCodeLocation sourceCodeLocation) {
this(propertyName, propertyValue, null, sourceCodeLocation);
}

/** Constructor of a node representing a CSS declaration. */
public CssDeclarationNode(
CssPropertyNode propertyName,
CssPropertyValueNode propertyValue,
@Nullable List<CssCommentNode> comments,
@Nullable SourceCodeLocation sourceCodeLocation) {
this(propertyName, propertyValue, comments, sourceCodeLocation, false);
}

/** Constructor of a node representing a CSS declaration. */
public CssDeclarationNode(
CssPropertyNode propertyName,
CssPropertyValueNode propertyValue,
@Nullable List<CssCommentNode> comments,
@Nullable SourceCodeLocation sourceCodeLocation,
boolean hasStarHack) {
super(null, comments, sourceCodeLocation);
this.propertyName = propertyName;
this.propertyValue = propertyValue;
becomeParentForNode(this.propertyName);
becomeParentForNode(this.propertyValue);
this.setStarHack(hasStarHack);
}

/**
* Copy constructor.
*
* @param node
*/
/** Copy constructor. */
public CssDeclarationNode(CssDeclarationNode node) {
this(
node.getPropertyName().deepCopy(),
node.getPropertyValue().deepCopy(),
node.getComments(),
node.getSourceCodeLocation(),
node.hasStarHack());
}

public CssDeclarationNode(CssPropertyNode propertyNode,
CssPropertyValueNode cssPropertyValueNode) {
this(propertyNode, cssPropertyValueNode, null /* sourceCodeLocation */);
}

@Override
public CssDeclarationNode deepCopy() {
return new CssDeclarationNode(this);
Expand Down
10 changes: 10 additions & 0 deletions src/com/google/common/css/compiler/ast/CssNodesListNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

import com.google.common.css.SourceCodeLocation;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -189,4 +190,13 @@ public String toString() {

return output.toString();
}

@Override
public SourceCodeLocation getSourceCodeLocation() {
SourceCodeLocation location = super.getSourceCodeLocation();
if (location == null) {
location = SourceCodeLocation.merge(children);
}
return location;
}
}
13 changes: 7 additions & 6 deletions src/com/google/common/css/compiler/ast/GssParserCC.jj
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public class GssParserCC {
int charIndex1 = charStream.convertToCharacterIndex(lineNumber1,
indexInLine1);
int lineNumber2 = t.endLine;
int indexInLine2 = t.endColumn;
int indexInLine2 = t.endColumn + 1; // Need to advance 1 to be beyond the end of the token.
int charIndex2 = charStream.convertToCharacterIndex(lineNumber2,
indexInLine2);
return new SourceCodeLocation(sourceCode, charIndex1, lineNumber1,
Expand Down Expand Up @@ -423,10 +423,11 @@ public class GssParserCC {

public CssDeclarationNode buildDeclarationNode(CssPropertyNode property,
CssPropertyValueNode value, List<Token> tokens) {
CssDeclarationNode node = new CssDeclarationNode(property, value);
node.setSourceCodeLocation(
mergeLocations(
property.getSourceCodeLocation(), value.getSourceCodeLocation()));
CssDeclarationNode node =
new CssDeclarationNode(
property,
value,
mergeLocations(property.getSourceCodeLocation(), value.getSourceCodeLocation()));
attachComments(tokens, node);
return node;
}
Expand Down Expand Up @@ -797,7 +798,7 @@ CssRulesetNode ruleSet() :
t = <RIGHTBRACE> { tokens.add(t); }
{
CssRulesetNode ruleSet = nodeBuilder.buildRulesetNode(declarations,
selectors, this.getLocation(), tokens);
selectors, mergeLocations(selectors.getSourceCodeLocation(), getLocation()), tokens);
return ruleSet;
}
} catch (ParseException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ public boolean enterDeclaration(CssDeclarationNode declaration) {
for (CssPropertyValueNode ruleValueNode : rule.getValueOnlyExpansionNodes()) {
// For valueOnlyExpansionNodes the property name comes from the declaration.
CssDeclarationNode expansionNode =
new CssDeclarationNode(declaration.getPropertyName(), ruleValueNode.deepCopy());
expansionNode.setSourceCodeLocation(declaration.getSourceCodeLocation());
new CssDeclarationNode(
declaration.getPropertyName(),
ruleValueNode.deepCopy(),
declaration.getSourceCodeLocation());
expansionNode.appendComment(new CssCommentNode("/* @alternate */", null));
expansionNodes.add(expansionNode);
}
Expand Down Expand Up @@ -207,8 +209,8 @@ private static List<CssDeclarationNode> expandMatchingValueOnlyFunctions(
// For valueOnlyExpansionNodes the property name comes from the declaration.
CssPropertyValueNode expansionValues = new CssPropertyValueNode(expansionNodeValues);
CssDeclarationNode expansionNode =
new CssDeclarationNode(declaration.getPropertyName(), expansionValues);
expansionNode.setSourceCodeLocation(declaration.getSourceCodeLocation());
new CssDeclarationNode(
declaration.getPropertyName(), expansionValues, declaration.getSourceCodeLocation());
expansionNode.appendComment(new CssCommentNode("/* @alternate */", null));
expansionNodes.add(expansionNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ private CssPropertyValueNode rebuildFont(
? ImmutableList.<CssValueNode>of()
: ImmutableList.<CssValueNode>of(reparseFamilies(
families,
getSourceCodeLocation(Iterables.get(families, 0))));
SourceCodeLocation.merge(families)));
ImmutableList.Builder<CssValueNode> resultNodes = ImmutableList.builder();
resultNodes.addAll(Iterables.concat(preFamily, tail));
if (priority != null) {
Expand Down Expand Up @@ -611,6 +611,8 @@ private void collect(List<CssValueNode> alternatives, CssValueNode item) {
alternatives.add(stump);
}
stump.setValue(stump.getValue() + item.getValue());
stump.setSourceCodeLocation(
SourceCodeLocation.merge(stump.getSourceCodeLocation(), item.getSourceCodeLocation()));
for (CssCommentNode c : item.getComments()) {
stump.appendComment(c);
}
Expand Down
Loading