Skip to content

Commit

Permalink
CHANGE: have % to be a valid word and not an empty file
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Mar 24, 2021
1 parent 87abe3f commit aece05f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/core/l-scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,13 @@
case LEX_SPECIAL_AT: /* @username */
return TOKEN_REF;

case LEX_SPECIAL_PERCENT: /* %filename */
case LEX_SPECIAL_PERCENT:
if (IS_LEX_DELIMIT(cp[1]) && cp[1] != '"' && cp[1] != '/') {
return TOKEN_WORD; // special case for having % as a word (reminder op!)
} else if (cp[1] == ':' && IS_LEX_DELIMIT(cp[2])) {
return TOKEN_SET;
}
/* %filename */
cp = scan_state->end;
if (*cp == '"') {
cp = Scan_Quote(cp, scan_state); // stores result string in BUF_MOLD
Expand All @@ -901,6 +907,13 @@
scan_state->end = cp+1;
return TOKEN_GET;
}
if (cp[1] == '%' && IS_LEX_DELIMIT(cp[2])) {
if (cp[2] == '"' || cp[2] == '/') { // no :%"" or :%/
scan_state->end = cp + 3;
return -TOKEN_GET;
}
return TOKEN_GET; // allowed :%
}
type = TOKEN_GET;
cp++; /* skip ':' */
goto scanword;
Expand All @@ -919,6 +932,13 @@
scan_state->end = cp+1;
return TOKEN_LIT;
}
if (cp[1] == '%' && IS_LEX_DELIMIT(cp[2])) {
if (cp[2] == '"' || cp[2] == '/') { // no '%"" or '%/
scan_state->end = cp + 3;
return -TOKEN_LIT;
}
return TOKEN_LIT; // allowed '%
}
}
if (cp[1] == '\'') return -TOKEN_LIT; // no ''foo
type = TOKEN_LIT;
Expand Down
30 changes: 30 additions & 0 deletions src/tests/units/lexer-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ Rebol [

===end-group===

===start-group=== "Special % word"
--test-- "valid % word cases"
--assert word? try [load {%}]
--assert word? try [load {% }]
--assert word? try [load {%^-}]
--assert word? try [first load {[%]}]
--test-- "valid % lit-word cases"
--assert lit-word? try [load {'%}]
--assert lit-word? try [load {'% }]
--assert lit-word? try [load {'%^-}]
--assert lit-word? try [first load {['%]}]
--test-- "valid % get-word cases"
--assert get-word? try [load {:%}]
--assert get-word? try [load {:% }]
--assert get-word? try [load {:%^-}]
--assert get-word? try [first load {[:%]}]
--test-- "invalid % lit-word cases"
--assert all [error? e: try [load {'%""}] e/id = 'invalid e/arg1 = "word-lit"]
--assert all [error? e: try [load {'%/}] e/id = 'invalid e/arg1 = "word-lit"]
--test-- "invalid % get-word cases"
--assert all [error? e: try [load {:%""}] e/id = 'invalid e/arg1 = "word-get"]
--assert all [error? e: try [load {:%/}] e/id = 'invalid e/arg1 = "word-get"]
--test-- "% used in object"
--assert all [
not error? try [o: make object! [%: 1]]
1 = o/%
]

===end-group===

===start-group=== "Email"
--test-- "valid `emails`"
--assert email? load {name@where}
Expand Down

0 comments on commit aece05f

Please sign in to comment.