Pull Parsing and CQS

111 Views Asked by At

I'm creating a pull parser and it relies heavily on reading characters in a sequential way. The state of the parser changes as characters are read, so we can say there is a "context" that designates how next characters are interpreted.

The problem is that I usually find that there are some methods that should be queries, but they also modify the state.

For example, this method:

private void string ReadIdentifier() 
{
    string identifier = ReadUntilTheseCharsAreFound("=", ";");
    if (this.lastChar != "=")
    {
        this.state = States.ReadingProperty;
    } 
    else 
    {
        this.state = States.ReadingValue;
    }

    return identifier;
}

As you can see, not only does this retrieve the identifier, but it also changes the state. If the last character was a "=", the state is one, and if it was ";" the state is another.

This doesn't conform to the CQS principle and I don't like it very much :(

But given that it's a parser, maybe it's the best way to do it. So my question is: can you think of a better way to do this without breaking CQS?

Thanks!

0

There are 0 best solutions below