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".
This commit is contained in:
A Pottinger
2022-01-29 16:14:04 -08:00
parent 30cee886da
commit b66c5f2487
9 changed files with 89 additions and 5 deletions
+2 -2
View File
@@ -63,8 +63,8 @@ public class Compiler {
"-g",
"-Xemacs",
//"-noExit", // not necessary for ecj
"-source", "11",
"-target", "11",
"-source", "11", // TODO: 17 if using new language features
"-target", "11", // TODO: 17 if using new language features
"-encoding", "utf8",
"-classpath", classpathEmptyRemoved,
"-nowarn", // we're not currently interested in warnings (works in ecj)
@@ -957,6 +957,7 @@ public class PreprocService {
static {
Map<String, String> compilerOptions = new HashMap<>();
// TODO: VERSION_17 if using new language features
compilerOptions.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_11);
compilerOptions.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_11);
compilerOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_11);
@@ -9,7 +9,7 @@
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
@@ -116,6 +116,8 @@ BOOL_LITERAL: 'true'
CHAR_LITERAL: '\'' (~['\\\r\n] | EscapeSequence) '\'';
STRING_LITERAL: '"' (~["\\\r\n] | EscapeSequence)* '"';
MULTI_STRING_LIT: '"""' (~[\\] | EscapeSequence)* '"""';
NULL_LITERAL: 'null';
// Separators
LPAREN: '(';
@@ -294,11 +294,19 @@ literal
: integerLiteral
| floatLiteral
| CHAR_LITERAL
| STRING_LITERAL
| stringLiteral
| BOOL_LITERAL
| NULL_LITERAL
;
baseStringLiteral: STRING_LITERAL;
multilineStringLiteral: MULTI_STRING_LIT;
stringLiteral
: baseStringLiteral
| multilineStringLiteral
;
integerLiteral
: DECIMAL_LITERAL
| HEX_LITERAL
@@ -55,6 +55,7 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
private static final String NO_SMOOTH_METHOD_NAME = "noSmooth";
private static final String PIXEL_DENSITY_METHOD_NAME = "pixelDensity";
private static final String FULLSCREEN_METHOD_NAME = "fullScreen";
private static final boolean SIMULATE_MULTILINE_STRINGS = true;
final private String sketchName;
private boolean isTesting;
@@ -452,6 +453,29 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
}
}
/**
* Endpoint for ANTLR to call after parsing a String literal.
*
* <p>
* Endpoint for ANTLR to call when finished parsing a string literal, simulating multiline
* strings if configured to do so.
* </p>
*
* @param ctx ANTLR context for the literal.
*/
public void exitMultilineStringLiteral(ProcessingParser.MultilineStringLiteralContext ctx) {
String fullLiteral = ctx.getText();
if (SIMULATE_MULTILINE_STRINGS) {
delete(ctx.start, ctx.stop);
int endIndex = fullLiteral.length() - 3;
String literalContents = fullLiteral.substring(3, endIndex);
String newLiteralContents = literalContents
.replace("\n", "\\n")
.replace("\"", "\\\"");
insertAfter(ctx.stop, "\"" + newLiteralContents + "\"");
}
}
/**
* Endpoint for ANTLR to call after parsing a static processing sketch.
*
@@ -107,7 +107,7 @@ literal
: integerLiteral
| floatLiteral
| CHAR_LITERAL
| STRING_LITERAL
| stringLiteral
| BOOL_LITERAL
| NULL_LITERAL
| hexColorLiteral
@@ -415,4 +415,9 @@ public class ParserTests {
expectGood("sizeclass");
}
@Test
public void testMultlineString() {
expectGood("multilinestr");
}
}
+35
View File
@@ -0,0 +1,35 @@
import processing.core.*;
import processing.data.*;
import processing.event.*;
import processing.opengl.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
public class multilinestr extends PApplet {
public void setup() {
String testOldStyle = "line1\"\nline 2 \"\"\nline 3";
String testMultiline = "\nline4 \"\nline 5 \"\"\nline 6\nline 7";
println(testOldStyle);
println(testMultiline);
noLoop();
}
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "multilinestr" };
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
}
+9
View File
@@ -0,0 +1,9 @@
String testOldStyle = "line1\"\nline 2 \"\"\nline 3";
String testMultiline = """
line4 "
line 5 ""
line 6
line 7""";
println(testOldStyle);
println(testMultiline);