use of java.io.StringReader in project j2objc by google.
the class SourceBuilder method reindent.
/**
* Fix line indention, based on brace count.
*/
public String reindent(String code) {
try {
// Remove indention from each line.
StringBuffer sb = new StringBuffer();
LineReader lr = new LineReader(new StringReader(code));
String line = lr.readLine();
while (line != null) {
sb.append(line.trim());
line = lr.readLine();
if (line != null) {
sb.append('\n');
}
}
String strippedCode = sb.toString();
// Now indent it again.
int indent = indention * DEFAULT_INDENTION;
// reset buffer
sb.setLength(0);
lr = new LineReader(new StringReader(strippedCode));
line = lr.readLine();
while (line != null) {
if (line.startsWith("}")) {
indent -= DEFAULT_INDENTION;
}
if (!line.startsWith("#line")) {
sb.append(pad(indent));
}
sb.append(line);
if (line.endsWith("{")) {
indent += DEFAULT_INDENTION;
}
line = lr.readLine();
if (line != null) {
sb.append('\n');
}
}
return sb.toString();
} catch (IOException e) {
// Should never happen with string readers.
throw new AssertionError(e);
}
}
use of java.io.StringReader in project groovy-core by groovy.
the class SourceBufferTest method getSourceBuffer.
private SourceBuffer getSourceBuffer(String text) throws Exception {
SourceBuffer buffer = new SourceBuffer();
Reader reader = new UnicodeEscapingReader(new StringReader(text), buffer);
while (reader.read() != -1) {
// empty loop
// - read all characters till the end of the reader
// UnicodeEscapingReader has side effects of
// filling the buffer
}
return buffer;
}
use of java.io.StringReader in project groovy-core by groovy.
the class CompositeVisitorTest method assertCompositeTransparency.
private void assertCompositeTransparency(String input) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GroovyRecognizer parser;
SourceBuffer sourceBuffer = new SourceBuffer();
UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new StringReader(input), sourceBuffer);
GroovyLexer lexer = new GroovyLexer(unicodeReader);
unicodeReader.setLexer(lexer);
parser = GroovyRecognizer.make(lexer);
parser.setSourceBuffer(sourceBuffer);
String[] tokenNames = parser.getTokenNames();
parser.compilationUnit();
AST ast = parser.getAST();
// determine direct result
Visitor directVisitor = new SourcePrinter(new PrintStream(baos), tokenNames, false);
AntlrASTProcessor traverser = new SourceCodeTraversal(directVisitor);
traverser.process(ast);
String directResult = new String(baos.toByteArray());
// determine composite result
baos.reset();
List wrappedVisitors = new ArrayList();
wrappedVisitors.add(directVisitor);
Visitor compositeVisitor = new CompositeVisitor(wrappedVisitors);
traverser = new SourceCodeTraversal(compositeVisitor);
traverser.process(ast);
String compositeResult = new String(baos.toByteArray());
assertEquals(directResult, compositeResult);
}
use of java.io.StringReader in project groovy-core by groovy.
the class TraversalTestHelper method traverse.
// todo - the visitor doesn't always take PrintStreams as constructor params! Could be a more reusable implementation than this...
public String traverse(String input, Class visitorClass, Boolean extraParam) throws Exception {
if (!Visitor.class.isAssignableFrom(visitorClass)) {
throw new RuntimeException("Invalid class for traversal: " + visitorClass.getName());
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GroovyRecognizer parser;
SourceBuffer sourceBuffer = new SourceBuffer();
UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new StringReader(input), sourceBuffer);
GroovyLexer lexer = new GroovyLexer(unicodeReader);
unicodeReader.setLexer(lexer);
parser = GroovyRecognizer.make(lexer);
parser.setSourceBuffer(sourceBuffer);
String[] tokenNames = parser.getTokenNames();
parser.compilationUnit();
AST ast = parser.getAST();
Class[] paramTypes;
Object[] params;
if (extraParam == null) {
paramTypes = new Class[] { PrintStream.class, String[].class };
params = new Object[] { new PrintStream(baos), tokenNames };
} else {
paramTypes = new Class[] { PrintStream.class, String[].class, Boolean.TYPE };
params = new Object[] { new PrintStream(baos), tokenNames, extraParam };
}
Constructor constructor = visitorClass.getConstructor(paramTypes);
Visitor visitor = (Visitor) constructor.newInstance(params);
AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
traverser.process(ast);
return new String(baos.toByteArray());
}
use of java.io.StringReader in project groovy-core by groovy.
the class AnnotationSourceParsingTest method testMultiLineAttributes.
public void testMultiLineAttributes() {
StringReader reader = new StringReader("class OtherSection\n" + "{\n" + " @CollectionOfElements\n" + " @JoinTable\n" + " (\n" + " table=@Table(name=\"gaga\"),\n" + " joinColumns = @JoinColumn(name=\"BoyId\")\n" + " )\n" + " @Column(name=\"favoritepoupon\", \n" + "nullable=false)\n" + " Set<String> questions = new HashSet<String> ()\n\n" + "}");
parse("testMultiLineAttributes", reader);
}
Aggregations