# How to handle nested final result code in AT-command text?

I am writing a parser (a finite state machine to be more precise) for AT-commands and I don't see how to handle a particular corner case:

Here is the specifications of the modem communication: When the DTE sends an AT-command terminated by <CR> (like ATE0<CR>), the DCE will optionally answer an information text response between <CR><LF> characters and finally give the final result code (like <CR><LF>OK<CR><LF>).

My algorithm is like this:

do {
} while (! is_final_result_code(line))


Here is the corner case: Suppose I send the command to read an SMS message and that the information text response (i.e. the SMS text) itself contains the sequence <CR><LF>OK<CR><LF>.

Then my parser will think wrongly that the first <CR><LF> terminates the information text response while the next part is the final result code.

Do I miss an important information on how to handle this case properly ?