1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4// ============================================================
5//
6// StringLexer.hpp
7//
8
9
10//
11// Defines the StringLexer class
12//
13// ============================================================
14
15#ifndef __BINDER__STRING_LEXER_HPP__
16#define __BINDER__STRING_LEXER_HPP__
17
18#include "bindertypes.hpp"
19
20#define GO_IF_NOT_EXPECTED(expr, kRequiredLexemeType) \
21 if ((expr) != kRequiredLexemeType) \
22 { \
23 fIsValid = FALSE; \
24 goto Exit; \
25 }
26
27#define GO_IF_END_OR_NOT_EXPECTED(expr, kRequiredLexemeType) \
28 { \
29 LEXEME_TYPE kGotLexemeType = (expr); \
30 if (kGotLexemeType == LEXEME_TYPE_END_OF_STREAM) \
31 { \
32 goto Exit; \
33 } \
34 else \
35 { \
36 GO_IF_NOT_EXPECTED(kGotLexemeType, kRequiredLexemeType); \
37 } \
38 }
39
40namespace BINDER_SPACE
41{
42 class StringLexer
43 {
44 public:
45 typedef enum
46 {
47 LEXEME_TYPE_INVALID,
48 LEXEME_TYPE_EQUALS,
49 LEXEME_TYPE_COMMA,
50 LEXEME_TYPE_COLON,
51 LEXEME_TYPE_SEMICOLON,
52 LEXEME_TYPE_STRING,
53 LEXEME_TYPE_END_OF_STREAM
54 } LEXEME_TYPE;
55
56 inline StringLexer();
57 inline ~StringLexer();
58
59 inline void Init(SString &inputString, BOOL fSupportEscaping);
60
61 static inline BOOL IsWhitespace(WCHAR wcChar);
62 static inline BOOL IsEOS(WCHAR wcChar);
63 static inline BOOL IsQuoteCharacter(WCHAR wcChar);
64
65 virtual BOOL IsSeparatorChar(WCHAR wcChar) = NULL;
66 virtual LEXEME_TYPE GetLexemeType(WCHAR wcChar) = NULL;
67
68 protected:
69 static const WCHAR INVALID_CHARACTER = -1;
70
71 LEXEME_TYPE GetNextLexeme(SString &currentString, BOOL fPermitUnescapedQuotes = FALSE);
72
73 inline WCHAR PopCharacter(BOOL *pfIsEscaped);
74 inline void PushCharacter(WCHAR wcCurrentChar,
75 BOOL fIsEscaped);
76
77 inline WCHAR GetRawCharacter();
78 inline void PushRawCharacter();
79 inline WCHAR DecodeUTF16Character();
80 inline WCHAR GetNextCharacter(BOOL *pfIsEscaped);
81
82 inline WCHAR ParseUnicode();
83 LEXEME_TYPE ParseString(SString &currentString,
84 BOOL fPermitUnescapeQuotes);
85
86 void TrimTrailingWhiteSpaces(SString &currentString);
87
88 SString::Iterator m_cursor;
89 SString::Iterator m_end;
90
91 WCHAR m_wcCurrentChar;
92 BOOL m_fCurrentCharIsEscaped;
93 BOOL m_fSupportEscaping;
94 BOOL m_fReadRawCharacter;
95 };
96
97#include "stringlexer.inl"
98};
99
100#endif
101