-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.hpp
66 lines (51 loc) · 1.55 KB
/
token.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*! \file token.hpp
Defines the Token and TokenSequence types, and associated functions.
*/
#ifndef TOKEN_HPP
#define TOKEN_HPP
#include <deque>
#include <istream>
#include <complex>
typedef std::complex<double> ComplexNumber;
/*! \class Token
\brief Value class representing a token.
A token is a composition of a tag type and an optional string value.
*/
class Token {
public:
/*! \enum TokenType
\brief a public enum defining the possible token types.
*/
enum TokenType { OPEN, //< open tag, aka '('
CLOSE, //< close tag, aka ')'
STRING, //< string tag
OPENQUOTATION,
CLOSEQUOTATION
};
/// construct a token of type t (if string default to empty value)
Token(TokenType t);
/// contruct a token of type String with value
Token(const std::string & str);
/// return the type of the token
TokenType type() const;
/// return the token rendered as a string
std::string asString() const;
private:
TokenType m_type;
std::string value;
};
/*! \typedef TokenSequenceType
Define the token sequence using a std container. Any supporting
sequential access should do.
*/
typedef std::deque<Token> TokenSequenceType;
/*! \fn TokenSequenceType tokenize(std::istream & seq)
\brief Split a stream into a sequnce of tokens
\param seq the input character stream
\return The sequence of tokens
Split a stream into a sequnce of tokens where a token is one of
OPEN or CLOSE or any space-delimited string
Ignores any whitespace and comments (from any ";" to end-of-line).
*/
TokenSequenceType tokenize(std::istream & seq);
#endif