Improve: Remove unnecessary field in NeoLang

This commit is contained in:
zt515
2017-08-08 10:45:52 +08:00
parent d21efbb46d
commit 4b9bf83181
8 changed files with 111 additions and 23 deletions

View File

@ -0,0 +1,3 @@
extra-key: {
program: { "vim", "vi" }
}

View File

@ -1,6 +1,8 @@
package io.neolang.parser package io.neolang.parser
import io.neolang.ast.* import io.neolang.ast.NeoLangToken
import io.neolang.ast.NeoLangTokenType
import io.neolang.ast.NeoLangTokenValue
import io.neolang.ast.base.NeoLangAst import io.neolang.ast.base.NeoLangAst
import io.neolang.ast.node.* import io.neolang.ast.node.*
@ -8,9 +10,6 @@ import io.neolang.ast.node.*
* @author kiva * @author kiva
*/ */
class NeoLangParser { class NeoLangParser {
var ast: NeoLangAst? = null
private set
private val lexer = NeoLangLexer() private val lexer = NeoLangLexer()
private var tokens = mutableListOf<NeoLangToken>() private var tokens = mutableListOf<NeoLangToken>()
private var currentPosition: Int = 0 private var currentPosition: Int = 0
@ -21,22 +20,20 @@ class NeoLangParser {
} }
fun parse(): NeoLangAst { fun parse(): NeoLangAst {
updateParserStatus(lexer.lex()) return updateParserStatus(lexer.lex()) ?: throw ParseException("AST is null")
return ast ?: throw ParseException("AST is null")
} }
private fun updateParserStatus(tokens: List<NeoLangToken>) { private fun updateParserStatus(tokens: List<NeoLangToken>): NeoLangAst? {
if (tokens.isEmpty()) { if (tokens.isEmpty()) {
// Allow empty program // Allow empty program
ast = NeoLangProgramNode.emptyNode() return NeoLangProgramNode.emptyNode()
return
} }
this.tokens.clear() this.tokens.clear()
this.tokens.addAll(tokens) this.tokens.addAll(tokens)
currentPosition = 0 currentPosition = 0
currentToken = tokens[currentPosition] currentToken = tokens[currentPosition]
ast = program() return program()
} }
private fun match(tokenType: NeoLangTokenType, errorThrow: Boolean = false): Boolean { private fun match(tokenType: NeoLangTokenType, errorThrow: Boolean = false): Boolean {

View File

@ -1,9 +1,97 @@
package io.neoterm.customize.config.loader package io.neoterm.customize.config.loader
import io.neoterm.customize.eks.ExtraKeyConfigParser
import io.neoterm.customize.eks.NeoExtraKey
import io.neoterm.frontend.config.ConfigVisitor
import io.neoterm.frontend.config.NeoConfigureFile import io.neoterm.frontend.config.NeoConfigureFile
import io.neoterm.view.eks.button.TextButton
import java.io.BufferedReader
import java.io.File import java.io.File
/** /**
* @author kiva * @author kiva
*/ */
class OldExtraKeysConfigureFile(configureFile: File) : NeoConfigureFile(configureFile) class OldExtraKeysConfigureFile(configureFile: File) : NeoConfigureFile(configureFile) {
override var configVisitor: ConfigVisitor? = null
override fun parseConfigure(): Boolean {
return super.parseConfigure()
}
private fun parseOldConfig(source: BufferedReader): NeoExtraKey {
val config = NeoExtraKey()
var line: String? = source.readLine()
while (line != null) {
line = line.trim().trimEnd()
if (line.isEmpty() || line.startsWith("#")) {
line = source.readLine()
continue
}
if (line.startsWith("version")) {
parseHeader(line, config)
} else if (line.startsWith("program")) {
parseProgram(line, config)
} else if (line.startsWith("define")) {
parseKeyDefine(line, config)
} else if (line.startsWith("with-default")) {
parseWithDefault(line, config)
}
line = source.readLine()
}
if (config.version < 0) {
throw RuntimeException("Not a valid shortcut config file")
}
if (config.programNames.size == 0) {
throw RuntimeException("At least one program name should be given")
}
return config
}
private fun parseWithDefault(line: String, config: NeoExtraKey) {
val value = line.substring("with-default".length).trim().trimEnd()
config.withDefaultKeys = value == "true"
}
private fun parseKeyDefine(line: String, config: NeoExtraKey) {
val keyDefine = line.substring("define".length).trim().trimEnd()
val keyValues = keyDefine.split(" ")
if (keyValues.size < 2) {
throw RuntimeException("Bad define")
}
val buttonText = keyValues[0]
val withEnter = keyValues[1] == "true"
config.shortcutKeys.add(TextButton(buttonText, withEnter))
}
private fun parseProgram(line: String, config: NeoExtraKey) {
val programNames = line.substring("program".length).trim().trimEnd()
if (programNames.isEmpty()) {
return
}
for (name in programNames.split(" ")) {
config.programNames.add(name)
}
}
private fun parseHeader(line: String, config: NeoExtraKey) {
val version: Int
val versionString = line.substring("version".length).trim().trimEnd()
try {
version = Integer.parseInt(versionString)
} catch (e: NumberFormatException) {
throw RuntimeException("Bad version '$versionString'")
}
if (version > ExtraKeyConfigParser.PARSER_VERSION) {
throw RuntimeException("Required version: $version, please upgrade your app")
}
config.version = version
}
}

View File

@ -9,7 +9,7 @@ import java.io.File
* @author kiva * @author kiva
*/ */
object ExtraKeyConfigLoader { object ExtraKeyConfigLoader {
class ConfiguredExtraKey(val config: ExtraKeyConfig) : ExtraKey { class ConfiguredExtraKey(val config: NeoExtraKey) : IExtraKey {
override fun applyShortcutKeys(extraKeysView: ExtraKeysView) { override fun applyShortcutKeys(extraKeysView: ExtraKeysView) {
if (config.withDefaultKeys) { if (config.withDefaultKeys) {
extraKeysView.loadDefaultUserKeys() extraKeysView.loadDefaultUserKeys()
@ -42,7 +42,7 @@ object ExtraKeyConfigLoader {
} }
} }
private fun registerConfig(extraKeysManager: ExtraKeysService, config: ExtraKeyConfig) { private fun registerConfig(extraKeysManager: ExtraKeysService, config: NeoExtraKey) {
val shortcutKey = ConfiguredExtraKey(config) val shortcutKey = ConfiguredExtraKey(config)
for (programName in config.programNames) { for (programName in config.programNames) {
extraKeysManager.registerShortcutKeys(programName, shortcutKey) extraKeysManager.registerShortcutKeys(programName, shortcutKey)

View File

@ -25,8 +25,8 @@ class ExtraKeyConfigParser {
source = BufferedReader(InputStreamReader(inputStream)) source = BufferedReader(InputStreamReader(inputStream))
} }
fun parse(): ExtraKeyConfig { fun parse(): NeoExtraKey {
val config = ExtraKeyConfig() val config = NeoExtraKey()
var line: String? = source.readLine() var line: String? = source.readLine()
while (line != null) { while (line != null) {
@ -57,12 +57,12 @@ class ExtraKeyConfigParser {
return config return config
} }
private fun parseWithDefault(line: String, config: ExtraKeyConfig) { private fun parseWithDefault(line: String, config: NeoExtraKey) {
val value = line.substring("with-default".length).trim().trimEnd() val value = line.substring("with-default".length).trim().trimEnd()
config.withDefaultKeys = value == "true" config.withDefaultKeys = value == "true"
} }
private fun parseKeyDefine(line: String, config: ExtraKeyConfig) { private fun parseKeyDefine(line: String, config: NeoExtraKey) {
val keyDefine = line.substring("define".length).trim().trimEnd() val keyDefine = line.substring("define".length).trim().trimEnd()
val keyValues = keyDefine.split(" ") val keyValues = keyDefine.split(" ")
if (keyValues.size < 2) { if (keyValues.size < 2) {
@ -75,7 +75,7 @@ class ExtraKeyConfigParser {
config.shortcutKeys.add(TextButton(buttonText, withEnter)) config.shortcutKeys.add(TextButton(buttonText, withEnter))
} }
private fun parseProgram(line: String, config: ExtraKeyConfig) { private fun parseProgram(line: String, config: NeoExtraKey) {
val programNames = line.substring("program".length).trim().trimEnd() val programNames = line.substring("program".length).trim().trimEnd()
if (programNames.isEmpty()) { if (programNames.isEmpty()) {
return return
@ -86,7 +86,7 @@ class ExtraKeyConfigParser {
} }
} }
private fun parseHeader(line: String, config: ExtraKeyConfig) { private fun parseHeader(line: String, config: NeoExtraKey) {
val version: Int val version: Int
val versionString = line.substring("version".length).trim().trimEnd() val versionString = line.substring("version".length).trim().trimEnd()
try { try {

View File

@ -19,7 +19,7 @@ class ExtraKeysService : NeoService {
checkForFiles() checkForFiles()
} }
val EKS_KEYS: MutableMap<String, ExtraKey> = mutableMapOf() val EKS_KEYS: MutableMap<String, IExtraKey> = mutableMapOf()
fun showShortcutKeys(program: String, extraKeysView: ExtraKeysView?) { fun showShortcutKeys(program: String, extraKeysView: ExtraKeysView?) {
if (extraKeysView == null) { if (extraKeysView == null) {
@ -35,7 +35,7 @@ class ExtraKeysService : NeoService {
extraKeysView.loadDefaultUserKeys() extraKeysView.loadDefaultUserKeys()
} }
fun registerShortcutKeys(program: String, eksKey: ExtraKey?) { fun registerShortcutKeys(program: String, eksKey: IExtraKey?) {
if (eksKey == null) { if (eksKey == null) {
if (this.EKS_KEYS.containsKey(program)) { if (this.EKS_KEYS.containsKey(program)) {
this.EKS_KEYS.remove(program) this.EKS_KEYS.remove(program)

View File

@ -5,6 +5,6 @@ import io.neoterm.view.eks.ExtraKeysView
/** /**
* @author kiva * @author kiva
*/ */
interface ExtraKey { interface IExtraKey {
fun applyShortcutKeys(extraKeysView: ExtraKeysView) fun applyShortcutKeys(extraKeysView: ExtraKeysView)
} }

View File

@ -5,7 +5,7 @@ import io.neoterm.view.eks.button.IExtraButton
/** /**
* @author kiva * @author kiva
*/ */
class ExtraKeyConfig { class NeoExtraKey {
var version: Int = -1 var version: Int = -1
val programNames: MutableList<String> = mutableListOf() val programNames: MutableList<String> = mutableListOf()
val shortcutKeys: MutableList<IExtraButton> = mutableListOf() val shortcutKeys: MutableList<IExtraButton> = mutableListOf()