Skip to content
Snippets Groups Projects
Commit 1ad51d5e authored by Jan Trávníček's avatar Jan Trávníček
Browse files

String from string parser

parent d277855f
No related branches found
No related tags found
No related merge requests found
#include "StringFromStringLexer.h"
namespace string {
StringFromStringLexer::StringFromStringLexer(const std::string& in) : m_In(in) {
this->next();
}
StringFromStringLexer& StringFromStringLexer::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 {
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 = ERROR;
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;
}
}
StringFromStringLexer::Token StringFromStringLexer::token() {
return m_Current;
}
}
\ No newline at end of file
#ifndef STRING_FROM_STRING_LEXER_H_
#define STRING_FROM_STRING_LEXER_H_
#include <string>
#include <sstream>
namespace string {
class StringFromStringLexer {
public:
enum TokenType {
SYMBOL,
TEOF,
ERROR
};
struct Token {
TokenType type;
std::string value;
};
private:
std::stringstream m_In;
Token m_Current;
public:
StringFromStringLexer(const std::string&);
StringFromStringLexer& next();
Token token();
};
} /* namespace string */
#endif /* STRING_FROM_STRING_LEXER_H_ */
\ No newline at end of file
#include "StringFromStringParser.h"
#include "../AlibException.h"
#include "Epsilon.h"
namespace string {
StringFromStringParser::StringFromStringParser(const std::string& lexer) : m_Lexer(lexer) {
}
string::String StringFromStringParser::parse() {
StringFromStringLexer::Token token = m_Lexer.token();
if(token.type == StringFromStringLexer::TEOF) {
return Epsilon();
} else {
std::vector<alphabet::Symbol> data = parseContent();
return String(data);
}
}
std::vector<alphabet::Symbol> StringFromStringParser::parseContent() {
std::vector<alphabet::Symbol> data;
StringFromStringLexer::Token token = m_Lexer.token();
while(token.type == StringFromStringLexer::SYMBOL) {
std::string symbol = token.value;
m_Lexer.next();
data.push_back(alphabet::Symbol(symbol));
}
return data;
}
} /* namespace string */
/*
* StringFromStringParser.h
*
* Created on: Nov 23, 2013
* Author: Jan Travnicek
*/
#ifndef STRING_FROM_STRING_PARSER_H_
#define STRING_FROM_STRING_PARSER_H_
#include "String.h"
#include "StringFromStringLexer.h"
namespace string {
class StringFromStringParser {
std::vector<alphabet::Symbol> parseContent();
StringFromStringLexer m_Lexer;
public:
StringFromStringParser(const std::string&);
String parse();
};
} /* namespace string */
#endif /* STRING_FROM_STRING_PARSER_H_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment