From 7875e483c0ca07f5a0f3fd6b03a82fb599727cac Mon Sep 17 00:00:00 2001 From: Matt VanNuys Date: Fri, 2 Feb 2018 12:37:24 -0500 Subject: [PATCH 1/3] Initial swipe at adding conditional compilation support to the BrightScript grammar --- grammar/BrightScript.g4 | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/grammar/BrightScript.g4 b/grammar/BrightScript.g4 index afe2615..9226dd5 100644 --- a/grammar/BrightScript.g4 +++ b/grammar/BrightScript.g4 @@ -23,6 +23,7 @@ block blockStatement : comment + | conditionalCompilationStatement | dimStatement | exitStatement | forStatement @@ -50,6 +51,15 @@ associativeElementInitializer : (identifier | reservedWord | stringLiteral) COLON assignableExpression ; +conditionalCompilationStatement + : CONDITIONAL_COMPILATION CONST untypedIdentifier EQUALS expression + | CONDITIONAL_COMPILATION ERROR .*? + | CONDITIONAL_COMPILATION IF expression + | CONDITIONAL_COMPILATION (ELSEIF | ELSE IF) expression + | CONDITIONAL_COMPILATION ELSE expression + | CONDITIONAL_COMPILATION (ENDIF | END IF) + ; + dimStatement : DIM identifier OPEN_BRACKET parameterList CLOSE_BRACKET ; @@ -313,6 +323,10 @@ BOX : B O X ; +CONST + : C O N S T + ; + CREATEOBJECT : C R E A T E O B J E C T ; @@ -361,6 +375,10 @@ ENDWHILE : E N D W H I L E ; +ERROR + : E R R O R + ; + EXIT : E X I T ; @@ -547,7 +565,7 @@ IDENTIFIER_TYPE_DECLARATION ; COMMENT - : (SINGLE_QUOTE | (REM (WS | NEWLINE))) ~[\r\n\u2028\u2029]* -> channel(HIDDEN) + : (SINGLE_QUOTE | (REM (WS | NEWLINE))) ~[\r\n\u2028\u2029]* -> channel(HIDDEN) ; NEWLINE @@ -558,6 +576,10 @@ WS : [ \t]+ -> skip ; +CONDITIONAL_COMPILATION + : '#' + ; + SINGLE_QUOTE : '\'' ; From cf7ba3595ad2adb1420bde15a31427f72c82ece6 Mon Sep 17 00:00:00 2001 From: Matt VanNuys Date: Fri, 2 Feb 2018 13:15:22 -0500 Subject: [PATCH 2/3] Conditional compilation seems to work; needs more testing --- grammar/BrightScript.g4 | 48 ++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/grammar/BrightScript.g4 b/grammar/BrightScript.g4 index 9226dd5..2627861 100644 --- a/grammar/BrightScript.g4 +++ b/grammar/BrightScript.g4 @@ -5,7 +5,7 @@ startRule ; component - : endOfLine* libraryStatement* endOfLine* componentBody + : endOfLine* (libraryStatement* | (conditionalCompilationStatement endOfLine)*) endOfLine* componentBody ; componentBody @@ -52,12 +52,12 @@ associativeElementInitializer ; conditionalCompilationStatement - : CONDITIONAL_COMPILATION CONST untypedIdentifier EQUALS expression - | CONDITIONAL_COMPILATION ERROR .*? - | CONDITIONAL_COMPILATION IF expression - | CONDITIONAL_COMPILATION (ELSEIF | ELSE IF) expression - | CONDITIONAL_COMPILATION ELSE expression - | CONDITIONAL_COMPILATION (ENDIF | END IF) + : CONDITIONAL_CONST untypedIdentifier EQUALS expression + | CONDITIONAL_ERROR .*? + | CONDITIONAL_IF expression + | CONDITIONAL_ELSEIF expression + | CONDITIONAL_ELSE expression + | CONDITIONAL_ENDIF ; dimStatement @@ -323,10 +323,6 @@ BOX : B O X ; -CONST - : C O N S T - ; - CREATEOBJECT : C R E A T E O B J E C T ; @@ -375,10 +371,6 @@ ENDWHILE : E N D W H I L E ; -ERROR - : E R R O R - ; - EXIT : E X I T ; @@ -576,8 +568,30 @@ WS : [ \t]+ -> skip ; -CONDITIONAL_COMPILATION - : '#' +CONDITIONAL_CONST + : '#' C O N S T + ; + +CONDITIONAL_ELSE + : '#' ELSE + ; + +CONDTIONAL_ELSEIF + : '#' ELSEIF + | '#' ELSE IF + ; + +CONDITIONAL_ENDIF + : '#' ENDIF + | '#' END IF + ; + +CONDITIONAL_ERROR + : '#' E R R O R + ; + +CONDITIONAL_IF + : '#' IF ; SINGLE_QUOTE From 20043dd8daa8ab2d5042094725721ef576c948f4 Mon Sep 17 00:00:00 2001 From: Matt VanNuys Date: Mon, 5 Feb 2018 14:42:59 -0500 Subject: [PATCH 3/3] Fix a typo, definition of else, and add some test scenarios to sample-project --- grammar/BrightScript.g4 | 24 ++++++++++++++++-------- sample-project/source/GoodSyntax.brs | 9 +++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/grammar/BrightScript.g4 b/grammar/BrightScript.g4 index 2627861..69bb999 100644 --- a/grammar/BrightScript.g4 +++ b/grammar/BrightScript.g4 @@ -5,11 +5,21 @@ startRule ; component - : endOfLine* (libraryStatement* | (conditionalCompilationStatement endOfLine)*) endOfLine* componentBody + : componentHead componentBody + ; + +componentHead + : endOfLine* componentHeadElement (endOfLine+ componentHeadElement)* endOfLine* + ; + +componentHeadElement + : libraryStatement + | conditionalCompilationStatement + | comment ; componentBody - : componentBodyElement (endOfLine+ componentBodyElement)* endOfLine* + : endOfLine* componentBodyElement (endOfLine+ componentBodyElement)* endOfLine* ; componentBodyElement @@ -56,7 +66,7 @@ conditionalCompilationStatement | CONDITIONAL_ERROR .*? | CONDITIONAL_IF expression | CONDITIONAL_ELSEIF expression - | CONDITIONAL_ELSE expression + | CONDITIONAL_ELSE | CONDITIONAL_ENDIF ; @@ -576,14 +586,12 @@ CONDITIONAL_ELSE : '#' ELSE ; -CONDTIONAL_ELSEIF - : '#' ELSEIF - | '#' ELSE IF +CONDITIONAL_ELSEIF + : '#' (ELSE WS IF | ELSEIF) ; CONDITIONAL_ENDIF - : '#' ENDIF - | '#' END IF + : '#' (END WS IF | ENDIF) ; CONDITIONAL_ERROR diff --git a/sample-project/source/GoodSyntax.brs b/sample-project/source/GoodSyntax.brs index d73d3d5..2be28c9 100644 --- a/sample-project/source/GoodSyntax.brs +++ b/sample-project/source/GoodSyntax.brs @@ -1,11 +1,20 @@ ' This is a sample file containing syntax that compiles properly +#const debug = true + REM Although the file compiles, REM REM Not all linting rules pass sub DefaultMain() + #if debug = true + print "debug mode enabled" + #else + print "debug mode disabled" + #end if + print "in showChannelSGScreen" + 'Indicate this is a Roku SceneGraph application' screen = CreateObject("roSGScreen") m.port = CreateObject("roMessagePort")