Skip to content

Commit

Permalink
Move the VT parser's parse state into instanced storage
Browse files Browse the repository at this point in the history
Fixes #3108
  • Loading branch information
DHowett committed Oct 8, 2019
1 parent 0691c21 commit 441b49d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/terminal/parser/stateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ StateMachine::StateMachine(IStateMachineEngine* const pEngine) :
// rgusParams Initialized below
_sOscNextChar(0),
_sOscParam(0),
_currRunLength(0)
_currRunLength(0),
_fProcessingIndividually(false)
{
ZeroMemory(_pwchOscStringBuffer, sizeof(_pwchOscStringBuffer));
ZeroMemory(_rgusParams, sizeof(_rgusParams));
Expand Down Expand Up @@ -1346,20 +1347,16 @@ void StateMachine::ProcessString(const wchar_t* const rgwch, const size_t cch)
_pwchSequenceStart = rgwch;
_currRunLength = 0;

// This should be static, because if one string starts a sequence, and the next finishes it,
// we want the partial sequence state to persist.
static bool s_fProcessIndividually = false;

for (size_t cchCharsRemaining = cch; cchCharsRemaining > 0; cchCharsRemaining--)
{
if (s_fProcessIndividually)
if (_fProcessingIndividually)
{
// If we're processing characters individually, send it to the state machine.
ProcessCharacter(*_pwchCurr);
_pwchCurr++;
if (_state == VTStates::Ground) // Then check if we're back at ground. If we are, the next character (pwchCurr)
{ // is the start of the next run of characters that might be printable.
s_fProcessIndividually = false;
_fProcessingIndividually = false;
_pwchSequenceStart = _pwchCurr;
_currRunLength = 0;
}
Expand All @@ -1371,13 +1368,13 @@ void StateMachine::ProcessString(const wchar_t* const rgwch, const size_t cch)
FAIL_FAST_IF(!(_pwchSequenceStart + _currRunLength <= rgwch + cch));
_pEngine->ActionPrintString(_pwchSequenceStart, _currRunLength); // ... print all the chars leading up to it as part of the run...
_trace.DispatchPrintRunTrace(_pwchSequenceStart, _currRunLength);
s_fProcessIndividually = true; // begin processing future characters individually...
_fProcessingIndividually = true; // begin processing future characters individually...
_currRunLength = 0;
_pwchSequenceStart = _pwchCurr;
ProcessCharacter(*_pwchCurr); // ... Then process the character individually.
if (_state == VTStates::Ground) // If the character took us right back to ground, start another run after it.
{
s_fProcessIndividually = false;
_fProcessingIndividually = false;
_pwchSequenceStart = _pwchCurr + 1;
_currRunLength = 0;
}
Expand All @@ -1391,13 +1388,13 @@ void StateMachine::ProcessString(const wchar_t* const rgwch, const size_t cch)
}

// If we're at the end of the string and have remaining un-printed characters,
if (!s_fProcessIndividually && _currRunLength > 0)
if (!_fProcessingIndividually && _currRunLength > 0)
{
// print the rest of the characters in the string
_pEngine->ActionPrintString(_pwchSequenceStart, _currRunLength);
_trace.DispatchPrintRunTrace(_pwchSequenceStart, _currRunLength);
}
else if (s_fProcessIndividually)
else if (_fProcessingIndividually)
{
// One of the "weird things" in VT input is the case of something like
// <kbd>alt+[</kbd>. In VT, that's encoded as `\x1b[`. However, that's
Expand Down
4 changes: 4 additions & 0 deletions src/terminal/parser/stateMachine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,9 @@ namespace Microsoft::Console::VirtualTerminal
const wchar_t* _pwchCurr;
const wchar_t* _pwchSequenceStart;
size_t _currRunLength;

// This is tracked per state machine instance so that separate calls to Process*
// can start and finish a sequence.
bool _fProcessingIndividually;
};
}

0 comments on commit 441b49d

Please sign in to comment.