Skip to content

Commit

Permalink
fix(lexer): make sure front-matter parse success #112
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Sep 30, 2024
1 parent f3e2e09 commit a9c3770
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
15 changes: 13 additions & 2 deletions shirelang/src/main/grammar/ShireLexer.flex
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ AFTER_STREAMING =afterStreaming
private boolean isInsideShireTemplate = false;
private boolean isInsideFunctionBlock = false;
private boolean isInsideFrontMatter = false;
private boolean hasFrontMatter = false;
private boolean patternActionBraceStart = false;
private int patternActionBraceLevel = 0;
%}
Expand Down Expand Up @@ -217,7 +218,16 @@ AFTER_STREAMING =afterStreaming

%%
<YYINITIAL> {
"---" { isInsideFrontMatter = true; yybegin(FRONT_MATTER_BLOCK); return FRONTMATTER_START; }
"---" {
if (hasFrontMatter | isCodeStart) {
return CODE_CONTENT;
} else {
isInsideFrontMatter = true;
yybegin(FRONT_MATTER_BLOCK);
return FRONTMATTER_START;
}
}

{CODE_CONTENT} { return content(); }
{NEWLINE} { return NEWLINE; }
"[" { yypushback(yylength()); yybegin(COMMENT_BLOCK); }
Expand All @@ -242,7 +252,8 @@ AFTER_STREAMING =afterStreaming

{INDENT} { yybegin(FRONT_MATTER_VAL_OBJECT); return INDENT; }
{NEWLINE} { return NEWLINE; }
"---" { isInsideFrontMatter = false; yybegin(YYINITIAL); return FRONTMATTER_END; }

"---" { isInsideFrontMatter = false; hasFrontMatter = true; yybegin(YYINITIAL); return FRONTMATTER_END; }
[^] { yypushback(yylength()); yybegin(YYINITIAL); }
}

Expand Down
7 changes: 3 additions & 4 deletions shirelang/src/test/testData/parser/MarkdownCompatible.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ ShireFile
PsiElement(ShireTokenType.NEWLINE)('\n')
PsiElement(ShireTokenType.CODE_CONTENT)('onStreamingEnd: { parseCode | patch($codepath, $output) }')
PsiElement(ShireTokenType.NEWLINE)('\n')
PsiErrorElement:<used>, ShireTokenType.#, ShireTokenType.CODE_BLOCK_END, ShireTokenType.CODE_BLOCK_START, ShireTokenType.CODE_CONTENT, ShireTokenType.COMMENTS, ShireTokenType.NEWLINE or ShireTokenType.TEXT_SEGMENT expected, got '---'
PsiElement(ShireTokenType.FRONTMATTER_START)('---')
PsiElement(ShireTokenType.NEWLINE)('\n')
PsiElement(ShireTokenType.CODE_BLOCK_END)('```')
PsiElement(ShireTokenType.CODE_CONTENT)('---')
PsiElement(ShireTokenType.NEWLINE)('\n')
PsiElement(ShireTokenType.CODE_BLOCK_END)('```')

0 comments on commit a9c3770

Please sign in to comment.