Skip to content

Commit 86bbf7c

Browse files
committed
did a wee bit of cleanup for dev release
1 parent 13977c2 commit 86bbf7c

17 files changed

+182
-170
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ To install: unzip PythonMode.zip into "{your sketch folder}/modes/PythonMode" an
1313

1414
Check build.xml for building instructions.
1515

16+
Changes:
17+
- the Processing set() function is now called setPixel(), because python
18+
1619
Done:
1720
- Basic functionality- running python code
1821
- Basic indentation & highlighting

release/PythonMode.zip

-1.68 KB
Binary file not shown.

resources/keywords.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,14 @@ UP LITERAL2 keyCode
176176
WAIT LITERAL2 cursor_
177177
WHITESPACE LITERAL2
178178

179+
# Python literals
180+
None LITERAL2
181+
True LITERAL2
182+
False LITERAL2
183+
Ellipsis LITERAL2
184+
NotImplemented LITERAL2
179185

180-
# Java [python] keywords (void, import, , etc.)
186+
# Python keywords
181187

182188
and KEYWORD1
183189
as KEYWORD1
@@ -427,7 +433,7 @@ keyArray FUNCTION2 FloatDict_keyArray_
427433
keys FUNCTION2 FloatDict_keys_
428434
mult FUNCTION2 FloatDict_mult_
429435
remove FUNCTION2 FloatDict_remove_
430-
set FUNCTION2 FloatDict_set_
436+
setPixel FUNCTION2 FloatDict_set_
431437
size FUNCTION2 FloatDict_size_
432438
sortKeys FUNCTION2 FloatDict_sortKeys_
433439
sortKeysReverse FUNCTION2 FloatDict_sortKeysReverse_

resources/prepend-commented.py

Lines changed: 0 additions & 94 deletions
This file was deleted.

src/info/sansgills/mode/python/Communicator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
/**
1414
*
15-
* The glue between the PDE process and the sketch process Handles sending
16-
* messages to and recieving messages from
15+
* A class that sends data to and gets data from the running sketch process.
16+
* Sends start signals, stop signals, and sketches.
1717
*
1818
*/
1919

src/info/sansgills/mode/python/PythonBuild.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
* interpreter, all this does is handle preprocessing and write to an output
2525
* file.
2626
*
27-
* See BUILD.md for notes.
28-
*
2927
*/
3028

3129
public class PythonBuild {
@@ -41,7 +39,7 @@ public class PythonBuild {
4139

4240
boolean usesOpenGL;
4341

44-
//build tracking
42+
//build tracking, not that this actually does anything
4543
private int buildnumber;
4644
private static int buildstotal = 0;
4745

@@ -57,9 +55,9 @@ public PythonBuild(Sketch sketch, PythonMode mode) {
5755
* Preprocess the sketch- turn the .pde files into valid python.
5856
*/
5957
public void build() throws Exception {
58+
//to hold all the code
6059
StringBuilder program = new StringBuilder();
61-
62-
SketchCode[] parts = sketch.getCode(); //fix'd
60+
SketchCode[] parts = sketch.getCode();
6361

6462
//concatenate code strings
6563
//TODO do this in some sort of vaguely intelligent way
@@ -68,11 +66,13 @@ public void build() throws Exception {
6866
program.append(parts[i].getProgram());
6967
}
7068

69+
//send to preprocessor
7170
resultProgram = preprocess(program);
7271

73-
//write output file
72+
//create output folder
7473
binFolder = sketch.makeTempFolder();
7574

75+
//create & write to output file
7676
outFile = new File(binFolder.getAbsolutePath() + File.separator + sketch.getName().toLowerCase() + ".py");
7777
outFile.createNewFile();
7878

@@ -81,17 +81,18 @@ public void build() throws Exception {
8181
writer.close();
8282
}
8383

84-
private static Pattern whitespace = Pattern.compile("\\s*");
85-
8684
/*
87-
* Turn .pde into valid python Now with antlr!
85+
* Turn .pde files into valid python and extract used libraries See
86+
* PyPdeConverter / generated antlr code for the actual preprocessing logic.
8887
*/
8988
private String preprocess(StringBuilder program) throws Exception {
90-
if (whitespace.matcher(program).matches()) {
91-
return "def setup():\n\tpass\n\n"; //empty sketch; TODO fix hack
89+
if (Pattern.matches("\\s*", program)) {
90+
return "def setup():\n\tpass\n\n";
9291
}
9392
try {
9493
program.append("\n"); //to be safe
94+
95+
//now for the antlr
9596
PyPdeLexer lexer = new PyPdeLexer(new ANTLRInputStream(program.toString()));
9697
CommonTokenStream tokens = new CommonTokenStream(lexer);
9798
PyPdeParser parser = new PyPdeParser(tokens);
@@ -118,7 +119,8 @@ private String preprocess(StringBuilder program) throws Exception {
118119
String entry = (dot == -1) ? lib : lib.substring(0, dot);
119120
Library library = mode.getLibrary(entry);
120121
if (library == null) {
121-
throw new Exception("Unknown library: " + lib);
122+
System.err.println("I don't know about the library "+lib+" so it might not work. Sorry.");
123+
continue;
122124
}
123125
javaLibs += library.getJarPath() + System.getProperty("path.separator");
124126
}
@@ -139,6 +141,9 @@ private String preprocess(StringBuilder program) throws Exception {
139141
}
140142
}
141143

144+
/*
145+
* Access to extracted library information
146+
*/
142147
public String getJavaLibraries() {
143148
return javaLibs;
144149
}
@@ -156,7 +161,7 @@ public boolean hasPythonLibraries() {
156161
}
157162

158163
/*
159-
* The output code string
164+
* The output code string, properly formatted and whatnot.
160165
*/
161166
public String getResultString() {
162167
return resultProgram;
@@ -170,25 +175,29 @@ public String getResultFile() {
170175
}
171176

172177
/*
173-
*
178+
* This may eventually tie in to some global thing... or not
174179
*/
175180
public int getBuildNumber() {
176181
return buildnumber;
177182
}
178183

179184
/*
180-
* Classes used to run the build.
185+
* Java classes used to run the build.
181186
*/
182187
public String getClassPath() {
183188
return classPath; //computed during preprocessing
184189
}
185190

191+
/*
192+
* ...
193+
*/
186194
public String getName() {
187195
return sketch.getName();
188196
}
189197

190198
/*
191-
* TODO
199+
* I'm not sure what the java library path actually is, so, leaving this
200+
* blank.
192201
*/
193202
public String getJavaLibraryPath() {
194203
return "";

src/info/sansgills/mode/python/PythonEditor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
/**
2121
*
22-
* Main editor. Superclass handles most of the nitty-gritty; we just have to
23-
* manage python-specific things, like running and stopping.
22+
* The main editor class. Unlike in Java Mode the editor also handles running and exporting code,
23+
* because I'm lazy.
24+
*
25+
* Does basically what you'd expect it to.
2426
*
2527
*/
2628
@SuppressWarnings("serial")

src/info/sansgills/mode/python/PythonFormat.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/**
66
*
77
* Class used when the user selects the 'format' option from the menu.
8+
* Currently does absolutely nothing.
89
*
910
*/
1011

src/info/sansgills/mode/python/PythonKeyListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public PythonKeyListener(Editor editor, JEditTextArea textarea) {
3737

3838
/*
3939
* Handles special stuff for Java brace indenting & outdenting, etc.
40-
* Overriding it 'cause we do things different here in python-land TODO use
41-
* actual parser; handle spaces
42-
* @return true if we've handled things correctly
40+
* Overriding it 'cause we do things different here in python-land
41+
* TODO use actual parser; handle spaces
42+
*
4343
*/
4444
@Override
4545
public boolean keyPressed(KeyEvent event) {

src/info/sansgills/mode/python/PythonMode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
/**
1212
*
13-
* Python Mode. Yeah.
13+
* The main mode class. I don't actually implement much functionality here; just
14+
* what the Mode class is contractually obligated to provide.
1415
*
1516
*/
1617
public class PythonMode extends Mode {

src/info/sansgills/mode/python/PythonRunner.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* Class to handle the running of sketches. Starts a new ProcessingJythonWrapper
1414
* process and interfaces between it and the PDE.
1515
*
16+
* Synchronization is a horrible tangled mess and I don't remember how it works.
17+
*
1618
*/
1719

1820
public class PythonRunner {

src/info/sansgills/mode/python/PythonToolbar.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*
1818
*/
1919

20-
//something up the chain is serializable, damned if I know why
2120
@SuppressWarnings("serial")
2221
public class PythonToolbar extends EditorToolbar {
2322

src/info/sansgills/mode/python/preproc/PyPde.g4

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/*
2-
* To change this template, choose Tools | Templates
3-
* and open the template in the editor.
2+
* A fairly complete python2.7 parser, written with ANTLR4.
3+
* Uses some ANTLR black magic to handle lexing indents & dedents, which parsers don't like.
44
*/
55

66
grammar PyPde;
77

8+
//these are injected into the headers of the generated parser & lexer.
89
@parser::header {
910
package info.sansgills.mode.python.preproc;
1011
}
11-
12+
1213
@lexer::header {
1314
package info.sansgills.mode.python.preproc;
1415
@@ -20,7 +21,7 @@ grammar PyPde;
2021
import org.antlr.v4.runtime.misc.Interval;
2122
}
2223

23-
//some black magic
24+
//this is injected into the generated lexer. It overrides ANTLR's default 'go to next token' method in order to handle *dents.
2425
@lexer::members {
2526
int bracket_nesting_level = 0;
2627
int parenth_nesting_level = 0;
@@ -109,12 +110,15 @@ grammar PyPde;
109110
}
110111
}
111112

113+
//and on the the actual parser.
114+
//essentially copied from http://docs.python.org/2/reference/, and probably quite inefficient.
115+
112116

113117
script: (NEWLINE | statement)*;
114118

115119

116120
//lexer!
117-
//strings first, so they'll gobble things up first
121+
//strings first, so they'll gobble things up
118122

119123
STRING: STRINGPREFIX? (SHORTSTRING | LONGSTRING);
120124
fragment SHORTSTRING: '\'' (~('\\'|'\n'|'\'')|'\\'.)* '\''
@@ -203,7 +207,7 @@ WS: [ \t]+ -> channel(HIDDEN);
203207

204208

205209
// onto the parser
206-
// atomic values: identifiers and literals (including enclodure literals
210+
// atomic values: identifiers and literals (including enclosure literals)
207211
atom : identifier | literal | enclosure;
208212

209213
literal: STRING | INTEGER | IMAGINARY | FLOAT;

0 commit comments

Comments
 (0)