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;
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.ASTNode;
import de.monticore.ast.Comment;
import de.monticore.commonexpressions._ast.*;
import de.monticore.expressionsbasis._ast.ASTExpression;
import de.monticore.lang.math._ast.*;
import de.monticore.lang.matrix._ast.ASTMathMatrixAccessExpression;
import de.monticore.lang.matrix._ast.ASTMathMatrixValueExplicitExpression;
......@@ -19,6 +18,8 @@ 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;
......@@ -49,6 +50,24 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
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 ================================================
========================================================*/
......@@ -129,6 +148,7 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
========================================================*/
@Override
public void visit(ASTMathCompilationUnit node) {
this.printComments(node.get_PreCommentList());
this.printPackage();
this.printSpace();
}
......@@ -147,6 +167,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());
......@@ -181,6 +202,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());
}
......@@ -210,12 +236,14 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
@Override
public void visit(ASTMathAssignmentStatement node) {
this.printComments(node.get_PreCommentList());
this.printer.print(node.getName());
}
@Override
public void endVisit(ASTMathAssignmentStatement node) {
this.printSemicolon();
this.printComments(node.get_PostCommentList());
this.printer.println();
}
......@@ -231,7 +259,6 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
@Override
public void visit(ASTMathForLoopHead node) {
this.printer.println();
this.printFor();
this.printSpace();
this.printer.print(node.getName());
......@@ -240,16 +267,21 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
this.printSpace();
}
@Override
public void visit(ASTMathForLoopExpression node) {
this.printComments(node.get_PreCommentList());
}
@Override
public void endVisit(ASTMathForLoopExpression node) {
this.printEnd();
this.printSemicolon();
this.printer.println(2);
this.printComments(node.get_PostCommentList());
this.printer.println();
}
@Override
public void visit(ASTMathIfExpression node) {
this.printer.println();
this.printIf();
this.printSpace();
}
......@@ -258,7 +290,7 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
public void endVisit(ASTMathIfStatement node) {
this.printEnd();
this.printSemicolon();
this.printer.println(2);
this.printer.println();
}
@Override
......@@ -272,10 +304,12 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
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();
}
......@@ -329,13 +363,9 @@ public class MathPrettyPrinter implements AstPrettyPrinter<ASTMathCompilationUni
}
@Override
public void visit(ASTMathDeclarationStatement node) {
public void endVisit(ASTMathDeclarationStatement node) {
this.printSpace();
this.printer.print(node.getName());
}
@Override
public void endVisit(ASTMathDeclarationStatement node) {
this.printSemicolon();
this.printer.println();
}
......
......@@ -51,12 +51,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());
......@@ -174,8 +177,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);
}
......
......@@ -41,17 +41,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 +68,6 @@ public class TeXHTMLMathPrettyPrinter extends HTMLMathPrettyPrinter implements T
@Override
public void visit(ASTMathForLoopHead node) {
this.printer.println();
this.printFor();
this.printSpace();
this.printImageTag(node);
......
......@@ -55,7 +55,7 @@ public class TeXMathPrettyPrinter extends MathPrettyPrinter implements TeXMathSt
}
@Override
public void visit(ASTMathAssignmentDeclarationStatement node) {
public void revisit(ASTMathAssignmentDeclarationStatement node) {
this.printer.print("~");
this.printer.print(node.getName());
this.printer.print("~");
......@@ -68,9 +68,11 @@ public class TeXMathPrettyPrinter extends MathPrettyPrinter implements TeXMathSt
}
@Override
public void visit(ASTMathDeclarationStatement node) {
public void endVisit(ASTMathDeclarationStatement node) {
this.printer.print("~");
this.printer.print(node.getName());
this.printSemicolon();
this.printer.println();
}
@Override
......
......@@ -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 {
......@@ -107,7 +112,7 @@
}
input:checked {
background-color:#009fff !important;
background-color:#569cd6 !important;
}
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