This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix recursive toString in Grammar, Token and Pattern
- Loading branch information
Dimitry Ivanov
committed
Jul 23, 2018
1 parent
25acbb4
commit cb4a133
Showing
5 changed files
with
181 additions
and
15 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
prism4j-languages/src/test/java/ru/noties/prism4j/ToStringTest.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,25 @@ | ||
package ru.noties.prism4j; | ||
|
||
import org.junit.Test; | ||
|
||
import ru.noties.prism4j.annotations.PrismBundle; | ||
|
||
@PrismBundle(includeAll = true, grammarLocatorClassName = ".GrammarLocatorToStringTest") | ||
public class ToStringTest { | ||
|
||
@Test | ||
public void test() { | ||
|
||
final GrammarLocator locator = new GrammarLocatorToStringTest(); | ||
final Prism4j prism4j = new Prism4j(locator); | ||
|
||
Prism4j.Grammar grammar; | ||
|
||
for (String language : locator.languages()) { | ||
grammar = prism4j.grammar(language); | ||
if (grammar != null) { | ||
System.err.printf("language: %s, toString: %s%n", language, ToString.toString(grammar)); | ||
} | ||
} | ||
} | ||
} |
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
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
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,153 @@ | ||
package ru.noties.prism4j; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
abstract class ToString { | ||
|
||
@NonNull | ||
static String toString(@NonNull Prism4j.Grammar grammar) { | ||
final StringBuilder builder = new StringBuilder(); | ||
toString(builder, new CacheImpl(), grammar); | ||
return builder.toString(); | ||
} | ||
|
||
@NonNull | ||
static String toString(@NonNull Prism4j.Token token) { | ||
final StringBuilder builder = new StringBuilder(); | ||
toString(builder, new CacheImpl(), token); | ||
return builder.toString(); | ||
} | ||
|
||
@NonNull | ||
static String toString(@NonNull Prism4j.Pattern pattern) { | ||
final StringBuilder builder = new StringBuilder(); | ||
toString(builder, new CacheImpl(), pattern); | ||
return builder.toString(); | ||
} | ||
|
||
private ToString() { | ||
} | ||
|
||
private interface Cache { | ||
|
||
boolean visited(@NonNull Object o); | ||
|
||
void markVisited(@NonNull Object o); | ||
} | ||
|
||
private static void toString(@NonNull StringBuilder builder, @NonNull Cache cache, @NonNull Prism4j.Grammar grammar) { | ||
|
||
builder | ||
.append("Grammar{id=0x") | ||
.append(Integer.toHexString(System.identityHashCode(grammar))) | ||
.append(",name=\"") | ||
.append(grammar.name()) | ||
.append('\"'); | ||
|
||
if (cache.visited(grammar)) { | ||
builder.append(",[...]"); | ||
} else { | ||
cache.markVisited(grammar); | ||
builder.append(",tokens=["); | ||
boolean first = true; | ||
for (Prism4j.Token token : grammar.tokens()) { | ||
if (first) { | ||
first = false; | ||
} else { | ||
builder.append(','); | ||
} | ||
toString(builder, cache, token); | ||
} | ||
builder.append(']'); | ||
} | ||
|
||
builder.append('}'); | ||
} | ||
|
||
private static void toString(@NonNull StringBuilder builder, @NonNull Cache cache, @NonNull Prism4j.Token token) { | ||
|
||
builder | ||
.append("Token{id=0x") | ||
.append(Integer.toHexString(System.identityHashCode(token))) | ||
.append(",name=\"") | ||
.append(token.name()) | ||
.append('\"'); | ||
|
||
if (cache.visited(token)) { | ||
builder.append(",[...]"); | ||
} else { | ||
cache.markVisited(token); | ||
builder.append(",patterns=["); | ||
boolean first = true; | ||
for (Prism4j.Pattern pattern : token.patterns()) { | ||
if (first) { | ||
first = false; | ||
} else { | ||
builder.append(','); | ||
} | ||
toString(builder, cache, pattern); | ||
} | ||
builder.append(']'); | ||
} | ||
builder.append('}'); | ||
} | ||
|
||
private static void toString(@NonNull StringBuilder builder, @NonNull Cache cache, @NonNull Prism4j.Pattern pattern) { | ||
|
||
builder | ||
.append("Pattern{id=0x") | ||
.append(Integer.toHexString(System.identityHashCode(pattern))); | ||
|
||
if (cache.visited(pattern)) { | ||
builder.append(",[...]"); | ||
} else { | ||
|
||
cache.markVisited(pattern); | ||
|
||
builder.append(",regex=\"").append(pattern.regex()).append('\"'); | ||
|
||
if (pattern.lookbehind()) { | ||
builder.append(",lookbehind=true"); | ||
} | ||
|
||
if (pattern.greedy()) { | ||
builder.append(",greedy=true"); | ||
} | ||
|
||
if (pattern.alias() != null) { | ||
builder.append(",alias=\"").append(pattern.alias()).append('\"'); | ||
} | ||
|
||
final Prism4j.Grammar inside = pattern.inside(); | ||
if (inside != null) { | ||
builder.append(",inside="); | ||
toString(builder, cache, inside); | ||
} | ||
} | ||
|
||
builder.append('}'); | ||
} | ||
|
||
private static class CacheImpl implements Cache { | ||
|
||
private final Set<Integer> cache = new HashSet<>(3); | ||
|
||
@Override | ||
public boolean visited(@NonNull Object o) { | ||
return cache.contains(key(o)); | ||
} | ||
|
||
@Override | ||
public void markVisited(@NonNull Object o) { | ||
cache.add(key(o)); | ||
} | ||
|
||
@NonNull | ||
private static Integer key(@NonNull Object o) { | ||
return System.identityHashCode(o); | ||
} | ||
} | ||
} |
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