Skip to content

Commit

Permalink
Fix construction of PropertyGrammarElementRef (to be squashed with 38…
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasblaesing committed Feb 5, 2023
1 parent a4840e0 commit ecd64ee
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ public GrammarParser(String expression, String propertyName) {
}

private GroupGrammarElement parse() {
AtomicInteger group_index = new AtomicInteger(0);
// Tracks index of all groups inside this parse invocation
AtomicInteger groupIndex = new AtomicInteger(0);
// Tracks index of property sub definitions inside their parent
AtomicInteger inPropertyIndex = new AtomicInteger(0);
int openedParenthesis = 0;
GroupGrammarElement root = new GroupGrammarElement(null, group_index.getAndIncrement(), propertyName);
GroupGrammarElement root = new GroupGrammarElement(null, groupIndex.getAndIncrement(), propertyName);
ParserInput input = new ParserInput(expression);

parseElements(input, root, false, group_index, openedParenthesis, new HashMap<>());
parseElements(input, root, false, groupIndex, inPropertyIndex, openedParenthesis, new HashMap<>());

if (openedParenthesis != 0) {
throw new IllegalStateException(String.format("Property '%s' parsing error - bracket pairs doesn't match: ", propertyName, openedParenthesis));
Expand All @@ -75,9 +78,12 @@ private GroupGrammarElement parse() {
}

@SuppressWarnings("fallthrough")
private void parseElements(ParserInput input, GroupGrammarElement parent, boolean ignoreInherits,
AtomicInteger group_index, int openedParenthesis, Map<PropertyGrammarElementRef, GroupGrammarElement> knownChilds) {
int localIndex = 0;
private void parseElements(
ParserInput input, GroupGrammarElement parent, boolean ignoreInherits,
AtomicInteger groupIndex, AtomicInteger inPropertyIndex,
int openedParenthesis, Map<PropertyGrammarElementRef,
GroupGrammarElement> knownChilds
) {
GrammarElement last = null;
for (;;) {
char c = input.read();
Expand All @@ -102,8 +108,8 @@ private void parseElements(ParserInput input, GroupGrammarElement parent, boolea
case '[':
openedParenthesis++;
//group start
last = new GroupGrammarElement(parent, group_index.getAndIncrement());
parseElements(input, (GroupGrammarElement) last, false, group_index, openedParenthesis, knownChilds);
last = new GroupGrammarElement(parent, groupIndex.getAndIncrement());
parseElements(input, (GroupGrammarElement) last, false, groupIndex, inPropertyIndex, openedParenthesis, knownChilds);
parent.addElement(last);
break;

Expand Down Expand Up @@ -136,12 +142,8 @@ private void parseElements(ParserInput input, GroupGrammarElement parent, boolea
}
}

//resolve reference
int index = localIndex;
localIndex++;

String referredElementName = buf.toString();
PropertyGrammarElementRef currRef = new PropertyGrammarElementRef(parent, index, referredElementName);
PropertyGrammarElementRef currRef = new PropertyGrammarElementRef(parent, inPropertyIndex.incrementAndGet(), referredElementName);
if(knownChilds.containsKey(currRef)) {
last = knownChilds.get(currRef);
parent.addElement(last);
Expand All @@ -155,12 +157,14 @@ private void parseElements(ParserInput input, GroupGrammarElement parent, boolea

ParserInput pinput = new ParserInput(property.getGrammar());
String propName = property.getName();
last = new GroupGrammarElement(parent, group_index.getAndIncrement(), propName);
last = new GroupGrammarElement(parent, groupIndex.getAndIncrement(), propName);

knownChilds.put(currRef, (GroupGrammarElement) last);

//ignore inherit tokens in the subtree
parseElements(pinput, (GroupGrammarElement) last, true, group_index, openedParenthesis, knownChilds);
// Parsing a property sub definition uses its own inPropertyIndex
AtomicInteger inPropertyIndexChild = new AtomicInteger(0);
parseElements(pinput, (GroupGrammarElement) last, true, groupIndex, inPropertyIndexChild, openedParenthesis, knownChilds);

parent.addElement(last);
}
Expand Down Expand Up @@ -389,19 +393,30 @@ public CharSequence readText() {

private static class PropertyGrammarElementRef {
private final GrammarElement parent;
private final int index;
private final int indexInsideParent;
private final String propertyName;

public PropertyGrammarElementRef(GrammarElement parent, int index, String propertyName) {
public PropertyGrammarElementRef(GrammarElement parent, int indexInsideParent, String propertyName) {
this.parent = parent;
this.index = index;
this.indexInsideParent = indexInsideParent;
this.propertyName = propertyName;
}

// Find the first named parent item (property definition) and return
// its name. If no names parent is found, null is returned
private String getParentName() {
for(GrammarElement p = this.parent; p != null; p = p.parent()) {
if(p.getName() != null) {
return p.getName();
}
}
return null;
}

@Override
public int hashCode() {
int hash = 7;
hash = 29 * hash + this.index;
hash = 29 * hash + this.indexInsideParent;
hash = 29 * hash + Objects.hashCode(this.propertyName);
return hash;
}
Expand All @@ -418,27 +433,13 @@ public boolean equals(Object obj) {
return false;
}
final PropertyGrammarElementRef other = (PropertyGrammarElementRef) obj;
if (this.index != other.index) {
if (this.indexInsideParent != other.indexInsideParent) {
return false;
}
if (!Objects.equals(this.propertyName, other.propertyName)) {
return false;
}
return truncatedPath(this.parent).equals(truncatedPath(other.parent));
}

@SuppressWarnings("AssignmentToMethodParameter")
private String truncatedPath(GrammarElement parent) {
StringBuilder path = new StringBuilder();
while(parent != null) {
path.append(parent.getName());
path.append("/");
if(parent.getName() != null) {
break;
}
parent = parent.parent();
}
return path.toString();
return Objects.equals(getParentName(), other.getParentName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ public void testFont2() {

Iterator<ResolvedToken> itr = resolved.iterator();
String next = itr.next().toString();
assertEquals("[S0|font]/[L1]/[S33|font-size]/[S36|@length]/[L37]/!length (20px(LENGTH;0-4))", next);
assertEquals("[S0|font]/[L1]/[S60|font-size]/[S63|@length]/[L64]/!length (20px(LENGTH;0-4))", next);
next = itr.next().toString();
assertEquals("[S0|font]/[L1]/[L76]// (/(SOLIDUS;5-6))", next);
assertEquals("[S0|font]/[L1]/[L103]// (/(SOLIDUS;5-6))", next);
next = itr.next().toString();
assertEquals("[S0|font]/[L1]/[L76]/[S77|line-height]/[S79|@length]/[L80]/!length (20px(LENGTH;7-11))", next);
assertEquals("[S0|font]/[L1]/[L103]/[S104|line-height]/[S106|@length]/[L107]/!length (20px(LENGTH;7-11))", next);
next = itr.next().toString();
assertEquals("[S0|font]/[L1]/[L83|font-family]/[L84]/[S85]/[S92|@generic-family]/fantasy (fantasy(IDENT;12-19))", next);
assertEquals("[S0|font]/[L1]/[L110|font-family]/[L111]/[S112]/[S119|@generic-family]/fantasy (fantasy(IDENT;12-19))", next);

}

Expand Down

0 comments on commit ecd64ee

Please sign in to comment.