mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-17 19:29:18 +02:00
* Regex parsing
git-svn-id: trunk@27030 -
This commit is contained in:
parent
2d60f0d576
commit
39efecf5df
@ -6,46 +6,13 @@ unit jsparser;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
Classes, SysUtils, jsscanner, jstree;
|
Classes, SysUtils, jsscanner, jstree, jstoken;
|
||||||
|
|
||||||
Const
|
Const
|
||||||
SEmptyLabel = '';
|
SEmptyLabel = '';
|
||||||
|
|
||||||
Type
|
Type
|
||||||
|
|
||||||
|
|
||||||
{ TJSLabelSet }
|
|
||||||
|
|
||||||
TJSLabelSet = Class(TObject)
|
|
||||||
private
|
|
||||||
FCOnt: Boolean;
|
|
||||||
FNext: TJSLabelSet;
|
|
||||||
FTarget: Cardinal;
|
|
||||||
Public
|
|
||||||
Property Target : Cardinal Read FTarget Write FTarget;
|
|
||||||
Property Next : TJSLabelSet Read FNext Write FNext; // Linked list
|
|
||||||
Property Continuable : Boolean Read FCOnt Write FCont;
|
|
||||||
end;
|
|
||||||
|
|
||||||
{ TJSLabel }
|
|
||||||
|
|
||||||
TJSLabel = Class(TObject)
|
|
||||||
private
|
|
||||||
FLabelSet: TJSLabelSet;
|
|
||||||
FLocationLine: Integer;
|
|
||||||
FLocationPos: Integer;
|
|
||||||
FLocationSource: String;
|
|
||||||
FName: String;
|
|
||||||
FNext: TJSLabel;
|
|
||||||
Public
|
|
||||||
Property Name : String Read FName Write FName;
|
|
||||||
Property LabelSet : TJSLabelSet Read FLabelSet Write FLabelSet;
|
|
||||||
Property LocationSource : String Read FLocationSource Write FLocationSource;
|
|
||||||
Property LocationLine : Integer Read FLocationLine Write FLocationLine;
|
|
||||||
Property LocationPos : Integer Read FLocationPos Write FLocationPos;
|
|
||||||
Property Next : TJSLabel Read FNext Write FNext;
|
|
||||||
end;
|
|
||||||
|
|
||||||
{ TJSParser }
|
{ TJSParser }
|
||||||
|
|
||||||
TJSParser = Class(TObject)
|
TJSParser = Class(TObject)
|
||||||
@ -601,7 +568,7 @@ begin
|
|||||||
begin
|
begin
|
||||||
While CurrentToken=tjsComma do
|
While CurrentToken=tjsComma do
|
||||||
GetNextToken;
|
GetNextToken;
|
||||||
If (CurrentToken in [tjsIdentifier,jsscanner.tjsString,tjsnumber]) then
|
If (CurrentToken in [tjsIdentifier,jstoken.tjsString,tjsnumber]) then
|
||||||
begin
|
begin
|
||||||
E:=N.Elements.AddElement;
|
E:=N.Elements.AddElement;
|
||||||
E.Name:=CurrenttokenString;
|
E.Name:=CurrenttokenString;
|
||||||
@ -724,7 +691,7 @@ begin
|
|||||||
GetNextToken;
|
GetNextToken;
|
||||||
end;
|
end;
|
||||||
tjsNumber : Result:=ParseNumericLiteral;
|
tjsNumber : Result:=ParseNumericLiteral;
|
||||||
jsscanner.tjsString : Result:=ParseStringLiteral;
|
jstoken.tjsString : Result:=ParseStringLiteral;
|
||||||
tjsDiv,
|
tjsDiv,
|
||||||
tjsDivEq : Result:=ParseRegularExpressionLiteral
|
tjsDivEq : Result:=ParseRegularExpressionLiteral
|
||||||
else
|
else
|
||||||
@ -777,6 +744,7 @@ function TJSParser.ParseMemberExpression: TJSElement;
|
|||||||
|
|
||||||
Var
|
Var
|
||||||
M : TJSDotMemberExpression;
|
M : TJSDotMemberExpression;
|
||||||
|
N : TJSNewMemberExpression;
|
||||||
B : TJSBracketMemberExpression;
|
B : TJSBracketMemberExpression;
|
||||||
C : TJSCallExpression;
|
C : TJSCallExpression;
|
||||||
Done : Boolean;
|
Done : Boolean;
|
||||||
@ -787,7 +755,16 @@ begin
|
|||||||
tjsFunction : Result:=ParseFunctionExpression();
|
tjsFunction : Result:=ParseFunctionExpression();
|
||||||
tjsNew : begin
|
tjsNew : begin
|
||||||
GetNextToken;
|
GetNextToken;
|
||||||
Result:=ParseMemberExpression();
|
N:=TJSNewMemberExpression(CreateElement(TJSNewMemberExpression));
|
||||||
|
try
|
||||||
|
Result:=N;
|
||||||
|
N.Mexpr:=ParseMemberExpression();
|
||||||
|
if (CurrentToken=tjsBraceOpen) then
|
||||||
|
N.Args:=ParseArguments;
|
||||||
|
except
|
||||||
|
FreeAndNil(N);
|
||||||
|
Raise;
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
else
|
else
|
||||||
Result:=ParsePrimaryExpression()
|
Result:=ParsePrimaryExpression()
|
||||||
@ -1481,7 +1458,7 @@ begin
|
|||||||
tjsDo :
|
tjsDo :
|
||||||
begin
|
begin
|
||||||
GetNextToken;
|
GetNextToken;
|
||||||
W:=TJSWhileStatement(CreateElement(TJSWhileStatement));
|
W:=TJSDoWhileStatement(CreateElement(TJSDoWhileStatement));
|
||||||
Result:=W;
|
Result:=W;
|
||||||
W.Body:=ParseStatement;
|
W.Body:=ParseStatement;
|
||||||
Consume(tjsWhile);
|
Consume(tjsWhile);
|
||||||
@ -1604,6 +1581,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
Consume(tjsSemicolon,True);
|
Consume(tjsSemicolon,True);
|
||||||
C.Target:=L.Labelset.Target;
|
C.Target:=L.Labelset.Target;
|
||||||
|
C.TargetName:=L.Name;
|
||||||
except
|
except
|
||||||
FreeAndNil(C);
|
FreeAndNil(C);
|
||||||
Raise;
|
Raise;
|
||||||
@ -1631,6 +1609,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
Consume(tjsSemicolon,True);
|
Consume(tjsSemicolon,True);
|
||||||
B.Target:=L.Labelset.Target;
|
B.Target:=L.Labelset.Target;
|
||||||
|
B.TargetName:=L.Name;
|
||||||
except
|
except
|
||||||
FreeAndNil(B);
|
FreeAndNil(B);
|
||||||
Raise;
|
Raise;
|
||||||
@ -1722,7 +1701,9 @@ begin
|
|||||||
Consume(tjsCurlyBraceClose);
|
Consume(tjsCurlyBraceClose);
|
||||||
finally
|
finally
|
||||||
LeaveLabel;
|
LeaveLabel;
|
||||||
|
FreeCurrentLabelSet;
|
||||||
end;
|
end;
|
||||||
|
Result:=N;
|
||||||
except
|
except
|
||||||
FreeAndNil(N);
|
FreeAndNil(N);
|
||||||
Raise;
|
Raise;
|
||||||
@ -1753,7 +1734,7 @@ function TJSParser.ParseTryStatement : TJSElement;
|
|||||||
|
|
||||||
Var
|
Var
|
||||||
BO,BC,BF : TJSElement;
|
BO,BC,BF : TJSElement;
|
||||||
Id : TJSString;
|
Id : jstree.TJSString;
|
||||||
T : TJSTryStatement;
|
T : TJSTryStatement;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
@ -1913,7 +1894,7 @@ begin
|
|||||||
FCurrentLabelSet:=Nil;
|
FCurrentLabelSet:=Nil;
|
||||||
LS.target:=CurrentLabelSet.Target;
|
LS.target:=CurrentLabelSet.Target;
|
||||||
Repeat
|
Repeat
|
||||||
EnterLabel(CurrentTokenString);
|
LS.TheLabel:=EnterLabel(CurrentTokenString);
|
||||||
Consume(tjsIdentifier);
|
Consume(tjsIdentifier);
|
||||||
Consume(tjsColon);
|
Consume(tjsColon);
|
||||||
Until (CurrentToken<>tjsIdentifier) or (PeekNextToken<>tjsColon);
|
Until (CurrentToken<>tjsIdentifier) or (PeekNextToken<>tjsColon);
|
||||||
@ -2037,13 +2018,13 @@ function TJSParser.ParseSourceElements : TJSSourceElements;
|
|||||||
|
|
||||||
Const
|
Const
|
||||||
StatementTokens = [tjsNULL, tjsTRUE, tjsFALSE,
|
StatementTokens = [tjsNULL, tjsTRUE, tjsFALSE,
|
||||||
tjsTHIS, tjsIdentifier,jsscanner.tjsSTRING,tjsNUMBER,
|
tjsTHIS, tjsIdentifier,jstoken.tjsSTRING,tjsNUMBER,
|
||||||
tjsBraceOpen,tjsCurlyBraceOpen,tjsSquaredBraceOpen,
|
tjsBraceOpen,tjsCurlyBraceOpen,tjsSquaredBraceOpen,
|
||||||
tjsNew,tjsDelete,tjsVoid,tjsTypeOf,
|
tjsNew,tjsDelete,tjsVoid,tjsTypeOf,
|
||||||
tjsPlusPlus,tjsMinusMinus,
|
tjsPlusPlus,tjsMinusMinus,
|
||||||
tjsPlus,tjsMinus,tjsNot,tjsNE,tjsSNE,tjsSemicolon,
|
tjsPlus,tjsMinus,tjsNot,tjsNE,tjsSNE,tjsSemicolon,
|
||||||
tjsVAR,tjsIF,tjsDO,tjsWHILE,tjsFOR,jsscanner.tjsCONTINUE,jsscanner.tjsBREAK,jsscanner.tjsReturn,
|
tjsVAR,tjsIF,tjsDO,tjsWHILE,tjsFOR,jstoken.tjsCONTINUE,jstoken.tjsBREAK,jstoken.tjsReturn,
|
||||||
tjsWith,jsscanner.tjsSWITCH,tjsThrow,TjsTry,tjsDIV,tjsDIVEQ];
|
tjsWith,jstoken.tjsSWITCH,tjsThrow,TjsTry,tjsDIV,tjsDIVEQ];
|
||||||
|
|
||||||
Var
|
Var
|
||||||
F : TJSFunctionDeclarationStatement;
|
F : TJSFunctionDeclarationStatement;
|
||||||
@ -2060,7 +2041,7 @@ begin
|
|||||||
FCurrentVars:=Result.Vars;
|
FCurrentVars:=Result.Vars;
|
||||||
Repeat
|
Repeat
|
||||||
{$ifdef debugparser} Writeln('Sourceelements start:',GetEnumName(TypeInfo(TJSToken),Ord(CurrentToken)), ' As string: ',CurrentTokenString);{$endif debugparser}
|
{$ifdef debugparser} Writeln('Sourceelements start:',GetEnumName(TypeInfo(TJSToken),Ord(CurrentToken)), ' As string: ',CurrentTokenString);{$endif debugparser}
|
||||||
If (CurrentToken=jsscanner.tjsFunction) then
|
If (CurrentToken=jstoken.tjsFunction) then
|
||||||
begin
|
begin
|
||||||
If (PeekNextToken<>tjsBraceOpen) then
|
If (PeekNextToken<>tjsBraceOpen) then
|
||||||
begin
|
begin
|
||||||
|
Loading…
Reference in New Issue
Block a user