Skip to content
Snippets Groups Projects
Commit a599cf8f authored by Johannes Mey's avatar Johannes Mey
Browse files

Merge branch 'simplified'

parents 30cdaa69 194a384f
No related branches found
No related tags found
No related merge requests found
Showing
with 2051 additions and 1118 deletions
builds/
.idea/workspace.xml
.idea/task.xml
**/out/
......
......@@ -18,6 +18,7 @@ jastadd: ast
rats:
mkdir -p src-gen/org/tud/forty/parser
cd spec; java -jar ../tools/rats.jar -lgpl -out ../src-gen/org/tud/forty/parser SlottableFortranParser.rats
./minify.sh
doc:
mkdir -p src-gen
......@@ -28,5 +29,12 @@ clean:
rm -f spec-gen/*
rm -rf src-gen/*
dot:
sed -i 's|class PrettyPrint|aspect PrettyPrint|' spec/Printing.jadd
sed -i 's|class PrettyPrint|aspect PrettyPrint|' spec/OmpPrinting.jadd
cd src-gen; java -jar ../tools/jastadd/jastadd2.jar --dot --package="org.tud.forty.ast" --indent='4space' ../spec-gen/*.ast ../spec/*.jadd
sed -i 's|aspect PrettyPrint|class PrettyPrint|' spec/Printing.jadd
sed -i 's|aspect PrettyPrint|class PrettyPrint|' spec/OmpPrinting.jadd
drfort:
javac -cp tools/rats.jar -d tools/DrFort/ src-gen/org/tud/forty/ast/* src-gen/org/tud/forty/parser/* src/DrFort.java
......@@ -4,8 +4,8 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src-gen" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src-gen" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
......
#!/bin/bash
cd src-gen/org/tud/forty/parser
sed -r ':a; s%(.*)/\*.*\*/%\1%; ta; /\/\*/ !b; N; ba' SlottableFortranParser.java > sp.tmp
sed '/ \/\/ ==/d' sp.tmp > sp2.tmp
sed '/^$/d' sp2.tmp > SlottableFortranParser.java
rm sp.tmp sp2.tmp
......@@ -91,4 +91,14 @@ aspect Debug {
}
return b;
}
protected String ASTNode.getPrettyPrintLine() {
PrettyPrinter sw = new PrettyPrinter(" ", false);
try {
this.prettyPrint(sw);
return sw.toString().trim().split("\n")[0];
} catch (RuntimeException e) {
return e.getMessage();
}
}
}
......@@ -145,7 +145,6 @@ aspect Printing {
public void AccLoopConstruct.prettyPrint(PrettyPrinter s) {
getAccLoopStmt().prettyPrint(s);
getDoConstruct().prettyPrint(s);
getDoConstruct().prettyPrint(s);
if (hasAccEndLoopStmt()) {
getAccEndLoopStmt().prettyPrint(s);
}
......
module ConditionalLines();
modify Fortran2008;
import ConditionalLinesTokens;
//// // allow OpenMP constructs as executable_constructs
//// ConditionalExecutableConstruct:ExecutableConstruct ::= ExecutableConstruct;
ExecutableConstruct executable_construct +=
<ConditionalExecutableConstruct> CL executable_construct
/ <BEGINNING> ...
;
//// ConditionalDeclarationConstruct:DeclarationConstruct ::= DeclarationConstruct;
DeclarationConstruct declaration_construct +=
<ConditionalDeclarationConstruct> CL declaration_construct
/ <BEGINNING> ...
;
aspect PrettyPrint {
//// ConditionalExecutableConstruct:ExecutableConstruct ::= ExecutableConstruct;
public void ConditionalExecutableConstruct.prettyPrint(PrettyPrinter s) {
s.append("!$ ");
getExecutableConstruct().prettyPrint(s);
}
//// ConditionalDeclarationConstruct:DeclarationConstruct ::= DeclarationConstruct;
public void ConditionalDeclarationConstruct.prettyPrint(PrettyPrinter s) {
s.append("!$ ");
getDeclarationConstruct().prettyPrint(s);
}
}
\ No newline at end of file
module ConditionalLinesTokens();
import FortranTokens;
transient void CL = "!$ " SS;
This diff is collapsed.
......@@ -2,15 +2,11 @@ module FortranParser();
modify OpenMp;
header {
import org.tud.forty.ast.*;
}
//// Root ::= Preamble:Comment* ProgramUnit Postamble:Comment* ;
//// Root ::= Preamble:Comment* Program ;
public Root root =
bc:(comment)* s:program_unit ec:(comment)*
bc:(comment)* s:program
{
yyValue = new Root(new List().addAll(bc.list()), s, new List().addAll(ec.list()));
yyValue = new Root(new List().addAll(bc.list()), s);
}
;
......@@ -76,6 +76,7 @@ transient void DELIM = D E L I M SS;
transient void DIMENSION = D I M E N S I O N SS;
transient void DIRECT = D I R E C T SS;
transient void DO = D O SS;
transient void DOUBLE_COMPLEX = D O U B L E SS C O M P L E X SS;
transient void DOUBLE_PRECISION = D O U B L E SS P R E C I S I O N SS;
transient void ELEMENTAL = E L E M E N T A L SS;
transient void ELSE = E L S E SS;
......@@ -223,3 +224,4 @@ transient void COMMA = "," SS;
transient void COLONCOLON = "::" SS;
transient void ASTERISK = "*" SS;
transient void EXCLAMATIONMARK = "!";
transient void SEMICOLON = ";";
aspect Helpers {
public void ASTNode.checkTreeStructure() {
for (ASTNode child : astChildren()) {
if (child.getParent() != this) {
throw new RuntimeException();
}
child.checkTreeStructure();
}
}
public void ASTNode.fixTreeStructure() {
for (ASTNode child : astChildren()) {
if (child.getParent() != this) {
// System.err.println("fixed parent of " + child);
child.parent = this;
}
child.fixTreeStructure();
}
}
}
class PrettyPrint {
// OmpVariableListItem:OmpListItem ::= Variable ;
public void OmpVariableListItem.prettyPrint(PrettyPrinter s) {
getVariable().prettyPrint(s);
}
// OmpArraySectionListItem:OmpListItem ::= ArraySection ;
public void OmpArraySectionListItem.prettyPrint(PrettyPrinter s) {
getArraySection().prettyPrint(s);
}
// OmpNameListItem:OmpListItem ::= Name ;
public void OmpNameListItem.prettyPrint(PrettyPrinter s) {
getName().prettyPrint(s);
}
// =============================================================================
// Directives
// =============================================================================
......@@ -49,12 +37,12 @@ class PrettyPrint {
}
// OMP 2.12.7
// OmpFlushDirective:OmpDirective ::= OmpListItem* ;
// OmpFlushDirective:OmpDirective ::= Variable* ;
public void OmpFlushDirective.prettyPrint(PrettyPrinter s) {
s.append("!$omp flush");
if (getOmpListItemList().numChildren() > 0) {
if (getVariableList().numChildren() > 0) {
s.append(" (");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
s.lb();
......@@ -125,10 +113,10 @@ class PrettyPrint {
}
// OMP 2.14.2
// OmpThreadprivateDirective:OmpDirective ::= OmpListItem* ;
// OmpThreadprivateDirective:OmpDirective ::= Variable* ;
public void OmpThreadprivateDirective.prettyPrint(PrettyPrinter s) {
s.append("!$omp threadprivate(");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
s.lb();
}
......@@ -138,12 +126,12 @@ class PrettyPrint {
// DeclarationConstructs
// =============================================================================
//// OmpDeclareTargetDirective:OmpDeclarationConstruct ::= Comment OmpListItem* ;
//// OmpDeclareTargetDirective:OmpDeclarationConstruct ::= Comment Variable* ;
public void OmpDeclareTargetDirective.prettyPrint(PrettyPrinter s) {
s.append("!$omp declare target");
if (getOmpListItemList().numChildren() > 0) {
if (getVariableList().numChildren() > 0) {
s.append("(");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
s.lb();
......@@ -1006,10 +994,10 @@ class PrettyPrint {
// Clauses
// =============================================================================
// OmpAlignedClause:OmpClause ::= OmpListItem* Expr ;
// OmpAlignedClause:OmpClause ::= Variable* Expr ;
public void OmpAlignedClause.prettyPrint(PrettyPrinter s) {
s.append("aligned(");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
if (hasExpr()) {
s.append(":");
getExpr().prettyPrint(s);
......@@ -1024,17 +1012,17 @@ class PrettyPrint {
s.append(")");
}
// OmpCopyinClause:OmpClause ::= OmpListItem* ;
// OmpCopyinClause:OmpClause ::= Variable* ;
public void OmpCopyinClause.prettyPrint(PrettyPrinter s) {
s.append("copyin(");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
// OmpCopyprivateClause:OmpClause ::= OmpListItem* ;
// OmpCopyprivateClause:OmpClause ::= Variable* ;
public void OmpCopyprivateClause.prettyPrint(PrettyPrinter s) {
s.append("copyprivate(");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
......@@ -1068,19 +1056,19 @@ class PrettyPrint {
// OmpDependInClause:OmpDependClause ;
public void OmpDependInClause.prettyPrint(PrettyPrinter s) {
s.append("depend(in: ");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
// OmpDependOutClause:OmpDependClause ;
public void OmpDependOutClause.prettyPrint(PrettyPrinter s) {
s.append("depend(out: ");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
// OmpDependInoutClause:OmpDependClause ;
public void OmpDependInoutClause.prettyPrint(PrettyPrinter s) {
s.append("depend(inout: ");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
......@@ -1098,10 +1086,10 @@ class PrettyPrint {
s.append(")");
}
// OmpFirstPrivateClause:OmpClause ::= OmpListItem* ;
// OmpFirstPrivateClause:OmpClause ::= Variable* ;
public void OmpFirstPrivateClause.prettyPrint(PrettyPrinter s) {
s.append("firstprivate(");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
......@@ -1112,24 +1100,24 @@ class PrettyPrint {
s.append(")");
}
//// OmpFromClause:OmpClause ::= OmpListItem* ;
//// OmpFromClause:OmpClause ::= Variable* ;
public void OmpFromClause.prettyPrint(PrettyPrinter s) {
s.append("from(");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
// OmpLastPrivateClause:OmpClause ::= OmpListItem* ;
// OmpLastPrivateClause:OmpClause ::= Variable* ;
public void OmpLastPrivateClause.prettyPrint(PrettyPrinter s) {
s.append("lastprivate(");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
//// OmpLinearClause:OmpClause ::= OmpListItem* [Expr] ;
//// OmpLinearClause:OmpClause ::= Variable* [Expr] ;
public void OmpLinearClause.prettyPrint(PrettyPrinter s) {
s.append("linear(");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
if (hasExpr()) {
s.append(":");
getExpr().prettyPrint(s);
......@@ -1140,25 +1128,25 @@ class PrettyPrint {
// OmpMapAllocClause:OmpMapClause ;
public void OmpMapAllocClause.prettyPrint(PrettyPrinter s) {
s.append("map(alloc: ");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
// OmpMapToClause:OmpMapClause ;
public void OmpMapToClause.prettyPrint(PrettyPrinter s) {
s.append("map(to: ");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
// OmpMapFromClause:OmpMapClause ;
public void OmpMapFromClause.prettyPrint(PrettyPrinter s) {
s.append("map(from: ");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
// OmpMapTofromClause:OmpMapClause ;
public void OmpMapTofromClause.prettyPrint(PrettyPrinter s) {
s.append("map(tofrom: ");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
......@@ -1167,7 +1155,7 @@ class PrettyPrint {
s.append("mergeable");
}
//// OmpNowaitClause:OmpClause ::= OmpListItem* ;
//// OmpNowaitClause:OmpClause ::= Variable* ;
public void OmpNowaitClause.prettyPrint(PrettyPrinter s) {
s.append("nowait");
}
......@@ -1191,10 +1179,10 @@ class PrettyPrint {
s.append("ordered");
}
// OmpPrivateClause:OmpClause ::= OmpListItem* ;
// OmpPrivateClause:OmpClause ::= Variable* ;
public void OmpPrivateClause.prettyPrint(PrettyPrinter s) {
s.append("private(");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
......@@ -1212,12 +1200,12 @@ class PrettyPrint {
}
// OmpReductionClause:OmpClause ::= OmpReductionIdentifier OmpListItem* ;
// OmpReductionClause:OmpClause ::= OmpReductionIdentifier Variable* ;
public void OmpReductionClause.prettyPrint(PrettyPrinter s) {
s.append("reduction(");
getOmpReductionIdentifier().prettyPrint(s);
s.append(":");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
......@@ -1308,10 +1296,10 @@ class PrettyPrint {
s.append("schedule(runtime)");
}
// OmpSharedClause:OmpClause ::= OmpListItem* ;
// OmpSharedClause:OmpClause ::= Variable* ;
public void OmpSharedClause.prettyPrint(PrettyPrinter s) {
s.append("shared(");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
......@@ -1322,10 +1310,10 @@ class PrettyPrint {
s.append(")");
}
//// OmpToClause:OmpClause ::= OmpListItem* ;
//// OmpToClause:OmpClause ::= Variable* ;
public void OmpToClause.prettyPrint(PrettyPrinter s) {
s.append("to(");
getOmpListItemList().prettyPrintJoin(s, ", ");
getVariableList().prettyPrintJoin(s, ", ");
s.append(")");
}
......
module OpenAcc();
modify Fortran2008;
modify ConditionalLines;
import OpenAccTokens;
// allow OpenACC constructs as executable_constructs
......@@ -244,7 +244,7 @@ AccEndAtomicStmt acc_end_atomic_stmt =
;
// loop constructs -------------------------------------------------------------
//// abstract AccDoConstruct:DoConstruct ;
//// abstract AccDoConstruct:DoConstruct ::= DoConstruct ;
AccDoConstruct acc_do_construct =
acc_loop_construct
/ acc_parallel_loop_construct
......
......@@ -25,26 +25,10 @@ DoConstruct do_construct +=
/ <BEGINNING> ...
;
//// abstract OmpListItem ;
//// // TODO rewrite to support common block names
//// OmpVariableListItem:OmpListItem ::= Variable ;
//// OmpArraySectionListItem:OmpListItem ::= ArraySection ;
//// OmpNameListItem:OmpListItem ::= Name ;
OmpListItem omp_list_item =
a:array_section
List<Variable> omp_list =
v:variable vl:(COMMA yyValue:variable)*
{
yyValue = new OmpArraySectionListItem(a);
}
/ v:variable
{
yyValue = new OmpVariableListItem(v);
}
;
List<OmpListItem> omp_list =
i:omp_list_item il:(COMMA yyValue:omp_list_item)*
{
yyValue = new List().add(i).addAll(il.list());
yyValue = new List().add(v).addAll(vl.list());
}
;
......@@ -108,7 +92,7 @@ OmpTaskwaitDirective omp_taskwait_directive =
;
//// // OMP 2.12.7
//// OmpFlushDirective:OmpDirective ::= OmpListItem* ;
//// OmpFlushDirective:OmpDirective ::= Variable* ;
OmpFlushDirective omp_flush_directive =
O_P O_FLUSH LPAREN l:omp_list RPAREN lnc:comment
{
......@@ -175,7 +159,7 @@ OmpCancellationPointDirective omp_cancellation_point_directive =
;
//// // OMP 2.14.2
//// OmpThreadprivateDirective:OmpDirective ::= OmpListItem* ;
//// OmpThreadprivateDirective:OmpDirective ::= Variable* ;
OmpThreadprivateDirective omp_threadprivate_directive =
O_P O_THREADPRIVATE LPAREN l:omp_list RPAREN lnc:comment
{
......@@ -192,7 +176,7 @@ OmpDeclarationConstruct omp_declaration_construct =
omp_declare_target_directive
;
//// OmpDeclareTargetDirective:OmpDeclarationConstruct ::= Comment OmpListItem* ;
//// OmpDeclareTargetDirective:OmpDeclarationConstruct ::= Comment Variable* ;
OmpDeclareTargetDirective omp_declare_target_directive =
O_P O_DECLARE O_TARGET LPAREN l:omp_list RPAREN lnc:comment
{
......@@ -798,7 +782,7 @@ OmpEndOrderedStmt omp_end_ordered_stmt =
// Loop Constructs
// =============================================================================
//// abstract OmpLoopConstruct:DoConstruct ;
//// abstract OmpLoopConstruct:DoConstruct ::= DoConstruct ;
OmpLoopConstruct omp_loop_construct =
omp_do_construct
/ omp_simd_construct
......@@ -1408,7 +1392,7 @@ OmpClause omp_clause =
/ omp_untied_clause
;
//// OmpAlignedClause:OmpClause ::= OmpListItem* [Expr] ;
//// OmpAlignedClause:OmpClause ::= Variable* [Expr] ;
OmpAlignedClause omp_aligned_clause =
O_ALIGNED LPAREN l:omp_list e:(COLON yyValue:expr)? RPAREN
{
......@@ -1425,7 +1409,7 @@ OmpCollapseClause omp_collapse_clause =
}
;
//// OmpCopyinClause:OmpClause ::= OmpListItem* ;
//// OmpCopyinClause:OmpClause ::= Variable* ;
OmpCopyinClause omp_copyin_clause =
O_COPYIN LPAREN l:omp_list RPAREN
{
......@@ -1433,7 +1417,7 @@ OmpCopyinClause omp_copyin_clause =
}
;
//// OmpCopyprivateClause:OmpClause ::= OmpListItem* ;
//// OmpCopyprivateClause:OmpClause ::= Variable* ;
OmpCopyprivateClause omp_copyprivate_clause =
O_COPYPRIVATE LPAREN l:omp_list RPAREN
{
......@@ -1465,7 +1449,7 @@ OmpDefaultClause omp_default_clause =
}
;
//// abstract OmpDependClause:OmpClause ::= OmpListItem* ;
//// abstract OmpDependClause:OmpClause ::= Variable* ;
//// OmpDependInClause:OmpDependClause ;
//// OmpDependOutClause:OmpDependClause ;
//// OmpDependInoutClause:OmpDependClause ;
......@@ -1509,7 +1493,7 @@ OmpFinalClause omp_final_clause =
}
;
//// OmpFirstPrivateClause:OmpClause ::= OmpListItem* ;
//// OmpFirstPrivateClause:OmpClause ::= Variable* ;
OmpFirstPrivateClause omp_firstprivate_clause =
O_FIRSTPRIVATE LPAREN l:omp_list RPAREN
{
......@@ -1525,7 +1509,7 @@ OmpIfClause omp_if_clause =
}
;
//// OmpFromClause:OmpClause ::= OmpListItem* ;
//// OmpFromClause:OmpClause ::= Variable* ;
OmpFromClause omp_from_clause =
O_FROM LPAREN l:omp_list RPAREN
{
......@@ -1533,7 +1517,7 @@ OmpFromClause omp_from_clause =
}
;
//// OmpLastPrivateClause:OmpClause ::= OmpListItem* ;
//// OmpLastPrivateClause:OmpClause ::= Variable* ;
OmpLastPrivateClause omp_lastprivate_clause =
O_LASTPRIVATE LPAREN l:omp_list RPAREN
{
......@@ -1541,7 +1525,7 @@ OmpLastPrivateClause omp_lastprivate_clause =
}
;
//// OmpLinearClause:OmpClause ::= OmpListItem* [Expr] ;
//// OmpLinearClause:OmpClause ::= Variable* [Expr] ;
OmpLinearClause omp_linear_clause =
O_LINEAR LPAREN l:omp_list e:(COLON yyValue:expr)? RPAREN
{
......@@ -1550,7 +1534,7 @@ OmpLinearClause omp_linear_clause =
}
;
//// abstract OmpMapClause:OmpClause ::= OmpListItem* ;
//// abstract OmpMapClause:OmpClause ::= Variable* ;
//// OmpMapAllocClause:OmpMapClause ;
//// OmpMapToClause:OmpMapClause ;
//// OmpMapFromClause:OmpMapClause ;
......@@ -1586,7 +1570,7 @@ OmpMergeableClause omp_mergeable_clause =
}
;
//// OmpNowaitClause:OmpClause ::= OmpListItem* ;
//// OmpNowaitClause:OmpClause ::= Variable* ;
OmpNowaitClause omp_nowait_clause =
O_NOWAIT
{
......@@ -1618,7 +1602,7 @@ OmpOrderedClause omp_ordered_clause =
}
;
//// OmpPrivateClause:OmpClause ::= OmpListItem* ;
//// OmpPrivateClause:OmpClause ::= Variable* ;
OmpPrivateClause omp_private_clause =
O_PRIVATE LPAREN l:omp_list RPAREN
{
......@@ -1645,7 +1629,7 @@ OmpProcBindClause omp_proc_bind_clause =
}
;
//// OmpReductionClause:OmpClause ::= OmpReductionIdentifier OmpListItem* ;
//// OmpReductionClause:OmpClause ::= OmpReductionIdentifier Variable* ;
OmpReductionClause omp_reduction_clause =
O_REDUCTION LPAREN i:omp_reduction_identifier COLON l:omp_list RPAREN
{
......@@ -1760,7 +1744,7 @@ OmpScheduleClause omp_schedule_clause =
;
//// OmpSharedClause:OmpClause ::= OmpListItem* ;
//// OmpSharedClause:OmpClause ::= Variable* ;
OmpSharedClause omp_shared_clause =
O_SHARED LPAREN l:omp_list RPAREN
{
......@@ -1776,7 +1760,7 @@ OmpThreadLimitClause omp_thread_limit_clause =
}
;
//// OmpToClause:OmpClause ::= OmpListItem* ;
//// OmpToClause:OmpClause ::= Variable* ;
OmpToClause omp_to_clause =
O_TO LPAREN l:omp_list RPAREN
{
......
This diff is collapsed.
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);
}
}
}
aspect Printing {
// abstract Slot ::= SlotName ;
// SlotName ::= <String> ;
// SlotExpr:Expr ::= ExprSlot ;
// ExprSlot:Slot ;
public void SlotName.prettyPrint(PrettyPrinter s) {
s.append(getString());
public void SlotExpr.prettyPrint(PrettyPrinter s) {
s.append("#");
s.append(getSlotName());
s.append("#");
}
public void Slot.prettyPrint(PrettyPrinter s) {
public void SlotIntLiteralConstant.prettyPrint(PrettyPrinter s) {
s.append("#");
getSlotName().prettyPrint(s);
s.append(getSlotName());
s.append("#");
}
public void SlotExpr.prettyPrint(PrettyPrinter s) {
getExprSlot().prettyPrint(s);
public void SlotBlock.prettyPrint(PrettyPrinter s) {
s.append("#");
s.append(getSlotName());
s.append("#");
s.lb();
}
public void SlotIntLiteralConstant.prettyPrint(PrettyPrinter s) {
getIntLiteralConstantSlot().prettyPrint(s);
public void SlotVariable.prettyPrint(PrettyPrinter s) {
s.append("#");
s.append(getSlotName());
s.append("#");
}
public void SlotVariable.prettyPrint(PrettyPrinter s) {
getVariableSlot().prettyPrint(s);
public void SlotDoConstruct.prettyPrint(PrettyPrinter s) {
s.append("#");
s.append(getSlotName());
s.append("#");
}
public void SlotExecutableConstruct.prettyPrint(PrettyPrinter s) {
getExecutableConstructSlot().prettyPrint(s);
s.append("#");
s.append(getSlotName());
s.append("#");
s.lb();
}
public void SlotExecutionPartConstruct.prettyPrint(PrettyPrinter s) {
s.append("#");
s.append(getSlotName());
s.append("#");
s.lb();
}
public void SlotForName.prettyPrint(PrettyPrinter s) {
s.append("#");
s.append(getString());
s.append("#");
}
}
\ No newline at end of file
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment