From e5a28cbe8e1a5036dd24080883b8c3ce71590224 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 25 Jun 2024 22:54:27 +0800 Subject: [PATCH] feat(shirelang): add default condition and new test for afterStreaming #24 Add the ability to specify a default condition in the lifecycle configuration of Shirelang. This change also includes a new test file and a test case for the afterStreaming condition. The commit adheres to the Conventional Commits specification, providing a clear and concise description of the main action. --- docs/lifecycle/afterStreaming.md | 1 + shirelang/src/main/grammar/ShireParser.bnf | 2 +- .../com/phodal/shirelang/ShireParsingTest.kt | 4 + .../test/testData/parser/AfterStream.shire | 13 ++ .../src/test/testData/parser/AfterStream.txt | 130 ++++++++++++++++++ 5 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 shirelang/src/test/testData/parser/AfterStream.shire create mode 100644 shirelang/src/test/testData/parser/AfterStream.txt diff --git a/docs/lifecycle/afterStreaming.md b/docs/lifecycle/afterStreaming.md index 7bc97a0b5..67747f0a0 100644 --- a/docs/lifecycle/afterStreaming.md +++ b/docs/lifecycle/afterStreaming.md @@ -13,6 +13,7 @@ afterStreaming: { condition { "variable-success" { $selection.length > 1 } "jsonpath-success" { jsonpath("/bookstore/book[price>35]") } + default { true } } case condition { "variable-sucesss" { done } diff --git a/shirelang/src/main/grammar/ShireParser.bnf b/shirelang/src/main/grammar/ShireParser.bnf index 7690938bd..c392df59c 100644 --- a/shirelang/src/main/grammar/ShireParser.bnf +++ b/shirelang/src/main/grammar/ShireParser.bnf @@ -82,7 +82,7 @@ frontMatterHeader ::= FRONTMATTER_START NEWLINE frontMatterEntries FRONTMATTER_E frontMatterEntries ::= frontMatterEntry* frontMatterEntry ::= - lifecycleId COLON conditionExpr NEWLINE? + lifecycleId COLON (functionStatement | conditionExpr) NEWLINE? | frontMatterKey COLON (frontMatterValue | patternAction | functionStatement) NEWLINE? private lifecycleId ::= "when" | "onStreaming" | "onStreamingEnd" | "afterStreaming" diff --git a/shirelang/src/test/kotlin/com/phodal/shirelang/ShireParsingTest.kt b/shirelang/src/test/kotlin/com/phodal/shirelang/ShireParsingTest.kt index 31ebec7a2..653f8ca73 100644 --- a/shirelang/src/test/kotlin/com/phodal/shirelang/ShireParsingTest.kt +++ b/shirelang/src/test/kotlin/com/phodal/shirelang/ShireParsingTest.kt @@ -83,4 +83,8 @@ class ShireParsingTest : ParsingTestCase("parser", "shire", ShireParserDefinitio fun testMultipleFMVariable() { doTest(true) } + + fun testAfterStream() { + doTest(true) + } } diff --git a/shirelang/src/test/testData/parser/AfterStream.shire b/shirelang/src/test/testData/parser/AfterStream.shire new file mode 100644 index 000000000..f3506200a --- /dev/null +++ b/shirelang/src/test/testData/parser/AfterStream.shire @@ -0,0 +1,13 @@ +--- +afterStreaming: { + condition { + "variable-success" { $selection.length > 1 } + "jsonpath-success" { jsonpath("/bookstore/book[price>35]") } + } + case condition { + "variable-sucesss" { done } + "jsonpath-success" { task() } + default { task() } + } + } +--- diff --git a/shirelang/src/test/testData/parser/AfterStream.txt b/shirelang/src/test/testData/parser/AfterStream.txt new file mode 100644 index 000000000..ccaa98273 --- /dev/null +++ b/shirelang/src/test/testData/parser/AfterStream.txt @@ -0,0 +1,130 @@ +ShireFile + ShireFrontMatterHeaderImpl(FRONT_MATTER_HEADER) + PsiElement(ShireTokenType.FRONTMATTER_START)('---') + PsiElement(ShireTokenType.NEWLINE)('\n') + ShireFrontMatterEntriesImpl(FRONT_MATTER_ENTRIES) + ShireFrontMatterEntryImpl(FRONT_MATTER_ENTRY) + PsiElement(ShireTokenType.afterStreaming)('afterStreaming') + PsiElement(ShireTokenType.COLON)(':') + PsiWhiteSpace(' ') + ShireFunctionStatementImpl(FUNCTION_STATEMENT) + PsiElement(ShireTokenType.{)('{') + PsiElement(ShireTokenType.NEWLINE)('\n') + PsiWhiteSpace(' ') + ShireFunctionBodyImpl(FUNCTION_BODY) + ShireActionBodyImpl(ACTION_BODY) + ShireActionExprImpl(ACTION_EXPR) + ShireCaseBodyImpl(CASE_BODY) + ShireConditionFlagImpl(CONDITION_FLAG) + PsiElement(ShireTokenType.condition)('condition') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.{)('{') + PsiElement(ShireTokenType.NEWLINE)('\n') + PsiWhiteSpace(' ') + ShireConditionStatementImpl(CONDITION_STATEMENT) + ShireCaseConditionImpl(CASE_CONDITION) + PsiElement(ShireTokenType.QUOTE_STRING)('"variable-success"') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.{)('{') + PsiWhiteSpace(' ') + ShireIneqComparisonExprImpl(INEQ_COMPARISON_EXPR) + ShireRefExprImpl(REF_EXPR) + ShireLiteralExprImpl(LITERAL_EXPR) + PsiElement(VARIABLE_START)('$') + PsiElement(ShireTokenType.IDENTIFIER)('selection') + PsiElement(ShireTokenType..)('.') + PsiElement(ShireTokenType.IDENTIFIER)('length') + PsiWhiteSpace(' ') + ShireIneqComparisonOpImpl(INEQ_COMPARISON_OP) + PsiElement(ShireTokenType.>)('>') + PsiWhiteSpace(' ') + ShireLiteralExprImpl(LITERAL_EXPR) + PsiElement(ShireTokenType.NUMBER)('1') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.})('}') + PsiElement(ShireTokenType.NEWLINE)('\n') + PsiWhiteSpace(' ') + ShireConditionStatementImpl(CONDITION_STATEMENT) + ShireCaseConditionImpl(CASE_CONDITION) + PsiElement(ShireTokenType.QUOTE_STRING)('"jsonpath-success"') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.{)('{') + PsiWhiteSpace(' ') + ShireCallExprImpl(CALL_EXPR) + ShireRefExprImpl(REF_EXPR) + PsiElement(ShireTokenType.IDENTIFIER)('jsonpath') + PsiElement(ShireTokenType.()('(') + ShireExpressionListImpl(EXPRESSION_LIST) + ShireLiteralExprImpl(LITERAL_EXPR) + PsiElement(ShireTokenType.QUOTE_STRING)('"/bookstore/book[price>35]"') + PsiElement(ShireTokenType.))(')') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.})('}') + PsiElement(ShireTokenType.NEWLINE)('\n') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.})('}') + PsiElement(ShireTokenType.NEWLINE)('\n') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.case)('case') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.condition)('condition') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.{)('{') + PsiElement(ShireTokenType.NEWLINE)('\n') + PsiWhiteSpace(' ') + ShireCasePatternActionImpl(CASE_PATTERN_ACTION) + ShireCaseConditionImpl(CASE_CONDITION) + PsiElement(ShireTokenType.QUOTE_STRING)('"variable-sucesss"') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.{)('{') + PsiWhiteSpace(' ') + ShireActionExprImpl(ACTION_EXPR) + ShireFuncCallImpl(FUNC_CALL) + ShireFuncNameImpl(FUNC_NAME) + PsiElement(ShireTokenType.IDENTIFIER)('done') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.})('}') + PsiElement(ShireTokenType.NEWLINE)('\n') + PsiWhiteSpace(' ') + ShireCasePatternActionImpl(CASE_PATTERN_ACTION) + ShireCaseConditionImpl(CASE_CONDITION) + PsiElement(ShireTokenType.QUOTE_STRING)('"jsonpath-success"') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.{)('{') + PsiWhiteSpace(' ') + ShireActionExprImpl(ACTION_EXPR) + ShireFuncCallImpl(FUNC_CALL) + ShireFuncNameImpl(FUNC_NAME) + PsiElement(ShireTokenType.IDENTIFIER)('task') + PsiElement(ShireTokenType.()('(') + ShirePipelineArgsImpl(PIPELINE_ARGS) + + PsiElement(ShireTokenType.))(')') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.})('}') + PsiElement(ShireTokenType.NEWLINE)('\n') + PsiWhiteSpace(' ') + ShireCasePatternActionImpl(CASE_PATTERN_ACTION) + ShireCaseConditionImpl(CASE_CONDITION) + PsiElement(ShireTokenType.default)('default') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.{)('{') + PsiWhiteSpace(' ') + ShireActionExprImpl(ACTION_EXPR) + ShireFuncCallImpl(FUNC_CALL) + ShireFuncNameImpl(FUNC_NAME) + PsiElement(ShireTokenType.IDENTIFIER)('task') + PsiElement(ShireTokenType.()('(') + ShirePipelineArgsImpl(PIPELINE_ARGS) + + PsiElement(ShireTokenType.))(')') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.})('}') + PsiElement(ShireTokenType.NEWLINE)('\n') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.})('}') + PsiElement(ShireTokenType.NEWLINE)('\n') + PsiWhiteSpace(' ') + PsiElement(ShireTokenType.})('}') + PsiElement(ShireTokenType.NEWLINE)('\n') + PsiElement(ShireTokenType.FRONTMATTER_END)('---') \ No newline at end of file