Skip to content

Commit

Permalink
Support single-quoted strings
Browse files Browse the repository at this point in the history
It's trivial, and makes command lines much simpler on Windows where
the quote character is " (double quote). Thanks @jftuga for the
suggestion. Fixes #5.
  • Loading branch information
benhoyt committed Aug 21, 2018
1 parent 65c6b2c commit 4ac40bb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion interp/interp_test.go
Expand Up @@ -121,6 +121,8 @@ BEGIN {
// TODO: tests for numeric strings from $ fields

// Other expressions: TODO ?: num str regex
{`BEGIN { print '\"' 'x' "y" '\"' }`, "", "\"xy\"\n", "", "syntax error"},

// Built-in variables: TODO
// Field expressions and assignment: TODO
// Assignment expressions: TODO
Expand Down Expand Up @@ -261,7 +263,6 @@ BEGIN { early() }
// {`BEGIN { printf "x" };; BEGIN { printf "y" }`, "", "xy", "", ""},

// Ensure syntax errors result in errors
{`BEGIN { }'`, "", "", `parse error at 1:10: unexpected '\''`, "syntax error"},
// {`{ $1 = substr($1, 1, 3) print $1 }`, "", "", "ERROR", "syntax error"},
}
for _, test := range tests {
Expand Down
7 changes: 5 additions & 2 deletions lexer/lexer.go
Expand Up @@ -248,9 +248,12 @@ func (l *Lexer) scan() (Position, Token, string) {
}
tok = NUMBER
val = string(runes)
case '"':
case '"', '\'':
// Note: POSIX awk spec doesn't allow single-quoted strings,
// but this helps without quoting, especially on Windows
// where the shell quote character is " (double quote).
runes := []rune{}
for l.ch != '"' {
for l.ch != ch {
c := l.ch
if c < 0 {
return l.pos, ILLEGAL, "didn't find end quote in string"
Expand Down
6 changes: 5 additions & 1 deletion lexer/lexer_test.go
Expand Up @@ -29,9 +29,13 @@ func TestLexer(t *testing.T) {

// String tokens
{`"foo"`, `1:1 string "foo"`},
{`"a\t\r\n\zb"`, `1:1 string "a\t\r\nzb"`},
{`"a\t\r\n\z\'\"b"`, `1:1 string "a\t\r\nz'\"b"`},
{`"x`, `1:3 <illegal> "didn't find end quote in string"`},
{"\"x\n\"", `1:3 <illegal> "can't have newline in string", 1:3 <newline> "", 2:2 <illegal> "didn't find end quote in string"`},
{`'foo'`, `1:1 string "foo"`},
{`'a\t\r\n\z\'\"b'`, `1:1 string "a\t\r\nz'\"b"`},
{`'x`, `1:3 <illegal> "didn't find end quote in string"`},
{"'x\n'", `1:3 <illegal> "can't have newline in string", 1:3 <newline> "", 2:2 <illegal> "didn't find end quote in string"`},

// Number tokens
{"0", `1:1 number "0"`},
Expand Down

0 comments on commit 4ac40bb

Please sign in to comment.