Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
FortyMcFortface
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Johannes Mey
FortyMcFortface
Commits
4d2d2d42
Commit
4d2d2d42
authored
8 years ago
by
Johannes Mey
Browse files
Options
Downloads
Patches
Plain Diff
add improved output (including long line breaks)
parent
62a56271
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Parser/spec/PrintingCore.jadd
+120
-2
120 additions, 2 deletions
Parser/spec/PrintingCore.jadd
with
120 additions
and
2 deletions
Parser/spec/PrintingCore.jadd
+
120
−
2
View file @
4d2d2d42
aspect SimplePrinting {
String ASTNode.prettyPrint() {
return this.prettyPrint(" ", false);
}
String ASTNode.prettyPrint(String indenter, boolean uppercase) {
PrettyPrinter pp = new PrettyPrinter(indenter, uppercase);
this.prettyPrint(pp);
return pp.toString();
}
}
public class PrettyPrinter {
private StringBuilder s;
...
...
@@ -6,6 +17,8 @@ public class PrettyPrinter {
private int indent;
private boolean newline;
private boolean bof;
private boolean useColons;
private int lineLength;
public PrettyPrinter(String indenter, boolean uppercase) {
...
...
@@ -16,6 +29,12 @@ public class PrettyPrinter {
this.indent = 0;
this.newline = false;
this.bof = true;
this.useColons = false;
this.lineLength = 84;
}
public boolean useColons() {
return useColons;
}
public PrettyPrinter append(String s){
...
...
@@ -81,7 +100,106 @@ public class PrettyPrinter {
public String toString() {
flushNewline();
return s.toString();
return linebreak(s.toString());
}
/**
* Adds linebreaks to fortran sources with long line
* Default fortran allows 132 chars on a line
* If more - you have to break it with an ampersand
*/
public String linebreak(String source) {
StringBuilder sb = new StringBuilder();
int index = source.indexOf("\n");
int builderIndex = 0;
while (index > 0)
{
// TODO long strings must be done here with everything else in an else-block
int nextIndex = source.indexOf("\n", index+1);
int nextComment = source.indexOf("!", index+1);
// when the line is too long but contains a comment, we substract the comment
if (nextComment < 0 || nextComment > nextIndex) {
nextComment = 0;
}
if (nextIndex - nextComment - index > lineLength) {
// append all previously found stuff to sb
sb.append(source.substring(builderIndex, index));
String longLine = source.substring(index+1, nextIndex);
String nextIndent = getNextIndent(longLine);
convertLongLine(sb, longLine, nextIndent);
builderIndex = nextIndex;
}
index = nextIndex;
}
sb.append(source.substring(builderIndex, source.length()));
return sb.toString();
}
/**
* detect the amount of indent for "line" by the "indenter" string
* returns the indent for the next line (amount+1)
*/
private String getNextIndent(String line) {
// first detect the indent-depth
int indentDepth = 0;
String followingIndents = "";
while (line.substring(indentDepth * indenter.length(), indentDepth*indenter.length() + indenter.length()).equals(indenter)) {
indentDepth++;
followingIndents += indenter;
}
followingIndents += indenter;
return followingIndents;
}
/**
* converts a long line (longer than "length") to multiple ampersand-split lines
* "nextIndent" is the amount of indention all following lines should get
* appends to the StringBuilder
*/
private void convertLongLine(StringBuilder sb, String line, String nextIndent) {
int length = lineLength;
String comment = "";
char[] splitChars = {' ', '\t', '(', ',', '+', '-', '*', '/', ')'};
Boolean isFirst = true;
// remove comment so we do not needlessly try to break this
if (line.indexOf("!")>0) {
comment = line.substring(line.indexOf("!"), line.length());
line = line.substring(0, line.indexOf("!"));
}
sb.append("\n");
while (line.length() > length) {
int breakPoint = 0;
for (char c: splitChars) {
breakPoint = line.lastIndexOf(c, length-1);
if (breakPoint > 0)
break;
}
if (breakPoint <= 0) {
sb.append("! ERROR your code can not be split in mulitple lines\n");
sb.append(nextIndent);
sb.append("! Try adding some whitespaces\n");
sb.append(nextIndent);
break;
}
// System.err.printf("Bla (%d, %d)\n", line.length(), breakPoint);
sb.append(line.substring(0, breakPoint));
for (int i=breakPoint; i<length-1; i++) {
sb.append(" ");
}
sb.append("&\n");
sb.append(nextIndent);
if (isFirst) {
isFirst = false;
length -= nextIndent.length();
}
line = line.substring(breakPoint, line.length()).replaceAll("^\\s*", "");
}
sb.append(line);
if (!comment.isEmpty()) {
sb.append("\n");
sb.append(nextIndent);
sb.append(comment);
}
}
}
\ No newline at end of file
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment