comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/codewriter/AbstractCodeWriter.java @ 11181:58f09779319c

Truffle-DSL: string line wrapping in generated code. (GRAAL-331 #resolve)
author Christian Humer <christian.humer@gmail.com>
date Mon, 29 Jul 2013 18:21:27 +0200
parents 79041ab43660
children 380e0248f873
comparison
equal deleted inserted replaced
11180:a9cb98ff8fd9 11181:58f09779319c
34 import com.oracle.truffle.dsl.processor.*; 34 import com.oracle.truffle.dsl.processor.*;
35 import com.oracle.truffle.dsl.processor.ast.*; 35 import com.oracle.truffle.dsl.processor.ast.*;
36 36
37 public abstract class AbstractCodeWriter extends CodeElementScanner<Void, Void> { 37 public abstract class AbstractCodeWriter extends CodeElementScanner<Void, Void> {
38 38
39 private static final int LINE_LENGTH = 200; 39 private static final int MAX_LINE_LENGTH = 200;
40 private static final int LINE_WRAP_INDENTS = 3; 40 private static final int LINE_WRAP_INDENTS = 3;
41 private static final String IDENT_STRING = " "; 41 private static final String IDENT_STRING = " ";
42 private static final String LN = "\n"; /* unix style */ 42 private static final String LN = "\n"; /* unix style */
43 43
44 protected Writer writer; 44 protected Writer writer;
620 private AbstractCodeWriter write(Name name) { 620 private AbstractCodeWriter write(Name name) {
621 return write(name.toString()); 621 return write(name.toString());
622 } 622 }
623 623
624 private AbstractCodeWriter write(String m) { 624 private AbstractCodeWriter write(String m) {
625 if (m.isEmpty()) {
626 return this;
627 }
625 try { 628 try {
626 lineLength += m.length(); 629 String s = m;
627 if (newLine && m != LN) { 630 lineLength += s.length();
631 if (newLine && s != LN) {
628 writeIndent(); 632 writeIndent();
629 newLine = false; 633 newLine = false;
630 } 634 }
631 if (lineLength > LINE_LENGTH && m.length() > 0) { 635 if (lineLength > MAX_LINE_LENGTH) {
632 char firstChar = m.charAt(0); 636 s = wrapLine(s);
633 if (Character.isAlphabetic(firstChar)) { 637 }
634 if (!lineWrapping) { 638 writer.write(s);
635 indent(LINE_WRAP_INDENTS);
636 }
637 lineWrapping = true;
638 lineLength = 0;
639 write(LN);
640 writeIndent();
641 }
642 }
643 writer.write(m);
644 } catch (IOException e) { 639 } catch (IOException e) {
645 throw new RuntimeException(e); 640 throw new RuntimeException(e);
646 } 641 }
647 return this; 642 return this;
648 } 643 }
649 644
645 private String wrapLine(String m) throws IOException {
646 assert !m.isEmpty();
647
648 char firstCharacter = m.charAt(0);
649 char lastCharacter = m.charAt(m.length() - 1);
650 if (firstCharacter == '\"' && lastCharacter == '\"') {
651 // string line wrapping
652 String string = m.substring(1, m.length() - 1);
653 if (string.isEmpty()) {
654 return m;
655 }
656
657 // restore original line length
658 lineLength = lineLength - m.length();
659 int size = 0;
660 for (int i = 0; i < string.length(); i += size) {
661 if (i != 0) {
662 write("+ ");
663 }
664 int nextSize = MAX_LINE_LENGTH - lineLength - 2;
665 int end = Math.min(i + nextSize, string.length());
666
667 assert lineLength + (end - i) + 2 < MAX_LINE_LENGTH;
668 write("\"" + string.substring(i, end) + "\"");
669 size = nextSize;
670 }
671
672 return "";
673 } else if (!Character.isAlphabetic(firstCharacter) && firstCharacter != '+') {
674 return m;
675 }
676
677 if (!lineWrapping) {
678 indent(LINE_WRAP_INDENTS);
679 }
680 lineWrapping = true;
681 lineLength = 0;
682 write(LN);
683 writeIndent();
684 return m;
685 }
686
650 private void writeIndent() throws IOException { 687 private void writeIndent() throws IOException {
688 lineLength += indentSize();
651 for (int i = 0; i < indent; i++) { 689 for (int i = 0; i < indent; i++) {
652 lineLength += IDENT_STRING.length();
653 writer.write(IDENT_STRING); 690 writer.write(IDENT_STRING);
654 } 691 }
692 }
693
694 private int indentSize() {
695 return IDENT_STRING.length() * indent;
655 } 696 }
656 697
657 private static class TrimTrailingSpaceWriter extends Writer { 698 private static class TrimTrailingSpaceWriter extends Writer {
658 699
659 private final Writer delegate; 700 private final Writer delegate;