Created
March 4, 2011 15:19
-
-
Save mopemope/854744 to your computer and use it in GitHub Desktop.
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 so_tag.rl && gcc so_tag.c -o so_tag */ | |
#include <stdio.h> | |
#include <string.h> | |
static char *text_start; | |
%%{ | |
machine parser; | |
action MarkStart { text_start = fpc; } | |
action PrintTextNode { | |
int text_len = fpc - text_start; | |
if (text_len > 0) { | |
printf("TEXT(%.*s)\n", text_len, text_start); | |
} | |
} | |
action PrintTagNode { | |
//int text_len = fpc - text_start - 1; /* drop closing bracket */ | |
//printf("TAG(%.*s)\n", text_len, text_start); | |
} | |
action key { | |
int text_len = fpc - text_start; /* drop closing bracket */ | |
printf("TAG(%.*s)\n", text_len, text_start); | |
} | |
key = ([^\ \[\]r\0\n]+) >MarkStart %key; | |
tag = '[[' space* key space* ']]' @PrintTagNode; | |
main := ( | |
(any - '[')* >MarkStart %PrintTextNode | |
('[' ^'[' %PrintTextNode | tag) >MarkStart | |
//('[' ^'[' %PrintTextNode | tag) >MarkStart | |
)* @eof(PrintTextNode); | |
}%% | |
%% write data; | |
int | |
main(void) { | |
char buffer[4096]; | |
int cs; | |
char *p = NULL; | |
char *pe = NULL; | |
char *eof = NULL; | |
%% write init; | |
do { | |
size_t nread = fread(buffer, 1, sizeof(buffer), stdin); | |
p = buffer; | |
pe = p + nread; | |
if (nread < sizeof(buffer) && feof(stdin)) eof = pe; | |
%% write exec; | |
if (eof || cs == %%{ write error; }%%) break; | |
} while (1); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment