From 7f15b7b716ad1f22d26bc265ea970a90399ee1ec Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 19 Oct 2021 22:12:54 +0200 Subject: [PATCH] remove unneeded check for duplicate module names as this is now caught by the logic in Program.addModule itself --- .../compiler/astprocessing/AstExtensions.kt | 21 ------------------- compilerAst/src/prog8/parser/Prog8Parser.kt | 5 ++--- compilerAst/test/ast/ProgramTests.kt | 8 +++---- 3 files changed, 6 insertions(+), 28 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt b/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt index e608f4a84..fcff410d1 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt @@ -3,7 +3,6 @@ package prog8.compiler.astprocessing import prog8.ast.Node import prog8.ast.Program import prog8.ast.base.DataType -import prog8.ast.base.FatalAstException import prog8.ast.expressions.CharLiteral import prog8.ast.expressions.NumericLiteralValue import prog8.ast.expressions.RangeExpr @@ -127,26 +126,6 @@ internal fun Program.checkIdentifiers(errors: IErrorReporter, options: Compilati lit2decl.visit(this) lit2decl.applyModifications() } - - // Check if each module has a unique name. - // If not report those that haven't. - // TODO: move check for unique module names to earlier stage and/or to unit tests - val namesToModules = mapOf>().toMutableMap() - for (m in modules) { - val others = namesToModules[m.name] - if (others == null) { - namesToModules[m.name] = listOf(m).toMutableList() - } else { - others.add(m) - } - } - val nonUniqueNames = namesToModules.keys - .map { Pair(it, namesToModules[it]!!.size) } - .filter { it.second > 1 } - .map { "\"${it.first}\" (x${it.second})"} - if (nonUniqueNames.isNotEmpty()) { - throw FatalAstException("modules must have unique names; of the ttl ${modules.size} these have not: $nonUniqueNames") - } } internal fun Program.variousCleanups(program: Program, errors: IErrorReporter) { diff --git a/compilerAst/src/prog8/parser/Prog8Parser.kt b/compilerAst/src/prog8/parser/Prog8Parser.kt index 5524be3ba..8e8d60002 100644 --- a/compilerAst/src/prog8/parser/Prog8Parser.kt +++ b/compilerAst/src/prog8/parser/Prog8Parser.kt @@ -95,8 +95,7 @@ object Prog8Parser { private class AntlrErrorListener(val src: SourceCode): BaseErrorListener() { override fun syntaxError(recognizer: Recognizer<*, *>?, offendingSymbol: Any?, line: Int, charPositionInLine: Int, msg: String, e: RecognitionException?) { if (e == null) { - TODO("no RecognitionException - create your own ParseError") - //throw ParseError() + throw ParseError(msg, Position(src.origin, line, charPositionInLine, charPositionInLine), RuntimeException("parse error")) } else { if(e.offendingToken==null) { throw ParseError(msg, Position(src.origin, line, charPositionInLine, charPositionInLine), e) @@ -111,7 +110,7 @@ object Prog8Parser { val offending = this.offendingToken val line = offending.line val beginCol = offending.charPositionInLine - val endCol = beginCol + offending.stopIndex - offending.startIndex // TODO: point to col *after* token? + val endCol = beginCol + offending.stopIndex - offending.startIndex // TODO: point to col *after* token? / why, what's wrong with endCol being inclusive return Position(file, line, beginCol, endCol) } diff --git a/compilerAst/test/ast/ProgramTests.kt b/compilerAst/test/ast/ProgramTests.kt index 5e51bf10c..9a9e13c3a 100644 --- a/compilerAst/test/ast/ProgramTests.kt +++ b/compilerAst/test/ast/ProgramTests.kt @@ -50,12 +50,12 @@ class ProgramTests { assertSame(program, m1.program) assertSame(program.namespace, m1.parent) - assertFailsWith { program.addModule(m1) } - .let { assertThat(it.message, containsString(m1.name)) } + assertThat("module may not occur multiple times", + assertFailsWith { program.addModule(m1) }.message, containsString(m1.name)) val m2 = Module(mutableListOf(), m1.position, m1.source) - assertFailsWith { program.addModule(m2) } - .let { assertThat(it.message, containsString(m2.name)) } + assertThat("other module but with same name may not occur multiple times", + assertFailsWith { program.addModule(m2) }.message, containsString(m2.name)) } }