Commit d06713af authored by 0xJMR's avatar 0xJMR
Browse files

Added basic comments support.

parent 54b8677e
Pipeline #60545 passed with stages
in 1 minute and 5 seconds
package de.monticore.lang.monticar.mpp.montimath; package de.monticore.lang.monticar.mpp.montimath;
import de.monticore.lang.math._ast.ASTMathCompilationUnit;
public class HTMLMathPrettyPrinter extends MathPrettyPrinter { public class HTMLMathPrettyPrinter extends MathPrettyPrinter {
protected String getWrappedKeyword(String keyword) { protected String getWrappedKeyword(String keyword) {
return String.format("<span class=\"keyword\">%s</span>", 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 @Override
protected void printPackage() { protected void printPackage() {
...@@ -47,4 +49,15 @@ public class HTMLMathPrettyPrinter extends MathPrettyPrinter { ...@@ -47,4 +49,15 @@ public class HTMLMathPrettyPrinter extends MathPrettyPrinter {
protected void printElseIf() { protected void printElseIf() {
this.printer.print(this.getWrappedKeyword("elseif")); 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; package de.monticore.lang.monticar.mpp.montimath;
import de.monticore.ast.ASTNode; import de.monticore.ast.Comment;
import de.monticore.commonexpressions._ast.*; import de.monticore.commonexpressions._ast.*;
import de.monticore.expressionsbasis._ast.ASTExpression;
import de.monticore.lang.math._ast.*; import de.monticore.lang.math._ast.*;
import de.monticore.lang.matrix._ast.ASTMathMatrixAccessExpression; import de.monticore.lang.matrix._ast.ASTMathMatrixAccessExpression;
import de.monticore.lang.matrix._ast.ASTMathMatrixValueExplicitExpression; import de.monticore.lang.matrix._ast.ASTMathMatrixValueExplicitExpression;
...@@ -19,6 +18,8 @@ import de.monticore.prettyprint.IndentPrinter; ...@@ -19,6 +18,8 @@ import de.monticore.prettyprint.IndentPrinter;
import de.monticore.types.types._ast.ASTImportStatement; import de.monticore.types.types._ast.ASTImportStatement;
import de.monticore.types.types._ast.ASTQualifiedName; import de.monticore.types.types._ast.ASTQualifiedName;
import java.util.List;
public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUnit>, MathStructuredVisitor { public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUnit>, MathStructuredVisitor {
protected final IndentPrinter printer; protected final IndentPrinter printer;
protected boolean importVisited; protected boolean importVisited;
...@@ -49,6 +50,24 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni ...@@ -49,6 +50,24 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
this.printer.print(" "); 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.print(line);
}
}
/*======================================================== /*========================================================
== TOKENS ================================================ == TOKENS ================================================
========================================================*/ ========================================================*/
...@@ -129,6 +148,7 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni ...@@ -129,6 +148,7 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
========================================================*/ ========================================================*/
@Override @Override
public void visit(ASTMathCompilationUnit node) { public void visit(ASTMathCompilationUnit node) {
this.printComments(node.get_PreCommentList());
this.printPackage(); this.printPackage();
this.printSpace(); this.printSpace();
} }
...@@ -147,6 +167,7 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni ...@@ -147,6 +167,7 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
@Override @Override
public void visit(ASTMathScript node) { public void visit(ASTMathScript node) {
this.printer.println(); this.printer.println();
this.printComments(node.get_PreCommentList());
this.printScript(); this.printScript();
this.printSpace(); this.printSpace();
this.printer.print(node.getName()); this.printer.print(node.getName());
...@@ -181,6 +202,11 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni ...@@ -181,6 +202,11 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
@Override @Override
public void visit(ASTMathAssignmentDeclarationStatement node) { public void visit(ASTMathAssignmentDeclarationStatement node) {
this.printComments(node.get_PreCommentList());
}
@Override
public void revisit(ASTMathAssignmentDeclarationStatement node) {
this.printSpace(); this.printSpace();
this.printer.print(node.getName()); this.printer.print(node.getName());
} }
...@@ -210,12 +236,14 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni ...@@ -210,12 +236,14 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
@Override @Override
public void visit(ASTMathAssignmentStatement node) { public void visit(ASTMathAssignmentStatement node) {
this.printComments(node.get_PreCommentList());
this.printer.print(node.getName()); this.printer.print(node.getName());
} }
@Override @Override
public void endVisit(ASTMathAssignmentStatement node) { public void endVisit(ASTMathAssignmentStatement node) {
this.printSemicolon(); this.printSemicolon();
this.printComments(node.get_PostCommentList());
this.printer.println(); this.printer.println();
} }
...@@ -231,7 +259,6 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni ...@@ -231,7 +259,6 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
@Override @Override
public void visit(ASTMathForLoopHead node) { public void visit(ASTMathForLoopHead node) {
this.printer.println();
this.printFor(); this.printFor();
this.printSpace(); this.printSpace();
this.printer.print(node.getName()); this.printer.print(node.getName());
...@@ -240,16 +267,21 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni ...@@ -240,16 +267,21 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
this.printSpace(); this.printSpace();
} }
@Override
public void visit(ASTMathForLoopExpression node) {
this.printComments(node.get_PreCommentList());
}
@Override @Override
public void endVisit(ASTMathForLoopExpression node) { public void endVisit(ASTMathForLoopExpression node) {
this.printEnd(); this.printEnd();
this.printSemicolon(); this.printSemicolon();
this.printer.println(2); this.printComments(node.get_PostCommentList());
this.printer.println();
} }
@Override @Override
public void visit(ASTMathIfExpression node) { public void visit(ASTMathIfExpression node) {
this.printer.println();
this.printIf(); this.printIf();
this.printSpace(); this.printSpace();
} }
...@@ -258,7 +290,7 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni ...@@ -258,7 +290,7 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
public void endVisit(ASTMathIfStatement node) { public void endVisit(ASTMathIfStatement node) {
this.printEnd(); this.printEnd();
this.printSemicolon(); this.printSemicolon();
this.printer.println(2); this.printer.println();
} }
@Override @Override
...@@ -272,10 +304,12 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni ...@@ -272,10 +304,12 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
public void visit(ASTMathStatements node) { public void visit(ASTMathStatements node) {
this.printer.println(); this.printer.println();
this.printer.indent(); this.printer.indent();
this.printComments(node.get_PreCommentList());
} }
@Override @Override
public void endVisit(ASTMathStatements node) { public void endVisit(ASTMathStatements node) {
this.printComments(node.get_PostCommentList());
this.printer.unindent(); this.printer.unindent();
} }
...@@ -329,13 +363,9 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni ...@@ -329,13 +363,9 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
} }
@Override @Override
public void visit(ASTMathDeclarationStatement node) { public void endVisit(ASTMathDeclarationStatement node) {
this.printSpace(); this.printSpace();
this.printer.print(node.getName()); this.printer.print(node.getName());
}
@Override
public void endVisit(ASTMathDeclarationStatement node) {
this.printSemicolon(); this.printSemicolon();
this.printer.println(); this.printer.println();
} }
......
...@@ -51,12 +51,15 @@ public interface MathStructuredVisitor extends MathVisitor { ...@@ -51,12 +51,15 @@ public interface MathStructuredVisitor extends MathVisitor {
@Override @Override
default void handle(ASTMathAssignmentDeclarationStatement node) { default void handle(ASTMathAssignmentDeclarationStatement node) {
this.getRealThis().traversePartOne(node);
this.getRealThis().visit(node); this.getRealThis().visit(node);
this.getRealThis().traversePartOne(node);
this.getRealThis().revisit(node);
this.getRealThis().traversePartTwo(node); this.getRealThis().traversePartTwo(node);
this.getRealThis().endVisit(node); this.getRealThis().endVisit(node);
} }
default void revisit(ASTMathAssignmentDeclarationStatement node) {}
default void traversePartOne(ASTMathAssignmentDeclarationStatement node) { default void traversePartOne(ASTMathAssignmentDeclarationStatement node) {
if (null != node.getType()) { if (null != node.getType()) {
node.getType().accept(this.getRealThis()); node.getType().accept(this.getRealThis());
...@@ -174,8 +177,8 @@ public interface MathStructuredVisitor extends MathVisitor { ...@@ -174,8 +177,8 @@ public interface MathStructuredVisitor extends MathVisitor {
@Override @Override
default void handle(ASTMathDeclarationStatement node) { default void handle(ASTMathDeclarationStatement node) {
this.getRealThis().traverse(node);
this.getRealThis().visit(node); this.getRealThis().visit(node);
this.getRealThis().traverse(node);
this.getRealThis().endVisit(node); this.getRealThis().endVisit(node);
} }
......
...@@ -41,17 +41,13 @@ public class TeXHTMLMathPrettyPrinter extends HTMLMathPrettyPrinter implements T ...@@ -41,17 +41,13 @@ public class TeXHTMLMathPrettyPrinter extends HTMLMathPrettyPrinter implements T
} }
@Override @Override
public void visit(ASTMathAssignmentDeclarationStatement node) { public void revisit(ASTMathAssignmentDeclarationStatement node) {
this.printImageTag(node);
}
@Override
public void visit(ASTMathDeclarationStatement node) {
this.printImageTag(node); this.printImageTag(node);
} }
@Override @Override
public void endVisit(ASTMathDeclarationStatement node) { public void endVisit(ASTMathDeclarationStatement node) {
this.printImageTag(node);
this.printer.println(); this.printer.println();
} }
...@@ -72,7 +68,6 @@ public class TeXHTMLMathPrettyPrinter extends HTMLMathPrettyPrinter implements T ...@@ -72,7 +68,6 @@ public class TeXHTMLMathPrettyPrinter extends HTMLMathPrettyPrinter implements T
@Override @Override
public void visit(ASTMathForLoopHead node) { public void visit(ASTMathForLoopHead node) {
this.printer.println();
this.printFor(); this.printFor();
this.printSpace(); this.printSpace();
this.printImageTag(node); this.printImageTag(node);
......
...@@ -55,7 +55,7 @@ public class TeXMathPrettyPrinter extends MathPrettyPrinter implements TeXMathSt ...@@ -55,7 +55,7 @@ public class TeXMathPrettyPrinter extends MathPrettyPrinter implements TeXMathSt
} }
@Override @Override
public void visit(ASTMathAssignmentDeclarationStatement node) { public void revisit(ASTMathAssignmentDeclarationStatement node) {
this.printer.print("~"); this.printer.print("~");
this.printer.print(node.getName()); this.printer.print(node.getName());
this.printer.print("~"); this.printer.print("~");
...@@ -68,9 +68,11 @@ public class TeXMathPrettyPrinter extends MathPrettyPrinter implements TeXMathSt ...@@ -68,9 +68,11 @@ public class TeXMathPrettyPrinter extends MathPrettyPrinter implements TeXMathSt
} }
@Override @Override
public void visit(ASTMathDeclarationStatement node) { public void endVisit(ASTMathDeclarationStatement node) {
this.printer.print("~"); this.printer.print("~");
this.printer.print(node.getName()); this.printer.print(node.getName());
this.printSemicolon();
this.printer.println();
} }
@Override @Override
......
...@@ -34,7 +34,11 @@ ...@@ -34,7 +34,11 @@
} }
.keyword { .keyword {
color:#009fff !important; color:#569cd6;
}
.comment {
color:#608b4e !important;
} }
pre { pre {
...@@ -48,6 +52,7 @@ ...@@ -48,6 +52,7 @@
padding-right:10px; padding-right:10px;
-moz-user-select:none; -moz-user-select:none;
-webkit-user-select:none; -webkit-user-select:none;
color:#5a5a5a;
} }
img { img {
...@@ -107,7 +112,7 @@ ...@@ -107,7 +112,7 @@
} }
input:checked { input:checked {
background-color:#009fff !important; background-color:#569cd6 !important;
} }
input.code { input.code {
......
/*
* (C) SE RWTH 2018
*/
package montimath.vanilla;
/*
* A Dummy Script to show that comments can be shown.
*/
script Comments
/* Vector of the form [1,3,5,7,9] */
Q(0:10)^{1,5} c = 1:2:10;
Q x = 0;
Q y = 0;
// Iterate over all i in c
for i = c
// Iterate over all j in c
for j = c
// y might become very small when the
// entries in c are very large.
y += 1 / (c(x) * j^i);
end;
x += 1;
end;
end
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment