From 367796028dc0e00c7de512968e147c99fcb8f8c1 Mon Sep 17 00:00:00 2001 From: Michael Forney Date: Wed, 8 May 2019 16:05:04 -0700 Subject: scan: Handle EOF in comments, character constants, and string literals --- scan.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/scan.c b/scan.c index f5ad2f9..489b599 100644 --- a/scan.c +++ b/scan.c @@ -207,6 +207,8 @@ charconst(struct scanner *s) return TCHARCONST; case '\n': error(&s->loc, "newline in character constant"); + case EOF: + error(&s->loc, "EOF in character constant"); default: nextchar(s); break; @@ -229,6 +231,8 @@ stringlit(struct scanner *s) return TSTRINGLIT; case '\n': error(&s->loc, "newline in string literal"); + case EOF: + error(&s->loc, "EOF in string literal"); default: nextchar(s); break; @@ -244,12 +248,16 @@ comment(struct scanner *s) switch (s->chr) { case '/': /* C++-style comment */ do nextchar(s); - while (s->chr != '\n'); + while (s->chr != '\n' && s->chr != EOF); return true; case '*': /* C-style comment */ nextchar(s); - do last = s->chr, nextchar(s); - while (last != '*' || s->chr != '/'); + do { + last = s->chr; + nextchar(s); + if (s->chr == EOF) + error(&s->loc, "EOF in comment"); + } while (last != '*' || s->chr != '/'); nextchar(s); return true; default: -- cgit v1.2.3