Skip to content

Commit b744ea7

Browse files
committed
Update remap and ignore errors in lines commented by the preprocessor
This remap update, among various improvements, moves the error handling to the consumer (us) which allows us to ignore any errors happening in lines which the preprocessor would have commented out anyway.
1 parent 897066f commit b744ea7

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ repositories {
4747
dependencies {
4848
implementation(gradleApi())
4949
compile(localGroovy())
50-
implementation("com.github.replaymod:remap:64d841a")
50+
implementation("com.github.replaymod:remap:19874df")
5151
implementation("net.fabricmc:tiny-mappings-parser:0.1.1.8")
5252
}

src/main/kotlin/com/replaymod/gradle/preprocess/PreprocessTask.kt

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.replaymod.gradle.remap.legacy.LegacyMappingSetModelFactory
66
import org.cadixdev.lorenz.MappingSet
77
import org.cadixdev.lorenz.io.MappingFormats
88
import org.gradle.api.DefaultTask
9+
import org.gradle.api.GradleException
910
import org.gradle.api.file.FileCollection
1011
import org.gradle.api.tasks.*
1112
import org.gradle.api.tasks.compile.AbstractCompile
@@ -104,7 +105,7 @@ open class PreprocessTask : DefaultTask() {
104105
val inPath = source.toPath()
105106
val outPath = generated!!.toPath()
106107
val inPlace = inPath.toAbsolutePath() == outPath.toAbsolutePath()
107-
var mappedSources: Map<String, String>? = null
108+
var mappedSources: Map<String, Pair<String, List<Pair<Int, String>>>>? = null
108109

109110
val mapping = mapping
110111
val classpath = classpath
@@ -156,7 +157,13 @@ open class PreprocessTask : DefaultTask() {
156157
val kws = keywords.get().entries.find { (ext, _) -> file.name.endsWith(ext) }
157158
if (kws != null) {
158159
val javaTransform = { lines: List<String> ->
159-
mappedSources?.get(relPath.toString())?.lines() ?: lines
160+
mappedSources?.get(relPath.toString())?.let { (source, errors) ->
161+
val errorsByLine = mutableMapOf<Int, MutableList<String>>()
162+
for ((line, error) in errors) {
163+
errorsByLine.getOrPut(line, ::mutableListOf).add(error)
164+
}
165+
source.lines().mapIndexed { index: Int, line: String -> Pair(line, errorsByLine[index] ?: emptyList<String>()) }
166+
} ?: lines.map { Pair(it, emptyList()) }
160167
}
161168
commentPreprocessor.convertFile(kws.value, file, outFile, javaTransform)
162169
} else if (!inPlace) {
@@ -166,6 +173,10 @@ open class PreprocessTask : DefaultTask() {
166173
}
167174
}
168175
}
176+
177+
if (commentPreprocessor.fail) {
178+
throw GradleException("Failed to remap sources. See errors above for details.")
179+
}
169180
}
170181
}
171182

@@ -176,6 +187,8 @@ class CommentPreprocessor(private val vars: Map<String, Int>) {
176187
private val AND_PATTERN = Pattern.quote("&&").toPattern()
177188
}
178189

190+
var fail = false
191+
179192
private fun String.evalVar() = toIntOrNull() ?: vars[this] ?: throw NoSuchElementException(this)
180193

181194
private fun String.evalExpr(): Boolean {
@@ -209,15 +222,17 @@ class CommentPreprocessor(private val vars: Map<String, Int>) {
209222
private val String.indentation: Int
210223
get() = takeWhile { it == ' ' }.length
211224

212-
fun convertSource(kws: Keywords, lines: List<String>, remapped: List<String>, fileName: String): List<String> {
225+
fun convertSource(kws: Keywords, lines: List<String>, remapped: List<Pair<String, List<String>>>, fileName: String): List<String> {
213226
val ifStack = mutableListOf<Boolean>()
214227
val indentStack = mutableListOf<Int>()
215228
var active = true
216229
var n = 0
217-
return lines.zip(remapped).map { (originalLine, line) ->
230+
return lines.zip(remapped).map { (originalLine, lineMapped) ->
231+
val (line, errors) = lineMapped
232+
var ignoreErrors = false
218233
n++
219234
val trimmed = line.trim()
220-
if (trimmed.startsWith(kws.`if`)) {
235+
val mapped = if (trimmed.startsWith(kws.`if`)) {
221236
val result = trimmed.substring(kws.`if`.length).trim().evalExpr()
222237
ifStack.push(result)
223238
indentStack.push(line.indentation)
@@ -269,23 +284,31 @@ class CommentPreprocessor(private val vars: Map<String, Int>) {
269284
// and, more importantly, if we do not preserve it, we might permanently loose it as the
270285
// remap process is only guaranteed to work on code which compiles and since we're
271286
// just about to comment it out, it probably doesn't compile.
287+
ignoreErrors = true
272288
" ".repeat(currIndent) + kws.eval + " " + originalLine.substring(currIndent)
273289
} else {
274290
line
275291
}
276292
}
277293
}
294+
if (errors.isNotEmpty() && !ignoreErrors) {
295+
fail = true
296+
for (message in errors) {
297+
System.err.println("$fileName:$n: $message")
298+
}
299+
}
300+
mapped
278301
}.also {
279302
if (ifStack.isNotEmpty()) {
280303
throw ParserException("Missing endif in $fileName")
281304
}
282305
}
283306
}
284307

285-
fun convertFile(kws: Keywords, inFile: File, outFile: File, remap: ((List<String>) -> List<String>)? = null) {
308+
fun convertFile(kws: Keywords, inFile: File, outFile: File, remap: ((List<String>) -> List<Pair<String, List<String>>>)? = null) {
286309
val string = inFile.readText()
287310
var lines = string.lines()
288-
val remapped = remap?.invoke(lines) ?: lines
311+
val remapped = remap?.invoke(lines) ?: lines.map { Pair(it, emptyList()) }
289312
try {
290313
lines = convertSource(kws, lines, remapped, inFile.path)
291314
} catch (e: Throwable) {

0 commit comments

Comments
 (0)