2727
2828package processing .app .preproc ;
2929
30- import java .io .ByteArrayOutputStream ;
31- import java .io .FileNotFoundException ;
3230import java .io .FileOutputStream ;
3331import java .io .IOException ;
3432import java .io .OutputStreamWriter ;
3533import java .io .PrintStream ;
34+ import java .io .PrintWriter ;
3635import java .io .StringReader ;
36+ import java .io .StringWriter ;
37+ import java .io .Writer ;
3738import java .util .ArrayList ;
3839import java .util .Arrays ;
3940import java .util .List ;
4344import processing .app .antlr .PdeTokenTypes ;
4445import processing .app .debug .RunnerException ;
4546import processing .core .PApplet ;
47+ import antlr .ANTLRException ;
4648import antlr .ASTFactory ;
4749import antlr .CommonAST ;
4850import antlr .CommonASTWithHiddenTokens ;
@@ -179,11 +181,13 @@ public void setProgramType(final ProgramType programType) {
179181 this .programType = programType ;
180182 }
181183
182- public PdePreprocessor (final String sketchName ) throws IOException {
183- this .name = sketchName ;
184+ public PdePreprocessor (final String sketchName ) {
185+ this (sketchName , Preferences .getInteger ("editor.tabs.size" ));
186+ }
184187
185- final char [] indentChars = new char [Preferences
186- .getInteger ("editor.tabs.size" )];
188+ public PdePreprocessor (final String sketchName , final int tabSize ) {
189+ this .name = sketchName ;
190+ final char [] indentChars = new char [tabSize ];
187191 Arrays .fill (indentChars , ' ' );
188192 indent = new String (indentChars );
189193 }
@@ -261,9 +265,14 @@ private static void checkForUnterminatedMultilineComment(final String program)
261265 }
262266 }
263267
264- public PreprocessResult write (final PrintStream stream , String program ,
268+ public PreprocessResult write (final Writer out , String program )
269+ throws RunnerException , ANTLRException {
270+ return write (out , program , null );
271+ }
272+
273+ public PreprocessResult write (final Writer out , String program ,
265274 final String codeFolderPackages [])
266- throws FileNotFoundException , RunnerException , Exception {
275+ throws RunnerException , ANTLRException {
267276
268277 // these ones have the .* at the end, since a class name might be at the end
269278 // instead of .* which would make trouble other classes using this can lop
@@ -277,6 +286,10 @@ public PreprocessResult write(final PrintStream stream, String program,
277286 // need to reset whether or not this has a main()
278287 foundMain = false ;
279288
289+ // bug #5
290+ if (!program .endsWith ("\n " ))
291+ program += "\n " ;
292+
280293 checkForUnterminatedMultilineComment (program );
281294
282295 if (Preferences .getBoolean ("preproc.substitute_unicode" )) {
@@ -309,6 +322,7 @@ public PreprocessResult write(final PrintStream stream, String program,
309322 }
310323 }
311324
325+ final PrintWriter stream = new PrintWriter (out );
312326 final int headerOffset = writeImports (stream , programImports ,
313327 codeFolderImports );
314328 return new PreprocessResult (headerOffset + 2 , write (program , stream ),
@@ -356,8 +370,8 @@ static String substituteUnicode(String program) {
356370 * preprocesses a pde file and writes out a java file
357371 * @return the class name of the exported Java
358372 */
359- private String write (final String program , final PrintStream stream )
360- throws java . lang . Exception {
373+ private String write (final String program , final PrintWriter stream )
374+ throws RunnerException , ANTLRException {
361375 // create a lexer with the stream reader, and tell it to handle
362376 // hidden tokens (eg whitespace, comments) since we want to pass these
363377 // through so that the line numbers when the compiler reports errors
@@ -438,13 +452,13 @@ private String write(final String program, final PrintStream stream)
438452
439453 // debug
440454 if (false ) {
441- final ByteArrayOutputStream buf = new ByteArrayOutputStream ();
442- final PrintStream bufout = new PrintStream (buf );
455+ final StringWriter buf = new StringWriter ();
456+ final PrintWriter bufout = new PrintWriter (buf );
443457 writeDeclaration (bufout , className );
444458 new PdeEmitter (this , bufout ).print (rootNode );
445459 writeFooter (bufout , className );
446460 debugAST (rootNode , true );
447- System .err .println (new String ( buf .toByteArray () ));
461+ System .err .println (buf .toString ( ));
448462 }
449463
450464 writeDeclaration (stream , className );
@@ -520,7 +534,7 @@ protected void writeParseTree(String filename, AST ast) {
520534 * @param codeFolderImports
521535 * @return the header offset
522536 */
523- protected int writeImports (final PrintStream out ,
537+ protected int writeImports (final PrintWriter out ,
524538 final List <String > programImports ,
525539 final List <String > codeFolderImports ) {
526540 int count = writeImportList (out , getCoreImports ());
@@ -530,11 +544,11 @@ protected int writeImports(final PrintStream out,
530544 return count ;
531545 }
532546
533- protected int writeImportList (PrintStream out , List <String > imports ) {
547+ protected int writeImportList (PrintWriter out , List <String > imports ) {
534548 return writeImportList (out , (String []) imports .toArray (new String [0 ]));
535549 }
536550
537- protected int writeImportList (PrintStream out , String [] imports ) {
551+ protected int writeImportList (PrintWriter out , String [] imports ) {
538552 int count = 0 ;
539553 if (imports != null && imports .length != 0 ) {
540554 for (String item : imports ) {
@@ -554,7 +568,7 @@ protected int writeImportList(PrintStream out, String[] imports) {
554568 * @param exporting Is this being exported from PDE?
555569 * @param className Name of the class being created.
556570 */
557- protected void writeDeclaration (PrintStream out , String className ) {
571+ protected void writeDeclaration (PrintWriter out , String className ) {
558572 if (programType == ProgramType .JAVA ) {
559573 // Print two blank lines so that the offset doesn't change
560574 out .println ();
@@ -576,7 +590,7 @@ protected void writeDeclaration(PrintStream out, String className) {
576590 *
577591 * @param out PrintStream to write it to.
578592 */
579- protected void writeFooter (PrintStream out , String className ) {
593+ protected void writeFooter (PrintWriter out , String className ) {
580594 if (programType == ProgramType .STATIC ) {
581595 // close off draw() definition
582596 out .println (indent + "noLoop();" );
0 commit comments