Skip to content

Commit 0f9b290

Browse files
committed
Closes #290: Support detection of mixed modes in preproc.
Make a more friendly error message when the user is mixing modes, allowing for localization of error.
1 parent efd22f8 commit 0f9b290

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

build/shared/lib/languages/PDE.properties

+2-1
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ editor.status.bad.generic = Possibly missing type in generic near '%s'?
415415
editor.status.bad.identifier = Bad identifier? Did you forget a variable or start an identifier with digits near '%s'?
416416
editor.status.bad.parameter = Error on parameter or method declaration near '%s'?
417417
editor.status.bad.import = Import not allowed here.
418+
editor.status.bad.mixed_mode = You may be mixing active and static modes which is not allowed.
418419
editor.status.extraneous = Incomplete statement or extra code near '%s'?
419420
editor.status.mismatched = Missing operator, semicolon, or '}' near '%s'?
420421
editor.status.missing.name = Missing name or ; near '%s'?
@@ -644,4 +645,4 @@ movie_maker.progress.creating_output_file = Creating output file
644645
movie_maker.progress.initializing = Initializing...
645646
movie_maker.progress.processing = Processing %s.
646647

647-
movie_maker.progress.handling_frame = Converting frame %s of %s...
648+
movie_maker.progress.handling_frame = Converting frame %s of %s...

build/shared/lib/languages/PDE_es.properties

+1
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ editor.status.bad.identifier = Error en este identificador? Es posible que tu ol
387387
editor.status.bad.generic = Error en genérico cerca '%s'. Falta un tipo?
388388
editor.status.bad.parameter = Error en un parámetro o una declaración de método cerca '%s'?
389389
editor.status.bad.import = Una declaración de importación no es permitida en una definición de una clasa.
390+
editor.status.bad.mixed_mode = Es possible que estás usando el modo estático y activo.
390391
editor.status.extraneous = Una declaración incompleta o un imprevisto clave cerca '%s'?
391392
editor.status.mismatched = Falta un punto y coma, un operador, o un '}' cerca '%s'?
392393
editor.status.missing.name = Falta ; o nombre cerca '%s'?

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,23 @@ public void exitProcessingSketch(ProcessingParser.ProcessingSketchContext ctx) {
316316
footerResult = prepareFooter(rewriter, length);
317317
}
318318

319+
@Override
320+
public void enterWarnMixedModes(ProcessingParser.WarnMixedModesContext ctx) {
321+
pdeParseTreeErrorListenerMaybe.ifPresent((listener) -> {
322+
Token token = ctx.getStart();
323+
int line = token.getLine();
324+
int charOffset = token.getCharPositionInLine();
325+
326+
listener.onError(new PdePreprocessIssue(
327+
line,
328+
charOffset,
329+
PreprocessIssueMessageSimplifier.getLocalStr(
330+
"editor.status.bad.mixed_mode"
331+
)
332+
));
333+
});
334+
}
335+
319336
/**
320337
* Endpoint for ANTLR to call when finished parsing a method invocatino.
321338
*
@@ -770,7 +787,7 @@ protected boolean calledFromGlobalOrSetup(ParserRuleContext callContext) {
770787
* @return True if setup and false otherwise.
771788
*/
772789
protected boolean isMethodSetup(ParserRuleContext declaration) {
773-
if (declaration.getChildCount() < 2) {
790+
if (declaration == null || declaration.getChildCount() < 2) {
774791
return false;
775792
}
776793
return declaration.getChild(1).getText().equals("setup");

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,34 @@ processingSketch
2323
: javaProcessingSketch
2424
| staticProcessingSketch
2525
| activeProcessingSketch
26+
| warnMixedModes
2627
;
2728

2829
// java mode, is a compilation unit
2930
javaProcessingSketch
3031
: packageDeclaration? importDeclaration* typeDeclaration+ EOF
3132
;
3233

34+
// No method declarations, just statements
3335
staticProcessingSketch
3436
: (importDeclaration | blockStatement)* EOF
3537
;
3638

3739
// active mode, has function definitions
3840
activeProcessingSketch
39-
: (importDeclaration | classBodyDeclaration)* EOF
40-
;
41+
: (importDeclaration | classBodyDeclaration)* EOF
42+
;
4143

4244
variableDeclaratorId
4345
: warnTypeAsVariableName
4446
| IDENTIFIER ('[' ']')*
4547
;
4648

49+
warnMixedModes
50+
: (importDeclaration | classBodyDeclaration | blockStatement)* blockStatement classBodyDeclaration (importDeclaration | classBodyDeclaration | blockStatement)*
51+
| (importDeclaration | classBodyDeclaration | blockStatement)* classBodyDeclaration blockStatement (importDeclaration | classBodyDeclaration | blockStatement)*
52+
;
53+
4754
// bug #93
4855
// https://github.com/processing/processing/issues/93
4956
// prevent from types being used as variable names

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

+5
Original file line numberDiff line numberDiff line change
@@ -405,4 +405,9 @@ public void testSizeThis() {
405405
expectGood("sizethis");
406406
}
407407

408+
@Test
409+
public void testMixing() {
410+
expectRunnerException("mixing", 1);
411+
}
412+
408413
}

0 commit comments

Comments
 (0)