Hi HS! > Le 28 févr. 2019 à 07:32, H. S. Teoh <[email protected]> a écrit : > > On Tue, Feb 26, 2019 at 06:33:55PM +0100, Akim Demaille wrote: > [...] >> What I did below is quite ugly. In particular, I don't know how to >> write a decent scanner in D. What I did is truly scary, a way to >> force C code (with gets and ungetc) into my zero knowledge of D. What >> is the right way to do the following? > [...] > > ungetc is a truly nasty hack of an API in C; is it really necessary to > support that?
Certainly not! I'm merely trying to get a scanner to feed our parsers with tokens, and mimicking the C API is all my knowledge of D allows me to do... > D supports a range API that lets you query the front of a > range (in this case, a stream of chars) without moving the current > position of the stream. So ungetc really shouldn't be necessary unless > it's an inextricable part of the Bison-generated parser. It has nothing to do with Bison. Bison generates parsers that pull tokens from the scanner. Tokens can be chars, but it should be avoided IMHO: tokens are, well, tokens (terminal symbols coded as ints for efficiency). getc, ungetc are just one (common) way to write *scanners* in C. And I need a simple scanner for our example, and our test suite. > What I'd do is to templatize CalcLexer on an arbitrary input range of > chars, and leave the specifics of binding to a File (or whatever else, > like a string in a unittest) to the caller. And I wouldn't bother with > using class inheritance at all, since I can't envision we'd ever need to > swap in multiple lexers to the same parser. So something like this: This is already quite advanced compared to the current state of the D skeleton. What I'm looking for is a simple scanner that works with what we have. For instance below I tried to import your suggestion into examples/d/calc.y, but it does not work. Could you help me make it work? Just put it in examples/d/ and run "make check". GEN examples/d/calc.d GEN examples/d/calc /opt/local/include/phobos/std/algorithm/iteration.d(475): Error: `this.__lambda11` has no value examples/d/calc.d(920): Error: CTFE failed because of previous errors in `map` examples/d/calc.d(921): called from here: `joiner(map(makeGlobal().byChunk(1024LU)))` Cheers!
calc.y
Description: Binary data
