Lexer in D

Namespace rswhite4 at googlemail.com
Sat Mar 2 10:09:59 PST 2013


> No-no and no.
>
> Just translate list of keywords/types to a bunch of 
> switch-cases that will give you near optimal performance. Use 
> CTFE to construct that string of D code.
> In fact I proposed to do just 2 levels of switches: first 
> switch by length then by first char.

I don't understand.
You don't mean something like:

switch (value) {
case "if": isKeyword = true; break;
...

right?

> Even simple if you don't want to go for CTFE code-generation is 
> to make plain array of array. The length of array is exactly 
> 256 i.e.
>
> Even this should have decent speed:
>
> string[][256] keywords = [
> null,
> null,
> ...
> //at index 'i'
> ['int', 'if', 'invariant', 'idouble', 'ifloat' ...]
> //array of strings starting with char for each
> ...
> ];
>
> bool isKeyword(string value)
> {
> 	string[] toSearch = keywords[value[0]];
> 	return toSearch.canFind(value);
> }

Ok I will try it. Thank you.


More information about the Digitalmars-d-learn mailing list