Skip to content

Commit

Permalink
kernel: catch some invalid uses of '~'; add tests
Browse files Browse the repository at this point in the history
Also, print a (more) helpful error message.

Before:

    gap> ~;
    Error, '~' does not have a value here
    not in any function at *stdin*:1
    gap> x -> ~;
    function( x ) ... end
    gap> x -> (1,~);
    function( x ) ... end
    gap> (1,~);
    Error, '~' does not have a value here
    not in any function at *stdin*:4

After:

    gap> ~;
    Syntax error: '~' not allowed here
    ~;
    ^
    gap> x -> ~;
    Syntax error: '~' not allowed here
    x -> ~;
         ^
    gap> x -> (1,~);
    Syntax error: '~' not allowed here
    x -> (1,~);
            ^
    gap> (1,~);
    Syntax error: '~' not allowed here
    (1,~);
       ^
  • Loading branch information
fingolfin committed Aug 27, 2018
1 parent 94242b8 commit 9ee6cbc
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ static void ReadFuncExprBody(
// push the new local variables list
PushPlist(STATE(StackNams), args.nams);

// begin interpreting the function expression (with 1 argument)
// begin interpreting the function expression
TRY_IF_NO_ERROR {
IntrFuncExprBegin(args.isvarg ? -args.narg : args.narg, nloc,
args.nams, startLine);
Expand Down Expand Up @@ -1480,6 +1480,9 @@ static void ReadLiteral (

/* '~' */
case S_TILDE:
if (ReaderState()->ReadTop == 0) {
SyntaxError("'~' not allowed here");
}
ReaderState()->ReadTilde = 1;
TRY_IF_NO_ERROR { IntrTildeExpr(); }
Match( S_TILDE, "~", follow );
Expand Down
73 changes: 73 additions & 0 deletions tst/testinstall/tilde.tst
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
gap> START_TEST("tilde.tst");

#
gap> aqq~ := 1;
Error, Variable: 'aqq' must have a value
Syntax error: ; expected in stream:1
aqq~ := 1;
^

#
gap> l := [2, ~];
[ 2, ~ ]
gap> l = l[2];
Expand All @@ -18,6 +22,71 @@ gap> r.x;
rec( x := ~, y := [ 1, 2, ~ ] )
gap> r.y[3];
rec( x := ~, y := [ 1, 2, ~ ] )

#
gap> [ ~ ];
[ ~ ]
gap> [ 1, 2, [0 .. Length(~)] ];
[ 1, 2, [ 0 .. 2 ] ]
gap> [0, 1 + ~, 2 ];
[ 0, [ 1 ], 2 ]

#
gap> [ (x->~)(1) ];
[ ~ ]
gap> l := [ x->~ ]; # this function escapes with an invalid tilde reference
[ function( x ) ... end ]
gap> f := l[1];;
gap> f(1);
Error, '~' does not have a value here
gap> [ f(1) ];
[ ~ ]

#
gap> ~;
Syntax error: '~' not allowed here in stream:1
~;
^
gap> (1,~);
Syntax error: '~' not allowed here in stream:1
(1,~);
^
gap> x->~;
Syntax error: '~' not allowed here in stream:1
x->~;
^
gap> x -> (1,~);
Syntax error: '~' not allowed here in stream:1
x -> (1,~);
^

#
gap> [1..~];
Syntax error: Sorry, '~' not allowed in range in stream:1
[1..~];
^
gap> [~..1];
Syntax error: Sorry, '~' not allowed in range in stream:1
[~..1];
^
gap> [1,~..5];
Syntax error: Sorry, '~' not allowed in range in stream:1
[1,~..5];
^
gap> x->[1..~];
Syntax error: Sorry, '~' not allowed in range in stream:1
x->[1..~];
^
gap> x->[~..1];
Syntax error: Sorry, '~' not allowed in range in stream:1
x->[~..1];
^
gap> x->[1,~..5];
Syntax error: Sorry, '~' not allowed in range in stream:1
x->[1,~..5];
^

#
gap> f := function(~) local a; end;
Syntax error: identifier expected in stream:1
f := function(~) local a; end;
Expand All @@ -42,6 +111,8 @@ gap> {~,~} -> 2;
Syntax error: identifier expected in stream:1
{~,~} -> 2;
^

#
gap> list1 := [1,~];
[ 1, ~ ]
gap> list2 := [1,[1,[1,[1,0]]]];
Expand Down Expand Up @@ -96,4 +167,6 @@ gap> [2,rem(~),3,4,rem(~),5,6,rem(~)];
[ , 1, 3,, 1, 5,, 1 ]
gap> (function() return [2,rem(~),3,4,rem(~),5,6,rem(~)]; end)();
[ , 1, 3,, 1, 5,, 1 ]

#
gap> STOP_TEST( "tilde.tst", 1);

0 comments on commit 9ee6cbc

Please sign in to comment.