TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TextInput.h
Go to the documentation of this file.
1 
17 #ifndef G3D_TextInput_h
18 #define G3D_TextInput_h
19 
20 #include "G3D/platform.h"
21 #include "G3D/Array.h"
22 #include "G3D/Set.h"
23 #include "G3D/ParseError.h"
24 #include <string>
25 #include <queue>
26 #include <ctype.h>
27 #include <stdio.h>
28 
29 namespace G3D {
30 
34 class Token {
35 public:
36 
40  enum ExtendedType {
51  };
52 
56  enum Type {
64  };
65 
66 private:
67 
68  friend class TextInput;
69 
74  std::string _string;
75 
76  bool _bool;
77  int _line;
82 
83 public:
84 
85  Token() :
86  _string(""),
87  _bool(false),
88  _line(0),
89  _character(0),
90  _bytePosition(0),
91  _type(END),
92  _extendedType(END_TYPE) {}
93 
94  Token(Type t, ExtendedType e, const std::string& s, int L, int c, uint64 byte)
95  : _string(s), _bool(false), _line(L), _character(c), _bytePosition(byte), _type(t), _extendedType(e) {}
96 
97  Token(Type t, ExtendedType e, const std::string& s, bool b, int L, int c, uint64 byte)
98  : _string(s), _bool(b), _line(L), _character(c), _bytePosition(byte), _type(t), _extendedType(e) {}
99 
100  Type type() const {
101  return _type;
102  }
103 
105  return _extendedType;
106  }
107 
113  const std::string& string() const {
114  return _string;
115  }
116 
117  bool boolean() const {
118  return _bool;
119  }
120 
125  int line() const {
126  return _line;
127  }
128 
133  int character() const {
134  return _character;
135  }
136 
140  return _bytePosition;
141  }
142 
146  double number() const;
147 };
148 
149 
223 class TextInput {
224 public:
226  static double parseNumber(const std::string& _string);
227 
229  static bool parseBoolean(const std::string& _string);
230 
231 
233  class Settings {
234  public:
242 
250 
260 
270 
280 
285 
294 
302 
306 
312 
317 
325  std::string sourceFileName;
326 
327 
332 
365 
369 
386 
392 
399 
402 
403  Settings();
404  };
405 
406 private:
407 
410 
411  std::deque<Token> stack;
412 
417 
423 
433 
443 
447 
448  void init();
449 
461  int eatInputChar();
462 
472  int peekInputChar(int distance = 0);
473 
478  inline int eatAndPeekInputChar() {
479  eatInputChar();
480  return peekInputChar(0);
481  }
482 
487  void nextToken(Token& t);
488 
496  void parseQuotedString(unsigned char delimiter, Token& t);
497 
498  void initFromString(const char* str, int len, const Settings& settings);
499 
500 public:
501 
502  class TokenException : public ParseError {
503  public:
507  std::string sourceFile;
508 
509  virtual ~TokenException() {}
510 
511  protected:
512 
514  const std::string& src,
515  int ln,
516  int ch);
517 
518  };
519 
523  public:
524 
526  const std::string& src,
527  int ln,
528  int ch);
529  };
530 
533  public:
536 
538  const std::string& src,
539  int ln,
540  int ch,
541  Token::Type e,
542  Token::Type a);
543  };
544 
545  class WrongSymbol : public TokenException {
546  public:
547  std::string expected;
548  std::string actual;
549 
550  WrongSymbol(
551  const std::string& src,
552  int ln,
553  int ch,
554  const std::string& e,
555  const std::string& a);
556  };
557 
558 
560  class WrongString : public TokenException {
561  public:
562  std::string expected;
563  std::string actual;
564 
565  WrongString(
566  const std::string& src,
567  int ln,
568  int ch,
569  const std::string& e,
570  const std::string& a);
571  };
572 
573  TextInput(const std::string& filename, const Settings& settings = Settings());
574 
575  enum FS {FROM_STRING};
579  TextInput(FS fs, const std::string& str, const Settings& settings = Settings());
580 
584  TextInput(FS fs, const char* str, size_t strLen, const Settings& settings = Settings());
585 
587  bool hasMore();
588 
591  void pushSettings(const Settings& settings) {
592  settingsStack.push(options);
593  options = settings;
594  }
595 
596  void popSettings() {
597  options = settingsStack.pop();
598  }
599 
615  Token read();
616 
618  void read(Token& t);
619 
622 
637  double readNumber();
638 
641  int readInteger();
642 
643  bool readBoolean();
644 
655 
662  std::string readString();
663 
680  void readString(const std::string& s);
681 
686  std::string readUntilNewlineAsString();
687 
692  std::string readUntilDelimiterAsString(const char delimiter1, const char delimiter2 = '\0');
693 
704 
711  std::string readComment();
712 
727  void readComment(const std::string& s);
728 
739 
746  std::string readNewline();
747 
762  void readNewline(const std::string& s);
763 
774 
776  void readSymbolToken(Token& t);
777 
784  std::string readSymbol();
785 
800  void readSymbol(const std::string& symbol);
801 
802 
804  void readSymbols(const std::string& s1, const std::string& s2) {
805  readSymbol(s1);
806  readSymbol(s2);
807  }
808 
811  const std::string& s1,
812  const std::string& s2,
813  const std::string& s3) {
814  readSymbol(s1);
815  readSymbol(s2);
816  readSymbol(s3);
817  }
818 
821  const std::string& s1,
822  const std::string& s2,
823  const std::string& s3,
824  const std::string& s4) {
825  readSymbol(s1);
826  readSymbol(s2);
827  readSymbol(s3);
828  readSymbol(s4);
829  }
830 
834  Token peek();
835 
837  int peekLineNumber();
838 
842  int peekCharacterNumber();
843 
850  void push(const Token& t);
851 
856  const std::string& filename() const;
857 };
858 
859 void deserialize(bool& b, TextInput& ti);
860 void deserialize(int& b, TextInput& ti);
861 void deserialize(uint8& b, TextInput& ti);
862 void deserialize(double& b, TextInput& ti);
863 void deserialize(float& b, TextInput& ti);
864 void deserialize(std::string& b, TextInput& ti);
865 
866 } // namespace
867 
868 #endif
869 
FS
Definition: TextInput.h:575
Definition: TextInput.h:61
Definition: TextInput.h:46
Set< std::string > trueSymbols
Definition: TextInput.h:398
void parseQuotedString(unsigned char delimiter, Token &t)
Definition: TextInput.cpp:882
void push(const T &value)
Definition: Array.h:770
Definition: TextInput.h:62
int peekLineNumber()
Definition: TextInput.cpp:110
Definition: TextInput.h:57
T pop(bool shrinkUnderlyingArrayIfNecessary=true)
Definition: Array.h:837
Definition: TextInput.h:58
bool caseSensitive
Definition: TextInput.h:391
std::string sourceFile
Definition: TextInput.h:507
int eatInputChar()
Definition: TextInput.cpp:217
bool proofSymbols
Definition: TextInput.h:385
char otherCommentCharacter2
Definition: TextInput.h:284
Definition: TextInput.h:45
std::string readNewline()
Definition: TextInput.cpp:1130
std::deque< Token > stack
Definition: TextInput.h:411
const std::string & filename() const
Definition: TextInput.cpp:1238
void readSymbols(const std::string &s1, const std::string &s2)
Definition: TextInput.h:804
char otherCommentCharacter
Definition: TextInput.h:279
int peekCharacterNumber()
Definition: TextInput.cpp:115
Token readSymbolToken()
Definition: TextInput.cpp:1147
Definition: TextInput.h:560
Definition: TextInput.h:50
int _character
Definition: TextInput.h:78
Token(Type t, ExtendedType e, const std::string &s, int L, int c, uint64 byte)
Definition: TextInput.h:94
bool boolean() const
Definition: TextInput.h:117
Definition: TextInput.h:63
Token peek()
Definition: TextInput.cpp:99
WrongTokenType(const std::string &src, int ln, int ch, Token::Type e, Token::Type a)
Definition: TextInput.cpp:1271
Definition: TextInput.h:43
Token readStringToken()
Definition: TextInput.cpp:1054
Definition: AABox.h:25
int line() const
Definition: TextInput.h:125
Dynamic 1D array tuned for performance.
Definition: Array.h:95
bool escapeSequencesInStrings
Definition: TextInput.h:269
ExtendedType extendedType() const
Definition: TextInput.h:104
virtual ~TokenException()
Definition: TextInput.h:509
Set< std::string > falseSymbols
Definition: TextInput.h:401
#define false
Definition: CascPort.h:18
ExtendedType _extendedType
Definition: TextInput.h:81
Definition: TextInput.h:48
void deserialize(std::string &s, BinaryInput &b)
Definition: serialize.h:16
uint64_t uint64
Definition: g3dmath.h:170
ExtendedType
Definition: TextInput.h:40
bool cppBlockComments
Definition: TextInput.h:241
uint64 _bytePosition
Definition: TextInput.h:79
Token()
Definition: TextInput.h:85
int startingLineNumberOffset
Definition: TextInput.h:331
const std::string & string() const
Definition: TextInput.h:113
void init()
Definition: TextInput.cpp:194
#define byte(x, n)
Definition: tomcrypt_macros.h:419
double distance(double x, double y)
Definition: g3dmath.h:731
Definition: TextInput.h:60
void readSymbols(const std::string &s1, const std::string &s2, const std::string &s3, const std::string &s4)
Definition: TextInput.h:820
std::string readString()
Definition: TextInput.cpp:1067
int _line
Definition: TextInput.h:77
bool msvcFloatSpecials
Definition: TextInput.h:364
std::string readUntilNewlineAsString()
Definition: TextInput.cpp:180
std::string sourceFileName
Definition: TextInput.h:325
TokenException(const std::string &src, int ln, int ch)
Definition: TextInput.cpp:1244
int character() const
Definition: TextInput.h:133
bool otherLineComments
Definition: TextInput.h:259
Definition: TextInput.h:47
Definition: TextInput.h:34
Definition: TextInput.h:522
void nextToken(Token &t)
Definition: TextInput.cpp:263
int charNumber
Definition: TextInput.h:442
double readNumber()
Definition: TextInput.cpp:1014
Definition: TextInput.h:575
Definition: TextInput.h:59
char singleQuoteCharacter
Definition: TextInput.h:316
bool _bool
Definition: TextInput.h:76
void pushSettings(const Settings &settings)
Definition: TextInput.h:591
std::string readUntilDelimiterAsString(const char delimiter1, const char delimiter2= '\0')
Definition: TextInput.cpp:137
bool readBoolean()
Definition: TextInput.cpp:957
Definition: TextInput.h:545
A simple tokenizer for parsing text files.
Definition: TextInput.h:223
WrongString(const std::string &src, int ln, int ch, const std::string &e, const std::string &a)
Definition: TextInput.cpp:1305
Definition: TextInput.h:233
Array< char > buffer
Definition: TextInput.h:416
BadMSVCSpecial(const std::string &src, int ln, int ch)
Definition: TextInput.cpp:1284
uint64 bytePosition() const
Definition: TextInput.h:139
Array< Settings > settingsStack
Definition: TextInput.h:409
Settings options
Definition: TextInput.h:446
Definition: TextInput.h:502
void initFromString(const char *str, int len, const Settings &settings)
Definition: TextInput.cpp:1211
bool generateCommentTokens
Definition: TextInput.h:293
Token readCommentToken()
Definition: TextInput.cpp:1085
Definition: TextInput.h:41
std::string _string
Definition: TextInput.h:74
Type type() const
Definition: TextInput.h:100
std::string expected
Definition: TextInput.h:562
TextInput(const std::string &filename, const Settings &settings=Settings())
Definition: TextInput.cpp:1186
static double parseNumber(const std::string &_string)
Definition: TextInput.cpp:46
Definition: ParseError.h:22
Definition: TextInput.h:42
Token readNewlineToken()
Definition: TextInput.cpp:1117
Definition: TextInput.h:532
int peekInputChar(int distance=0)
Definition: TextInput.cpp:252
void readSymbols(const std::string &s1, const std::string &s2, const std::string &s3)
Definition: TextInput.h:810
uint8_t uint8
Definition: Define.h:152
Token readSignificant()
Definition: TextInput.cpp:24
Token(Type t, ExtendedType e, const std::string &s, bool b, int L, int c, uint64 byte)
Definition: TextInput.h:97
bool singleQuotedStrings
Definition: TextInput.h:311
void push(const Token &t)
Definition: TextInput.cpp:207
std::string actual
Definition: TextInput.h:548
double number() const
Definition: TextInput.cpp:33
Token::Type expected
Definition: TextInput.h:534
bool generateNewlineTokens
Definition: TextInput.h:301
Token read()
Definition: TextInput.cpp:120
bool cppLineComments
Definition: TextInput.h:249
Type _type
Definition: TextInput.h:80
bool simpleFloatSpecials
Definition: TextInput.h:368
Settings()
Definition: TextInput.cpp:75
int currentCharOffset
Definition: TextInput.h:422
std::string readSymbol()
Definition: TextInput.cpp:1167
std::string expected
Definition: TextInput.h:547
void popSettings()
Definition: TextInput.h:596
std::string actual
Definition: TextInput.h:563
Definition: TextInput.h:49
Type
Definition: TextInput.h:56
int eatAndPeekInputChar()
Definition: TextInput.h:478
static bool parseBoolean(const std::string &_string)
Definition: TextInput.cpp:42
int lineNumber
Definition: TextInput.h:432
std::string readComment()
Definition: TextInput.cpp:1099
Token::Type actual
Definition: TextInput.h:535
int readInteger()
Definition: TextInput.cpp:973
bool hasMore()
Definition: TextInput.cpp:212
Definition: TextInput.h:44
WrongSymbol(const std::string &src, int ln, int ch, const std::string &e, const std::string &a)
Definition: TextInput.cpp:1292
bool signedNumbers
Definition: TextInput.h:305