From 366b0696799b9801b1decc11ca15b82347411206 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Fri, 31 Aug 2012 15:14:59 +0400 Subject: [PATCH] Methods extracted --- .../ConfluenceHyperlinksGenerator.java | 107 ++++++++++-------- 1 file changed, 61 insertions(+), 46 deletions(-) diff --git a/grammar/src/org/jetbrains/jet/grammar/ConfluenceHyperlinksGenerator.java b/grammar/src/org/jetbrains/jet/grammar/ConfluenceHyperlinksGenerator.java index d4aed84c73a..358d77cb227 100644 --- a/grammar/src/org/jetbrains/jet/grammar/ConfluenceHyperlinksGenerator.java +++ b/grammar/src/org/jetbrains/jet/grammar/ConfluenceHyperlinksGenerator.java @@ -41,29 +41,41 @@ import java.util.regex.Pattern; */ public class ConfluenceHyperlinksGenerator { + private static final String GRAMMAR_EXTENSION = "grm"; + private static final List 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 fileNamesInOrder = Arrays.asList( - "notation", - "toplevel", - "class", - "class_members", - "enum", - "types", - "control", - "expressions", - "when", - "modifiers", - "attributes", - "lexical" - ); - - StringBuilder allRules = new StringBuilder(); Set used = new HashSet(); - 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 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 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 declaredSymbols = new HashSet(); Set usedSymbols = new HashSet(); - Multimap usages = Multimaps.newSetMultimap(Maps.>newHashMap(), new Supplier>() { + Multimap + usages = Multimaps.newSetMultimap(Maps.>newHashMap(), new Supplier>() { @Override public Set get() { return Sets.newHashSet(); } }); - List tokens = new ArrayList(); - Declaration lastDeclaration = null; - while (true) { - Token advance = grammarLexer.advance(); - if (advance == null) { - break; - } + List 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 myUsages = usages.get(declaration.getName()); @@ -170,7 +185,22 @@ public class ConfluenceHyperlinksGenerator { } result.append(token); } + return result; + } + private static List tokenize(_GrammarLexer grammarLexer) throws IOException { + List tokens = new ArrayList(); + 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 recordStarts(StringBuilder allRules, String regex) { - Pattern symbolDeclaration = Pattern.compile(regex, Pattern.MULTILINE); - Matcher matcher = symbolDeclaration.matcher(allRules); - Set declarationStarts = new HashSet(); - while (matcher.find()) { - if (matcher.groupCount() > 0) { - declarationStarts.add(matcher.start(1)); - } - else { - declarationStarts.add(matcher.start()); - } - } - return declarationStarts; - } }