Feature: NeoConfigureFile interface

This commit is contained in:
zt515 2017-08-06 00:28:51 +08:00
parent 5de8771857
commit bd4ea35ed9
12 changed files with 163 additions and 26 deletions

View File

@ -9,4 +9,9 @@ class AstVisitor(private val ast: NeoLangAst, private val visitorCallback: IVisi
fun start() {
AstVisitorImpl.visitStartAst(ast, visitorCallback)
}
@Suppress("UNCHECKED_CAST")
fun <T : IVisitorCallback> getCallback() : T {
return visitorCallback as T
}
}

View File

@ -1,8 +0,0 @@
package io.neolang.runtime.context
/**
* @author kiva
*/
interface IContextMaker {
fun makeContext()
}

View File

@ -15,4 +15,8 @@ class NeoLangContext(val contextName: String) {
fun getAttribute(attributeName: String) : NeoLangValue {
return attributes[attributeName] ?: NeoLangValue.UNDEFINED
}
fun getAttributes() : Map<String, NeoLangValue> {
return attributes
}
}

View File

@ -16,6 +16,10 @@ class NeoLangValue(private val rawValue: Any) {
}
}
fun isValid() : Boolean {
return this != UNDEFINED
}
companion object {
val UNDEFINED = NeoLangValue("<undefined>")
}

View File

@ -57,4 +57,5 @@ dependencies {
// compile 'com.ramotion.cardslider:card-slider:0.1.0'
// compile 'com.github.igalata:Bubble-Picker:v0.2.4'
compile project(path: ':NeoLang')
testCompile project(path: ':NeoLang')
}

View File

@ -1,14 +1,7 @@
package io.neoterm
import android.app.Application
import android.util.Log
import io.neoterm.customize.color.ColorSchemeManager
import io.neoterm.customize.completion.CompletionProviderManager
import io.neoterm.customize.eks.ExtraKeysManager
import io.neoterm.customize.font.FontManager
import io.neoterm.customize.pm.NeoPackageManager
import io.neoterm.customize.script.UserScriptManager
import io.neoterm.frontend.service.ServiceManager
import io.neoterm.frontend.NeoFrontend
import io.neoterm.preference.NeoPreference
import io.neoterm.utils.CrashHandler
@ -21,14 +14,7 @@ class App : Application() {
app = this
NeoPreference.init(this)
CrashHandler.init()
// ensure that we can access these any time
ServiceManager.registerService(ColorSchemeManager::class.java)
ServiceManager.registerService(FontManager::class.java)
ServiceManager.registerService(UserScriptManager::class.java)
ServiceManager.registerService(ExtraKeysManager::class.java)
ServiceManager.registerService(CompletionProviderManager::class.java)
ServiceManager.registerService(NeoPackageManager::class.java)
NeoFrontend.initialize()
}
companion object {

View File

@ -14,7 +14,6 @@ import java.util.*
open class NeoColorScheme(val colorName: String) {
companion object {
private const val COLOR_PREFIX = "color"
const val COLOR_DIM_BLACK = 0
const val COLOR_DIM_RED = 1
const val COLOR_DIM_GREEN = 2
@ -64,7 +63,7 @@ open class NeoColorScheme(val colorName: String) {
}
fun createConfig(): Properties {
// TODO: 设计新的配色方案语法,这个只是临时用一下
// TODO: 兼容旧版本的配置并且解析新版本的配置文件
validateColors()
val prop = Properties()
prop.put("foreground", foregroundColor)
@ -77,6 +76,7 @@ open class NeoColorScheme(val colorName: String) {
}
fun parseConfig(file: File): Boolean {
// TODO: 兼容旧版本的配置并且解析新版本的配置文件
try {
return FileInputStream(file).use {
val prop = Properties()

View File

@ -0,0 +1,27 @@
package io.neoterm.frontend
import io.neoterm.customize.color.ColorSchemeManager
import io.neoterm.customize.completion.CompletionProviderManager
import io.neoterm.customize.eks.ExtraKeysManager
import io.neoterm.customize.font.FontManager
import io.neoterm.customize.pm.NeoPackageManager
import io.neoterm.customize.script.UserScriptManager
import io.neoterm.frontend.logger.NLog
import io.neoterm.frontend.service.ServiceManager
/**
* @author kiva
*/
object NeoFrontend {
fun initialize() {
// ensure that we can access these any time
ServiceManager.registerService(ColorSchemeManager::class.java)
ServiceManager.registerService(FontManager::class.java)
ServiceManager.registerService(UserScriptManager::class.java)
ServiceManager.registerService(ExtraKeysManager::class.java)
ServiceManager.registerService(CompletionProviderManager::class.java)
ServiceManager.registerService(NeoPackageManager::class.java)
NLog.initialize()
}
}

View File

@ -0,0 +1,49 @@
package io.neoterm.frontend.config
import io.neolang.ast.visitor.IVisitorCallback
import io.neolang.runtime.context.NeoLangContext
import io.neolang.runtime.type.NeoLangValue
import java.util.*
class ConfigVisitor : IVisitorCallback {
private val emptyContext = NeoLangContext("<NeoTerm-Empty-Safety>")
private val contextStack = Stack<NeoLangContext>()
private val definedContext = mutableListOf<NeoLangContext>()
fun getContext(contextName: String) : NeoLangContext {
definedContext.forEach {
if (it.contextName == contextName) {
return it
}
}
return emptyContext
}
fun getAttribute(contextName: String, attrName: String) : NeoLangValue {
return getContext(contextName).getAttribute(attrName)
}
override fun onStart() {
onEnterContext("global")
}
override fun onFinish() {
while (contextStack.isNotEmpty()) {
onExitContext()
}
}
override fun onEnterContext(contextName: String) {
val context = NeoLangContext(contextName)
contextStack.push(context)
}
override fun onExitContext() {
val context = contextStack.pop()
definedContext.add(context)
}
override fun getCurrentContext(): NeoLangContext {
return contextStack.peek()
}
}

View File

@ -0,0 +1,36 @@
package io.neoterm.frontend.config
import io.neolang.parser.NeoLangParser
import io.neoterm.utils.FileUtils
import java.io.File
/**
* @author kiva
*/
class NeoConfigureFile(val configureFile: String) {
private val configParser = NeoLangParser()
private var configVisitor : ConfigVisitor? = null
fun getVisitor(): ConfigVisitor {
checkParsed()
return configVisitor!!
}
fun parseConfigure(): Boolean {
val configContent = FileUtils.readFile(File(configureFile)) ?: return false
val programCode = String(configContent)
configParser.setInputSource(programCode)
val ast = configParser.parse()
val astVisitor = ast.visit().getVisitor(ConfigVisitor::class.java) ?: return false
astVisitor.start()
configVisitor = astVisitor.getCallback()
return true
}
private fun checkParsed() {
if (configVisitor == null) {
throw IllegalStateException("Configure file not loaded.")
}
}
}

View File

@ -0,0 +1,9 @@
package io.neoterm.frontend.logger
/**
* @author kiva
*/
object NLog {
fun initialize() {
}
}

View File

@ -0,0 +1,24 @@
package io.neoterm
import io.neoterm.frontend.config.ConfigVisitor
import io.neoterm.frontend.config.NeoConfigureFile
import org.junit.Test
/**
* @author kiva
*/
class ConfigureFileTest {
private fun printAttr(visitor: ConfigVisitor, contextName: String, attrName: String) {
println("attr [$contextName->$attrName]: ${visitor.getAttribute(contextName, attrName).asString()}")
}
@Test
fun configureFileTest() {
val config = NeoConfigureFile("NeoLang/example/color-scheme.nl")
if (config.parseConfigure()) {
println("Parsed!")
val visitor = config.getVisitor()
printAttr(visitor, "colors", "foreground")
}
}
}