123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- #include "ColorParser.h"
- using namespace KSGScript;
- const char *(ColorParser::keyword[]) = { "lesen", "var", "func", "Rückruf", "class", "public", "private", "if", "else", "return", "break", "continue", "for", "while" };
- const int ColorParser::keywordAnz = 14;
- const char *( ColorParser::type[] ) = { "void", "bool", "int", "double", "Array", "Thread", "Text", "Bild", "MausEreignis", "TastaturEreignis", "TextFeld", "Knopf", "Fenster", "BildZ", "Animation2DData", "Animation2D" };
- const int ColorParser::typeAnz = 16;
- // Konstructor
- // zTxt: Der Text, der geparsed werden soll
- ColorParser::ColorParser( Text *zTxt )
- {
- pfad = new Text( "" );
- txt = zTxt->getThis();
- reset();
- reload();
- }
- // Destructor
- ColorParser::~ColorParser()
- {
- for( auto i = abschnitt.getIterator(); i; i++ )
- {
- if( i._.zFunktion )
- i._.zFunktion->release();
- if( i._.zKlasse )
- i._.zKlasse->release();
- }
- txt->release();
- }
- // behandelt einen Fehler
- void ColorParser::handleError( int begin, int ende )
- {
- if( errorIgnore )
- return;
- 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.inKeyword = 0;
- pos.inTypename = 0;
- pos.inParameter = 0;
- pos.inInstanzVar = 0;
- }
- // Lädt das Script neu
- void ColorParser::reload()
- {
- error.leeren();
- string.leeren();
- kommentar.leeren();
- for( auto i = abschnitt.getIterator(); i; i++ )
- {
- if( i._.zFunktion )
- i._.zFunktion->release();
- if( i._.zKlasse )
- i._.zKlasse->release();
- }
- abschnitt.leeren();
- delete dat;
- dat = new KSGSLeseScript();
- d = new TextReader( txt->getThis() );
- zeile = 1;
- while( !d->istEnde() )
- {
- bool br = 0;
- int id = leseNext();
- switch( id )
- {
- case -1: // Ende der Datei
- break;
- case 1: // Klasse
- if( 1 )
- {
- KSGSLeseKlasse *k = leseKlasse();
- if( !k )
- {
- //error( 6, { "Klasse", *pfad }, zObj );
- br = 1;
- }
- else
- dat->klassen.add( k );
- }
- break;
- case 2: // funktion
- if( 1 )
- {
- KSGSLeseFunktion *f = leseFunktion();
- if( !f )
- {
- //error( 6, { "Funktion", *pfad }, zObj );
- br = 1;
- }
- else
- dat->funktionen.add( f );
- }
- break;
- case 3: // Variable
- if( 1 )
- {
- KSGSLeseVariable *v = leseVariable();
- if( !v )
- {
- //error( 6, { "Variable", *pfad }, zObj );
- br = 1;
- }
- else
- dat->variablen.add( v );
- }
- break;
- case 12: // lesen
- if( 1 )
- {
- if( !leseBis( '(' ) )
- {
- //error( 6, { "Lade Anweisung", *pfad }, zObj );
- br = 1;
- break;
- }
- d->setLPosition( d->getLPosition() + 1, 0 );
- if( !leseBis( '"' ) )
- {
- //error( 6, { "Lade Anweisung", *pfad }, zObj );
- br = 1;
- break;
- }
- d->setLPosition( d->getLPosition() + 1, 0 );
- __int64 end = nextPosOf( '"', 0 );
- if( end < 0 )
- {
- //error( 6, { "Lade Anweisung", *pfad }, zObj );
- br = 1;
- break;
- }
- char *datei = new char[ (int)( end - d->getLPosition() ) + 1 ];
- datei[ end - d->getLPosition() ] = 0;
- d->lese( datei, (int)( end - d->getLPosition() ) );
- Text *dPf = new Text( datei );
- dPf->ersetzen( '\\', '/' );
- if( dPf->getText()[ 0 ] == '/' )
- dPf->remove( 0 );
- else
- dPf->insert( 0, wd->getText() );
- delete[] datei;
- if( !leseBis( ')' ) )
- {
- //error( 6, { "Lade Anweisung", *pfad }, zObj );
- br = 1;
- break;
- }
- d->setLPosition( d->getLPosition() + 1, 0 );
- Text *pf = new Text( pfad->getText() );
- pfad->setText( dPf );
- Reader *tmp = d;
- if( !ladeDatei() )
- {
- handleError( (int)d->getLPosition() - 1, (int)d->getLPosition() );
- d = tmp;
- pfad->setText( pf );
- //error( 6, { "Lade Anweisung", *pfad }, zObj );
- br = 1;
- break;
- }
- d = tmp;
- pfad->setText( pf );
- }
- break;
- default: // Fehler
- handleError( (int)d->getLPosition() - 1, (int)d->getLPosition() );
- //error( 5, { *pfad, Text() += zeile }, zObj );
- br = 1;
- break;
- }
- if( id == -1 )
- break;
- if( br )
- {
- //ok = 0;
- break;
- }
- }
- d = ( (TextReader*)d )->release();
- }
- KSGSLeseKlasse *ColorParser::leseKlasse()
- {
- Abschnitt abs;
- abs.anfang = (int)d->getLPosition();
- KSGSLeseKlasse *ret = __super::leseKlasse();
- abs.ende = (int)d->getLPosition();
- abs.zKlasse = ret;
- abs.zFunktion = 0;
- if( ret )
- ret->ref++;
- abschnitt.add( abs );
- return ret;
- }
- KSGSLeseFunktion *ColorParser::leseFunktion()
- {
- Abschnitt abs;
- abs.anfang = (int)d->getLPosition();
- KSGSLeseFunktion *ret = __super::leseFunktion();
- abs.ende = (int)d->getLPosition();
- abs.zKlasse = 0;
- abs.zFunktion = ret;
- if( ret )
- ret->ref++;
- abschnitt.add( abs );
- return ret;
- }
- // Gibt den Farbtyp des nächsten Zeichens zurück
- KSGScriptEditor::ColorType ColorParser::getNextColor( int p, int &underlineC )
- {
- KSGScriptEditor::ColorType color = KSGScriptEditor::ColorType::NORMAL_TEXT;
- int scriptL = txt->getLength();
- 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;
- }
- bool inString = 0;
- bool inKommentar = 0;
- for( auto i = string.getIterator(); i; i++ )
- {
- if( i._.anfang <= p && i._.ende >= p )
- inString = 1;
- }
- for( auto i = kommentar.getIterator(); i; i++ )
- {
- if( i._.anfang <= p && i._.ende >= p )
- inKommentar = 1;
- }
- if( !inString && !inKommentar )
- {
- 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 + 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 && istLehr( cm1 ) )
- {
- for( int i = 0; i < typeAnz; i++ )
- {
- int tl = textLength( type[ i ] );
- pos.inTypename = 1;
- 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;
- }
- if( !pos.inTypename && dat )
- {
- int anz = dat->klassen.getEintragAnzahl();
- for( int i = 0; i < anz; i++ )
- {
- int tl = dat->klassen.get( i )->name.getLength();
- pos.inTypename = 1;
- 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 && istLehr( cm1 ) )
- {
- for( auto i = abschnitt.getIterator(); i; i++ )
- {
- if( i._.anfang <= p && i._.ende >= p )
- {
- if( i._.zFunktion )
- {
- int anz = i._.zFunktion->parameter.getEintragAnzahl();
- for( int j = 0; j < anz; j++ )
- {
- int nl = i._.zFunktion->parameter.get( j )->name.getLength();
- pos.inParameter = 1;
- for( int k = 0; k < nl && k + p < scriptL && pos.inParameter; k++ )
- pos.inParameter &= txt->getText()[ k + p ] == i._.zFunktion->parameter.get( j )->name.getText()[ k ];
- if( pos.inParameter && ( nl + p == scriptL || istTrenner( txt->getText()[ nl + p ] ) ) )
- break;
- pos.inParameter = 0;
- }
- }
- }
- }
- }
- if( !pos.inInstanzVar && !pos.inParameter && !pos.inKeyword && !pos.inTypename && istLehr( cm1 ) )
- {
- for( auto i = abschnitt.getIterator(); i; i++ )
- {
- if( i._.anfang <= p && i._.ende >= p )
- {
- if( i._.zKlasse )
- {
- int anz = i._.zKlasse->variablen.getEintragAnzahl();
- for( int j = 0; j < anz; j++ )
- {
- int nl = i._.zKlasse->variablen.get( j )->name.getLength();
- pos.inInstanzVar = 1;
- for( int k = 0; k < nl && k + p < scriptL && pos.inInstanzVar; k++ )
- pos.inInstanzVar &= txt->getText()[ k + p ] == i._.zKlasse->variablen.get( j )->name.getText()[ k ];
- if( pos.inInstanzVar && ( nl + p == scriptL || istTrenner( txt->getText()[ nl + p ] ) ) )
- break;
- pos.inInstanzVar = 0;
- }
- }
- }
- }
- }
- }
- if( pos.inKeyword )
- color = KSGScriptEditor::ColorType::KEYWORD;
- if( pos.inTypename )
- color = KSGScriptEditor::ColorType::TYPENAME;
- if( pos.inParameter )
- color = KSGScriptEditor::ColorType::PARAMETER_VARIABLE;
- if( pos.inInstanzVar )
- color = KSGScriptEditor::ColorType::INSTANCE_VARIABLE;
- if( inString )
- color = KSGScriptEditor::ColorType::STRING;
- if( inKommentar )
- color = KSGScriptEditor::ColorType::KOMMENTAR;
- for( auto i = error.getIterator(); i; i++ )
- {
- if( i._.anfang <= p && i._.ende >= p )
- underlineC = KSGScriptEditor::ColorType::ERROR_UNDERLINE;
- }
- return color;
- }
|