adrian-thurston / ragel

Ragel State Machine Compiler
MIT License
524 stars 46 forks source link

[ragel] allow machines to be specified using comment blocks #7

Open adrian-thurston opened 4 years ago

adrian-thurston commented 4 years ago

This way we can add a ragel state machine to a C file without disrupting the C syntax. The resulting file can still be handled as a C (or whichever) language by the IDE.

http://www.colm.net/pipermail/ragel-users/2019-April/003622.html

olesenm commented 4 years ago

Cannot see how you could handle ragel machines that have c-comments in their dispatch actions. Or do you introduce a comment() builtin to emit comments for the target code?

adrian-thurston commented 4 years ago

That's a good point, you can't use them. Would be a limitation of the feature. Probably I would just point out the limitation in the docs, rather than add something to ragel to accommodate them.

milahu commented 2 years ago

Cannot see how you could handle ragel machines that have c-comments in their dispatch actions.

/* and */ could be escaped with a noop token → /\%* and *\%/

instead of comments, im currently using pseudo macro functions

#include <stdio.h>

RAGEL({
  // ragel block

  // set machine name
  machine some_machine;

  /*
    comment block
  */
})

// "write error;" evaluates to "some_machine_error"
if (state->rc == RAGEL(write error;)) {
  printf("error\n");
}

... but thats not valid C code. slightly more valid:

#include <stdio.h>

#ifdef RAGEL
  // ragel block

  // set machine name
  machine some_machine;

  /*
    comment block
  */
#endif

// "write error;" evaluates to "some_machine_error"
if (state->rc == /* %ragel write error; */) {
  printf("error\n");
}

prototype solution: add one more preprocessor https://github.com/milahu/ragel-from-comments

rofl0r commented 2 years ago

i've used the approach to embed build instructions in comments once but wasn't really satisfied with the solution, as i had to parse the code before the C preprocessor and so didn't get the opportunity to leverage preprocessor conditions. at one point i then realized i could use #pragma instead because the preprocessor leaves those lines in the context where they appeared, and the compiler ignores them (the project using this approach is https://github.com/rofl0r/rcb2 ). maybe this could be leveraged in some form for use in ragel, too.