iash  v0.5
Library to provide a bash-like shell in an application
Tokenizer.h
1 /*
2  * Tokenizer.h
3  *
4  * Created on: Nov 22, 2016
5  * Author: paul
6  */
7 
8 #ifndef SRC_TOOLS_TOKENIZER_H_
9 #define SRC_TOOLS_TOKENIZER_H_
10 
11 #include <string>
12 #include <vector>
13 #include <exception>
14 #include <stdexcept>
15 
16 #include "Token.h"
17 
25 class Tokenizer {
26 public:
30  Tokenizer();
31 
39  Tokenizer(const std::string &raw);
40 
48  Tokenizer(const std::string &raw, const char delim);
49 
58  Tokenizer(const std::string &raw, const std::string &delims);
59 
67  Tokenizer(const Token &raw);
68 
76  Tokenizer(const Token &raw, const char delim);
77 
86  Tokenizer(const Token &raw, const std::string &delims);
87 
93  void setSource (const std::string &raw);
94 
98  void setDelims (const std::string &delims);
99 
106  const std::vector<Token>& getTokens () const;
107 
114  const Token& getToken (const size_t pos) const;
115 
122  size_t indexOf (const Token &token, const size_t start = 0) const;
123 private:
130  void limitDelims (const std::string &raw);
131 
137  void tokenize (std::string raw);
138 
139  std::vector<Token> m_tokens;
140  std::string m_delims;
141 };
142 
148 class TokenizeException : public std::runtime_error
149 {
150 public:
156  TokenizeException(const std::string &reason);
157 };
158 
159 #endif /* SRC_TOOLS_TOKENIZER_H_ */
Splits up a source string into Tokens based on the given delimiter(s).
Definition: Tokenizer.h:25
const std::vector< Token > & getTokens() const
Gets a reference to a vector of all of the tokens that were found in the source string.
Definition: Tokenizer.cpp:57
void setSource(const std::string &raw)
Sets the source string to tokenize and tokenizes it.
Definition: Tokenizer.cpp:47
size_t indexOf(const Token &token, const size_t start=0) const
Finds the index of the given Token in the token vector.
Definition: Tokenizer.cpp:67
Thrown when the tokenizer encounters trouble tokenizing a string.
Definition: Tokenizer.h:148
const Token & getToken(const size_t pos) const
Gets the Token at the given position in the token vector.
Definition: Tokenizer.cpp:62
Represents a single unit of a command language.
Definition: Token.h:23
Tokenizer()
Creates a Tokenizer with no initial settings.
Definition: Tokenizer.cpp:13
void setDelims(const std::string &delims)
Sets the delimiters to tokenize over.
Definition: Tokenizer.cpp:52