KSquare Utilities
Tokenizer.h
Go to the documentation of this file.
1 /* Tokenizer.h -- Class to Manage Token Parsing
2  * Copyright (C) 1994-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 
6 #ifndef _TOKENIZER_
7 #define _TOKENIZER_
8 /**
9  *@class KKB::Tokenizer
10  *@brief Class is meant to break down a stream into a set of logical tokens.
11  *@author Kurt Kramer
12  *@details This class was originally created while taking Non Linear Systems. It breaks up a source
13  * KKStr or text file into logical tokens. You can create your own source of characters by
14  * creating a Class derived from KKB::TokenBuffer.
15  */
16 
17 
18 #include <vector>
19 #include "TokenBuffer.h"
20 
21 namespace KKB
22 {
23  class Tokenizer
24  {
25  public:
27 
28  Tokenizer (const KKStr& _str);
29 
30  Tokenizer (const KKStr& _fileName,
31  bool& _fileOpened
32  );
33 
34  ~Tokenizer ();
35 
36 
37  void DefineOperatorChars (char* const _operatorChars);
38 
39  bool EndOfFile ();
40 
41  KKStrPtr GetNextToken ();
42 
43  /**
44  *@brief Returns a list of tokens up to and including the first occurrence of 'delToken'.
45  *@details Caller will take ownership of the returned tokens, and be responsible for
46  * deleting them.
47  */
48  KKStrListPtr GetNextTokens (const KKStr& delToken);
49 
50  KKStrConstPtr Peek (kkuint32 idx);
51 
52  void PushTokenOnFront (KKStrPtr t);
53 
54  KKStrConstPtr operator[](kkuint32 idx); /**< Returns pointers to following Tokens in the stream where idx==0 indicates the next token. */
55 
56 
57  private:
58  bool DelimiterChar (char c) const;
59  bool OperatorChar (char c) const;
60  KKStrPtr GetNextTokenRaw ();
61  char GetNextChar ();
62 
63  void Initialize ();
64  KKStrPtr ProcessOperatorToken ();
65  KKStrPtr ProcessStringToken (char strDelChar);
66  KKStrPtr ProcessFieldToken ();
67  void ReadInNextLogicalToken ();
68  bool WhiteSpaceChar (char c) const;
69 
70  bool atEndOfFile;
71  TokenBufferPtr in;
72  bool secondCharAtEndOfFile;
73 
74  char* operatorChars;
75 
76  kkint32 tokenListLen;
77  KKStrList tokenList; /**< @brief Will contain a fixed list of future tokens to read.
78  * As end of stream is approached will fill with end of file
79  * Tokens as a flag.
80  */
81 
82  bool weOwnTokenBuffer; /**< @brief Set to true indicates that we need to call the destructor on the TokenBuffer 'in' that we are processing. */
83 
84  char firstChar;
85  char secondChar;
86  }; /* Tokenizer */
87 
88 
90 }
91 
92 #endif
bool EndOfFile()
Definition: Tokenizer.cpp:175
__int32 kkint32
Definition: KKBaseTypes.h:88
KKStrConstPtr operator[](kkuint32 idx)
Definition: Tokenizer.cpp:419
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
Tokenizer(const KKStr &_fileName, bool &_fileOpened)
Definition: Tokenizer.cpp:52
KKTHread * KKTHreadPtr
void PushTokenOnFront(KKStrPtr t)
Definition: Tokenizer.cpp:154
KKStrPtr GetNextToken()
Definition: Tokenizer.cpp:116
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
KKStrConstPtr Peek(kkuint32 idx)
Definition: Tokenizer.cpp:162
Tokenizer(TokenBufferPtr _in)
Definition: Tokenizer.cpp:22
TokenBuffer * TokenBufferPtr
Definition: TokenBuffer.h:31
Class is meant to break down a stream into a set of logical tokens.
Definition: Tokenizer.h:23
Tokenizer(const KKStr &_str)
Definition: Tokenizer.cpp:36
KKStrListPtr GetNextTokens(const KKStr &delToken)
Returns a list of tokens up to and including the first occurrence of &#39;delToken&#39;.
Definition: Tokenizer.cpp:130
void DefineOperatorChars(char *const _operatorChars)
Definition: Tokenizer.cpp:109
Tokenizer * TokenizerPtr
Definition: Tokenizer.h:89