123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- #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( "" );
- ref = 1;
- txt = zTxt->getThis();
- reset();
- reload();
- }
- // Destructor
- ColorParser::~ColorParser()
- {
- txt->release();
- }
- // behandelt einen Fehler
- void ColorParser::handleError( int begin, int ende )
- {
- if( errorIgnore )
- return;
- Error err;
- err.anfang = begin;
- err.ende = ende;
- error.add( err );
- }
- // 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();
- for( auto i = abschnitt.getArray(); i.set; i++ )
- {
- if( i.var.zFunktion )
- i.var.zFunktion->release();
- if( i.var.zKlasse )
- i.var.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 &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 ) )
- {
- pos.inKeyword = 0;
- pos.inTypename = 0;
- pos.inInstanzVar = 0;
- pos.inParameter = 0;
- }
- else if( !pos.inChar && !pos.inString )
- {
- if( !pos.inInstanzVar && !pos.inParameter && !pos.inKeyword && !pos.inTypename && pos.lastLehr )
- {
- 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 ] ) ) )
- break;
- pos.inKeyword = 0;
- }
- }
- if( !pos.inInstanzVar && !pos.inParameter && !pos.inKeyword && !pos.inTypename && pos.lastLehr )
- {
- 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 ] ) ) )
- 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 + 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 ] ) ) )
- break;
- pos.inTypename = 0;
- }
- }
- }
- if( !pos.inInstanzVar && !pos.inParameter && !pos.inKeyword && !pos.inTypename && pos.lastLehr )
- {
- for( auto i = abschnitt.getArray(); i.set; i++ )
- {
- if( i.var.anfang <= pos.pos - 1 && i.var.ende >= pos.pos - 1 )
- {
- if( i.var.zFunktion )
- {
- int anz = i.var.zFunktion->parameter.getEintragAnzahl();
- for( int j = 0; j < anz; j++ )
- {
- 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 ] ) ) )
- break;
- pos.inParameter = 0;
- }
- }
- }
- }
- }
- if( !pos.inInstanzVar && !pos.inParameter && !pos.inKeyword && !pos.inTypename && pos.lastLehr )
- {
- for( auto i = abschnitt.getArray(); i.set; i++ )
- {
- if( i.var.anfang <= pos.pos - 1 && i.var.ende >= pos.pos - 1 )
- {
- if( i.var.zKlasse )
- {
- int anz = i.var.zKlasse->variablen.getEintragAnzahl();
- for( int j = 0; j < anz; j++ )
- {
- 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 ] ) ) )
- 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( pos.inChar || pos.inString || inString )
- color = KSGScriptEditor::ColorType::STRING;
- for( auto i = error.getArray(); i.set; i++ )
- {
- if( i.var.anfang <= pos.pos && i.var.ende >= pos.pos )
- underlineC = KSGScriptEditor::ColorType::ERROR_UNDERLINE;
- }
- pos.lastLehr = istLehr( c );
- pos.lastTrenner = istTrenner( c );
- return color;
- }
- // Reference Counting
- ColorParser *ColorParser::getThis()
- {
- ref++;
- return this;
- }
- ColorParser *ColorParser::release()
- {
- if( !--ref )
- delete this;
- return 0;
- }
|