summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorflywind <xzsflywind@gmail.com>2022-04-25 18:00:23 +0800
committerGitHub <noreply@github.com>2022-04-25 12:00:23 +0200
commit42ac50e988a4734624b204740613c795a0e789a3 (patch)
treeb0d851ea9a5eb05ea7dd7270cf4a1140f21c77fc
parentee0a47029443e6d717ed4a85825b7dcf971dbdf8 (diff)
downloadNim-42ac50e988a4734624b204740613c795a0e789a3.tar.gz
fixes #19662; Indent level errored for first line (#19718)
-rw-r--r--compiler/parser.nim4
-rw-r--r--compiler/passes.nim1
-rw-r--r--compiler/syntaxes.nim2
-rw-r--r--tests/parser/t19662.nim6
4 files changed, 12 insertions, 1 deletions
diff --git a/compiler/parser.nim b/compiler/parser.nim
index 7973f7d37..52466d9fa 100644
--- a/compiler/parser.nim
+++ b/compiler/parser.nim
@@ -2393,6 +2393,10 @@ proc parseAll(p: var Parser): PNode =
     if p.tok.indent != 0:
       parMessage(p, errInvalidIndentation)
 
+proc checkFirstLineIndentation*(p: var Parser) =
+  if p.tok.indent != 0 and p.tok.strongSpaceA > 0:
+    parMessage(p, errInvalidIndentation)
+
 proc parseTopLevelStmt(p: var Parser): PNode =
   ## Implements an iterator which, when called repeatedly, returns the next
   ## top-level statement or emptyNode if end of stream.
diff --git a/compiler/passes.nim b/compiler/passes.nim
index ce52b10c5..7fb2842f5 100644
--- a/compiler/passes.nim
+++ b/compiler/passes.nim
@@ -150,6 +150,7 @@ proc processModule*(graph: ModuleGraph; module: PSym; idgen: IdGenerator;
         processImplicits graph, graph.config.implicitImports, nkImportStmt, a, module
         processImplicits graph, graph.config.implicitIncludes, nkIncludeStmt, a, module
 
+    checkFirstLineIndentation(p)
     while true:
       if graph.stopCompile(): break
       var n = parseTopLevelStmt(p)
diff --git a/compiler/syntaxes.nim b/compiler/syntaxes.nim
index e47b0483d..c00fe8b67 100644
--- a/compiler/syntaxes.nim
+++ b/compiler/syntaxes.nim
@@ -16,7 +16,7 @@ import
 when defined(nimPreviewSlimSystem):
   import std/[syncio, assertions]
 
-export Parser, parseAll, parseTopLevelStmt, closeParser
+export Parser, parseAll, parseTopLevelStmt, checkFirstLineIndentation, closeParser
 
 type
   FilterKind = enum
diff --git a/tests/parser/t19662.nim b/tests/parser/t19662.nim
new file mode 100644
index 000000000..7a1864ac6
--- /dev/null
+++ b/tests/parser/t19662.nim
@@ -0,0 +1,6 @@
+                var i: int # bug #19662
+echo i
+
+discard """
+  errormsg: "invalid indentation"
+"""
\ No newline at end of file