Skip to content

Commit

Permalink
Clean up parser code
Browse files Browse the repository at this point in the history
  • Loading branch information
sblundy committed Aug 25, 2019
1 parent 5c08370 commit 717ae9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import com.intellij.psi.tree.IFileElementType
import com.intellij.psi.tree.TokenSet



/**
*/
class ElvishParserDefinition: ParserDefinition {
class ElvishParserDefinition : ParserDefinition {
override fun createParser(project: Project?): PsiParser = ElvishParser()
override fun createLexer(project: Project?): Lexer = ElvishLexerAdapter()
override fun createFile(viewProvider: FileViewProvider): PsiFile = ElvishFile(viewProvider)
Expand All @@ -30,5 +27,17 @@ class ElvishParserDefinition: ParserDefinition {
companion object {
val FILE = IFileElementType(ElvishLanguage.INSTANCE)
val COMMENT = TokenSet.create(ElvishTypes.COMMENT)
val KEYWORDS = TokenSet.create(
ElvishTypes.KEYWORD_DEL,
ElvishTypes.KEYWORD_ELIF,
ElvishTypes.KEYWORD_ELSE,
ElvishTypes.KEYWORD_EXCEPT,
ElvishTypes.KEYWORD_FINALLY,
ElvishTypes.KEYWORD_FN,
ElvishTypes.KEYWORD_FOR,
ElvishTypes.KEYWORD_IF,
ElvishTypes.KEYWORD_TRY,
ElvishTypes.KEYWORD_WHILE
)
}
}
19 changes: 4 additions & 15 deletions src/main/kotlin/com/github/sblundy/elvish/lang/parserUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,10 @@ package com.github.sblundy.elvish.lang

import com.github.sblundy.elvish.psi.ElvishTypes
import com.intellij.lang.PsiBuilder
import com.intellij.lang.parser.GeneratedParserUtilBase

private val keywords = setOf(
ElvishTypes.KEYWORD_DEL,
ElvishTypes.KEYWORD_ELIF,
ElvishTypes.KEYWORD_ELSE,
ElvishTypes.KEYWORD_EXCEPT,
ElvishTypes.KEYWORD_FINALLY,
ElvishTypes.KEYWORD_FN,
ElvishTypes.KEYWORD_FOR,
ElvishTypes.KEYWORD_IF,
ElvishTypes.KEYWORD_TRY,
ElvishTypes.KEYWORD_WHILE)

fun parseKeywordAsBareword(builder: PsiBuilder, level: Int): Boolean {
if (!(builder.tokenType in keywords)) return false
fun parseKeywordAsBareword(builder: PsiBuilder, @Suppress("UNUSED_PARAMETER") level: Int): Boolean {
if (builder.tokenType !in ElvishParserDefinition.KEYWORDS) return false
builder.remapCurrentToken(ElvishTypes.BAREWORD_CHAR)
return true
return GeneratedParserUtilBase.consumeToken(builder, ElvishTypes.BAREWORD_CHAR)
}

0 comments on commit 717ae9e

Please sign in to comment.