Browse Source

* Patch from Mattias Gaertner
- jstree: add some explanatory comments
- jswriter:
TJSWriter.WriteSourceElements write functions and vars,
fixed unneeded semicolon in TJSWriter.WriteFuncDef.

git-svn-id: trunk@34217 -

michael 9 years ago
parent
commit
19bd74faa9
2 changed files with 87 additions and 76 deletions
  1. 6 6
      packages/fcl-js/src/jstree.pp
  2. 81 70
      packages/fcl-js/src/jswriter.pp

+ 6 - 6
packages/fcl-js/src/jstree.pp

@@ -422,7 +422,6 @@ Type
   end;
   end;
 
 
 
 
-
   { TJSBinary - base class }
   { TJSBinary - base class }
 
 
   TJSBinary = Class(TJSElement)
   TJSBinary = Class(TJSElement)
@@ -436,7 +435,7 @@ Type
   end;
   end;
   TJSBinaryClass = Class of TJSBinary;
   TJSBinaryClass = Class of TJSBinary;
 
 
-  { TJSStatementList }
+  { TJSStatementList - a list of statements enclosed in curly brackets }
 
 
   TJSStatementList = Class(TJSBinary); // A->first statement, B->next in list, chained.
   TJSStatementList = Class(TJSBinary); // A->first statement, B->next in list, chained.
 
 
@@ -935,7 +934,7 @@ Type
     Property Node : TJSElement Read FNode Write FNode;
     Property Node : TJSElement Read FNode Write FNode;
   end;
   end;
 
 
-  { TJSElementNodes -  }
+  { TJSElementNodes - see TJSSourceElements }
 
 
   TJSElementNodes = Class(TCollection)
   TJSElementNodes = Class(TCollection)
   private
   private
@@ -945,7 +944,8 @@ Type
     Property Nodes[AIndex : Integer] : TJSElementNode Read GetN ; default;
     Property Nodes[AIndex : Integer] : TJSElementNode Read GetN ; default;
   end;
   end;
 
 
-  { TJSSourceElements }
+  { TJSSourceElements - a list of elements, every element ends in semicolon,
+    first Vars, then Functions, finally Statements }
 
 
   TJSSourceElements = Class(TJSElement)
   TJSSourceElements = Class(TJSElement)
   private
   private
@@ -955,9 +955,9 @@ Type
   Public
   Public
     Constructor Create(ALine,ARow : Integer; const ASource : String = ''); override;
     Constructor Create(ALine,ARow : Integer; const ASource : String = ''); override;
     Destructor Destroy; override;
     Destructor Destroy; override;
-    Property Statements : TJSElementNodes Read FStatements;
-    Property Functions : TJSElementNodes Read FFunctions;
     Property Vars : TJSElementNodes Read FVars;
     Property Vars : TJSElementNodes Read FVars;
+    Property Functions : TJSElementNodes Read FFunctions;
+    Property Statements : TJSElementNodes Read FStatements;
   end;
   end;
 
 
 implementation
 implementation

+ 81 - 70
packages/fcl-js/src/jswriter.pp

@@ -127,7 +127,7 @@ Type
     Procedure WriteWithStatement(El: TJSWithStatement);virtual;
     Procedure WriteWithStatement(El: TJSWithStatement);virtual;
     Procedure WriteVarDeclarationList(El: TJSVariableDeclarationList);virtual;
     Procedure WriteVarDeclarationList(El: TJSVariableDeclarationList);virtual;
     Procedure WriteConditionalExpression(El: TJSConditionalExpression);virtual;
     Procedure WriteConditionalExpression(El: TJSConditionalExpression);virtual;
-    Procedure WriteFunctionBody(el: TJSFunctionBody);virtual;
+    Procedure WriteFunctionBody(El: TJSFunctionBody);virtual;
     Procedure WriteFunctionDeclarationStatement(El: TJSFunctionDeclarationStatement);virtual;
     Procedure WriteFunctionDeclarationStatement(El: TJSFunctionDeclarationStatement);virtual;
     Procedure WriteLabeledStatement(El: TJSLabeledStatement);virtual;
     Procedure WriteLabeledStatement(El: TJSLabeledStatement);virtual;
     Procedure WriteReturnStatement(EL: TJSReturnStatement);virtual;
     Procedure WriteReturnStatement(EL: TJSReturnStatement);virtual;
@@ -480,7 +480,10 @@ begin
     begin
     begin
     FSkipBrackets:=True;
     FSkipBrackets:=True;
     WriteJS(FD.Body);
     WriteJS(FD.Body);
-    If not (FD.Body.A is TJSStatementList) then
+    If (Assigned(FD.Body.A))
+    and (not (FD.Body.A is TJSStatementList))
+    and (not (FD.Body.A is TJSSourceElements))
+    then
       if C then
       if C then
         Write('; ')
         Write('; ')
       else
       else
@@ -1039,11 +1042,11 @@ begin
     end;
     end;
 end;
 end;
 
 
-procedure TJSWriter.WriteFunctionBody(el: TJSFunctionBody);
+procedure TJSWriter.WriteFunctionBody(El: TJSFunctionBody);
 
 
 begin
 begin
-  if Assigned(EL.A) then
-    WriteJS(EL.A);
+  if Assigned(El.A) then
+    WriteJS(El.A);
 end;
 end;
 
 
 procedure TJSWriter.WriteFunctionDeclarationStatement(
 procedure TJSWriter.WriteFunctionDeclarationStatement(
@@ -1057,27 +1060,35 @@ end;
 procedure TJSWriter.WriteSourceElements(El: TJSSourceElements);
 procedure TJSWriter.WriteSourceElements(El: TJSSourceElements);
 
 
 Var
 Var
-  I : Integer;
   C : Boolean;
   C : Boolean;
-  E : TJSElement;
+
+  Procedure WriteElements(Elements: TJSElementNodes);
+  Var
+    I : Integer;
+    E : TJSElement;
+  begin
+    if Elements=nil then exit;
+    For I:=0 to Elements.Count-1 do
+      begin
+      E:=Elements.Nodes[i].Node;
+      WriteJS(E);
+      if Not C then
+        WriteLn(';')
+      else
+        if I<Elements.Count-1 then
+          Write('; ')
+        else
+          Write(';')
+      end;
+  end;
 
 
 begin
 begin
   C:=(woCompact in Options);
   C:=(woCompact in Options);
-  For I:=0 to EL.Statements.Count-1 do
-    begin
-    E:=EL.Statements.Nodes[i].Node;
-    WriteJS(E);
-    if Not C then
-      WriteLn(';')
-    else
-      if I<EL.Statements.Count-1 then
-        Write('; ')
-      else
-        Write(';')
-    end;
+  WriteElements(El.Vars);
+  WriteElements(El.Functions);
+  WriteElements(El.Statements);
 end;
 end;
 
 
-
 procedure TJSWriter.WriteVariableStatement(el: TJSVariableStatement);
 procedure TJSWriter.WriteVariableStatement(el: TJSVariableStatement);
 
 
 begin
 begin
@@ -1094,62 +1105,62 @@ begin
     system.Writeln('WriteJS : El = Nil');
     system.Writeln('WriteJS : El = Nil');
 {$ENDIF}
 {$ENDIF}
   if (El is TJSEmptyBlockStatement ) then
   if (El is TJSEmptyBlockStatement ) then
-    WriteEmptyBlockStatement(TJSEmptyBlockStatement(el))
+    WriteEmptyBlockStatement(TJSEmptyBlockStatement(El))
   else if (El is TJSEmptyStatement) then
   else if (El is TJSEmptyStatement) then
-    WriteEmptyStatement(TJSEmptyStatement(el))
-  else if (el is TJSLiteral) then
-    WriteLiteral(TJSLiteral(el))
-  else if (el is TJSPrimaryExpression) then
-    WritePrimaryExpression(TJSPrimaryExpression(el))
-  else if (el is TJSArrayLiteral) then
-    WriteArrayLiteral(TJSArrayLiteral(el))
-  else if (el is TJSObjectLiteral) then
-    WriteObjectLiteral(TJSObjectLiteral(el))
-  else if (el is TJSMemberExpression) then
-    WriteMemberExpression(TJSMemberExpression(el))
-  else if (el is TJSRegularExpressionLiteral) then
+    WriteEmptyStatement(TJSEmptyStatement(El))
+  else if (El is TJSLiteral) then
+    WriteLiteral(TJSLiteral(El))
+  else if (El is TJSPrimaryExpression) then
+    WritePrimaryExpression(TJSPrimaryExpression(El))
+  else if (El is TJSArrayLiteral) then
+    WriteArrayLiteral(TJSArrayLiteral(El))
+  else if (El is TJSObjectLiteral) then
+    WriteObjectLiteral(TJSObjectLiteral(El))
+  else if (El is TJSMemberExpression) then
+    WriteMemberExpression(TJSMemberExpression(El))
+  else if (El is TJSRegularExpressionLiteral) then
     WriteRegularExpressionLiteral(TJSRegularExpressionLiteral(El))
     WriteRegularExpressionLiteral(TJSRegularExpressionLiteral(El))
-  else if (el is TJSCallExpression) then
-    WriteCallExpression(TJSCallExpression(el))
-  else if (el is TJSLabeledStatement) then // Before unary
-    WriteLabeledStatement(TJSLabeledStatement(el))
-  else if (el is TJSFunctionBody) then // Before unary
-    WriteFunctionBody(TJSFunctionBody(el))
-  else if (el is TJSVariableStatement) then // Before unary
-    WriteVariableStatement(TJSVariableStatement(el))
-  else if (el is TJSUNary) then
-    WriteUnary(TJSUnary(el))
-  else if (el is TJSVariableDeclarationList) then
-    WriteVarDeclarationList(TJSVariableDeclarationList(el)) // Must be before binary
-  else if (el is TJSStatementList) then
-    WriteStatementList(TJSStatementList(el)) // Must be before binary
-  else if (el is TJSWithStatement) then
+  else if (El is TJSCallExpression) then
+    WriteCallExpression(TJSCallExpression(El))
+  else if (El is TJSLabeledStatement) then // Before unary
+    WriteLabeledStatement(TJSLabeledStatement(El))
+  else if (El is TJSFunctionBody) then // Before unary
+    WriteFunctionBody(TJSFunctionBody(El))
+  else if (El is TJSVariableStatement) then // Before unary
+    WriteVariableStatement(TJSVariableStatement(El))
+  else if (El is TJSUNary) then
+    WriteUnary(TJSUnary(El))
+  else if (El is TJSVariableDeclarationList) then
+    WriteVarDeclarationList(TJSVariableDeclarationList(El)) // Must be before binary
+  else if (El is TJSStatementList) then
+    WriteStatementList(TJSStatementList(El)) // Must be before binary
+  else if (El is TJSWithStatement) then
     WriteWithStatement(TJSWithStatement(El)) // Must be before binary
     WriteWithStatement(TJSWithStatement(El)) // Must be before binary
-  else if (el is TJSBinary) then
-    WriteBinary(TJSBinary(el))
-  else if (el is TJSConditionalExpression) then
-    WriteConditionalExpression(TJSConditionalExpression(el))
-  else if (el is TJSAssignStatement) then
-    WriteAssignStatement(TJSAssignStatement(el))
-  else if (el is TJSVarDeclaration) then
-    WriteVarDeclaration(TJSVarDeclaration(el))
-  else if (el is TJSIfStatement) then
-    WriteIfStatement(TJSIfStatement(el))
-  else if (el is TJSTargetStatement) then
-    WriteTargetStatement(TJSTargetStatement(el))
-  else if (el is TJSReturnStatement) then
-    WriteReturnStatement(TJSReturnStatement(el))
-  else if (el is TJSTryStatement) then
-    WriteTryStatement(TJSTryStatement(el))
-  else if (el is TJSFunctionDeclarationStatement) then
-    WriteFunctionDeclarationStatement(TJSFunctionDeclarationStatement(el))
-  else if (el is TJSSourceElements) then
-    WriteSourceElements(TJSSourceElements(el))
-  else if EL=Nil then
+  else if (El is TJSBinary) then
+    WriteBinary(TJSBinary(El))
+  else if (El is TJSConditionalExpression) then
+    WriteConditionalExpression(TJSConditionalExpression(El))
+  else if (El is TJSAssignStatement) then
+    WriteAssignStatement(TJSAssignStatement(El))
+  else if (El is TJSVarDeclaration) then
+    WriteVarDeclaration(TJSVarDeclaration(El))
+  else if (El is TJSIfStatement) then
+    WriteIfStatement(TJSIfStatement(El))
+  else if (El is TJSTargetStatement) then
+    WriteTargetStatement(TJSTargetStatement(El))
+  else if (El is TJSReturnStatement) then
+    WriteReturnStatement(TJSReturnStatement(El))
+  else if (El is TJSTryStatement) then
+    WriteTryStatement(TJSTryStatement(El))
+  else if (El is TJSFunctionDeclarationStatement) then
+    WriteFunctionDeclarationStatement(TJSFunctionDeclarationStatement(El))
+  else if (El is TJSSourceElements) then
+    WriteSourceElements(TJSSourceElements(El))
+  else if El=Nil then
     Error(SErrNilNode)
     Error(SErrNilNode)
   else
   else
     Error(SErrUnknownJSClass,[El.ClassName]);
     Error(SErrUnknownJSClass,[El.ClassName]);
-//  Write('/* '+EL.ClassName+' */');
+//  Write('/* '+El.ClassName+' */');
   FSkipBrackets:=False;
   FSkipBrackets:=False;
 end;
 end;