Skip to content

Commit

Permalink
now not allow ${34 }, ${3.14} notation. require space after ${. close #…
Browse files Browse the repository at this point in the history
  • Loading branch information
sekiguchi-nagisa committed Dec 5, 2023
1 parent 30da6a2 commit 8bc3f46
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#### Core

- **Breaking Change**: remove ``name`` parameter from ``CLI`` attribute
- **Breaking Change**: need spaces between `${` and number
- due to suppress potential syntax ambiguity
- now ``${345 }``, ``${3.14}`` notations are syntax error
- add ``toplevel`` parameter to ``CLI`` attribute
- in io redirection, allow file descriptor number greater than 4 (up to 9)
- now support like the following bash idiom
Expand Down
13 changes: 13 additions & 0 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2325,7 +2325,20 @@ std::unique_ptr<Node> Parser::parse_interpolation(EmbedNode::Kind kind) {
auto ctx = this->inIgnorableNLCtx();
unsigned int pos = START_POS();
TRY(this->expect(TokenKind::START_INTERP));
bool mayNeedSpace = false;
const Token oldToken = this->curToken;
const TokenKind oldKind = this->curKind;
if (oldKind == TokenKind::INT_LITERAL || oldKind == TokenKind::FLOAT_LITERAL) {
if (!this->hasSpace() && !this->hasNewline()) {
mayNeedSpace = true;
}
}
auto node = TRY(this->parse_expression());
if (mayNeedSpace && isa<NumberNode>(*node)) {
this->createError(oldKind, oldToken, START_INTERP_NUM_NEED_SPACE,
"require space between `${' and number");
return nullptr;
}
auto endToken = TRY(this->expect(TokenKind::RBC));
return std::make_unique<EmbedNode>(pos, kind, std::move(node), endToken);
}
Expand Down
1 change: 1 addition & 0 deletions src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Parser : public ydsh::ParserBase<TokenKind, Lexer, TokenTracker> {
static constexpr const char *REDIR_NEED_SPACE = "RedirNeedSpace";
static constexpr const char *INVALID_HERE_START = "InvalidHereStart";
static constexpr const char *HERE_START_NEED_SPACE = "HereStartNeedSpace";
static constexpr const char *START_INTERP_NUM_NEED_SPACE = "InterpNumNeedSpace";

ObserverPtr<CodeCompletionContext> compCtx;

Expand Down
2 changes: 1 addition & 1 deletion test/cmdline/cmdline_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ TEST_F(CmdlineTest, execPath) {

TEST_F(CmdlineTest, locale) {
{
auto builder = ds("-c", "echo ${3.14}").addEnv("LANG", "de_DE.UTF-8"); // locale independent
auto builder = ds("-c", "echo ${ 3.14 }").addEnv("LANG", "de_DE.UTF-8"); // locale independent
ASSERT_NO_FATAL_FAILURE(this->expect(std::move(builder), 0, "3.140000\n"));
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/exec/cases/base/arg_array.ds
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ $args = @(${0.0/-0.0})
assert $args.size() == 1
assert $args[0] == (0.0/-0.0) as String

$args = @(${3.156})
$args = @(${ 3.156 })
assert $args.size() == 1
assert $args[0] == (3.156) as String

Expand Down
2 changes: 1 addition & 1 deletion test/exec/cases/base/string_interpolation.ds
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ assert("this is true" == "this is ${true}")
assert("this is false" == "this is ${false}")

## Float
assert "${3.14}" == 3.14 as String
assert "${ 3.14 }" == 3.14 as String
assert "${3.14/0.0}" == (3.14/0.0) as String
assert "${-3.1/0.0}" == (-3.1/0.0) as String
assert "${0.0/0.0}" == (0.0/0.0) as String
Expand Down
3 changes: 3 additions & 0 deletions test/exec/cases/syntax/interp1.ds
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#$test($result = 'parse', $lineNum = 3, $chars = 8, $errorKind = 'InterpNumNeedSpace', $status = 1)

echo ${34245 }
8 changes: 8 additions & 0 deletions test/exec/cases/syntax/interp2.ds
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#$test($result = 'parse', $lineNum = 8, $chars = 9, $errorKind = 'InterpNumNeedSpace', $status = 1)

echo ${
23
}
echo ${(52345)}

echo "${34245.0}"
30 changes: 15 additions & 15 deletions test/node/node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,36 +504,36 @@ static constexpr NodeDumpParam paramTable[] = {
resolvedType: null
)"},

{DumpOp::untyped, R"(assert (!ls > 34 | 34 with < ${34.1} &).poll())", 1, 0, R"EOF(
{DumpOp::untyped, R"(assert (!ls > 34 | 34 with < ${ 34.1} &).poll())", 1, 0, R"EOF(
nodes:
- nodeKind: Assert
token:
pos: 0
size: 46
size: 47
type:
condNode:
nodeKind: Apply
token:
pos: 7
size: 39
size: 40
type:
exprNode:
nodeKind: Access
token:
pos: 7
size: 37
size: 38
type:
recvNode:
nodeKind: Fork
token:
pos: 7
size: 32
size: 33
type:
exprNode:
nodeKind: Pipeline
token:
pos: 8
size: 28
size: 29
type:
nodes:
- nodeKind: UnaryOp
Expand Down Expand Up @@ -598,7 +598,7 @@ static constexpr NodeDumpParam paramTable[] = {
- nodeKind: With
token:
pos: 19
size: 17
size: 18
type:
exprNode:
nodeKind: Number
Expand All @@ -613,7 +613,7 @@ static constexpr NodeDumpParam paramTable[] = {
- nodeKind: Redir
token:
pos: 27
size: 9
size: 10
type:
fdName: "0"
newFd: -1
Expand All @@ -622,7 +622,7 @@ static constexpr NodeDumpParam paramTable[] = {
nodeKind: CmdArg
token:
pos: 29
size: 7
size: 8
type:
expansionSize: 0
expansionError: false
Expand All @@ -631,13 +631,13 @@ static constexpr NodeDumpParam paramTable[] = {
- nodeKind: Embed
token:
pos: 29
size: 7
size: 8
type:
kind: "CMD_ARG"
exprNode:
nodeKind: Number
token:
pos: 31
pos: 32
size: 4
type:
kind: "Float"
Expand All @@ -657,15 +657,15 @@ static constexpr NodeDumpParam paramTable[] = {
opKind: "ForkKind::JOB"
nameInfo:
token:
pos: 40
pos: 41
size: 4
name: "poll"
handle: null
additionalOp: "NOP"
argsNode:
nodeKind: Args
token:
pos: 44
pos: 45
size: 2
type:
nodes:
Expand All @@ -676,11 +676,11 @@ static constexpr NodeDumpParam paramTable[] = {
nodeKind: String
token:
pos: 7
size: 39
size: 40
type:
kind: "STRING"
init: true
value: "`(!ls > 34 | 34 with < ${34.1} &).poll()'"
value: "`(!ls > 34 | 34 with < ${ 34.1} &).poll()'"
)EOF"},

{DumpOp::typed, R"(case $SIGINT { $SIGINT => [34:34]; else => (34,)})", 0, 0, R"EOF(
Expand Down

0 comments on commit 8bc3f46

Please sign in to comment.