Commit c77bf77e authored by Jean-Marc Ronck's avatar Jean-Marc Ronck
Browse files

Merge branch 'bram.kohlen' into 'master'

Merge comments, tex and enhanced printer into master

See merge request !10
parents 34568ded 2e4fc95d
Pipeline #61231 passed with stages
in 1 minute and 28 seconds
......@@ -6,7 +6,7 @@
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>math-pretty-printer</artifactId>
<version>0.2.2-SNAPSHOT</version>
<version>0.3.0-SNAPSHOT</version>
<properties>
......
......@@ -4,6 +4,7 @@ import com.google.common.eventbus.Subscribe;
import de.monticore.lang.math._ast.ASTMathCompilationUnit;
import de.monticore.lang.math._ast.ASTMathScript;
import de.monticore.lang.monticar.mpp.core.events.EventsService;
import de.monticore.lang.monticar.mpp.core.options.OptionsParsedEvent;
import de.monticore.lang.monticar.mpp.core.options.OptionsService;
import de.monticore.lang.monticar.mpp.core.parser.MathFileParsedEvent;
import de.monticore.lang.monticar.mpp.core.tex.SVGGenerator;
......@@ -32,7 +33,7 @@ public class GeneratorHelper {
this.svggenerator = new SVGGenerator(this.optionsService);
this.htmlPrinter = new HTMLMathPrettyPrinter();
this.texPrinter = new TeXHTMLMathPrettyPrinter(svggenerator);
this.texPrinter = new TeXHTMLMathPrettyPrinter(this.svggenerator);
this.nodes = new ArrayList<>();
......@@ -82,4 +83,12 @@ public class GeneratorHelper {
public void onMathFileParsed(MathFileParsedEvent event) {
this.nodes.add(event.ast);
}
@Subscribe
public void onOptionsParsed(OptionsParsedEvent event) {
boolean isInternal = this.optionsService.isInternal();
this.texPrinter.setInternal(isInternal);
this.htmlPrinter.setInternal(isInternal);
}
}
......@@ -17,6 +17,7 @@ public class OptionsService {
protected Path modelPath;
protected Path outputPath;
protected boolean internal;
public OptionsService(EventsService eventsService) {
this.eventsService = eventsService;
......@@ -31,6 +32,8 @@ public class OptionsService {
"Absolute Path to the package root of the models.");
this.options.addOption("out", "output-path", true,
"Absolute Path to the root directory of the output.");
this.options.addOption("i", "internal", false,
"If set, runs the application in internal mode.");
}
public Path getModelPath() {
......@@ -49,6 +52,9 @@ public class OptionsService {
return this.outputPath.resolve(relativePath);
}
public boolean isInternal() {
return this.internal;
}
@Subscribe
public void onApplicationConfigure(ApplicationConfigureEvent event) throws ParseException {
......@@ -60,6 +66,7 @@ public class OptionsService {
this.modelPath = Paths.get(modelPath);
this.outputPath = Paths.get(outputPath);
this.internal = commandLine.hasOption("i");
OptionsParsedEvent optionsEvent = new OptionsParsedEvent(this.modelPath, this.outputPath);
......
package de.monticore.lang.monticar.mpp.montimath;
import de.monticore.lang.math._ast.ASTMathCompilationUnit;
public class HTMLMathPrettyPrinter extends MathPrettyPrinter {
protected String getWrappedKeyword(String keyword) {
return String.format("<span class=\"keyword\">%s</span>", keyword);
}
protected String getWrappedComment(String comment) {
return String.format("<span class=\"comment\">%s</span>", comment);
}
@Override
protected void printPackage() {
......@@ -47,4 +49,15 @@ public class HTMLMathPrettyPrinter extends MathPrettyPrinter {
protected void printElseIf() {
this.printer.print(this.getWrappedKeyword("elseif"));
}
@Override
protected void printComment(String comment) {
String[] lines = comment.split("\n");
if (lines.length > 0) lines[0] = lines[0].trim();
for (String line : lines) {
this.printer.println(this.getWrappedComment(line));
}
}
}
package de.monticore.lang.monticar.mpp.montimath;
import de.monticore.ast.Comment;
import de.monticore.commonexpressions._ast.*;
import de.monticore.lang.math._ast.*;
import de.monticore.lang.matrix._ast.ASTMathMatrixAccessExpression;
......@@ -20,22 +21,43 @@ import de.monticore.prettyprint.IndentPrinter;
import de.monticore.types.types._ast.ASTImportStatement;
import de.monticore.types.types._ast.ASTQualifiedName;
import java.util.List;
public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUnit>, MathStructuredVisitor {
protected final IndentPrinter printer;
protected boolean importVisited;
protected boolean inMatrixEnvironment;
protected boolean internal;
public MathPrettyPrinter() {
public MathPrettyPrinter(int spacing, boolean internal) {
this.printer = new IndentPrinter();
this.printer.setIndentLength(4);
this.printer.setIndentLength(spacing);
this.importVisited = false;
this.inMatrixEnvironment = false;
this.internal = internal;
}
public MathPrettyPrinter(int spacing) {
this(spacing, false);
}
public MathPrettyPrinter() {
this(4,false);
}
public void setInternal(boolean internal) {
this.internal = internal;
}
@Override
public String prettyPrint(ASTMathCompilationUnit node) {
this.printer.clearBuffer();
this.handle(node);
if(this.internal) {
this.printStatements(node);
} else {
this.handle(node);
}
return this.printer.getContent();
}
......@@ -47,10 +69,37 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
return this.printer.getContent();
}
protected void printStatements(ASTMathCompilationUnit node) {
List<ASTStatement> statements = node.getMathScript().getStatementsList();
for(ASTStatement statement: statements)
{
statement.accept(this);
}
}
protected void printSpace() {
this.printer.print(" ");
}
protected void printComments(List<Comment> comments) {
for (Comment comment : comments) {
String text = comment.getText();
this.printComment(text);
}
}
protected void printComment(String comment) {
String[] lines = comment.split("\n");
if (lines.length > 0) lines[0] = lines[0].trim();
for (String line : lines) {
this.printer.println(line);
}
}
/*========================================================
== TOKENS ================================================
========================================================*/
......@@ -131,6 +180,7 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
========================================================*/
@Override
public void visit(ASTMathCompilationUnit node) {
this.printComments(node.get_PreCommentList());
this.printPackage();
this.printSpace();
}
......@@ -149,6 +199,7 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
@Override
public void visit(ASTMathScript node) {
this.printer.println();
this.printComments(node.get_PreCommentList());
this.printScript();
this.printSpace();
this.printer.print(node.getName());
......@@ -186,6 +237,11 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
@Override
public void visit(ASTMathAssignmentDeclarationStatement node) {
this.printComments(node.get_PreCommentList());
}
@Override
public void revisit(ASTMathAssignmentDeclarationStatement node) {
this.printSpace();
this.printer.print(node.getName());
}
......@@ -215,6 +271,7 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
@Override
public void visit(ASTMathAssignmentStatement node) {
this.printComments(node.get_PreCommentList());
this.printer.print(node.getName());
}
......@@ -250,11 +307,15 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
this.printer.indent();
}
public void visit(ASTMathForLoopExpression node) {
this.printComments(node.get_PreCommentList());
}
@Override
public void endVisit(ASTMathForLoopExpression node) {
this.printer.unindent();
this.printEnd();
this.printer.println(2);
this.printer.println();
}
@Override
......@@ -276,7 +337,7 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
@Override
public void endVisit(ASTMathIfStatement node) {
this.printEnd();
this.printer.println(2);
this.printer.println();
}
@Override
......@@ -293,6 +354,18 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
this.printSpace();
}
/*public void visit(ASTMathStatements node) {
this.printer.println();
this.printer.indent();
this.printComments(node.get_PreCommentList());
}
@Override
public void endVisit(ASTMathStatements node) {
this.printComments(node.get_PostCommentList());
this.printer.unindent();
}*/
@Override
public void visit(ASTLessEqualExpression node) {
this.printSpace();
......@@ -355,12 +428,13 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
@Override
public void visit(ASTMathDeclarationStatement node) {
this.printSpace();
this.printer.print(node.getName());
this.printComments(node.get_PreCommentList());
}
@Override
public void endVisit(ASTMathDeclarationStatement node) {
this.printSpace();
this.printer.print(node.getName());
this.printSemicolon();
this.printer.println();
}
......
......@@ -53,12 +53,15 @@ public interface MathStructuredVisitor extends MathVisitor {
@Override
default void handle(ASTMathAssignmentDeclarationStatement node) {
this.getRealThis().traversePartOne(node);
this.getRealThis().visit(node);
this.getRealThis().traversePartOne(node);
this.getRealThis().revisit(node);
this.getRealThis().traversePartTwo(node);
this.getRealThis().endVisit(node);
}
default void revisit(ASTMathAssignmentDeclarationStatement node) {}
default void traversePartOne(ASTMathAssignmentDeclarationStatement node) {
if (null != node.getType()) {
node.getType().accept(this.getRealThis());
......@@ -176,8 +179,8 @@ public interface MathStructuredVisitor extends MathVisitor {
@Override
default void handle(ASTMathDeclarationStatement node) {
this.getRealThis().traverse(node);
this.getRealThis().visit(node);
this.getRealThis().traverse(node);
this.getRealThis().endVisit(node);
}
......
package de.monticore.lang.monticar.mpp.montimath;
import de.monticore.expressionsbasis._ast.ASTExpression;
import de.monticore.lang.math._ast.*;
import de.monticore.lang.monticar.mpp.core.tex.SVGGenerator;
import de.se_rwth.commons.logging.Log;
......@@ -41,17 +40,13 @@ public class TeXHTMLMathPrettyPrinter extends HTMLMathPrettyPrinter implements T
}
@Override
public void visit(ASTMathAssignmentDeclarationStatement node) {
this.printImageTag(node);
}
@Override
public void visit(ASTMathDeclarationStatement node) {
public void revisit(ASTMathAssignmentDeclarationStatement node) {
this.printImageTag(node);
}
@Override
public void endVisit(ASTMathDeclarationStatement node) {
this.printImageTag(node);
this.printer.println();
}
......@@ -72,7 +67,6 @@ public class TeXHTMLMathPrettyPrinter extends HTMLMathPrettyPrinter implements T
@Override
public void visit(ASTMathForLoopHead node) {
this.printer.println();
this.printFor();
this.printSpace();
this.printImageTag(node);
......@@ -80,7 +74,6 @@ public class TeXHTMLMathPrettyPrinter extends HTMLMathPrettyPrinter implements T
@Override
public void visit(ASTMathIfExpression node) {
this.printer.println();
this.printIf();
this.printSpace();
this.printImageTag(node);
......
......@@ -2,6 +2,7 @@ package de.monticore.lang.monticar.mpp.montimath;
import de.monticore.lang.math._ast.*;
public interface TeXHTMLMathStructuredVisitor extends MathStructuredVisitor {
@Override
default void traversePartOne(ASTMathAssignmentDeclarationStatement node) {}
......@@ -18,11 +19,15 @@ public interface TeXHTMLMathStructuredVisitor extends MathStructuredVisitor {
@Override
default void traverse(ASTMathForLoopHead node) {}
@Override
/*@Override
default void traverse(ASTMathIfExpression node) {
if (null != node.getBody(0)) {
node.getBody(0).accept(this.getRealThis());
for(ASTStatement statement: node.getBodyList())
{
if (null != node.getBody(0)) {
node.getBody(0).accept(this.getRealThis());
}
}
}
@Override
......@@ -30,5 +35,21 @@ public interface TeXHTMLMathStructuredVisitor extends MathStructuredVisitor {
if (null != node.getBody(0)) {
node.getBody(0).accept(this.getRealThis());
}
}*/
@Override
default void handle(ASTMathIfExpression node) {
this.getRealThis().visit(node);
this.getRealThis().revisit(node);
this.getRealThis().traversePartTwo(node);
this.getRealThis().endVisit(node);
}
@Override
default void handle(ASTMathElseIfExpression node) {
this.getRealThis().visit(node);
this.getRealThis().revisit(node);
this.getRealThis().traversePartTwo(node);
this.getRealThis().endVisit(node);
}
}
......@@ -8,6 +8,7 @@ import de.monticore.lang.matrixexpressions._ast.ASTMathArithmeticMatrixComplexTr
import de.monticore.lang.matrixexpressions._ast.ASTMathArithmeticMatrixEEPowExpression;
import de.monticore.lang.monticar.types2._ast.ASTDimension;
import de.monticore.lang.monticar.types2._ast.ASTElementType;
import de.monticore.lang.matrixexpressions._ast.*;
import de.monticore.numberunit._ast.ASTTimeDiv;
public class TeXMathPrettyPrinter extends MathPrettyPrinter implements TeXMathStructuredVisitor {
......@@ -58,7 +59,12 @@ public class TeXMathPrettyPrinter extends MathPrettyPrinter implements TeXMathSt
}
@Override
public void visit(ASTMathAssignmentDeclarationStatement node) {
public void visit(ASTMathAssignmentDeclarationStatement node){
}
@Override
public void revisit(ASTMathAssignmentDeclarationStatement node) {
this.printer.print("~");
this.printer.print(node.getName());
this.printer.print("~");
......@@ -72,8 +78,15 @@ public class TeXMathPrettyPrinter extends MathPrettyPrinter implements TeXMathSt
@Override
public void visit(ASTMathDeclarationStatement node) {
}
@Override
public void endVisit(ASTMathDeclarationStatement node) {
this.printer.print("~");
this.printer.print(node.getName());
this.printSemicolon();
this.printer.println();
}
@Override
......@@ -140,6 +153,13 @@ public class TeXMathPrettyPrinter extends MathPrettyPrinter implements TeXMathSt
this.printer.print("~\\text{mod}~");
}
@Override
public void revisit(ASTMathArithmeticMatrixLeftDivideExpression node) {
this.printSpace();
this.printer.print("~\\backslash~");
this.printSpace();
}
@Override
public void visit(ASTMathForLoopHead node) {
this.printer.print(node.getName());
......@@ -218,4 +238,23 @@ public class TeXMathPrettyPrinter extends MathPrettyPrinter implements TeXMathSt
public void endVisit(ASTMathArithmeticMatrixComplexTransposeExpression node) {
this.printer.print("^T");
}
@Override
public void revisit(ASTMathArithmeticMatrixEEMultExpression node) {
this.printSpace();
this.printer.print("\\circ");
this.printSpace();
}
@Override
public void revisit(ASTMathArithmeticMatrixEERightDivideExpression node) {
this.printSpace();
this.printer.print("\\circ");
this.printSpace();
}
@Override
public void endVisit(ASTMathArithmeticMatrixEERightDivideExpression node) {
this.printer.print("^{\\circ(-1)}");
}
}
......@@ -4,7 +4,7 @@ import de.monticore.lang.math._ast.ASTMathElseIfExpression;
import de.monticore.lang.math._ast.ASTMathIfExpression;
public interface TeXMathStructuredVisitor extends MathStructuredVisitor {
@Override
/*@Override
default void traverse(ASTMathIfExpression node) {
if (null != node.getCondition()) {
node.getCondition().accept(this.getRealThis());
......@@ -16,5 +16,20 @@ public interface TeXMathStructuredVisitor extends MathStructuredVisitor {
if (null != node.getCondition()) {
node.getCondition().accept(this.getRealThis());
}
}*/
@Override
default void handle(ASTMathIfExpression node) {
this.getRealThis().visit(node);
this.getRealThis().traversePartOne(node);
this.getRealThis().revisit(node);
this.getRealThis().endVisit(node);
}
@Override
default void handle(ASTMathElseIfExpression node) {
this.getRealThis().visit(node);
this.getRealThis().traversePartOne(node);
this.getRealThis().revisit(node);
this.getRealThis().endVisit(node);
}
}
......@@ -34,7 +34,11 @@
}
.keyword {
color:#009fff !important;
color:#569cd6;
}
.comment {
color:#608b4e !important;
}
pre {
......@@ -48,6 +52,7 @@
padding-right:10px;
-moz-user-select:none;
-webkit-user-select:none;
color:#5a5a5a;
}
img {
......@@ -71,6 +76,7 @@
height:100%;
width:100%;
backface-visibility:hidden;
-webkit-backface-visibility:hidden;
margin-top:35px;
}
......@@ -106,7 +112,7 @@
}
input:checked {
background-color:#009fff !important;
background-color:#569cd6 !important;
}
input.code {
......
......@@ -8,7 +8,6 @@ import java.nio.file.Paths;
public class ApplicationTest {
@Test
@Ignore
public void testStart() {
String modelPath = Paths.get("src/test/resources/models/montimath/vanilla").toAbsolutePath().toString();
String outputPath = Paths.get("target/generated-sources/application").toAbsolutePath().toString();
......
......@@ -24,6 +24,7 @@ public class ArithmeticTest extends AbstractModelTest {
"package montimath.vanilla;\n" +
"\n" +
"script Arithmetic\n" +
" //Variable Declaration\n" +
" Q rational;\n" +
" rational = 1 + 2 * 3;\n" +
"end";
......
package de.monticore.lang.monticar.mpp.models;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import static junit.framework.TestCase.assertEquals;
public class CommentsTest extends AbstractModelTest {
@Test