1 // bfpp.rl written by Boris 'billiob' Faure billiob<att>gmail<dott>com
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_BFPP_PARSER_H
5 #define OHCOUNT_BFPP_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *BFPP_LANG = LANG_BFPP;
12 // the languages entities
13 const char *bfpp_entities[] = {
14 "space", "comment", "operator", "include"
17 // constants associated with the entities
19 BFPP_SPACE = 0, BFPP_COMMENT, BFPP_OPERATOR, BFPP_INCLUDE
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action bfpp_ccallback {
44 std_internal_newline(BFPP_LANG)
47 std_newline(BFPP_LANG)
51 bfpp_operator = [+\-<>.,\[\]\%\!\#\^\:\;] @code;
53 bfpp_line_comment = ('=') @comment nonnewline*;
55 bfpp_include = '@include(' @code (
56 newline %{ entity = INTERNAL_NL; } %bfpp_ccallback
60 (nonnewline - ws) @code
64 spaces ${ entity = BFPP_SPACE; } => bfpp_ccallback;
65 newline ${ entity = NEWLINE; } => bfpp_ccallback;
68 bfpp_operator ${ entity = BFPP_OPERATOR; } => bfpp_ccallback;
69 ^space ${ entity = BFPP_COMMENT; } => bfpp_ccallback;
74 action bfpp_ecallback {
75 callback(BFPP_LANG, bfpp_entities[entity], cint(ts), cint(te), userdata);
78 bfpp_operator_entity = [+\-<>.,\[\]%\#^;:];
80 bfpp_include_entity = '@include(' any* :>> ')';
82 bfpp_line_comment_entity = '=' (escaped_newline | nonnewline)*;
84 bfpp_comment_entity = (bfpp_line_comment_entity |
85 !(space | bfpp_operator_entity | bfpp_include_entity));
88 space+ ${ entity = BFPP_SPACE; } => bfpp_ecallback;
89 bfpp_operator_entity ${ entity = BFPP_OPERATOR; } => bfpp_ecallback;
90 bfpp_include_entity ${ entity = BFPP_INCLUDE; } => bfpp_ecallback;
91 bfpp_comment_entity ${ entity = BFPP_COMMENT; } => bfpp_ecallback;
95 /************************* Required for every parser *************************/
97 /* Parses a string buffer with Brainfuck++ code.
99 * @param *buffer The string to parse.
100 * @param length The length of the string to parse.
101 * @param count Integer flag specifying whether or not to count lines. If yes,
102 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
103 * machine optimized for returning entity positions.
104 * @param *callback Callback function. If count is set, callback is called for
105 * every line of code, comment, or blank with 'lcode', 'lcomment', and
106 * 'lblank' respectively. Otherwise callback is called for each entity found.
108 void parse_bfpp(char *buffer, int length, int count,
109 void (*callback) (const char *lang, const char *entity, int s,
116 cs = (count) ? bfpp_en_bfpp_line : bfpp_en_bfpp_entity;
119 // if no newline at EOF; callback contents of last line
120 if (count) { process_last_line(BFPP_LANG) }
125 /*****************************************************************************/