diff options
Diffstat (limited to 'xfa/fxfa/fm2js/cxfa_fmlexer.cpp')
-rw-r--r-- | xfa/fxfa/fm2js/cxfa_fmlexer.cpp | 192 |
1 files changed, 81 insertions, 111 deletions
diff --git a/xfa/fxfa/fm2js/cxfa_fmlexer.cpp b/xfa/fxfa/fm2js/cxfa_fmlexer.cpp index 5ae1225541..78e99a53e3 100644 --- a/xfa/fxfa/fm2js/cxfa_fmlexer.cpp +++ b/xfa/fxfa/fm2js/cxfa_fmlexer.cpp @@ -110,12 +110,13 @@ XFA_FM_TOKEN TokenizeIdentifier(const WideStringView& str) { } // namespace -CXFA_FMToken::CXFA_FMToken() : m_type(TOKreserver), m_line_num(1) {} +CXFA_FMToken::CXFA_FMToken(XFA_FM_TOKEN token) : m_type(token) {} -CXFA_FMToken::CXFA_FMToken(uint32_t line_num) - : m_type(TOKreserver), m_line_num(line_num) {} +CXFA_FMToken::CXFA_FMToken() : CXFA_FMToken(TOKreserver) {} -CXFA_FMToken::~CXFA_FMToken() {} +CXFA_FMToken::CXFA_FMToken(const CXFA_FMToken&) = default; + +CXFA_FMToken::~CXFA_FMToken() = default; #ifndef NDEBUG WideString CXFA_FMToken::ToDebugString() const { @@ -123,8 +124,6 @@ WideString CXFA_FMToken::ToDebugString() const { str += tokenStrings[m_type]; str += L", string = "; str += m_string; - str += L", line_num = "; - str += std::to_wstring(m_line_num).c_str(); return str; } #endif // NDEBUG @@ -137,21 +136,19 @@ CXFA_FMLexer::CXFA_FMLexer(const WideStringView& wsFormCalc) CXFA_FMLexer::~CXFA_FMLexer() {} -std::unique_ptr<CXFA_FMToken> CXFA_FMLexer::NextToken() { +CXFA_FMToken CXFA_FMLexer::NextToken() { if (m_lexer_error) - return nullptr; + return CXFA_FMToken(); - m_token = pdfium::MakeUnique<CXFA_FMToken>(m_current_line); while (m_cursor <= m_end && *m_cursor) { if (!IsFormCalcCharacter(*m_cursor)) { RaiseError(); - return nullptr; + return CXFA_FMToken(); } switch (*m_cursor) { case '\n': ++m_current_line; - m_token->m_line_num = m_current_line; ++m_cursor; break; case '\r': @@ -161,9 +158,7 @@ std::unique_ptr<CXFA_FMToken> CXFA_FMLexer::NextToken() { AdvanceForComment(); break; case '"': - m_token->m_type = TOKstring; - AdvanceForString(); - return std::move(m_token); + return AdvanceForString(); case '0': case '1': case '2': @@ -174,153 +169,125 @@ std::unique_ptr<CXFA_FMToken> CXFA_FMLexer::NextToken() { case '7': case '8': case '9': - m_token->m_type = TOKnumber; - AdvanceForNumber(); - return std::move(m_token); + return AdvanceForNumber(); case '=': ++m_cursor; - if (m_cursor > m_end) { - m_token->m_type = TOKassign; - return std::move(m_token); - } + if (m_cursor > m_end) + return CXFA_FMToken(TOKassign); if (!IsFormCalcCharacter(*m_cursor)) { RaiseError(); - return nullptr; + return CXFA_FMToken(); } if (*m_cursor == '=') { - m_token->m_type = TOKeq; ++m_cursor; - } else { - m_token->m_type = TOKassign; + return CXFA_FMToken(TOKeq); } - return std::move(m_token); + return CXFA_FMToken(TOKassign); case '<': ++m_cursor; - if (m_cursor > m_end) { - m_token->m_type = TOKlt; - return std::move(m_token); - } + if (m_cursor > m_end) + return CXFA_FMToken(TOKlt); if (!IsFormCalcCharacter(*m_cursor)) { RaiseError(); - return nullptr; + return CXFA_FMToken(); } if (*m_cursor == '=') { - m_token->m_type = TOKle; ++m_cursor; - } else if (*m_cursor == '>') { - m_token->m_type = TOKne; + return CXFA_FMToken(TOKle); + } + if (*m_cursor == '>') { ++m_cursor; - } else { - m_token->m_type = TOKlt; + return CXFA_FMToken(TOKne); } - return std::move(m_token); + return CXFA_FMToken(TOKlt); case '>': ++m_cursor; - if (m_cursor > m_end) { - m_token->m_type = TOKgt; - return std::move(m_token); - } + if (m_cursor > m_end) + return CXFA_FMToken(TOKgt); if (!IsFormCalcCharacter(*m_cursor)) { RaiseError(); - return nullptr; + return CXFA_FMToken(); } if (*m_cursor == '=') { - m_token->m_type = TOKge; ++m_cursor; - } else { - m_token->m_type = TOKgt; + return CXFA_FMToken(TOKge); } - return std::move(m_token); + return CXFA_FMToken(TOKgt); case ',': - m_token->m_type = TOKcomma; ++m_cursor; - return std::move(m_token); + return CXFA_FMToken(TOKcomma); case '(': - m_token->m_type = TOKlparen; ++m_cursor; - return std::move(m_token); + return CXFA_FMToken(TOKlparen); case ')': - m_token->m_type = TOKrparen; ++m_cursor; - return std::move(m_token); + return CXFA_FMToken(TOKrparen); case '[': - m_token->m_type = TOKlbracket; ++m_cursor; - return std::move(m_token); + return CXFA_FMToken(TOKlbracket); case ']': - m_token->m_type = TOKrbracket; ++m_cursor; - return std::move(m_token); + return CXFA_FMToken(TOKrbracket); case '&': ++m_cursor; - m_token->m_type = TOKand; - return std::move(m_token); + return CXFA_FMToken(TOKand); case '|': ++m_cursor; - m_token->m_type = TOKor; - return std::move(m_token); + return CXFA_FMToken(TOKor); case '+': ++m_cursor; - m_token->m_type = TOKplus; - return std::move(m_token); + return CXFA_FMToken(TOKplus); case '-': ++m_cursor; - m_token->m_type = TOKminus; - return std::move(m_token); + return CXFA_FMToken(TOKminus); case '*': ++m_cursor; - m_token->m_type = TOKmul; - return std::move(m_token); + return CXFA_FMToken(TOKmul); case '/': { ++m_cursor; - if (m_cursor > m_end) { - m_token->m_type = TOKdiv; - return std::move(m_token); - } + if (m_cursor > m_end) + return CXFA_FMToken(TOKdiv); if (!IsFormCalcCharacter(*m_cursor)) { RaiseError(); - return nullptr; - } - if (*m_cursor != '/') { - m_token->m_type = TOKdiv; - return std::move(m_token); + return CXFA_FMToken(); } + if (*m_cursor != '/') + return CXFA_FMToken(TOKdiv); + AdvanceForComment(); break; } case '.': ++m_cursor; - if (m_cursor > m_end) { - m_token->m_type = TOKdot; - return std::move(m_token); - } + if (m_cursor > m_end) + return CXFA_FMToken(TOKdot); if (!IsFormCalcCharacter(*m_cursor)) { RaiseError(); - return nullptr; + return CXFA_FMToken(); } if (*m_cursor == '.') { - m_token->m_type = TOKdotdot; ++m_cursor; - } else if (*m_cursor == '*') { - m_token->m_type = TOKdotstar; + return CXFA_FMToken(TOKdotdot); + } + if (*m_cursor == '*') { ++m_cursor; - } else if (*m_cursor == '#') { - m_token->m_type = TOKdotscream; + return CXFA_FMToken(TOKdotstar); + } + if (*m_cursor == '#') { ++m_cursor; - } else if (*m_cursor <= '9' && *m_cursor >= '0') { - m_token->m_type = TOKnumber; + return CXFA_FMToken(TOKdotscream); + } + if (*m_cursor <= '9' && *m_cursor >= '0') { --m_cursor; - AdvanceForNumber(); - } else { - m_token->m_type = TOKdot; + return AdvanceForNumber(); } - return std::move(m_token); + return CXFA_FMToken(TOKdot); default: if (IsWhitespaceCharacter(*m_cursor)) { ++m_cursor; @@ -328,35 +295,34 @@ std::unique_ptr<CXFA_FMToken> CXFA_FMLexer::NextToken() { } if (!IsInitialIdentifierCharacter(*m_cursor)) { RaiseError(); - return nullptr; + return CXFA_FMToken(); } - AdvanceForIdentifier(); - return std::move(m_token); + return AdvanceForIdentifier(); } } - - // If there isn't currently a token type then mark it EOF. - if (m_token->m_type == TOKreserver) - m_token->m_type = TOKeof; - return std::move(m_token); + return CXFA_FMToken(TOKeof); } -void CXFA_FMLexer::AdvanceForNumber() { +CXFA_FMToken CXFA_FMLexer::AdvanceForNumber() { // This will set end to the character after the end of the number. wchar_t* end = nullptr; if (m_cursor) wcstod(const_cast<wchar_t*>(m_cursor), &end); if (!end || FXSYS_iswalpha(*end)) { RaiseError(); - return; + return CXFA_FMToken(); } - m_token->m_string = + CXFA_FMToken token(TOKnumber); + token.m_string = WideStringView(m_cursor, static_cast<size_t>(end - m_cursor)); m_cursor = end; + return token; } -void CXFA_FMLexer::AdvanceForString() { +CXFA_FMToken CXFA_FMLexer::AdvanceForString() { + CXFA_FMToken token(TOKstring); + const wchar_t* start = m_cursor; ++m_cursor; while (m_cursor <= m_end && *m_cursor) { @@ -368,9 +334,9 @@ void CXFA_FMLexer::AdvanceForString() { ++m_cursor; // If the end of the input has been reached it was not escaped. if (m_cursor > m_end) { - m_token->m_string = + token.m_string = WideStringView(start, static_cast<size_t>(m_cursor - start)); - return; + return token; } // If the next character is not a " then the end of the string has been // found. @@ -378,8 +344,8 @@ void CXFA_FMLexer::AdvanceForString() { if (!IsFormCalcCharacter(*m_cursor)) break; - m_token->m_string = WideStringView(start, (m_cursor - start)); - return; + token.m_string = WideStringView(start, (m_cursor - start)); + return token; } } ++m_cursor; @@ -387,24 +353,28 @@ void CXFA_FMLexer::AdvanceForString() { // Didn't find the end of the string. RaiseError(); + return CXFA_FMToken(); } -void CXFA_FMLexer::AdvanceForIdentifier() { +CXFA_FMToken CXFA_FMLexer::AdvanceForIdentifier() { const wchar_t* start = m_cursor; ++m_cursor; while (m_cursor <= m_end && *m_cursor) { if (!IsFormCalcCharacter(*m_cursor)) { RaiseError(); - return; + return CXFA_FMToken(); } if (!IsIdentifierCharacter(*m_cursor)) break; ++m_cursor; } - m_token->m_string = + + WideStringView str = WideStringView(start, static_cast<size_t>(m_cursor - start)); - m_token->m_type = TokenizeIdentifier(m_token->m_string); + CXFA_FMToken token(TokenizeIdentifier(str)); + token.m_string = str; + return token; } void CXFA_FMLexer::AdvanceForComment() { |