Skip to content

Instantly share code, notes, and snippets.

@paoloambrosio
Last active December 23, 2015 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paoloambrosio/6675339 to your computer and use it in GitHub Desktop.
Save paoloambrosio/6675339 to your computer and use it in GitHub Desktop.
Minimal Ragel test for Gherkin 3 issue
name:S[1]N[24]\n SUCCESS parsing 'name: with_space_with_line_end
'
name:S[2]N[27] SUCCESS parsing 'name: with_space_without_line_end'
name:S[0]N[27]\n SUCCESS parsing 'name:without_space_with_line_end
'
#include <string.h>
#include <stdio.h>
%%{
machine foo;
main := |*
'name:' => { printf("name:"); fnext skip_leading_spaces; };
[\n] => { printf("\\n"); };
*|;
skip_leading_spaces := |*
[ ]* => { printf("S[%ld]", (te-ts)); fnext name; };
*|;
name := |*
[^\n]* => { printf("N[%ld]", (te-ts)); fnext main; };
*|;
}%%
%% write data;
int lex(char *str) {
char *p = str;
char *pe = str + strlen(p);
char *eof = pe, *te = 0, *ts = 0;
int act, cs;
%% write init;
%% write exec;
return p == eof;
}
void assertParsed(char *p) {
if (lex(p)) {
printf(" SUCCESS parsing '%s'\n", p);
} else {
printf(" FAILURE parsing '%s'\n", p);
}
}
int main() {
assertParsed("name: with_space_with_line_end\n");
assertParsed("name: with_space_without_line_end");
assertParsed("name:without_space_with_line_end\n");
}
$ ragel lexer.rl && gcc -o lexer lexer.c && ./lexer
name:S[1]N[24]\n SUCCESS parsing 'name: with_space_with_line_end
'
name:S[2]N[27] SUCCESS parsing 'name: with_space_without_line_end'
name: FAILURE parsing 'name:without_space_with_line_end
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment