/** * * Is a virtual machine for parsing. It has some ideas * drawn from the sed tool. * * @author http://bumble.sf.net */ // good examples // http://www.josuttis.com/libbook/i18n/loc1.cpp.html #if !defined (_MACHINE_H) #define _MACHINE_H_ #include #include #include #include #include #include #include #include "Tape.h" using namespace std; class Machine { private: //-------------------------------------------- vector tokenstack; //-------------------------------------------- Tape tape; //-------------------------------------------- /* where the stack and tape are used */ string workarea; //-------------------------------------------- /* the next char of the input stream */ char peep; //-------------------------------------------- string lastOperation; //-------------------------------------------- bool stackChanged; public: //-------------------------------------------- Machine(); //-------------------------------------------- string getWorkspace(); //-------------------------------------------- string workspace(); //-------------------------------------------- void peek(); //-------------------------------------------- bool stackFlag(); //-------------------------------------------- void setFlag(); //-------------------------------------------- void resetFlag(); //-------------------------------------------- bool peek(string sTest); //-------------------------------------------- /* reads while the peep is */ void readWhile(istream& inputstream, string sWhile); //-------------------------------------------- /* reads while the peep is not */ void readWhileNot(istream& inputstream, string sWhileNot); //-------------------------------------------- /* reads the next character from the input stream */ void readNext(istream& inputstream); //-------------------------------------------- /* llegir fins que el accumulador termini amb la cadena */ void readUntil(istream& inputstream, string sTerminator); //-------------------------------------------- void readUntil(istream& inputstream, string sTerminator, string sNotTerminator); //-------------------------------------------- /* si el espai d'accumulacio termina amb */ bool endsWith(string sFinal); //-------------------------------------------- /* si el espai d'accumulacio commenca amb */ bool beginsWith(string sBegin); //-------------------------------------------- /** this method may not be necessary, can check with pops */ int stacksize(); //-------------------------------------------- bool matches(string sTest); //-------------------------------------------- /* determines if the workspace is a space character */ bool isSpace(); //-------------------------------------------- /* determines if the workspace is a digit character */ bool isDigit(); //-------------------------------------------- /* determines if the workspace is a letter character */ bool isLetter(); //-------------------------------------------- bool isUnicode(); //-------------------------------------------- /* to allow simple pattern testing for literal values */ bool workspaceInRange(char cStart, char cEnd); //-------------------------------------------- /* to allow simple pattern testing for literal values */ bool matches(char cStart, char cEnd); //-------------------------------------------- /** decrements the pointer to the tape by one */ void decrementTape(); //-------------------------------------------- /** increments the pointer to the tape by one */ void incrementTape(); //-------------------------------------------- /** puts the workspace into the current item of the tape. * The workspace is not changed */ void put(); //-------------------------------------------- /** gets the current item of the tape and adds it to * the end of the workspace */ void get(); //-------------------------------------------- /** inserts the last item of the stack at the front of the workspace, * adding a bar character "|" to separate the token. If the * stack is empty, there is no change to the machine. */ void pop(); //-------------------------------------------- string replaceText(string sOriginal, string sReplace, string sReplacement); //-------------------------------------------- void replace(string sReplace, string sReplacement); //-------------------------------------------- /** pushes the contents of the workspace onto the stack. * The first token on the workspace is deleted. */ void push(); //-------------------------------------------- /** prints the workspace to standard out */ void print(); //-------------------------------------------- /** adds a piece of text to the end of the workspace */ void add(string sText); //-------------------------------------------- /** clears the workspace */ void clear(); //-------------------------------------------- /** anyade una nueva linea al espacio */ void newline(); //-------------------------------------------- /** indents each line of the workspace, which may * be useful for print code fragments */ void indent(); //-------------------------------------------- /** */ void clip(); //-------------------------------------------- string toString(); //-------------------------------------------- string printStack(); //-------------------------------------------- /** returns a description of the current state of the machine, displays the * contents of the stack, tape and the workspace. */ string printState(); }; //-- class definition #endif