tahoma2d/toonz/sources/include/ttokenizer.h

98 lines
2.1 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#ifndef TTOKENIZER_INCLUDED
#define TTOKENIZER_INCLUDED
#include "tcommon.h"
2016-04-15 17:11:23 +12:00
#ifdef _WIN32
2016-03-19 06:57:51 +13:00
#pragma warning(push)
#pragma warning(disable : 4251)
#endif
#undef DVAPI
#undef DVVAR
#ifdef TNZBASE_EXPORTS
#define DVAPI DV_EXPORT_API
#define DVVAR DV_EXPORT_VAR
#else
#define DVAPI DV_IMPORT_API
#define DVVAR DV_IMPORT_VAR
#endif
2016-06-15 18:43:10 +12:00
namespace TSyntax {
2016-03-19 06:57:51 +13:00
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class DVAPI Token {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
enum Type { None, Space, Ident, Number, Punct, Eos };
Token(int pos = 0) : m_text(""), m_type(None), m_pos(pos) {}
Token(int p0, int p1) : m_text(p1 - p0 + 1, ' '), m_type(Space), m_pos(p0) {}
Token(std::string text, Type type, int pos)
: m_text(text), m_type(type), m_pos(pos) {}
Type getType() const { return m_type; }
std::string getText() const { return m_text; }
int getIntValue() const;
double getDoubleValue() const;
int getPos() const { return m_pos; }
int getPos1() const { return m_pos + m_text.length() - 1; }
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
std::string m_text;
int m_pos;
Type m_type;
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class DVAPI Tokenizer {
std::string m_buffer;
std::vector<Token> m_tokens;
int m_index;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
Tokenizer();
Tokenizer(std::string buffer);
~Tokenizer();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void setBuffer(std::string buffer);
std::string getBuffer() const { return m_buffer; }
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int getTokenCount() const;
const Token &getToken(int index) const;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
//! get the token containing the pos-th character in the input string
Token getTokenFromPos(int pos) const;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
//! reset the token index. (set it to 0)
void reset();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
//! return the current token (possibly Eos)
const Token &getToken();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
//! same as getToken(), but post-increment the token index (if !eos())
Token nextToken();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
//! return true if the current token is the last one
bool eos() const;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
//! to read all the sequence:
//! while(!tokenizer.eos()) {token = tokenizer.nextToken();...}
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
} // namespace TSyntax
2016-03-19 06:57:51 +13:00
2016-04-15 17:11:23 +12:00
#ifdef _WIN32
2016-03-19 06:57:51 +13:00
#pragma warning(pop)
#endif
#endif