Skip to content
Snippets Groups Projects
RegExpFromStringLexer.cpp 2.49 KiB
Newer Older
  • Learn to ignore specific revisions
  • #include "RegExpFromStringLexer.h"
    
    namespace regexp {
    
    RegExpFromStringLexer::RegExpFromStringLexer(const std::string& in) : m_In(in) {
    	this->next();
    }
    
    RegExpFromStringLexer& RegExpFromStringLexer::next() {
    	char character;
    
    	m_Current.value = "";
    
    
    L0:
    	character = m_In.get();
    	if(m_In.eof()) {
    		m_Current.type = TEOF;
    		return *this;
    	} else if(character == ' ' || character == '\n' || character == '\t') {
    		goto L0;
    
    	} else if(character == '"') {
    		goto L3;
    
    	} else if((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (character >= '0' && character <= '9')) {
    		m_Current.type = SYMBOL;
    
    		m_Current.value += character;
    		goto L2;
    
    	} else if(character == '(') {
    		m_Current.type = LPAR;
    		return *this;
    	} else if(character == ')') {
    		m_Current.type = RPAR;
    		return *this;
    	} else if(character == '+') {
    		m_Current.type = PLUS;
    		return *this;
    	} else if(character == '*') {
    		m_Current.type = STAR;
    		return *this;
    	} else if(character == '\\') {
    		goto L1;
    	} else {
    		m_In.unget();
    		m_Current.type = ERROR;
    		return *this;
    	}
    L1:
    	character = m_In.get();
    	if(m_In.eof()) {
    		m_Current.type = ERROR;
    		return *this;
    	} else if(character == 'e') {
    		m_Current.type = EPS;
    		return *this;
    	} else if(character == '0') {
    		m_Current.type = EMPTY;
    		return *this;
    	} else {
    		m_In.unget();
    		m_Current.type = ERROR;
    		return *this;
    	}
    
    L2:
    	character = m_In.get();
    	if(m_In.eof()) {
    		return *this;
    	} else if((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (character >= '0' && character <= '9')) {
    		m_Current.value += character;
    		goto L2;
    	} else {
    		m_In.unget();
    		return *this;
    	}
    L3:
    	character = m_In.get();
    	if(m_In.eof()) {
    		m_Current.type = ERROR;
    		return *this;
    	} else if(character == '"') {
    		m_Current.type = EPS;
    		return *this;
    	} else if(character == '\\') {
    		m_Current.type = SYMBOL;
    		goto L5;
    	} else {
    		m_Current.type = SYMBOL;
    		m_Current.value += character;
    		goto L4;
    	}
    L4:
    	character = m_In.get();
    	if(m_In.eof()) {
    		m_Current.type = ERROR;
    		return *this;
    	} else if(character == '"') {
    		return *this;
    	} else if(character == '\\') {
    		goto L5;
    	} else {
    		m_Current.value += character;
    		goto L4;
    	}
    L5:
    	character = m_In.get();
    	if(m_In.eof()) {
    		m_Current.type = ERROR;
    		return *this;
    	} else if(character == '"' || character == '\\') {
    		m_Current.value += character;
    		goto L4;
    	} else {
    		m_Current.type = ERROR;
    		return *this;
    	} 
    
    }
    
    RegExpFromStringLexer::Token RegExpFromStringLexer::token() {
    
    	return std::move(m_Current);