Skip to content

Commit

Permalink
Improve whitespace errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tomblachut committed Feb 23, 2020
1 parent b601460 commit 461e878
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 78 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package dev.blachut.svelte.lang.parsing.html;
import com.intellij.lexer.FlexLexer;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.xml.*;
import com.intellij.psi.TokenType;
import dev.blachut.svelte.lang.psi.SvelteTokenTypes;

%%
Expand Down Expand Up @@ -147,7 +148,7 @@ CONDITIONAL_COMMENT_CONDITION=({ALPHA})({ALPHA}|{WHITE_SPACE_CHARS}|{DIGIT}|"."|
<YYINITIAL> "{" { yybeginNestable(SVELTE_INTERPOLATION_START); return SvelteTokenTypes.START_MUSTACHE_TEMP; }

<SVELTE_INTERPOLATION_START> {
{WHITE_SPACE} { return SvelteTokenTypes.TEMP_PREFIX; }
{WHITE_SPACE} { return TokenType.WHITE_SPACE; }
"#" { yybegin(SVELTE_INTERPOLATION_KEYWORD); return SvelteTokenTypes.HASH; }
":" { yybegin(SVELTE_INTERPOLATION_KEYWORD); return SvelteTokenTypes.COLON; }
"/" { yybegin(SVELTE_INTERPOLATION_KEYWORD); return SvelteTokenTypes.SLASH; }
Expand All @@ -156,8 +157,7 @@ CONDITIONAL_COMMENT_CONDITION=({ALPHA})({ALPHA}|{WHITE_SPACE_CHARS}|{DIGIT}|"."|
}

<SVELTE_INTERPOLATION_KEYWORD> {
// TODO Disallow whitespace
// {WHITE_SPACE} { return BAD_CHARACTER; }
{WHITE_SPACE} { return TokenType.WHITE_SPACE; }
"if" { yybegin(SVELTE_INTERPOLATION); return SvelteTokenTypes.LAZY_IF; }
"else" { yybegin(SVELTE_INTERPOLATION); return SvelteTokenTypes.LAZY_ELSE; }
"each" { yybegin(SVELTE_INTERPOLATION); return SvelteTokenTypes.LAZY_EACH; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ package dev.blachut.svelte.lang.parsing.html

import com.intellij.lang.PsiBuilder
import com.intellij.lang.javascript.JSTokenTypes
import com.intellij.psi.TokenType
import com.intellij.psi.tree.IElementType
import com.intellij.psi.tree.ILazyParseableElementType
import dev.blachut.svelte.lang.isTokenAfterWhiteSpace
import dev.blachut.svelte.lang.psi.SvelteBlockLazyElementTypes
import dev.blachut.svelte.lang.psi.SvelteJSLazyElementTypes
import dev.blachut.svelte.lang.psi.SvelteTokenTypes

object SvelteManualParsing {
fun parseNotAllowedWhitespace(builder: PsiBuilder, precedingSymbol: String) {
if (builder.isTokenAfterWhiteSpace()) {
builder.error("whitespace is not allowed after $precedingSymbol")
}
}

fun parseLazyBlock(builder: PsiBuilder): Pair<IElementType, PsiBuilder.Marker> {
val marker = builder.mark()
builder.remapCurrentToken(JSTokenTypes.LBRACE)
builder.advanceLexer()

if (builder.tokenType == SvelteTokenTypes.TEMP_PREFIX) {
builder.remapCurrentToken(TokenType.WHITE_SPACE)
builder.advanceLexer()
}

if (builder.tokenType == SvelteTokenTypes.HASH) {
builder.advanceLexer()
val token = when (builder.tokenType) {
Expand All @@ -41,6 +42,8 @@ object SvelteManualParsing {
} else if (builder.tokenType == SvelteTokenTypes.SLASH) {
builder.remapCurrentToken(JSTokenTypes.DIV)
builder.advanceLexer()
parseNotAllowedWhitespace(builder, "/")

val token = when (builder.tokenType) {
SvelteTokenTypes.LAZY_IF -> {
builder.remapCurrentToken(JSTokenTypes.IF_KEYWORD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package dev.blachut.svelte.lang.psi
import com.intellij.lang.PsiBuilder
import com.intellij.lang.javascript.JSTokenTypes
import com.intellij.lang.javascript.parsing.JavaScriptParser
import dev.blachut.svelte.lang.parsing.html.SvelteManualParsing

object SvelteBlockLazyElementTypes {
val IF_START = object : SvelteBlockLazyElementType("IF_START") {
override val noTokensErrorMessage = "expression expected"

override fun parseTokens(builder: PsiBuilder, parser: JavaScriptParser<*, *, *, *>) {
builder.advanceLexer() // JSTokenTypes.SHARP
SvelteManualParsing.parseNotAllowedWhitespace(builder, "#")
builder.advanceLexer() // JSTokenTypes.IF_KEYWORD

parser.expressionParser.parseExpression()
Expand All @@ -21,6 +23,7 @@ object SvelteBlockLazyElementTypes {

override fun parseTokens(builder: PsiBuilder, parser: JavaScriptParser<*, *, *, *>) {
builder.advanceLexer() // JSTokenTypes.COLON
SvelteManualParsing.parseNotAllowedWhitespace(builder, ":")
builder.advanceLexer() // JSTokenTypes.ELSE_KEYWORD

if (builder.tokenType === JSTokenTypes.IF_KEYWORD) {
Expand All @@ -39,6 +42,7 @@ object SvelteBlockLazyElementTypes {

override fun parseTokens(builder: PsiBuilder, parser: JavaScriptParser<*, *, *, *>) {
builder.advanceLexer() // JSTokenTypes.SHARP
SvelteManualParsing.parseNotAllowedWhitespace(builder, "#")
builder.advanceLexer() // JSTokenTypes.IDENTIFIER -- fake EACH

parser.expressionParser.parseExpression()
Expand Down Expand Up @@ -76,6 +80,7 @@ object SvelteBlockLazyElementTypes {

override fun parseTokens(builder: PsiBuilder, parser: JavaScriptParser<*, *, *, *>) {
builder.advanceLexer() // JSTokenTypes.SHARP
SvelteManualParsing.parseNotAllowedWhitespace(builder, "#")
builder.advanceLexer() // JSTokenTypes.AWAIT_KEYWORD

parser.expressionParser.parseExpression()
Expand All @@ -93,6 +98,7 @@ object SvelteBlockLazyElementTypes {

override fun parseTokens(builder: PsiBuilder, parser: JavaScriptParser<*, *, *, *>) {
builder.advanceLexer() // JSTokenTypes.COLON
SvelteManualParsing.parseNotAllowedWhitespace(builder, ":")
builder.advanceLexer() // JSTokenTypes.IDENTIFIER -- fake THEN

// TODO Check weird RBRACE placement
Expand All @@ -105,6 +111,7 @@ object SvelteBlockLazyElementTypes {

override fun parseTokens(builder: PsiBuilder, parser: JavaScriptParser<*, *, *, *>) {
builder.advanceLexer() // JSTokenTypes.COLON
SvelteManualParsing.parseNotAllowedWhitespace(builder, ":")
builder.advanceLexer() // JSTokenTypes.CATCH_KEYWORD

parser.expressionParser.parseDestructuringElement(SvelteJSElementTypes.PARAMETER, false, false)
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/dev/blachut/svelte/lang/psi/SvelteTokenTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ object SvelteTokenTypes {
@JvmField
val END_MUSTACHE = SvelteElementType("END_MUSTACHE")

@JvmField
val TEMP_PREFIX = SvelteElementType("TEMP_PREFIX")
@JvmField
val LAZY_IF = SvelteElementType("LAZY_IF")
@JvmField
Expand Down

0 comments on commit 461e878

Please sign in to comment.