pastojs: errors on illegal char const

This commit is contained in:
mattias 2023-10-17 13:02:45 +02:00
parent f477b94f0f
commit c7db379a4f
2 changed files with 74 additions and 48 deletions

View File

@ -217,6 +217,7 @@ const
nAwaitWithoutPromise = 3144; nAwaitWithoutPromise = 3144;
nSymbolCannotBeExportedFromALibrary = 3145; nSymbolCannotBeExportedFromALibrary = 3145;
nForLoopControlVarMustBeSimpleLocalVar = 3146; nForLoopControlVarMustBeSimpleLocalVar = 3146;
nIllegalCharConst = 3147;
// using same IDs as FPC // using same IDs as FPC
nVirtualMethodXHasLowerVisibility = 3250; // was 3050 nVirtualMethodXHasLowerVisibility = 3250; // was 3050
@ -374,6 +375,7 @@ resourcestring
sAwaitWithoutPromise = 'Await without promise'; sAwaitWithoutPromise = 'Await without promise';
sSymbolCannotBeExportedFromALibrary = 'The symbol cannot be exported from a library'; sSymbolCannotBeExportedFromALibrary = 'The symbol cannot be exported from a library';
sForLoopControlVarMustBeSimpleLocalVar = 'For loop control variable must be simple local variable'; sForLoopControlVarMustBeSimpleLocalVar = 'For loop control variable must be simple local variable';
sIllegalCharConst = 'Illegal char constant';
type type
{ TResolveData - base class for data stored in TPasElement.CustomData } { TResolveData - base class for data stored in TPasElement.CustomData }

View File

@ -6750,8 +6750,65 @@ function TPas2JSResolver.ExtractPasStringLiteral(El: TPasElement;
Note that invalid UTF-8 sequences are checked by the scanner Note that invalid UTF-8 sequences are checked by the scanner
} }
var var
p, StartP, i, l: integer; p, StartP, l: integer;
procedure Err(id: TMaxPrecInt);
begin
RaiseMsg(id,nIllegalCharConst,sIllegalCharConst,[],El);
end;
function ReadNumber: integer;
var
c: AnsiChar;
begin
Result:=0;
inc(p);
if p>l then
Err(20170207155121);
if S[p]='$' then
begin
// #$hexnumber
inc(p);
StartP:=p;
while p<=l do
begin
c:=S[p];
case c of
'0'..'9': Result:=Result*16+ord(c)-ord('0');
'a'..'f': Result:=Result*16+ord(c)-ord('a')+10;
'A'..'F': Result:=Result*16+ord(c)-ord('A')+10;
else break;
end;
if Result>$10ffff then
RaiseNotYetImplemented(20170207164657,El,'maximum codepoint is $10ffff');
inc(p);
end;
if p=StartP then
Err(20170207164956);
end
else
begin
// #decimalnumber
StartP:=p;
while p<=l do
begin
c:=S[p];
case c of
'0'..'9': Result:=Result*10+ord(c)-ord('0');
else break;
end;
if Result>$10ffff then
Err(20170207171140);
inc(p);
end;
if p=StartP then
Err(20170207171148);
end;
end;
var
c: AnsiChar; c: AnsiChar;
i, j: Integer;
begin begin
Result:=''; Result:='';
{$IFDEF VerbosePas2JS} {$IFDEF VerbosePas2JS}
@ -6769,7 +6826,7 @@ begin
StartP:=p; StartP:=p;
repeat repeat
if p>l then if p>l then
RaiseInternalError(20170207155120); Err(20170207155120);
c:=S[p]; c:=S[p];
case c of case c of
'''': '''':
@ -6793,69 +6850,36 @@ begin
end; end;
'#': '#':
begin begin
// word sequence // number
inc(p); i:=ReadNumber;
if p>l then if (i>=$D800) and (i<=$DFFF) and (p<l) and (S[p]='#') then
RaiseInternalError(20170207155121);
if S[p]='$' then
begin begin
// #$hexnumber // surrogate
inc(p); j:=ReadNumber;
StartP:=p; if (j>=$DC00) and (j<$DFFF) then
i:=0; Result:=Result+CodePointToJSString((i and $3FF) shl 10 + (j and $3ff) + $10000)
while p<=l do else
begin Err(20231017120034);
c:=S[p];
case c of
'0'..'9': i:=i*16+ord(c)-ord('0');
'a'..'f': i:=i*16+ord(c)-ord('a')+10;
'A'..'F': i:=i*16+ord(c)-ord('A')+10;
else break;
end;
if i>$10ffff then
RaiseNotYetImplemented(20170207164657,El,'maximum codepoint is $10ffff');
inc(p);
end;
if p=StartP then
RaiseInternalError(20170207164956);
end end
else else
begin Result:=Result+CodePointToJSString(i);
// #decimalnumber
StartP:=p;
i:=0;
while p<=l do
begin
c:=S[p];
case c of
'0'..'9': i:=i*10+ord(c)-ord('0');
else break;
end;
if i>$10ffff then
RaiseNotYetImplemented(20170207171140,El,'maximum codepoint is $10ffff');
inc(p);
end;
if p=StartP then
RaiseInternalError(20170207171148);
end;
Result:=Result+CodePointToJSString(i);
end; end;
'^': '^':
begin begin
// ^A is #1 // ^A is #1
inc(p); inc(p);
if p>l then if p>l then
RaiseInternalError(20181025125920); Err(20181025125920);
c:=S[p]; c:=S[p];
case c of case c of
'a'..'z': Result:=Result+TJSChar(ord(c)-ord('a')+1); 'a'..'z': Result:=Result+TJSChar(ord(c)-ord('a')+1);
'A'..'Z': Result:=Result+TJSChar(ord(c)-ord('A')+1); 'A'..'Z': Result:=Result+TJSChar(ord(c)-ord('A')+1);
else RaiseInternalError(20170207160412); else Err(20170207160412);
end; end;
inc(p); inc(p);
end; end;
else else
RaiseNotYetImplemented(20170207154653,El,'ord='+IntToStr(ord(S[p]))); Err(20170207154653);
end; end;
{$IFDEF VerbosePas2JS} {$IFDEF VerbosePas2JS}
{AllowWriteln} {AllowWriteln}