Last active
December 23, 2015 18:19
-
-
Save paoloambrosio/6675339 to your computer and use it in GitHub Desktop.
Minimal Ragel test for Gherkin 3 issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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