Methods extracted

This commit is contained in:
Andrey Breslav
2012-08-31 15:14:59 +04:00
parent 1c5236af5c
commit 366b069679
@@ -41,29 +41,41 @@ import java.util.regex.Pattern;
*/
public class ConfluenceHyperlinksGenerator {
private static final String GRAMMAR_EXTENSION = "grm";
private static final List<String> FILE_NAMES_IN_ORDER = Arrays.asList(
"notation",
"toplevel",
"class",
"class_members",
"enum",
"types",
"control",
"expressions",
"when",
"modifiers",
"attributes",
"lexical"
);
public static void main(String[] args) throws IOException {
String grammarExtension = "grm";
File grammarDir = new File("grammar/src");
List<String> fileNamesInOrder = Arrays.asList(
"notation",
"toplevel",
"class",
"class_members",
"enum",
"types",
"control",
"expressions",
"when",
"modifiers",
"attributes",
"lexical"
);
StringBuilder allRules = new StringBuilder();
Set<File> used = new HashSet<File>();
for (String fileName : fileNamesInOrder) {
File file = new File(grammarDir, fileName + "." + grammarExtension);
StringBuilder allRules = getAllRulesInOneString(grammarDir, used);
assertAllFilesAreUsed(grammarDir, used);
StringBuilder output = markDeclarations(allRules);
StringBuilder result = generate(output);
copyToClipboard(result);
}
private static StringBuilder getAllRulesInOneString(File grammarDir, Set<File> used) throws IOException {
StringBuilder allRules = new StringBuilder();
for (String fileName : FILE_NAMES_IN_ORDER) {
File file = new File(grammarDir, fileName + "." + GRAMMAR_EXTENSION);
used.add(file);
FileReader fileReader = new FileReader(file);
try {
@@ -77,15 +89,20 @@ public class ConfluenceHyperlinksGenerator {
fileReader.close();
}
}
return allRules;
}
private static void assertAllFilesAreUsed(File grammarDir, Set<File> used) {
for (File file : grammarDir.listFiles()) {
if (file.getName().endsWith(grammarExtension)) {
if (file.getName().endsWith(GRAMMAR_EXTENSION)) {
if (!used.contains(file)) {
throw new IllegalStateException("Unused grammar file : " + file.getAbsolutePath());
}
}
}
}
private static StringBuilder markDeclarations(StringBuilder allRules) {
StringBuilder output = new StringBuilder();
Pattern symbolReference = Pattern.compile("^\\w+$", Pattern.MULTILINE);
@@ -100,8 +117,10 @@ public class ConfluenceHyperlinksGenerator {
copiedUntil = matcher.end();
}
output.append(allRules.subSequence(copiedUntil, allRules.length()));
// System.out.println(output);
return output;
}
private static StringBuilder generate(StringBuilder output) throws IOException {
_GrammarLexer grammarLexer = new _GrammarLexer((Reader) null);
grammarLexer
.reset(output, 0, output.length(), 0);
@@ -110,20 +129,17 @@ public class ConfluenceHyperlinksGenerator {
Set<String> declaredSymbols = new HashSet<String>();
Set<String> usedSymbols = new HashSet<String>();
Multimap<String, String> usages = Multimaps.newSetMultimap(Maps.<String, Collection<String>>newHashMap(), new Supplier<Set<String>>() {
Multimap<String, String>
usages = Multimaps.newSetMultimap(Maps.<String, Collection<String>>newHashMap(), new Supplier<Set<String>>() {
@Override
public Set<String> get() {
return Sets.newHashSet();
}
});
List<Token> tokens = new ArrayList<Token>();
Declaration lastDeclaration = null;
while (true) {
Token advance = grammarLexer.advance();
if (advance == null) {
break;
}
List<Token> tokens = tokenize(grammarLexer);
Declaration lastDeclaration = null;
for (Token advance: tokens) {
if (advance instanceof Declaration) {
Declaration declaration = (Declaration) advance;
lastDeclaration = declaration;
@@ -135,7 +151,6 @@ public class ConfluenceHyperlinksGenerator {
usages.put(identifier.getName(), lastDeclaration.getName());
usedSymbols.add(identifier.getName());
}
tokens.add(advance);
}
for (Token token : tokens) {
@@ -143,7 +158,7 @@ public class ConfluenceHyperlinksGenerator {
Declaration declaration = (Declaration) token;
result.append("{anchor:").append(declaration.getName()).append("}");
if (!usedSymbols.contains(declaration.getName())) {
// result.append("(!) *Unused!* ");
// result.append("(!) *Unused!* ");
System.out.println("Unused: " + token + " at line " + token.getLine());
}
Collection<String> myUsages = usages.get(declaration.getName());
@@ -170,7 +185,22 @@ public class ConfluenceHyperlinksGenerator {
}
result.append(token);
}
return result;
}
private static List<Token> tokenize(_GrammarLexer grammarLexer) throws IOException {
List<Token> tokens = new ArrayList<Token>();
while (true) {
Token advance = grammarLexer.advance();
if (advance == null) {
break;
}
tokens.add(advance);
}
return tokens;
}
private static void copyToClipboard(StringBuilder result) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(result.toString()), new ClipboardOwner() {
@Override
@@ -179,19 +209,4 @@ public class ConfluenceHyperlinksGenerator {
}
});
}
private static Set<Integer> recordStarts(StringBuilder allRules, String regex) {
Pattern symbolDeclaration = Pattern.compile(regex, Pattern.MULTILINE);
Matcher matcher = symbolDeclaration.matcher(allRules);
Set<Integer> declarationStarts = new HashSet<Integer>();
while (matcher.find()) {
if (matcher.groupCount() > 0) {
declarationStarts.add(matcher.start(1));
}
else {
declarationStarts.add(matcher.start());
}
}
return declarationStarts;
}
}