diff --git a/grammar/BrightScript.g4 b/grammar/BrightScript.g4 index afe2615..69bb999 100644 --- a/grammar/BrightScript.g4 +++ b/grammar/BrightScript.g4 @@ -5,11 +5,21 @@ startRule ; component - : endOfLine* libraryStatement* 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 @@ -23,6 +33,7 @@ block blockStatement : comment + | conditionalCompilationStatement | dimStatement | exitStatement | forStatement @@ -50,6 +61,15 @@ associativeElementInitializer : (identifier | reservedWord | stringLiteral) COLON assignableExpression ; +conditionalCompilationStatement + : CONDITIONAL_CONST untypedIdentifier EQUALS expression + | CONDITIONAL_ERROR .*? + | CONDITIONAL_IF expression + | CONDITIONAL_ELSEIF expression + | CONDITIONAL_ELSE + | CONDITIONAL_ENDIF + ; + dimStatement : DIM identifier OPEN_BRACKET parameterList CLOSE_BRACKET ; @@ -547,7 +567,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 +578,30 @@ WS : [ \t]+ -> skip ; +CONDITIONAL_CONST + : '#' C O N S T + ; + +CONDITIONAL_ELSE + : '#' ELSE + ; + +CONDITIONAL_ELSEIF + : '#' (ELSE WS IF | ELSEIF) + ; + +CONDITIONAL_ENDIF + : '#' (END WS IF | ENDIF) + ; + +CONDITIONAL_ERROR + : '#' E R R O R + ; + +CONDITIONAL_IF + : '#' IF + ; + SINGLE_QUOTE : '\'' ; 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")