Skip to content

Commit

Permalink
Merge pull request antlr#17 from teverett/master
Browse files Browse the repository at this point in the history
codacity fixes
  • Loading branch information
teverett authored Dec 27, 2018
2 parents 01a9b94 + 3de7bf2 commit 69e3337
Show file tree
Hide file tree
Showing 6 changed files with 506 additions and 472 deletions.
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@

# Antlr4Formatter

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.
## Building

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:

```bash
./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

```
<groupId>com.khubla.antlr4formatter</groupId>
<artifactId>antlr4-formatter</artifactId>
<version>1.1.0</version>
<packaging>jar</packaging>
```


```
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,104 +49,100 @@
* @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<String> 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<String> listFilesFromDirectory(String dir, List<String> 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) throws Antlr4FormatterException {
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 Antlr4FormatterException("Exception reading and parsing file", e);
}
}

public static void formatDirectory(String inputDirOption) throws Antlr4FormatterException {
List<String> 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 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 Antlr4FormatterException {
final File inputFile = new File(inputFilename);
File outputFile;
if (null == outputFilename) {
outputFile = inputFile;
} else {
outputFile = new File(outputFilename);
}
formatSingleFile(inputFile, outputFile);
}

private static List<String> listFilesFromDirectory(String dir, List<String> 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() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*
*/
package com.khubla.antlr4formatter;

public class Antlr4FormatterException extends Exception {
private static final long serialVersionUID = 1L;

public Antlr4FormatterException(Exception e) {
super(e);
}

public Antlr4FormatterException(String message) {
super(message);
}

public Antlr4FormatterException(String message, Exception e) {
super(message, e);
}
}
Loading

0 comments on commit 69e3337

Please sign in to comment.