From fa96fb0a8e74e334453c1c73b68b9b79ffd03e60 Mon Sep 17 00:00:00 2001 From: mauvm Date: Sat, 3 Jan 2015 20:53:12 +0100 Subject: [PATCH 1/5] Let compiler ignore shebang on first line. --- src/wren_compiler.c | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/wren_compiler.c b/src/wren_compiler.c index bbc73134..1803a7b6 100644 --- a/src/wren_compiler.c +++ b/src/wren_compiler.c @@ -682,6 +682,24 @@ static void readString(Parser* parser) makeToken(parser, TOKEN_STRING); } +// Default behaviour for unmatched characters. +static void unmatchedChar(Parser* parser, char c) +{ + // Handle unmatched character + if (isName(c)) + { + readName(parser, TOKEN_NAME); + } + else if (isDigit(c)) + { + readNumber(parser); + } + else + { + lexError(parser, "Invalid character '%c'.", c); + } +} + // Lex the next token and store it in [parser.current]. static void nextToken(Parser* parser) { @@ -800,19 +818,19 @@ static void nextToken(Parser* parser) peekChar(parser) == '_' ? TOKEN_STATIC_FIELD : TOKEN_FIELD); return; + case '#': + // Ignore shebang on the first line. + if (peekChar(parser) == '!' && parser->currentLine == 1) + { + skipLineComment(parser); + break; + } + + unmatchedChar(parser, c); + return; + default: - if (isName(c)) - { - readName(parser, TOKEN_NAME); - } - else if (isDigit(c)) - { - readNumber(parser); - } - else - { - lexError(parser, "Invalid character '%c'.", c); - } + unmatchedChar(parser, c); return; } } From 8a54ef9919c92459731e117b4cc24855c5aee13f Mon Sep 17 00:00:00 2001 From: mauvm Date: Sat, 3 Jan 2015 20:53:17 +0100 Subject: [PATCH 2/5] Add tests. --- test/shebang/shebang.wren | 2 ++ test/shebang/shebang_at_eof.wren | 1 + test/shebang/shebang_at_other_line.wren | 3 +++ 3 files changed, 6 insertions(+) create mode 100644 test/shebang/shebang.wren create mode 100644 test/shebang/shebang_at_eof.wren create mode 100644 test/shebang/shebang_at_other_line.wren diff --git a/test/shebang/shebang.wren b/test/shebang/shebang.wren new file mode 100644 index 00000000..83f35c0a --- /dev/null +++ b/test/shebang/shebang.wren @@ -0,0 +1,2 @@ +#!/bin/wren +IO.print("ok") // expect: ok diff --git a/test/shebang/shebang_at_eof.wren b/test/shebang/shebang_at_eof.wren new file mode 100644 index 00000000..0eabdbab --- /dev/null +++ b/test/shebang/shebang_at_eof.wren @@ -0,0 +1 @@ +#!/bin/wren \ No newline at end of file diff --git a/test/shebang/shebang_at_other_line.wren b/test/shebang/shebang_at_other_line.wren new file mode 100644 index 00000000..4fa3ac95 --- /dev/null +++ b/test/shebang/shebang_at_other_line.wren @@ -0,0 +1,3 @@ +// expect error line 3 +IO.print("nope") +#!/bin/wren \ No newline at end of file From dfb381e43baf5be4cd17bdda8d7cf024eb2072fc Mon Sep 17 00:00:00 2001 From: mauvm Date: Sun, 4 Jan 2015 08:52:40 +0100 Subject: [PATCH 3/5] Remove duplicate comment. Oops. --- src/wren_compiler.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wren_compiler.c b/src/wren_compiler.c index 1803a7b6..64f6f42c 100644 --- a/src/wren_compiler.c +++ b/src/wren_compiler.c @@ -685,7 +685,6 @@ static void readString(Parser* parser) // Default behaviour for unmatched characters. static void unmatchedChar(Parser* parser, char c) { - // Handle unmatched character if (isName(c)) { readName(parser, TOKEN_NAME); From 6c549e0cc5afdfa8df522751852bd6ebbac3e80b Mon Sep 17 00:00:00 2001 From: mauvm Date: Sun, 4 Jan 2015 19:56:16 +0100 Subject: [PATCH 4/5] Use lexError() and add test for invalid shebang. --- src/wren_compiler.c | 32 +++++++++------------- test/shebang/shebang_with_exclamation.wren | 3 ++ 2 files changed, 16 insertions(+), 19 deletions(-) create mode 100644 test/shebang/shebang_with_exclamation.wren diff --git a/src/wren_compiler.c b/src/wren_compiler.c index 64f6f42c..8782e55a 100644 --- a/src/wren_compiler.c +++ b/src/wren_compiler.c @@ -682,23 +682,6 @@ static void readString(Parser* parser) makeToken(parser, TOKEN_STRING); } -// Default behaviour for unmatched characters. -static void unmatchedChar(Parser* parser, char c) -{ - if (isName(c)) - { - readName(parser, TOKEN_NAME); - } - else if (isDigit(c)) - { - readNumber(parser); - } - else - { - lexError(parser, "Invalid character '%c'.", c); - } -} - // Lex the next token and store it in [parser.current]. static void nextToken(Parser* parser) { @@ -825,11 +808,22 @@ static void nextToken(Parser* parser) break; } - unmatchedChar(parser, c); + lexError(parser, "Invalid character '%c'.", c); return; default: - unmatchedChar(parser, c); + if (isName(c)) + { + readName(parser, TOKEN_NAME); + } + else if (isDigit(c)) + { + readNumber(parser); + } + else + { + lexError(parser, "Invalid character '%c'.", c); + } return; } } diff --git a/test/shebang/shebang_with_exclamation.wren b/test/shebang/shebang_with_exclamation.wren new file mode 100644 index 00000000..828c4fa0 --- /dev/null +++ b/test/shebang/shebang_with_exclamation.wren @@ -0,0 +1,3 @@ +#/invalid/shebang +// expect error line 1 +IO.print("nope") From 4200f18f83283900767a94d2b3b7573cb25e8833 Mon Sep 17 00:00:00 2001 From: mauvm Date: Sun, 4 Jan 2015 20:09:33 +0100 Subject: [PATCH 5/5] Rename test for invalid shebang. --- .../{shebang_with_exclamation.wren => shebang_invalid.wren} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/shebang/{shebang_with_exclamation.wren => shebang_invalid.wren} (100%) diff --git a/test/shebang/shebang_with_exclamation.wren b/test/shebang/shebang_invalid.wren similarity index 100% rename from test/shebang/shebang_with_exclamation.wren rename to test/shebang/shebang_invalid.wren