Skip to content

Commit

Permalink
Merge branch 'SI-PrettyPrinter' into 'dev'
Browse files Browse the repository at this point in the history
SIUnitPrettyPrinter

Closes #4318

See merge request monticore/monticore!1053
  • Loading branch information
SE-FDr committed Oct 30, 2024
2 parents 9c9f3c2 + 66d517a commit d8e74c2
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 0 deletions.
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());
}
}

}
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("Ω")
);
}
}

0 comments on commit d8e74c2

Please sign in to comment.