/** Is the state of analyzer equal to a given state info? */ publicstaticfinalint EQUAL_STATE = 0;
/** Is the state of analyzer different from given state info? */ publicstaticfinalint DIFFERENT_STATE = 1;
/** Initial internal state of the analyzer */ publicstaticfinalint INIT = -1;
/** Internal state of the lexical analyzer. At the begining *it'ssettoINITvaluebutitischangedby<code>parseToken()</code> *asthecharactersareprocessedonebyone.
*/ protectedint state = INIT;
/** Text buffer to scan */ protectedchar buffer[];
/** Current offset in the buffer */ protectedint offset;
/** Offset holding the begining of the current token */ protectedint tokenOffset;
/** This variable is the length of the token that was found */ protectedint tokenLength;
/** Path from which the found token-id comes from. *The<code>TokenContext.getContextPath()</code>canbeused *togetthepath.Ifthelexicalanalyzerdoesn'tuse *anychildrentoken-contextsitcanassign *thepathintheconstructor.
*/ protected TokenContextPath tokenContextPath;
/** Setting this flag to true means that there are currently no more *buffersavailablesothatanalyzershouldreturnallthetokens *includingthosewhosesuccessfulscanningwouldbeotherwise *leftforlaterwhenthenextbufferwillbeavailable.Setting *thisflagtotrueensuresthatallthecharactersinthecurrent *bufferwillbeprocessed. *Thelexicalanalyzershouldononehandprocessallthecharacters *butontheotherhanditshould"save"itscontext.Forexample *ifthescannerfindstheunclosedcommentattheendofthebuffer *itshouldreturnthecommenttokenbut *stayinthe"beingincomment"internalstate.
*/ protectedboolean lastBuffer;
/** On which offset in the buffer scanning should stop. */ protectedint stopOffset;
/** The position in the document that logically corresponds *tothestopOffsetvalue.Ifthere'snorelation *tothedocument,it's-1.Thereasonwhytherelation *tothedocument'sdataisexpressedthrough *thestopOffsettostopPositionrelationisbecause *thestopOffsetistheonlyoffsetthatdoesn'tchange *rapidlyintheoperationofthelexicalanalyzer.
*/ protectedint stopPosition;
/** This variable can be populated by the parseToken() method *incasetheusertypesanerrorneousconstructionbut *it'sclearwhatcorrecttokenhemeanttowrite. *Forexampleiftheuserwritesasingle'0x'it'sanerrorneous *constructbutit'sclearthattheuserwantstoenter *thehexa-number.InthissituationtheparseToken() *shouldreporterror,butitshouldalsosetthesupposedTokenID *tothehexa-numbertoken. *Thisinformationisusedwhiledrawingthetext.Ifthecaret *standinsideoraroundsuchtoken,itcallsthegetSupposedTokenID() *aftercallingthenextToken()andifit'snon-nullitusesit *insteadoftheoriginaltoken.
*/ protected TokenID supposedTokenID;
/** Function that should be called externally to scan the text. *ItmanagesthecalltoparseToken()andcaresabouttheproper *settingoftheoffsets. *Itcanbeextendedtosupportanycustomdebuggingrequired.
*/ public TokenID nextToken() { // Return immediately when at the end of buffer if (tokenOffset >= stopOffset) {
tokenLength = 0; returnnull; // signal no token found
}
// Divide non-debug and debug sections
supposedTokenID = null;
TokenID tokenID = parseToken(); if (tokenID != null) { // regular token found
tokenLength = offset - tokenOffset;
tokenOffset = offset; // move to the next token if (tokenLength == 0) { // test for empty token return nextToken(); // repeat until non-empty token is found
}
} else { // EOT reached
tokenLength = 0;
}
return tokenID;
}
/** This is core function of analyzer and it returns either the token-id *ornulltoindicatethattheendofbufferwasfound. *Thefunctionscanstheactivecharacteranddoesoneormore *ofthefollowingactions: *1.changeinternalanalyzerstate *2.setthetoken-context-pathandreturntoken-id *3.adjustcurrentpositiontosignaldifferentendoftoken; *thecharacterthatoffsetpointstoisnotincludedinthetoken
*/ protected TokenID parseToken() { returnnull;
}
/** Load the state from syntax mark into analyzer. This method is used when *@paramstateInfoinfoaboutthestateofthelexicalanalyzertoload. *Itcanbenulltoindicatethere'snopreviousstatesotheanalyzer *startsfromitsinitialstate. *@parambufferbufferthatwillbescanned *@paramoffsetoffsetofthefirstcharacterthatwillbescanned *@paramlenlengthoftheareatobescanned *@paramlastBufferwhetherthisisthelastbufferinthedocument.Allthetokens *willbereturnedincludingthelastpossiblyincompleteone.Ifthedata *comefromthedocument,thesimpleruleforthisparameter *is(doc.getLength()==stop-position)wherestop-position *isthepositioncorrespondingtothe(offset+len)inthebuffer *thatcomesfromthedocumentdata. *@paramstopPositionpositioninthedocumentthatcorrespondsto(offset+len)offset *intheprovidedbuffer.Ithasonlysenseifthedatainthebuffercomefromthedocument. *Ithelpsinwritingtheadvancedanalyzersthatneedtointeractwithsomeotherdata *inthedocumentthanonlythoseprovidedinthecharacterbuffer. *Ifthereisnorelationtothedocumentdata,thestopPositionparameter *mustbefilledwith-1whichmeansaninvalidvalue. *Thestop-positionispassed(insteadofstart-position)becauseitdoesn't *changethroughtheanalyzeroperation.Itcorrespondstothe<code>stopOffset</code> *thatalsodoesn'tchangethroughtheanalyzeroperationsoany *buffer-offsetcanbetransferredtopositionbycomputing *<code>stopPosition+buffer-offset-stopOffset</code> *wherestopOffsetistheinstancevariablethatisassigned *to<code>offset+len</code>inthebodyofrelocate().
*/ publicvoid load(StateInfo stateInfo, char buffer[], int offset, int len, boolean lastBuffer, int stopPosition) { this.buffer = buffer; this.offset = offset; this.tokenOffset = offset; this.stopOffset = offset + len; this.lastBuffer = lastBuffer; this.stopPosition = stopPosition;
int delta = offset - this.offset; // delta according to current offset this.offset += delta; this.tokenOffset += delta; this.stopOffset = offset + len; this.stopPosition = stopPosition;
}
/** Get the current buffer */ publicchar[] getBuffer() { return buffer;
}
/** Get the current scanning offset */ publicint getOffset() { return offset;
}
/** Get start of token in scanned buffer. */ publicint getTokenOffset() { return offset - tokenLength;
}
/** Get length of token in scanned buffer. */ publicint getTokenLength() { return tokenLength;
}
/** Get the token-context-path of the returned token. */ public TokenContextPath getTokenContextPath() { return tokenContextPath;
}
public TokenID getSupposedTokenID() { return supposedTokenID;
}
/** Get the pre-scan which is a number *ofcharactersbetweenoffsetandtokenOffset. *Ifthere'snomorecharactersinthecurrentbuffer, *theanalyzerreturnsEOT,butitcanbeinastatewhen *therearealreadysomecharactersparsedattheendof *thecurrentbufferbutthetoken *isstillincompleteanditcannotbereturnedyet. *Thepre-scanvaluehelpstodeterminehowmanycharacters *fromtheendofthecurrentbuffershouldbepresent *atthebeginingofthenextbuffersothatthecurrent *incompletetokencanbereturnedasthefirsttoken *whenparsingthenextbuffer.
*/ publicint getPreScan() { return offset - tokenOffset;
}
/** Initialize the analyzer when scanning from the begining *ofthedocumentorwhenthestatestoredinsyntaxmark *isnullforsomereasonortoexplicitlyresettheanalyzer *totheinitialstate.Theoffsetsmustnotbetouchedbythismethod.
*/ publicvoid loadInitState() {
state = INIT;
}
/** Load valid mark state into the analyzer. Offsets *arealreadyinitializedwhenthismethodiscalled.Thismethod *mustgetthestatefromthemarkandsetittotheanalyzer.Then *itmustdecreasetokenOffsetbythepreScanstoredinthemarkstate. *@paramstateInfomarkstatetobeloadedintosyntax.Itmustbenon-nullvalue.
*/ publicvoid loadState(StateInfo stateInfo) {
state = stateInfo.getState();
tokenOffset -= stateInfo.getPreScan();
}
/** Store state of this analyzer into given mark state. */ publicvoid storeState(StateInfo stateInfo) {
stateInfo.setState(state);
stateInfo.setPreScan(getPreScan());
}
/** Compare state of this analyzer to given state info */ publicint compareState(StateInfo stateInfo) { if (stateInfo != null) { return ((stateInfo.getState() == state) && stateInfo.getPreScan() == getPreScan())
? EQUAL_STATE : DIFFERENT_STATE;
} else { return DIFFERENT_STATE;
}
}
/** Create state info appropriate for particular analyzer */ public StateInfo createStateInfo() { returnnew BaseStateInfo();
}
/** Get state name as string. It can be used for debugging purposes *bydeveloperofnewsyntaxanalyzer.Thestatesthatthisfunction *recognizescanincludeallconstantsusedinanalyzersothatitcan *beusedeverywhereinanalyzertoconvertnumberstomorepracticalstrings.
*/ public String getStateName(int stateNumber) { switch(stateNumber) { case INIT: return"INIT"; // NOI18N
default: return"Unknown state " + stateNumber; // NOI18N
}
}
/** Interface that stores two basic pieces of information about *thestateofthewholelexicalanalyzer-itsinternalstateandpreScan.
*/ publicinterface StateInfo {
/** Get the internal state */ publicint getState();
/** Store the internal state */ publicvoid setState(int state);
/** Get the preScan value */ publicint getPreScan();
/** Store the preScan value */ publicvoid setPreScan(int preScan);
}
/** Base implementation of the StateInfo interface */ publicstaticclass BaseStateInfo implements StateInfo {
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.