From b4700af2f55e118d23ad359433fbe5631289be5f Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 5 Apr 2021 00:12:04 +0200 Subject: [PATCH] fix windows line ending (\r\n) parse errors --- compilerAst/src/prog8/parser/ModuleParsing.kt | 7 ++++--- parser/antlr/prog8.g4 | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/compilerAst/src/prog8/parser/ModuleParsing.kt b/compilerAst/src/prog8/parser/ModuleParsing.kt index 5099d7cb0..458d9afc6 100644 --- a/compilerAst/src/prog8/parser/ModuleParsing.kt +++ b/compilerAst/src/prog8/parser/ModuleParsing.kt @@ -41,8 +41,8 @@ class ModuleImporter { if(!Files.isReadable(filePath)) throw ParsingFailedError("No such file: $filePath") - val input = CharStreams.fromPath(filePath) - return importModule(program, input, filePath, false, encoder, compilationTargetName) + val content = Files.readAllBytes(filePath).toString(Charsets.UTF_8).replace("\r\n", "\n") + return importModule(program, CharStreams.fromString(content), filePath, false, encoder, compilationTargetName) } fun importLibraryModule(program: Program, name: String, @@ -122,7 +122,8 @@ class ModuleImporter { val (resource, resourcePath) = rsc resource.use { println("importing '$moduleName' (library)") - importModule(program, CharStreams.fromStream(it), Paths.get("@embedded@/$resourcePath"), + val content = it.readAllBytes().toString(Charsets.UTF_8).replace("\r\n", "\n") + importModule(program, CharStreams.fromString(content), Paths.get("@embedded@/$resourcePath"), true, encoder, compilationTargetName) } } else { diff --git a/parser/antlr/prog8.g4 b/parser/antlr/prog8.g4 index c812be199..780dff4db 100644 --- a/parser/antlr/prog8.g4 +++ b/parser/antlr/prog8.g4 @@ -5,6 +5,7 @@ NOTES: - whitespace is ignored. (tabs/spaces) - every position can be empty, be a comment, or contain ONE statement. +- input is assumed to be a text file with UNIX line endings (\n). */ @@ -14,10 +15,10 @@ grammar prog8; package prog8.parser; } -LINECOMMENT : [\r\n][ \t]* COMMENT -> channel(HIDDEN); -COMMENT : ';' ~[\r\n]* -> channel(HIDDEN) ; +LINECOMMENT : [\n][ \t]* COMMENT -> channel(HIDDEN); +COMMENT : ';' ~[\n]* -> channel(HIDDEN) ; WS : [ \t] -> skip ; -EOL : [\r\n]+ ; +EOL : [\n]+ ; // WS2 : '\\' EOL -> skip; VOID: 'void'; NAME : [a-zA-Z][a-zA-Z0-9_]* ;