1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-20 21:01:25 +03:00

Reworked build system: makefiles replaced with in-program logic; core replaced with targets; preproc/ replaced with Wiring's; now prepend "#include "WProgram.h" instead of wiringlite.inc; new entries in preferences.txt; bundled Wiring libs.

This commit is contained in:
David A. Mellis
2005-09-25 14:11:32 +00:00
parent 10b3f4fe08
commit 7fbb37cbe0
98 changed files with 44948 additions and 18090 deletions

View File

@ -1,11 +1,12 @@
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Compiler - default compiler class that connects to the external compiler
Compiler - default compiler class that connects to avr-gcc
Part of the Arduino project - http://arduino.berlios.de/
Part of the Arduino project - http://arduino.berlios.de
Copyright (c) 2004-05 Hernando Barragan
Derived from the Processing project - http://processing.org
Copyleft 2005 Massimo Banzi (arduino modifications)
Processing version
Copyright (c) 2004-05 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
@ -22,6 +23,8 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id:$
*/
package processing.app;
@ -32,8 +35,10 @@ import java.util.zip.*;
import javax.swing.*;
public class Compiler implements MessageConsumer {
static final String BUGS_URL = "http://arduino.berlios.de";
static final String SUPER_BADNESS = "Compiler error, please submit this code to " + BUGS_URL;
static final String BUGS_URL =
"https://developer.berlios.de/bugs/?group_id=3590";
static final String SUPER_BADNESS =
"Compiler error, please submit this code to " + BUGS_URL;
Sketch sketch;
String buildPath;
@ -57,11 +62,12 @@ public class Compiler implements MessageConsumer {
public boolean compile(PrintStream leechErr) {
*/
public Compiler() { } // null constructor
public Compiler() { } // consider this a warning, you werkin soon.
public boolean compile(Sketch sketch, String buildPath, Target target)
throws RunnerException {
public boolean compile(Sketch sketch, String buildPath)
throws RunnerException {
this.sketch = sketch;
this.buildPath = buildPath;
@ -69,46 +75,402 @@ public class Compiler implements MessageConsumer {
MessageStream pms = new MessageStream(this);
String userdir = System.getProperty("user.dir") + File.separator;
System.out.println("Compiling Arduino program");
Process process;
String commandLine = "";
//TODO test this in windows
// FIXME: this is really nasty, it seems that MACOS is making the
// compilation inside the lib folder, while windows is doing it
// inside the work folder ... why why why --DojoDave
if (Base.isWindows()) {
commandLine = userdir + "tools\\gnumake.exe -C " + userdir + ". compile";
} else if (Base.isMacOS()) {
commandLine = userdir + "tools/gnumake -C " + userdir + "lib compile";
String baseCommandCompiler[] = new String[] {
((!Base.isMacOS()) ? "tools/avr/bin/avr-gcc" :
userdir + "tools/avr/bin/avr-gcc"),
"-c", // compile, don't link
"-g", // include debugging info (so errors include line numbers)
"-Os", // optimize for size
"-I" + target.getPath(),
"-w", // surpress all warnings
"-mmcu=" + Preferences.get("build.mcu"),
"-DF_CPU=" + Preferences.get("build.f_cpu"),
" ",
" "
};
String baseCommandCompilerCPP[] = new String[] {
((!Base.isMacOS()) ? "tools/avr/bin/avr-g++" :
userdir + "tools/avr/bin/avr-g++"),
"-c", // compile, don't link
"-g", // include debugging info (so errors include line numbers)
"-Os", // optimize for size
"-I" + target.getPath(),
"-w", // surpress all warnings
"-fno-exceptions",
"-mmcu=" + Preferences.get("build.mcu"),
"-DF_CPU=" + Preferences.get("build.f_cpu"),
" ",
" "
};
String baseCommandLinker[] = new String[] {
((!Base.isMacOS()) ? "tools/avr/bin/avr-gcc" :
userdir + "tools/avr/bin/avr-gcc"),
" ",
"-mmcu=" + Preferences.get("build.mcu"),
"-o",
" ",
// ((!Base.isMacOS()) ? "" : userdir) + "lib/uart.o",
// ((!Base.isMacOS()) ? "" : userdir) + "lib/buffer.o",
// ((!Base.isMacOS()) ? "" : userdir) + "lib/timer.o",
// ((!Base.isMacOS()) ? "" : userdir) + "lib/wiring.o",
// ((!Base.isMacOS()) ? "" : userdir) + "lib/pins_arduino.o",
//((!Base.isMacOS()) ? "lib/WApplet.o" :
//userdir + "lib/WApplet.o"),
//((!Base.isMacOS()) ? "lib/WSerial.o" :
//userdir + "lib/WSerial.o"),
//((!Base.isMacOS()) ? "lib/WTimer.o" :
//userdir + "lib/WTimer.o"),
//((!Base.isMacOS()) ? "lib/Servo.o" :
//userdir + "lib/Servo.o"),
////((!Base.isMacOS()) ? "lib/Wire.o" :
//// userdir + "lib/Wire.o"),
////((!Base.isMacOS()) ? "lib/WServo.o" :
//// userdir + "lib/WServo.o"),
//((!Base.isMacOS()) ? "lib/WDisplay.o" :
//userdir + "lib/WDisplay.o"),
//((!Base.isMacOS()) ? "lib/WEncoder.o" :
//userdir + "lib/WEncoder.o"),
//((!Base.isMacOS()) ? "lib/WInterrupts.o" :
//userdir + "lib/WInterrupts.o"),
//((!Base.isMacOS()) ? "lib/WCounter.o" :
//userdir + "lib/WCounter.o"),
//((!Base.isMacOS()) ? "tools/avr/avr/lib/libm.a" :
//userdir + "tools/avr/avr/lib/libm.a")
};
String baseCommandObjcopy[] = new String[] {
((!Base.isMacOS()) ? "tools/avr/bin/avr-objcopy" :
userdir + "tools/avr/bin/avr-objcopy"),
"-O",
" ",
"-R",
" ",
" ",
" "
};
/*String baseCommand[] = new String[] {
// user.dir is folder containing P5 (and therefore jikes)
// macosx needs the extra path info. linux doesn't like it, though
// windows doesn't seem to care. write once, headache anywhere.
((!Base.isMacOS()) ? "jikes" :
System.getProperty("user.dir") + File.separator + "jikes"),
// this doesn't help much.. also java 1.4 seems to not support
// -source 1.1 for javac, and jikes seems to also have dropped it.
// for versions of jikes that don't complain, "final int" inside
// a function doesn't throw an error, so it could just be a
// ms jvm error that this sort of thing doesn't work. blech.
//"-source",
//"1.1",
// necessary to make output classes compatible with 1.1
// i.e. so that exported applets can work with ms jvm on the web
"-target",
Preferences.get("preproc.jdk_version"), //"1.1",
// let the incompatability headache begin
// used when run without a vm ("expert" mode)
"-bootclasspath",
calcBootClassPath(),
// needed for macosx so that the classpath is set properly
// also for windows because qtjava will most likely be here
// and for linux, it just doesn't hurt
"-classpath",
sketch.classPath, //calcClassPath(includeFolder),
"-nowarn", // we're not currently interested in warnings
"+E", // output errors in machine-parsable format
"-d", buildPath // output the classes in the buildPath
//buildPath + File.separator + className + ".java" // file to compile
};*/
// make list of code files that need to be compiled and the object files
// that they will be compiled to (includes code from the sketch and the
// library for the target platform)
String sourceNames[] = new String[sketch.codeCount + target.getSourceFilenames().size()];
String sourceNamesCPP[] = new String[sketch.codeCount + target.getSourceFilenames().size()];
String objectNames[] = new String[sketch.codeCount + target.getSourceFilenames().size()];
String objectNamesCPP[] = new String[sketch.codeCount + target.getSourceFilenames().size()];
int fileCount = 0;
int fileCountCPP = 0;
for (int i = 0; i < sketch.codeCount; i++) {
if (sketch.code[i].preprocName != null) {
if (sketch.code[i].preprocName.endsWith(".c")) {
sourceNames[fileCount] = buildPath + File.separator + sketch.code[i].preprocName;
objectNames[fileCount++] = buildPath + File.separator + sketch.code[i].preprocName + ".o";
} else if (sketch.code[i].preprocName.endsWith(".cpp")) {
sourceNamesCPP[fileCountCPP] = buildPath + File.separator + sketch.code[i].preprocName;
objectNamesCPP[fileCountCPP++] = buildPath + File.separator + sketch.code[i].preprocName + ".o";
}
}
}
int result = 0;
for (Iterator iter = target.getSourceFilenames().iterator(); iter.hasNext(); ) {
String filename = (String) iter.next();
if (filename != null) {
if (filename.endsWith(".c")) {
sourceNames[fileCount] = target.getPath() + File.separator + filename;
objectNames[fileCount++] = buildPath + File.separator + filename + ".o";
} else if (filename.endsWith(".cpp")) {
sourceNamesCPP[fileCountCPP] = target.getPath() + File.separator + filename;
objectNamesCPP[fileCountCPP++] = buildPath + File.separator + filename + ".o";
}
}
}
/*
String commandCompiler[] = new String[baseCommandCompiler.length + preprocCount];
System.arraycopy(baseCommandCompiler, 0, commandCompiler, 0, baseCommandCompiler.length);
// append each of the files to the command string
for (int i = 0; i < preprocCount; i++) {
commandCompiler[baseCommandCompiler.length + i] =
buildPath + File.separator + preprocNames[i];
}
String commandCompilerCPP[] = new String[baseCommandCompilerCPP.length + preprocCountCPP];
System.arraycopy(baseCommandCompilerCPP, 0, commandCompilerCPP, 0, baseCommandCompilerCPP.length);
for (int i = 0; i < preprocCountCPP; i++) {
commandCompilerCPP[baseCommandCompilerCPP.length + i] =
buildPath + File.separator + preprocNamesCPP[i];
}
*/
//PApplet.printarr(command);
baseCommandLinker[1] = "-Os -Wl,-u,vfprintf -lprintf_flt -lm,-Map=" +
((!Base.isMacOS()) ? buildPath : userdir + buildPath)
+ File.separator + sketch.name + ".map,--cref";
baseCommandLinker[4] = ((!Base.isMacOS()) ? buildPath
: buildPath) + File.separator + sketch.name + ".elf";
String commandLinker[] = new String[baseCommandLinker.length + fileCount +
fileCountCPP + target.getObjectFilenames().size()];
System.arraycopy(baseCommandLinker, 0, commandLinker, 0, baseCommandLinker.length);
int idx = 0;
for(int i = 0; i < fileCount; i++, idx++) {
commandLinker[baseCommandLinker.length + idx] = objectNames[i];
}
for(int i = 0; i < fileCountCPP; i++, idx++) {
commandLinker[baseCommandLinker.length + idx] = objectNamesCPP[i];
}
for(Iterator iter = target.getObjectFilenames().iterator(); iter.hasNext(); idx++) {
commandLinker[baseCommandLinker.length + idx] = target.getPath() + File.separator + iter.next(); //already ends in ".o"
}
/*String command[] = new String[baseCommand.length + preprocCount];
System.arraycopy(baseCommand, 0, command, 0, baseCommand.length);
// append each of the files to the command string
for (int i = 0; i < preprocCount; i++) {
command[baseCommand.length + i] =
buildPath + File.separator + preprocNames[i];
}
//PApplet.printarr(command);
*/
/*
String command[] = new String[baseCommand.length + sketch.codeCount];
System.arraycopy(baseCommand, 0, command, 0, baseCommand.length);
// append each of the files to the command string
for (int i = 0; i < sketch.codeCount; i++) {
command[baseCommand.length + i] =
buildPath + File.separator + sketch.code[i].preprocName;
}
*/
//for (int i = 0; i < command.length; i++) {
//System.out.println("cmd " + i + " " + command[i]);
//}
firstErrorFound = false; // haven't found any errors yet
secondErrorFound = false;
int result = 0; // pre-initialized to quiet a bogus warning from jikes
try {
// System.out.println(commandLine);
process = Runtime.getRuntime().exec(commandLine);
// execute the compiler, and create threads to deal
// with the input and error streams
//
Process process;
boolean compiling = true;
for(int i = 0; i < fileCount; i++) {
baseCommandCompiler[8] = sourceNames[i];
baseCommandCompiler[9] = "-o"+ objectNames[i];
//System.arraycopy(baseCommandCompiler.length
//for(int j = 0; j < baseCommandCompiler.length; j++) {
// System.out.println(baseCommandCompiler[j]);
//}
process = Runtime.getRuntime().exec(baseCommandCompiler);
new MessageSiphon(process.getInputStream(), this);
new MessageSiphon(process.getErrorStream(), this);
// wait for the process to finish. if interrupted
// before waitFor returns, continue waiting
//
compiling = true;
while (compiling) {
try {
result = process.waitFor();
//System.out.println("result is " + result);
compiling = false;
} catch (InterruptedException ignored) { }
}
if (exception != null) {
exception.hideStackTrace = true;
throw exception;
}
if(result!=0)
return false;
}
for(int i = 0; i < fileCountCPP; i++) {
baseCommandCompilerCPP[9] = sourceNamesCPP[i];
baseCommandCompilerCPP[10] = "-o"+ objectNamesCPP[i];
//for(int j = 0; j < baseCommandCompilerCPP.length; j++) {
// System.out.println(baseCommandCompilerCPP[j]);
//}
process = Runtime.getRuntime().exec(baseCommandCompilerCPP);
new MessageSiphon(process.getInputStream(), this);
new MessageSiphon(process.getErrorStream(), this);
// wait for the process to finish. if interrupted
// before waitFor returns, continue waiting
//
compiling = true;
while (compiling) {
try {
result = process.waitFor();
//System.out.println("result is " + result);
compiling = false;
} catch (InterruptedException ignored) { }
}
if (exception != null) {
exception.hideStackTrace = true;
throw exception;
}
if(result!=0)
return false;
}
//for(int j = 0; j < commandLinker.length; j++) {
// System.out.println(commandLinker[j]);
//}
process = Runtime.getRuntime().exec(commandLinker);
new MessageSiphon(process.getInputStream(), this);
new MessageSiphon(process.getErrorStream(), this);
compiling = true;
while(compiling) {
try {
result = process.waitFor();
compiling = false;
} catch (InterruptedException intExc) { }
}
if (exception != null) {
exception.hideStackTrace = true;
throw exception;
}
if(result!=0)
return false;
/*for(int j = 0; j < baseCommandObjcopy.length; j++) {
System.out.println(baseCommandObjcopy[j]);
}*/
baseCommandObjcopy[2] = "srec";
baseCommandObjcopy[4] = ".eeprom";
baseCommandObjcopy[5] = buildPath + File.separator + sketch.name + ".elf";
baseCommandObjcopy[6] = buildPath + File.separator + sketch.name + ".rom";
process = Runtime.getRuntime().exec(baseCommandObjcopy);
new MessageSiphon(process.getInputStream(), this);
new MessageSiphon(process.getErrorStream(), this);
compiling = true;
while(compiling) {
try {
result = process.waitFor();
compiling = false;
} catch (InterruptedException intExc) { }
}
if (exception != null) {
exception.hideStackTrace = true;
throw exception;
}
if(result!=0)
return false;
baseCommandObjcopy[2] = "ihex";
baseCommandObjcopy[4] = ".flash";
baseCommandObjcopy[5] = buildPath + File.separator + sketch.name + ".elf";
baseCommandObjcopy[6] = buildPath + File.separator + sketch.name + ".hex";
process = Runtime.getRuntime().exec(baseCommandObjcopy);
new MessageSiphon(process.getInputStream(), this);
new MessageSiphon(process.getErrorStream(), this);
compiling = true;
while(compiling) {
try {
result = process.waitFor();
compiling = false;
} catch (InterruptedException intExc) { }
}
if (exception != null) {
exception.hideStackTrace = true;
throw exception;
}
if(result!=0)
return false;
/*Process process = Runtime.getRuntime().exec(command);
new MessageSiphon(process.getInputStream(), this);
new MessageSiphon(process.getErrorStream(), this);
// wait for the process to finish. if interrupted
// before waitFor returns, continue waiting
//
boolean compiling = true;
while (compiling) {
try {
result = process.waitFor();
//System.out.println("result is " + result);
compiling = false;
} catch (InterruptedException ignored) { }
}
}*/
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error: GNUMake probably couldn't be found");
result = 99;
String msg = e.getMessage();
if ((msg != null) && (msg.indexOf("avr-gcc: not found") != -1)) {
//System.err.println("jikes is missing");
Base.showWarning("Compiler error",
"Could not find the compiler.\n" +
"avr-gcc is missing from your PATH,\n" +
"see readme.txt for help.", null);
return false;
} else {
e.printStackTrace();
result = -1;
}
}
if(0 == result){
System.out.println("Arduino Compilation Successful");
}else{
System.out.println("Arduino Compilation Unsuccessful (error: " + result + ")");
// an error was queued up by message(), barf this back to build()
// which will barf it back to Editor. if you're having trouble
// discerning the imagery, consider how cows regurgitate their food
// to digest it, and the fact that they have five stomaches.
//
//System.out.println("throwing up " + exception);
if (exception != null) throw exception;
// if the result isn't a known, expected value it means that something
// is fairly wrong, one possibility is that jikes has crashed.
//
if (result != 0 && result != 1 ) {
//exception = new RunnerException(SUPER_BADNESS);
//editor.error(exception); // this will instead be thrown
Base.openURL(BUGS_URL);
throw new RunnerException(SUPER_BADNESS);
}
return (result == 0);
// success would mean that 'result' is set to zero
return (result == 0); // ? true : false;
}
@ -120,49 +482,21 @@ public class Compiler implements MessageConsumer {
* whenever a piece (usually a line) of error message is spewed
* out from the compiler. The errors are parsed for their contents
* and line number, which is then reported back to Editor.
* In Arduino v1 this is very very crude
*/
public void message(String s) {
// This receives messages as full lines, so a newline needs
// to be added as they're printed to the console.
//System.out.print(s);
//if ((s.indexOf("warning:") != -1) && (s.indexOf("prog.c:") != -1) ) {
// String[] result = s.split(":");
// for (int x=0; x<result.length; x++)
// System.out.println(x + " " +result[x]);
// //System.out.print(s);
// //totalErrors++;
//}
if (((s.indexOf("error:") != -1) || (s.indexOf("warning:") != -1)) && (s.indexOf("prog.c:") != -1) ) {
String[] result = s.split(":");
// 0 = prog.c
// 1 = line number
// 2 = error or warning
// 3 = message
// TODO put the length of the header file into a Preference!!!
//if ((s.indexOf("(Each undeclared") == 0) && (s.indexOf("for each function it appears in") == 0)) {
int lineNum = Integer.parseInt(result[1]) - 15;
System.out.print( result[2] + " at line " + lineNum + " " + result[3]);
//}
}
}
public void processing_message(String s) {
//System.err.print(s);
// ignore cautions
if (s.indexOf("Caution") != -1) return;
if (s.indexOf("warning") != -1) return;
// jikes always uses a forward slash character as its separator,
// so replace any platform-specific separator characters before
// attemping to compare
//
String buildPathSubst = buildPath.replace(File.separatorChar, '/') + "/";
//String buildPathSubst = buildPath.replace(File.separatorChar, '/') + "/";
String buildPathSubst = buildPath.replace(File.separatorChar,File.separatorChar) + File.separatorChar;
String partialTempPath = null;
int partialStartIndex = -1; //s.indexOf(partialTempPath);
@ -191,13 +525,28 @@ public class Compiler implements MessageConsumer {
//
String s1 = s.substring(partialStartIndex +
partialTempPath.length() + 1);
int colon = s1.indexOf(':');
int lineNumber = Integer.parseInt(s1.substring(0, colon));
//System.out.println("pde / line number: " + lineNumber);
//System.out.println(s1);
if (s1.indexOf("In function")!= -1) {
System.err.print(s1);
//firstErrorFound = true;
return;
}
int colon = s1.indexOf(':');
int lineNumber = Integer.parseInt(s1.substring(0, colon));
// the "1" corresponds to the amount of lines written to the main code
// file by PdePreprocessor's writeHeader() routine before prototypes
if (fileIndex == 0)
lineNumber -= 1;
//System.out.println("pde / line number: " + lineNumber);
if (fileIndex == 0) { // main class, figure out which tab
for (int i = 1; i < sketch.codeCount; i++) {
if (sketch.code[i].flavor == Sketch.PDE) {
//System.out.println("preprocOffset "+ sketch.code[i].preprocOffset);
if (sketch.code[i].preprocOffset < lineNumber) {
fileIndex = i;
//System.out.println("i'm thinkin file " + i);
@ -211,7 +560,7 @@ public class Compiler implements MessageConsumer {
}
//String s2 = s1.substring(colon + 2);
int err = s1.indexOf("Error:");
int err = s1.indexOf(":");
if (err != -1) {
// if the first error has already been found, then this must be
@ -224,32 +573,33 @@ public class Compiler implements MessageConsumer {
// if executing at this point, this is *at least* the first error
firstErrorFound = true;
//err += "error:".length();
String description = s1.substring(err + "Error:".length());
err += ":".length();
String description = s1.substring(err);
description = description.trim();
System.err.print(description);
String hasLoop = "The method \"void loop();\" with default access";
/* String hasLoop = "The method \"void loop();\" with default access";
if (description.indexOf(hasLoop) != -1) {
description =
"Rename loop() to draw() in Processing 0070 and higher";
}
String constructorProblem =
*/
/* String constructorProblem =
"No applicable overload was found for a constructor of type";
if (description.indexOf(constructorProblem) != -1) {
//"simong.particles.ParticleSystem". Perhaps you wanted the overloaded version "ParticleSystem();" instead?
int nextSentence = description.indexOf("\".") + 3;
description = description.substring(nextSentence);
}
String overloadProblem = "No applicable overload";
*/
/* String overloadProblem = "No applicable overload";
if (description.indexOf(overloadProblem) != -1) {
int nextSentence = description.indexOf("\".") + 3;
description = description.substring(nextSentence);
}
*/
// c:/fry/processing/build/windows/work/lib/build/Temporary_6858_2476.java:1:34:1:41: Semantic Error: You need to modify your classpath, sourcepath, bootclasspath, and/or extdirs setup. Package "poo/shoe" could not be found in:
String classpathProblem = "You need to modify your classpath";
/* String classpathProblem = "You need to modify your classpath";
if (description.indexOf(classpathProblem) != -1) {
if (description.indexOf("quicktime/std") != -1) {
// special case for the quicktime libraries
@ -264,7 +614,7 @@ public class Compiler implements MessageConsumer {
" the code folder or in any libraries.";
}
}
*/
//System.out.println("description = " + description);
//System.out.println("creating exception " + exception);
exception = new RunnerException(description, fileIndex, lineNumber-1, -1);
@ -292,7 +642,7 @@ public class Compiler implements MessageConsumer {
}
}
/*
static String bootClassPath;
static public String calcBootClassPath() {
@ -307,7 +657,7 @@ public class Compiler implements MessageConsumer {
return bootClassPath;
}
*/
///
@ -340,8 +690,8 @@ public class Compiler implements MessageConsumer {
String list[] = folder.list();
for (int i = 0; i < list.length; i++) {
if (list[i].toLowerCase().endsWith(".jar") ||
list[i].toLowerCase().endsWith(".zip")) {
if (list[i].toLowerCase().endsWith(".o") ||
list[i].toLowerCase().endsWith(".a")) {
abuffer.append(sep);
abuffer.append(path);
abuffer.append(list[i]);
@ -364,7 +714,7 @@ public class Compiler implements MessageConsumer {
* @param path the input classpath
* @return array of possible package names
*/
static public String[] packageListFromClassPath(String path) {
/* static public String[] packageListFromClassPath(String path) {
Hashtable table = new Hashtable();
String pieces[] =
Base.split(path, File.pathSeparatorChar);
@ -373,8 +723,8 @@ public class Compiler implements MessageConsumer {
//System.out.println("checking piece '" + pieces[i] + "'");
if (pieces[i].length() == 0) continue;
if (pieces[i].toLowerCase().endsWith(".jar") ||
pieces[i].toLowerCase().endsWith(".zip")) {
if (pieces[i].toLowerCase().endsWith(".o") ||
pieces[i].toLowerCase().endsWith(".a")) {
packageListFromZip(pieces[i], table);
} else { // it's another type of file or directory
@ -398,7 +748,7 @@ public class Compiler implements MessageConsumer {
//PApplet.printarr(output);
return output;
}
*/
static private void packageListFromZip(String filename, Hashtable table) {
try {

View File

@ -1,323 +0,0 @@
/*
Downloader - default downloader class that connects to uisp
Part of the Arduino project http://arduino.berlios.de
Based on PdeDownloader by
Copyright (c) 2005
Hernando Barragan, Interaction Design Institute Ivrea
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.app;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;
import gnu.io.*;
public class Downloader implements MessageConsumer {
static final String BUGS_URL =
"http://arduino.berlios.de";
static final String SUPER_BADNESS =
"Compiler error, please submit this code to " + BUGS_URL;
String buildPath;
String className;
File includeFolder;
RunnerException exception;
Sketch sketch;
//Preferences preferences;
//static SerialPort serialPort;
static InputStream serialInput;
static OutputStream serialOutput;
//int serial; // last byte of data received
private String serial_port = "COM1";
private int serial_rate = 9600;
private char serial_parity = 'N';
private int serial_databits = 8;
private float serial_stopbits = 1;
public Downloader() {
serial_port = Preferences.get("serial.port");
}
// Calls the makefile with the "program" option
// TODO Windows paths!
private boolean downloadMake(String userdir) {
System.out.println("Downloading - makefile");
Process process;
int result = 0;
String command = "";
try {
serial_port = Preferences.get("serial.port");
//TODO test this in windows
// FIXME: this is really nasty, it seems that MACOS is making the
// compilation inside the lib folder, while windows is doing it
// inside the work folder ... why why why --DojoDave
if (Base.isWindows()) {
command = userdir + "tools\\gnumake.exe SERIAL=" + serial_port + " -C " + userdir + ". program";
} else if (Base.isMacOS()) {
command = userdir + "tools/gnumake SERIAL=" + serial_port + " -C " + "lib program";
}
// String command = userdir + "lib/wiringlite/bin/gnumake SERIAL=" + serial_port + " -C " + userdir + "lib/wiringlite program";
System.out.println(command);
process = Runtime.getRuntime().exec(command);
new MessageSiphon(process.getInputStream(), this);
new MessageSiphon(process.getErrorStream(), this);
boolean compiling = true;
while (compiling) {
try {
result = process.waitFor();
compiling = false;
} catch (InterruptedException ignored) { }
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error: GNUMake probably couldn't be found");
result = 99;
}
if(0 == result){
System.out.println("Arduino Download Successful");
}else{
System.out.println("Arduino Download Unsuccessful (error: " + result + ")");
}
return (result == 0);
}
public boolean downloadJava() {
String userdir = System.getProperty("user.dir") + File.separator;
//return downloadNative(userdir);
return downloadNew(userdir);
}
// Calls UISP directly, skipping the makefile
//
// TODO Windows paths!!
public boolean downloadNative(String userdir) {
System.out.println("Downloading code");
// FIXME: this is really nasty, it seems that MACOS is making the
// compilation inside the lib folder, while windows is doing it
// inside the work folder ... why why why --DojoDave
if (Base.isWindows()) {
buildPath = userdir + "tmp";
} else if (Base.isMacOS()) {
buildPath = userdir + "lib/tmp";
}
String commandDownloader[] = new String[] {
userdir + "tools/avr/bin/uisp",
//[2] Serial port
//[6] hex class file
"-dprog=stk500",
" ",
"-dspeed=9600",
"-dpart=atmega8",
"--upload",
" "
};
firstErrorFound = false; // haven't found any errors yet
secondErrorFound = false;
int result=0; // pre-initialized to quiet a bogus warning from gcc
try {
serial_port = Preferences.get("serial.port");
commandDownloader[2] = "-dserial=" + serial_port;
commandDownloader[6] = "if=" + buildPath + File.separator + "prog.hex";
// for(int i = 0; i < commandDownloader.length; i++) {
// System.out.print(commandDownloader[i] + " ");
//}
Process process = Runtime.getRuntime().exec(commandDownloader);
new processing.app.MessageSiphon(process.getInputStream(), this);
new processing.app.MessageSiphon(process.getErrorStream(), this);
// wait for the process to finish. if interrupted
// before waitFor returns, continue waiting
//
boolean compiling = true;
while (compiling) {
try {
result = process.waitFor();
compiling = false;
} catch (InterruptedException intExc) {
}
}
} catch (Exception e) {
String msg = e.getMessage();
if ((msg != null) && (msg.indexOf("uisp: not found") != -1)) {
//System.err.println("gcc is missing");
//JOptionPane.showMessageDialog(editor.base,
// "Could not find the downloader.\n" +
// "uisp is missing from your PATH,\n" +
// "see readme.txt for help.",
// "Downloade error",
// JOptionPane.ERROR_MESSAGE);
return false;
}
e.printStackTrace();
result = -1;
}
// if the result isn't a known, expected value it means that something
// is fairly wrong, one possibility is that gcc has crashed.
//
if (result != 0 && result != 1 ) {
exception = new RunnerException(SUPER_BADNESS);
//editor.error(exception);
//Base.openURL(BUGS_URL);
}
return (result == 0) ? true : false;
}
boolean firstErrorFound;
boolean secondErrorFound;
// part of the MessageConsumer interface
//
// at the moment we are pretty happy just to see the error messages coming back
public void message(String s) {
//System.err.println("MSG: " + s);
System.err.print(s);
}
public boolean downloadNew(String userdir) {
serial_port = Preferences.get("serial.port");
// TODO must manage this flag from the editor
boolean alreadyCompiled = true;
// TODO must manage this flag from global prefs
boolean debug = false;
String verbose = " -s ";
if (alreadyCompiled) {
if (serial_port == "") {
System.out.println("The serial port is not set!");
System.out.println("Use the Options -> Serial menu to select");
System.out.println("where you have connected your arduino board");
return false;
};
String commandLine = "";
if (debug) System.out.println("userdir is "+userdir);
int result = -1;
// TODO make this code more portable using File.separator
if (Base.isMacOS()) {
commandLine = userdir + "tools/avr/bin/uisp ";
commandLine += " -dprog=stk500 -dspeed=9600 ";
commandLine += " -dserial=" + serial_port;
commandLine += " -dpart=ATmega8";
commandLine += " if=" +userdir + "lib/tmp/prog.hex --upload";
} else {
commandLine = userdir + "tools\\gnumake.exe" + " SERIAL=" + serial_port + verbose + " -C " + userdir + ". program";
}
if (debug) System.out.println(commandLine);
// Launch the command as a thread (this way we can kill it
// in the case it times out)
Command command = new Command(commandLine, true);
command.setName("theDownloader");
command.start();
// TODO move this to Preferences
// Default timeout when calling a command (in seconds)
final int maxTimeOut = 30; // 10 secs
// Start timer to monitor buffer timeout ==> deadlock in process
int timeCount = 0;
while ((timeCount <= maxTimeOut) && (result == -1) && command.isAlive()) {
try {
result = command.waitResult;
Thread.currentThread().sleep(1000);
} catch (InterruptedException ie) {
}
timeCount++;
}
result = command.waitResult;
if ((result != 0) && (command.errorResult == -1)) {
// result = 94;
System.out.println("Time out error when trying to upload the program");
System.out.println("Board not present, bootloader not installed or processor's failure");
System.out.println("Arduino download unsuccessful (error: " + result + ")");
} else if (result == 0) {
System.out.println(command.errorMsg);
System.out.println("OK - Arduino download successful");
} else if ((result != 0) && (49 == command.errorResult)) {
System.out.println(command.errorMsg);
System.out.println("Bootloader not responding");
System.out.println("Arduino download unsuccessful (error: " + result + ")");
} else {
System.out.println(command.errorMsg);
System.out.println("Arduino download unsuccessful (error: " + result + ")");
}
if (command.isAlive()) command.process.destroy();
} else {
System.out.println("You have to compile the code first");
System.out.println("Arduino download unsuccessful");
}
return true;
}
}

View File

@ -20,6 +20,8 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id:$
*/
package processing.app;
@ -1135,58 +1137,52 @@ public class Editor extends JFrame
doClose();
running = true;
buttons.run();
message("Compiling...");
// do this for the terminal window / dos prompt / etc
for (int i = 0; i < 10; i++) System.out.println();
// clear the console on each run, unless the user doesn't want to
//if (Base.getBoolean("console.auto_clear", true)) {
//if (Preferences.getBoolean("console.auto_clear", true)) {
if (Preferences.getBoolean("console.auto_clear")) {
console.clear();
console.clear();
}
presenting = present;
// console.message("Arduino compiling\n", false,true);
String cfilename = System.getProperty("user.dir") + File.separator + "lib/build/arduino.c";
//console.message(" file = " + cfilename, false,true);
try {
Writer out = new FileWriter(cfilename);
out.write(textarea.getText());
out.flush();
out.close();
}
catch (IOException e)
{
error(e);
}
if (presenting && Base.isMacOS()) {
// check to see if osx 10.2, if so, show a warning
String osver = System.getProperty("os.version").substring(0, 4);
if (osver.equals("10.2")) {
Base.showWarning("Time for an OS Upgrade",
"The \"Present\" feature may not be available on\n" +
"Mac OS X 10.2, because of what appears to be\n" +
"a bug in the Java 1.4 implementation on 10.2.\n" +
"In case it works on your machine, present mode\n" +
"will start, but if you get a flickering white\n" +
"window, using Command-Q to quit the sketch", null);
}
}
Compiler compiler = new Compiler();
String buildPath = Preferences.get("build.path");
//console.message(" buildpath = " + buildPath, false,true);
try {
boolean success = compiler.compile(sketch, buildPath);
} catch (RunnerException e) {
error(e);
}
// FIXED: 20050902 - DojoDave
// the clear button requires a the clearRun method instead of only clear!!
buttons.clearRun();
try {
if (!sketch.handleRun(new Target(
System.getProperty("user.dir") + File.separator + "lib" +
File.separator + "targets", Preferences.get("build.target"))))
return;
//runtime = new Runner(sketch, Editor.this);
//runtime.start(appletLocation);
watcher = new RunButtonWatcher();
message("Done compiling.");
if(watcher != null) watcher.stop();
//- if (!sketch.handleRun()) return;
//- runtime = new Runner(sketch, Editor.this);
//- runtime.start(appletLocation);
//- watcher = new RunButtonWatcher();
//- } catch (RunnerException e) {
//- error(e);
//- } catch (Exception e) {
//- e.printStackTrace();
//- }
} catch (RunnerException e) {
message("Error compiling...");
error(e);
} catch (Exception e) {
e.printStackTrace();
}
// this doesn't seem to help much or at all
/*
final SwingWorker worker = new SwingWorker() {
@ -1197,10 +1193,12 @@ public class Editor extends JFrame
runtime = new Runner(sketch, Editor.this);
runtime.start(presenting ? presentLocation : appletLocation);
watcher = new RunButtonWatcher();
message("Done compiling.");
} catch (RunnerException e) {
message("Error compiling...");
error(e);
} catch (Exception e) {
e.printStackTrace();
}
@ -1210,6 +1208,7 @@ public class Editor extends JFrame
worker.start();
*/
//sketch.cleanup(); // where does this go?
buttons.clear();
}
@ -1661,9 +1660,30 @@ public class Editor extends JFrame
* hitting export twice, quickly, and horking things up.
*/
synchronized public void handleExport() {
//if(debugging)
//doStop();
console.clear();
//String what = sketch.isLibrary() ? "Applet" : "Library";
//message("Exporting " + what + "...");
Downloader d = new Downloader();
d.downloadJava();
message("Uploading to I/O Board...");
try {
//boolean success = sketch.isLibrary() ?
//sketch.exportLibrary() : sketch.exportApplet();
boolean success = sketch.exportApplet(new Target(
System.getProperty("user.dir") + File.separator + "lib" +
File.separator + "targets", Preferences.get("build.target")));
if (success) {
message("Done uploading.");
} else {
// error message will already be visible
}
} catch (RunnerException e) {
message("Error during upload.");
//e.printStackTrace();
error(e);
} catch (Exception e) {
e.printStackTrace();
}
buttons.clear();
}

View File

@ -1,7 +1,7 @@
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Part of the Arduino project - http://arduino.berlios.de/
Copyright (c) 2004-05 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
@ -19,6 +19,8 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id:$
*/
package processing.app;
@ -100,10 +102,11 @@ public class Sketch {
// off of the main file name
if (mainFilename.endsWith(".pde")) {
name = mainFilename.substring(0, mainFilename.length() - 4);
} else if (mainFilename.endsWith(".java")) {
name = mainFilename.substring(0, mainFilename.length() - 5);
} else if (mainFilename.endsWith(".c")) {
name = mainFilename.substring(0, mainFilename.length() - 2);
} else if (mainFilename.endsWith(".cpp")) {
name = mainFilename.substring(0, mainFilename.length() - 4);
}
// lib/build must exist when the application is started
// it is added to the CLASSPATH by default, but if it doesn't
// exist when the application is started, then java will remove
@ -132,15 +135,15 @@ public class Sketch {
/**
* Build the list of files.
* <P>
*
* Generally this is only done once, rather than
* each time a change is made, because otherwise it gets to be
* a nightmare to keep track of what files went where, because
* not all the data will be saved to disk.
* <P>
*
* This also gets called when the main sketch file is renamed,
* because the sketch has to be reloaded from a different folder.
* <P>
*
* Another exception is when an external editor is in use,
* in which case the load happens each time "run" is hit.
*/
@ -153,9 +156,11 @@ public class Sketch {
for (int i = 0; i < list.length; i++) {
if (list[i].endsWith(".pde")) codeCount++;
else if (list[i].endsWith(".java")) codeCount++;
else if (list[i].endsWith(".c")) codeCount++;
else if (list[i].endsWith(".cpp")) codeCount++;
else if (list[i].endsWith(".pde.x")) hiddenCount++;
else if (list[i].endsWith(".java.x")) hiddenCount++;
else if (list[i].endsWith(".c.x")) hiddenCount++;
else if (list[i].endsWith(".cpp.x")) hiddenCount++;
}
code = new SketchCode[codeCount];
@ -171,9 +176,15 @@ public class Sketch {
new File(folder, list[i]),
PDE);
} else if (list[i].endsWith(".java")) {
} else if (list[i].endsWith(".c")) {
code[codeCounter++] =
new SketchCode(list[i].substring(0, list[i].length() - 5),
new SketchCode(list[i].substring(0, list[i].length() - 2),
new File(folder, list[i]),
JAVA);
} else if (list[i].endsWith(".cpp")) {
code[codeCounter++] =
new SketchCode(list[i].substring(0, list[i].length() - 4),
new File(folder, list[i]),
JAVA);
@ -183,9 +194,14 @@ public class Sketch {
new File(folder, list[i]),
PDE);
} else if (list[i].endsWith(".java.x")) {
} else if (list[i].endsWith(".c.x")) {
hidden[hiddenCounter++] =
new SketchCode(list[i].substring(0, list[i].length() - 7),
new SketchCode(list[i].substring(0, list[i].length() - 4),
new File(folder, list[i]),
JAVA);
} else if (list[i].endsWith(".cpp.x")) {
hidden[hiddenCounter++] =
new SketchCode(list[i].substring(0, list[i].length() - 6),
new File(folder, list[i]),
JAVA);
}
@ -299,7 +315,7 @@ public class Sketch {
String prompt = (current == code[0]) ?
"New name for sketch:" : "New name for file:";
String oldName =
(current.flavor == PDE) ? current.name : current.name + ".java";
(current.flavor == PDE) ? current.name : current.name + ".cpp";
editor.status.edit(prompt, oldName);
}
@ -331,8 +347,9 @@ public class Sketch {
return;
}
if (newName.trim().equals(".java") ||
newName.trim().equals(".pde")) {
if (newName.trim().equals(".c") ||
newName.trim().equals(".pde") ||
newName.trim().equals(".cpp")) {
return;
}
@ -346,18 +363,21 @@ public class Sketch {
newName = newName.substring(0, newName.length() - 4);
newFlavor = PDE;
} else if (newName.endsWith(".java")) {
} else if (newName.endsWith(".c") || newName.endsWith(".cpp")) {
// don't show this error if creating a new tab
if (renamingCode && (code[0] == current)) {
Base.showWarning("Problem with rename",
"The main .pde file cannot be .java file.\n" +
"The main .pde file cannot be .c or .cpp file.\n" +
"(It may be time for your to graduate to a\n" +
"\"real\" programming environment)", null);
return;
}
newFilename = newName;
newName = newName.substring(0, newName.length() - 5);
if(newName.endsWith(".c"))
newName = newName.substring(0, newName.length() - 2);
else if(newName.endsWith(".cpp"))
newName = newName.substring(0, newName.length() - 4);
newFlavor = JAVA;
} else {
@ -370,7 +390,7 @@ public class Sketch {
// or something like that (nothing against poo time)
if (newName.indexOf('.') != -1) {
newName = Sketchbook.sanitizedName(newName);
newFilename = newName + ((newFlavor == PDE) ? ".pde" : ".java");
newFilename = newName + ((newFlavor == PDE) ? ".pde" : ".cpp");
}
// create the new file, new SketchCode object and load it
@ -522,6 +542,7 @@ public class Sketch {
// update the tabs
//editor.header.repaint();
editor.header.rebuild();
// force the update on the mac?
@ -658,7 +679,6 @@ public class Sketch {
// update the tabs
setCurrent(0);
editor.header.repaint();
//editor.header.rebuild();
}
@ -981,17 +1001,18 @@ public class Sketch {
// if the file appears to be code related, drop it
// into the code folder, instead of the data folder
if (filename.toLowerCase().endsWith(".class") ||
if (filename.toLowerCase().endsWith(".o") /*||
filename.toLowerCase().endsWith(".jar") ||
filename.toLowerCase().endsWith(".dll") ||
filename.toLowerCase().endsWith(".jnilib") ||
filename.toLowerCase().endsWith(".so")) {
filename.toLowerCase().endsWith(".so") */ ) {
//File codeFolder = new File(this.folder, "code");
if (!codeFolder.exists()) codeFolder.mkdirs();
destFile = new File(codeFolder, filename);
} else if (filename.toLowerCase().endsWith(".pde") ||
filename.toLowerCase().endsWith(".java")) {
filename.toLowerCase().endsWith(".c") ||
filename.toLowerCase().endsWith(".cpp")) {
destFile = new File(this.folder, filename);
addingCode = true;
@ -1030,7 +1051,7 @@ public class Sketch {
newName = newName.substring(0, newName.length() - 4);
newFlavor = PDE;
} else {
newName = newName.substring(0, newName.length() - 5);
newName = newName.substring(0, newName.length() - 2);
newFlavor = JAVA;
}
@ -1045,7 +1066,8 @@ public class Sketch {
public void importLibrary(String jarPath) {
// make sure the user didn't hide the sketch folder
System.out.println(jarPath);
/* // make sure the user didn't hide the sketch folder
ensureExistence();
String list[] = Compiler.packageListFromClassPath(jarPath);
@ -1068,7 +1090,7 @@ public class Sketch {
buffer.append(editor.getText());
editor.setText(buffer.toString(), 0, 0); // scroll to start
setModified();
}
*/ }
/**
@ -1174,7 +1196,7 @@ public class Sketch {
* X. afterwards, some of these steps need a cleanup function
* </PRE>
*/
public boolean handleRun() throws RunnerException {
public boolean handleRun(Target target) throws RunnerException {
// make sure the user didn't hide the sketch folder
ensureExistence();
@ -1210,7 +1232,7 @@ public class Sketch {
// handle preprocessing the main file's code
//mainClassName = build(TEMP_BUILD_PATH, suggestedClassName);
mainClassName =
build(tempBuildFolder.getAbsolutePath(), suggestedClassName);
build(target, tempBuildFolder.getAbsolutePath(), suggestedClassName);
// externalPaths is magically set by build()
if (!externalRuntime) { // only if not running externally already
@ -1240,7 +1262,7 @@ public class Sketch {
*
* @return null if compilation failed, main class name if not
*/
protected String build(String buildPath, String suggestedClassName)
protected String build(Target target, String buildPath, String suggestedClassName)
throws RunnerException {
// make sure the user didn't hide the sketch folder
ensureExistence();
@ -1279,8 +1301,8 @@ public class Sketch {
String codeFolderClassPath =
Compiler.contentsToClassPath(codeFolder);
// get list of packages found in those jars
codeFolderPackages =
Compiler.packageListFromClassPath(codeFolderClassPath);
// codeFolderPackages =
// Compiler.packageListFromClassPath(codeFolderClassPath);
//PApplet.println(libraryPath);
//PApplet.println("packages:");
//PApplet.printarr(codeFolderPackages);
@ -1329,11 +1351,11 @@ public class Sketch {
// since using the special classloader,
// run externally whenever there are extra classes defined
if ((bigCode.indexOf(" class ") != -1) ||
/* if ((bigCode.indexOf(" class ") != -1) ||
(bigCode.indexOf("\nclass ") != -1)) {
externalRuntime = true;
}
*/
// if running in opengl mode, this is gonna be external
//if (Preferences.get("renderer").equals("opengl")) {
//externalRuntime = true;
@ -1360,7 +1382,7 @@ public class Sketch {
//System.out.println();
} else {
code[0].preprocName = className + ".java";
code[0].preprocName = className + "." + Preferences.get("build.extension");
}
// store this for the compiler and the runtime
@ -1384,6 +1406,7 @@ public class Sketch {
}
}
errorLine -= code[errorFile].preprocOffset;
//errorLine -= preprocessor.prototypeCount;
throw new RunnerException(re.getMessage(), errorFile,
errorLine, re.getColumn());
@ -1402,9 +1425,11 @@ public class Sketch {
pattern = compiler.compile(mess);
} catch (MalformedPatternException e) {
Base.showWarning("Internal Problem",
"An internal error occurred while trying\n" +
"to compile the sketch. Please report\n" +
"this online at http://processing.org/bugs", e);
"An internal error occurred while trying\n" +
"to compile the sketch. Please report\n" +
"this online at " +
"https://developer.berlios.de/bugs/?group_id=3590",
e);
}
PatternMatcherInput input =
@ -1422,6 +1447,7 @@ public class Sketch {
}
}
errorLine -= code[errorFile].preprocOffset;
//errorLine -= preprocessor.prototypeCount;
throw new RunnerException(tsre.getMessage(),
errorFile, errorLine, errorColumn);
@ -1485,7 +1511,7 @@ public class Sketch {
// just write the the contents of 'program' to a .java file
// into the build directory. uses byte stream and reader/writer
// shtuff so that unicode bunk is properly handled
String filename = code[i].name + ".java";
String filename = code[i].name + "." + Preferences.get("build.extension");
try {
Base.saveFile(code[i].program, new File(buildPath, filename));
} catch (IOException e) {
@ -1500,12 +1526,53 @@ public class Sketch {
// compile the program. errors will happen as a RunnerException
// that will bubble up to whomever called build().
//
// note: this has been changed to catch build exceptions, adjust
// line number for number of included prototypes, and rethrow
Compiler compiler = new Compiler();
boolean success = compiler.compile(this, buildPath);
boolean success;
try {
success = compiler.compile(this, buildPath, target);
} catch (RunnerException re) {
throw new RunnerException(re.getMessage(),
re.file,
re.line,// - preprocessor.prototypeCount,
re.column);
} catch (Exception ex) {
// TODO better method for handling this?
throw new RunnerException(ex.toString());
}
//System.out.println("success = " + success + " ... " + primaryClassName);
return success ? primaryClassName : null;
}
protected String upload(String buildPath, String suggestedClassName)
throws RunnerException {
// download the program
//
Uploader downloader =
new Uploader(buildPath, suggestedClassName, this);
// macos9 now officially broken.. see PdeCompilerJavac
//PdeCompiler compiler =
// ((PdeBase.platform == PdeBase.MACOS9) ?
// new PdeCompilerJavac(buildPath, className, this) :
// new PdeCompiler(buildPath, className, this));
// run the compiler, and funnel errors to the leechErr
// which is a wrapped around
// (this will catch and parse errors during compilation
// the messageStream will call message() for 'compiler')
MessageStream messageStream = new MessageStream(downloader);
//PrintStream leechErr = new PrintStream(messageStream);
//boolean result = compiler.compileJava(leechErr);
//return compiler.compileJava(leechErr);
boolean success =
downloader.downloadJava(new PrintStream(messageStream));
return success ? suggestedClassName : null;
}
protected int countLines(String what) {
char c[] = what.toCharArray();
@ -1546,7 +1613,306 @@ public class Sketch {
* +-------------------------------------------------------+
* </PRE>
*/
public boolean exportApplet() throws Exception {
public boolean exportApplet(Target target) throws RunnerException {
// make sure the user didn't hide the sketch folder
ensureExistence();
zipFileContents = new Hashtable();
// nuke the old applet folder because it can cause trouble
File appletFolder = new File(folder, "applet");
Base.removeDir(appletFolder);
appletFolder.mkdirs();
// build the sketch
String foundName = build(target, appletFolder.getPath(), name);
foundName = upload(appletFolder.getPath(), name);
// (already reported) error during export, exit this function
if (foundName == null) return false;
// if name != exportSketchName, then that's weirdness
// BUG unfortunately, that can also be a bug in the preproc :(
if (!name.equals(foundName)) {
Base.showWarning("Error during export",
"Sketch name is " + name + " but the sketch\n" +
"name in the code was " + foundName, null);
return false;
}
/* int wide = PApplet.DEFAULT_WIDTH;
int high = PApplet.DEFAULT_HEIGHT;
PatternMatcher matcher = new Perl5Matcher();
PatternCompiler compiler = new Perl5Compiler();
// this matches against any uses of the size() function,
// whether they contain numbers of variables or whatever.
// this way, no warning is shown if size() isn't actually
// used in the applet, which is the case especially for
// beginners that are cutting/pasting from the reference.
// modified for 83 to match size(XXX, ddd so that it'll
// properly handle size(200, 200) and size(200, 200, P3D)
String sizing =
"[\\s\\;]size\\s*\\(\\s*(\\S+)\\s*,\\s*(\\d+)";
Pattern pattern = compiler.compile(sizing);
// adds a space at the beginning, in case size() is the very
// first thing in the program (very common), since the regexp
// needs to check for things in front of it.
PatternMatcherInput input =
new PatternMatcherInput(" " + code[0].program);
if (matcher.contains(input, pattern)) {
MatchResult result = matcher.getMatch();
try {
wide = Integer.parseInt(result.group(1).toString());
high = Integer.parseInt(result.group(2).toString());
} catch (NumberFormatException e) {
// found a reference to size, but it didn't
// seem to contain numbers
final String message =
"The size of this applet could not automatically be\n" +
"determined from your code. You'll have to edit the\n" +
"HTML file to set the size of the applet.";
Base.showWarning("Could not find applet size", message, null);
}
} // else no size() command found
// originally tried to grab this with a regexp matcher,
// but it wouldn't span over multiple lines for the match.
// this could prolly be forced, but since that's the case
// better just to parse by hand.
StringBuffer dbuffer = new StringBuffer();
String lines[] = PApplet.split(code[0].program, '\n');
for (int i = 0; i < lines.length; i++) {
if (lines[i].trim().startsWith("/**")) { // this is our comment
*/ // some smartass put the whole thing on the same line
//if (lines[j].indexOf("*/") != -1) break;
// for (int j = i+1; j < lines.length; j++) {
// if (lines[j].trim().endsWith("*/")) {
// remove the */ from the end, and any extra *s
// in case there's also content on this line
// nah, don't bother.. make them use the three lines
// break;
// }
/* int offset = 0;
while ((offset < lines[j].length()) &&
((lines[j].charAt(offset) == '*') ||
(lines[j].charAt(offset) == ' '))) {
offset++;
}
// insert the return into the html to help w/ line breaks
dbuffer.append(lines[j].substring(offset) + "\n");
}
}
}
String description = dbuffer.toString();
StringBuffer sources = new StringBuffer();
for (int i = 0; i < codeCount; i++) {
sources.append("<a href=\"" + code[i].file.getName() + "\">" +
code[i].name + "</a> ");
}
File htmlOutputFile = new File(appletFolder, "index.html");
FileOutputStream fos = new FileOutputStream(htmlOutputFile);
PrintStream ps = new PrintStream(fos);
// @@sketch@@, @@width@@, @@height@@, @@archive@@, @@source@@
// and now @@description@@
InputStream is = null;
// if there is an applet.html file in the sketch folder, use that
File customHtml = new File(folder, "applet.html");
if (customHtml.exists()) {
is = new FileInputStream(customHtml);
}
if (is == null) {
is = Base.getStream("applet.html");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = reader.readLine()) != null) {
if (line.indexOf("@@") != -1) {
StringBuffer sb = new StringBuffer(line);
int index = 0;
while ((index = sb.indexOf("@@sketch@@")) != -1) {
sb.replace(index, index + "@@sketch@@".length(),
name);
}
while ((index = sb.indexOf("@@source@@")) != -1) {
sb.replace(index, index + "@@source@@".length(),
sources.toString());
}
while ((index = sb.indexOf("@@archive@@")) != -1) {
sb.replace(index, index + "@@archive@@".length(),
name + ".jar");
}
while ((index = sb.indexOf("@@width@@")) != -1) {
sb.replace(index, index + "@@width@@".length(),
String.valueOf(wide));
}
while ((index = sb.indexOf("@@height@@")) != -1) {
sb.replace(index, index + "@@height@@".length(),
String.valueOf(high));
}
while ((index = sb.indexOf("@@description@@")) != -1) {
sb.replace(index, index + "@@description@@".length(),
description);
}
line = sb.toString();
}
ps.println(line);
}
reader.close();
ps.flush();
ps.close();
// copy the loading gif to the applet
String LOADING_IMAGE = "loading.gif";
File loadingImage = new File(folder, LOADING_IMAGE);
if (!loadingImage.exists()) {
loadingImage = new File("lib", LOADING_IMAGE);
}
Base.copyFile(loadingImage, new File(appletFolder, LOADING_IMAGE));
*/
// copy the source files to the target, since we like
// to encourage people to share their code
for (int i = 0; i < codeCount; i++) {
try {
Base.copyFile(code[i].file,
new File(appletFolder, code[i].file.getName()));
} catch (IOException e) {
e.printStackTrace();
}
}
/* // create new .jar file
FileOutputStream zipOutputFile =
new FileOutputStream(new File(appletFolder, name + ".jar"));
ZipOutputStream zos = new ZipOutputStream(zipOutputFile);
ZipEntry entry;
// add the manifest file
addManifest(zos);
// add the contents of the code folder to the jar
// unpacks all jar files
//File codeFolder = new File(folder, "code");
if (codeFolder.exists()) {
String includes = Compiler.contentsToClassPath(codeFolder);
packClassPathIntoZipFile(includes, zos);
}
// add contents of 'library' folders to the jar file
// if a file called 'export.txt' is in there, it contains
// a list of the files that should be exported.
// otherwise, all files are exported.
Enumeration en = importedLibraries.elements();
while (en.hasMoreElements()) {
// in the list is a File object that points the
// library sketch's "library" folder
File libraryFolder = (File)en.nextElement();
File exportSettings = new File(libraryFolder, "export.txt");
String exportList[] = null;
if (exportSettings.exists()) {
String info[] = Base.loadStrings(exportSettings);
for (int i = 0; i < info.length; i++) {
if (info[i].startsWith("applet")) {
int idx = info[i].indexOf('='); // get applet= or applet =
String commas = info[i].substring(idx+1).trim();
exportList = PApplet.split(commas, ", ");
}
}
} else {
exportList = libraryFolder.list();
}
for (int i = 0; i < exportList.length; i++) {
if (exportList[i].equals(".") ||
exportList[i].equals("..")) continue;
exportList[i] = PApplet.trim(exportList[i]);
if (exportList[i].equals("")) continue;
File exportFile = new File(libraryFolder, exportList[i]);
if (!exportFile.exists()) {
System.err.println("File " + exportList[i] + " does not exist");
} else if (exportFile.isDirectory()) {
System.err.println("Ignoring sub-folder \"" + exportList[i] + "\"");
} else if (exportFile.getName().toLowerCase().endsWith(".zip") ||
exportFile.getName().toLowerCase().endsWith(".jar")) {
packClassPathIntoZipFile(exportFile.getAbsolutePath(), zos);
} else { // just copy the file over.. prolly a .dll or something
Base.copyFile(exportFile,
new File(appletFolder, exportFile.getName()));
}
}
}
*/
/* String bagelJar = "lib/core.jar";
packClassPathIntoZipFile(bagelJar, zos);
// files to include from data directory
// TODO this needs to be recursive
if (dataFolder.exists()) {
String dataFiles[] = dataFolder.list();
for (int i = 0; i < dataFiles.length; i++) {
// don't export hidden files
// skipping dot prefix removes all: . .. .DS_Store
if (dataFiles[i].charAt(0) == '.') continue;
entry = new ZipEntry(dataFiles[i]);
zos.putNextEntry(entry);
zos.write(Base.grabFile(new File(dataFolder, dataFiles[i])));
zos.closeEntry();
}
}
// add the project's .class files to the jar
// just grabs everything from the build directory
// since there may be some inner classes
// (add any .class files from the applet dir, then delete them)
// TODO this needs to be recursive (for packages)
String classfiles[] = appletFolder.list();
for (int i = 0; i < classfiles.length; i++) {
if (classfiles[i].endsWith(".class")) {
entry = new ZipEntry(classfiles[i]);
zos.putNextEntry(entry);
zos.write(Base.grabFile(new File(appletFolder, classfiles[i])));
zos.closeEntry();
}
}
*/
String classfiles[] = appletFolder.list();
// remove the .class files from the applet folder. if they're not
// removed, the msjvm will complain about an illegal access error,
// since the classes are outside the jar file.
for (int i = 0; i < classfiles.length; i++) {
if (classfiles[i].endsWith(".class")) {
File deadguy = new File(appletFolder, classfiles[i]);
if (!deadguy.delete()) {
Base.showWarning("Could not delete",
classfiles[i] + " could not \n" +
"be deleted from the applet folder. \n" +
"You'll need to remove it by hand.", null);
}
}
}
// close up the jar file
/* zos.flush();
zos.close();
*/
if(Preferences.getBoolean("uploader.open_folder"))
Base.openFolder(appletFolder);
return true;
}
@ -1603,7 +1969,7 @@ public class Sketch {
}
public void addManifest(ZipOutputStream zos) throws IOException {
/* public void addManifest(ZipOutputStream zos) throws IOException {
ZipEntry entry = new ZipEntry("META-INF/MANIFEST.MF");
zos.putNextEntry(entry);
@ -1613,7 +1979,7 @@ public class Sketch {
"Main-Class: " + name + "\n"; // TODO not package friendly
zos.write(contents.getBytes());
zos.closeEntry();
*/
/*
for (int i = 0; i < bagelClasses.length; i++) {
if (!bagelClasses[i].endsWith(".class")) continue;
@ -1623,17 +1989,17 @@ public class Sketch {
zos.closeEntry();
}
*/
}
/* }
*/
/**
* Slurps up .class files from a colon (or semicolon on windows)
* separated list of paths and adds them to a ZipOutputStream.
*/
public void packClassPathIntoZipFile(String path,
/* public void packClassPathIntoZipFile(String path,
ZipOutputStream zos)
throws IOException {
String pieces[] = Base.split(path, File.pathSeparatorChar);
String pieces[] = PApplet.split(path, File.pathSeparatorChar);
for (int i = 0; i < pieces.length; i++) {
if (pieces[i].length() == 0) continue;
@ -1692,14 +2058,14 @@ public class Sketch {
}
}
}
*/
/**
* Continue the process of magical exporting. This function
* can be called recursively to walk through folders looking
* for more goodies that will be added to the ZipOutputStream.
*/
static public void packClassPathIntoZipFileRecursive(File dir,
/* static public void packClassPathIntoZipFileRecursive(File dir,
String sofar,
ZipOutputStream zos)
throws IOException {
@ -1729,7 +2095,7 @@ public class Sketch {
}
}
}
*/
/**
* Make sure the sketch hasn't been moved or deleted by some

View File

@ -565,8 +565,8 @@ public class Sketchbook {
librariesClassPath +=
File.pathSeparatorChar + libraryClassPath;
// need to associate each import with a library folder
String packages[] =
Compiler.packageListFromClassPath(libraryClassPath);
String packages[] = new String[0];
//Compiler.packageListFromClassPath(libraryClassPath);
for (int k = 0; k < packages.length; k++) {
importToLibraryTable.put(packages[k], exported);
}

79
app/Target.java Normal file
View File

@ -0,0 +1,79 @@
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Target - represents a target platform
Part of the Arduino project - http://arduino.berlios.de/
Copyright (c) 2005
David A. Mellis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id:$
*/
package processing.app;
import java.io.*;
import java.util.*;
/**
* Represents a target platform (e.g. Wiring board, Arduino board).
*/
public class Target {
String path;
List sources = new ArrayList();
List objects = new ArrayList();
/**
* Create a Target.
* @param path the directory containing config, source, and object files for
* the target platform.
*/
public Target(String base, String target) throws IOException {
path = base + File.separator + target;
String[] files = (new File(path)).list();
if (files == null)
throw new IOException("Target platform: \"" + target + "\" not found.\n" +
"Make sure that \"build.target\" in the \n" +
"preferences file points to a subdirectory of \n" +
base);
for (int i = 0; i < files.length; i++) {
if (files[i].endsWith(".c") || files[i].endsWith(".cpp"))
sources.add(files[i]);
if (files[i].endsWith(".o"))
objects.add(files[i]);
}
}
public String getPath() { return path; }
/**
* The source files in the library for the target platform.
* @return A read-only collection of strings containing the name of each source file.
*/
public Collection getSourceFilenames() {
return Collections.unmodifiableList(sources);
}
/**
* The object files in the library for the target platform.
* @return A read-only collection of strings containing the name of each object file.
*/
public Collection getObjectFilenames() {
return Collections.unmodifiableList(objects);
}
}

230
app/Uploader.java Executable file
View File

@ -0,0 +1,230 @@
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Uploader - default downloader class that connects to uisp
Part of the Arduino project - http://arduino.berlios.de/
Copyright (c) 2004-05
Hernando Barragan
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id:$
*/
package processing.app;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;
//#ifndef RXTX
//import javax.comm.*;
//#else
// rxtx uses package gnu.io, but all the class names
// are the same as those used by javax.comm
import gnu.io.*;
//#endif
public class Uploader implements MessageConsumer {
static final String BUGS_URL =
"https://developer.berlios.de/bugs/?group_id=3590";
static final String SUPER_BADNESS =
"Compiler error, please submit this code to " + BUGS_URL;
String buildPath;
String className;
File includeFolder;
RunnerException exception;
Sketch sketch;
//PdePreferences preferences;
//Serial serialPort;
static InputStream serialInput;
static OutputStream serialOutput;
//int serial; // last byte of data received
private String serial_port = "COM1";
private int serial_rate = 9600;
private char serial_parity = 'N';
private int serial_databits = 8;
private float serial_stopbits = 1;
public void serialPreferences() {
//System.out.println("setting serial properties");
serial_port = Preferences.get("serial.port");
serial_rate = Preferences.getInteger("serial.download_rate");
serial_parity = Preferences.get("serial.parity").charAt(0);
serial_databits = Preferences.getInteger("serial.databits");
serial_stopbits = new Float(Preferences.get("serial.stopbits")).floatValue();
}
public Uploader(String buildPath, String className,
Sketch sketch) {
this.buildPath = buildPath;
this.includeFolder = includeFolder;
this.className = className;
this.sketch = sketch;
}
public boolean downloadJava(PrintStream leechErr) throws RunnerException {
String userdir = System.getProperty("user.dir") + File.separator;
// String commandDownloader[] = new String[] {
// ((!Base.isMacOS()) ? "tools/avr/bin/uisp" :
// userdir + "tools/avr/bin/uisp"),
// //[2] Serial port
// //[3] Serial download rate
// //[6] hex class file
// "-dprog=stk500",
// " ",
// " ",
// "-dpart=" + Preferences.get("build.mcu"),
// "--upload",
// " "
// };
firstErrorFound = false; // haven't found any errors yet
secondErrorFound = false;
notFoundError = false;
int result=0; // pre-initialized to quiet a bogus warning from jikes
try {
serialPreferences();
List commandDownloader = new ArrayList();
commandDownloader.add((!Base.isMacOS() ? "" : userdir) + "tools/avr/bin/uisp");
commandDownloader.add("-dprog=" + Preferences.get("upload.programmer"));
commandDownloader.add("-dpart=" + Preferences.get("build.mcu"));
if (Preferences.get("upload.programmer").equals("dapa"))
commandDownloader.add("-dlpt=" + Preferences.get("parallel.port"));
else {
commandDownloader.add("-dserial=" + (!Base.isMacOS() ? "/dev/" + serial_port.toLowerCase() : serial_port));
commandDownloader.add("-dspeed=" + serial_rate);
}
if (Preferences.getBoolean("upload.erase"))
commandDownloader.add("--erase");
commandDownloader.add("--upload");
if (Preferences.getBoolean("upload.verify"))
commandDownloader.add("--verify");
commandDownloader.add("if=" + buildPath + File.separator + className + ".hex");
// commandDownloader[2] = ((!Base.isMacOS()) ? "-dserial=/dev/" + serial_port.toLowerCase() : "-dserial=" + serial_port );
// commandDownloader[3] = "-dspeed=" + serial_rate;
// commandDownloader[6] = "if=" + buildPath + File.separator + className + ".hex";
/*for(int i = 0; i < commandDownloader.length; i++) {
System.out.println(commandDownloader[i]);
}*/
// Cleanup the serial buffer
/*serialPort = new Serial();
byte[] readBuffer;
while(serialPort.available() > 0) {
readBuffer = serialPort.readBytes();
Thread.sleep(100);
}
serialPort.dispose(); */
String[] commandArray = new String[commandDownloader.size()];
Process process = Runtime.getRuntime().exec((String[]) commandDownloader.toArray(commandArray));
new MessageSiphon(process.getInputStream(), this);
new MessageSiphon(process.getErrorStream(), this);
// wait for the process to finish. if interrupted
// before waitFor returns, continue waiting
//
boolean compiling = true;
while (compiling) {
try {
result = process.waitFor();
compiling = false;
} catch (InterruptedException intExc) {
}
}
if(exception!=null) {
exception.hideStackTrace = true;
throw exception;
}
if(result!=0)
return false;
} catch (Exception e) {
String msg = e.getMessage();
if ((msg != null) && (msg.indexOf("uisp: not found") != -1)) {
//System.err.println("uisp is missing");
//JOptionPane.showMessageDialog(editor.base,
// "Could not find the compiler.\n" +
// "uisp is missing from your PATH,\n" +
// "see readme.txt for help.",
// "Compiler error",
// JOptionPane.ERROR_MESSAGE);
return false;
} else {
e.printStackTrace();
result = -1;
}
}
//System.out.println("result2 is "+result);
// if the result isn't a known, expected value it means that something
// is fairly wrong, one possibility is that jikes has crashed.
//
if (exception != null) throw exception;
if ((result != 0) && (result != 1 )) {
exception = new RunnerException(SUPER_BADNESS);
//editor.error(exception);
//PdeBase.openURL(BUGS_URL);
//throw new PdeException(SUPER_BADNESS);
}
return (result == 0); // ? true : false;
}
boolean firstErrorFound;
boolean secondErrorFound;
// part of the PdeMessageConsumer interface
//
boolean notFoundError;
public void message(String s) {
//System.err.println("MSG: " + s);
System.err.print(s);
// ignore cautions
if (s.indexOf("Error") != -1) {
//exception = new RunnerException(s+" Check the serial port selected or your Board is connected");
//System.out.println(s);
notFoundError = true;
return;
}
if(notFoundError) {
//System.out.println("throwing something");
exception = new RunnerException("the selected serial port "+s+" does not exist or your board is not connected");
return;
}
// jikes always uses a forward slash character as its separator, so
// we need to replace any platform-specific separator characters before
// attemping to compare
//
if (s.indexOf("Device is not responding") != -1 ) {
exception = new RunnerException("Device is not responding, check the right serial port is selected or RESET the board right before exporting");
return;
}
if (s.indexOf("Programmer is not responding") != -1) {
exception = new RunnerException("Programmer is not responding, RESET the board right before exporting");
return;
}
}
}

View File

@ -1,9 +0,0 @@
*Lexer.java
*Recognizer.java
*TokenTypes.java
*TokenTypes.txt
*TreeParser.java
*TreeParserTokenTypes.java
*TreeParserTokenTypes.txt
expanded*.g

133
app/preproc/CSymbolTable.java Executable file
View File

@ -0,0 +1,133 @@
package processing.app.preproc;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
public class CSymbolTable {
/** holds list of scopes */
private Vector scopeStack;
/** table where all defined names are mapped to TNode tree nodes */
private Hashtable symTable;
public CSymbolTable() {
scopeStack = new Vector(10);
symTable = new Hashtable(533);
}
/** push a new scope onto the scope stack.
*/
public void pushScope(String s) {
//System.out.println("push scope:" + s);
scopeStack.addElement(s);
}
/** pop the last scope off the scope stack.
*/
public void popScope() {
//System.out.println("pop scope");
int size = scopeStack.size();
if(size > 0)
scopeStack.removeElementAt(size - 1);
}
/** return the current scope as a string
*/
public String currentScopeAsString() {
StringBuffer buf = new StringBuffer(100);
boolean first = true;
Enumeration e = scopeStack.elements();
while(e.hasMoreElements()) {
if(first)
first = false;
else
buf.append("::");
buf.append(e.nextElement().toString());
}
return buf.toString();
}
/** given a name for a type, append it with the
current scope.
*/
public String addCurrentScopeToName(String name) {
String currScope = currentScopeAsString();
return addScopeToName(currScope, name);
}
/** given a name for a type, append it with the
given scope. MBZ
*/
public String addScopeToName(String scope, String name) {
if(scope == null || scope.length() > 0)
return scope + "::" + name;
else
return name;
}
/** remove one level of scope from name MBZ*/
public String removeOneLevelScope(String scopeName) {
int index = scopeName.lastIndexOf("::");
if (index > 0) {
return scopeName.substring(0,index);
}
if (scopeName.length() > 0) {
return "";
}
return null;
}
/** add a node to the table with it's key as
the current scope and the name */
public TNode add(String name, TNode node) {
return (TNode)symTable.put(addCurrentScopeToName(name),node);
}
/** lookup a fully scoped name in the symbol table */
public TNode lookupScopedName(String scopedName) {
return (TNode)symTable.get(scopedName);
}
/** lookup an unscoped name in the table by prepending
the current scope.
MBZ -- if not found, pop scopes and look again
*/
public TNode lookupNameInCurrentScope(String name) {
String scope = currentScopeAsString();
String scopedName;
TNode tnode = null;
//System.out.println( "\n"+ this.toString() );
while (tnode == null && scope != null) {
scopedName = addScopeToName(scope, name);
//System.out.println("lookup trying " + scopedName);
tnode = (TNode)symTable.get(scopedName);
scope = removeOneLevelScope(scope);
}
return tnode;
}
/** convert this table to a string */
public String toString() {
StringBuffer buff = new StringBuffer(300);
buff.append("CSymbolTable { \nCurrentScope: " + currentScopeAsString() +
"\nDefinedSymbols:\n");
Enumeration ke = symTable.keys();
Enumeration ve = symTable.elements();
while(ke.hasMoreElements()) {
buff.append(ke.nextElement().toString() + " (" +
TNode.getNameForType(((TNode)ve.nextElement()).getType()) + ")\n");
}
buff.append("}\n");
return buff.toString();
}
};

32
app/preproc/CToken.java Executable file
View File

@ -0,0 +1,32 @@
package processing.app.preproc;
import antlr.CommonToken;
public class CToken extends antlr.CommonToken {
String source = "";
int tokenNumber;
public String getSource()
{
return source;
}
public void setSource(String src)
{
source = src;
}
public int getTokenNumber()
{
return tokenNumber;
}
public void setTokenNumber(int i)
{
tokenNumber = i;
}
public String toString() {
return "CToken:" +"(" + hashCode() + ")" + "[" + getType() + "] "+ getText() + " line:" + getLine() + " source:" + source ;
}
}

15
app/preproc/CVS/Entries Normal file
View File

@ -0,0 +1,15 @@
/CSymbolTable.java/1.1.1.1/Thu Sep 22 15:32:54 2005//
/CToken.java/1.1.1.1/Thu Sep 22 15:32:54 2005//
/ExtendedCommonASTWithHiddenTokens.java/1.1.1.1/Thu Sep 22 15:32:55 2005//
/LineObject.java/1.1.1.1/Thu Sep 22 15:32:55 2005//
/Makefile/1.1.1.1/Thu Sep 22 15:32:55 2005//
/PreprocessorInfoChannel.java/1.1.1.1/Thu Sep 22 15:32:55 2005//
/StdCParser.g/1.1.1.1/Thu Sep 22 15:32:56 2005//
/TNode.java/1.1.1.1/Thu Sep 22 15:32:56 2005//
/TNodeFactory.java/1.1.1.1/Thu Sep 22 15:32:56 2005//
/WEmitter.g/1.4/Thu Sep 22 15:32:56 2005//
/WParser.g/1.4/Thu Sep 22 15:32:57 2005//
/WTreeParser.g/1.2/Thu Sep 22 15:32:57 2005//
/whitespace_test.pde/1.1.1.1/Thu Sep 22 15:32:56 2005//
/PdePreprocessor.java/1.13/Sat Sep 24 11:26:19 2005//
D

View File

@ -0,0 +1 @@
wiring/app/preproc

1
app/preproc/CVS/Root Normal file
View File

@ -0,0 +1 @@
:ext:dmellis@wcvs.uniandes.edu.co:/home/cvs/cvsrep

2
app/preproc/ExtendedCommonASTWithHiddenTokens.java Normal file → Executable file
View File

@ -4,7 +4,7 @@ package antlr;
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
* $Id: ExtendedCommonASTWithHiddenTokens.java,v 1.1 2005/04/09 02:30:36 benfry Exp $
* $Id: ExtendedCommonASTWithHiddenTokens.java,v 1.1.1.1 2005/06/22 22:18:14 h Exp $
*/
import java.io.*;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,157 +0,0 @@
// $ANTLR 2.7.2: "java.g" -> "JavaLexer.java"$
package antlr.java;
public interface JavaTokenTypes {
int EOF = 1;
int NULL_TREE_LOOKAHEAD = 3;
int BLOCK = 4;
int MODIFIERS = 5;
int OBJBLOCK = 6;
int SLIST = 7;
int CTOR_DEF = 8;
int METHOD_DEF = 9;
int VARIABLE_DEF = 10;
int INSTANCE_INIT = 11;
int STATIC_INIT = 12;
int TYPE = 13;
int CLASS_DEF = 14;
int INTERFACE_DEF = 15;
int PACKAGE_DEF = 16;
int ARRAY_DECLARATOR = 17;
int EXTENDS_CLAUSE = 18;
int IMPLEMENTS_CLAUSE = 19;
int PARAMETERS = 20;
int PARAMETER_DEF = 21;
int LABELED_STAT = 22;
int TYPECAST = 23;
int INDEX_OP = 24;
int POST_INC = 25;
int POST_DEC = 26;
int METHOD_CALL = 27;
int EXPR = 28;
int ARRAY_INIT = 29;
int IMPORT = 30;
int UNARY_MINUS = 31;
int UNARY_PLUS = 32;
int CASE_GROUP = 33;
int ELIST = 34;
int FOR_INIT = 35;
int FOR_CONDITION = 36;
int FOR_ITERATOR = 37;
int EMPTY_STAT = 38;
int FINAL = 39;
int ABSTRACT = 40;
int STRICTFP = 41;
int SUPER_CTOR_CALL = 42;
int CTOR_CALL = 43;
int LITERAL_package = 44;
int SEMI = 45;
int LITERAL_import = 46;
int LBRACK = 47;
int RBRACK = 48;
int LITERAL_void = 49;
int LITERAL_boolean = 50;
int LITERAL_byte = 51;
int LITERAL_char = 52;
int LITERAL_short = 53;
int LITERAL_int = 54;
int LITERAL_float = 55;
int LITERAL_long = 56;
int LITERAL_double = 57;
int IDENT = 58;
int DOT = 59;
int STAR = 60;
int LITERAL_private = 61;
int LITERAL_public = 62;
int LITERAL_protected = 63;
int LITERAL_static = 64;
int LITERAL_transient = 65;
int LITERAL_native = 66;
int LITERAL_threadsafe = 67;
int LITERAL_synchronized = 68;
int LITERAL_volatile = 69;
int LITERAL_class = 70;
int LITERAL_extends = 71;
int LITERAL_interface = 72;
int LCURLY = 73;
int RCURLY = 74;
int COMMA = 75;
int LITERAL_implements = 76;
int LPAREN = 77;
int RPAREN = 78;
int LITERAL_this = 79;
int LITERAL_super = 80;
int ASSIGN = 81;
int LITERAL_throws = 82;
int COLON = 83;
int LITERAL_if = 84;
int LITERAL_else = 85;
int LITERAL_for = 86;
int LITERAL_while = 87;
int LITERAL_do = 88;
int LITERAL_break = 89;
int LITERAL_continue = 90;
int LITERAL_return = 91;
int LITERAL_switch = 92;
int LITERAL_throw = 93;
int LITERAL_assert = 94;
int LITERAL_case = 95;
int LITERAL_default = 96;
int LITERAL_try = 97;
int LITERAL_finally = 98;
int LITERAL_catch = 99;
int PLUS_ASSIGN = 100;
int MINUS_ASSIGN = 101;
int STAR_ASSIGN = 102;
int DIV_ASSIGN = 103;
int MOD_ASSIGN = 104;
int SR_ASSIGN = 105;
int BSR_ASSIGN = 106;
int SL_ASSIGN = 107;
int BAND_ASSIGN = 108;
int BXOR_ASSIGN = 109;
int BOR_ASSIGN = 110;
int QUESTION = 111;
int LOR = 112;
int LAND = 113;
int BOR = 114;
int BXOR = 115;
int BAND = 116;
int NOT_EQUAL = 117;
int EQUAL = 118;
int LT = 119;
int GT = 120;
int LE = 121;
int GE = 122;
int LITERAL_instanceof = 123;
int SL = 124;
int SR = 125;
int BSR = 126;
int PLUS = 127;
int MINUS = 128;
int DIV = 129;
int MOD = 130;
int INC = 131;
int DEC = 132;
int BNOT = 133;
int LNOT = 134;
int LITERAL_true = 135;
int LITERAL_false = 136;
int LITERAL_null = 137;
int LITERAL_new = 138;
int NUM_INT = 139;
int CHAR_LITERAL = 140;
int STRING_LITERAL = 141;
int NUM_FLOAT = 142;
int NUM_LONG = 143;
int NUM_DOUBLE = 144;
int WS = 145;
int SL_COMMENT = 146;
int ML_COMMENT = 147;
int ESC = 148;
int HEX_DIGIT = 149;
int VOCAB = 150;
int EXPONENT = 151;
int FLOAT_SUFFIX = 152;
}

View File

@ -1,151 +0,0 @@
// $ANTLR 2.7.2: java.g -> JavaTokenTypes.txt$
Java // output token vocab name
BLOCK=4
MODIFIERS=5
OBJBLOCK=6
SLIST=7
CTOR_DEF=8
METHOD_DEF=9
VARIABLE_DEF=10
INSTANCE_INIT=11
STATIC_INIT=12
TYPE=13
CLASS_DEF=14
INTERFACE_DEF=15
PACKAGE_DEF=16
ARRAY_DECLARATOR=17
EXTENDS_CLAUSE=18
IMPLEMENTS_CLAUSE=19
PARAMETERS=20
PARAMETER_DEF=21
LABELED_STAT=22
TYPECAST=23
INDEX_OP=24
POST_INC=25
POST_DEC=26
METHOD_CALL=27
EXPR=28
ARRAY_INIT=29
IMPORT=30
UNARY_MINUS=31
UNARY_PLUS=32
CASE_GROUP=33
ELIST=34
FOR_INIT=35
FOR_CONDITION=36
FOR_ITERATOR=37
EMPTY_STAT=38
FINAL="final"=39
ABSTRACT="abstract"=40
STRICTFP="strictfp"=41
SUPER_CTOR_CALL=42
CTOR_CALL=43
LITERAL_package="package"=44
SEMI=45
LITERAL_import="import"=46
LBRACK=47
RBRACK=48
LITERAL_void="void"=49
LITERAL_boolean="boolean"=50
LITERAL_byte="byte"=51
LITERAL_char="char"=52
LITERAL_short="short"=53
LITERAL_int="int"=54
LITERAL_float="float"=55
LITERAL_long="long"=56
LITERAL_double="double"=57
IDENT=58
DOT=59
STAR=60
LITERAL_private="private"=61
LITERAL_public="public"=62
LITERAL_protected="protected"=63
LITERAL_static="static"=64
LITERAL_transient="transient"=65
LITERAL_native="native"=66
LITERAL_threadsafe="threadsafe"=67
LITERAL_synchronized="synchronized"=68
LITERAL_volatile="volatile"=69
LITERAL_class="class"=70
LITERAL_extends="extends"=71
LITERAL_interface="interface"=72
LCURLY=73
RCURLY=74
COMMA=75
LITERAL_implements="implements"=76
LPAREN=77
RPAREN=78
LITERAL_this="this"=79
LITERAL_super="super"=80
ASSIGN=81
LITERAL_throws="throws"=82
COLON=83
LITERAL_if="if"=84
LITERAL_else="else"=85
LITERAL_for="for"=86
LITERAL_while="while"=87
LITERAL_do="do"=88
LITERAL_break="break"=89
LITERAL_continue="continue"=90
LITERAL_return="return"=91
LITERAL_switch="switch"=92
LITERAL_throw="throw"=93
LITERAL_assert="assert"=94
LITERAL_case="case"=95
LITERAL_default="default"=96
LITERAL_try="try"=97
LITERAL_finally="finally"=98
LITERAL_catch="catch"=99
PLUS_ASSIGN=100
MINUS_ASSIGN=101
STAR_ASSIGN=102
DIV_ASSIGN=103
MOD_ASSIGN=104
SR_ASSIGN=105
BSR_ASSIGN=106
SL_ASSIGN=107
BAND_ASSIGN=108
BXOR_ASSIGN=109
BOR_ASSIGN=110
QUESTION=111
LOR=112
LAND=113
BOR=114
BXOR=115
BAND=116
NOT_EQUAL=117
EQUAL=118
LT=119
GT=120
LE=121
GE=122
LITERAL_instanceof="instanceof"=123
SL=124
SR=125
BSR=126
PLUS=127
MINUS=128
DIV=129
MOD=130
INC=131
DEC=132
BNOT=133
LNOT=134
LITERAL_true="true"=135
LITERAL_false="false"=136
LITERAL_null="null"=137
LITERAL_new="new"=138
NUM_INT=139
CHAR_LITERAL=140
STRING_LITERAL=141
NUM_FLOAT=142
NUM_LONG=143
NUM_DOUBLE=144
WS=145
SL_COMMENT=146
ML_COMMENT=147
ESC=148
HEX_DIGIT=149
VOCAB=150
EXPONENT=151
FLOAT_SUFFIX=152

126
app/preproc/LineObject.java Executable file
View File

@ -0,0 +1,126 @@
package processing.app.preproc;
class LineObject {
LineObject parent = null;
String source = "";
int line = 1;
boolean enteringFile = false;
boolean returningToFile = false;
boolean systemHeader = false;
boolean treatAsC = false;
public LineObject()
{
super();
}
public LineObject( LineObject lobj )
{
parent = lobj.getParent();
source = lobj.getSource();
line = lobj.getLine();
enteringFile = lobj.getEnteringFile();
returningToFile = lobj.getReturningToFile();
systemHeader = lobj.getSystemHeader();
treatAsC = lobj.getTreatAsC();
}
public LineObject( String src)
{
source = src;
}
public void setSource(String src)
{
source = src;
}
public String getSource()
{
return source;
}
public void setParent(LineObject par)
{
parent = par;
}
public LineObject getParent()
{
return parent;
}
public void setLine(int l)
{
line = l;
}
public int getLine()
{
return line;
}
public void newline()
{
line++;
}
public void setEnteringFile(boolean v)
{
enteringFile = v;
}
public boolean getEnteringFile()
{
return enteringFile;
}
public void setReturningToFile(boolean v)
{
returningToFile = v;
}
public boolean getReturningToFile()
{
return returningToFile;
}
public void setSystemHeader(boolean v)
{
systemHeader = v;
}
public boolean getSystemHeader()
{
return systemHeader;
}
public void setTreatAsC(boolean v)
{
treatAsC = v;
}
public boolean getTreatAsC()
{
return treatAsC;
}
public String toString() {
StringBuffer ret;
ret = new StringBuffer("# " + line + " \"" + source + "\"");
if (enteringFile) {
ret.append(" 1");
}
if (returningToFile) {
ret.append(" 2");
}
if (systemHeader) {
ret.append(" 3");
}
if (treatAsC) {
ret.append(" 4");
}
return ret.toString();
}
}

36
app/preproc/Makefile Executable file
View File

@ -0,0 +1,36 @@
classfiles = LineObject.class PreprocessorInfoChannel.class StdCParser.class StdCLexer.class WParser.class WLexer.class WTreeParser.class WEmitter.class
javafiles = CSymbolTable.java TNode.java TNodeFactory.java CToken.java LineObject.java PreprocessorInfoChannel.java StdCParser.java StdCLexer.java WParser.java WLexer.java WTreeParser.java WEmitter.java
all : $(javafiles) $(classfiles)
clean :
StdCParser.java StdCLexer.java : StdCParser.g
java -cp "..\\..\\build\\windows\\work\\lib\\antlr.jar" antlr.Tool StdCParser.g
WParser.java WLexer.java : WParser.g WParser.g
java -cp "..\\..\\build\\windows\\work\\lib\\antlr.jar" antlr.Tool -glib "StdCParser.g" WParser.g
WTreeParser.java : WTreeParser.g
java -cp "..\\..\\build\\windows\\work\\lib\\antlr.jar" antlr.Tool WTreeParser.g
WEmitter.java : WEmitter.g WTreeParser.g
java -cp "..\\..\\build\\windows\\work\\lib\\antlr.jar" antlr.Tool -glib "WTreeParser.g" WEmitter.g
.SUFFIXES: .java .class
.java.class :
../../build/windows/work/jikes -cp "..\\..\\build\\windows\\work\\java\\lib\\rt.jar;..\\..\\build\\windows\\work\\lib\\mrj.jar;..\\..\\build\\windows\\work\\lib\\antlr.jar;." $<

View File

@ -1,922 +0,0 @@
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
package processing.app.preproc;
import processing.app.*;
/* Based on original code copyright (c) 2003 Andy Tripp <atripp@comcast.net>.
* shipped under GPL with permission.
*/
import antlr.*;
import antlr.collections.*;
import antlr.collections.impl.*;
import java.io.*;
import java.util.*;
/**
* PDEEmitter: A class that can take an ANTLR Java AST and produce
* reasonably formatted Java code from it. To use it, create a
* PDEEmitter object, call setOut() if you want to print to something
* other than System.out, and then call print(), passing the
* AST. Typically, the AST node that you pass would be the root of a
* tree - the ROOT_ID node that represents a Java file.
*/
public class PdeEmitter implements PdeTokenTypes
{
private PrintStream out = System.out;
private PrintStream debug = System.err;
private static int ALL = -1;
private java.util.Stack stack = new java.util.Stack();
private static String[] tokenNames;
private final static int ROOT_ID = 0;
static {
setupTokenNames();
}
/*
private static Hashtable publicMethods;
private static final String publicMethodList[] = {
"setup", "draw", //"loop",
"mousePressed", "mouseReleased", "mouseClicked",
"mouseEntered", "mouseExited",
"mouseMoved", "mouseDragged",
"keyPressed", "keyReleased", "keyTyped"
};
static {
publicMethods = new Hashtable();
for (int i = 0; i < publicMethodList.length; i++) {
publicMethods.put(publicMethodList[i], new Object());
}
}
*/
// Map each AST token type to a String
private static void setupTokenNames() {
tokenNames = new String[200];
for (int i=0; i<tokenNames.length; i++) {
tokenNames[i] = "ERROR:" + i;
}
tokenNames[POST_INC]="++";
tokenNames[POST_DEC]="--";
tokenNames[UNARY_MINUS]="-";
tokenNames[UNARY_PLUS]="+";
tokenNames[STAR]="*";
tokenNames[ASSIGN]="=";
tokenNames[PLUS_ASSIGN]="+=";
tokenNames[MINUS_ASSIGN]="-=";
tokenNames[STAR_ASSIGN]="*=";
tokenNames[DIV_ASSIGN]="/=";
tokenNames[MOD_ASSIGN]="%=";
tokenNames[SR_ASSIGN]=">>=";
tokenNames[BSR_ASSIGN]=">>>=";
tokenNames[SL_ASSIGN]="<<=";
tokenNames[BAND_ASSIGN]="&=";
tokenNames[BXOR_ASSIGN]="^=";
tokenNames[BOR_ASSIGN]="|=";
tokenNames[QUESTION]="?";
tokenNames[LOR]="||";
tokenNames[LAND]="&&";
tokenNames[BOR]="|";
tokenNames[BXOR]="^";
tokenNames[BAND]="&";
tokenNames[NOT_EQUAL]="!=";
tokenNames[EQUAL]="==";
tokenNames[LT]="<";
tokenNames[GT]=">";
tokenNames[LE]="<=";
tokenNames[GE]=">=";
tokenNames[SL]="<<";
tokenNames[SR]=">>";
tokenNames[BSR]=">>>";
tokenNames[PLUS]="+";
tokenNames[MINUS]="-";
tokenNames[DIV]="/";
tokenNames[MOD]="%";
tokenNames[INC]="++";
tokenNames[DEC]="--";
tokenNames[BNOT]="~";
tokenNames[LNOT]="!";
tokenNames[FINAL]="final";
tokenNames[ABSTRACT]="abstract";
tokenNames[LITERAL_package]="package";
tokenNames[LITERAL_import]="import";
tokenNames[LITERAL_void]="void";
tokenNames[LITERAL_boolean]="boolean";
tokenNames[LITERAL_byte]="byte";
tokenNames[LITERAL_char]="char";
tokenNames[LITERAL_short]="short";
tokenNames[LITERAL_int]="int";
tokenNames[LITERAL_float]="float";
tokenNames[LITERAL_long]="long";
tokenNames[LITERAL_double]="double";
tokenNames[LITERAL_private]="private";
tokenNames[LITERAL_public]="public";
tokenNames[LITERAL_protected]="protected";
tokenNames[LITERAL_static]="static";
tokenNames[LITERAL_transient]="transient";
tokenNames[LITERAL_native]="native";
tokenNames[LITERAL_threadsafe]="threadsafe";
tokenNames[LITERAL_synchronized]="synchronized";
tokenNames[LITERAL_volatile]="volatile";
tokenNames[LITERAL_class]="class";
tokenNames[LITERAL_extends]="extends";
tokenNames[LITERAL_interface]="interface";
tokenNames[LITERAL_implements]="implements";
tokenNames[LITERAL_throws]="throws";
tokenNames[LITERAL_if]="if";
tokenNames[LITERAL_else]="else";
tokenNames[LITERAL_for]="for";
tokenNames[LITERAL_while]="while";
tokenNames[LITERAL_do]="do";
tokenNames[LITERAL_break]="break";
tokenNames[LITERAL_continue]="continue";
tokenNames[LITERAL_return]="return";
tokenNames[LITERAL_switch]="switch";
tokenNames[LITERAL_throw]="throw";
tokenNames[LITERAL_case]="case";
tokenNames[LITERAL_default]="default";
tokenNames[LITERAL_try]="try";
tokenNames[LITERAL_finally]="finally";
tokenNames[LITERAL_catch]="catch";
tokenNames[LITERAL_instanceof]="instanceof";
tokenNames[LITERAL_this]="this";
tokenNames[LITERAL_super]="super";
tokenNames[LITERAL_true]="true";
tokenNames[LITERAL_false]="false";
tokenNames[LITERAL_null]="null";
tokenNames[LITERAL_new]="new";
tokenNames[LITERAL_color]="int"; // PDE specific alias
}
/**
* Specify a PrintStream to print to. System.out is the default.
* @param out the PrintStream to print to
*/
public void setOut(PrintStream out) {
this.out = out;
}
private String name(AST ast) {
return tokenNames[ast.getType()];
}
private String name(int type) {
return tokenNames[type];
}
/**
* Find a child of the given AST that has the given type
* @returns a child AST of the given type. If it can't find a child of the
* given type, return null.
*/
private AST getChild(AST ast, int childType) {
AST child = ast.getFirstChild();
while (child != null) {
if (child.getType() == childType) {
// debug.println("getChild: found:" + name(ast));
return child;
}
child = child.getNextSibling();
}
return null;
}
/**
* Dump the list of hidden tokens linked to after the AST node passed in.
* Most hidden tokens are dumped from this function.
*/
private void dumpHiddenAfter(AST ast) {
dumpHiddenTokens(((antlr.CommonASTWithHiddenTokens)ast).getHiddenAfter());
}
/**
* Dump the list of hidden tokens linked to before the AST node passed in.
* The only time hidden tokens need to be dumped with this function is when
* dealing parts of the tree where automatic tree construction was
* turned off with the ! operator in the grammar file and the nodes were
* manually constructed in such a way that the usual tokens don't have the
* necessary hiddenAfter links.
*/
private void dumpHiddenBefore(AST ast) {
antlr.CommonHiddenStreamToken
child = null,
parent = ((antlr.CommonASTWithHiddenTokens)ast).getHiddenBefore();
// if there aren't any hidden tokens here, quietly return
//
if (parent == null) {
return;
}
// traverse back to the head of the list of tokens before this node
do {
child = parent;
parent = child.getHiddenBefore();
} while (parent != null);
// dump that list
dumpHiddenTokens(child);
}
/**
* Dump the list of hidden tokens linked to from the token passed in.
*/
private void dumpHiddenTokens(antlr.CommonHiddenStreamToken t) {
for ( ; t != null ; t=PdePreprocessor.filter.getHiddenAfter(t) ) {
out.print(t.getText());
}
}
/**
* Print the children of the given AST
* @param ast The AST to print
* @returns true iff anything was printed
*/
private boolean printChildren(AST ast) throws RunnerException {
boolean ret = false;
AST child = ast.getFirstChild();
while (child != null) {
ret = true;
print(child);
child = child.getNextSibling();
}
return ret;
}
/**
* Tells whether an AST has any children or not.
* @return true iff the AST has at least one child
*/
private boolean hasChildren(AST ast) {
return (ast.getFirstChild() != null);
}
/**
* Gets the best node in the subtree for printing. This really means
* the next node which could potentially have hiddenBefore data. It's
* usually the first printable leaf, but not always.
*
* @param includeThisNode Should this node be included in the search?
* If false, only descendants are searched.
*
* @return the first printable leaf node in an AST
*/
private AST getBestPrintableNode(AST ast, boolean includeThisNode) {
AST child;
if (includeThisNode) {
child = ast;
} else {
child = ast.getFirstChild();
}
if (child != null) {
switch (child.getType()) {
// the following node types are printing nodes that print before
// any children, but then also recurse over children. So they
// may have hiddenBefore chains that need to be printed first. Many
// statements and all unary expression types qualify. Return these
// nodes directly
case CLASS_DEF:
case LITERAL_if:
case LITERAL_for:
case LITERAL_while:
case LITERAL_do:
case LITERAL_break:
case LITERAL_continue:
case LITERAL_return:
case LITERAL_switch:
case LITERAL_try:
case LITERAL_throw:
case LITERAL_synchronized:
case LITERAL_assert:
case BNOT:
case LNOT:
case INC:
case DEC:
case UNARY_MINUS:
case UNARY_PLUS:
return child;
// Some non-terminal node types (at the moment, I only know of
// MODIFIERS, but there may be other such types), can be
// leaves in the tree but not have any children. If this is
// such a node, move on to the next sibling.
case MODIFIERS:
if (child.getFirstChild() == null ) {
return getBestPrintableNode(child.getNextSibling(), false);
}
// new jikes doesn't like fallthrough, so just duplicated here:
return getBestPrintableNode(child, false);
default:
return getBestPrintableNode(child, false);
}
}
return ast;
}
/**
* Prints a binary operator
*/
private void printBinaryOperator(AST ast) throws RunnerException {
print(ast.getFirstChild());
out.print(name(ast));
dumpHiddenAfter(ast);
print(ast.getFirstChild().getNextSibling());
}
/**
* Print the given AST. Call this function to print your PDE code.
*
* It works by making recursive calls to print children.
* So the code below is one big "switch" statement on the passed AST type.
*/
public void print (AST ast) throws RunnerException {
if (ast == null) {
return;
}
AST parent = null;
if (!stack.isEmpty()) {
parent = (AST) stack.peek();
}
stack.push(ast);
AST child1 = ast.getFirstChild();
AST child2 = null;
AST child3 = null;
if (child1 != null) {
child2 = child1.getNextSibling();
if (child2 != null) {
child3 = child2.getNextSibling();
}
}
switch(ast.getType()) {
// The top of the tree looks like this:
// ROOT_ID "Whatever.java"
// package
// imports
// class definition
case ROOT_ID:
dumpHiddenTokens(PdePreprocessor.filter.getInitialHiddenToken());
printChildren(ast);
break;
// supporting a "package" statement in a PDE program has
// a bunch of issues with it that need to dealt in the compilation
// code too, so this isn't actually tested.
case PACKAGE_DEF:
out.print("package");
dumpHiddenAfter(ast);
print(ast.getFirstChild());
break;
// IMPORT has exactly one child
case IMPORT:
out.print("import");
dumpHiddenAfter(ast);
print(ast.getFirstChild());
break;
case CLASS_DEF:
case INTERFACE_DEF:
print(getChild(ast, MODIFIERS));
if (ast.getType() == CLASS_DEF) {
out.print("class");
} else {
out.print("interface");
}
dumpHiddenBefore(getChild(ast, IDENT));
print(getChild(ast, IDENT));
print(getChild(ast, EXTENDS_CLAUSE));
print(getChild(ast, IMPLEMENTS_CLAUSE));
print(getChild(ast, OBJBLOCK));
break;
case EXTENDS_CLAUSE:
if (hasChildren(ast)) {
out.print("extends");
dumpHiddenBefore(getBestPrintableNode(ast, false));
printChildren(ast);
}
break;
case IMPLEMENTS_CLAUSE:
if (hasChildren(ast)) {
out.print("implements");
dumpHiddenBefore(getBestPrintableNode(ast, false));
printChildren(ast);
}
break;
// DOT always has exactly two children.
case DOT:
print(child1);
out.print(".");
dumpHiddenAfter(ast);
print(child2);
break;
case MODIFIERS:
case OBJBLOCK:
case CTOR_DEF:
//case METHOD_DEF:
case PARAMETERS:
case PARAMETER_DEF:
case VARIABLE_DEF:
case TYPE:
case SLIST:
case ELIST:
case ARRAY_DECLARATOR:
case TYPECAST:
case EXPR:
case ARRAY_INIT:
case FOR_INIT:
case FOR_CONDITION:
case FOR_ITERATOR:
case METHOD_CALL:
case INSTANCE_INIT:
case INDEX_OP:
case SUPER_CTOR_CALL:
case CTOR_CALL:
printChildren(ast);
break;
case METHOD_DEF:
// kids seem to be: MODIFIERS TYPE setup PARAMETERS
//AST parent = (AST) stack.peek();
AST modifiersChild = ast.getFirstChild();
AST typeChild = modifiersChild.getNextSibling();
AST methodNameChild = typeChild.getNextSibling();
AST parametersChild = methodNameChild.getNextSibling();
// to output, use print(child) on each of the four
/*
// 1. figure out if this is setup, draw, or loop
String methodName = methodNameChild.getText();
if (publicMethods.get(methodName) != null) {
// make sure this feller is public
boolean foundPublic = false;
AST child = modifiersChild.getFirstChild();
while (child != null) {
if (child.getText().equals("public")) {
foundPublic = true;
child = null;
} else {
//out.print("." + child.getText() + ".");
child = child.getNextSibling();
}
}
if (!foundPublic) {
out.print("public ");
}
*/
// if this method doesn't have a specifier, make it public
// (useful for setup/keyPressed/etc)
boolean foundSpecifier = false;
AST child = modifiersChild.getFirstChild();
while (child != null) {
String childText = child.getText();
if (childText.equals("public") ||
childText.equals("protected") ||
childText.equals("private")) {
foundSpecifier = true;
child = null;
} else {
//out.print("." + child.getText() + ".");
child = child.getNextSibling();
}
}
if (!foundSpecifier) {
out.print("public ");
}
printChildren(ast); // everything is fine
break;
// if we have two children, it's of the form "a=0"
// if just one child, it's of the form "=0" (where the
// lhs is above this AST).
case ASSIGN:
if (child2 != null) {
print(child1);
out.print("=");
dumpHiddenAfter(ast);
print(child2);
}
else {
out.print("=");
dumpHiddenAfter(ast);
print(child1);
}
break;
// binary operators:
case PLUS:
case MINUS:
case DIV:
case MOD:
case NOT_EQUAL:
case EQUAL:
case LT:
case GT:
case LE:
case GE:
case LOR:
case LAND:
case BOR:
case BXOR:
case BAND:
case SL:
case SR:
case BSR:
case LITERAL_instanceof:
case PLUS_ASSIGN:
case MINUS_ASSIGN:
case STAR_ASSIGN:
case DIV_ASSIGN:
case MOD_ASSIGN:
case SR_ASSIGN:
case BSR_ASSIGN:
case SL_ASSIGN:
case BAND_ASSIGN:
case BXOR_ASSIGN:
case BOR_ASSIGN:
printBinaryOperator(ast);
break;
case LITERAL_for:
out.print(name(ast));
dumpHiddenAfter(ast);
printChildren(ast);
break;
case POST_INC:
case POST_DEC:
print(child1);
out.print(name(ast));
dumpHiddenAfter(ast);
break;
// unary operators:
case BNOT:
case LNOT:
case INC:
case DEC:
case UNARY_MINUS:
case UNARY_PLUS:
out.print(name(ast));
dumpHiddenAfter(ast);
print(child1);
break;
case LITERAL_new:
out.print("new");
dumpHiddenAfter(ast);
print(child1);
print(child2);
// "new String[] {...}": the stuff in {} is child3
if (child3 != null) {
print(child3);
}
break;
case LITERAL_return:
out.print("return");
dumpHiddenAfter(ast);
print(child1);
break;
case STATIC_INIT:
out.print("static");
dumpHiddenBefore(getBestPrintableNode(ast, false));
print(child1);
break;
case LITERAL_switch:
out.print("switch");
dumpHiddenAfter(ast);
printChildren(ast);
break;
case CASE_GROUP:
printChildren(ast);
break;
case LITERAL_case:
out.print("case");
dumpHiddenAfter(ast);
printChildren(ast);
break;
case LITERAL_default:
out.print("default");
dumpHiddenAfter(ast);
printChildren(ast);
break;
case NUM_INT:
case CHAR_LITERAL:
case STRING_LITERAL:
case NUM_FLOAT:
out.print(ast.getText());
dumpHiddenAfter(ast);
break;
case LITERAL_private:
case LITERAL_public:
case LITERAL_protected:
case LITERAL_static:
case LITERAL_transient:
case LITERAL_native:
case LITERAL_threadsafe:
case LITERAL_synchronized:
case LITERAL_volatile:
case FINAL:
case ABSTRACT:
case LITERAL_package:
case LITERAL_void:
case LITERAL_boolean:
case LITERAL_byte:
case LITERAL_char:
case LITERAL_short:
case LITERAL_int:
case LITERAL_float:
case LITERAL_long:
case LITERAL_double:
case LITERAL_true:
case LITERAL_false:
case LITERAL_null:
case SEMI:
case LITERAL_this:
case LITERAL_super:
case LITERAL_continue:
case LITERAL_break:
out.print(name(ast));
dumpHiddenAfter(ast);
break;
case EMPTY_STAT:
case EMPTY_FIELD:
break;
// yuck: Distinguish between "import x.y.*" and "x = 1 * 3"
case STAR:
if (hasChildren(ast)) { // the binary mult. operator
printBinaryOperator(ast);
}
else { // the special "*" in import:
out.print("*");
dumpHiddenAfter(ast);
}
break;
case LITERAL_throws:
out.print("throws");
dumpHiddenAfter(ast);
printChildren(ast);
break;
case LITERAL_if:
out.print("if");
dumpHiddenAfter(ast);
print(child1); // the "if" condition: an EXPR
print(child2); // the "then" clause is an SLIST
if (child3 != null) {
out.print("else");
dumpHiddenBefore(getBestPrintableNode(child3, true));
print(child3); // optional "else" clause: an SLIST
}
break;
case LITERAL_while:
out.print("while");
dumpHiddenAfter(ast);
printChildren(ast);
break;
case LITERAL_do:
out.print("do");
dumpHiddenAfter(ast);
print(child1); // an SLIST
out.print("while");
dumpHiddenBefore(getBestPrintableNode(child2, false));
print(child2); // an EXPR
break;
case LITERAL_try:
out.print("try");
dumpHiddenAfter(ast);
printChildren(ast);
break;
case LITERAL_catch:
out.print("catch");
dumpHiddenAfter(ast);
printChildren(ast);
break;
// the first child is the "try" and the second is the SLIST
case LITERAL_finally:
out.print("finally");
dumpHiddenAfter(ast);
printChildren(ast);
break;
case LITERAL_throw:
out.print("throw");
dumpHiddenAfter(ast);
print(child1);
break;
// the dreaded trinary operator
case QUESTION:
print(child1);
out.print("?");
dumpHiddenAfter(ast);
print(child2);
print(child3);
break;
// pde specific or modified tokens start here
// Image -> BImage, Font -> BFont as appropriate
case IDENT:
/*
if (ast.getText().equals("Image") &&
Preferences.getBoolean("preproc.substitute_image")) { //, true)) {
out.print("BImage");
} else if (ast.getText().equals("Font") &&
Preferences.getBoolean("preproc.substitute_font")) { //, true)) {
out.print("BFont");
} else {
*/
out.print(ast.getText());
//}
dumpHiddenAfter(ast);
break;
// the color datatype is just an alias for int
case LITERAL_color:
out.print("int");
dumpHiddenAfter(ast);
break;
case WEBCOLOR_LITERAL:
if (ast.getText().length() != 6) {
System.err.println("Internal error: incorrect length of webcolor " +
"literal should have been detected sooner.");
break;
}
out.print("0xff" + ast.getText());
dumpHiddenAfter(ast);
break;
// allow for stuff like int(43.2).
case CONSTRUCTOR_CAST:
AST nonTerminalTypeNode = child1;
AST terminalTypeNode = child1.getFirstChild();
AST exprToCast = child2;
/*
// if this is a string type, add .valueOf()
if (nonTerminalTypeNode.getType() == PdeRecognizer.TYPE &&
terminalTypeNode.getText().equals("String")) {
out.print(terminalTypeNode.getText() + ".valueOf");
dumpHiddenAfter(terminalTypeNode);
print(exprToCast);
// if the expresion to be cast is a string literal, try and parse it.
//
// ideally, we'd be able to do this for all expressions with a
// string type, not just string literals. however, the parser
// doesn't currently track expression type, and for full
// functionality, we'd need to do semantic analysis to handle
// imports so that we could know the return types of method calls.
//
} else if (exprToCast.getFirstChild().getType() == STRING_LITERAL ) {
switch (terminalTypeNode.getType()) {
case PdeRecognizer.LITERAL_byte:
out.print("Byte.parseByte");
dumpHiddenAfter(terminalTypeNode);
print(exprToCast);
break;
case PdeRecognizer.LITERAL_double:
out.print("(new Double");
dumpHiddenAfter(terminalTypeNode);
out.print(exprToCast.getFirstChild().getText() + ").doubleValue()");
dumpHiddenAfter(exprToCast.getFirstChild());
break;
case PdeRecognizer.LITERAL_float:
out.print("(new Float");
dumpHiddenAfter(terminalTypeNode);
out.print(exprToCast.getFirstChild().getText() + ").floatValue()");
dumpHiddenAfter(exprToCast.getFirstChild());
break;
case PdeRecognizer.LITERAL_int:
case PdeRecognizer.LITERAL_color:
out.print("Integer.parseInt");
dumpHiddenAfter(terminalTypeNode);
print(exprToCast);
break;
case PdeRecognizer.LITERAL_long:
out.print("Long.parseLong");
break;
case PdeRecognizer.LITERAL_short:
out.print("Short.parseShort");
break;
default:
throw new RunnerException(Compiler.SUPER_BADNESS);
}
// for builtin types, use regular casting syntax
} else {
*/
// result of below is (int)(4.0
//out.print("(");
//out.print(terminalTypeNode.getText() + ")"); // typename
//dumpHiddenAfter(terminalTypeNode);
//print(exprToCast);
//}
//out.print("(");
String pooType = terminalTypeNode.getText();
out.print("PApplet.to" +
Character.toUpperCase(pooType.charAt(0)) +
pooType.substring(1));
dumpHiddenAfter(terminalTypeNode); // the left paren
print(exprToCast);
//out.print("x)");
break;
// making floating point literals default to floats, not doubles
case NUM_DOUBLE:
out.print(ast.getText());
if (Preferences.getBoolean("preproc.substitute_floats")) { //, true) ) {
out.print("f");
}
dumpHiddenAfter(ast);
break;
default:
debug.println("Invalid type:" + ast.getType());
break;
/* The following are tokens, but I don't think JavaRecognizer
ever produces an AST with one of these types:
case COMMA:
case LITERAL_implements:
case LITERAL_class:
case LITERAL_extends:
case EOF:
case NULL_TREE_LOOKAHEAD:
case BLOCK:
case LABELED_STAT: // refuse to implement on moral grounds :)
case LITERAL_import:
case LBRACK:
case RBRACK:
case LCURLY:
case RCURLY:
case LPAREN:
case RPAREN:
case LITERAL_else: // else is a child of "if" AST
case COLON: // part of the trinary operator
case WS: // whitespace
case ESC:
case HEX_DIGIT:
case VOCAB:
case EXPONENT: // exponents and float suffixes are left in the NUM_FLOAT
case FLOAT_SUFFIX
*/
}
stack.pop();
}
}

View File

@ -1,163 +0,0 @@
// $ANTLR 2.7.2: "expandedpde.g" -> "PdeRecognizer.java"$
package processing.app.preproc;
import processing.app.*;
public interface PdePartialTokenTypes {
int EOF = 1;
int NULL_TREE_LOOKAHEAD = 3;
int BLOCK = 4;
int MODIFIERS = 5;
int OBJBLOCK = 6;
int SLIST = 7;
int CTOR_DEF = 8;
int METHOD_DEF = 9;
int VARIABLE_DEF = 10;
int INSTANCE_INIT = 11;
int STATIC_INIT = 12;
int TYPE = 13;
int CLASS_DEF = 14;
int INTERFACE_DEF = 15;
int PACKAGE_DEF = 16;
int ARRAY_DECLARATOR = 17;
int EXTENDS_CLAUSE = 18;
int IMPLEMENTS_CLAUSE = 19;
int PARAMETERS = 20;
int PARAMETER_DEF = 21;
int LABELED_STAT = 22;
int TYPECAST = 23;
int INDEX_OP = 24;
int POST_INC = 25;
int POST_DEC = 26;
int METHOD_CALL = 27;
int EXPR = 28;
int ARRAY_INIT = 29;
int IMPORT = 30;
int UNARY_MINUS = 31;
int UNARY_PLUS = 32;
int CASE_GROUP = 33;
int ELIST = 34;
int FOR_INIT = 35;
int FOR_CONDITION = 36;
int FOR_ITERATOR = 37;
int EMPTY_STAT = 38;
int FINAL = 39;
int ABSTRACT = 40;
int STRICTFP = 41;
int SUPER_CTOR_CALL = 42;
int CTOR_CALL = 43;
int LITERAL_package = 44;
int SEMI = 45;
int LITERAL_import = 46;
int LBRACK = 47;
int RBRACK = 48;
int LITERAL_void = 49;
int LITERAL_boolean = 50;
int LITERAL_byte = 51;
int LITERAL_char = 52;
int LITERAL_short = 53;
int LITERAL_int = 54;
int LITERAL_float = 55;
int LITERAL_long = 56;
int LITERAL_double = 57;
int IDENT = 58;
int DOT = 59;
int STAR = 60;
int LITERAL_private = 61;
int LITERAL_public = 62;
int LITERAL_protected = 63;
int LITERAL_static = 64;
int LITERAL_transient = 65;
int LITERAL_native = 66;
int LITERAL_threadsafe = 67;
int LITERAL_synchronized = 68;
int LITERAL_volatile = 69;
int LITERAL_class = 70;
int LITERAL_extends = 71;
int LITERAL_interface = 72;
int LCURLY = 73;
int RCURLY = 74;
int COMMA = 75;
int LITERAL_implements = 76;
int LPAREN = 77;
int RPAREN = 78;
int LITERAL_this = 79;
int LITERAL_super = 80;
int ASSIGN = 81;
int LITERAL_throws = 82;
int COLON = 83;
int LITERAL_if = 84;
int LITERAL_else = 85;
int LITERAL_for = 86;
int LITERAL_while = 87;
int LITERAL_do = 88;
int LITERAL_break = 89;
int LITERAL_continue = 90;
int LITERAL_return = 91;
int LITERAL_switch = 92;
int LITERAL_throw = 93;
int LITERAL_assert = 94;
int LITERAL_case = 95;
int LITERAL_default = 96;
int LITERAL_try = 97;
int LITERAL_finally = 98;
int LITERAL_catch = 99;
int PLUS_ASSIGN = 100;
int MINUS_ASSIGN = 101;
int STAR_ASSIGN = 102;
int DIV_ASSIGN = 103;
int MOD_ASSIGN = 104;
int SR_ASSIGN = 105;
int BSR_ASSIGN = 106;
int SL_ASSIGN = 107;
int BAND_ASSIGN = 108;
int BXOR_ASSIGN = 109;
int BOR_ASSIGN = 110;
int QUESTION = 111;
int LOR = 112;
int LAND = 113;
int BOR = 114;
int BXOR = 115;
int BAND = 116;
int NOT_EQUAL = 117;
int EQUAL = 118;
int LT = 119;
int GT = 120;
int LE = 121;
int GE = 122;
int LITERAL_instanceof = 123;
int SL = 124;
int SR = 125;
int BSR = 126;
int PLUS = 127;
int MINUS = 128;
int DIV = 129;
int MOD = 130;
int INC = 131;
int DEC = 132;
int BNOT = 133;
int LNOT = 134;
int LITERAL_true = 135;
int LITERAL_false = 136;
int LITERAL_null = 137;
int LITERAL_new = 138;
int NUM_INT = 139;
int CHAR_LITERAL = 140;
int STRING_LITERAL = 141;
int NUM_FLOAT = 142;
int NUM_LONG = 143;
int NUM_DOUBLE = 144;
int WS = 145;
int SL_COMMENT = 146;
int ML_COMMENT = 147;
int ESC = 148;
int HEX_DIGIT = 149;
int VOCAB = 150;
int EXPONENT = 151;
int FLOAT_SUFFIX = 152;
int CONSTRUCTOR_CAST = 153;
int EMPTY_FIELD = 154;
int WEBCOLOR_LITERAL = 155;
int LITERAL_color = 156;
}

View File

@ -1,155 +0,0 @@
// $ANTLR 2.7.2: expandedpde.g -> PdePartialTokenTypes.txt$
PdePartial // output token vocab name
BLOCK=4
MODIFIERS=5
OBJBLOCK=6
SLIST=7
CTOR_DEF=8
METHOD_DEF=9
VARIABLE_DEF=10
INSTANCE_INIT=11
STATIC_INIT=12
TYPE=13
CLASS_DEF=14
INTERFACE_DEF=15
PACKAGE_DEF=16
ARRAY_DECLARATOR=17
EXTENDS_CLAUSE=18
IMPLEMENTS_CLAUSE=19
PARAMETERS=20
PARAMETER_DEF=21
LABELED_STAT=22
TYPECAST=23
INDEX_OP=24
POST_INC=25
POST_DEC=26
METHOD_CALL=27
EXPR=28
ARRAY_INIT=29
IMPORT=30
UNARY_MINUS=31
UNARY_PLUS=32
CASE_GROUP=33
ELIST=34
FOR_INIT=35
FOR_CONDITION=36
FOR_ITERATOR=37
EMPTY_STAT=38
FINAL="final"=39
ABSTRACT="abstract"=40
STRICTFP="strictfp"=41
SUPER_CTOR_CALL=42
CTOR_CALL=43
LITERAL_package="package"=44
SEMI=45
LITERAL_import="import"=46
LBRACK=47
RBRACK=48
LITERAL_void="void"=49
LITERAL_boolean="boolean"=50
LITERAL_byte="byte"=51
LITERAL_char="char"=52
LITERAL_short="short"=53
LITERAL_int="int"=54
LITERAL_float="float"=55
LITERAL_long="long"=56
LITERAL_double="double"=57
IDENT=58
DOT=59
STAR=60
LITERAL_private="private"=61
LITERAL_public="public"=62
LITERAL_protected="protected"=63
LITERAL_static="static"=64
LITERAL_transient="transient"=65
LITERAL_native="native"=66
LITERAL_threadsafe="threadsafe"=67
LITERAL_synchronized="synchronized"=68
LITERAL_volatile="volatile"=69
LITERAL_class="class"=70
LITERAL_extends="extends"=71
LITERAL_interface="interface"=72
LCURLY=73
RCURLY=74
COMMA=75
LITERAL_implements="implements"=76
LPAREN=77
RPAREN=78
LITERAL_this="this"=79
LITERAL_super="super"=80
ASSIGN=81
LITERAL_throws="throws"=82
COLON=83
LITERAL_if="if"=84
LITERAL_else="else"=85
LITERAL_for="for"=86
LITERAL_while="while"=87
LITERAL_do="do"=88
LITERAL_break="break"=89
LITERAL_continue="continue"=90
LITERAL_return="return"=91
LITERAL_switch="switch"=92
LITERAL_throw="throw"=93
LITERAL_assert="assert"=94
LITERAL_case="case"=95
LITERAL_default="default"=96
LITERAL_try="try"=97
LITERAL_finally="finally"=98
LITERAL_catch="catch"=99
PLUS_ASSIGN=100
MINUS_ASSIGN=101
STAR_ASSIGN=102
DIV_ASSIGN=103
MOD_ASSIGN=104
SR_ASSIGN=105
BSR_ASSIGN=106
SL_ASSIGN=107
BAND_ASSIGN=108
BXOR_ASSIGN=109
BOR_ASSIGN=110
QUESTION=111
LOR=112
LAND=113
BOR=114
BXOR=115
BAND=116
NOT_EQUAL=117
EQUAL=118
LT=119
GT=120
LE=121
GE=122
LITERAL_instanceof="instanceof"=123
SL=124
SR=125
BSR=126
PLUS=127
MINUS=128
DIV=129
MOD=130
INC=131
DEC=132
BNOT=133
LNOT=134
LITERAL_true="true"=135
LITERAL_false="false"=136
LITERAL_null="null"=137
LITERAL_new="new"=138
NUM_INT=139
CHAR_LITERAL=140
STRING_LITERAL=141
NUM_FLOAT=142
NUM_LONG=143
NUM_DOUBLE=144
WS=145
SL_COMMENT=146
ML_COMMENT=147
ESC=148
HEX_DIGIT=149
VOCAB=150
EXPONENT=151
FLOAT_SUFFIX=152
CONSTRUCTOR_CAST=153
EMPTY_FIELD=154
WEBCOLOR_LITERAL=155
LITERAL_color="color"=156

135
app/preproc/PdePreprocessor.java Normal file → Executable file
View File

@ -2,9 +2,11 @@
/*
PdePreprocessor - wrapper for default ANTLR-generated parser
Part of the Processing project - http://processing.org
Part of the Wiring project - http://wiring.org.co
Copyright (c) 2004-05 Ben Fry and Casey Reas
Copyright (c) 2004-05 Hernando Barragan
Processing version Copyright (c) 2004-05 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
ANTLR-generated parser and several supporting classes written
@ -68,26 +70,28 @@ public class PdePreprocessor {
// used for calling the ASTFactory to get the root node
private static final int ROOT_ID = 0;
// stores number of built user-defined function prototypes
public int prototypeCount = 0;
/**
* These may change in-between (if the prefs panel adds this option)
* so grab them here on construction.
*/
public PdePreprocessor() {
defaultImports[JDK11] =
/* defaultImports[JDK11] =
Base.split(Preferences.get("preproc.imports.jdk11"), ',');
defaultImports[JDK13] =
Base.split(Preferences.get("preproc.imports.jdk13"), ',');
defaultImports[JDK14] =
Base.split(Preferences.get("preproc.imports.jdk14"), ',');
}
*/ }
/**
* Used by PdeEmitter.dumpHiddenTokens()
*/
public static TokenStreamCopyingHiddenTokenFilter filter;
//public static TokenStreamCopyingHiddenTokenFilter filter;
/**
@ -159,6 +163,7 @@ public class PdePreprocessor {
return null;
}
/*
do {
PatternMatcherInput input = new PatternMatcherInput(program);
if (!matcher.contains(input, pattern)) break;
@ -179,6 +184,7 @@ public class PdePreprocessor {
//System.out.println("removing " + piece);
} while (true);
*/
extraImports = new String[imports.size()];
imports.copyInto(extraImports);
@ -215,43 +221,85 @@ public class PdePreprocessor {
// do this after the program gets re-combobulated
this.programReader = new StringReader(program);
this.buildPath = buildPath;
// create function prototypes
mess = "^(\\w+)\\s+(\\w+)\\s*\\(([^)]*)\\)\\s*{";
pattern = null;
try {
pattern = compiler.compile(mess);
} catch (MalformedPatternException e) {
e.printStackTrace();
return null;
}
PatternMatcherInput input = new PatternMatcherInput(program);
MatchResult result;
String returntype, functioname, parameterlist, prototype;
java.util.LinkedList prototypes = new java.util.LinkedList();
//System.out.println("prototypes:");
if (Preferences.get("build.extension").equals("cpp")) {
while(matcher.contains(input, pattern)){
result = matcher.getMatch();
//System.out.println(result);
returntype = result.group(1).toString();
functioname = result.group(2).toString();
parameterlist = result.group(3).toString().replace('\n', ' ');
prototype = returntype + " " + functioname + "(" + parameterlist + ");";
if(0 == functioname.compareTo("setup")){
continue;
}
if(0 == functioname.compareTo("loop")){
continue;
}
prototypes.add(prototype);
//System.out.println(prototype);
}
}
// store # of prototypes so that line number reporting can be adjusted
prototypeCount = prototypes.size();
// create a lexer with the stream reader, and tell it to handle
// hidden tokens (eg whitespace, comments) since we want to pass these
// through so that the line numbers when the compiler reports errors
// match those that will be highlighted in the PDE IDE
//
PdeLexer lexer = new PdeLexer(programReader);
lexer.setTokenObjectClass("antlr.CommonHiddenStreamToken");
WLexer lexer = new WLexer(programReader);
//lexer.setTokenObjectClass("antlr.CommonHiddenStreamToken");
lexer.setTokenObjectClass("processing.app.preproc.CToken");
lexer.initialize();
// create the filter for hidden tokens and specify which tokens to
// hide and which to copy to the hidden text
//
filter = new TokenStreamCopyingHiddenTokenFilter(lexer);
filter.hide(PdeRecognizer.SL_COMMENT);
filter.hide(PdeRecognizer.ML_COMMENT);
filter.hide(PdeRecognizer.WS);
filter.copy(PdeRecognizer.SEMI);
filter.copy(PdeRecognizer.LPAREN);
filter.copy(PdeRecognizer.RPAREN);
filter.copy(PdeRecognizer.LCURLY);
filter.copy(PdeRecognizer.RCURLY);
filter.copy(PdeRecognizer.COMMA);
filter.copy(PdeRecognizer.RBRACK);
filter.copy(PdeRecognizer.LBRACK);
filter.copy(PdeRecognizer.COLON);
/*filter = new TokenStreamCopyingHiddenTokenFilter(lexer);
filter.hide(WParser.CPPComment);
filter.hide(WParser.Comment);
filter.hide(WParser.Whitespace);
filter.copy(WParser.SEMI);
filter.copy(WParser.LPAREN);
filter.copy(WParser.RPAREN);
filter.copy(WParser.LCURLY);
filter.copy(WParser.RCURLY);
filter.copy(WParser.COMMA);
filter.copy(WParser.RBRACK);
filter.copy(WParser.LBRACK);
filter.copy(WParser.COLON);
*/
// create a parser and set what sort of AST should be generated
//
PdeRecognizer parser = new PdeRecognizer(filter);
//PdeRecognizer parser = new PdeRecognizer(filter);
WParser parser = new WParser(lexer);
// use our extended AST class
//
parser.setASTNodeClass("antlr.ExtendedCommonASTWithHiddenTokens");
//parser.setASTNodeClass("antlr.ExtendedCommonASTWithHiddenTokens");
parser.setASTNodeType(TNode.class.getName());
TNode.setTokenVocabulary("processing.app.preproc.WTokenTypes");
// start parsing at the compilationUnit non-terminal
//
parser.pdeProgram();
//parser.pdeProgram();
parser.translationUnit();
// set up the AST for traversal by PdeEmitter
//
@ -280,15 +328,17 @@ public class PdePreprocessor {
// output the code
//
PdeEmitter emitter = new PdeEmitter();
File streamFile = new File(buildPath, name + ".java");
WEmitter emitter = new WEmitter(lexer.getPreprocessorInfoChannel());
File streamFile = new File(buildPath, name + "." + Preferences.get("build.extension"));
PrintStream stream = new PrintStream(new FileOutputStream(streamFile));
//writeHeader(stream, extraImports, name);
writeHeader(stream, name);
writeHeader(stream, name, prototypes);
emitter.setASTNodeType(TNode.class.getName());
emitter.setOut(stream);
emitter.print(rootNode);
emitter.printDeclarations(rootNode);
//emitter.print(rootNode);
emitter.translationUnit(parser.getAST());
writeFooter(stream);
stream.close();
@ -313,7 +363,6 @@ public class PdePreprocessor {
return name;
}
/**
* Write any required header material (eg imports, class decl stuff)
*
@ -321,15 +370,18 @@ public class PdePreprocessor {
* @param exporting Is this being exported from PDE?
* @param name Name of the class being created.
*/
void writeHeader(PrintStream out, String className) {
void writeHeader(PrintStream out, String className, java.util.LinkedList prototypes) {
out.print("#include \"WProgram.h\"\n");
// must include processing.core
out.print("import processing.core.*; ");
// print user defined prototypes
while(0 < prototypes.size()){
out.print(prototypes.removeFirst() + "\n");
}
// emit emports that are needed for classes from the code folder
if (extraImports != null) {
for (int i = 0; i < extraImports.length; i++) {
out.print("import " + extraImports[i] + "; ");
out.print("#include \"" + extraImports[i] + "\"\n");
}
}
@ -341,7 +393,7 @@ public class PdePreprocessor {
// emit standard imports (read from pde.properties)
// for each language level that's being used.
String jdkVersionStr = Preferences.get("preproc.jdk_version");
/* String jdkVersionStr = Preferences.get("preproc.jdk_version");
int jdkVersion = JDK11; // default
if (jdkVersionStr.equals("1.3")) { jdkVersion = JDK13; };
@ -357,8 +409,8 @@ public class PdePreprocessor {
//if (opengl) {
//out.println("import processing.opengl.*; ");
//}
if (programType < JAVA) {
*/
/* if (programType < JAVA) {
// open the class definition
out.print("public class " + className + " extends ");
//if (opengl) {
@ -377,7 +429,7 @@ public class PdePreprocessor {
out.print("public void setup() {");
}
}
}
*/ }
/**
* Write any necessary closing text.
@ -385,8 +437,9 @@ public class PdePreprocessor {
* @param out PrintStream to write it to.
*/
void writeFooter(PrintStream out) {
//out.print("}");
if (programType == STATIC) {
/* if (programType == STATIC) {
// close off draw() definition
out.print("noLoop(); ");
out.print("}");
@ -396,7 +449,7 @@ public class PdePreprocessor {
// close off the class definition
out.print("}");
}
}
*/ }
static String advClassName = "";

File diff suppressed because it is too large Load Diff

View File

@ -1,163 +0,0 @@
// $ANTLR 2.7.2: "expandedpde.g" -> "PdeRecognizer.java"$
package processing.app.preproc;
import processing.app.*;
public interface PdeTokenTypes {
int EOF = 1;
int NULL_TREE_LOOKAHEAD = 3;
int BLOCK = 4;
int MODIFIERS = 5;
int OBJBLOCK = 6;
int SLIST = 7;
int CTOR_DEF = 8;
int METHOD_DEF = 9;
int VARIABLE_DEF = 10;
int INSTANCE_INIT = 11;
int STATIC_INIT = 12;
int TYPE = 13;
int CLASS_DEF = 14;
int INTERFACE_DEF = 15;
int PACKAGE_DEF = 16;
int ARRAY_DECLARATOR = 17;
int EXTENDS_CLAUSE = 18;
int IMPLEMENTS_CLAUSE = 19;
int PARAMETERS = 20;
int PARAMETER_DEF = 21;
int LABELED_STAT = 22;
int TYPECAST = 23;
int INDEX_OP = 24;
int POST_INC = 25;
int POST_DEC = 26;
int METHOD_CALL = 27;
int EXPR = 28;
int ARRAY_INIT = 29;
int IMPORT = 30;
int UNARY_MINUS = 31;
int UNARY_PLUS = 32;
int CASE_GROUP = 33;
int ELIST = 34;
int FOR_INIT = 35;
int FOR_CONDITION = 36;
int FOR_ITERATOR = 37;
int EMPTY_STAT = 38;
int FINAL = 39;
int ABSTRACT = 40;
int STRICTFP = 41;
int SUPER_CTOR_CALL = 42;
int CTOR_CALL = 43;
int LITERAL_package = 44;
int SEMI = 45;
int LITERAL_import = 46;
int LBRACK = 47;
int RBRACK = 48;
int LITERAL_void = 49;
int LITERAL_boolean = 50;
int LITERAL_byte = 51;
int LITERAL_char = 52;
int LITERAL_short = 53;
int LITERAL_int = 54;
int LITERAL_float = 55;
int LITERAL_long = 56;
int LITERAL_double = 57;
int IDENT = 58;
int DOT = 59;
int STAR = 60;
int LITERAL_private = 61;
int LITERAL_public = 62;
int LITERAL_protected = 63;
int LITERAL_static = 64;
int LITERAL_transient = 65;
int LITERAL_native = 66;
int LITERAL_threadsafe = 67;
int LITERAL_synchronized = 68;
int LITERAL_volatile = 69;
int LITERAL_class = 70;
int LITERAL_extends = 71;
int LITERAL_interface = 72;
int LCURLY = 73;
int RCURLY = 74;
int COMMA = 75;
int LITERAL_implements = 76;
int LPAREN = 77;
int RPAREN = 78;
int LITERAL_this = 79;
int LITERAL_super = 80;
int ASSIGN = 81;
int LITERAL_throws = 82;
int COLON = 83;
int LITERAL_if = 84;
int LITERAL_else = 85;
int LITERAL_for = 86;
int LITERAL_while = 87;
int LITERAL_do = 88;
int LITERAL_break = 89;
int LITERAL_continue = 90;
int LITERAL_return = 91;
int LITERAL_switch = 92;
int LITERAL_throw = 93;
int LITERAL_assert = 94;
int LITERAL_case = 95;
int LITERAL_default = 96;
int LITERAL_try = 97;
int LITERAL_finally = 98;
int LITERAL_catch = 99;
int PLUS_ASSIGN = 100;
int MINUS_ASSIGN = 101;
int STAR_ASSIGN = 102;
int DIV_ASSIGN = 103;
int MOD_ASSIGN = 104;
int SR_ASSIGN = 105;
int BSR_ASSIGN = 106;
int SL_ASSIGN = 107;
int BAND_ASSIGN = 108;
int BXOR_ASSIGN = 109;
int BOR_ASSIGN = 110;
int QUESTION = 111;
int LOR = 112;
int LAND = 113;
int BOR = 114;
int BXOR = 115;
int BAND = 116;
int NOT_EQUAL = 117;
int EQUAL = 118;
int LT = 119;
int GT = 120;
int LE = 121;
int GE = 122;
int LITERAL_instanceof = 123;
int SL = 124;
int SR = 125;
int BSR = 126;
int PLUS = 127;
int MINUS = 128;
int DIV = 129;
int MOD = 130;
int INC = 131;
int DEC = 132;
int BNOT = 133;
int LNOT = 134;
int LITERAL_true = 135;
int LITERAL_false = 136;
int LITERAL_null = 137;
int LITERAL_new = 138;
int NUM_INT = 139;
int CHAR_LITERAL = 140;
int STRING_LITERAL = 141;
int NUM_FLOAT = 142;
int NUM_LONG = 143;
int NUM_DOUBLE = 144;
int WS = 145;
int SL_COMMENT = 146;
int ML_COMMENT = 147;
int ESC = 148;
int HEX_DIGIT = 149;
int VOCAB = 150;
int EXPONENT = 151;
int FLOAT_SUFFIX = 152;
int CONSTRUCTOR_CAST = 153;
int EMPTY_FIELD = 154;
int WEBCOLOR_LITERAL = 155;
int LITERAL_color = 156;
}

View File

@ -1,155 +0,0 @@
// $ANTLR 2.7.2: expandedpde.g -> PdeTokenTypes.txt$
Pde // output token vocab name
BLOCK=4
MODIFIERS=5
OBJBLOCK=6
SLIST=7
CTOR_DEF=8
METHOD_DEF=9
VARIABLE_DEF=10
INSTANCE_INIT=11
STATIC_INIT=12
TYPE=13
CLASS_DEF=14
INTERFACE_DEF=15
PACKAGE_DEF=16
ARRAY_DECLARATOR=17
EXTENDS_CLAUSE=18
IMPLEMENTS_CLAUSE=19
PARAMETERS=20
PARAMETER_DEF=21
LABELED_STAT=22
TYPECAST=23
INDEX_OP=24
POST_INC=25
POST_DEC=26
METHOD_CALL=27
EXPR=28
ARRAY_INIT=29
IMPORT=30
UNARY_MINUS=31
UNARY_PLUS=32
CASE_GROUP=33
ELIST=34
FOR_INIT=35
FOR_CONDITION=36
FOR_ITERATOR=37
EMPTY_STAT=38
FINAL="final"=39
ABSTRACT="abstract"=40
STRICTFP="strictfp"=41
SUPER_CTOR_CALL=42
CTOR_CALL=43
LITERAL_package="package"=44
SEMI=45
LITERAL_import="import"=46
LBRACK=47
RBRACK=48
LITERAL_void="void"=49
LITERAL_boolean="boolean"=50
LITERAL_byte="byte"=51
LITERAL_char="char"=52
LITERAL_short="short"=53
LITERAL_int="int"=54
LITERAL_float="float"=55
LITERAL_long="long"=56
LITERAL_double="double"=57
IDENT=58
DOT=59
STAR=60
LITERAL_private="private"=61
LITERAL_public="public"=62
LITERAL_protected="protected"=63
LITERAL_static="static"=64
LITERAL_transient="transient"=65
LITERAL_native="native"=66
LITERAL_threadsafe="threadsafe"=67
LITERAL_synchronized="synchronized"=68
LITERAL_volatile="volatile"=69
LITERAL_class="class"=70
LITERAL_extends="extends"=71
LITERAL_interface="interface"=72
LCURLY=73
RCURLY=74
COMMA=75
LITERAL_implements="implements"=76
LPAREN=77
RPAREN=78
LITERAL_this="this"=79
LITERAL_super="super"=80
ASSIGN=81
LITERAL_throws="throws"=82
COLON=83
LITERAL_if="if"=84
LITERAL_else="else"=85
LITERAL_for="for"=86
LITERAL_while="while"=87
LITERAL_do="do"=88
LITERAL_break="break"=89
LITERAL_continue="continue"=90
LITERAL_return="return"=91
LITERAL_switch="switch"=92
LITERAL_throw="throw"=93
LITERAL_assert="assert"=94
LITERAL_case="case"=95
LITERAL_default="default"=96
LITERAL_try="try"=97
LITERAL_finally="finally"=98
LITERAL_catch="catch"=99
PLUS_ASSIGN=100
MINUS_ASSIGN=101
STAR_ASSIGN=102
DIV_ASSIGN=103
MOD_ASSIGN=104
SR_ASSIGN=105
BSR_ASSIGN=106
SL_ASSIGN=107
BAND_ASSIGN=108
BXOR_ASSIGN=109
BOR_ASSIGN=110
QUESTION=111
LOR=112
LAND=113
BOR=114
BXOR=115
BAND=116
NOT_EQUAL=117
EQUAL=118
LT=119
GT=120
LE=121
GE=122
LITERAL_instanceof="instanceof"=123
SL=124
SR=125
BSR=126
PLUS=127
MINUS=128
DIV=129
MOD=130
INC=131
DEC=132
BNOT=133
LNOT=134
LITERAL_true="true"=135
LITERAL_false="false"=136
LITERAL_null="null"=137
LITERAL_new="new"=138
NUM_INT=139
CHAR_LITERAL=140
STRING_LITERAL=141
NUM_FLOAT=142
NUM_LONG=143
NUM_DOUBLE=144
WS=145
SL_COMMENT=146
ML_COMMENT=147
ESC=148
HEX_DIGIT=149
VOCAB=150
EXPONENT=151
FLOAT_SUFFIX=152
CONSTRUCTOR_CAST=153
EMPTY_FIELD=154
WEBCOLOR_LITERAL=155
LITERAL_color="color"=156

View File

@ -0,0 +1,73 @@
package processing.app.preproc;
import java.util.*;
public class PreprocessorInfoChannel
{
Hashtable lineLists = new Hashtable(); // indexed by Token number
int firstValidTokenNumber = 0;
int maxTokenNumber = 0;
public void addLineForTokenNumber( Object line, Integer toknum )
{
if ( lineLists.containsKey( toknum ) ) {
Vector lines = (Vector) lineLists.get( toknum );
lines.addElement(line);
}
else {
Vector lines = new Vector();
lines.addElement(line);
lineLists.put(toknum, lines);
if ( maxTokenNumber < toknum.intValue() ) {
maxTokenNumber = toknum.intValue();
}
}
}
public int getMaxTokenNumber()
{
return maxTokenNumber;
}
public Vector extractLinesPrecedingTokenNumber( Integer toknum )
{
Vector lines = new Vector();
if (toknum == null) return lines;
for (int i = firstValidTokenNumber; i < toknum.intValue(); i++){
Integer inti = new Integer(i);
if ( lineLists.containsKey( inti ) ) {
Vector tokenLineVector = (Vector) lineLists.get( inti );
if ( tokenLineVector != null) {
Enumeration tokenLines = tokenLineVector.elements();
while ( tokenLines.hasMoreElements() ) {
lines.addElement( tokenLines.nextElement() );
}
lineLists.remove(inti);
}
}
}
firstValidTokenNumber = toknum.intValue();
return lines;
}
public String toString()
{
StringBuffer sb = new StringBuffer("PreprocessorInfoChannel:\n");
for (int i = 0; i <= maxTokenNumber + 1; i++){
Integer inti = new Integer(i);
if ( lineLists.containsKey( inti ) ) {
Vector tokenLineVector = (Vector) lineLists.get( inti );
if ( tokenLineVector != null) {
Enumeration tokenLines = tokenLineVector.elements();
while ( tokenLines.hasMoreElements() ) {
sb.append(inti + ":" + tokenLines.nextElement() + '\n');
}
}
}
}
return sb.toString();
}
}

View File

@ -1,106 +0,0 @@
The PDE Preprocessor is based on the Java Grammar that comes with
ANTLR 2.7.2. Moving it forward to a new version of the grammar
shouldn't be too difficult.
Here's some info about the various files in this directory:
java.g: this is the ANTLR grammar for Java 1.3/1.4 from the ANTLR
distribution. It is in the public domain. The only change to this
file from the original this file is the uncommenting of the clauses
required to support assert().
java.tree.g: this describes the Abstract Syntax Tree (AST) generated
by java.g. It is only here as a reference for coders hacking on the
preprocessor, it is not built or used at all. Note that pde.g
overrides some of the java.g rules so that in PDE ASTs, there are a
few minor differences. Also in the public domain.
pde.g: this is the grammar and lexer for the PDE language itself. It
subclasses the java.g grammar and lexer. There are a couple of
overrides to java.g that I hope to convince the ANTLR folks to fold
back into their grammar, but most of this file is highly specific to
PDE itself.
PdeEmitter.java: this class traverses the AST generated by the PDE
Recognizer, and emits it as Java code, doing any necessary
transformations along the way. It is based on JavaEmitter.java,
available from antlr.org, written by Andy Tripp <atripp@comcast.net>,
who has given permission for it to be distributed under the GPL.
ExtendedCommonASTWithHiddenTokens.java: this adds a necessary
initialize() method, as well as a number of methods to allow for XML
serialization of the parse tree in a such a way that the hidden tokens
are visible. Much of the code is taken from the original
CommonASTWithHiddenTokens class. I hope to convince the ANTLR folks
to fold these changes back into that class so that this file will be
unnecessary.
TokenStreamCopyingHiddenTokenFilter.java: this class provides
TokenStreamHiddenTokenFilters with the concept of tokens which can be
copied so that they are seen by both the hidden token stream as well
as the parser itself. This is useful when one wants to use an
existing parser (like the Java parser included with ANTLR) that throws
away some tokens to create a parse tree which can be used to spit out
a copy of the code with only minor modifications. Partially derived
from ANTLR code. I hope to convince the ANTLR folks to fold this
functionality back into ANTLR proper as well.
whitespace_test.pde: a torture test to ensure that the preprocessor is
correctly preserving whitespace, comments, and other hidden tokens
correctly. See the comments in the code for details about how to run
the test.
All other files in this directory are generated at build time by ANTLR
itself. The ANTLR manual goes into a fair amount of detail about the
what each type of file is for.
....
Current Preprocessor Subsitutions:
"compiler.substitute_floats" (currently "substitute_f")
- treat doubles as floats, i.e. 12.3 becomes 12.3f so that people
don't have to add f after their numbers all the time. this is
confusing for beginners.
"compiler.enhanced_casting"
- byte(), char(), int(), float() works for casting. this is basic in
the current implementation, but should be expanded as described
above. color() works similarly to int(), however there is also a
*function* called color(r, g, b) in p5. will this cause trouble?
"compiler.color_datattype"
- 'color' is aliased to 'int' as a datatype to represent ARGB packed
into a single int, commonly used in p5 for pixels[] and other color
operations. this is just a search/replace type thing, and it can be
used interchangeably with int.
"compiler.web_colors" (currently "inline_web_colors")
- color c = #cc0080; should unpack to 0xffcc0080 (the ff at the top is
so that the color is opaque), which is just an int.
Other preprocessor functionality
- detects what 'mode' the program is in: static (no function brackets
at all, just assumes everything is in draw), active (setup plus draw
or loop), and java mode (full java support).
http://proce55ing.net/reference/environment/index.html
- size and background are pulled from draw mode programs and placed
into setup(). this has a problem if size() is based on a variable,
which we try to avoid people doing, but would like to be able to
support it (perhaps by requiring the size() to be final?)
- currently does a godawful scrambling of the comments so that the
substitution doesn't try to run on them. this also causes lots of
bizarro bugs.
Possible?
- would be nice to just type code wherever, mixing a 'static' style
app with a few functions. would be simpler for starting out. but it
seems that the declarations would have to be pulled out, but that
all seems problematic. or maybe it could all be inside a static { }
block. but that wouldn't seem to work either.

View File

@ -0,0 +1,149 @@
// $ANTLR 2.7.2: "StdCParser.g" -> "StdCLexer.java"$
package processing.app.preproc;
import java.io.*;
import antlr.CommonAST;
import antlr.DumpASTVisitor;
public interface STDCTokenTypes {
int EOF = 1;
int NULL_TREE_LOOKAHEAD = 3;
int LITERAL_typedef = 4;
int LITERAL_asm = 5;
int LITERAL_volatile = 6;
int LCURLY = 7;
int RCURLY = 8;
int SEMI = 9;
int LITERAL_struct = 10;
int LITERAL_union = 11;
int LITERAL_enum = 12;
int LITERAL_auto = 13;
int LITERAL_register = 14;
int LITERAL_extern = 15;
int LITERAL_static = 16;
int LITERAL_const = 17;
int LITERAL_void = 18;
int LITERAL_char = 19;
int LITERAL_short = 20;
int LITERAL_int = 21;
int LITERAL_long = 22;
int LITERAL_float = 23;
int LITERAL_double = 24;
int LITERAL_signed = 25;
int LITERAL_unsigned = 26;
int ID = 27;
int COMMA = 28;
int COLON = 29;
int ASSIGN = 30;
int STAR = 31;
int LPAREN = 32;
int RPAREN = 33;
int LBRACKET = 34;
int RBRACKET = 35;
int VARARGS = 36;
int LITERAL_while = 37;
int LITERAL_do = 38;
int LITERAL_for = 39;
int LITERAL_goto = 40;
int LITERAL_continue = 41;
int LITERAL_break = 42;
int LITERAL_return = 43;
int LITERAL_case = 44;
int LITERAL_default = 45;
int LITERAL_if = 46;
int LITERAL_else = 47;
int LITERAL_switch = 48;
int DIV_ASSIGN = 49;
int PLUS_ASSIGN = 50;
int MINUS_ASSIGN = 51;
int STAR_ASSIGN = 52;
int MOD_ASSIGN = 53;
int RSHIFT_ASSIGN = 54;
int LSHIFT_ASSIGN = 55;
int BAND_ASSIGN = 56;
int BOR_ASSIGN = 57;
int BXOR_ASSIGN = 58;
int QUESTION = 59;
int LOR = 60;
int LAND = 61;
int BOR = 62;
int BXOR = 63;
int BAND = 64;
int EQUAL = 65;
int NOT_EQUAL = 66;
int LT = 67;
int LTE = 68;
int GT = 69;
int GTE = 70;
int LSHIFT = 71;
int RSHIFT = 72;
int PLUS = 73;
int MINUS = 74;
int DIV = 75;
int MOD = 76;
int INC = 77;
int DEC = 78;
int LITERAL_sizeof = 79;
int BNOT = 80;
int LNOT = 81;
int PTR = 82;
int DOT = 83;
int CharLiteral = 84;
int StringLiteral = 85;
int IntOctalConst = 86;
int LongOctalConst = 87;
int UnsignedOctalConst = 88;
int IntIntConst = 89;
int LongIntConst = 90;
int UnsignedIntConst = 91;
int IntHexConst = 92;
int LongHexConst = 93;
int UnsignedHexConst = 94;
int FloatDoubleConst = 95;
int DoubleDoubleConst = 96;
int LongDoubleConst = 97;
int NTypedefName = 98;
int NInitDecl = 99;
int NDeclarator = 100;
int NStructDeclarator = 101;
int NDeclaration = 102;
int NCast = 103;
int NPointerGroup = 104;
int NExpressionGroup = 105;
int NFunctionCallArgs = 106;
int NNonemptyAbstractDeclarator = 107;
int NInitializer = 108;
int NStatementExpr = 109;
int NEmptyExpression = 110;
int NParameterTypeList = 111;
int NFunctionDef = 112;
int NCompoundStatement = 113;
int NParameterDeclaration = 114;
int NCommaExpr = 115;
int NUnaryExpr = 116;
int NLabel = 117;
int NPostfixExpr = 118;
int NRangeExpr = 119;
int NStringSeq = 120;
int NInitializerElementLabel = 121;
int NLcurlyInitializer = 122;
int NAsmAttribute = 123;
int NGnuAsmExpr = 124;
int NTypeMissing = 125;
int Vocabulary = 126;
int Whitespace = 127;
int Comment = 128;
int CPPComment = 129;
int PREPROC_DIRECTIVE = 130;
int Space = 131;
int LineDirective = 132;
int BadStringLiteral = 133;
int Escape = 134;
int Digit = 135;
int LongSuffix = 136;
int UnsignedSuffix = 137;
int FloatSuffix = 138;
int Exponent = 139;
int Number = 140;
}

View File

@ -0,0 +1,139 @@
// $ANTLR 2.7.2: StdCParser.g -> STDCTokenTypes.txt$
STDC // output token vocab name
LITERAL_typedef="typedef"=4
LITERAL_asm="asm"=5
LITERAL_volatile="volatile"=6
LCURLY=7
RCURLY=8
SEMI=9
LITERAL_struct="struct"=10
LITERAL_union="union"=11
LITERAL_enum="enum"=12
LITERAL_auto="auto"=13
LITERAL_register="register"=14
LITERAL_extern="extern"=15
LITERAL_static="static"=16
LITERAL_const="const"=17
LITERAL_void="void"=18
LITERAL_char="char"=19
LITERAL_short="short"=20
LITERAL_int="int"=21
LITERAL_long="long"=22
LITERAL_float="float"=23
LITERAL_double="double"=24
LITERAL_signed="signed"=25
LITERAL_unsigned="unsigned"=26
ID=27
COMMA=28
COLON=29
ASSIGN=30
STAR=31
LPAREN=32
RPAREN=33
LBRACKET=34
RBRACKET=35
VARARGS=36
LITERAL_while="while"=37
LITERAL_do="do"=38
LITERAL_for="for"=39
LITERAL_goto="goto"=40
LITERAL_continue="continue"=41
LITERAL_break="break"=42
LITERAL_return="return"=43
LITERAL_case="case"=44
LITERAL_default="default"=45
LITERAL_if="if"=46
LITERAL_else="else"=47
LITERAL_switch="switch"=48
DIV_ASSIGN=49
PLUS_ASSIGN=50
MINUS_ASSIGN=51
STAR_ASSIGN=52
MOD_ASSIGN=53
RSHIFT_ASSIGN=54
LSHIFT_ASSIGN=55
BAND_ASSIGN=56
BOR_ASSIGN=57
BXOR_ASSIGN=58
QUESTION=59
LOR=60
LAND=61
BOR=62
BXOR=63
BAND=64
EQUAL=65
NOT_EQUAL=66
LT=67
LTE=68
GT=69
GTE=70
LSHIFT=71
RSHIFT=72
PLUS=73
MINUS=74
DIV=75
MOD=76
INC=77
DEC=78
LITERAL_sizeof="sizeof"=79
BNOT=80
LNOT=81
PTR=82
DOT=83
CharLiteral=84
StringLiteral=85
IntOctalConst=86
LongOctalConst=87
UnsignedOctalConst=88
IntIntConst=89
LongIntConst=90
UnsignedIntConst=91
IntHexConst=92
LongHexConst=93
UnsignedHexConst=94
FloatDoubleConst=95
DoubleDoubleConst=96
LongDoubleConst=97
NTypedefName=98
NInitDecl=99
NDeclarator=100
NStructDeclarator=101
NDeclaration=102
NCast=103
NPointerGroup=104
NExpressionGroup=105
NFunctionCallArgs=106
NNonemptyAbstractDeclarator=107
NInitializer=108
NStatementExpr=109
NEmptyExpression=110
NParameterTypeList=111
NFunctionDef=112
NCompoundStatement=113
NParameterDeclaration=114
NCommaExpr=115
NUnaryExpr=116
NLabel=117
NPostfixExpr=118
NRangeExpr=119
NStringSeq=120
NInitializerElementLabel=121
NLcurlyInitializer=122
NAsmAttribute=123
NGnuAsmExpr=124
NTypeMissing=125
Vocabulary=126
Whitespace=127
Comment=128
CPPComment=129
PREPROC_DIRECTIVE("a line directive")=130
Space=131
LineDirective=132
BadStringLiteral=133
Escape=134
Digit=135
LongSuffix=136
UnsignedSuffix=137
FloatSuffix=138
Exponent=139
Number=140

File diff suppressed because it is too large Load Diff

1358
app/preproc/StdCParser.g Executable file

File diff suppressed because it is too large Load Diff

5886
app/preproc/StdCParser.java Normal file

File diff suppressed because it is too large Load Diff

434
app/preproc/TNode.java Executable file
View File

@ -0,0 +1,434 @@
package processing.app.preproc;
import antlr.collections.AST;
import antlr.CommonAST;
import antlr.Token;
import java.lang.reflect.*;
import java.util.Hashtable;
import java.util.Enumeration;
//import CToken;
/**
Class TNode is an implementation of the AST interface
and adds many useful features:
It is double-linked for reverse searching.
(this is currently incomplete, in that method doubleLink() must
be called after any changes to the tree to maintain the
reverse links).
It can store a definition node (defNode), so that nodes such
as scoped names can refer to the node that defines the name.
It stores line numbers for nodes.
Searches for parents and children of a tree can be done
based on their type.
The tree can be printed to System.out using a lisp-style syntax.
*/
public class TNode extends CommonAST {
protected int ttype;
protected String text;
protected int lineNum = 0;
protected TNode defNode;
protected TNode up;
protected TNode left;
protected boolean marker = false;
protected Hashtable attributes = null;
static String tokenVocabulary;
/** Set the token vocabulary to a tokentypes class
generated by antlr.
*/
public static void setTokenVocabulary(String s) {
tokenVocabulary = s;
}
public void initialize(Token token) {
CToken tok = (CToken) token;
setText(tok.getText());
setType(tok.getType());
setLineNum(tok.getLine());
setAttribute("source", tok.getSource());
setAttribute("tokenNumber", new Integer(tok.getTokenNumber()));
}
public void initialize(AST tr) {
TNode t = (TNode) tr;
setText(t.getText());
setType(t.getType());
setLineNum(t.getLineNum());
setDefNode(t.getDefNode());
this.attributes = t.getAttributesTable();
}
/** Get the token type for this node */
public int getType() { return ttype; }
/** Set the token type for this node */
public void setType(int ttype_) {
ttype = ttype_;
}
/** Get the marker value for this node.
This member is a general-use marker.
*/
public boolean getMarker() { return marker; }
/** Set the marker value for this node.
This property is a general-use boolean marker.
*/
public void setMarker(boolean marker_) {
marker = marker_;
}
/** get the hashtable that holds attribute values.
*/
public Hashtable getAttributesTable() {
if(attributes == null)
attributes = new Hashtable(7);
return attributes;
}
/** set an attribute in the attribute table.
*/
public void setAttribute(String attrName, Object value) {
if(attributes == null)
attributes = new Hashtable(7);
attributes.put(attrName,value);
}
/** lookup the attribute name in the attribute table.
If the value does not exist, it returns null.
*/
public Object getAttribute(String attrName) {
if(attributes == null)
return null;
else
return attributes.get(attrName);
}
/** Get the line number for this node.
If the line number is 0, search for a non-zero line num among children */
public int getLineNum() {
if(lineNum != 0)
return lineNum;
else
if(down == null)
return lineNum;
else
return ((TNode)down).getLocalLineNum();
}
public int getLocalLineNum() {
if(lineNum != 0)
return lineNum;
else
if(down == null)
if(right == null)
return lineNum;
else
return ((TNode)right).getLocalLineNum();
else
return ((TNode)down).getLocalLineNum();
}
/** Set the line number for this node */
public void setLineNum(int lineNum_) {
lineNum = lineNum_;
}
/** Get the token text for this node */
public String getText() { return text; }
/** Set the token text for this node */
public void setText(String text_) {
text = text_;
}
/** return the last child of this node, or null if there is none */
public TNode getLastChild() {
TNode down = (TNode)getFirstChild();
if(down != null)
return down.getLastSibling();
else
return null;
}
/** return the last sibling of this node, which is
this if the next sibling is null */
public TNode getLastSibling() {
TNode next = (TNode)getNextSibling();
if(next != null)
return next.getLastSibling();
else
return this;
}
/** return the first sibling of this node, which is
this if the prev sibling is null */
public TNode getFirstSibling() {
TNode prev = (TNode)left;
if(prev != null)
return prev.getFirstSibling();
else
return this;
}
/** return the parent node of this node */
public TNode getParent() {
return (TNode)getFirstSibling().up;
}
/** add the new node as a new sibling, inserting it ahead of any
existing next sibling. This method maintains double-linking.
if node is null, nothing happens. If the node has siblings,
then they are added in as well.
*/
public void addSibling(AST node) {
if(node == null) return;
TNode next = (TNode)right;
right = (TNode)node;
((TNode)node).left = this;
TNode nodeLastSib = ((TNode)node).getLastSibling();
nodeLastSib.right = next;
if(next != null)
next.left = nodeLastSib;
}
/** return the number of children of this node */
public int numberOfChildren() {
int count = 0;
AST child = getFirstChild();
while(child != null) {
count++;
child = child.getNextSibling();
}
return count;
}
/** remove this node from the tree, resetting sibling and parent
pointers as necessary. This method maintains double-linking */
public void removeSelf() {
TNode parent = (TNode)up;
TNode prev = (TNode)left;
TNode next = (TNode)right;
if(parent != null) {
parent.down = next;
if(next != null) {
next.up = parent;
next.left = prev; // which should be null
}
}
else {
if(prev != null)
prev.right = next;
if(next != null)
next.left = prev;
}
}
/** return the def node for this node */
public TNode getDefNode() {
return defNode;
}
/** set the def node for this node */
public void setDefNode(TNode n) {
defNode = n;
}
/** return a deep copy of this node, and all sub nodes.
New tree is doubleLinked, with no parent or siblings.
Marker value is not copied!
*/
public TNode deepCopy() {
TNode copy = new TNode();
copy.ttype = ttype;
copy.text = text;
copy.lineNum = lineNum;
copy.defNode = defNode;
if(attributes != null)
copy.attributes = (Hashtable)attributes.clone();
if(down != null)
copy.down = ((TNode)down).deepCopyWithRightSiblings();
copy.doubleLink();
return copy;
}
/** return a deep copy of this node, all sub nodes,
and right siblings.
New tree is doubleLinked, with no parent or left siblings.
defNode is not copied */
public TNode deepCopyWithRightSiblings() {
TNode copy = new TNode();
copy.ttype = ttype;
copy.text = text;
copy.lineNum = lineNum;
copy.defNode = defNode;
if(attributes != null)
copy.attributes = (Hashtable)attributes.clone();
if(down != null)
copy.down = ((TNode)down).deepCopyWithRightSiblings();
if(right != null)
copy.right = ((TNode)right).deepCopyWithRightSiblings();
copy.doubleLink();
return copy;
}
/** return a short string representation of the node */
public String toString() {
StringBuffer str = new StringBuffer( getNameForType(getType()) +
"[" + getText() + ", " + "]");
if(this.getLineNum() != 0)
str.append(" line:" + (this.getLineNum() ) );
Enumeration keys = (this.getAttributesTable().keys());
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
str.append(" " + key + ":" + (this.getAttribute(key)));
}
return str.toString();
}
/** print given tree to System.out */
public static void printTree(AST t) {
if (t == null) return;
printASTNode(t,0);
System.out.print("\n");
}
/** protected method that does the work of printing */
protected static void printASTNode(AST t, int indent) {
AST child1, next;
child1 = t.getFirstChild();
System.out.print("\n");
for(int i = 0; i < indent; i++)
System.out.print(" ");
if(child1 != null)
System.out.print("(");
String s = t.getText();
if(s != null && s.length() > 0) {
System.out.print(getNameForType(t.getType()));
System.out.print(": \"" + s + "\"");
}
else
System.out.print(getNameForType(t.getType()));
if(((TNode)t).getLineNum() != 0)
System.out.print(" line:" + ((TNode)t).getLineNum() );
Enumeration keys = ((TNode)t).getAttributesTable().keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
System.out.print(" " + key + ":" + ((TNode)t).getAttribute(key));
}
TNode def = ((TNode)t).getDefNode();
if(def != null)
System.out.print("[" + getNameForType(def.getType()) + "]");
if(child1 != null) {
printASTNode(child1,indent + 1);
System.out.print("\n");
for(int i = 0; i < indent; i++)
System.out.print(" ");
System.out.print(")");
}
next = t.getNextSibling();
if(next != null) {
printASTNode(next,indent);
}
}
/** converts an int tree token type to a name.
Does this by reflecting on nsdidl.IDLTreeTokenTypes,
and is dependent on how ANTLR 2.00 outputs that class. */
public static String getNameForType(int t) {
try{
Class c = Class.forName(tokenVocabulary);
Field[] fields = c.getDeclaredFields();
if(t-2 < fields.length)
return fields[t-2].getName();
} catch (Exception e) { System.out.println(e); }
return "unfoundtype: " + t;
}
/** set up reverse links between this node and its first
child and its first sibling, and link those as well */
public void doubleLink() {
TNode right = (TNode)getNextSibling();
if(right != null) {
right.left = this;
right.doubleLink();
}
TNode down = (TNode)getFirstChild();
if(down != null) {
down.up = this;
down.doubleLink();
}
}
/** find first parent of the given type,
return null on failure */
public TNode parentOfType(int type) {
if(up == null) {
if(left == null)
return null;
else
return left.parentOfType(type);
}
if(up.getType() == type)
return up;
return up.parentOfType(type);
}
/** find the first child of the node
of the given type, return null on failure */
public TNode firstChildOfType(int type) {
TNode down = (TNode)getFirstChild();
if(down == null)
return null;
if(down.getType() == type)
return down;
return down.firstSiblingOfType(type);
}
/** find the first sibling of the node
of the given type, return null on failure */
public TNode firstSiblingOfType(int type) {
TNode right = (TNode)getNextSibling();
if(right == null)
return null;
if(right.getType() == type)
return right;
return right.firstSiblingOfType(type);
}
}

33
app/preproc/TNodeFactory.java Executable file
View File

@ -0,0 +1,33 @@
package processing.app.preproc;
import antlr.Token;
import antlr.ASTFactory;
import antlr.collections.AST;
/** This class extends ASTFactory to build instances
of class TNode */
public class TNodeFactory extends ASTFactory {
/** Create a new ampty AST node */
public AST create() {
return new TNode();
}
/** Create a new AST node from type and text */
public AST create(int ttype, String text) {
AST ast = new TNode();
ast.setType(ttype);
ast.setText(text);
return ast;
}
/** Create a new AST node from an existing AST node */
public AST create(AST ast) {
AST newast = new TNode();
newast.setType(ast.getType());
newast.setText(ast.getText());
return newast;
}
}

View File

@ -1,221 +0,0 @@
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
package antlr;
//package processing.app.preproc;
import antlr.*;
import antlr.collections.impl.BitSet;
/**
* This class provides TokenStreamHiddenTokenFilters with the concept of
* tokens which can be copied so that they are seen by both the hidden token
* stream as well as the parser itself. This is useful when one wants to use
* an existing parser (like the Java parser included with ANTLR) that throws
* away some tokens to create a parse tree which can be used to spit out
* a copy of the code with only minor modifications.
*
* This code is partially derived from the public domain ANLTR TokenStream
*/
public class TokenStreamCopyingHiddenTokenFilter
extends TokenStreamHiddenTokenFilter
implements TokenStream {
protected BitSet copyMask;
CommonHiddenStreamToken hiddenCopy = null;
public TokenStreamCopyingHiddenTokenFilter(TokenStream input) {
super(input);
copyMask = new BitSet();
}
/**
* Indicate that all tokens of type tokenType should be copied. The copy
* is put in the stream of hidden tokens, and the original is returned in the
* stream of normal tokens.
*
* @param tokenType integer representing the token type to copied
*/
public void copy(int tokenType) {
copyMask.add(tokenType);
}
/**
* Create a clone of the important parts of the given token. Note that this
* does NOT copy the hiddenBefore and hiddenAfter fields.
*
* @param t token to partially clone
* @return newly created partial clone
*/
public CommonHiddenStreamToken partialCloneToken(CommonHiddenStreamToken t) {
CommonHiddenStreamToken u = new CommonHiddenStreamToken(t.getType(),
t.getText());
u.setColumn(t.getColumn());
u.setLine(t.getLine());
u.setFilename(t.getFilename());
return u;
}
public void linkAndCopyToken(CommonHiddenStreamToken prev,
CommonHiddenStreamToken monitored) {
// create a copy of the token in the lookahead for use as hidden token
hiddenCopy = partialCloneToken(LA(1));
// attach copy to the previous token, whether hidden or monitored
prev.setHiddenAfter(hiddenCopy);
// if previous token was hidden, set the hiddenBefore pointer of the
// copy to point back to it
if (prev != monitored) {
hiddenCopy.setHiddenBefore(prev);
}
// we don't want the non-hidden copy to link back to the hidden
// copy on the next pass through this function, so we leave
// lastHiddenToken alone
//System.err.println("hidden copy: " + hiddenCopy.toString());
return;
}
private void consumeFirst() throws TokenStreamException {
consume(); // get first token of input stream
// Handle situation where hidden or discarded tokens
// appear first in input stream
CommonHiddenStreamToken p=null;
// while hidden, copied, or discarded scarf tokens
while ( hideMask.member(LA(1).getType()) ||
discardMask.member(LA(1).getType()) ||
copyMask.member(LA(1).getType()) ) {
// if we've hit one of the tokens that needs to be copied, we copy it
// and then break out of the loop, because the parser needs to see it
// too
//
if (copyMask.member(LA(1).getType())) {
// copy the token in the lookahead
hiddenCopy = partialCloneToken(LA(1));
// if there's an existing token before this, link that and the
// copy together
if (p != null) {
p.setHiddenAfter(hiddenCopy);
hiddenCopy.setHiddenBefore(p); // double-link
}
lastHiddenToken = hiddenCopy;
if (firstHidden == null) {
firstHidden = hiddenCopy;
}
// we don't want to consume this token, because it also needs to
// be passed through to the parser, so break out of the while look
// entirely
//
break;
} else if (hideMask.member(LA(1).getType())) {
if (p != null) {
p.setHiddenAfter(LA(1));
LA(1).setHiddenBefore(p); // double-link
}
p = LA(1);
lastHiddenToken = p;
if (firstHidden == null) {
firstHidden = p; // record hidden token if first
}
}
consume();
}
}
/** Return the next monitored token.
* Test the token following the monitored token.
* If following is another monitored token, save it
* for the next invocation of nextToken (like a single
* lookahead token) and return it then.
* If following is unmonitored, nondiscarded (hidden)
* channel token, add it to the monitored token.
*
* Note: EOF must be a monitored Token.
*/
public Token nextToken() throws TokenStreamException {
// handle an initial condition; don't want to get lookahead
// token of this splitter until first call to nextToken
if (LA(1) == null) {
consumeFirst();
}
//System.err.println();
// we always consume hidden tokens after monitored, thus,
// upon entry LA(1) is a monitored token.
CommonHiddenStreamToken monitored = LA(1);
// point to hidden tokens found during last invocation
monitored.setHiddenBefore(lastHiddenToken);
lastHiddenToken = null;
// Look for hidden tokens, hook them into list emanating
// from the monitored tokens.
consume();
CommonHiddenStreamToken prev = monitored;
// deal with as many not-purely-monitored tokens as possible
while ( hideMask.member(LA(1).getType()) ||
discardMask.member(LA(1).getType()) ||
copyMask.member(LA(1).getType()) ) {
if (copyMask.member(LA(1).getType())) {
// copy the token and link it backwards
if (hiddenCopy != null) {
linkAndCopyToken(hiddenCopy, monitored);
} else {
linkAndCopyToken(prev, monitored);
}
// we now need to parse it as a monitored token, so we return, which
// avoids the consume() call at the end of this loop. the next call
// will parse it as a monitored token.
//System.err.println("returned: " + monitored.toString());
return monitored;
} else if (hideMask.member(LA(1).getType())) {
// attach the hidden token to the monitored in a chain
// link forwards
prev.setHiddenAfter(LA(1));
// link backwards
if (prev != monitored) { //hidden cannot point to monitored tokens
LA(1).setHiddenBefore(prev);
} else if (hiddenCopy != null) {
hiddenCopy.setHiddenAfter(LA(1));
LA(1).setHiddenBefore(hiddenCopy);
hiddenCopy = null;
}
//System.err.println("hidden: " + prev.getHiddenAfter().toString() + "\" after: " + prev.toString());
prev = lastHiddenToken = LA(1);
}
consume();
}
// remember the last hidden token for next time around
if (hiddenCopy != null) {
lastHiddenToken = hiddenCopy;
hiddenCopy = null;
}
//System.err.println("returned: " + monitored.toString());
return monitored;
}
}

1191
app/preproc/WEmitter.g Executable file

File diff suppressed because it is too large Load Diff

6689
app/preproc/WEmitter.java Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,163 @@
// $ANTLR 2.7.2: "expandedWEmitter.g" -> "WEmitter.java"$
package processing.app.preproc;
import processing.app.*;
import java.io.*;
import java.util.*;
import antlr.CommonAST;
import antlr.DumpASTVisitor;
public interface WEmitterTokenTypes {
int EOF = 1;
int NULL_TREE_LOOKAHEAD = 3;
int LITERAL_typedef = 4;
int LITERAL_asm = 5;
int LITERAL_volatile = 6;
int LCURLY = 7;
int RCURLY = 8;
int SEMI = 9;
int LITERAL_struct = 10;
int LITERAL_union = 11;
int LITERAL_enum = 12;
int LITERAL_auto = 13;
int LITERAL_register = 14;
int LITERAL_extern = 15;
int LITERAL_static = 16;
int LITERAL_const = 17;
int LITERAL_void = 18;
int LITERAL_char = 19;
int LITERAL_short = 20;
int LITERAL_int = 21;
int LITERAL_long = 22;
int LITERAL_float = 23;
int LITERAL_double = 24;
int LITERAL_signed = 25;
int LITERAL_unsigned = 26;
int ID = 27;
int COMMA = 28;
int COLON = 29;
int ASSIGN = 30;
int STAR = 31;
int LPAREN = 32;
int RPAREN = 33;
int LBRACKET = 34;
int RBRACKET = 35;
int VARARGS = 36;
int LITERAL_while = 37;
int LITERAL_do = 38;
int LITERAL_for = 39;
int LITERAL_goto = 40;
int LITERAL_continue = 41;
int LITERAL_break = 42;
int LITERAL_return = 43;
int LITERAL_case = 44;
int LITERAL_default = 45;
int LITERAL_if = 46;
int LITERAL_else = 47;
int LITERAL_switch = 48;
int DIV_ASSIGN = 49;
int PLUS_ASSIGN = 50;
int MINUS_ASSIGN = 51;
int STAR_ASSIGN = 52;
int MOD_ASSIGN = 53;
int RSHIFT_ASSIGN = 54;
int LSHIFT_ASSIGN = 55;
int BAND_ASSIGN = 56;
int BOR_ASSIGN = 57;
int BXOR_ASSIGN = 58;
int QUESTION = 59;
int LOR = 60;
int LAND = 61;
int BOR = 62;
int BXOR = 63;
int BAND = 64;
int EQUAL = 65;
int NOT_EQUAL = 66;
int LT = 67;
int LTE = 68;
int GT = 69;
int GTE = 70;
int LSHIFT = 71;
int RSHIFT = 72;
int PLUS = 73;
int MINUS = 74;
int DIV = 75;
int MOD = 76;
int INC = 77;
int DEC = 78;
int LITERAL_sizeof = 79;
int BNOT = 80;
int LNOT = 81;
int PTR = 82;
int DOT = 83;
int CharLiteral = 84;
int StringLiteral = 85;
int IntOctalConst = 86;
int LongOctalConst = 87;
int UnsignedOctalConst = 88;
int IntIntConst = 89;
int LongIntConst = 90;
int UnsignedIntConst = 91;
int IntHexConst = 92;
int LongHexConst = 93;
int UnsignedHexConst = 94;
int FloatDoubleConst = 95;
int DoubleDoubleConst = 96;
int LongDoubleConst = 97;
int NTypedefName = 98;
int NInitDecl = 99;
int NDeclarator = 100;
int NStructDeclarator = 101;
int NDeclaration = 102;
int NCast = 103;
int NPointerGroup = 104;
int NExpressionGroup = 105;
int NFunctionCallArgs = 106;
int NNonemptyAbstractDeclarator = 107;
int NInitializer = 108;
int NStatementExpr = 109;
int NEmptyExpression = 110;
int NParameterTypeList = 111;
int NFunctionDef = 112;
int NCompoundStatement = 113;
int NParameterDeclaration = 114;
int NCommaExpr = 115;
int NUnaryExpr = 116;
int NLabel = 117;
int NPostfixExpr = 118;
int NRangeExpr = 119;
int NStringSeq = 120;
int NInitializerElementLabel = 121;
int NLcurlyInitializer = 122;
int NAsmAttribute = 123;
int NGnuAsmExpr = 124;
int NTypeMissing = 125;
int Vocabulary = 126;
int Whitespace = 127;
int Comment = 128;
int CPPComment = 129;
int PREPROC_DIRECTIVE = 130;
int Space = 131;
int LineDirective = 132;
int BadStringLiteral = 133;
int Escape = 134;
int Digit = 135;
int LongSuffix = 136;
int UnsignedSuffix = 137;
int FloatSuffix = 138;
int Exponent = 139;
int Number = 140;
int LITERAL___label__ = 141;
int LITERAL_inline = 142;
int LITERAL_byte = 143;
int LITERAL_boolean = 144;
int LITERAL_Servo = 145;
int LITERAL_Wire = 146;
int LITERAL_typeof = 147;
int LITERAL___complex = 148;
int LITERAL___attribute = 149;
int LITERAL___alignof = 150;
int LITERAL___real = 151;
int LITERAL___imag = 152;
}

View File

@ -0,0 +1,151 @@
// $ANTLR 2.7.2: expandedWEmitter.g -> WEmitterTokenTypes.txt$
WEmitter // output token vocab name
LITERAL_typedef="typedef"=4
LITERAL_asm="asm"=5
LITERAL_volatile="volatile"=6
LCURLY=7
RCURLY=8
SEMI=9
LITERAL_struct="struct"=10
LITERAL_union="union"=11
LITERAL_enum="enum"=12
LITERAL_auto="auto"=13
LITERAL_register="register"=14
LITERAL_extern="extern"=15
LITERAL_static="static"=16
LITERAL_const="const"=17
LITERAL_void="void"=18
LITERAL_char="char"=19
LITERAL_short="short"=20
LITERAL_int="int"=21
LITERAL_long="long"=22
LITERAL_float="float"=23
LITERAL_double="double"=24
LITERAL_signed="signed"=25
LITERAL_unsigned="unsigned"=26
ID=27
COMMA=28
COLON=29
ASSIGN=30
STAR=31
LPAREN=32
RPAREN=33
LBRACKET=34
RBRACKET=35
VARARGS=36
LITERAL_while="while"=37
LITERAL_do="do"=38
LITERAL_for="for"=39
LITERAL_goto="goto"=40
LITERAL_continue="continue"=41
LITERAL_break="break"=42
LITERAL_return="return"=43
LITERAL_case="case"=44
LITERAL_default="default"=45
LITERAL_if="if"=46
LITERAL_else="else"=47
LITERAL_switch="switch"=48
DIV_ASSIGN=49
PLUS_ASSIGN=50
MINUS_ASSIGN=51
STAR_ASSIGN=52
MOD_ASSIGN=53
RSHIFT_ASSIGN=54
LSHIFT_ASSIGN=55
BAND_ASSIGN=56
BOR_ASSIGN=57
BXOR_ASSIGN=58
QUESTION=59
LOR=60
LAND=61
BOR=62
BXOR=63
BAND=64
EQUAL=65
NOT_EQUAL=66
LT=67
LTE=68
GT=69
GTE=70
LSHIFT=71
RSHIFT=72
PLUS=73
MINUS=74
DIV=75
MOD=76
INC=77
DEC=78
LITERAL_sizeof="sizeof"=79
BNOT=80
LNOT=81
PTR=82
DOT=83
CharLiteral=84
StringLiteral=85
IntOctalConst=86
LongOctalConst=87
UnsignedOctalConst=88
IntIntConst=89
LongIntConst=90
UnsignedIntConst=91
IntHexConst=92
LongHexConst=93
UnsignedHexConst=94
FloatDoubleConst=95
DoubleDoubleConst=96
LongDoubleConst=97
NTypedefName=98
NInitDecl=99
NDeclarator=100
NStructDeclarator=101
NDeclaration=102
NCast=103
NPointerGroup=104
NExpressionGroup=105
NFunctionCallArgs=106
NNonemptyAbstractDeclarator=107
NInitializer=108
NStatementExpr=109
NEmptyExpression=110
NParameterTypeList=111
NFunctionDef=112
NCompoundStatement=113
NParameterDeclaration=114
NCommaExpr=115
NUnaryExpr=116
NLabel=117
NPostfixExpr=118
NRangeExpr=119
NStringSeq=120
NInitializerElementLabel=121
NLcurlyInitializer=122
NAsmAttribute=123
NGnuAsmExpr=124
NTypeMissing=125
Vocabulary=126
Whitespace=127
Comment=128
CPPComment=129
PREPROC_DIRECTIVE("a line directive")=130
Space=131
LineDirective=132
BadStringLiteral=133
Escape=134
Digit=135
LongSuffix=136
UnsignedSuffix=137
FloatSuffix=138
Exponent=139
Number=140
LITERAL___label__="__label__"=141
LITERAL_inline="inline"=142
LITERAL_byte="byte"=143
LITERAL_boolean="boolean"=144
LITERAL_Servo="Servo"=145
LITERAL_Wire="Wire"=146
LITERAL_typeof="typeof"=147
LITERAL___complex="__complex"=148
LITERAL___attribute="__attribute"=149
LITERAL___alignof="__alignof"=150
LITERAL___real="__real"=151
LITERAL___imag="__imag"=152

2810
app/preproc/WLexer.java Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,168 @@
// $ANTLR 2.7.2: "expandedWParser.g" -> "WLexer.java"$
package processing.app.preproc;
import java.io.*;
import antlr.CommonAST;
import antlr.DumpASTVisitor;
public interface WLexerTokenTypes {
int EOF = 1;
int NULL_TREE_LOOKAHEAD = 3;
int LITERAL_typedef = 4;
int LITERAL_asm = 5;
int LITERAL_volatile = 6;
int LCURLY = 7;
int RCURLY = 8;
int SEMI = 9;
int LITERAL_struct = 10;
int LITERAL_union = 11;
int LITERAL_enum = 12;
int LITERAL_auto = 13;
int LITERAL_register = 14;
int LITERAL_extern = 15;
int LITERAL_static = 16;
int LITERAL_const = 17;
int LITERAL_void = 18;
int LITERAL_char = 19;
int LITERAL_short = 20;
int LITERAL_int = 21;
int LITERAL_long = 22;
int LITERAL_float = 23;
int LITERAL_double = 24;
int LITERAL_signed = 25;
int LITERAL_unsigned = 26;
int ID = 27;
int COMMA = 28;
int COLON = 29;
int ASSIGN = 30;
int STAR = 31;
int LPAREN = 32;
int RPAREN = 33;
int LBRACKET = 34;
int RBRACKET = 35;
int VARARGS = 36;
int LITERAL_while = 37;
int LITERAL_do = 38;
int LITERAL_for = 39;
int LITERAL_goto = 40;
int LITERAL_continue = 41;
int LITERAL_break = 42;
int LITERAL_return = 43;
int LITERAL_case = 44;
int LITERAL_default = 45;
int LITERAL_if = 46;
int LITERAL_else = 47;
int LITERAL_switch = 48;
int DIV_ASSIGN = 49;
int PLUS_ASSIGN = 50;
int MINUS_ASSIGN = 51;
int STAR_ASSIGN = 52;
int MOD_ASSIGN = 53;
int RSHIFT_ASSIGN = 54;
int LSHIFT_ASSIGN = 55;
int BAND_ASSIGN = 56;
int BOR_ASSIGN = 57;
int BXOR_ASSIGN = 58;
int QUESTION = 59;
int LOR = 60;
int LAND = 61;
int BOR = 62;
int BXOR = 63;
int BAND = 64;
int EQUAL = 65;
int NOT_EQUAL = 66;
int LT = 67;
int LTE = 68;
int GT = 69;
int GTE = 70;
int LSHIFT = 71;
int RSHIFT = 72;
int PLUS = 73;
int MINUS = 74;
int DIV = 75;
int MOD = 76;
int INC = 77;
int DEC = 78;
int LITERAL_sizeof = 79;
int BNOT = 80;
int LNOT = 81;
int PTR = 82;
int DOT = 83;
int CharLiteral = 84;
int StringLiteral = 85;
int IntOctalConst = 86;
int LongOctalConst = 87;
int UnsignedOctalConst = 88;
int IntIntConst = 89;
int LongIntConst = 90;
int UnsignedIntConst = 91;
int IntHexConst = 92;
int LongHexConst = 93;
int UnsignedHexConst = 94;
int FloatDoubleConst = 95;
int DoubleDoubleConst = 96;
int LongDoubleConst = 97;
int NTypedefName = 98;
int NInitDecl = 99;
int NDeclarator = 100;
int NStructDeclarator = 101;
int NDeclaration = 102;
int NCast = 103;
int NPointerGroup = 104;
int NExpressionGroup = 105;
int NFunctionCallArgs = 106;
int NNonemptyAbstractDeclarator = 107;
int NInitializer = 108;
int NStatementExpr = 109;
int NEmptyExpression = 110;
int NParameterTypeList = 111;
int NFunctionDef = 112;
int NCompoundStatement = 113;
int NParameterDeclaration = 114;
int NCommaExpr = 115;
int NUnaryExpr = 116;
int NLabel = 117;
int NPostfixExpr = 118;
int NRangeExpr = 119;
int NStringSeq = 120;
int NInitializerElementLabel = 121;
int NLcurlyInitializer = 122;
int NAsmAttribute = 123;
int NGnuAsmExpr = 124;
int NTypeMissing = 125;
int Vocabulary = 126;
int Whitespace = 127;
int Comment = 128;
int CPPComment = 129;
int PREPROC_DIRECTIVE = 130;
int Space = 131;
int LineDirective = 132;
int BadStringLiteral = 133;
int Escape = 134;
int Digit = 135;
int LongSuffix = 136;
int UnsignedSuffix = 137;
int FloatSuffix = 138;
int Exponent = 139;
int Number = 140;
int LITERAL___label__ = 141;
int LITERAL_inline = 142;
int LITERAL_byte = 143;
int LITERAL_boolean = 144;
int LITERAL_Servo = 145;
int LITERAL_Wire = 146;
int LITERAL_typeof = 147;
int LITERAL___complex = 148;
int LITERAL___attribute = 149;
int LITERAL___alignof = 150;
int LITERAL___real = 151;
int LITERAL___imag = 152;
int LITERAL___extension__ = 153;
int IntSuffix = 154;
int NumberSuffix = 155;
int IDMEAT = 156;
int WideCharLiteral = 157;
int WideStringLiteral = 158;
}

View File

@ -0,0 +1,157 @@
// $ANTLR 2.7.2: expandedWParser.g -> WLexerTokenTypes.txt$
WLexer // output token vocab name
LITERAL_typedef="typedef"=4
LITERAL_asm="asm"=5
LITERAL_volatile="volatile"=6
LCURLY=7
RCURLY=8
SEMI=9
LITERAL_struct="struct"=10
LITERAL_union="union"=11
LITERAL_enum="enum"=12
LITERAL_auto="auto"=13
LITERAL_register="register"=14
LITERAL_extern="extern"=15
LITERAL_static="static"=16
LITERAL_const="const"=17
LITERAL_void="void"=18
LITERAL_char="char"=19
LITERAL_short="short"=20
LITERAL_int="int"=21
LITERAL_long="long"=22
LITERAL_float="float"=23
LITERAL_double="double"=24
LITERAL_signed="signed"=25
LITERAL_unsigned="unsigned"=26
ID=27
COMMA=28
COLON=29
ASSIGN=30
STAR=31
LPAREN=32
RPAREN=33
LBRACKET=34
RBRACKET=35
VARARGS=36
LITERAL_while="while"=37
LITERAL_do="do"=38
LITERAL_for="for"=39
LITERAL_goto="goto"=40
LITERAL_continue="continue"=41
LITERAL_break="break"=42
LITERAL_return="return"=43
LITERAL_case="case"=44
LITERAL_default="default"=45
LITERAL_if="if"=46
LITERAL_else="else"=47
LITERAL_switch="switch"=48
DIV_ASSIGN=49
PLUS_ASSIGN=50
MINUS_ASSIGN=51
STAR_ASSIGN=52
MOD_ASSIGN=53
RSHIFT_ASSIGN=54
LSHIFT_ASSIGN=55
BAND_ASSIGN=56
BOR_ASSIGN=57
BXOR_ASSIGN=58
QUESTION=59
LOR=60
LAND=61
BOR=62
BXOR=63
BAND=64
EQUAL=65
NOT_EQUAL=66
LT=67
LTE=68
GT=69
GTE=70
LSHIFT=71
RSHIFT=72
PLUS=73
MINUS=74
DIV=75
MOD=76
INC=77
DEC=78
LITERAL_sizeof="sizeof"=79
BNOT=80
LNOT=81
PTR=82
DOT=83
CharLiteral=84
StringLiteral=85
IntOctalConst=86
LongOctalConst=87
UnsignedOctalConst=88
IntIntConst=89
LongIntConst=90
UnsignedIntConst=91
IntHexConst=92
LongHexConst=93
UnsignedHexConst=94
FloatDoubleConst=95
DoubleDoubleConst=96
LongDoubleConst=97
NTypedefName=98
NInitDecl=99
NDeclarator=100
NStructDeclarator=101
NDeclaration=102
NCast=103
NPointerGroup=104
NExpressionGroup=105
NFunctionCallArgs=106
NNonemptyAbstractDeclarator=107
NInitializer=108
NStatementExpr=109
NEmptyExpression=110
NParameterTypeList=111
NFunctionDef=112
NCompoundStatement=113
NParameterDeclaration=114
NCommaExpr=115
NUnaryExpr=116
NLabel=117
NPostfixExpr=118
NRangeExpr=119
NStringSeq=120
NInitializerElementLabel=121
NLcurlyInitializer=122
NAsmAttribute=123
NGnuAsmExpr=124
NTypeMissing=125
Vocabulary=126
Whitespace=127
Comment=128
CPPComment=129
PREPROC_DIRECTIVE("a line directive")=130
Space=131
LineDirective=132
BadStringLiteral=133
Escape=134
Digit=135
LongSuffix=136
UnsignedSuffix=137
FloatSuffix=138
Exponent=139
Number=140
LITERAL___label__="__label__"=141
LITERAL_inline="inline"=142
LITERAL_byte="byte"=143
LITERAL_boolean="boolean"=144
LITERAL_Servo="Servo"=145
LITERAL_Wire="Wire"=146
LITERAL_typeof="typeof"=147
LITERAL___complex="__complex"=148
LITERAL___attribute="__attribute"=149
LITERAL___alignof="__alignof"=150
LITERAL___real="__real"=151
LITERAL___imag="__imag"=152
LITERAL___extension__="__extension__"=153
IntSuffix=154
NumberSuffix=155
IDMEAT=156
WideCharLiteral=157
WideStringLiteral=158

856
app/preproc/WParser.g Executable file
View File

@ -0,0 +1,856 @@
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Copyright (c) Non, Inc. 1998 -- All Rights Reserved
PROJECT: C Compiler
MODULE: WParser
FILE: WParser.g
AUTHOR: Monty Zukowski (jamz@cdsnet.net) April 28, 1998
MODIFICATIONS: Hernando Barragan added support for the Wiring language
DESCRIPTION:
This is a grammar for the GNU C compiler. It is a
grammar subclass of StdCParser, overriding only those
rules which are different from Standard C.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
header {
package processing.app.preproc;
import java.io.*;
import antlr.CommonAST;
import antlr.DumpASTVisitor;
}
class WParser extends StdCParser;
options
{
k = 2;
exportVocab = W;
buildAST = true;
ASTLabelType = "TNode";
defaultErrorHandler = false;
// Copied following options from java grammar.
codeGenMakeSwitchThreshold = 2;
codeGenBitsetTestThreshold = 3;
}
{
// Suppport C++-style single-line comments?
public static boolean CPPComments = true;
// access to symbol table
public CSymbolTable symbolTable = new CSymbolTable();
// source for names to unnamed scopes
protected int unnamedScopeCounter = 0;
public boolean isTypedefName(String name) {
boolean returnValue = false;
TNode node = symbolTable.lookupNameInCurrentScope(name);
for (; node != null; node = (TNode) node.getNextSibling() ) {
if(node.getType() == LITERAL_typedef) {
returnValue = true;
break;
}
}
return returnValue;
}
public String getAScopeName() {
return "" + (unnamedScopeCounter++);
}
public void pushScope(String scopeName) {
symbolTable.pushScope(scopeName);
}
public void popScope() {
symbolTable.popScope();
}
int traceDepth = 0;
public void reportError(RecognitionException ex) {
try {
System.err.println("ANTLR Parsing Error: "+ex + " token name:" + tokenNames[LA(1)]);
ex.printStackTrace(System.err);
}
catch (TokenStreamException e) {
System.err.println("ANTLR Parsing Error: "+ex);
ex.printStackTrace(System.err);
}
}
public void reportError(String s) {
System.err.println("ANTLR Parsing Error from String: " + s);
}
public void reportWarning(String s) {
System.err.println("ANTLR Parsing Warning from String: " + s);
}
public void match(int t) throws MismatchedTokenException {
boolean debugging = false;
if ( debugging ) {
for (int x=0; x<traceDepth; x++) System.out.print(" ");
try {
System.out.println("Match("+tokenNames[t]+") with LA(1)="+
tokenNames[LA(1)] + ((inputState.guessing>0)?" [inputState.guessing "+ inputState.guessing + "]":""));
}
catch (TokenStreamException e) {
System.out.println("Match("+tokenNames[t]+") " + ((inputState.guessing>0)?" [inputState.guessing "+ inputState.guessing + "]":""));
}
}
try {
if ( LA(1)!=t ) {
if ( debugging ){
for (int x=0; x<traceDepth; x++) System.out.print(" ");
System.out.println("token mismatch: "+tokenNames[LA(1)]
+ "!="+tokenNames[t]);
}
throw new MismatchedTokenException(tokenNames, LT(1), t, false, getFilename());
} else {
// mark token as consumed -- fetch next token deferred until LA/LT
consume();
}
}
catch (TokenStreamException e) {
}
}
public void traceIn(String rname) {
traceDepth += 1;
for (int x=0; x<traceDepth; x++) System.out.print(" ");
try {
System.out.println("> "+rname+"; LA(1)==("+ tokenNames[LT(1).getType()]
+ ") " + LT(1).getText() + " [inputState.guessing "+ inputState.guessing + "]");
}
catch (TokenStreamException e) {
}
}
public void traceOut(String rname) {
for (int x=0; x<traceDepth; x++) System.out.print(" ");
try {
System.out.println("< "+rname+"; LA(1)==("+ tokenNames[LT(1).getType()]
+ ") "+LT(1).getText() + " [inputState.guessing "+ inputState.guessing + "]");
}
catch (TokenStreamException e) {
}
traceDepth -= 1;
}
}
translationUnit
: ( externalList )? /* Empty source files are allowed. */
;
asm_expr
: "asm"^
("volatile")? LCURLY expr RCURLY ( SEMI )+
;
idList
: ID ( options{warnWhenFollowAmbig=false;}: COMMA ID )*
;
externalDef
: ( "typedef" | declaration )=> declaration
| ( functionPrefix )=> functionDef
| typelessDeclaration
| asm_expr
| SEMI
;
/* these two are here because GCC allows "cat = 13;" as a valid program! */
functionPrefix
{ String declName; }
: ( (functionDeclSpecifiers)=> ds:functionDeclSpecifiers
| //epsilon
)
declName = d:declarator[true]
( declaration )* (VARARGS)? ( SEMI )*
LCURLY
;
typelessDeclaration
{ AST typeMissing = #[NTypeMissing]; }
: initDeclList[typeMissing] SEMI { ## = #( #[NTypeMissing], ##); }
;
initializer
: ( ( ( (initializerElementLabel)=> initializerElementLabel )?
( assignExpr | lcurlyInitializer ) { ## = #( #[NInitializer], ## ); }
)
| lcurlyInitializer
)
;
// GCC allows more specific initializers
initializerElementLabel
: ( ( LBRACKET ((constExpr VARARGS)=> rangeExpr | constExpr) RBRACKET (ASSIGN)? )
| ID COLON
| DOT ID ASSIGN
)
{ ## = #( #[NInitializerElementLabel], ##) ; }
;
// GCC allows empty initializer lists
lcurlyInitializer
:
LCURLY^ (initializerList ( COMMA! )? )? RCURLY
{ ##.setType( NLcurlyInitializer ); }
;
initializerList
: initializer ( options{warnWhenFollowAmbig=false;}:COMMA! initializer )*
;
declarator[boolean isFunctionDefinition] returns [String declName]
{ declName = ""; }
:
( pointerGroup )?
( id:ID { declName = id.getText(); }
| LPAREN declName = declarator[false] RPAREN
)
( declaratorParamaterList[isFunctionDefinition, declName]
| LBRACKET ( expr )? RBRACKET
)*
{ ## = #( #[NDeclarator], ## ); }
;
declaratorParamaterList[boolean isFunctionDefinition, String declName]
:
LPAREN^
{
if (isFunctionDefinition) {
pushScope(declName);
}
else {
pushScope("!"+declName);
}
}
(
(declSpecifiers)=> parameterTypeList
| (idList)?
)
{
popScope();
}
( COMMA! )?
RPAREN
{ ##.setType(NParameterTypeList); }
;
parameterTypeList
: parameterDeclaration
( options {
warnWhenFollowAmbig = false;
} :
( COMMA | SEMI )
parameterDeclaration
)*
( ( COMMA | SEMI )
VARARGS
)?
;
declarationList
: ( options { // this loop properly aborts when
// it finds a non-typedefName ID MBZ
warnWhenFollowAmbig = false;
} :
localLabelDeclaration
| ( declarationPredictor )=> declaration
)+
;
localLabelDeclaration
: ( //GNU note: any __label__ declarations must come before regular declarations.
"__label__"^ ID (options{warnWhenFollowAmbig=false;}: COMMA! ID)* ( COMMA! )? ( SEMI! )+
)
;
declaration
{ AST ds1 = null; }
: ds:declSpecifiers { ds1 = astFactory.dupList(#ds); }
(
initDeclList[ds1]
)?
( SEMI )+
{ ## = #( #[NDeclaration], ##); }
;
functionStorageClassSpecifier
: "extern"
| "static"
| "inline"
;
typeSpecifier [int specCount] returns [int retSpecCount]
{ retSpecCount = specCount + 1; }
:
( "void"
| "char"
| "short"
| "int"
| "long"
| "float"
| "double"
| "signed"
| "unsigned"
| "byte"
| "boolean"
| "Servo"
| "Wire"
| structOrUnionSpecifier ( options{warnWhenFollowAmbig=false;}: attributeDecl )*
| enumSpecifier
| { specCount==0 }? typedefName
| "typeof"^ LPAREN
( ( typeName )=> typeName
| expr
)
RPAREN
| "__complex"
)
;
structOrUnionSpecifier
{ String scopeName; }
: sou:structOrUnion!
( ( ID LCURLY )=> i:ID l:LCURLY
{
scopeName = #sou.getText() + " " + #i.getText();
#l.setText(scopeName);
pushScope(scopeName);
}
( structDeclarationList )?
{ popScope();}
RCURLY
| l1:LCURLY
{
scopeName = getAScopeName();
#l1.setText(scopeName);
pushScope(scopeName);
}
( structDeclarationList )?
{ popScope(); }
RCURLY
| ID
)
{
## = #( #sou, ## );
}
;
structDeclaration
: specifierQualifierList structDeclaratorList ( COMMA! )? ( SEMI! )+
;
structDeclaratorList
: structDeclarator ( options{warnWhenFollowAmbig=false;}: COMMA! structDeclarator )*
;
structDeclarator
: ( declarator[false] )?
( COLON constExpr )?
( attributeDecl )*
{ ## = #( #[NStructDeclarator], ##); }
;
enumSpecifier
: "enum"^
( ( ID LCURLY )=> i:ID LCURLY enumList[i.getText()] RCURLY
| LCURLY enumList["anonymous"] RCURLY
| ID
)
;
enumList[String enumName]
: enumerator[enumName] ( options{warnWhenFollowAmbig=false;}: COMMA! enumerator[enumName] )* ( COMMA! )?
;
initDeclList[AST declarationSpecifiers]
: initDecl[declarationSpecifiers]
( options{warnWhenFollowAmbig=false;}: COMMA! initDecl[declarationSpecifiers] )*
( COMMA! )?
;
initDecl[AST declarationSpecifiers]
{ String declName = ""; }
: declName = d:declarator[false]
{ AST ds1, d1;
ds1 = astFactory.dupList(declarationSpecifiers);
d1 = astFactory.dupList(#d);
symbolTable.add(declName, #(null, ds1, d1) );
}
( attributeDecl )*
( ASSIGN initializer
| COLON expr
)?
{ ## = #( #[NInitDecl], ## ); }
;
attributeDecl
: "__attribute"^ LPAREN LPAREN attributeList RPAREN RPAREN
| "asm"^ LPAREN stringConst RPAREN { ##.setType( NAsmAttribute ); }
;
attributeList
: attribute ( options{warnWhenFollowAmbig=false;}: COMMA attribute)* ( COMMA )?
;
attribute
: ( ~(LPAREN | RPAREN | COMMA)
| LPAREN attributeList RPAREN
)*
;
compoundStatement[String scopeName]
: LCURLY^
{
pushScope(scopeName);
}
( //this ambiguity is ok, declarationList and nestedFunctionDef end properly
options {
warnWhenFollowAmbig = false;
} :
( "typedef" | "__label__" | declaration )=> declarationList
| (nestedFunctionDef)=> nestedFunctionDef
)*
( statementList )?
{ popScope(); }
RCURLY
{ ##.setType( NCompoundStatement ); ##.setAttribute( "scopeName", scopeName ); }
;
nestedFunctionDef
{ String declName; }
: ( "auto" )? //only for nested functions
( (functionDeclSpecifiers)=> ds:functionDeclSpecifiers
)?
declName = d:declarator[false]
{
AST d2, ds2;
d2 = astFactory.dupList(#d);
ds2 = astFactory.dupList(#ds);
symbolTable.add(declName, #(null, ds2, d2));
pushScope(declName);
}
( declaration )*
{ popScope(); }
compoundStatement[declName]
{ ## = #( #[NFunctionDef], ## );}
;
statement
: SEMI // Empty statements
| compoundStatement[getAScopeName()] // Group of statements
| expr SEMI! { ## = #( #[NStatementExpr], ## );} // Expressions
// Iteration statements:
| "while"^ LPAREN! expr RPAREN! statement
| "do"^ statement "while"! LPAREN! expr RPAREN! SEMI!
|! "for"
LPAREN ( e1:expr )? SEMI ( e2:expr )? SEMI ( e3:expr )? RPAREN
s:statement
{
if ( #e1 == null) { #e1 = #[ NEmptyExpression ]; }
if ( #e2 == null) { #e2 = #[ NEmptyExpression ]; }
if ( #e3 == null) { #e3 = #[ NEmptyExpression ]; }
## = #( #[LITERAL_for, "for"], #e1, #e2, #e3, #s );
}
// Jump statements:
| "goto"^ expr SEMI!
| "continue" SEMI!
| "break" SEMI!
| "return"^ ( expr )? SEMI!
| ID COLON! (options {warnWhenFollowAmbig=false;}: statement)? { ## = #( #[NLabel], ## ); }
// GNU allows range expressions in case statements
| "case"^ ((constExpr VARARGS)=> rangeExpr | constExpr) COLON! ( options{warnWhenFollowAmbig=false;}:statement )?
| "default"^ COLON! ( options{warnWhenFollowAmbig=false;}: statement )?
// Selection statements:
| "if"^
LPAREN! expr RPAREN! statement
( //standard if-else ambiguity
options {
warnWhenFollowAmbig = false;
} :
"else" statement )?
| "switch"^ LPAREN! expr RPAREN! statement
;
conditionalExpr
: logicalOrExpr
( QUESTION^ (expr)? COLON conditionalExpr )?
;
rangeExpr //used in initializers only
: constExpr VARARGS constExpr
{ ## = #(#[NRangeExpr], ##); }
;
castExpr
: ( LPAREN typeName RPAREN )=>
LPAREN^ typeName RPAREN ( castExpr | lcurlyInitializer )
{ ##.setType(NCast); }
| unaryExpr
;
nonemptyAbstractDeclarator
: (
pointerGroup
( (LPAREN
( nonemptyAbstractDeclarator
| parameterTypeList
)?
( COMMA! )?
RPAREN)
| (LBRACKET (expr)? RBRACKET)
)*
| ( (LPAREN
( nonemptyAbstractDeclarator
| parameterTypeList
)?
( COMMA! )?
RPAREN)
| (LBRACKET (expr)? RBRACKET)
)+
)
{ ## = #( #[NNonemptyAbstractDeclarator], ## ); }
;
unaryExpr
: postfixExpr
| INC^ castExpr
| DEC^ castExpr
| u:unaryOperator castExpr { ## = #( #[NUnaryExpr], ## ); }
| "sizeof"^
( ( LPAREN typeName )=> LPAREN typeName RPAREN
| unaryExpr
)
| "__alignof"^
( ( LPAREN typeName )=> LPAREN typeName RPAREN
| unaryExpr
)
| gnuAsmExpr
;
unaryOperator
: BAND
| STAR
| PLUS
| MINUS
| BNOT //also stands for complex conjugation
| LNOT
| LAND //for label dereference (&&label)
| "__real"
| "__imag"
;
gnuAsmExpr
: "asm"^ ("volatile")?
LPAREN stringConst
( options { warnWhenFollowAmbig = false; }:
COLON (strOptExprPair ( COMMA strOptExprPair)* )?
( options { warnWhenFollowAmbig = false; }:
COLON (strOptExprPair ( COMMA strOptExprPair)* )?
)?
)?
( COLON stringConst ( COMMA stringConst)* )?
RPAREN
{ ##.setType(NGnuAsmExpr); }
;
//GCC requires the PARENs
strOptExprPair
: stringConst ( LPAREN expr RPAREN )?
;
primaryExpr
: ID
| Number
| charConst
| stringConst
// JTC:
// ID should catch the enumerator
// leaving it in gives ambiguous err
// | enumerator
| (LPAREN LCURLY) => LPAREN^ compoundStatement[getAScopeName()] RPAREN
| LPAREN^ expr RPAREN { ##.setType(NExpressionGroup); }
;
{
// import CToken;
import java.io.*;
// import LineObject;
import antlr.*;
}
class WLexer extends StdCLexer;
options
{
k = 3;
importVocab = W;
testLiterals = false;
}
tokens {
LITERAL___extension__ = "__extension__";
}
{
public void initialize(String src)
{
setOriginalSource(src);
initialize();
}
public void initialize()
{
literals.put(new ANTLRHashString("__alignof__", this), new Integer(LITERAL___alignof));
literals.put(new ANTLRHashString("__asm", this), new Integer(LITERAL_asm));
literals.put(new ANTLRHashString("__asm__", this), new Integer(LITERAL_asm));
literals.put(new ANTLRHashString("__attribute__", this), new Integer(LITERAL___attribute));
literals.put(new ANTLRHashString("__complex__", this), new Integer(LITERAL___complex));
literals.put(new ANTLRHashString("__const", this), new Integer(LITERAL_const));
literals.put(new ANTLRHashString("__const__", this), new Integer(LITERAL_const));
literals.put(new ANTLRHashString("__imag__", this), new Integer(LITERAL___imag));
literals.put(new ANTLRHashString("__inline", this), new Integer(LITERAL_inline));
literals.put(new ANTLRHashString("__inline__", this), new Integer(LITERAL_inline));
literals.put(new ANTLRHashString("__real__", this), new Integer(LITERAL___real));
literals.put(new ANTLRHashString("__signed", this), new Integer(LITERAL_signed));
literals.put(new ANTLRHashString("__signed__", this), new Integer(LITERAL_signed));
literals.put(new ANTLRHashString("__typeof", this), new Integer(LITERAL_typeof));
literals.put(new ANTLRHashString("__typeof__", this), new Integer(LITERAL_typeof));
literals.put(new ANTLRHashString("__volatile", this), new Integer(LITERAL_volatile));
literals.put(new ANTLRHashString("__volatile__", this), new Integer(LITERAL_volatile));
}
LineObject lineObject = new LineObject();
String originalSource = "";
PreprocessorInfoChannel preprocessorInfoChannel = new PreprocessorInfoChannel();
int tokenNumber = 0;
boolean countingTokens = true;
int deferredLineCount = 0;
public void setCountingTokens(boolean ct)
{
countingTokens = ct;
if ( countingTokens ) {
tokenNumber = 0;
}
else {
tokenNumber = 1;
}
}
public void setOriginalSource(String src)
{
originalSource = src;
lineObject.setSource(src);
}
public void setSource(String src)
{
lineObject.setSource(src);
}
public PreprocessorInfoChannel getPreprocessorInfoChannel()
{
return preprocessorInfoChannel;
}
public void setPreprocessingDirective(String pre)
{
preprocessorInfoChannel.addLineForTokenNumber( pre, new Integer(tokenNumber) );
}
protected Token makeToken(int t)
{
if ( t != Token.SKIP && countingTokens) {
tokenNumber++;
}
CToken tok = (CToken) super.makeToken(t);
tok.setLine(lineObject.line);
tok.setSource(lineObject.source);
tok.setTokenNumber(tokenNumber);
lineObject.line += deferredLineCount;
deferredLineCount = 0;
return tok;
}
public void deferredNewline() {
deferredLineCount++;
}
public void newline() {
lineObject.newline();
}
}
Whitespace
: ( ( ' ' | '\t' | '\014')
| "\r\n" { newline(); }
| ( '\n' | '\r' ) { newline(); }
) { _ttype = Token.SKIP; }
;
protected
Escape
: '\\'
( options{warnWhenFollowAmbig=false;}:
~('0'..'7' | 'x')
| ('0'..'3') ( options{warnWhenFollowAmbig=false;}: Digit )*
| ('4'..'7') ( options{warnWhenFollowAmbig=false;}: Digit )*
| 'x' ( options{warnWhenFollowAmbig=false;}: Digit | 'a'..'f' | 'A'..'F' )+
)
;
protected IntSuffix
: 'L'
| 'l'
| 'U'
| 'u'
| 'I'
| 'i'
| 'J'
| 'j'
;
protected NumberSuffix
:
IntSuffix
| 'F'
| 'f'
;
Number
: ( ( Digit )+ ( '.' | 'e' | 'E' ) )=> ( Digit )+
( '.' ( Digit )* ( Exponent )?
| Exponent
)
( NumberSuffix
)*
| ( "..." )=> "..." { _ttype = VARARGS; }
| '.' { _ttype = DOT; }
( ( Digit )+ ( Exponent )?
{ _ttype = Number; }
( NumberSuffix
)*
)?
| '0' ( '0'..'7' )*
( NumberSuffix
)*
| '1'..'9' ( Digit )*
( NumberSuffix
)*
| '0' ( 'x' | 'X' ) ( 'a'..'f' | 'A'..'F' | Digit )+
( IntSuffix
)*
;
IDMEAT
:
i:ID {
if ( i.getType() == LITERAL___extension__ ) {
$setType(Token.SKIP);
}
else {
$setType(i.getType());
}
}
;
protected ID
options
{
testLiterals = true;
}
: ( 'a'..'z' | 'A'..'Z' | '_' | '$')
( 'a'..'z' | 'A'..'Z' | '_' | '$' | '0'..'9' )*
;
WideCharLiteral
:
'L' CharLiteral
{ $setType(CharLiteral); }
;
WideStringLiteral
:
'L' StringLiteral
{ $setType(StringLiteral); }
;
StringLiteral
:
'"'
( ('\\' ~('\n'))=> Escape
| ( '\r' { newline(); }
| '\n' {
newline();
}
| '\\' '\n' {
newline();
}
)
| ~( '"' | '\r' | '\n' | '\\' )
)*
'"'
;

6718
app/preproc/WParser.java Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,162 @@
// $ANTLR 2.7.2: "expandedWParser.g" -> "WLexer.java"$
package processing.app.preproc;
import java.io.*;
import antlr.CommonAST;
import antlr.DumpASTVisitor;
public interface WTokenTypes {
int EOF = 1;
int NULL_TREE_LOOKAHEAD = 3;
int LITERAL_typedef = 4;
int LITERAL_asm = 5;
int LITERAL_volatile = 6;
int LCURLY = 7;
int RCURLY = 8;
int SEMI = 9;
int LITERAL_struct = 10;
int LITERAL_union = 11;
int LITERAL_enum = 12;
int LITERAL_auto = 13;
int LITERAL_register = 14;
int LITERAL_extern = 15;
int LITERAL_static = 16;
int LITERAL_const = 17;
int LITERAL_void = 18;
int LITERAL_char = 19;
int LITERAL_short = 20;
int LITERAL_int = 21;
int LITERAL_long = 22;
int LITERAL_float = 23;
int LITERAL_double = 24;
int LITERAL_signed = 25;
int LITERAL_unsigned = 26;
int ID = 27;
int COMMA = 28;
int COLON = 29;
int ASSIGN = 30;
int STAR = 31;
int LPAREN = 32;
int RPAREN = 33;
int LBRACKET = 34;
int RBRACKET = 35;
int VARARGS = 36;
int LITERAL_while = 37;
int LITERAL_do = 38;
int LITERAL_for = 39;
int LITERAL_goto = 40;
int LITERAL_continue = 41;
int LITERAL_break = 42;
int LITERAL_return = 43;
int LITERAL_case = 44;
int LITERAL_default = 45;
int LITERAL_if = 46;
int LITERAL_else = 47;
int LITERAL_switch = 48;
int DIV_ASSIGN = 49;
int PLUS_ASSIGN = 50;
int MINUS_ASSIGN = 51;
int STAR_ASSIGN = 52;
int MOD_ASSIGN = 53;
int RSHIFT_ASSIGN = 54;
int LSHIFT_ASSIGN = 55;
int BAND_ASSIGN = 56;
int BOR_ASSIGN = 57;
int BXOR_ASSIGN = 58;
int QUESTION = 59;
int LOR = 60;
int LAND = 61;
int BOR = 62;
int BXOR = 63;
int BAND = 64;
int EQUAL = 65;
int NOT_EQUAL = 66;
int LT = 67;
int LTE = 68;
int GT = 69;
int GTE = 70;
int LSHIFT = 71;
int RSHIFT = 72;
int PLUS = 73;
int MINUS = 74;
int DIV = 75;
int MOD = 76;
int INC = 77;
int DEC = 78;
int LITERAL_sizeof = 79;
int BNOT = 80;
int LNOT = 81;
int PTR = 82;
int DOT = 83;
int CharLiteral = 84;
int StringLiteral = 85;
int IntOctalConst = 86;
int LongOctalConst = 87;
int UnsignedOctalConst = 88;
int IntIntConst = 89;
int LongIntConst = 90;
int UnsignedIntConst = 91;
int IntHexConst = 92;
int LongHexConst = 93;
int UnsignedHexConst = 94;
int FloatDoubleConst = 95;
int DoubleDoubleConst = 96;
int LongDoubleConst = 97;
int NTypedefName = 98;
int NInitDecl = 99;
int NDeclarator = 100;
int NStructDeclarator = 101;
int NDeclaration = 102;
int NCast = 103;
int NPointerGroup = 104;
int NExpressionGroup = 105;
int NFunctionCallArgs = 106;
int NNonemptyAbstractDeclarator = 107;
int NInitializer = 108;
int NStatementExpr = 109;
int NEmptyExpression = 110;
int NParameterTypeList = 111;
int NFunctionDef = 112;
int NCompoundStatement = 113;
int NParameterDeclaration = 114;
int NCommaExpr = 115;
int NUnaryExpr = 116;
int NLabel = 117;
int NPostfixExpr = 118;
int NRangeExpr = 119;
int NStringSeq = 120;
int NInitializerElementLabel = 121;
int NLcurlyInitializer = 122;
int NAsmAttribute = 123;
int NGnuAsmExpr = 124;
int NTypeMissing = 125;
int Vocabulary = 126;
int Whitespace = 127;
int Comment = 128;
int CPPComment = 129;
int PREPROC_DIRECTIVE = 130;
int Space = 131;
int LineDirective = 132;
int BadStringLiteral = 133;
int Escape = 134;
int Digit = 135;
int LongSuffix = 136;
int UnsignedSuffix = 137;
int FloatSuffix = 138;
int Exponent = 139;
int Number = 140;
int LITERAL___label__ = 141;
int LITERAL_inline = 142;
int LITERAL_byte = 143;
int LITERAL_boolean = 144;
int LITERAL_Servo = 145;
int LITERAL_Wire = 146;
int LITERAL_typeof = 147;
int LITERAL___complex = 148;
int LITERAL___attribute = 149;
int LITERAL___alignof = 150;
int LITERAL___real = 151;
int LITERAL___imag = 152;
}

151
app/preproc/WTokenTypes.txt Normal file
View File

@ -0,0 +1,151 @@
// $ANTLR 2.7.2: expandedWParser.g -> WTokenTypes.txt$
W // output token vocab name
LITERAL_typedef="typedef"=4
LITERAL_asm="asm"=5
LITERAL_volatile="volatile"=6
LCURLY=7
RCURLY=8
SEMI=9
LITERAL_struct="struct"=10
LITERAL_union="union"=11
LITERAL_enum="enum"=12
LITERAL_auto="auto"=13
LITERAL_register="register"=14
LITERAL_extern="extern"=15
LITERAL_static="static"=16
LITERAL_const="const"=17
LITERAL_void="void"=18
LITERAL_char="char"=19
LITERAL_short="short"=20
LITERAL_int="int"=21
LITERAL_long="long"=22
LITERAL_float="float"=23
LITERAL_double="double"=24
LITERAL_signed="signed"=25
LITERAL_unsigned="unsigned"=26
ID=27
COMMA=28
COLON=29
ASSIGN=30
STAR=31
LPAREN=32
RPAREN=33
LBRACKET=34
RBRACKET=35
VARARGS=36
LITERAL_while="while"=37
LITERAL_do="do"=38
LITERAL_for="for"=39
LITERAL_goto="goto"=40
LITERAL_continue="continue"=41
LITERAL_break="break"=42
LITERAL_return="return"=43
LITERAL_case="case"=44
LITERAL_default="default"=45
LITERAL_if="if"=46
LITERAL_else="else"=47
LITERAL_switch="switch"=48
DIV_ASSIGN=49
PLUS_ASSIGN=50
MINUS_ASSIGN=51
STAR_ASSIGN=52
MOD_ASSIGN=53
RSHIFT_ASSIGN=54
LSHIFT_ASSIGN=55
BAND_ASSIGN=56
BOR_ASSIGN=57
BXOR_ASSIGN=58
QUESTION=59
LOR=60
LAND=61
BOR=62
BXOR=63
BAND=64
EQUAL=65
NOT_EQUAL=66
LT=67
LTE=68
GT=69
GTE=70
LSHIFT=71
RSHIFT=72
PLUS=73
MINUS=74
DIV=75
MOD=76
INC=77
DEC=78
LITERAL_sizeof="sizeof"=79
BNOT=80
LNOT=81
PTR=82
DOT=83
CharLiteral=84
StringLiteral=85
IntOctalConst=86
LongOctalConst=87
UnsignedOctalConst=88
IntIntConst=89
LongIntConst=90
UnsignedIntConst=91
IntHexConst=92
LongHexConst=93
UnsignedHexConst=94
FloatDoubleConst=95
DoubleDoubleConst=96
LongDoubleConst=97
NTypedefName=98
NInitDecl=99
NDeclarator=100
NStructDeclarator=101
NDeclaration=102
NCast=103
NPointerGroup=104
NExpressionGroup=105
NFunctionCallArgs=106
NNonemptyAbstractDeclarator=107
NInitializer=108
NStatementExpr=109
NEmptyExpression=110
NParameterTypeList=111
NFunctionDef=112
NCompoundStatement=113
NParameterDeclaration=114
NCommaExpr=115
NUnaryExpr=116
NLabel=117
NPostfixExpr=118
NRangeExpr=119
NStringSeq=120
NInitializerElementLabel=121
NLcurlyInitializer=122
NAsmAttribute=123
NGnuAsmExpr=124
NTypeMissing=125
Vocabulary=126
Whitespace=127
Comment=128
CPPComment=129
PREPROC_DIRECTIVE("a line directive")=130
Space=131
LineDirective=132
BadStringLiteral=133
Escape=134
Digit=135
LongSuffix=136
UnsignedSuffix=137
FloatSuffix=138
Exponent=139
Number=140
LITERAL___label__="__label__"=141
LITERAL_inline="inline"=142
LITERAL_byte="byte"=143
LITERAL_boolean="boolean"=144
LITERAL_Servo="Servo"=145
LITERAL_Wire="Wire"=146
LITERAL_typeof="typeof"=147
LITERAL___complex="__complex"=148
LITERAL___attribute="__attribute"=149
LITERAL___alignof="__alignof"=150
LITERAL___real="__real"=151
LITERAL___imag="__imag"=152

857
app/preproc/WTreeParser.g Executable file
View File

@ -0,0 +1,857 @@
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Copyright (c) Non, Inc. 1998 -- All Rights Reserved
PROJECT: C Compiler
MODULE: WTreeParser
FILE: WTreeParser.g
AUTHOR: Monty Zukowski (jamz@cdsnet.net) April 28, 1998
MODIFICATIONS: Hernando Barragan added support for the Wiring language
DESCRIPTION:
This tree grammar is for a Gnu C AST. No actions in it,
subclass to do something useful.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
header {
package processing.app.preproc;
import java.io.*;
import antlr.CommonAST;
import antlr.DumpASTVisitor;
}
class WTreeParser extends TreeParser;
options
{
importVocab = W;
buildAST = false;
ASTLabelType = "TNode";
// Copied following options from java grammar.
codeGenMakeSwitchThreshold = 2;
codeGenBitsetTestThreshold = 3;
}
{
int traceDepth = 0;
public void reportError(RecognitionException ex) {
if ( ex != null) {
System.err.println("ANTLR Tree Parsing RecognitionException Error: " + ex.getClass().getName() + " " + ex );
ex.printStackTrace(System.err);
}
}
public void reportError(NoViableAltException ex) {
System.err.println("ANTLR Tree Parsing NoViableAltException Error: " + ex.toString());
TNode.printTree( ex.node );
ex.printStackTrace(System.err);
}
public void reportError(MismatchedTokenException ex) {
if ( ex != null) {
TNode.printTree( ex.node );
System.err.println("ANTLR Tree Parsing MismatchedTokenException Error: " + ex );
ex.printStackTrace(System.err);
}
}
public void reportError(String s) {
System.err.println("ANTLR Error from String: " + s);
}
public void reportWarning(String s) {
System.err.println("ANTLR Warning from String: " + s);
}
protected void match(AST t, int ttype) throws MismatchedTokenException {
//System.out.println("match("+ttype+"); cursor is "+t);
super.match(t, ttype);
}
public void match(AST t, BitSet b) throws MismatchedTokenException {
//System.out.println("match("+b+"); cursor is "+t);
super.match(t, b);
}
protected void matchNot(AST t, int ttype) throws MismatchedTokenException {
//System.out.println("matchNot("+ttype+"); cursor is "+t);
super.matchNot(t, ttype);
}
public void traceIn(String rname, AST t) {
traceDepth += 1;
for (int x=0; x<traceDepth; x++) System.out.print(" ");
super.traceIn(rname, t);
}
public void traceOut(String rname, AST t) {
for (int x=0; x<traceDepth; x++) System.out.print(" ");
super.traceOut(rname, t);
traceDepth -= 1;
}
}
translationUnit options {
defaultErrorHandler=false;
}
: ( externalList )?
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
externalList
: ( externalDef )+
;
externalDef
: declaration
| functionDef
| asm_expr
| SEMI
| typelessDeclaration
;
typelessDeclaration
: #(NTypeMissing initDeclList SEMI)
;
asm_expr
: #( "asm" ( "volatile" )? LCURLY expr RCURLY ( SEMI )+ )
;
declaration
: #( NDeclaration
declSpecifiers
(
initDeclList
)?
( SEMI )+
)
;
declSpecifiers
: ( storageClassSpecifier
| typeQualifier
| typeSpecifier
)+
;
storageClassSpecifier
: "auto"
| "register"
| "typedef"
| functionStorageClassSpecifier
;
functionStorageClassSpecifier
: "extern"
| "static"
| "inline"
;
typeQualifier
: "const"
| "volatile"
;
typeSpecifier
: "void"
| "char"
| "short"
| "int"
| "long"
| "float"
| "double"
| "signed"
| "unsigned"
| "byte"
| "boolean"
| "Servo"
| "Wire"
| structSpecifier ( attributeDecl )*
| unionSpecifier ( attributeDecl )*
| enumSpecifier
| typedefName
| #("typeof" LPAREN
( (typeName )=> typeName
| expr
)
RPAREN
)
| "__complex"
;
typedefName
: #(NTypedefName ID)
;
structSpecifier
: #( "struct" structOrUnionBody )
;
unionSpecifier
: #( "union" structOrUnionBody )
;
structOrUnionBody
: ( (ID LCURLY) => ID LCURLY
( structDeclarationList )?
RCURLY
| LCURLY
( structDeclarationList )?
RCURLY
| ID
)
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
structDeclarationList
: ( structDeclaration )+
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
structDeclaration
: specifierQualifierList structDeclaratorList
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
specifierQualifierList
: (
typeSpecifier
| typeQualifier
)+
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
structDeclaratorList
: ( structDeclarator )+
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
structDeclarator
:
#( NStructDeclarator
( declarator )?
( COLON expr )?
( attributeDecl )*
)
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
enumSpecifier
: #( "enum"
( ID )?
( LCURLY enumList RCURLY )?
)
;
enumList
: ( enumerator )+
;
enumerator
: ID ( ASSIGN expr )?
;
attributeDecl:
#( "__attribute" (.)* )
| #( NAsmAttribute LPAREN expr RPAREN )
;
initDeclList
: ( initDecl )+
;
initDecl
{ String declName = ""; }
: #( NInitDecl
declarator
( attributeDecl )*
( ASSIGN initializer
| COLON expr
)?
)
;
pointerGroup
: #( NPointerGroup ( STAR ( typeQualifier )* )+ )
;
idList
: ID ( COMMA ID )*
;
initializer
: #( NInitializer (initializerElementLabel)? expr )
| lcurlyInitializer
;
initializerElementLabel
: #( NInitializerElementLabel
(
( LBRACKET expr RBRACKET (ASSIGN)? )
| ID COLON
| DOT ID ASSIGN
)
)
;
lcurlyInitializer
: #( NLcurlyInitializer
initializerList
RCURLY
)
;
initializerList
: ( initializer )*
;
declarator
: #( NDeclarator
( pointerGroup )?
( id:ID
| LPAREN declarator RPAREN
)
( #( NParameterTypeList
(
parameterTypeList
| (idList)?
)
RPAREN
)
| LBRACKET ( expr )? RBRACKET
)*
)
;
parameterTypeList
: ( parameterDeclaration ( COMMA | SEMI )? )+ ( VARARGS )?
;
parameterDeclaration
: #( NParameterDeclaration
declSpecifiers
(declarator | nonemptyAbstractDeclarator)?
)
;
functionDef
: #( NFunctionDef
( functionDeclSpecifiers)?
declarator
(declaration | VARARGS)*
compoundStatement
)
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
functionDeclSpecifiers
:
( functionStorageClassSpecifier
| typeQualifier
| typeSpecifier
)+
;
declarationList
:
( //ANTLR doesn't know that declarationList properly eats all the declarations
//so it warns about the ambiguity
options {
warnWhenFollowAmbig = false;
} :
localLabelDecl
| declaration
)+
;
localLabelDecl
: #("__label__" (ID)+ )
;
compoundStatement
: #( NCompoundStatement
( declarationList
| functionDef
)*
( statementList )?
RCURLY
)
;
statementList
: ( statement )+
;
statement
: statementBody
;
statementBody
: SEMI // Empty statements
| compoundStatement // Group of statements
| #(NStatementExpr expr) // Expressions
// Iteration statements:
| #( "while" expr statement )
| #( "do" statement expr )
| #( "for"
expr expr expr
statement
)
// Jump statements:
| #( "goto" expr )
| "continue"
| "break"
| #( "return" ( expr )? )
// Labeled statements:
| #( NLabel ID (statement)? )
| #( "case" expr (statement)? )
| #( "default" (statement)? )
// Selection statements:
| #( "if"
expr statement
( "else" statement )?
)
| #( "switch" expr statement )
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
expr
: assignExpr
| conditionalExpr
| logicalOrExpr
| logicalAndExpr
| inclusiveOrExpr
| exclusiveOrExpr
| bitAndExpr
| equalityExpr
| relationalExpr
| shiftExpr
| additiveExpr
| multExpr
| castExpr
| unaryExpr
| postfixExpr
| primaryExpr
| commaExpr
| emptyExpr
| compoundStatementExpr
| initializer
| rangeExpr
| gnuAsmExpr
;
commaExpr
: #(NCommaExpr expr expr)
;
emptyExpr
: NEmptyExpression
;
compoundStatementExpr
: #(LPAREN compoundStatement RPAREN)
;
rangeExpr
: #(NRangeExpr expr VARARGS expr)
;
gnuAsmExpr
: #(NGnuAsmExpr
("volatile")?
LPAREN stringConst
( options { warnWhenFollowAmbig = false; }:
COLON (strOptExprPair ( COMMA strOptExprPair)* )?
( options { warnWhenFollowAmbig = false; }:
COLON (strOptExprPair ( COMMA strOptExprPair)* )?
)?
)?
( COLON stringConst ( COMMA stringConst)* )?
RPAREN
)
;
strOptExprPair
: stringConst ( LPAREN expr RPAREN )?
;
assignExpr
: #( ASSIGN expr expr)
| #( DIV_ASSIGN expr expr)
| #( PLUS_ASSIGN expr expr)
| #( MINUS_ASSIGN expr expr)
| #( STAR_ASSIGN expr expr)
| #( MOD_ASSIGN expr expr)
| #( RSHIFT_ASSIGN expr expr)
| #( LSHIFT_ASSIGN expr expr)
| #( BAND_ASSIGN expr expr)
| #( BOR_ASSIGN expr expr)
| #( BXOR_ASSIGN expr expr)
;
conditionalExpr
: #( QUESTION expr (expr)? COLON expr )
;
logicalOrExpr
: #( LOR expr expr)
;
logicalAndExpr
: #( LAND expr expr )
;
inclusiveOrExpr
: #( BOR expr expr )
;
exclusiveOrExpr
: #( BXOR expr expr )
;
bitAndExpr
: #( BAND expr expr )
;
equalityExpr
: #( EQUAL expr expr)
| #( NOT_EQUAL expr expr)
;
relationalExpr
: #( LT expr expr)
| #( LTE expr expr)
| #( GT expr expr)
| #( GTE expr expr)
;
shiftExpr
: #( LSHIFT expr expr)
| #( RSHIFT expr expr)
;
additiveExpr
: #( PLUS expr expr)
| #( MINUS expr expr)
;
multExpr
: #( STAR expr expr)
| #( DIV expr expr)
| #( MOD expr expr)
;
castExpr
: #( NCast typeName RPAREN expr)
;
typeName
: specifierQualifierList (nonemptyAbstractDeclarator)?
;
nonemptyAbstractDeclarator
: #( NNonemptyAbstractDeclarator
( pointerGroup
( (LPAREN
( nonemptyAbstractDeclarator
| parameterTypeList
)?
RPAREN)
| (LBRACKET (expr)? RBRACKET)
)*
| ( (LPAREN
( nonemptyAbstractDeclarator
| parameterTypeList
)?
RPAREN)
| (LBRACKET (expr)? RBRACKET)
)+
)
)
;
unaryExpr
: #( INC expr )
| #( DEC expr )
| #( NUnaryExpr unaryOperator expr)
| #( "sizeof"
( ( LPAREN typeName )=> LPAREN typeName RPAREN
| expr
)
)
| #( "__alignof"
( ( LPAREN typeName )=> LPAREN typeName RPAREN
| expr
)
)
;
/*
exception
catch [RecognitionException ex]
{
reportError(ex);
System.out.println("PROBLEM TREE:\n"
+ _t.toStringList());
if (_t!=null) {_t = _t.getNextSibling();}
}
*/
unaryOperator
: BAND
| STAR
| PLUS
| MINUS
| BNOT
| LNOT
| LAND
| "__real"
| "__imag"
;
postfixExpr
: #( NPostfixExpr
primaryExpr
( PTR ID
| DOT ID
| #( NFunctionCallArgs (argExprList)? RPAREN )
| LBRACKET expr RBRACKET
| INC
| DEC
)+
)
;
primaryExpr
: ID
| Number
| charConst
| stringConst
// JTC:
// ID should catch the enumerator
// leaving it in gives ambiguous err
// | enumerator
| #( NExpressionGroup expr )
;
argExprList
: ( expr )+
;
protected
charConst
: CharLiteral
;
protected
stringConst
: #(NStringSeq (StringLiteral)+)
;
protected
intConst
: IntOctalConst
| LongOctalConst
| UnsignedOctalConst
| IntIntConst
| LongIntConst
| UnsignedIntConst
| IntHexConst
| LongHexConst
| UnsignedHexConst
;
protected
floatConst
: FloatDoubleConst
| DoubleDoubleConst
| LongDoubleConst
;

5551
app/preproc/WTreeParser.java Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,162 @@
// $ANTLR 2.7.2: "WTreeParser.g" -> "WTreeParser.java"$
package processing.app.preproc;
import java.io.*;
import antlr.CommonAST;
import antlr.DumpASTVisitor;
public interface WTreeParserTokenTypes {
int EOF = 1;
int NULL_TREE_LOOKAHEAD = 3;
int LITERAL_typedef = 4;
int LITERAL_asm = 5;
int LITERAL_volatile = 6;
int LCURLY = 7;
int RCURLY = 8;
int SEMI = 9;
int LITERAL_struct = 10;
int LITERAL_union = 11;
int LITERAL_enum = 12;
int LITERAL_auto = 13;
int LITERAL_register = 14;
int LITERAL_extern = 15;
int LITERAL_static = 16;
int LITERAL_const = 17;
int LITERAL_void = 18;
int LITERAL_char = 19;
int LITERAL_short = 20;
int LITERAL_int = 21;
int LITERAL_long = 22;
int LITERAL_float = 23;
int LITERAL_double = 24;
int LITERAL_signed = 25;
int LITERAL_unsigned = 26;
int ID = 27;
int COMMA = 28;
int COLON = 29;
int ASSIGN = 30;
int STAR = 31;
int LPAREN = 32;
int RPAREN = 33;
int LBRACKET = 34;
int RBRACKET = 35;
int VARARGS = 36;
int LITERAL_while = 37;
int LITERAL_do = 38;
int LITERAL_for = 39;
int LITERAL_goto = 40;
int LITERAL_continue = 41;
int LITERAL_break = 42;
int LITERAL_return = 43;
int LITERAL_case = 44;
int LITERAL_default = 45;
int LITERAL_if = 46;
int LITERAL_else = 47;
int LITERAL_switch = 48;
int DIV_ASSIGN = 49;
int PLUS_ASSIGN = 50;
int MINUS_ASSIGN = 51;
int STAR_ASSIGN = 52;
int MOD_ASSIGN = 53;
int RSHIFT_ASSIGN = 54;
int LSHIFT_ASSIGN = 55;
int BAND_ASSIGN = 56;
int BOR_ASSIGN = 57;
int BXOR_ASSIGN = 58;
int QUESTION = 59;
int LOR = 60;
int LAND = 61;
int BOR = 62;
int BXOR = 63;
int BAND = 64;
int EQUAL = 65;
int NOT_EQUAL = 66;
int LT = 67;
int LTE = 68;
int GT = 69;
int GTE = 70;
int LSHIFT = 71;
int RSHIFT = 72;
int PLUS = 73;
int MINUS = 74;
int DIV = 75;
int MOD = 76;
int INC = 77;
int DEC = 78;
int LITERAL_sizeof = 79;
int BNOT = 80;
int LNOT = 81;
int PTR = 82;
int DOT = 83;
int CharLiteral = 84;
int StringLiteral = 85;
int IntOctalConst = 86;
int LongOctalConst = 87;
int UnsignedOctalConst = 88;
int IntIntConst = 89;
int LongIntConst = 90;
int UnsignedIntConst = 91;
int IntHexConst = 92;
int LongHexConst = 93;
int UnsignedHexConst = 94;
int FloatDoubleConst = 95;
int DoubleDoubleConst = 96;
int LongDoubleConst = 97;
int NTypedefName = 98;
int NInitDecl = 99;
int NDeclarator = 100;
int NStructDeclarator = 101;
int NDeclaration = 102;
int NCast = 103;
int NPointerGroup = 104;
int NExpressionGroup = 105;
int NFunctionCallArgs = 106;
int NNonemptyAbstractDeclarator = 107;
int NInitializer = 108;
int NStatementExpr = 109;
int NEmptyExpression = 110;
int NParameterTypeList = 111;
int NFunctionDef = 112;
int NCompoundStatement = 113;
int NParameterDeclaration = 114;
int NCommaExpr = 115;
int NUnaryExpr = 116;
int NLabel = 117;
int NPostfixExpr = 118;
int NRangeExpr = 119;
int NStringSeq = 120;
int NInitializerElementLabel = 121;
int NLcurlyInitializer = 122;
int NAsmAttribute = 123;
int NGnuAsmExpr = 124;
int NTypeMissing = 125;
int Vocabulary = 126;
int Whitespace = 127;
int Comment = 128;
int CPPComment = 129;
int PREPROC_DIRECTIVE = 130;
int Space = 131;
int LineDirective = 132;
int BadStringLiteral = 133;
int Escape = 134;
int Digit = 135;
int LongSuffix = 136;
int UnsignedSuffix = 137;
int FloatSuffix = 138;
int Exponent = 139;
int Number = 140;
int LITERAL___label__ = 141;
int LITERAL_inline = 142;
int LITERAL_byte = 143;
int LITERAL_boolean = 144;
int LITERAL_Servo = 145;
int LITERAL_Wire = 146;
int LITERAL_typeof = 147;
int LITERAL___complex = 148;
int LITERAL___attribute = 149;
int LITERAL___alignof = 150;
int LITERAL___real = 151;
int LITERAL___imag = 152;
}

View File

@ -0,0 +1,151 @@
// $ANTLR 2.7.2: WTreeParser.g -> WTreeParserTokenTypes.txt$
WTreeParser // output token vocab name
LITERAL_typedef="typedef"=4
LITERAL_asm="asm"=5
LITERAL_volatile="volatile"=6
LCURLY=7
RCURLY=8
SEMI=9
LITERAL_struct="struct"=10
LITERAL_union="union"=11
LITERAL_enum="enum"=12
LITERAL_auto="auto"=13
LITERAL_register="register"=14
LITERAL_extern="extern"=15
LITERAL_static="static"=16
LITERAL_const="const"=17
LITERAL_void="void"=18
LITERAL_char="char"=19
LITERAL_short="short"=20
LITERAL_int="int"=21
LITERAL_long="long"=22
LITERAL_float="float"=23
LITERAL_double="double"=24
LITERAL_signed="signed"=25
LITERAL_unsigned="unsigned"=26
ID=27
COMMA=28
COLON=29
ASSIGN=30
STAR=31
LPAREN=32
RPAREN=33
LBRACKET=34
RBRACKET=35
VARARGS=36
LITERAL_while="while"=37
LITERAL_do="do"=38
LITERAL_for="for"=39
LITERAL_goto="goto"=40
LITERAL_continue="continue"=41
LITERAL_break="break"=42
LITERAL_return="return"=43
LITERAL_case="case"=44
LITERAL_default="default"=45
LITERAL_if="if"=46
LITERAL_else="else"=47
LITERAL_switch="switch"=48
DIV_ASSIGN=49
PLUS_ASSIGN=50
MINUS_ASSIGN=51
STAR_ASSIGN=52
MOD_ASSIGN=53
RSHIFT_ASSIGN=54
LSHIFT_ASSIGN=55
BAND_ASSIGN=56
BOR_ASSIGN=57
BXOR_ASSIGN=58
QUESTION=59
LOR=60
LAND=61
BOR=62
BXOR=63
BAND=64
EQUAL=65
NOT_EQUAL=66
LT=67
LTE=68
GT=69
GTE=70
LSHIFT=71
RSHIFT=72
PLUS=73
MINUS=74
DIV=75
MOD=76
INC=77
DEC=78
LITERAL_sizeof="sizeof"=79
BNOT=80
LNOT=81
PTR=82
DOT=83
CharLiteral=84
StringLiteral=85
IntOctalConst=86
LongOctalConst=87
UnsignedOctalConst=88
IntIntConst=89
LongIntConst=90
UnsignedIntConst=91
IntHexConst=92
LongHexConst=93
UnsignedHexConst=94
FloatDoubleConst=95
DoubleDoubleConst=96
LongDoubleConst=97
NTypedefName=98
NInitDecl=99
NDeclarator=100
NStructDeclarator=101
NDeclaration=102
NCast=103
NPointerGroup=104
NExpressionGroup=105
NFunctionCallArgs=106
NNonemptyAbstractDeclarator=107
NInitializer=108
NStatementExpr=109
NEmptyExpression=110
NParameterTypeList=111
NFunctionDef=112
NCompoundStatement=113
NParameterDeclaration=114
NCommaExpr=115
NUnaryExpr=116
NLabel=117
NPostfixExpr=118
NRangeExpr=119
NStringSeq=120
NInitializerElementLabel=121
NLcurlyInitializer=122
NAsmAttribute=123
NGnuAsmExpr=124
NTypeMissing=125
Vocabulary=126
Whitespace=127
Comment=128
CPPComment=129
PREPROC_DIRECTIVE("a line directive")=130
Space=131
LineDirective=132
BadStringLiteral=133
Escape=134
Digit=135
LongSuffix=136
UnsignedSuffix=137
FloatSuffix=138
Exponent=139
Number=140
LITERAL___label__="__label__"=141
LITERAL_inline="inline"=142
LITERAL_byte="byte"=143
LITERAL_boolean="boolean"=144
LITERAL_Servo="Servo"=145
LITERAL_Wire="Wire"=146
LITERAL_typeof="typeof"=147
LITERAL___complex="__complex"=148
LITERAL___attribute="__attribute"=149
LITERAL___alignof="__alignof"=150
LITERAL___real="__real"=151
LITERAL___imag="__imag"=152

View File

@ -1,11 +0,0 @@
#!/bin/sh
rm -f *Lexer.java
rm -f *Recognizer.java
rm -f *TokenTypes.java
rm -f *TokenTypes.txt
rm -f *TreeParser.java
rm -f *TreeParserTokenTypes.java
rm -f *TreeParserTokenTypes.txt
rm -f expanded*.g

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,326 +0,0 @@
package antlr;
/** Java 1.3 AST Recognizer Grammar
*
* Author: (see java.g preamble)
*
* This grammar is in the PUBLIC DOMAIN
*/
class JavaTreeParser extends TreeParser;
options {
importVocab = Java;
}
compilationUnit
: (packageDefinition)?
(importDefinition)*
(typeDefinition)*
;
packageDefinition
: #( PACKAGE_DEF identifier )
;
importDefinition
: #( IMPORT identifierStar )
;
typeDefinition
: #(CLASS_DEF modifiers IDENT extendsClause implementsClause objBlock )
| #(INTERFACE_DEF modifiers IDENT extendsClause interfaceBlock )
;
typeSpec
: #(TYPE typeSpecArray)
;
typeSpecArray
: #( ARRAY_DECLARATOR typeSpecArray )
| type
;
type: identifier
| builtInType
;
builtInType
: "void"
| "boolean"
| "byte"
| "char"
| "short"
| "int"
| "float"
| "long"
| "double"
;
modifiers
: #( MODIFIERS (modifier)* )
;
modifier
: "private"
| "public"
| "protected"
| "static"
| "transient"
| "final"
| "abstract"
| "native"
| "threadsafe"
| "synchronized"
| "const"
| "volatile"
| "strictfp"
;
extendsClause
: #(EXTENDS_CLAUSE (identifier)* )
;
implementsClause
: #(IMPLEMENTS_CLAUSE (identifier)* )
;
interfaceBlock
: #( OBJBLOCK
( methodDecl
| variableDef
)*
)
;
objBlock
: #( OBJBLOCK
( ctorDef
| methodDef
| variableDef
| typeDefinition
| #(STATIC_INIT slist)
| #(INSTANCE_INIT slist)
)*
)
;
ctorDef
: #(CTOR_DEF modifiers methodHead (slist)?)
;
methodDecl
: #(METHOD_DEF modifiers typeSpec methodHead)
;
methodDef
: #(METHOD_DEF modifiers typeSpec methodHead (slist)?)
;
variableDef
: #(VARIABLE_DEF modifiers typeSpec variableDeclarator varInitializer)
;
parameterDef
: #(PARAMETER_DEF modifiers typeSpec IDENT )
;
objectinitializer
: #(INSTANCE_INIT slist)
;
variableDeclarator
: IDENT
| LBRACK variableDeclarator
;
varInitializer
: #(ASSIGN initializer)
|
;
initializer
: expression
| arrayInitializer
;
arrayInitializer
: #(ARRAY_INIT (initializer)*)
;
methodHead
: IDENT #( PARAMETERS (parameterDef)* ) (throwsClause)?
;
throwsClause
: #( "throws" (identifier)* )
;
identifier
: IDENT
| #( DOT identifier IDENT )
;
identifierStar
: IDENT
| #( DOT identifier (STAR|IDENT) )
;
slist
: #( SLIST (stat)* )
;
stat: typeDefinition
| variableDef
| expression
| #(LABELED_STAT IDENT stat)
| #("if" expression stat (stat)? )
| #( "for"
#(FOR_INIT (variableDef | elist)?)
#(FOR_CONDITION (expression)?)
#(FOR_ITERATOR (elist)?)
stat
)
| #("while" expression stat)
| #("do" stat expression)
| #("break" (IDENT)? )
| #("continue" (IDENT)? )
| #("return" (expression)? )
| #("switch" expression (caseGroup)*)
| #("throw" expression)
| #("synchronized" expression stat)
| tryBlock
| slist // nested SLIST
// uncomment to make assert JDK 1.4 stuff work
| #("assert" expression (expression)?)
| EMPTY_STAT
;
caseGroup
: #(CASE_GROUP (#("case" expression) | "default")+ slist)
;
tryBlock
: #( "try" slist (handler)* (#("finally" slist))? )
;
handler
: #( "catch" parameterDef slist )
;
elist
: #( ELIST (expression)* )
;
expression
: #(EXPR expr)
;
expr: #(QUESTION expr expr expr) // trinary operator
| #(ASSIGN expr expr) // binary operators...
| #(PLUS_ASSIGN expr expr)
| #(MINUS_ASSIGN expr expr)
| #(STAR_ASSIGN expr expr)
| #(DIV_ASSIGN expr expr)
| #(MOD_ASSIGN expr expr)
| #(SR_ASSIGN expr expr)
| #(BSR_ASSIGN expr expr)
| #(SL_ASSIGN expr expr)
| #(BAND_ASSIGN expr expr)
| #(BXOR_ASSIGN expr expr)
| #(BOR_ASSIGN expr expr)
| #(LOR expr expr)
| #(LAND expr expr)
| #(BOR expr expr)
| #(BXOR expr expr)
| #(BAND expr expr)
| #(NOT_EQUAL expr expr)
| #(EQUAL expr expr)
| #(LT expr expr)
| #(GT expr expr)
| #(LE expr expr)
| #(GE expr expr)
| #(SL expr expr)
| #(SR expr expr)
| #(BSR expr expr)
| #(PLUS expr expr)
| #(MINUS expr expr)
| #(DIV expr expr)
| #(MOD expr expr)
| #(STAR expr expr)
| #(INC expr)
| #(DEC expr)
| #(POST_INC expr)
| #(POST_DEC expr)
| #(BNOT expr)
| #(LNOT expr)
| #("instanceof" expr expr)
| #(UNARY_MINUS expr)
| #(UNARY_PLUS expr)
| primaryExpression
;
primaryExpression
: IDENT
| #( DOT
( expr
( IDENT
| arrayIndex
| "this"
| "class"
| #( "new" IDENT elist )
| "super"
)
| #(ARRAY_DECLARATOR typeSpecArray)
| builtInType ("class")?
)
)
| arrayIndex
| #(METHOD_CALL primaryExpression elist)
| ctorCall
| #(TYPECAST typeSpec expr)
| newExpression
| constant
| "super"
| "true"
| "false"
| "this"
| "null"
| typeSpec // type name used with instanceof
;
ctorCall
: #( CTOR_CALL elist )
| #( SUPER_CTOR_CALL
( elist
| primaryExpression elist
)
)
;
arrayIndex
: #(INDEX_OP expr expression)
;
constant
: NUM_INT
| CHAR_LITERAL
| STRING_LITERAL
| NUM_FLOAT
| NUM_DOUBLE
| NUM_LONG
;
newExpression
: #( "new" type
( newArrayDeclarator (arrayInitializer)?
| elist (objBlock)?
)
)
;
newArrayDeclarator
: #( ARRAY_DECLARATOR (newArrayDeclarator)? (expression)? )
;

View File

@ -1,3 +0,0 @@
java -cp ../../build/macosx/work/lib/antlr.jar antlr.Tool java.g
# now build the pde stuff that extends the java classes
java -cp ../../build/macosx/work/lib/antlr.jar antlr.Tool -glib java.g pde.g

View File

@ -1,304 +0,0 @@
/* -*- mode: antlr; c-basic-offset: 4; indent-tabs-mode: nil -*- */
header {
package processing.app.preproc;
import processing.app.*;
}
class PdeRecognizer extends JavaRecognizer;
options {
importVocab = Java;
exportVocab = PdePartial;
codeGenMakeSwitchThreshold=10; // this is set high for debugging
codeGenBitsetTestThreshold=10; // this is set high for debugging
// developers may to want to set this to true for better
// debugging messages, however, doing so disables highlighting errors
// in the editor.
defaultErrorHandler = false; //true;
}
tokens {
CONSTRUCTOR_CAST; EMPTY_FIELD;
}
pdeProgram
// only java mode programs will have their own public classes or
// imports (and they must have at least one)
: ( "public" "class" | "import" ) => javaProgram
{ PdePreprocessor.programType = PdePreprocessor.JAVA; }
// the syntactic predicate here looks for any minimal (thus
// the non-greedy qualifier) number of fields, followed by
// the tokens that represent the definition of loop() or
// some other member function. java mode programs may have such
// definitions, but they won't reach this point, having already been
// selected in the previous alternative. static mode programs
// don't have member functions.
//
| ( ( options {greedy=false;}: possiblyEmptyField)* "void" IDENT LPAREN )
=> activeProgram
{ PdePreprocessor.programType = PdePreprocessor.ACTIVE; }
| staticProgram
{ PdePreprocessor.programType = PdePreprocessor.STATIC; }
;
// advanced mode is really just a normal java file
javaProgram
: compilationUnit
;
activeProgram
: (possiblyEmptyField)+
;
staticProgram
: (statement)*
;
// copy of the java.g rule with WEBCOLOR_LITERAL added
constant
: NUM_INT
| CHAR_LITERAL
| STRING_LITERAL
| NUM_FLOAT
| NUM_LONG
| NUM_DOUBLE
| webcolor_literal
;
// of the form #cc008f in PDE
webcolor_literal
: w:WEBCOLOR_LITERAL
{ Preferences.getBoolean("preproc.web_colors") &&
w.getText().length() == 6 }? // must be exactly 6 hex digits
;
// copy of the java.g builtInType rule
builtInConsCastType
: "void"
| "boolean"
| "byte"
| "char"
| "short"
| "int"
| "float"
| "long"
| "double"
;
// our types include the java types and "color". this is separated into two
// rules so that constructor casts can just use the original typelist, since
// we don't want to support the color type as a constructor cast.
//
builtInType
: builtInConsCastType
| "color" // aliased to an int in PDE
{ Preferences.getBoolean("preproc.color_datatype") }?
;
// constructor style casts.
constructorCast!
: t:consCastTypeSpec[true]
LPAREN!
e:expression
RPAREN!
// if this is a string literal, make sure the type we're trying to cast
// to is one of the supported ones
//
{ #e.getType() != STRING_LITERAL ||
( #t.getType() == LITERAL_byte ||
#t.getType() == LITERAL_double ||
#t.getType() == LITERAL_float ||
#t.getType() == LITERAL_int ||
#t.getType() == LITERAL_long ||
#t.getType() == LITERAL_short ) }?
// create the node
//
{#constructorCast = #(#[CONSTRUCTOR_CAST,"CONSTRUCTOR_CAST"], t, e);}
;
// A list of types that be used as the destination type in a constructor-style
// cast. Ideally, this would include all class types, not just "String".
// Unfortunately, it's not possible to tell whether Foo(5) is supposed to be
// a method call or a constructor cast without have a table of all valid
// types or methods, which requires semantic analysis (eg processing of import
// statements). So we accept the set of built-in types plus "String".
//
consCastTypeSpec[boolean addImagNode]
// : stringTypeSpec[addImagNode]
// | builtInConsCastTypeSpec[addImagNode]
: builtInConsCastTypeSpec[addImagNode]
// trying to remove String() cast [fry]
;
//stringTypeSpec[boolean addImagNode]
// : id:IDENT { #id.getText().equals("String") }?
// {
// if ( addImagNode ) {
// #stringTypeSpec = #(#[TYPE,"TYPE"],
// #stringTypeSpec);
// }
// }
// ;
builtInConsCastTypeSpec[boolean addImagNode]
: builtInConsCastType
{
if ( addImagNode ) {
#builtInConsCastTypeSpec = #(#[TYPE,"TYPE"],
#builtInConsCastTypeSpec);
}
}
;
// Since "color" tokens are lexed as LITERAL_color now, we need to have a rule
// that can generate a method call from an expression that starts with this
// token
//
colorMethodCall
: c:"color" {#c.setType(IDENT);} // this would default to LITERAL_color
lp:LPAREN^ {#lp.setType(METHOD_CALL);}
argList
RPAREN!
;
// copy of the java.g rule with added constructorCast and colorMethodCall
// alternatives
primaryExpression
: (consCastTypeSpec[false] LPAREN) => constructorCast
{ Preferences.getBoolean("preproc.enhanced_casting") }?
| identPrimary ( options {greedy=true;} : DOT^ "class" )?
| constant
| "true"
| "false"
| "null"
| newExpression
| "this"
| "super"
| LPAREN! assignmentExpression RPAREN!
| colorMethodCall
// look for int.class and int[].class
| builtInType
( lbt:LBRACK^ {#lbt.setType(ARRAY_DECLARATOR);} RBRACK! )*
DOT^ "class"
;
// the below variable rule hacks are needed so that it's possible for the
// emitter to correctly output variable declarations of the form "float a, b"
// from the AST. This means that our AST has a somewhat different form in
// these rules than the java one does, and this new form may have its own
// semantic issues. But it seems to fix the comma declaration issues.
//
variableDefinitions![AST mods, AST t]
: vd:variableDeclarator[getASTFactory().dupTree(mods),
getASTFactory().dupTree(t)]
{#variableDefinitions = #(#[VARIABLE_DEF,"VARIABLE_DEF"], mods,
t, vd);}
;
variableDeclarator[AST mods, AST t]
: ( id:IDENT (lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);} RBRACK!)*
v:varInitializer (COMMA!)? )+
;
// java.g builds syntax trees with an inconsistent structure. override one of
// the rules there to fix this.
//
explicitConstructorInvocation!
: t:"this" LPAREN a1:argList RPAREN SEMI
{#explicitConstructorInvocation = #(#[CTOR_CALL, "CTOR_CALL"],
#t, #a1);}
| s:"super" LPAREN a2:argList RPAREN SEMI
{#explicitConstructorInvocation = #(#[SUPER_CTOR_CALL,
"SUPER_CTOR_CALL"],
#s, #a2);}
;
// quick-n-dirty hack to the get the advanced class name. we should
// really be getting it from the AST and not forking this rule from
// the java.g copy at all. Since this is a recursive descent parser, we get
// the last class name in the file so that we don't end up with the classname
// of an inner class. If there is more than one "outer" class in a file,
// this heuristic will fail.
//
classDefinition![AST modifiers]
: "class" i:IDENT
// it _might_ have a superclass...
sc:superClassClause
// it might implement some interfaces...
ic:implementsClause
// now parse the body of the class
cb:classBlock
{#classDefinition = #(#[CLASS_DEF,"CLASS_DEF"],
modifiers,i,sc,ic,cb);
PdePreprocessor.advClassName = i.getText();}
;
possiblyEmptyField
: field
| s:SEMI {#s.setType(EMPTY_FIELD);}
;
class PdeLexer extends JavaLexer;
options {
importVocab=PdePartial;
exportVocab=Pde;
}
// We need to preserve whitespace and commentary instead of ignoring
// like the supergrammar does. Otherwise Jikes won't be able to give
// us error messages that point to the equivalent PDE code.
// WS, SL_COMMENT, ML_COMMENT are copies of the original productions,
// but with the SKIP assigment removed.
WS : ( ' '
| '\t'
| '\f'
// handle newlines
| ( options {generateAmbigWarnings=false;}
: "\r\n" // Evil DOS
| '\r' // Macintosh
| '\n' // Unix (the right way)
)
{ newline(); }
)+
;
// Single-line comments
SL_COMMENT
: "//"
(~('\n'|'\r'))* ('\n'|'\r'('\n')?)
{newline();}
;
// multiple-line comments
ML_COMMENT
: "/*"
( /* '\r' '\n' can be matched in one alternative or by matching
'\r' in one iteration and '\n' in another. I am trying to
handle any flavor of newline that comes in, but the language
that allows both "\r\n" and "\r" and "\n" to all be valid
newline is ambiguous. Consequently, the resulting grammar
must be ambiguous. I'm shutting this warning off.
*/
options {
generateAmbigWarnings=false;
}
:
{ LA(2)!='/' }? '*'
| '\r' '\n' {newline();}
| '\r' {newline();}
| '\n' {newline();}
| ~('*'|'\n'|'\r')
)*
"*/"
;
WEBCOLOR_LITERAL
: '#'! (HEX_DIGIT)+
;