Ver código fonte

Performance des Editors verbessert und Farbe für Kommentare hinzugefügt

Kolja Strohm 7 anos atrás
pai
commit
1501d2924e

+ 11 - 4
ksgScript/Editor/Editor.cpp

@@ -30,6 +30,7 @@ Editor::Editor()
     setTextColor( 0xFFA0A0A0, ColorType::INSTANCE_VARIABLE );
     setTextColor( 0xFF40FF70, ColorType::TYPENAME );
     setTextColor( 0xFFFFA000, ColorType::STRING );
+    setTextColor( 0xFF20FFFF, ColorType::KOMMENTAR );
     setTextColor( 0xFFFF4040, ColorType::ERROR_UNDERLINE );
     parser = 0;
     reloadCounter = 10;
@@ -132,6 +133,7 @@ void Editor::setSchriftZ( Schrift *s )
 //  txt: Der Text
 void Editor::setText( Text *txt )
 {
+    lockZeichnung();
     if( script )
         script->release();
     if( parser )
@@ -141,6 +143,7 @@ void Editor::setText( Text *txt )
     txt->release();
     updateHScroll( -1 );
     updateVScroll( -1 );
+    unlockZeichnung();
 }
 
 // Gibt den aktuellen Text zurück
@@ -562,8 +565,10 @@ void Editor::doTastaturEreignis( TastaturEreignis &te )
     lockZeichnung();
     if( !--reloadCounter )
     {
-        reloadCounter = 10;
+        zm.messungStart();
         parser->reload();
+        zm.messungEnde();
+        reloadCounter = (int)( zm.getSekunden() * 100 );
     }
     unlockZeichnung();
     --ref;
@@ -590,8 +595,10 @@ bool Editor::tick( double tickval )
             lockZeichnung();
             if( (reloadCounter -= 2 ) <= 0 )
             {
-                reloadCounter = 10;
+                zm.messungStart();
                 parser->reload();
+                zm.messungEnde();
+                reloadCounter = (int)( zm.getSekunden() * 100 );
             }
             unlockZeichnung();
             tickVal -= 1;
@@ -629,12 +636,12 @@ void Editor::render( Bild &rObj )
         xxx -= horizontalScrollBar->getScroll();
     schrift->setDrawPosition( xxx, yyy );
     parser->reset();
-    auto colorF = [ this, &rObj ]( int x, int y ) -> int
+    auto colorF = [ this, &rObj ]( int x, int y, int pos ) -> int
     {
         if( !parser )
             return colors[ KSGScriptEditor::ColorType::NORMAL_TEXT ];
         int uC =  -1;
-        int tC = colors[ parser->getNextColor( uC ) ];
+        int tC = colors[ parser->getNextColor( pos, uC ) ];
         if( uC >= 0 )
             rObj.drawLinieH( x, y + 12, 12, colors[ uC ] );
         return tC;

+ 2 - 0
ksgScript/Editor/Editor.h

@@ -5,6 +5,7 @@
 #include <MausEreignis.h>
 #include <TastaturEreignis.h>
 #include "../Include/KSGScript.h"
+#include <Zeit.h>
 
 using namespace Framework;
 
@@ -19,6 +20,7 @@ namespace KSGScript
         Schrift *schrift;
         Text *script;
         ColorParser *parser;
+        ZeitMesser zm;
         bool errorDetection;
         bool warningDetection;
         double tickVal;

+ 68 - 40
ksgScript/Editor/Parser/ColorParser.cpp

@@ -21,6 +21,13 @@ ColorParser::ColorParser( Text *zTxt )
 // Destructor
 ColorParser::~ColorParser()
 {
+    for( auto i = abschnitt.getArray(); i.set; i++ )
+    {
+        if( i.var.zFunktion )
+            i.var.zFunktion->release();
+        if( i.var.zKlasse )
+            i.var.zKlasse->release();
+    }
     txt->release();
 }
 
@@ -29,30 +36,43 @@ void ColorParser::handleError( int begin, int ende )
 {
     if( errorIgnore )
         return;
-    Error err;
+    TextAbschnitt err;
     err.anfang = begin;
     err.ende = ende;
     error.add( err );
 }
 
+void ColorParser::handleKommentar( int beginn, int ende )
+{
+    TextAbschnitt kom;
+    kom.anfang = beginn;
+    kom.ende = ende;
+    kommentar.add( kom );
+}
+
+void ColorParser::handleString( int beginn, int ende )
+{
+    TextAbschnitt str;
+    str.anfang = beginn;
+    str.ende = ende;
+    string.add( str );
+}
+
 // Setzt den Parsevorgang zum Beginn zurück
 void ColorParser::reset()
 {
-    pos.pos = 0;
     pos.inKeyword = 0;
     pos.inTypename = 0;
     pos.inParameter = 0;
     pos.inInstanzVar = 0;
-    pos.inString = 0;
-    pos.inChar = 0;
-    pos.lastLehr = 1;
-    pos.lastTrenner = 1;
 }
 
 // Lädt das Script neu
 void ColorParser::reload()
 {
     error.leeren();
+    string.leeren();
+    kommentar.leeren();
     for( auto i = abschnitt.getArray(); i.set; i++ )
     {
         if( i.var.zFunktion )
@@ -215,47 +235,55 @@ KSGSLeseFunktion *ColorParser::leseFunktion()
 }
 
 // Gibt den Farbtyp des nächsten Zeichens zurück
-KSGScriptEditor::ColorType ColorParser::getNextColor( int &underlineC )
+KSGScriptEditor::ColorType ColorParser::getNextColor( int p, int &underlineC )
 {
     KSGScriptEditor::ColorType color = KSGScriptEditor::ColorType::NORMAL_TEXT;
     int scriptL = txt->getLength();
-    char c = txt->getText()[ pos.pos++ ];
-    bool inString = pos.inChar || pos.inString;
-    if( c == '\'' && txt->getText()[ pos.pos - 2 ] != '\\' )
-        pos.inChar = !pos.inChar;
-    if( c == '"' && txt->getText()[ pos.pos - 2 ] != '\\' )
-        pos.inString = !pos.inString;
-    if( istLehr( c ) || istTrenner( c ) )
+    char c = txt->getText()[ p ];
+    char cm1 = txt->getText()[ p - 1 ];
+    if( istLehr( cm1 ) || istTrenner( cm1 ) )
     {
         pos.inKeyword = 0;
         pos.inTypename = 0;
         pos.inInstanzVar = 0;
         pos.inParameter = 0;
     }
-    else if( !pos.inChar && !pos.inString )
+    bool inString = 0;
+    bool inKommentar = 0;
+    for( auto i = string.getArray(); i.set; i++ )
+    {
+        if( i.var.anfang <= p && i.var.ende >= p )
+            inString = 1;
+    }
+    for( auto i = kommentar.getArray(); i.set; i++ )
+    {
+        if( i.var.anfang <= p && i.var.ende >= p )
+            inKommentar = 1;
+    }
+    if( !inString && !inKommentar )
     {
-        if( !pos.inInstanzVar && !pos.inParameter && !pos.inKeyword && !pos.inTypename && pos.lastLehr )
+        if( !pos.inInstanzVar && !pos.inParameter && !pos.inKeyword && !pos.inTypename && istLehr( cm1 ) )
         {
             for( int i = 0; i < keywordAnz; i++ )
             {
                 int kl = textLength( keyword[ i ] );
                 pos.inKeyword = 1;
-                for( int j = 0; j < kl && j + pos.pos <= scriptL && pos.inKeyword; j++ )
-                    pos.inKeyword &= txt->getText()[ j + pos.pos - 1 ] == keyword[ i ][ j ];
-                if( pos.inKeyword && ( kl - 1 + pos.pos == scriptL || istTrenner( txt->getText()[ kl - 1 + pos.pos ] ) ) )
+                for( int j = 0; j < kl && j + p < scriptL && pos.inKeyword; j++ )
+                    pos.inKeyword &= txt->getText()[ j + p ] == keyword[ i ][ j ];
+                if( pos.inKeyword && ( kl + p == scriptL || istTrenner( txt->getText()[ kl + p ] ) ) )
                     break;
                 pos.inKeyword = 0;
             }
         }
-        if( !pos.inInstanzVar && !pos.inParameter && !pos.inKeyword && !pos.inTypename && pos.lastLehr )
+        if( !pos.inInstanzVar && !pos.inParameter && !pos.inKeyword && !pos.inTypename && istLehr( cm1 ) )
         {
             for( int i = 0; i < typeAnz; i++ )
             {
                 int tl = textLength( type[ i ] );
                 pos.inTypename = 1;
-                for( int j = 0; j < tl && j + pos.pos <= scriptL && pos.inTypename; j++ )
-                    pos.inTypename &= txt->getText()[ j + pos.pos - 1 ] == type[ i ][ j ];
-                if( pos.inTypename && ( tl - 1 + pos.pos == scriptL || istTrenner( txt->getText()[ tl - 1 + pos.pos ] ) ) )
+                for( int j = 0; j < tl && j + p < scriptL && pos.inTypename; j++ )
+                    pos.inTypename &= txt->getText()[ j + p ] == type[ i ][ j ];
+                if( pos.inTypename && ( tl + p == scriptL || istTrenner( txt->getText()[ tl + p ] ) ) )
                     break;
                 pos.inTypename = 0;
             }
@@ -266,19 +294,19 @@ KSGScriptEditor::ColorType ColorParser::getNextColor( int &underlineC )
                 {
                     int tl = dat->klassen.get( i )->name.getLength();
                     pos.inTypename = 1;
-                    for( int j = 0; j < tl && j + pos.pos <= scriptL && pos.inTypename; j++ )
-                        pos.inTypename &= txt->getText()[ j + pos.pos - 1 ] == dat->klassen.get( i )->name.getText()[ j ];
-                    if( pos.inTypename && ( tl - 1 + pos.pos == scriptL || istTrenner( txt->getText()[ tl - 1 + pos.pos ] ) ) )
+                    for( int j = 0; j < tl && j + p < scriptL && pos.inTypename; j++ )
+                        pos.inTypename &= txt->getText()[ j + p ] == dat->klassen.get( i )->name.getText()[ j ];
+                    if( pos.inTypename && ( tl + p == scriptL || istTrenner( txt->getText()[ tl + p ] ) ) )
                         break;
                     pos.inTypename = 0;
                 }
             }
         }
-        if( !pos.inInstanzVar && !pos.inParameter && !pos.inKeyword && !pos.inTypename && pos.lastLehr )
+        if( !pos.inInstanzVar && !pos.inParameter && !pos.inKeyword && !pos.inTypename && istLehr( cm1 ) )
         {
             for( auto i = abschnitt.getArray(); i.set; i++ )
             {
-                if( i.var.anfang <= pos.pos - 1 && i.var.ende >= pos.pos - 1 )
+                if( i.var.anfang <= p && i.var.ende >= p )
                 {
                     if( i.var.zFunktion )
                     {
@@ -287,9 +315,9 @@ KSGScriptEditor::ColorType ColorParser::getNextColor( int &underlineC )
                         {
                             int nl = i.var.zFunktion->parameter.get( j )->name.getLength();
                             pos.inParameter = 1;
-                            for( int k = 0; k < nl && k + pos.pos <= scriptL && pos.inParameter; k++ )
-                                pos.inParameter &= txt->getText()[ k + pos.pos - 1 ] == i.var.zFunktion->parameter.get( j )->name.getText()[ k ];
-                            if( pos.inParameter && ( nl - 1 + pos.pos == scriptL || istTrenner( txt->getText()[ nl - 1 + pos.pos ] ) ) )
+                            for( int k = 0; k < nl && k + p < scriptL && pos.inParameter; k++ )
+                                pos.inParameter &= txt->getText()[ k + p ] == i.var.zFunktion->parameter.get( j )->name.getText()[ k ];
+                            if( pos.inParameter && ( nl + p == scriptL || istTrenner( txt->getText()[ nl + p ] ) ) )
                                 break;
                             pos.inParameter = 0;
                         }
@@ -297,11 +325,11 @@ KSGScriptEditor::ColorType ColorParser::getNextColor( int &underlineC )
                 }
             }
         }
-        if( !pos.inInstanzVar && !pos.inParameter && !pos.inKeyword && !pos.inTypename && pos.lastLehr )
+        if( !pos.inInstanzVar && !pos.inParameter && !pos.inKeyword && !pos.inTypename && istLehr( cm1 ) )
         {
             for( auto i = abschnitt.getArray(); i.set; i++ )
             {
-                if( i.var.anfang <= pos.pos - 1 && i.var.ende >= pos.pos - 1 )
+                if( i.var.anfang <= p && i.var.ende >= p )
                 {
                     if( i.var.zKlasse )
                     {
@@ -310,9 +338,9 @@ KSGScriptEditor::ColorType ColorParser::getNextColor( int &underlineC )
                         {
                             int nl = i.var.zKlasse->variablen.get( j )->name.getLength();
                             pos.inInstanzVar = 1;
-                            for( int k = 0; k < nl && k + pos.pos <= scriptL && pos.inInstanzVar; k++ )
-                                pos.inInstanzVar &= txt->getText()[ k + pos.pos - 1 ] == i.var.zKlasse->variablen.get( j )->name.getText()[ k ];
-                            if( pos.inInstanzVar && ( nl - 1 + pos.pos == scriptL || istTrenner( txt->getText()[ nl - 1 + pos.pos ] ) ) )
+                            for( int k = 0; k < nl && k + p < scriptL && pos.inInstanzVar; k++ )
+                                pos.inInstanzVar &= txt->getText()[ k + p ] == i.var.zKlasse->variablen.get( j )->name.getText()[ k ];
+                            if( pos.inInstanzVar && ( nl + p == scriptL || istTrenner( txt->getText()[ nl + p ] ) ) )
                                 break;
                             pos.inInstanzVar = 0;
                         }
@@ -329,15 +357,15 @@ KSGScriptEditor::ColorType ColorParser::getNextColor( int &underlineC )
         color = KSGScriptEditor::ColorType::PARAMETER_VARIABLE;
     if( pos.inInstanzVar )
         color = KSGScriptEditor::ColorType::INSTANCE_VARIABLE;
-    if( pos.inChar || pos.inString || inString )
+    if( inString )
         color = KSGScriptEditor::ColorType::STRING;
+    if( inKommentar )
+        color = KSGScriptEditor::ColorType::KOMMENTAR;
     for( auto i = error.getArray(); i.set; i++ )
     {
-        if( i.var.anfang <= pos.pos && i.var.ende >= pos.pos )
+        if( i.var.anfang <= p && i.var.ende >= p )
             underlineC = KSGScriptEditor::ColorType::ERROR_UNDERLINE;
     }
-    pos.lastLehr = istLehr( c );
-    pos.lastTrenner = istTrenner( c );
     return color;
 }
 

+ 7 - 8
ksgScript/Editor/Parser/ColorParser.h

@@ -12,15 +12,10 @@ namespace KSGScript
     private:
         struct Position
         {
-            int pos;
             bool inKeyword;
             bool inTypename;
             bool inInstanzVar;
             bool inParameter;
-            bool inString;
-            bool inChar;
-            bool lastLehr;
-            bool lastTrenner;
         };
         struct Abschnitt
         {
@@ -29,13 +24,15 @@ namespace KSGScript
             KSGSLeseKlasse *zKlasse;
             KSGSLeseFunktion *zFunktion;
         };
-        struct Error
+        struct TextAbschnitt
         {
             int anfang;
             int ende;
         };
         Array< Abschnitt > abschnitt;
-        Array< Error > error;
+        Array< TextAbschnitt > error;
+        Array< TextAbschnitt > kommentar;
+        Array< TextAbschnitt > string;
         int ref;
         Text *txt;
         Text *wd;
@@ -53,6 +50,8 @@ namespace KSGScript
         ~ColorParser();
         // behandelt einen Fehler
         void handleError( int begin, int ende ) override;
+        virtual void handleKommentar( int beginn, int ende );
+        virtual void handleString( int beginn, int ende );
         // Setzt den Parsevorgang zum Beginn zurück
         void reset();
         // Lädt das Script neu
@@ -60,7 +59,7 @@ namespace KSGScript
         KSGSLeseKlasse *leseKlasse() override;
         KSGSLeseFunktion *leseFunktion() override;
         // Gibt den Farbtyp des nächsten Zeichens zurück
-        KSGScriptEditor::ColorType getNextColor( int &underlineC );
+        KSGScriptEditor::ColorType getNextColor( int pos, int &underlineC );
         // Reference Counting
         ColorParser *getThis();
         ColorParser *release();

+ 1 - 0
ksgScript/Include/KSGScript.h

@@ -124,6 +124,7 @@ namespace KSGScript
             PARAMETER_VARIABLE,
             INSTANCE_VARIABLE,
             STRING,
+            KOMMENTAR,
             NORMAL_TEXT,
             ERROR_UNDERLINE,
             COLOR_ANZAHL // Dies wird nur benötigt um die Anzahl der verschiedenen Farben zu zählen

+ 72 - 0
ksgScript/Leser/KSGSLeser.cpp

@@ -264,6 +264,12 @@ KSGSLeser::KSGSLeser()
 void KSGSLeser::handleError( int beginn, int ende )
 {}
 
+void KSGSLeser::handleKommentar( int beginn, int ende )
+{}
+
+void KSGSLeser::handleString( int beginn, int ende )
+{}
+
 // Script Laden
 bool KSGSLeser::ladeDatei()
 {
@@ -400,19 +406,40 @@ bool KSGSLeser::leseBis( char c )
     int tmpZ = zeile;
     char byte = 0;
     bool kommentar = 0;
+    int kBeg = 0;
     bool slash = 0;
     bool gefunden = 0;
+    bool inString = 0;
+    bool inChar = 0;
+    int sBeg = 0;
     while( !d->istEnde() )
     {
         d->lese( &byte, 1 );
         if( byte == '\n' )
         {
             zeile++;
+            if( kommentar )
+                handleKommentar( kBeg, (int)d->getLPosition() );
             kommentar = 0;
             continue;
         }
+        if( byte == '"' && !inChar && !kommentar )
+        {
+            if( inString )
+                handleString( sBeg, (int)d->getLPosition() );
+            inString = !inString;
+            sBeg = (int)d->getLPosition() - 1;
+        }
+        if( byte == '\'' && !inString && !kommentar )
+        {
+            if( inChar )
+                handleString( sBeg, (int)d->getLPosition() );
+            inChar = !inChar;
+            sBeg = (int)d->getLPosition() - 1;
+        }
         if( byte == '#' )
         {
+            kBeg = (int)d->getLPosition() - 1;
             kommentar = 1;
             continue;
         }
@@ -420,6 +447,7 @@ bool KSGSLeser::leseBis( char c )
         {
             if( slash )
             {
+                kBeg = (int)d->getLPosition() - 2;
                 kommentar = 1;
                 slash = 0;
             }
@@ -451,19 +479,40 @@ __int64 KSGSLeser::nextPosOf( char c, char c2 )
     __int64 pos = d->getLPosition();
     char byte = 0;
     bool kommentar = 0;
+    int kBeg = 0;
     bool slash = 0;
     int count = 0;
     bool gefunden = 0;
+    bool inString = 0;
+    bool inChar = 0;
+    int sBeg = 0;
     while( !d->istEnde() )
     {
         d->lese( &byte, 1 );
         if( byte == '\n' )
         {
+            if( kommentar )
+                handleKommentar( kBeg, (int)d->getLPosition() );
             kommentar = 0;
             continue;
         }
+        if( byte == '"' && !inChar && !kommentar )
+        {
+            if( inString )
+                handleString( sBeg, (int)d->getLPosition() );
+            inString = !inString;
+            sBeg = (int)d->getLPosition() - 1;
+        }
+        if( byte == '\'' && !inString && !kommentar )
+        {
+            if( inChar )
+                handleString( sBeg, (int)d->getLPosition() );
+            inChar = !inChar;
+            sBeg = (int)d->getLPosition() - 1;
+        }
         if( byte == '#' )
         {
+            kBeg = (int)d->getLPosition() - 1;
             kommentar = 1;
             continue;
         }
@@ -471,6 +520,7 @@ __int64 KSGSLeser::nextPosOf( char c, char c2 )
         {
             if( slash )
             {
+                kBeg = (int)d->getLPosition() - 2;
                 kommentar = 1;
                 slash = 0;
             }
@@ -515,19 +565,40 @@ bool KSGSLeser::leseBisText()
     __int64 pos = d->getLPosition();
     char c = 0;
     bool kommentar = 0;
+    int kBeg = 0;
     bool slash = 0;
     bool gefunden = 0;
+    bool inString = 0;
+    bool inChar = 0;
+    int sBeg = 0;
     while( !d->istEnde() )
     {
         d->lese( &c, 1 );
         if( c == '\n' )
         {
             zeile++;
+            if( kommentar )
+                handleKommentar( kBeg, (int)d->getLPosition() );
             kommentar = 0;
             continue;
         }
+        if( c == '"' && !inChar && !kommentar )
+        {
+            if( inString )
+                handleString( sBeg, (int)d->getLPosition() );
+            inString = !inString;
+            sBeg = (int)d->getLPosition() - 1;
+        }
+        if( c == '\'' && !inString && !kommentar )
+        {
+            if( inChar )
+                handleString( sBeg, (int)d->getLPosition() );
+            inChar = !inChar;
+            sBeg = (int)d->getLPosition() - 1;
+        }
         if( c == '#' )
         {
+            kBeg = (int)d->getLPosition() - 1;
             kommentar = 1;
             continue;
         }
@@ -535,6 +606,7 @@ bool KSGSLeser::leseBisText()
         {
             if( slash )
             {
+                kBeg = (int)d->getLPosition() - 2;
                 kommentar = 1;
                 slash = 0;
             }

+ 2 - 0
ksgScript/Leser/KSGSLeser.h

@@ -105,6 +105,8 @@ namespace KSGScript
 		// Script Laden
         KSGSLeser();
         virtual void handleError( int begin, int ende );
+        virtual void handleKommentar( int beginn, int ende );
+        virtual void handleString( int beginn, int ende );
 		__declspec( dllexport ) bool ladeDatei();
 		__declspec( dllexport ) bool leseBis( char c );
 		__declspec( dllexport ) __int64 nextPosOf( char c, char c2 );