From 797ff830415722a79590f71c1472856f4aa8957f Mon Sep 17 00:00:00 2001 From: Tom Everett Date: Thu, 27 Dec 2018 15:33:55 -0700 Subject: [PATCH 1/9] changes to readme --- README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4247b15..5b8ccba 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ # Antlr4Formatter -Building -==== +## Building + The project uses [maven wrapper](https://github.com/takari/maven-wrapper), so it's easy to build the project without worring about having the right Maven version installed locally. To build `antlr4-formatter` and the standalone utility `antlr4-formatter-standalone` run: @@ -13,13 +13,11 @@ To build `antlr4-formatter` and the standalone utility `antlr4-formatter-standal ./mvnw clean package ``` -Using the standalone formatter -==== +## Using the standalone formatter To format a grammar use the script `formatFile.sh` passing the grammar file as an argument to the script. -Maven Coordinates -==== +## Maven Coordinates ``` com.khubla.antlr4formatter From 34f0020e685a7abfb3a4a503f933e04203c98423 Mon Sep 17 00:00:00 2001 From: Tom Everett Date: Thu, 27 Dec 2018 15:38:49 -0700 Subject: [PATCH 2/9] codacy fixes --- .../antlr4formatter/Antlr4Formatter.java | 204 +++---- .../Antlr4ParseTreeListenerImpl.java | 564 +++++++++--------- 2 files changed, 386 insertions(+), 382 deletions(-) diff --git a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4Formatter.java b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4Formatter.java index feec3f6..d8d0381 100644 --- a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4Formatter.java +++ b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4Formatter.java @@ -17,7 +17,17 @@ */ package com.khubla.antlr4formatter; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.StringWriter; +import java.io.Writer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardCopyOption; @@ -39,104 +49,96 @@ * @author Tom Everett */ public class Antlr4Formatter { - - private Antlr4Formatter() {} - - private static final Logger LOG = LoggerFactory.getLogger(Antlr4Formatter.class); - - public static String format(String string) { - try { - if (null != string) { - StringWriter writer = new StringWriter(); - CodePointCharStream input = CharStreams.fromString(string); - formatGrammar(input, writer); - return writer.toString(); - } else { - return ""; - } - } catch (final Exception e) { - throw new RuntimeException("Exception reading and parsing file", e); - } - } - - private static void formatGrammar(CharStream input, Writer output) { - final ANTLRv4Lexer lexer = new ANTLRv4Lexer(input); - final CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); - final ANTLRv4Parser parser = new ANTLRv4Parser(commonTokenStream); - final GrammarSpecContext grammarSpecContext = parser.grammarSpec(); - ParseTreeWalker.DEFAULT.walk(new Antlr4ParseTreeListenerImpl(output, commonTokenStream), grammarSpecContext); - } - - public static void format(InputStream inputStream, OutputStream outputStream) { - - if (null == inputStream) { - throw new IllegalArgumentException("Input stream must not be null!"); - } - final Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); - try (OutputStreamWriter writer = new OutputStreamWriter(outputStream)) { - formatGrammar(CharStreams.fromReader(reader), writer); - } catch (IOException e) { - LOG.error("Could not format file", e); - } - } - - public static void formatDirectory(String inputDirOption) throws Exception { - List files = new ArrayList<>(); - files = listFilesFromDirectory(inputDirOption, files, ".g4"); - for (final String filename : files) { - final File file = new File(filename); - formatSingleFile(file, file); - } - } - - public static void formatSingleFile(File inputFile, File outputFile) throws Exception { - - if (inputFile.exists()) { - - final File tempFile = File.createTempFile(inputFile.getName(), ".g4"); - - LOG.info("Formatting: {}", inputFile.getName()); - - format(new FileInputStream(inputFile), new FileOutputStream(tempFile)); - - Files.copy(tempFile.toPath(), outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - Files.delete(tempFile.toPath()); - } else { - throw new Exception("Unable to find: '" + inputFile + "'"); - } - } - - public static void formatSingleFile(String inputFilename, String outputFilename) throws Exception { - final File inputFile = new File(inputFilename); - File outputFile; - - if (null == outputFilename) { - outputFile = inputFile; - } else { - outputFile = new File(outputFilename); - } - - formatSingleFile(inputFile, outputFile); - } - - private static List listFilesFromDirectory(String dir, List files, String filter) { - final File file = new File(dir); - final String[] list = file.list(); - if (null != list) { - for (String s : list) { - final String fileName = dir + (dir.endsWith("/") ? "" : "/") + s; - final File f2 = new File(fileName); - if (!f2.isHidden()) { - if (f2.isDirectory()) { - listFilesFromDirectory(fileName + "/", files, filter); - } else { - if (fileName.endsWith(filter)) { - files.add(fileName); - } - } - } - } - } - return files; - } + private static final Logger LOG = LoggerFactory.getLogger(Antlr4Formatter.class); + + public static void format(InputStream inputStream, OutputStream outputStream) { + if (null == inputStream) { + throw new IllegalArgumentException("Input stream must not be null!"); + } + final Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); + try (OutputStreamWriter writer = new OutputStreamWriter(outputStream)) { + formatGrammar(CharStreams.fromReader(reader), writer); + } catch (final IOException e) { + LOG.error("Could not format file", e); + } + } + + public static String format(String string) { + try { + if (null != string) { + final StringWriter writer = new StringWriter(); + final CodePointCharStream input = CharStreams.fromString(string); + formatGrammar(input, writer); + return writer.toString(); + } else { + return ""; + } + } catch (final Exception e) { + throw new RuntimeException("Exception reading and parsing file", e); + } + } + + public static void formatDirectory(String inputDirOption) throws Exception { + List files = new ArrayList<>(); + files = listFilesFromDirectory(inputDirOption, files, ".g4"); + for (final String filename : files) { + final File file = new File(filename); + formatSingleFile(file, file); + } + } + + private static void formatGrammar(CharStream input, Writer output) { + final ANTLRv4Lexer lexer = new ANTLRv4Lexer(input); + final CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); + final ANTLRv4Parser parser = new ANTLRv4Parser(commonTokenStream); + final GrammarSpecContext grammarSpecContext = parser.grammarSpec(); + ParseTreeWalker.DEFAULT.walk(new Antlr4ParseTreeListenerImpl(output, commonTokenStream), grammarSpecContext); + } + + public static void formatSingleFile(File inputFile, File outputFile) throws Exception { + if (inputFile.exists()) { + final File tempFile = File.createTempFile(inputFile.getName(), ".g4"); + LOG.info("Formatting: {}", inputFile.getName()); + format(new FileInputStream(inputFile), new FileOutputStream(tempFile)); + Files.copy(tempFile.toPath(), outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + Files.delete(tempFile.toPath()); + } else { + throw new Exception("Unable to find: '" + inputFile + "'"); + } + } + + public static void formatSingleFile(String inputFilename, String outputFilename) throws Exception { + final File inputFile = new File(inputFilename); + File outputFile; + if (null == outputFilename) { + outputFile = inputFile; + } else { + outputFile = new File(outputFilename); + } + formatSingleFile(inputFile, outputFile); + } + + private static List listFilesFromDirectory(String dir, List files, String filter) { + final File file = new File(dir); + final String[] list = file.list(); + if (null != list) { + for (final String s : list) { + final String fileName = dir + (dir.endsWith("/") ? "" : "/") + s; + final File f2 = new File(fileName); + if (!f2.isHidden()) { + if (f2.isDirectory()) { + listFilesFromDirectory(fileName + "/", files, filter); + } else { + if (fileName.endsWith(filter)) { + files.add(fileName); + } + } + } + } + } + return files; + } + + private Antlr4Formatter() { + } } diff --git a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java index cb529d0..e115119 100644 --- a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java +++ b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java @@ -37,295 +37,297 @@ import org.slf4j.LoggerFactory; public class Antlr4ParseTreeListenerImpl implements ParseTreeListener { + /** + * + */ + private static final Logger LOG = LoggerFactory.getLogger(Antlr4ParseTreeListenerImpl.class); + /** + * non space tokens + */ + private static final Set noSpacingBeforeTokens = new HashSet<>(Arrays.asList(new String[] { "?", "*", ";", ")" })); + private static final Set noSpacingAfterTokens = new HashSet<>(Arrays.asList(new String[] { "(" })); + /** + * indent + */ + private int indent = 0; + /** + * current context + */ + private ParserRuleContext ctx; + /** + * newline + */ + boolean newline = true; + /** + * PrintStream + */ + private final Writer writer; + /** + * token stream + */ + private final CommonTokenStream commonTokenStream; + /** + * hidden token marker + */ + private int hiddenTokenPos = -1; + /** + * parenth count + */ + private int parenthcount = 0; + /** + * previous token + */ + private String previousToken = ""; - /** - * - */ - private static final Logger LOG = LoggerFactory.getLogger(Antlr4ParseTreeListenerImpl.class); - /** - * non space tokens - */ - private static final Set noSpacingBeforeTokens = new HashSet<>(Arrays.asList(new String[]{"?", "*", ";", ")"})); - private static final Set noSpacingAfterTokens = new HashSet<>(Arrays.asList(new String[]{"("})); - /** - * indent - */ - private int indent = 0; - /** - * current context - */ - private ParserRuleContext ctx; - /** - * newline - */ - boolean newline = true; - /** - * PrintStream - */ - private final Writer writer; - /** - * token stream - */ - private final CommonTokenStream commonTokenStream; - /** - * hidden token marker - */ - private int hiddenTokenPos = -1; - /** - * parenth count - */ - private int parenthcount = 0; - /** - * previous token - */ - private String previousToken = ""; + /** + * ctor + */ + public Antlr4ParseTreeListenerImpl(Writer writer, CommonTokenStream commonTokenStream) { + this.writer = writer; + this.commonTokenStream = commonTokenStream; + } - /** - * ctor - */ - public Antlr4ParseTreeListenerImpl(Writer writer, CommonTokenStream commonTokenStream) { - this.writer = writer; - this.commonTokenStream = commonTokenStream; - } + /** + * build indent + */ + private String buildIndent(int indent) { + final StringBuilder sb = new StringBuilder(); + for (int i = 0; i < (indent * 3); i++) { + sb.append(" "); + } + return sb.toString(); + } - /** - * build indent - */ - String buildIndent(int indent) { - final StringBuilder sb = new StringBuilder(); - for (int i = 0; i < (indent * 3); i++) { - sb.append(" "); - } - return sb.toString(); - } + /** + * write a CR + */ + private void CR() { + writeSimple("\n"); + writeSimple(buildIndent(indent)); + newline = true; + } - /** - * write a CR - */ - void CR() { - writeSimple("\n"); - writeSimple(buildIndent(indent)); - newline = true; - } + @Override + public void enterEveryRule(ParserRuleContext ctx) { + /* + * check for hidden tokens + */ + final int tokPos = ctx.getStart().getTokenIndex(); + if (tokPos > hiddenTokenPos) { + hiddenTokenPos = tokPos; + final List refChannel = commonTokenStream.getHiddenTokensToLeft(tokPos, ANTLRv4Lexer.OFF_CHANNEL); + if ((null != refChannel) && (refChannel.size() > 0)) { + for (final Token token : refChannel) { + /* + * print comments + */ + final String str = token.getText().trim(); + if (str.length() > 0) { + if (str.trim().startsWith("//") || str.trim().startsWith("/*")) { + if ((false == (ctx instanceof ANTLRv4Parser.GrammarSpecContext))) { + CR(); + } + write(token.getText()); + if ((true == ((ctx instanceof ANTLRv4Parser.GrammarSpecContext))) || (ctx instanceof ANTLRv4Parser.GrammarTypeContext)) { + CR(); + } + } + } + } + } + } + if (ctx instanceof ANTLRv4Parser.RuleSpecContext) { + this.ctx = ctx; + CR(); + } else if (ctx instanceof ANTLRv4Parser.GrammarSpecContext) { + this.ctx = ctx; + CR(); + } else if (ctx instanceof ANTLRv4Parser.OptionsSpecContext) { + this.ctx = ctx; + CR(); + } else if (ctx instanceof ANTLRv4Parser.TokensSpecContext) { + this.ctx = ctx; + CR(); + } else if (ctx instanceof ANTLRv4Parser.ChannelsSpecContext) { + this.ctx = ctx; + CR(); + } else if (ctx instanceof ANTLRv4Parser.ModeSpecContext) { + this.ctx = ctx; + CR(); + } else if (ctx instanceof ANTLRv4Parser.LexerRuleSpecContext) { + this.ctx = ctx; + CR(); + } else if (ctx instanceof ANTLRv4Parser.LabeledAltContext) { + this.ctx = ctx; + } else if (ctx instanceof ANTLRv4Parser.ActionBlockContext) { + this.ctx = ctx; + } + } - @Override - public void enterEveryRule(ParserRuleContext ctx) { - /* - * check for hidden tokens - */ - final int tokPos = ctx.getStart().getTokenIndex(); - if (tokPos > hiddenTokenPos) { - hiddenTokenPos = tokPos; - final List refChannel = commonTokenStream.getHiddenTokensToLeft(tokPos, ANTLRv4Lexer.OFF_CHANNEL); - if ((null != refChannel) && (refChannel.size() > 0)) { - for (final Token token : refChannel) { - /* - * print comments - */ - final String str = token.getText().trim(); - if (str.length() > 0) { - if (str.trim().startsWith("//") || str.trim().startsWith("/*")) { - if ((false == (ctx instanceof ANTLRv4Parser.GrammarSpecContext))) { - CR(); - } - write(token.getText()); - if ((true == ((ctx instanceof ANTLRv4Parser.GrammarSpecContext))) || (ctx instanceof ANTLRv4Parser.GrammarTypeContext)) { - CR(); - } - } - } - } - } - } - if (ctx instanceof ANTLRv4Parser.RuleSpecContext) { - this.ctx = ctx; - CR(); - } else if (ctx instanceof ANTLRv4Parser.GrammarSpecContext) { - this.ctx = ctx; - CR(); - } else if (ctx instanceof ANTLRv4Parser.OptionsSpecContext) { - this.ctx = ctx; - CR(); - } else if (ctx instanceof ANTLRv4Parser.TokensSpecContext) { - this.ctx = ctx; - CR(); - } else if (ctx instanceof ANTLRv4Parser.ChannelsSpecContext) { - this.ctx = ctx; - CR(); - } else if (ctx instanceof ANTLRv4Parser.ModeSpecContext) { - this.ctx = ctx; - CR(); - } else if (ctx instanceof ANTLRv4Parser.LexerRuleSpecContext) { - this.ctx = ctx; - CR(); - } else if (ctx instanceof ANTLRv4Parser.LabeledAltContext) { - this.ctx = ctx; - } else if (ctx instanceof ANTLRv4Parser.ActionBlockContext) { - this.ctx = ctx; - } - } + @Override + public void exitEveryRule(ParserRuleContext ctx) { + // do nothing + } - @Override - public void exitEveryRule(ParserRuleContext ctx) {} + @Override + public void visitErrorNode(ErrorNode node) { + // do nothing + } - @Override - public void visitErrorNode(ErrorNode node) {} + @Override + public void visitTerminal(TerminalNode node) { + /* + * log the rule + */ + LOG.debug(ctx.getClass().getSimpleName() + " : " + node.getText()); + /* + * options indenting + */ + if ((ctx instanceof ANTLRv4Parser.OptionsSpecContext) || (ctx instanceof ANTLRv4Parser.ModeSpecContext) || (ctx instanceof ANTLRv4Parser.TokensSpecContext) + || (ctx instanceof ANTLRv4Parser.ChannelsSpecContext)) { + if (node.getSymbol().getType() == ANTLRv4Lexer.LBRACE) { + indent++; + CR(); + write(node); + } else if (node.getSymbol().getType() == ANTLRv4Lexer.RBRACE) { + write(node); + indent--; + CR(); + } else { + write(node); + } + } else if (ctx instanceof ANTLRv4Parser.LabeledAltContext) { + if (node.getSymbol().getType() == ANTLRv4Lexer.LPAREN) { + parenthcount++; + } else if (node.getSymbol().getType() == ANTLRv4Lexer.RPAREN) { + parenthcount--; + } + if (node.getSymbol().getType() == ANTLRv4Lexer.SEMI) { + CR(); + write(node); + indent--; + CR(); + } else if (node.getSymbol().getType() == ANTLRv4Lexer.OR) { + if (parenthcount == 0) { + CR(); + } + write(node); + } else { + if (node.getSymbol().getType() != Recognizer.EOF) { + write(node); + } + } + } + /* + * rule indenting + */ + else if ((ctx instanceof ANTLRv4Parser.RuleSpecContext) || (ctx instanceof ANTLRv4Parser.LexerRuleSpecContext)) { + parenthcount = 0; + if (node.getSymbol().getType() == ANTLRv4Lexer.COLON) { + indent++; + CR(); + write(node); + } else if (node.getSymbol().getType() == ANTLRv4Lexer.SEMI) { + CR(); + write(node); + indent--; + CR(); + } else if (node.getSymbol().getType() == ANTLRv4Lexer.DOC_COMMENT) { + write(node); + CR(); + } else { + if (node.getSymbol().getType() != Recognizer.EOF) { + write(node); + } + } + } + /* + * grammar spec + */ + else if (ctx instanceof ANTLRv4Parser.GrammarSpecContext) { + if (node.getSymbol().getType() == ANTLRv4Lexer.SEMI) { + write(node); + CR(); + } else if (node.getSymbol().getType() == ANTLRv4Lexer.DOC_COMMENT) { + write(node); + CR(); + } else { + write(node); + } + } + /* + * grammar spec + */ + else if (ctx instanceof ANTLRv4Parser.ActionBlockContext) { + if (node.getSymbol().getType() == ANTLRv4Lexer.SEMI) { + write(node); + CR(); + } else if (node.getSymbol().getType() == ANTLRv4Lexer.DOC_COMMENT) { + write(node); + CR(); + } else if (node.getSymbol().getType() == ANTLRv4Lexer.AT) { + write(node); + CR(); + } else { + write(node); + } + } + /* + * grammar spec + */ + else { + write(node); + } + } - @Override - public void visitTerminal(TerminalNode node) { - /* - * log the rule - */ - LOG.debug(ctx.getClass().getSimpleName() + " : " + node.getText()); - /* - * options indenting - */ - if ((ctx instanceof ANTLRv4Parser.OptionsSpecContext) || (ctx instanceof ANTLRv4Parser.ModeSpecContext) || (ctx instanceof ANTLRv4Parser.TokensSpecContext) - || (ctx instanceof ANTLRv4Parser.ChannelsSpecContext)) { - if (node.getSymbol().getType() == ANTLRv4Lexer.LBRACE) { - indent++; - CR(); - write(node); - } else if (node.getSymbol().getType() == ANTLRv4Lexer.RBRACE) { - write(node); - indent--; - CR(); - } else { - write(node); - } - } else if (ctx instanceof ANTLRv4Parser.LabeledAltContext) { - if (node.getSymbol().getType() == ANTLRv4Lexer.LPAREN) { - parenthcount++; - } else if (node.getSymbol().getType() == ANTLRv4Lexer.RPAREN) { - parenthcount--; - } - if (node.getSymbol().getType() == ANTLRv4Lexer.SEMI) { - CR(); - write(node); - indent--; - CR(); - } else if (node.getSymbol().getType() == ANTLRv4Lexer.OR) { - if (parenthcount == 0) { - CR(); - } - write(node); - } else { - if (node.getSymbol().getType() != Recognizer.EOF) { - write(node); - } - } - } - /* - * rule indenting - */ - else if ((ctx instanceof ANTLRv4Parser.RuleSpecContext) || (ctx instanceof ANTLRv4Parser.LexerRuleSpecContext)) { - parenthcount = 0; - if (node.getSymbol().getType() == ANTLRv4Lexer.COLON) { - indent++; - CR(); - write(node); - } else if (node.getSymbol().getType() == ANTLRv4Lexer.SEMI) { - CR(); - write(node); - indent--; - CR(); - } else if (node.getSymbol().getType() == ANTLRv4Lexer.DOC_COMMENT) { - write(node); - CR(); - } else { - if (node.getSymbol().getType() != Recognizer.EOF) { - write(node); - } - } - } - /* - * grammar spec - */ - else if (ctx instanceof ANTLRv4Parser.GrammarSpecContext) { - if (node.getSymbol().getType() == ANTLRv4Lexer.SEMI) { - write(node); - CR(); - } else if (node.getSymbol().getType() == ANTLRv4Lexer.DOC_COMMENT) { - write(node); - CR(); - } else { - write(node); - } - } - /* - * grammar spec - */ - else if (ctx instanceof ANTLRv4Parser.ActionBlockContext) { - if (node.getSymbol().getType() == ANTLRv4Lexer.SEMI) { - write(node); - CR(); - } else if (node.getSymbol().getType() == ANTLRv4Lexer.DOC_COMMENT) { - write(node); - CR(); - } else if (node.getSymbol().getType() == ANTLRv4Lexer.AT) { - write(node); - CR(); - } else { - write(node); - } - } - /* - * grammar spec - */ - else { - write(node); - } - } + /** + * write to the output stream + */ + void write(String str) { + /* + * print token + */ + writeSimple(str); + if (true == newline) { + newline = false; + } + /* + * save the previous + */ + previousToken = str; + } - private void writeSimple(String string) { - try { - writer.write(string); - } catch (IOException e) { - throw new RuntimeException("Could not write to writer", e); - } - } + /** + * write to the output stream + */ + private void write(TerminalNode node) { + LOG.debug("Writing: '" + node.getText() + "'"); + /* + * space before the output + */ + if (false == (ctx instanceof ANTLRv4Parser.ActionBlockContext)) { + if ((false == newline) && (false == noSpacingAfterTokens.contains(previousToken)) && (false == noSpacingBeforeTokens.contains(node.getText()))) { + writeSimple(" "); + } + } + /* + * print token + */ + writeSimple(node.getText()); + if (true == newline) { + newline = false; + } + /* + * save the previous + */ + previousToken = node.getText(); + } - /** - * write to the output stream - */ - void write(String str) { - /* - * print token - */ - writeSimple(str); - if (true == newline) { - newline = false; - } - /* - * save the previous - */ - previousToken = str; - - } - - /** - * write to the output stream - */ - void write(TerminalNode node) { - LOG.debug("Writing: '" + node.getText() + "'"); - /* - * space before the output - */ - if (false == (ctx instanceof ANTLRv4Parser.ActionBlockContext)) { - if ((false == newline) && (false == noSpacingAfterTokens.contains(previousToken)) && (false == noSpacingBeforeTokens.contains(node.getText()))) { - writeSimple(" "); - } - } - /* - * print token - */ - writeSimple(node.getText()); - if (true == newline) { - newline = false; - } - /* - * save the previous - */ - previousToken = node.getText(); - } + private void writeSimple(String string) { + try { + writer.write(string); + } catch (final IOException e) { + throw new RuntimeException("Could not write to writer", e); + } + } } From c11c60800d7ea2dba3579ec4437da57d5a50311d Mon Sep 17 00:00:00 2001 From: Tom Everett Date: Thu, 27 Dec 2018 15:39:26 -0700 Subject: [PATCH 3/9] codacity fix --- .../com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java index e115119..c55392e 100644 --- a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java +++ b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java @@ -283,7 +283,7 @@ else if (ctx instanceof ANTLRv4Parser.ActionBlockContext) { /** * write to the output stream */ - void write(String str) { + private void write(String str) { /* * print token */ From 7752c36e0e68ad935f7d5b2a2875528d8dbf735b Mon Sep 17 00:00:00 2001 From: Tom Everett Date: Thu, 27 Dec 2018 15:41:58 -0700 Subject: [PATCH 4/9] codacity fixes --- README.md | 2 -- .../com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 5b8ccba..40a8916 100644 --- a/README.md +++ b/README.md @@ -25,5 +25,3 @@ To format a grammar use the script `formatFile.sh` passing the grammar file as a 1.1.0 jar ``` - - diff --git a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java index c55392e..5356328 100644 --- a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java +++ b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java @@ -57,7 +57,7 @@ public class Antlr4ParseTreeListenerImpl implements ParseTreeListener { /** * newline */ - boolean newline = true; + private boolean newline = true; /** * PrintStream */ From b26a493b4222b075045894fd83ba9ab49f353c8c Mon Sep 17 00:00:00 2001 From: Tom Everett Date: Thu, 27 Dec 2018 15:44:38 -0700 Subject: [PATCH 5/9] codacity fixes --- .../Antlr4ParseTreeListenerImpl.java | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java index 5356328..10e6b97 100644 --- a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java +++ b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java @@ -101,7 +101,7 @@ private String buildIndent(int indent) { /** * write a CR */ - private void CR() { + private void writeCR() { writeSimple("\n"); writeSimple(buildIndent(indent)); newline = true; @@ -125,11 +125,11 @@ public void enterEveryRule(ParserRuleContext ctx) { if (str.length() > 0) { if (str.trim().startsWith("//") || str.trim().startsWith("/*")) { if ((false == (ctx instanceof ANTLRv4Parser.GrammarSpecContext))) { - CR(); + writeCR(); } write(token.getText()); if ((true == ((ctx instanceof ANTLRv4Parser.GrammarSpecContext))) || (ctx instanceof ANTLRv4Parser.GrammarTypeContext)) { - CR(); + writeCR(); } } } @@ -138,25 +138,25 @@ public void enterEveryRule(ParserRuleContext ctx) { } if (ctx instanceof ANTLRv4Parser.RuleSpecContext) { this.ctx = ctx; - CR(); + writeCR(); } else if (ctx instanceof ANTLRv4Parser.GrammarSpecContext) { this.ctx = ctx; - CR(); + writeCR(); } else if (ctx instanceof ANTLRv4Parser.OptionsSpecContext) { this.ctx = ctx; - CR(); + writeCR(); } else if (ctx instanceof ANTLRv4Parser.TokensSpecContext) { this.ctx = ctx; - CR(); + writeCR(); } else if (ctx instanceof ANTLRv4Parser.ChannelsSpecContext) { this.ctx = ctx; - CR(); + writeCR(); } else if (ctx instanceof ANTLRv4Parser.ModeSpecContext) { this.ctx = ctx; - CR(); + writeCR(); } else if (ctx instanceof ANTLRv4Parser.LexerRuleSpecContext) { this.ctx = ctx; - CR(); + writeCR(); } else if (ctx instanceof ANTLRv4Parser.LabeledAltContext) { this.ctx = ctx; } else if (ctx instanceof ANTLRv4Parser.ActionBlockContext) { @@ -187,12 +187,12 @@ public void visitTerminal(TerminalNode node) { || (ctx instanceof ANTLRv4Parser.ChannelsSpecContext)) { if (node.getSymbol().getType() == ANTLRv4Lexer.LBRACE) { indent++; - CR(); + writeCR(); write(node); } else if (node.getSymbol().getType() == ANTLRv4Lexer.RBRACE) { write(node); indent--; - CR(); + writeCR(); } else { write(node); } @@ -203,13 +203,13 @@ public void visitTerminal(TerminalNode node) { parenthcount--; } if (node.getSymbol().getType() == ANTLRv4Lexer.SEMI) { - CR(); + writeCR(); write(node); indent--; - CR(); + writeCR(); } else if (node.getSymbol().getType() == ANTLRv4Lexer.OR) { if (parenthcount == 0) { - CR(); + writeCR(); } write(node); } else { @@ -225,16 +225,16 @@ else if ((ctx instanceof ANTLRv4Parser.RuleSpecContext) || (ctx instanceof ANTLR parenthcount = 0; if (node.getSymbol().getType() == ANTLRv4Lexer.COLON) { indent++; - CR(); + writeCR(); write(node); } else if (node.getSymbol().getType() == ANTLRv4Lexer.SEMI) { - CR(); + writeCR(); write(node); indent--; - CR(); + writeCR(); } else if (node.getSymbol().getType() == ANTLRv4Lexer.DOC_COMMENT) { write(node); - CR(); + writeCR(); } else { if (node.getSymbol().getType() != Recognizer.EOF) { write(node); @@ -247,10 +247,10 @@ else if ((ctx instanceof ANTLRv4Parser.RuleSpecContext) || (ctx instanceof ANTLR else if (ctx instanceof ANTLRv4Parser.GrammarSpecContext) { if (node.getSymbol().getType() == ANTLRv4Lexer.SEMI) { write(node); - CR(); + writeCR(); } else if (node.getSymbol().getType() == ANTLRv4Lexer.DOC_COMMENT) { write(node); - CR(); + writeCR(); } else { write(node); } @@ -261,13 +261,13 @@ else if (ctx instanceof ANTLRv4Parser.GrammarSpecContext) { else if (ctx instanceof ANTLRv4Parser.ActionBlockContext) { if (node.getSymbol().getType() == ANTLRv4Lexer.SEMI) { write(node); - CR(); + writeCR(); } else if (node.getSymbol().getType() == ANTLRv4Lexer.DOC_COMMENT) { write(node); - CR(); + writeCR(); } else if (node.getSymbol().getType() == ANTLRv4Lexer.AT) { write(node); - CR(); + writeCR(); } else { write(node); } From e4ad5c14e4da680f0891c3edbd3ed06e3326ec48 Mon Sep 17 00:00:00 2001 From: Tom Everett Date: Thu, 27 Dec 2018 15:50:53 -0700 Subject: [PATCH 6/9] Added custom exception type --- .../antlr4formatter/Antlr4Formatter.java | 30 +++++++------ .../Antlr4FormatterException.java | 30 +++++++++++++ .../Antlr4ParseTreeListenerImpl.java | 18 ++++---- .../antlr4formatter/Antlr4FormatterTest.java | 44 +++++++++---------- 4 files changed, 76 insertions(+), 46 deletions(-) create mode 100644 antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4FormatterException.java diff --git a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4Formatter.java b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4Formatter.java index d8d0381..4f79b0b 100644 --- a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4Formatter.java +++ b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4Formatter.java @@ -63,7 +63,7 @@ public static void format(InputStream inputStream, OutputStream outputStream) { } } - public static String format(String string) { + public static String format(String string) throws Antlr4FormatterException { try { if (null != string) { final StringWriter writer = new StringWriter(); @@ -74,11 +74,11 @@ public static String format(String string) { return ""; } } catch (final Exception e) { - throw new RuntimeException("Exception reading and parsing file", e); + throw new Antlr4FormatterException("Exception reading and parsing file", e); } } - public static void formatDirectory(String inputDirOption) throws Exception { + public static void formatDirectory(String inputDirOption) throws Antlr4FormatterException { List files = new ArrayList<>(); files = listFilesFromDirectory(inputDirOption, files, ".g4"); for (final String filename : files) { @@ -95,19 +95,23 @@ private static void formatGrammar(CharStream input, Writer output) { ParseTreeWalker.DEFAULT.walk(new Antlr4ParseTreeListenerImpl(output, commonTokenStream), grammarSpecContext); } - public static void formatSingleFile(File inputFile, File outputFile) throws Exception { - if (inputFile.exists()) { - final File tempFile = File.createTempFile(inputFile.getName(), ".g4"); - LOG.info("Formatting: {}", inputFile.getName()); - format(new FileInputStream(inputFile), new FileOutputStream(tempFile)); - Files.copy(tempFile.toPath(), outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - Files.delete(tempFile.toPath()); - } else { - throw new Exception("Unable to find: '" + inputFile + "'"); + public static void formatSingleFile(File inputFile, File outputFile) throws Antlr4FormatterException { + try { + if (inputFile.exists()) { + final File tempFile = File.createTempFile(inputFile.getName(), ".g4"); + LOG.info("Formatting: {}", inputFile.getName()); + format(new FileInputStream(inputFile), new FileOutputStream(tempFile)); + Files.copy(tempFile.toPath(), outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + Files.delete(tempFile.toPath()); + } else { + throw new Exception("Unable to find: '" + inputFile + "'"); + } + } catch (final Exception e) { + throw new Antlr4FormatterException("Exception fromatting file", e); } } - public static void formatSingleFile(String inputFilename, String outputFilename) throws Exception { + public static void formatSingleFile(String inputFilename, String outputFilename) throws Antlr4FormatterException { final File inputFile = new File(inputFilename); File outputFile; if (null == outputFilename) { diff --git a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4FormatterException.java b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4FormatterException.java new file mode 100644 index 0000000..6261372 --- /dev/null +++ b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4FormatterException.java @@ -0,0 +1,30 @@ +/* + * Antlr4Formatter Copyright 2018, khubla.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.khubla.antlr4formatter; + +public class Antlr4FormatterException extends Exception { + private static final long serialVersionUID = 1L; + + public Antlr4FormatterException(String message) { + super(message); + } + + public Antlr4FormatterException(String message, Exception e) { + super(message, e); + } +} \ No newline at end of file diff --git a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java index 10e6b97..17ecb30 100644 --- a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java +++ b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4ParseTreeListenerImpl.java @@ -98,15 +98,6 @@ private String buildIndent(int indent) { return sb.toString(); } - /** - * write a CR - */ - private void writeCR() { - writeSimple("\n"); - writeSimple(buildIndent(indent)); - newline = true; - } - @Override public void enterEveryRule(ParserRuleContext ctx) { /* @@ -323,6 +314,15 @@ private void write(TerminalNode node) { previousToken = node.getText(); } + /** + * write a CR + */ + private void writeCR() { + writeSimple("\n"); + writeSimple(buildIndent(indent)); + newline = true; + } + private void writeSimple(String string) { try { writer.write(string); diff --git a/antlr4-formatter/src/test/java/com/khubla/antlr4formatter/Antlr4FormatterTest.java b/antlr4-formatter/src/test/java/com/khubla/antlr4formatter/Antlr4FormatterTest.java index abf5713..65c7e66 100644 --- a/antlr4-formatter/src/test/java/com/khubla/antlr4formatter/Antlr4FormatterTest.java +++ b/antlr4-formatter/src/test/java/com/khubla/antlr4formatter/Antlr4FormatterTest.java @@ -13,28 +13,24 @@ import org.junit.Test; public class Antlr4FormatterTest { - - @Test - public void formatString() throws IOException { - // given - String unformattedGrammar = readFileAsUtf8ToString("Hello.unformatted.g4"); - String formattedGrammar = readFileAsUtf8ToString("Hello.formatted.g4"); - - // when - String result = Antlr4Formatter.format(unformattedGrammar); - - // then - assertThat(result).isEqualTo(formattedGrammar); - } - - private String readFileAsUtf8ToString(String fileName) throws IOException { - try { - URI uri = Antlr4FormatterTest.class.getClassLoader().getResource(fileName).toURI(); - Path path = Paths.get(uri); - return new String(Files.readAllBytes(path), StandardCharsets.UTF_8); - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } - } - + @Test + public void formatString() throws Antlr4FormatterException, IOException { + // given + String unformattedGrammar = readFileAsUtf8ToString("Hello.unformatted.g4"); + String formattedGrammar = readFileAsUtf8ToString("Hello.formatted.g4"); + // when + String result = Antlr4Formatter.format(unformattedGrammar); + // then + assertThat(result).isEqualTo(formattedGrammar); + } + + private String readFileAsUtf8ToString(String fileName) throws IOException { + try { + URI uri = Antlr4FormatterTest.class.getClassLoader().getResource(fileName).toURI(); + Path path = Paths.get(uri); + return new String(Files.readAllBytes(path), StandardCharsets.UTF_8); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } } From 6fdeb70fec147c1f38b14b9f28b93deb1ca90e1a Mon Sep 17 00:00:00 2001 From: Tom Everett Date: Thu, 27 Dec 2018 15:52:55 -0700 Subject: [PATCH 7/9] minor fixes --- .../Antlr4FormatterException.java | 4 + .../org/antlr/parser/antlr4/LexerAdaptor.java | 112 +++++++++--------- .../antlr4formatter/Antlr4FormatterTest.java | 4 +- 3 files changed, 62 insertions(+), 58 deletions(-) diff --git a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4FormatterException.java b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4FormatterException.java index 6261372..85bbedf 100644 --- a/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4FormatterException.java +++ b/antlr4-formatter/src/main/java/com/khubla/antlr4formatter/Antlr4FormatterException.java @@ -20,6 +20,10 @@ public class Antlr4FormatterException extends Exception { private static final long serialVersionUID = 1L; + public Antlr4FormatterException(Exception e) { + super(e); + } + public Antlr4FormatterException(String message) { super(message); } diff --git a/antlr4-formatter/src/main/java/org/antlr/parser/antlr4/LexerAdaptor.java b/antlr4-formatter/src/main/java/org/antlr/parser/antlr4/LexerAdaptor.java index 0ac2b74..416a6f8 100644 --- a/antlr4-formatter/src/main/java/org/antlr/parser/antlr4/LexerAdaptor.java +++ b/antlr4-formatter/src/main/java/org/antlr/parser/antlr4/LexerAdaptor.java @@ -6,68 +6,68 @@ import org.antlr.v4.runtime.misc.Interval; public abstract class LexerAdaptor extends Lexer { - /** - * Track whether we are inside of a rule and whether it is lexical parser. currentRuleType==Token.INVALID_TYPE means that we are outside of a rule. At the first sign of a rule name reference and - * currentRuleType==invalid, we can assume that we are starting a parser rule. Similarly, seeing a token reference when not already in rule means starting a token rule. The terminating ';' of a - * rule, flips this back to invalid type. This is not perfect logic but works. For example, "grammar T;" means that we start and stop a lexical rule for the "T;". Dangerous but works. The whole - * point of this state information is to distinguish between [..arg actions..] and [charsets]. Char sets can only occur in lexical rules and arg actions cannot occur. - */ - private int currentRuleType = Token.INVALID_TYPE; + /** + * Track whether we are inside of a rule and whether it is lexical parser. currentRuleType==Token.INVALID_TYPE means that we are outside of a rule. At the first sign of a rule name reference and + * currentRuleType==invalid, we can assume that we are starting a parser rule. Similarly, seeing a token reference when not already in rule means starting a token rule. The terminating ';' of a + * rule, flips this back to invalid type. This is not perfect logic but works. For example, "grammar T;" means that we start and stop a lexical rule for the "T;". Dangerous but works. The whole + * point of this state information is to distinguish between [..arg actions..] and [charsets]. Char sets can only occur in lexical rules and arg actions cannot occur. + */ + private int currentRuleType = Token.INVALID_TYPE; - public LexerAdaptor(CharStream input) { - super(input); - } + public LexerAdaptor(CharStream input) { + super(input); + } - @Override - public Token emit() { - if (_type == ANTLRv4Lexer.ID) { - final String firstChar = _input.getText(Interval.of(_tokenStartCharIndex, _tokenStartCharIndex)); - if (Character.isUpperCase(firstChar.charAt(0))) { - _type = ANTLRv4Lexer.TOKEN_REF; - } else { - _type = ANTLRv4Lexer.RULE_REF; - } - if (currentRuleType == Token.INVALID_TYPE) { // if outside of rule def - currentRuleType = _type; // set to inside lexer or parser rule - } - } else if (_type == ANTLRv4Lexer.SEMI) { // exit rule def - currentRuleType = Token.INVALID_TYPE; - } - return super.emit(); - } + @Override + public Token emit() { + if (_type == ANTLRv4Lexer.ID) { + final String firstChar = _input.getText(Interval.of(_tokenStartCharIndex, _tokenStartCharIndex)); + if (Character.isUpperCase(firstChar.charAt(0))) { + _type = ANTLRv4Lexer.TOKEN_REF; + } else { + _type = ANTLRv4Lexer.RULE_REF; + } + if (currentRuleType == Token.INVALID_TYPE) { // if outside of rule def + currentRuleType = _type; // set to inside lexer or parser rule + } + } else if (_type == ANTLRv4Lexer.SEMI) { // exit rule def + currentRuleType = Token.INVALID_TYPE; + } + return super.emit(); + } - public int getCurrentRuleType() { - return currentRuleType; - } + public int getCurrentRuleType() { + return currentRuleType; + } - protected void handleBeginArgument() { - if (inLexerRule()) { - pushMode(ANTLRv4Lexer.LexerCharSet); - more(); - } else { - pushMode(ANTLRv4Lexer.Argument); - } - } + protected void handleBeginArgument() { + if (inLexerRule()) { + pushMode(ANTLRv4Lexer.LexerCharSet); + more(); + } else { + pushMode(ANTLRv4Lexer.Argument); + } + } - protected void handleEndAction() { - popMode(); - if (_modeStack.size() > 0) { - setType(ANTLRv4Lexer.ACTION_CONTENT); - } - } + protected void handleEndAction() { + popMode(); + if (_modeStack.size() > 0) { + setType(ANTLRv4Lexer.ACTION_CONTENT); + } + } - protected void handleEndArgument() { - popMode(); - if (_modeStack.size() > 0) { - setType(ANTLRv4Lexer.ARGUMENT_CONTENT); - } - } + protected void handleEndArgument() { + popMode(); + if (_modeStack.size() > 0) { + setType(ANTLRv4Lexer.ARGUMENT_CONTENT); + } + } - private boolean inLexerRule() { - return currentRuleType == ANTLRv4Lexer.TOKEN_REF; - } + private boolean inLexerRule() { + return currentRuleType == ANTLRv4Lexer.TOKEN_REF; + } - public void setCurrentRuleType(int ruleType) { - currentRuleType = ruleType; - } + public void setCurrentRuleType(int ruleType) { + currentRuleType = ruleType; + } } diff --git a/antlr4-formatter/src/test/java/com/khubla/antlr4formatter/Antlr4FormatterTest.java b/antlr4-formatter/src/test/java/com/khubla/antlr4formatter/Antlr4FormatterTest.java index 65c7e66..2635230 100644 --- a/antlr4-formatter/src/test/java/com/khubla/antlr4formatter/Antlr4FormatterTest.java +++ b/antlr4-formatter/src/test/java/com/khubla/antlr4formatter/Antlr4FormatterTest.java @@ -24,13 +24,13 @@ public void formatString() throws Antlr4FormatterException, IOException { assertThat(result).isEqualTo(formattedGrammar); } - private String readFileAsUtf8ToString(String fileName) throws IOException { + private String readFileAsUtf8ToString(String fileName) throws IOException, Antlr4FormatterException { try { URI uri = Antlr4FormatterTest.class.getClassLoader().getResource(fileName).toURI(); Path path = Paths.get(uri); return new String(Files.readAllBytes(path), StandardCharsets.UTF_8); } catch (URISyntaxException e) { - throw new RuntimeException(e); + throw new Antlr4FormatterException(e); } } } From ad4dbe3751dd1f9a58edfc5731e5e51c0abc7db5 Mon Sep 17 00:00:00 2001 From: Tom Everett Date: Thu, 27 Dec 2018 15:54:08 -0700 Subject: [PATCH 8/9] fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 40a8916..2fd1842 100644 --- a/README.md +++ b/README.md @@ -24,4 +24,4 @@ To format a grammar use the script `formatFile.sh` passing the grammar file as a antlr4-formatter 1.1.0 jar -``` +``` \ No newline at end of file From 3de7bf27a5a29a461f5f7c9fd211b7f2ad708e61 Mon Sep 17 00:00:00 2001 From: Tom Everett Date: Thu, 27 Dec 2018 15:54:57 -0700 Subject: [PATCH 9/9] spelling --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2fd1842..627fa2a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ## Building -The project uses [maven wrapper](https://github.com/takari/maven-wrapper), so it's easy to build the project without worring about having the right Maven version installed locally. +The project uses [maven wrapper](https://github.com/takari/maven-wrapper), so it's easy to build the project without worrying about having the right Maven version installed locally. To build `antlr4-formatter` and the standalone utility `antlr4-formatter-standalone` run: