forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringlexer.cpp
More file actions
151 lines (127 loc) · 4.27 KB
/
stringlexer.cpp
File metadata and controls
151 lines (127 loc) · 4.27 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ============================================================
//
// StringLexer.cpp
//
//
// Implements the StringLexer class
//
// ============================================================
#include "stringlexer.hpp"
#include "utils.hpp"
#include "ex.h"
namespace BINDER_SPACE
{
StringLexer::LEXEME_TYPE
StringLexer::GetNextLexeme(SString ¤tString, BOOL fPermitUnescapedQuotes)
{
BOOL fIsEscaped = FALSE;
WCHAR wcCurrentChar = INVALID_CHARACTER;
// Remove any white spaces
do
{
wcCurrentChar = PopCharacter(&fIsEscaped);
}
while (IsWhitespace(wcCurrentChar));
// Determine lexeme type
if (!fIsEscaped)
{
LEXEME_TYPE kLexemeType = GetLexemeType(wcCurrentChar);
if (kLexemeType != LEXEME_TYPE_STRING)
{
return kLexemeType;
}
}
// First character of string lexeme; push it back
PushCharacter(wcCurrentChar, fIsEscaped);
return ParseString(currentString, fPermitUnescapedQuotes);
}
StringLexer::LEXEME_TYPE
StringLexer::ParseString(SString ¤tString, BOOL fPermitUnescapedQuotes)
{
BOOL fIsFirstCharacter = TRUE;
WCHAR wcCurrentChar = INVALID_CHARACTER;
WCHAR wcOpeningQuote = INVALID_CHARACTER;
currentString.Clear();
// Read until we find another lexeme that's not a string character
for (;;)
{
BOOL fIsEscaped = FALSE;
wcCurrentChar = PopCharacter(&fIsEscaped);
if (wcCurrentChar == INVALID_CHARACTER)
{
// Found invalid character encoding
return LEXEME_TYPE_INVALID;
}
if (IsEOS(wcCurrentChar))
{
if (IsQuoteCharacter(wcOpeningQuote))
{
// EOS and unclosed quotes is an error
return LEXEME_TYPE_INVALID;
}
else
{
// Reached end of input and therefore of string
break;
}
}
if (fIsFirstCharacter)
{
fIsFirstCharacter = FALSE;
// If first character is quote, then record its quoteness
if (IsQuoteCharacter(wcCurrentChar))
{
wcOpeningQuote = wcCurrentChar;
continue;
}
}
if (wcCurrentChar == wcOpeningQuote)
{
// We've found the closing quote for a quoted string
break;
}
if (!fPermitUnescapedQuotes && !fIsEscaped && IsQuoteCharacter(wcCurrentChar) && !IsQuoteCharacter(wcOpeningQuote))
{
// Unescaped quotes in the middle of the string are an error
return LEXEME_TYPE_INVALID;
}
if (IsSeparatorChar(wcCurrentChar) && !IsQuoteCharacter(wcOpeningQuote) && !fIsEscaped)
{
// Unescaped separator char terminates the string
PushCharacter(wcCurrentChar, fIsEscaped);
break;
}
// Add character to current string
currentString.Append(wcCurrentChar);
}
if (!IsQuoteCharacter(wcOpeningQuote))
{
// Remove trailing white spaces from unquoted string
TrimTrailingWhiteSpaces(currentString);
}
return LEXEME_TYPE_STRING;
}
void StringLexer::TrimTrailingWhiteSpaces(SString ¤tString)
{
SString::Iterator begin = currentString.Begin();
SString::Iterator cursor = currentString.End() - 1;
BOOL fFoundWhiteSpace = FALSE;
for (;;)
{
if ((cursor >= begin) && IsWhitespace(cursor[0]))
{
fFoundWhiteSpace = TRUE;
cursor--;
continue;
}
break;
}
if (fFoundWhiteSpace)
{
currentString.Truncate(cursor + 1);
}
}
};