-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix labels+primitives bug, fix status-requests, update readme
- Loading branch information
1 parent
d5bb59b
commit e7b0be0
Showing
10 changed files
with
470 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 43 additions & 12 deletions
55
src/main/kotlin/senior/joinu/candid/CandidCodeGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,56 @@ | ||
package senior.joinu.candid | ||
|
||
import com.github.h0tk3y.betterParse.grammar.parseToEnd | ||
import com.squareup.kotlinpoet.FileSpec | ||
import senior.joinu.candid.idl.IDLGrammar | ||
import senior.joinu.candid.transpile.KtTranspiler | ||
import java.nio.charset.Charset | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Path | ||
|
||
|
||
object CandidCodeGenerator { | ||
sealed class Source { | ||
data class Str( | ||
val data: String, | ||
val generatedFileName: String | ||
) : Source() | ||
|
||
data class File( | ||
val path: Path, | ||
val generatedFileName: String? = null, | ||
val encoding: Charset = StandardCharsets.UTF_8 | ||
) : Source() | ||
} | ||
|
||
fun generateFor( | ||
didPath: Path, | ||
genPath: Path, | ||
genPackage: String = "", | ||
didEncoding: Charset = StandardCharsets.UTF_8 | ||
) { | ||
val did = didPath.toFile().readText(didEncoding) | ||
|
||
val program = IDLGrammar.parseToEnd(did) | ||
val ktContext = KtTranspiler.transpile(program, genPackage, didPath.fileName.toString()) | ||
val spec = ktContext.currentSpec.build() | ||
|
||
spec.writeTo(genPath) | ||
input: Source, | ||
genPackage: String = "" | ||
): FileSpec { | ||
val ktContext = when (input) { | ||
is Source.Str -> { | ||
val did = input.data | ||
val program = IDLGrammar.parseToEnd(did) | ||
|
||
KtTranspiler.transpile( | ||
program = program, | ||
packageName = genPackage, | ||
fileName = input.generatedFileName | ||
) | ||
} | ||
is Source.File -> { | ||
val did = input.path.toFile().readText(input.encoding) | ||
val program = IDLGrammar.parseToEnd(did) | ||
|
||
KtTranspiler.transpile( | ||
program = program, | ||
packageName = genPackage, | ||
fileName = input.generatedFileName ?: input.path.fileName.toString() | ||
) | ||
} | ||
} | ||
|
||
|
||
return ktContext.currentSpec.build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.