Skip to content

Commit b66c5f2

Browse files
committed
Closes #371: Mutli-line string support.
Offers support for multi-line strings with a flag to simulate the feature prior to switching to Java 17 build flags. Note that comments are left in (TODOs) where the switch would be required to support it "natively".
1 parent 30cee88 commit b66c5f2

File tree

9 files changed

+89
-5
lines changed

9 files changed

+89
-5
lines changed

java/src/processing/mode/java/Compiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ static public boolean compile(JavaBuild build) throws SketchException {
6363
"-g",
6464
"-Xemacs",
6565
//"-noExit", // not necessary for ecj
66-
"-source", "11",
67-
"-target", "11",
66+
"-source", "11", // TODO: 17 if using new language features
67+
"-target", "11", // TODO: 17 if using new language features
6868
"-encoding", "utf8",
6969
"-classpath", classpathEmptyRemoved,
7070
"-nowarn", // we're not currently interested in warnings (works in ecj)

java/src/processing/mode/java/PreprocService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ private static boolean checkIfImportsChanged(List<ImportStatement> prevImports,
957957
static {
958958
Map<String, String> compilerOptions = new HashMap<>();
959959

960+
// TODO: VERSION_17 if using new language features
960961
compilerOptions.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_11);
961962
compilerOptions.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_11);
962963
compilerOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_11);

java/src/processing/mode/java/preproc/JavaLexer.g4

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
but WITHOUT ANY WARRANTY; without even the implied warranty of
1010
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1111
GNU General Public License for more details.
12-
12+
1313
You should have received a copy of the GNU General Public License
1414
along with this program; if not, write to the Free Software Foundation,
1515
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@@ -116,6 +116,8 @@ BOOL_LITERAL: 'true'
116116
CHAR_LITERAL: '\'' (~['\\\r\n] | EscapeSequence) '\'';
117117

118118
STRING_LITERAL: '"' (~["\\\r\n] | EscapeSequence)* '"';
119+
MULTI_STRING_LIT: '"""' (~[\\] | EscapeSequence)* '"""';
120+
119121
NULL_LITERAL: 'null';
120122
// Separators
121123
LPAREN: '(';

java/src/processing/mode/java/preproc/JavaParser.g4

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,19 @@ literal
294294
: integerLiteral
295295
| floatLiteral
296296
| CHAR_LITERAL
297-
| STRING_LITERAL
297+
| stringLiteral
298298
| BOOL_LITERAL
299299
| NULL_LITERAL
300300
;
301301

302+
baseStringLiteral: STRING_LITERAL;
303+
multilineStringLiteral: MULTI_STRING_LIT;
304+
305+
stringLiteral
306+
: baseStringLiteral
307+
| multilineStringLiteral
308+
;
309+
302310
integerLiteral
303311
: DECIMAL_LITERAL
304312
| HEX_LITERAL

java/src/processing/mode/java/preproc/PdeParseTreeListener.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
5555
private static final String NO_SMOOTH_METHOD_NAME = "noSmooth";
5656
private static final String PIXEL_DENSITY_METHOD_NAME = "pixelDensity";
5757
private static final String FULLSCREEN_METHOD_NAME = "fullScreen";
58+
private static final boolean SIMULATE_MULTILINE_STRINGS = true;
5859

5960
final private String sketchName;
6061
private boolean isTesting;
@@ -452,6 +453,29 @@ public void exitFloatLiteral(ProcessingParser.FloatLiteralContext ctx) {
452453
}
453454
}
454455

456+
/**
457+
* Endpoint for ANTLR to call after parsing a String literal.
458+
*
459+
* <p>
460+
* Endpoint for ANTLR to call when finished parsing a string literal, simulating multiline
461+
* strings if configured to do so.
462+
* </p>
463+
*
464+
* @param ctx ANTLR context for the literal.
465+
*/
466+
public void exitMultilineStringLiteral(ProcessingParser.MultilineStringLiteralContext ctx) {
467+
String fullLiteral = ctx.getText();
468+
if (SIMULATE_MULTILINE_STRINGS) {
469+
delete(ctx.start, ctx.stop);
470+
int endIndex = fullLiteral.length() - 3;
471+
String literalContents = fullLiteral.substring(3, endIndex);
472+
String newLiteralContents = literalContents
473+
.replace("\n", "\\n")
474+
.replace("\"", "\\\"");
475+
insertAfter(ctx.stop, "\"" + newLiteralContents + "\"");
476+
}
477+
}
478+
455479
/**
456480
* Endpoint for ANTLR to call after parsing a static processing sketch.
457481
*

java/src/processing/mode/java/preproc/Processing.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ literal
107107
: integerLiteral
108108
| floatLiteral
109109
| CHAR_LITERAL
110-
| STRING_LITERAL
110+
| stringLiteral
111111
| BOOL_LITERAL
112112
| NULL_LITERAL
113113
| hexColorLiteral

java/test/processing/mode/java/ParserTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,9 @@ public void testSizeClass() {
415415
expectGood("sizeclass");
416416
}
417417

418+
@Test
419+
public void testMultlineString() {
420+
expectGood("multilinestr");
421+
}
422+
418423
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import processing.core.*;
2+
import processing.data.*;
3+
import processing.event.*;
4+
import processing.opengl.*;
5+
6+
import java.util.HashMap;
7+
import java.util.ArrayList;
8+
import java.io.File;
9+
import java.io.BufferedReader;
10+
import java.io.PrintWriter;
11+
import java.io.InputStream;
12+
import java.io.OutputStream;
13+
import java.io.IOException;
14+
15+
public class multilinestr extends PApplet {
16+
17+
public void setup() {
18+
String testOldStyle = "line1\"\nline 2 \"\"\nline 3";
19+
String testMultiline = "\nline4 \"\nline 5 \"\"\nline 6\nline 7";
20+
21+
println(testOldStyle);
22+
println(testMultiline);
23+
24+
noLoop();
25+
}
26+
27+
static public void main(String[] passedArgs) {
28+
String[] appletArgs = new String[] { "multilinestr" };
29+
if (passedArgs != null) {
30+
PApplet.main(concat(appletArgs, passedArgs));
31+
} else {
32+
PApplet.main(appletArgs);
33+
}
34+
}
35+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
String testOldStyle = "line1\"\nline 2 \"\"\nline 3";
2+
String testMultiline = """
3+
line4 "
4+
line 5 ""
5+
line 6
6+
line 7""";
7+
8+
println(testOldStyle);
9+
println(testMultiline);

0 commit comments

Comments
 (0)