mirror of
https://github.com/arduino/Arduino.git
synced 2025-03-14 11:29:26 +01:00
Refactored PdePreprocessor class
This commit is contained in:
parent
8a8bb44745
commit
316b871829
@ -1339,7 +1339,6 @@ public class Sketch {
|
|||||||
// make sure the user didn't hide the sketch folder
|
// make sure the user didn't hide the sketch folder
|
||||||
ensureExistence();
|
ensureExistence();
|
||||||
|
|
||||||
String[] codeFolderPackages = null;
|
|
||||||
classPath = buildPath;
|
classPath = buildPath;
|
||||||
|
|
||||||
// // figure out the contents of the code folder to see if there
|
// // figure out the contents of the code folder to see if there
|
||||||
@ -1381,12 +1380,8 @@ public class Sketch {
|
|||||||
// Note that the headerOffset isn't applied until compile and run, because
|
// Note that the headerOffset isn't applied until compile and run, because
|
||||||
// it only applies to the code after it's been written to the .java file.
|
// it only applies to the code after it's been written to the .java file.
|
||||||
int headerOffset = 0;
|
int headerOffset = 0;
|
||||||
//PdePreprocessor preprocessor = new PdePreprocessor();
|
|
||||||
try {
|
try {
|
||||||
headerOffset = preprocessor.writePrefix(bigCode.toString(),
|
headerOffset = preprocessor.writePrefix(bigCode.toString());
|
||||||
buildPath,
|
|
||||||
name,
|
|
||||||
codeFolderPackages);
|
|
||||||
} catch (FileNotFoundException fnfe) {
|
} catch (FileNotFoundException fnfe) {
|
||||||
fnfe.printStackTrace();
|
fnfe.printStackTrace();
|
||||||
String msg = _("Build folder disappeared or could not be written");
|
String msg = _("Build folder disappeared or could not be written");
|
||||||
@ -1399,24 +1394,14 @@ public class Sketch {
|
|||||||
String primaryClassName = null;
|
String primaryClassName = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// if (i != 0) preproc will fail if a pde file is not
|
// Output file
|
||||||
// java mode, since that's required
|
File streamFile = new File(buildPath, name + ".cpp");
|
||||||
String className = preprocessor.write();
|
FileOutputStream outputStream = new FileOutputStream(streamFile);
|
||||||
|
preprocessor.write(outputStream);
|
||||||
if (className == null) {
|
outputStream.close();
|
||||||
throw new RunnerException(_("Could not find main class"));
|
|
||||||
// this situation might be perfectly fine,
|
|
||||||
// (i.e. if the file is empty)
|
|
||||||
//System.out.println("No class found in " + code[i].name);
|
|
||||||
//System.out.println("(any code in that file will be ignored)");
|
|
||||||
//System.out.println();
|
|
||||||
|
|
||||||
// } else {
|
|
||||||
// code[0].setPreprocName(className + ".java");
|
|
||||||
}
|
|
||||||
|
|
||||||
// store this for the compiler and the runtime
|
// store this for the compiler and the runtime
|
||||||
primaryClassName = className + ".cpp";
|
primaryClassName = name + ".cpp";
|
||||||
|
|
||||||
} catch (FileNotFoundException fnfe) {
|
} catch (FileNotFoundException fnfe) {
|
||||||
fnfe.printStackTrace();
|
fnfe.printStackTrace();
|
||||||
@ -1668,57 +1653,6 @@ public class Sketch {
|
|||||||
return success ? suggestedClassName : null;
|
return success ? suggestedClassName : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Replace all commented portions of a given String as spaces.
|
|
||||||
* Utility function used here and in the preprocessor.
|
|
||||||
*/
|
|
||||||
static public String scrubComments(String what) {
|
|
||||||
char p[] = what.toCharArray();
|
|
||||||
|
|
||||||
int index = 0;
|
|
||||||
while (index < p.length) {
|
|
||||||
// for any double slash comments, ignore until the end of the line
|
|
||||||
if ((p[index] == '/') &&
|
|
||||||
(index < p.length - 1) &&
|
|
||||||
(p[index+1] == '/')) {
|
|
||||||
p[index++] = ' ';
|
|
||||||
p[index++] = ' ';
|
|
||||||
while ((index < p.length) &&
|
|
||||||
(p[index] != '\n')) {
|
|
||||||
p[index++] = ' ';
|
|
||||||
}
|
|
||||||
|
|
||||||
// check to see if this is the start of a new multiline comment.
|
|
||||||
// if it is, then make sure it's actually terminated somewhere.
|
|
||||||
} else if ((p[index] == '/') &&
|
|
||||||
(index < p.length - 1) &&
|
|
||||||
(p[index+1] == '*')) {
|
|
||||||
p[index++] = ' ';
|
|
||||||
p[index++] = ' ';
|
|
||||||
boolean endOfRainbow = false;
|
|
||||||
while (index < p.length - 1) {
|
|
||||||
if ((p[index] == '*') && (p[index+1] == '/')) {
|
|
||||||
p[index++] = ' ';
|
|
||||||
p[index++] = ' ';
|
|
||||||
endOfRainbow = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// continue blanking this area
|
|
||||||
p[index++] = ' ';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!endOfRainbow) {
|
|
||||||
throw new RuntimeException(_("Missing the */ from the end of a " +
|
|
||||||
"/* comment */"));
|
|
||||||
}
|
|
||||||
} else { // any old character, move along
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new String(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public boolean exportApplicationPrompt() throws IOException, RunnerException {
|
public boolean exportApplicationPrompt() throws IOException, RunnerException {
|
||||||
return false;
|
return false;
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
package processing.app.preproc;
|
package processing.app.preproc;
|
||||||
|
|
||||||
|
import static processing.app.I18n._;
|
||||||
import processing.app.*;
|
import processing.app.*;
|
||||||
import processing.core.*;
|
import processing.core.*;
|
||||||
|
|
||||||
@ -61,38 +62,22 @@ public class PdePreprocessor {
|
|||||||
// than the others, since the imports are auto-generated.
|
// than the others, since the imports are auto-generated.
|
||||||
List<String> codeFolderImports;
|
List<String> codeFolderImports;
|
||||||
|
|
||||||
String indent;
|
|
||||||
|
|
||||||
PrintStream stream;
|
|
||||||
String program;
|
String program;
|
||||||
String buildPath;
|
|
||||||
// starts as sketch name, ends as main class name
|
|
||||||
String name;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup a new preprocessor.
|
* Setup a new preprocessor.
|
||||||
*/
|
*/
|
||||||
public PdePreprocessor() {
|
public PdePreprocessor() {
|
||||||
int tabSize = Preferences.getInteger("editor.tabs.size");
|
|
||||||
char[] indentChars = new char[tabSize];
|
|
||||||
Arrays.fill(indentChars, ' ');
|
|
||||||
indent = new String(indentChars);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes out the head of the c++ code generated for a sketch.
|
* Writes out the head of the c++ code generated for a sketch.
|
||||||
* Called from processing.app.Sketch.
|
* Called from processing.app.Sketch.
|
||||||
* @param program the concatenated code from all tabs containing pde-files
|
* @param program the concatenated code from all tabs containing pde-files
|
||||||
* @param buildPath the path into which the processed pde-code is to be written
|
|
||||||
* @param name the name of the sketch
|
|
||||||
* @param codeFolderPackages unused param (leftover from processing)
|
|
||||||
*/
|
*/
|
||||||
public int writePrefix(String program, String buildPath,
|
public int writePrefix(String program)
|
||||||
String sketchName, String codeFolderPackages[]) throws FileNotFoundException {
|
throws FileNotFoundException {
|
||||||
this.buildPath = buildPath;
|
|
||||||
this.name = sketchName;
|
|
||||||
|
|
||||||
// if the program ends with no CR or LF an OutOfMemoryError will happen.
|
// if the program ends with no CR or LF an OutOfMemoryError will happen.
|
||||||
// not gonna track down the bug now, so here's a hack for it:
|
// not gonna track down the bug now, so here's a hack for it:
|
||||||
// http://dev.processing.org/bugs/show_bug.cgi?id=5
|
// http://dev.processing.org/bugs/show_bug.cgi?id=5
|
||||||
@ -102,7 +87,7 @@ public class PdePreprocessor {
|
|||||||
// an OutOfMemoryError or NullPointerException will happen.
|
// an OutOfMemoryError or NullPointerException will happen.
|
||||||
// again, not gonna bother tracking this down, but here's a hack.
|
// again, not gonna bother tracking this down, but here's a hack.
|
||||||
// http://dev.processing.org/bugs/show_bug.cgi?id=16
|
// http://dev.processing.org/bugs/show_bug.cgi?id=16
|
||||||
Sketch.scrubComments(program);
|
scrubComments(program);
|
||||||
// If there are errors, an exception is thrown and this fxn exits.
|
// If there are errors, an exception is thrown and this fxn exits.
|
||||||
|
|
||||||
if (Preferences.getBoolean("preproc.substitute_unicode")) {
|
if (Preferences.getBoolean("preproc.substitute_unicode")) {
|
||||||
@ -134,10 +119,6 @@ public class PdePreprocessor {
|
|||||||
// do this after the program gets re-combobulated
|
// do this after the program gets re-combobulated
|
||||||
this.program = program;
|
this.program = program;
|
||||||
|
|
||||||
// output the code
|
|
||||||
File streamFile = new File(buildPath, name + ".cpp");
|
|
||||||
stream = new PrintStream(new FileOutputStream(streamFile));
|
|
||||||
|
|
||||||
return headerCount + prototypeCount;
|
return headerCount + prototypeCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,17 +162,16 @@ public class PdePreprocessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* preprocesses a pde file and writes out a java file
|
* preprocesses a pde file and writes out a cpp file into the specified
|
||||||
* @return the classname of the exported Java
|
* OutputStream
|
||||||
|
*
|
||||||
|
* @param output
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
//public String write(String program, String buildPath, String name,
|
public void write(OutputStream output) throws Exception {
|
||||||
// String extraImports[]) throws java.lang.Exception {
|
PrintStream stream = new PrintStream(output);
|
||||||
public String write() throws java.lang.Exception {
|
|
||||||
writeProgram(stream, program, prototypes);
|
writeProgram(stream, program, prototypes);
|
||||||
writeFooter(stream);
|
writeFooter(stream);
|
||||||
stream.close();
|
|
||||||
|
|
||||||
return name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the pde program to the cpp file
|
// Write the pde program to the cpp file
|
||||||
@ -344,4 +324,56 @@ public class PdePreprocessor {
|
|||||||
|
|
||||||
return functionMatches;
|
return functionMatches;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace all commented portions of a given String as spaces.
|
||||||
|
* Utility function used here and in the preprocessor.
|
||||||
|
*/
|
||||||
|
static public String scrubComments(String what) {
|
||||||
|
char p[] = what.toCharArray();
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
while (index < p.length) {
|
||||||
|
// for any double slash comments, ignore until the end of the line
|
||||||
|
if ((p[index] == '/') &&
|
||||||
|
(index < p.length - 1) &&
|
||||||
|
(p[index+1] == '/')) {
|
||||||
|
p[index++] = ' ';
|
||||||
|
p[index++] = ' ';
|
||||||
|
while ((index < p.length) &&
|
||||||
|
(p[index] != '\n')) {
|
||||||
|
p[index++] = ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
// check to see if this is the start of a new multiline comment.
|
||||||
|
// if it is, then make sure it's actually terminated somewhere.
|
||||||
|
} else if ((p[index] == '/') &&
|
||||||
|
(index < p.length - 1) &&
|
||||||
|
(p[index+1] == '*')) {
|
||||||
|
p[index++] = ' ';
|
||||||
|
p[index++] = ' ';
|
||||||
|
boolean endOfRainbow = false;
|
||||||
|
while (index < p.length - 1) {
|
||||||
|
if ((p[index] == '*') && (p[index+1] == '/')) {
|
||||||
|
p[index++] = ' ';
|
||||||
|
p[index++] = ' ';
|
||||||
|
endOfRainbow = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// continue blanking this area
|
||||||
|
p[index++] = ' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!endOfRainbow) {
|
||||||
|
throw new RuntimeException(_("Missing the */ from the end of a " +
|
||||||
|
"/* comment */"));
|
||||||
|
}
|
||||||
|
} else { // any old character, move along
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new String(p);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user