[ragel-users] getNextToken() : how to return ?

M Conrad silverdirk-rgl at silverdirk.com
Thu Jan 5 18:13:37 UTC 2012


> On 5 January 2012 16:20, Maƫl Nison <nison.mael at gmail.com> wrote:
>> I would like to write a very basic function taking a c++ char const *
>> &, and returning the first token type.
>> The reference would imply that the cstring pointer's target is, after
>> the function call, the very next character of the returned token.
...
>> I've tried to use something like that (example, probably does not
>> compile),  but the value of 'p' is not modified when returning
>> (infinite loop) :

"p" works exactly like you expect it should.  i.e. it is always
pointing to the next character that will be consumed by the state
machine.  If "p" didn't get modified, it means the state machine
didn't consume any characters.

> It seems to work if I manually set p = te; just before returning, but I
> don't really know if it's a good practice.

The only reason to modify "p" is if you intend to skip characters.

>>   %%{
>>     foo = "foo";
>>     main := |* foo => { return Foo; }; *|;
>>   }%%;

I've never tried "return" from inside the action code... that is
likely your problem.  Try "tok= Foo; fbreak;" (and define a local
var named "tok")

Also, since "p" is used quite often in the generated code, you
might consider the following, so that the compiler can optimize
it into a register:

     int getNextToken( const char **pos ) {
       int tok= -1;
       const char* p= *pos;
       ...
       ...
       ...
       *pos= p;
       return tok;
     }

Whether to use references or pointers is a style preference, but
with a pointer you make it clear to the caller that they need to
pass an lvalue.

Also, since you're doing this in C++, you might consider wrapping
it all in a class and using fields instead of a parameter.

-Mike

_______________________________________________
ragel-users mailing list
ragel-users at complang.org
http://www.complang.org/mailman/listinfo/ragel-users


More information about the ragel-users mailing list