Browse Source

fcl-passrc: added tkOtherwise in all mdoes except ISO and ExtPas

git-svn-id: trunk@45472 -
Mattias Gaertner 5 years ago
parent
commit
e0d2651a61
2 changed files with 50 additions and 49 deletions
  1. 40 46
      packages/fcl-passrc/src/pparser.pp
  2. 10 3
      packages/fcl-passrc/src/pscanner.pp

+ 40 - 46
packages/fcl-passrc/src/pparser.pp

@@ -2075,12 +2075,14 @@ function TPasParser.isEndOfExp(AllowEqual : Boolean = False; CheckHints : Boolea
 const
 const
   EndExprToken = [
   EndExprToken = [
     tkEOF, tkBraceClose, tkSquaredBraceClose, tkSemicolon, tkComma, tkColon,
     tkEOF, tkBraceClose, tkSquaredBraceClose, tkSemicolon, tkComma, tkColon,
-    tkdo, tkdownto, tkelse, tkend, tkof, tkthen, tkto
+    tkdo, tkdownto, tkelse, tkend, tkof, tkthen, tkto, tkotherwise
   ];
   ];
 begin
 begin
-  Result:=(CurToken in EndExprToken) or (CheckHints and IsCurTokenHint);
-  if Not (Result or AllowEqual) then
-    Result:=(Curtoken=tkEqual);
+  if (CurToken in EndExprToken) or (CheckHints and IsCurTokenHint) then
+    exit(true);
+  if AllowEqual and (CurToken=tkEqual) then
+    exit(true);
+  Result:=false;
 end;
 end;
 
 
 function TPasParser.ExprToText(Expr: TPasExpr): String;
 function TPasParser.ExprToText(Expr: TPasExpr): String;
@@ -5831,7 +5833,7 @@ var
       exit; // at start of block
       exit; // at start of block
     t:=GetPrevToken;
     t:=GetPrevToken;
     case t of
     case t of
-    tkSemicolon,tkColon,tkElse: exit;
+    tkSemicolon,tkColon,tkElse,tkotherwise: exit;
     end;
     end;
     {$IFDEF VerbosePasParser}
     {$IFDEF VerbosePasParser}
     writeln('TPasParser.ParseStatement.CheckSemicolon Prev=',GetPrevToken,' Cur=',CurToken,' ',CurBlock.ClassName,' ',CurBlock.Elements.Count,' ',TObject(CurBlock.Elements[0]).ClassName);
     writeln('TPasParser.ParseStatement.CheckSemicolon Prev=',GetPrevToken,' Cur=',CurToken,' ',CurBlock.ClassName,' ',CurBlock.Elements.Count,' ',TObject(CurBlock.Elements[0]).ClassName);
@@ -5913,11 +5915,11 @@ begin
         El:=nil;
         El:=nil;
         ExpectToken(tkthen);
         ExpectToken(tkthen);
         end;
         end;
-      tkelse:
+      tkelse,tkotherwise:
         // ELSE can close multiple blocks, similar to semicolon
         // ELSE can close multiple blocks, similar to semicolon
         repeat
         repeat
           {$IFDEF VerbosePasParser}
           {$IFDEF VerbosePasParser}
-          writeln('TPasParser.ParseStatement CurBlock=',CurBlock.ClassName);
+          writeln('TPasParser.ParseStatement ELSE CurBlock=',CurBlock.ClassName);
           {$ENDIF}
           {$ENDIF}
           if CurBlock is TPasImplIfElse then
           if CurBlock is TPasImplIfElse then
             begin
             begin
@@ -5928,10 +5930,10 @@ begin
               CurBlock.AddElement(El); // this sets TPasImplIfElse(CurBlock).IfBranch:=El
               CurBlock.AddElement(El); // this sets TPasImplIfElse(CurBlock).IfBranch:=El
               El:=nil;
               El:=nil;
             end;
             end;
-            if TPasImplIfElse(CurBlock).ElseBranch=nil then
+            if (CurToken=tkelse) and (TPasImplIfElse(CurBlock).ElseBranch=nil) then
               break; // add next statement as ElseBranch
               break; // add next statement as ElseBranch
             end
             end
-          else if CurBlock is TPasImplTryExcept then
+          else if (CurBlock is TPasImplTryExcept) and (CurToken=tkelse) then
             begin
             begin
             // close TryExcept handler and open an TryExceptElse handler
             // close TryExcept handler and open an TryExceptElse handler
             CloseBlock;
             CloseBlock;
@@ -6087,7 +6089,7 @@ begin
                 ParseExc(nParserExpectCase,SParserExpectCase);
                 ParseExc(nParserExpectCase,SParserExpectCase);
               break; // end without else
               break; // end without else
               end;
               end;
-            tkelse:
+            tkelse,tkotherwise:
               begin
               begin
                 // create case-else block
                 // create case-else block
                 El:=TPasImplCaseElse(CreateElement(TPasImplCaseElse,'',CurBlock,CurTokenPos));
                 El:=TPasImplCaseElse(CreateElement(TPasImplCaseElse,'',CurBlock,CurTokenPos));
@@ -6098,46 +6100,38 @@ begin
               end
               end
             else
             else
               // read case values
               // read case values
-              if (curToken=tkIdentifier) and (LowerCase(CurtokenString)='otherwise') then
-                begin
-                // create case-else block
-                El:=TPasImplCaseElse(CreateElement(TPasImplCaseElse,'',CurBlock,CurTokenPos));
-                TPasImplCaseOf(CurBlock).ElseBranch:=TPasImplCaseElse(El);
-                CreateBlock(TPasImplCaseElse(El));
-                El:=nil;
-                break;
-                end
-              else
-                repeat
-                  SrcPos:=CurTokenPos;
-                  Left:=DoParseExpression(CurBlock);
-                  //writeln(i,'CASE value="',Expr,'" Token=',CurTokenText);
-                  if CurBlock is TPasImplCaseStatement then
-                    begin
-                    TPasImplCaseStatement(CurBlock).AddExpression(Left);
-                    Left:=nil;
-                    end
-                  else
-                    begin
-                    El:=TPasImplCaseStatement(CreateElement(TPasImplCaseStatement,'',CurBlock,SrcPos));
-                    TPasImplCaseStatement(El).AddExpression(Left);
-                    Left:=nil;
-                    CreateBlock(TPasImplCaseStatement(El));
-                    El:=nil;
-                    end;
-                  //writeln(i,'CASE after value Token=',CurTokenText);
-                  if (CurToken=tkComma) then
-                    NextToken
-                  else if (CurToken<>tkColon) then
-                    ParseExcTokenError(TokenInfos[tkComma]);
-                until Curtoken=tkColon;
+              repeat
+                SrcPos:=CurTokenPos;
+                Left:=DoParseExpression(CurBlock);
+                //writeln(i,'CASE value="',Expr,'" Token=',CurTokenText);
+                if CurBlock is TPasImplCaseStatement then
+                  begin
+                  TPasImplCaseStatement(CurBlock).AddExpression(Left);
+                  Left:=nil;
+                  end
+                else
+                  begin
+                  El:=TPasImplCaseStatement(CreateElement(TPasImplCaseStatement,'',CurBlock,SrcPos));
+                  TPasImplCaseStatement(El).AddExpression(Left);
+                  Left:=nil;
+                  CreateBlock(TPasImplCaseStatement(El));
+                  El:=nil;
+                  end;
+                //writeln(i,'CASE after value Token=',CurTokenText);
+                if (CurToken=tkComma) then
+                  NextToken
+                else if (CurToken<>tkColon) then
+                  ParseExcTokenError(TokenInfos[tkComma]);
+              until Curtoken=tkColon;
               // read statement
               // read statement
               ParseStatement(CurBlock,SubBlock);
               ParseStatement(CurBlock,SubBlock);
               // CurToken is now at last token of case-statement
               // CurToken is now at last token of case-statement
               CloseBlock;
               CloseBlock;
               if CurToken<>tkSemicolon then
               if CurToken<>tkSemicolon then
                 NextToken;
                 NextToken;
-              if not (CurToken in [tkSemicolon,tkelse,tkend]) then
+              if (CurToken in [tkSemicolon,tkelse,tkend,tkotherwise]) then
+                // ok
+              else
                 ParseExcTokenError(TokenInfos[tkSemicolon]);
                 ParseExcTokenError(TokenInfos[tkSemicolon]);
               if CurToken<>tkSemicolon then
               if CurToken<>tkSemicolon then
                 UngetToken;
                 UngetToken;
@@ -6195,7 +6189,7 @@ begin
         ImplRaise:=TPasImplRaise(CreateElement(TPasImplRaise,'',CurBlock,CurTokenPos));
         ImplRaise:=TPasImplRaise(CreateElement(TPasImplRaise,'',CurBlock,CurTokenPos));
         CreateBlock(ImplRaise);
         CreateBlock(ImplRaise);
         NextToken;
         NextToken;
-        If Curtoken in [tkElse,tkEnd,tkSemicolon] then
+        If Curtoken in [tkElse,tkEnd,tkSemicolon,tkotherwise] then
           UnGetToken
           UnGetToken
         else
         else
           begin
           begin
@@ -6205,7 +6199,7 @@ begin
             NextToken;
             NextToken;
             ImplRaise.ExceptAddr:=DoParseExpression(ImplRaise);
             ImplRaise.ExceptAddr:=DoParseExpression(ImplRaise);
             end;
             end;
-          if Curtoken in [tkElse,tkEnd,tkSemicolon] then
+          If Curtoken in [tkElse,tkEnd,tkSemicolon,tkotherwise] then
             UngetToken
             UngetToken
           end;
           end;
         end;
         end;

+ 10 - 3
packages/fcl-passrc/src/pscanner.pp

@@ -224,6 +224,7 @@ type
     tkof,
     tkof,
     tkoperator,
     tkoperator,
     tkor,
     tkor,
+    tkotherwise,
     tkpacked,
     tkpacked,
     tkprocedure,
     tkprocedure,
     tkprogram,
     tkprogram,
@@ -1007,6 +1008,7 @@ const
     'of',
     'of',
     'operator',
     'operator',
     'or',
     'or',
+    'otherwise',
     'packed',
     'packed',
     'procedure',
     'procedure',
     'program',
     'program',
@@ -3608,7 +3610,8 @@ procedure TPascalScanner.HandleMode(const Param: String);
   procedure SetMode(const LangMode: TModeSwitch;
   procedure SetMode(const LangMode: TModeSwitch;
     const NewModeSwitches: TModeSwitches; IsDelphi: boolean;
     const NewModeSwitches: TModeSwitches; IsDelphi: boolean;
     const AddBoolSwitches: TBoolSwitches = [];
     const AddBoolSwitches: TBoolSwitches = [];
-    const RemoveBoolSwitches: TBoolSwitches = []
+    const RemoveBoolSwitches: TBoolSwitches = [];
+    UseOtherwise: boolean = true
     );
     );
   var
   var
     Handled: Boolean;
     Handled: Boolean;
@@ -3627,6 +3630,10 @@ procedure TPascalScanner.HandleMode(const Param: String);
         FOptions:=FOptions+[po_delphi]
         FOptions:=FOptions+[po_delphi]
       else
       else
         FOptions:=FOptions-[po_delphi];
         FOptions:=FOptions-[po_delphi];
+      if UseOtherwise then
+        UnsetNonToken(tkotherwise)
+      else
+        SetNonToken(tkotherwise);
       end;
       end;
     Handled:=false;
     Handled:=false;
     if Assigned(OnModeChanged) then
     if Assigned(OnModeChanged) then
@@ -3668,9 +3675,9 @@ begin
   'MACPAS':
   'MACPAS':
     SetMode(msMac,MacModeSwitches,false,bsMacPasMode);
     SetMode(msMac,MacModeSwitches,false,bsMacPasMode);
   'ISO':
   'ISO':
-    SetMode(msIso,ISOModeSwitches,false);
+    SetMode(msIso,ISOModeSwitches,false,[],[],false);
   'EXTENDED':
   'EXTENDED':
-    SetMode(msExtpas,ExtPasModeSwitches,false);
+    SetMode(msExtpas,ExtPasModeSwitches,false,[],[],false);
   'GPC':
   'GPC':
     SetMode(msGPC,GPCModeSwitches,false);
     SetMode(msGPC,GPCModeSwitches,false);
   else
   else