Mercurial > hg > truffle
changeset 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 | a9cb98ff8fd9 |
children | fa65fc74eb76 |
files | graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/codewriter/AbstractCodeWriter.java |
diffstat | 1 files changed, 57 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/codewriter/AbstractCodeWriter.java Mon Jul 29 16:50:10 2013 +0200 +++ b/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/codewriter/AbstractCodeWriter.java Mon Jul 29 18:21:27 2013 +0200 @@ -36,7 +36,7 @@ public abstract class AbstractCodeWriter extends CodeElementScanner<Void, Void> { - private static final int LINE_LENGTH = 200; + private static final int MAX_LINE_LENGTH = 200; private static final int LINE_WRAP_INDENTS = 3; private static final String IDENT_STRING = " "; private static final String LN = "\n"; /* unix style */ @@ -622,38 +622,79 @@ } private AbstractCodeWriter write(String m) { + if (m.isEmpty()) { + return this; + } try { - lineLength += m.length(); - if (newLine && m != LN) { + String s = m; + lineLength += s.length(); + if (newLine && s != LN) { writeIndent(); newLine = false; } - if (lineLength > LINE_LENGTH && m.length() > 0) { - char firstChar = m.charAt(0); - if (Character.isAlphabetic(firstChar)) { - if (!lineWrapping) { - indent(LINE_WRAP_INDENTS); - } - lineWrapping = true; - lineLength = 0; - write(LN); - writeIndent(); - } + if (lineLength > MAX_LINE_LENGTH) { + s = wrapLine(s); } - writer.write(m); + writer.write(s); } catch (IOException e) { throw new RuntimeException(e); } return this; } + private String wrapLine(String m) throws IOException { + assert !m.isEmpty(); + + char firstCharacter = m.charAt(0); + char lastCharacter = m.charAt(m.length() - 1); + if (firstCharacter == '\"' && lastCharacter == '\"') { + // string line wrapping + String string = m.substring(1, m.length() - 1); + if (string.isEmpty()) { + return m; + } + + // restore original line length + lineLength = lineLength - m.length(); + int size = 0; + for (int i = 0; i < string.length(); i += size) { + if (i != 0) { + write("+ "); + } + int nextSize = MAX_LINE_LENGTH - lineLength - 2; + int end = Math.min(i + nextSize, string.length()); + + assert lineLength + (end - i) + 2 < MAX_LINE_LENGTH; + write("\"" + string.substring(i, end) + "\""); + size = nextSize; + } + + return ""; + } else if (!Character.isAlphabetic(firstCharacter) && firstCharacter != '+') { + return m; + } + + if (!lineWrapping) { + indent(LINE_WRAP_INDENTS); + } + lineWrapping = true; + lineLength = 0; + write(LN); + writeIndent(); + return m; + } + private void writeIndent() throws IOException { + lineLength += indentSize(); for (int i = 0; i < indent; i++) { - lineLength += IDENT_STRING.length(); writer.write(IDENT_STRING); } } + private int indentSize() { + return IDENT_STRING.length() * indent; + } + private static class TrimTrailingSpaceWriter extends Writer { private final Writer delegate;