mirror of
https://github.com/irmen/prog8.git
synced 2024-07-05 22:29:04 +00:00
+ temporarily add PetsciiEncoding (and Petscii.kt copied from compiler) to parser; .linkParents for child nodes of Module
This commit is contained in:
parent
4eb61529f6
commit
46911a8905
1171
compilerAst/src/prog8/parser/Petscii.kt
Normal file
1171
compilerAst/src/prog8/parser/Petscii.kt
Normal file
File diff suppressed because it is too large
Load Diff
24
compilerAst/src/prog8/parser/PetsciiEncoding.kt
Normal file
24
compilerAst/src/prog8/parser/PetsciiEncoding.kt
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package prog8.parser
|
||||||
|
|
||||||
|
import prog8.ast.IStringEncoding
|
||||||
|
import java.io.CharConversionException
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: remove once [IStringEncoding] has been to compiler module
|
||||||
|
*/
|
||||||
|
object PetsciiEncoding : IStringEncoding {
|
||||||
|
override fun encodeString(str: String, altEncoding: Boolean) =
|
||||||
|
try {
|
||||||
|
if (altEncoding) Petscii.encodeScreencode(str, true) else Petscii.encodePetscii(str, true)
|
||||||
|
} catch (x: CharConversionException) {
|
||||||
|
throw CharConversionException("can't convert string to target machine's char encoding: ${x.message}")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun decodeString(bytes: List<Short>, altEncoding: Boolean) =
|
||||||
|
try {
|
||||||
|
if (altEncoding) Petscii.decodeScreencode(bytes, true) else Petscii.decodePetscii(bytes, true)
|
||||||
|
} catch (x: CharConversionException) {
|
||||||
|
throw CharConversionException("can't decode string: ${x.message}")
|
||||||
|
}
|
||||||
|
}
|
@ -4,20 +4,8 @@ import org.antlr.v4.runtime.*
|
|||||||
import org.antlr.v4.runtime.misc.ParseCancellationException
|
import org.antlr.v4.runtime.misc.ParseCancellationException
|
||||||
import prog8.ast.antlr.toAst
|
import prog8.ast.antlr.toAst
|
||||||
import prog8.ast.Module
|
import prog8.ast.Module
|
||||||
import prog8.ast.base.Position
|
|
||||||
import prog8.ast.IStringEncoding
|
|
||||||
|
|
||||||
|
|
||||||
object DummyEncoding: IStringEncoding {
|
|
||||||
override fun encodeString(str: String, altEncoding: Boolean): List<Short> {
|
|
||||||
TODO("move StringEncoding out of compilerAst")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun decodeString(bytes: List<Short>, altEncoding: Boolean): String {
|
|
||||||
TODO("move StringEncoding out of compilerAst")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Prog8ErrorStrategy: BailErrorStrategy() {
|
class Prog8ErrorStrategy: BailErrorStrategy() {
|
||||||
override fun recover(recognizer: Parser?, e: RecognitionException?) {
|
override fun recover(recognizer: Parser?, e: RecognitionException?) {
|
||||||
try {
|
try {
|
||||||
@ -56,10 +44,14 @@ class Prog8Parser(private val errorListener: ANTLRErrorListener = ThrowErrorList
|
|||||||
parser.addErrorListener(errorListener)
|
parser.addErrorListener(errorListener)
|
||||||
|
|
||||||
val parseTree = parser.module()
|
val parseTree = parser.module()
|
||||||
|
val moduleName = "anonymous"
|
||||||
|
|
||||||
|
val module = parseTree.toAst(moduleName, pathFrom(""), PetsciiEncoding)
|
||||||
// TODO: use Module ctor directly
|
// TODO: use Module ctor directly
|
||||||
val moduleAst = parseTree.toAst("anonymous", pathFrom(""), DummyEncoding)
|
|
||||||
|
|
||||||
return moduleAst
|
for (statement in module.statements) {
|
||||||
|
statement.linkParents(module)
|
||||||
|
}
|
||||||
|
return module
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
16
compilerAst/src/prog8/parser/ThrowTodoEncoding.kt
Normal file
16
compilerAst/src/prog8/parser/ThrowTodoEncoding.kt
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package prog8.parser
|
||||||
|
|
||||||
|
import prog8.ast.IStringEncoding
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: remove once [IStringEncoding] has been to compiler module
|
||||||
|
*/
|
||||||
|
object ThrowTodoEncoding: IStringEncoding {
|
||||||
|
override fun encodeString(str: String, altEncoding: Boolean): List<Short> {
|
||||||
|
TODO("move StringEncoding out of compilerAst")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun decodeString(bytes: List<Short>, altEncoding: Boolean): String {
|
||||||
|
TODO("move StringEncoding out of compilerAst")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user