summaryrefslogtreecommitdiff
path: root/src/binder/stringlexer.cpp
blob: 78c26dfee2bfa85dd12e60cc670fcf3180ebf518 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// ============================================================
//
// StringLexer.cpp
//


//
// Implements the StringLexer class
//
// ============================================================

#define DISABLE_BINDER_DEBUG_LOGGING

#include "stringlexer.hpp"
#include "utils.hpp"

#include "ex.h"

namespace BINDER_SPACE
{
    StringLexer::LEXEME_TYPE
    StringLexer::GetNextLexeme(SString &currentString, BOOL fPermitUnescapedQuotes)
    {
        BOOL fIsEscaped = FALSE;
        WCHAR wcCurrentChar = INVALID_CHARACTER;
        BINDER_LOG_ENTER(L"StringLexer::GetNextLexeme");

        // Remove any white spaces
        do
        {
            wcCurrentChar = PopCharacter(&fIsEscaped);
        }
        while (IsWhitespace(wcCurrentChar));

        // Determine lexeme type
        LEXEME_TYPE kLexemeType = LEXEME_TYPE_INVALID;
        if (!fIsEscaped)
        {
            kLexemeType = GetLexemeType(wcCurrentChar);

            if (kLexemeType != LEXEME_TYPE_STRING)
            {
                return kLexemeType;
            }
        }

        // First character of string lexeme; push it back
        PushCharacter(wcCurrentChar, fIsEscaped);
        kLexemeType = ParseString(currentString, fPermitUnescapedQuotes);
        if (kLexemeType == LEXEME_TYPE_STRING)
        {
            BINDER_LOG_LEAVE_HR(L"StringLexer::GetNextLexeme(LEXEME_TYPE_STRING)", S_OK);
        }
        else
        {
            BINDER_LOG_LEAVE_HR(L"StringLexer::GetNextLexeme(LEXEME_TYPE_INVALID)",
                                S_FALSE);
        }

        return kLexemeType;
    }

    StringLexer::LEXEME_TYPE
    StringLexer::ParseString(SString &currentString, 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
                BINDER_LOG(L"StringLexer::ParseString: Invalid character encoding");
                return LEXEME_TYPE_INVALID;
            }

            if (IsEOS(wcCurrentChar))
            {
                if (IsQuoteCharacter(wcOpeningQuote))
                {
                    // EOS and unclosed quotes is an error
                    BINDER_LOG(L"StringLexer::ParseString: EOS and unclosed quotes");
                    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
                BINDER_LOG(L"StringLexer::ParseString: Quote in the middle of a string");
                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
            BINDER_LOG(L"StringLexer::ParseString: Trimming string");
            TrimTrailingWhiteSpaces(currentString);
        }

        BINDER_LOG_STRING(L"string", currentString);

        return LEXEME_TYPE_STRING;
    }

    void StringLexer::TrimTrailingWhiteSpaces(SString &currentString)
    {
        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);
        }
    }
};