Skip to content

Commit

Permalink
feat(grammar): Add support for if expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
lkk214 committed Nov 8, 2024
1 parent 5f1548d commit d536289
Show file tree
Hide file tree
Showing 7 changed files with 793 additions and 171 deletions.
17 changes: 12 additions & 5 deletions shirelang/src/main/grammar/ShireParser.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,18 @@ code ::= CODE_BLOCK_START (languageId | variableStart expr)? NEWLINE? code_conte
code_contents ::= (NEWLINE | CODE_CONTENT)*

velocityExpr ::=
'#' 'if' '('variableStart? expr ')'
| '#' 'else'
| '#' 'elseif' '(' variableStart? expr ')'
| '#' 'end'
| '#' 'endif'
ifExpr NEWLINE*

velocityBlock ::= (used | code | velocityExpr | markdownHeader | TEXT_SEGMENT | NEWLINE | CONTENT_COMMENTS)*

ifExpr ::= ifClause elseifClause* elseClause? '#' 'end'

ifClause ::= '#' 'if' '(' expr ')' velocityBlock

elseifClause ::= '#' 'elseif' '(' expr ')' velocityBlock

elseClause ::= '#' 'else' velocityBlock

// for example, the commit id will be #$storyId

markdownHeader ::= SHARP SHARP* TEXT_SEGMENT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class ShireSyntaxAnalyzer(

WHITE_SPACE, DUMMY_BLOCK -> output.append(psiElement.text)
ShireTypes.VELOCITY_EXPR -> {
output.append(psiElement.text)
logger.info("Velocity expression found: ${psiElement.text}")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,8 @@ class ParsingNormalTest : ParsingTestCase("parser", "shire", ShireParserDefiniti
fun testCustomFunctions() {
doTest(true)
}

fun testIfExpression() {
doTest(true)
}
}
59 changes: 59 additions & 0 deletions shirelang/src/test/testData/parser/IfExpression.shire
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
This is an expression that includes an if.
#if(1 > 0)
${language} if
#end


This is an expression that includes if and elseif.
#if($language.length() > 1)
${language} if
#elseif($language.length() > 5)
${language} elseif
#end


This is an expression that includes if and else.
#if($language.length() > 1 )
${language} if
#else
${language} else
#end


This is an expression that includes if,elseif and else.
#if($language.length() > 10 )
${language} if
#elseif($language.length() > 2 )
${language} elseif
#else
${language} else
#end


This is an expression that includes multiple if and elseif.
#if($language.length() > 1 )
${language} if
#if($language.length() > 2 )
${language} if_if
#elseif($language.length() > 20 )
${language} if_elseif
#end
#elseif($language.length() > 10 )
${language} elseif
#end


This is an expression that includes multiple if and else.
#if($language.length() > 10 )
${language} if
#else
${language} else
#if($language.length() > 2 )
${language} else_if
#else
${language} else_else
#end
#end


Please ignore the content and don't reply to me.
Loading

0 comments on commit d536289

Please sign in to comment.