Lexer in D

Jonathan M Davis jmdavisProg at gmx.com
Sun Mar 3 13:34:20 PST 2013


On Sunday, March 03, 2013 17:19:25 Namespace wrote:
> But hasn't each range an array internally?

Many do, but some don't. Take std.algorithm.ioto, or the ranges in std.random 
for instance. They're generative. And if all ranges had arrays underneat the 
hood, infinite ranges wouldn't be possible except for something like 
std.algorithm.cycle. Something like

struct Range
{
    @property int front() { return _i; }
    void popFront() {++i}
    @property bool empty { return _i != int.max; }

    private int _i;
}

would be a valid range. In the case of a lexer, you generate the next token on 
each popFront call, so you consume the range of characters that you're lexing 
as you go but don't need to allocate memory for more than the current token at 
a time.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list