commonMain.org.antlr.v4.kotlinruntime.misc.InterpreterDataReader.kt Maven / Gradle / Ivy
// Copyright 2017-present Strumenta and contributors, licensed under Apache 2.0.
// Copyright 2024-present Strumenta and contributors, licensed under BSD 3-Clause.
package org.antlr.v4.kotlinruntime.misc
import org.antlr.v4.kotlinruntime.Vocabulary
import org.antlr.v4.kotlinruntime.atn.ATN
// TODO(Edoardo): this one requires reading files
// A class to read plain text interpreter data produced by ANTLR.
public object InterpreterDataReader {
public class InterpreterData {
internal var atn: ATN? = null
internal var vocabulary: Vocabulary? = null
internal var ruleNames: MutableList? = null
internal var channels: MutableList? = null // Only valid for lexer grammars.
internal var modes: MutableList? = null // ditto
}
// /**
// * The structure of the data file is very simple. Everything is line based with empty lines
// * separating the different parts. For lexers the layout is:
// * token literal names:
// * ...
// *
// * token symbolic names:
// * ...
// *
// * rule names:
// * ...
// *
// * channel names:
// * ...
// *
// * mode names:
// * ...
// *
// * atn:
// * enclosed in a pair of squared brackets.
// *
// * Data for a parser does not contain channel and mode names.
// */
// fun parseFile(fileName: String): InterpreterData {
// val result = InterpreterData()
// result.ruleNames = ArrayList()
//
// try {
// BufferedReader(FileReader(fileName)).use { br ->
// var line: String
// val literalNames = ArrayList()
// val symbolicNames = ArrayList()
//
// line = br.readLine()
// if (line != "token literal names:")
// throw RuntimeException("Unexpected data entry")
// while ((line = br.readLine()) != null) {
// if (line.isEmpty())
// break
// literalNames.add(if (line == "null") "" else line)
// }
//
// line = br.readLine()
// if (line != "token symbolic names:")
// throw RuntimeException("Unexpected data entry")
// while ((line = br.readLine()) != null) {
// if (line.isEmpty())
// break
// symbolicNames.add(if (line == "null") "" else line)
// }
//
// result.vocabulary = VocabularyImpl(literalNames.toTypedArray(), symbolicNames.toTypedArray())
//
// line = br.readLine()
// if (line != "rule names:")
// throw RuntimeException("Unexpected data entry")
// while ((line = br.readLine()) != null) {
// if (line.isEmpty())
// break
// result.ruleNames!!.add(line)
// }
//
// if (line == "channel names:") { // Additional lexer data.
// result.channels = ArrayList()
// while ((line = br.readLine()) != null) {
// if (line.isEmpty())
// break
// result.channels!!.add(line)
// }
//
// line = br.readLine()
// if (line != "mode names:")
// throw RuntimeException("Unexpected data entry")
// result.modes = ArrayList()
// while ((line = br.readLine()) != null) {
// if (line.isEmpty())
// break
// result.modes!!.add(line)
// }
// }
//
// line = br.readLine()
// if (line != "atn:")
// throw RuntimeException("Unexpected data entry")
// line = br.readLine()
// val elements = line.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
// val serializedATN = CharArray(elements.size)
//
// for (i in elements.indices) {
// val value: Int
// val element = elements[i]
// if (element.startsWith("["))
// value = Integer.parseInt(element.substring(1).trim { it <= ' ' })
// else if (element.endsWith("]"))
// value = Integer.parseInt(element.substring(0, element.length - 1).trim { it <= ' ' })
// else
// value = Integer.parseInt(element.trim { it <= ' ' })
// serializedATN[i] = value.toChar()
// }
//
// val deserializer = ATNDeserializer()
// result.atn = deserializer.deserialize(serializedATN)
// }
// } catch (e: java.io.IOException) {
// // We just swallow the error and return empty objects instead.
// }
//
// return result
// }
}