int grabInteger() { int result = 0; int sign = 1; char c; peekCharacter(c); if(c=='-') { passCharacter(); sign = -1; peekCharacter(c); }; if('0'<=c&&c<='9') { while('0'<=c && c<='9') { passCharacter(); result *=10; result += (c-'0'); if(eof()) break; peekCharacter(c); }; } else { GBStream << "Error: no integer found\n."; errorh(__LINE__); }; result *= sign; return result; };
simpleString getString(int & len,bool keep = true) { bool save = d_skipws; d_skipws = false; if(!d_word) { d_word = new char[1001]; } else if(strlen(d_word)<1000) { delete d_word; d_word = new char[10001]; }; char * t = d_word; char c; getCharacter(c); if(c!='\"') errorh(__LINE__); if(keep) { *t=c;++t;++len; }; peekCharacter(c); len = 0; while(c!='\"') { passCharacter(); *t = c;++t;++len; if(len>1000) errorh(__LINE__); peekCharacter(c); }; if(keep) { *t=c;++t;++len; }; passCharacter(); *t = '\0'; simpleString result(d_word); d_skipws = save; return result; };
simpleString getAlphaNumWord(int & len) { len = 0; if(!d_word) { d_word = new char[1001]; } else if(strlen(d_word)<1000) { delete d_word; d_word = new char[1001]; }; char c; char * t = d_word; peekCharacter(c); if(('a'<=c && c <= 'z') || ('A'<=c && c <= 'Z')) { while(('a'<=c && c <= 'z') || ('A'<=c && c <= 'Z') || ('0'<=c && c <= '9')) { passCharacter(); *t = c; ++t;++len; if(len>1000) errorh(__LINE__); if(eof()) break; peekCharacter(c); }; }; *t = '\0'; simpleString result(d_word); return result; };
void passString() { char c; getCharacter(c); if(c!='\"') errorh(__LINE__); peekCharacter(c); while(c!='\"') { passCharacter(); peekCharacter(c); }; passCharacter(); };
simpleString getUntilCharacterButNot(const char * avoids,int & len) { len = 0; if(!d_word) { d_word = new char[1001]; } else if(strlen(d_word)<1000) { delete d_word; d_word = new char[10001]; }; char * t = d_word; peekCharacter(*t); while(check(*t,avoids)) { passCharacter(); ++t;++len; if(len>1000) errorh(__LINE__); peekCharacter(*t); }; *t = '\0'; simpleString result(d_word); return result; };
simpleString getUntilCharacterButNot(char avoid) { int len = 0; if(!d_word) { d_word = new char[1001]; } else if(strlen(d_word)<1000) { delete d_word; d_word = new char[1001]; }; char * t = d_word; peekCharacter(*t); cout << "char:" << *t << endl; while(*t!=avoid) { cout << "char:" << *t << endl; passCharacter(); ++t;++len; if(len>1000) errorh(__LINE__); peekCharacter(*t,"\n\t "); }; *t = '\0'; simpleString result(d_word); return result; };
int getNumber() { int result = 0; int sign = 1; char c; peekCharacter(c); if(c=='-') { sign = -1; passCharacter(); peekCharacter(c); if(c=='+') { passCharacter(); peekCharacter(c); } else if(c=='-') { sign = 1; passCharacter(); peekCharacter(c); }; } else if(c=='+') { passCharacter(); peekCharacter(c); if(c=='+') { passCharacter(); peekCharacter(c); } else if(c=='-') { sign = -1; passCharacter(); peekCharacter(c); }; }; if('0'<=c&&c<='9') { while('0'<=c && c<='9') { passCharacter(); result *=10; result += (c-'0'); if(eof()) break; peekCharacter(c); }; } else { GBStream << "Error: no integer found"; GBStream << "Last character peeked:" << c << '\n'; errorh(__LINE__); }; result *= sign; return result; };
Token EBNFScanner::scanComment() { char currchar = getCurrentCharacter(); TokPosition pos = createPosition(); String content = String(&currchar, 1); while (hasNextCharacter()) { nextCharacter(); content.append(getCurrentCharacter()); if(getCurrentCharacter() == '*' && peekCharacter() == ')') { nextCharacter(); content.append(getCurrentCharacter()); break; } checkNewLine(); } return Token (TokComment, content, pos); }
Token EBNFScanner::scanOperator() { TokPosition pos = createPosition(); char currchar = getCurrentCharacter(); String content = String(&currchar, 1); char peak = peekCharacter(); if(getCurrentCharacter() == ':' && peak == ':') { nextCharacter(); content.append(getCurrentCharacter()); nextCharacter(); if(getCurrentCharacter() != '=') { raiseError (String("Expecting '=' but got '") + getCurrentCharacter() + "' instead."); content.append(getCurrentCharacter()); return Token (Token::TypeInvalid, content, pos); } content.append(getCurrentCharacter()); } return Token (TokOperator, content, pos); }
void getCharacter(char & c) { peekCharacter(c); passCharacter(); };
void getCharacter(char & c,const char * ignore) { peekCharacter(c,ignore); d_useUnget = false; };
void EBNFScanner::nextToken() { if(currentToken > -1 && currentToken < tokens.size() - 1) { currentToken++; return; } while (hasNextCharacter()) { nextCharacter(); // Scan the Identifier type. [a-zA-Z\-_] if(TokenScannerHelper::isAlpha(currentCharacter)) { Token tok = scanIdentifier(); tokens.append(std::move(tok)); currentToken++; return; } // Scan a literal else if (TokenScannerHelper::isQuote(currentCharacter)) { Token tok = scanLiteral(); tokens.append(std::move(tok)); currentToken++; return; } // Scan an operator Token. else if (TokenScannerHelper::isOperator(currentCharacter)) { if (currentCharacter == '(' && peekCharacter() == '*') { Token tok = scanComment(); tokens.append(std::move(tok)); currentToken++; return; } else { Token tok = scanOperator(); tokens.append(std::move(tok)); currentToken++; return; } } else if (TokenScannerHelper::isWhiteSpace(currentCharacter)) { // Ignore white spaces. } else { raiseError (String("Invalid Character.")); } checkNewLine(); } tokens.append(Token(Token::TypeEof, String(""), createPosition())); currentToken++; }