-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'SI-PrettyPrinter' into 'dev'
SIUnitPrettyPrinter Closes #4318 See merge request monticore/monticore!1053
- Loading branch information
Showing
2 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
...-grammar/src/main/java/de/monticore/siunit/siunits/_prettyprint/SIUnitsPrettyPrinter.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,110 @@ | ||
package de.monticore.siunit.siunits._prettyprint; | ||
|
||
import de.monticore.prettyprint.CommentPrettyPrinter; | ||
import de.monticore.prettyprint.IndentPrinter; | ||
import de.monticore.siunit.siunits._ast.ASTSIUnitKindGroupWithExponent; | ||
import de.monticore.siunit.siunits._ast.ASTSIUnitWithPrefix; | ||
|
||
public class SIUnitsPrettyPrinter extends SIUnitsPrettyPrinterTOP { | ||
|
||
public SIUnitsPrettyPrinter(IndentPrinter printer, boolean printComments) { | ||
super(printer, printComments); | ||
} | ||
|
||
//mostly removed trailing whitespaces from default pretty printer | ||
|
||
@Override | ||
public void handle(ASTSIUnitWithPrefix node) { | ||
if (this.isPrintComments()) { | ||
CommentPrettyPrinter.printPreComments(node, getPrinter()); | ||
} | ||
|
||
if (node.isPresentName()) { | ||
getPrinter().print(node.getName()); | ||
} | ||
else if (node.isPresentNonNameUnit()) { | ||
getPrinter().print(node.getNonNameUnit()); | ||
} | ||
|
||
if (this.isPrintComments()) { | ||
CommentPrettyPrinter.printPostComments(node, getPrinter()); | ||
} | ||
} | ||
|
||
@Override | ||
public void handle(de.monticore.siunit.siunits._ast.ASTSIUnitWithoutPrefix node) { | ||
if (this.isPrintComments()) { | ||
CommentPrettyPrinter.printPreComments(node, getPrinter()); | ||
} | ||
|
||
if (node.isPresentName()) { | ||
getPrinter().print(node.getName()); | ||
} | ||
else if (node.isPresentNonNameUnit()) { | ||
getPrinter().print(node.getNonNameUnit()); | ||
} | ||
|
||
if (this.isPrintComments()) { | ||
CommentPrettyPrinter.printPostComments(node, getPrinter()); | ||
} | ||
} | ||
|
||
@Override | ||
public void handle(de.monticore.siunit.siunits._ast.ASTCelsiusFahrenheit node) { | ||
if (this.isPrintComments()) { | ||
CommentPrettyPrinter.printPreComments(node, getPrinter()); | ||
} | ||
|
||
getPrinter().stripTrailing(); | ||
getPrinter().print("°"); | ||
getPrinter().print(node.getUnit()); | ||
|
||
if (this.isPrintComments()) { | ||
CommentPrettyPrinter.printPostComments(node, getPrinter()); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void handle(de.monticore.siunit.siunits._ast.ASTSIUnitDimensionless node) { | ||
if (this.isPrintComments()) { | ||
CommentPrettyPrinter.printPreComments(node, getPrinter()); | ||
} | ||
|
||
if (node.isPresentUnit()) { | ||
getPrinter().print(node.getUnit()); | ||
} | ||
else { | ||
getPrinter().stripTrailing(); | ||
getPrinter().print("°"); | ||
} | ||
|
||
if (this.isPrintComments()) { | ||
CommentPrettyPrinter.printPostComments(node, getPrinter()); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void handle(ASTSIUnitKindGroupWithExponent node) { | ||
if (this.isPrintComments()) { | ||
CommentPrettyPrinter.printPreComments(node, getPrinter()); | ||
} | ||
|
||
for (int i = 0; i < node.sizeExponent(); i++) { | ||
node.getSIUnitGroupPrimitive(i).accept(getTraverser()); | ||
getPrinter().print("^"); | ||
node.getExponent(i).accept(getTraverser()); | ||
} | ||
// optional end without exponent | ||
if (node.sizeExponent() != node.sizeSIUnitGroupPrimitives()) { | ||
node.getSIUnitGroupPrimitive(node.sizeSIUnitGroupPrimitives() - 1) | ||
.accept(getTraverser()); | ||
} | ||
|
||
if (this.isPrintComments()) { | ||
CommentPrettyPrinter.printPostComments(node, getPrinter()); | ||
} | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
monticore-grammar/src/test/java/de/monticore/prettyprint/SIUnitPrettyPrinterTest.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,73 @@ | ||
/* (c) https://github.com/MontiCore/monticore */ | ||
|
||
package de.monticore.prettyprint; | ||
|
||
import de.monticore.siunit.siunits.SIUnitsMill; | ||
import de.monticore.siunit.siunits._ast.ASTSIUnit; | ||
import de.monticore.siunit.siunits._parser.SIUnitsParser; | ||
import de.monticore.siunit.siunits._prettyprint.SIUnitsFullPrettyPrinter; | ||
import de.se_rwth.commons.logging.Log; | ||
import de.se_rwth.commons.logging.LogStub; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class SIUnitPrettyPrinterTest { | ||
|
||
@BeforeEach | ||
public void init() { | ||
LogStub.init(); | ||
Log.enableFailQuick(false); | ||
SIUnitsMill.reset(); | ||
SIUnitsMill.init(); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
public void testSIUnitPrettyPrinting(String siunitStr) throws IOException { | ||
SIUnitsParser parser = SIUnitsMill.parser(); | ||
Optional<ASTSIUnit> parsedOpt = | ||
parser.parseSIUnit(new StringReader(siunitStr)); | ||
assertFalse(parser.hasErrors()); | ||
assertTrue(parsedOpt.isPresent()); | ||
ASTSIUnit parsed = parsedOpt.get(); | ||
|
||
SIUnitsFullPrettyPrinter prettyPrinter = | ||
new SIUnitsFullPrettyPrinter(new IndentPrinter()); | ||
String prettyPrinted = prettyPrinter.prettyprint(parsed); | ||
|
||
Optional<ASTSIUnit> parsedPrinted = parser.parse_String(prettyPrinted); | ||
assertFalse(parser.hasErrors()); | ||
assertTrue(parsedOpt.isPresent()); | ||
assertTrue(parsed.deepEquals(parsedPrinted.get())); | ||
|
||
assertTrue(Log.getFindings().isEmpty()); | ||
} | ||
|
||
public static Stream<Arguments> testSIUnitPrettyPrinting() { | ||
return Stream.of( | ||
Arguments.of("m"), | ||
Arguments.of("m^2"), | ||
Arguments.of("m^1"), | ||
Arguments.of("m^0"), | ||
Arguments.of("dm"), | ||
Arguments.of("dm^2"), | ||
Arguments.of("m^2s"), | ||
Arguments.of("m^2s^2g"), | ||
Arguments.of("m^2ds^7g^4"), | ||
Arguments.of("°"), | ||
Arguments.of("°C"), | ||
Arguments.of("µm"), | ||
Arguments.of("Ω") | ||
); | ||
} | ||
} |