Commit f95a42e6 authored by Austin Clements's avatar Austin Clements

Produce friendlier errors messages for malformed character

literals and when the parser hits an unexpected EOF.  Also,
disallow newlines in character literals.

R=gri
APPROVED=gri
DELTA=23  (15 added, 1 deleted, 7 changed)
OCL=31790
CL=31797
parent 35e5906f
...@@ -96,6 +96,7 @@ func (S *Scanner) Init(filename string, src []byte, err ErrorHandler, mode uint) ...@@ -96,6 +96,7 @@ func (S *Scanner) Init(filename string, src []byte, err ErrorHandler, mode uint)
func charString(ch int) string { func charString(ch int) string {
var s string; var s string;
switch ch { switch ch {
case -1: return `EOF`;
case '\a': s = `\a`; case '\a': s = `\a`;
case '\b': s = `\b`; case '\b': s = `\b`;
case '\f': s = `\f`; case '\f': s = `\f`;
...@@ -306,16 +307,29 @@ func (S *Scanner) scanEscape(quote int) { ...@@ -306,16 +307,29 @@ func (S *Scanner) scanEscape(quote int) {
} }
func (S *Scanner) scanChar() { func (S *Scanner) scanChar(pos token.Position) {
// '\'' already consumed // '\'' already consumed
ch := S.ch; n := 0;
S.next(); for S.ch != '\'' {
if ch == '\\' { ch := S.ch;
S.scanEscape('\''); n++;
S.next();
if ch == '\n' || ch < 0 {
S.error(pos, "character literal not terminated");
n = 1;
break;
}
if ch == '\\' {
S.scanEscape('\'');
}
} }
S.expect('\''); S.next();
if n != 1 {
S.error(pos, "illegal character literal");
}
} }
...@@ -431,7 +445,7 @@ scan_again: ...@@ -431,7 +445,7 @@ scan_again:
switch ch { switch ch {
case -1 : tok = token.EOF; case -1 : tok = token.EOF;
case '"' : tok = token.STRING; S.scanString(pos); case '"' : tok = token.STRING; S.scanString(pos);
case '\'': tok = token.CHAR; S.scanChar(); case '\'': tok = token.CHAR; S.scanChar(pos);
case '`' : tok = token.STRING; S.scanRawString(pos); case '`' : tok = token.STRING; S.scanRawString(pos);
case ':' : tok = S.switch2(token.COLON, token.DEFINE); case ':' : tok = S.switch2(token.COLON, token.DEFINE);
case '.' : case '.' :
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment