1 // bat.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_BAT_PARSER_H
5 #define OHCOUNT_BAT_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *BAT_LANG = LANG_BAT;
12 // the languages entities
13 const char *bat_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 BAT_SPACE = 0, BAT_COMMENT, BAT_STRING, BAT_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action bat_ccallback {
40 std_internal_newline(BAT_LANG)
47 bat_comment = ( /rem/i | /@rem/i | '::' ) @comment nonnewline*;
50 spaces ${ entity = BAT_SPACE; } => bat_ccallback;
52 newline ${ entity = NEWLINE; } => bat_ccallback;
53 ^space ${ entity = BAT_ANY; } => bat_ccallback;
58 action bat_ecallback {
59 callback(BAT_LANG, bat_entities[entity], cint(ts), cint(te), userdata);
62 bat_comment_entity = ( /rem/i | /@rem/i | '::' ) nonnewline*;
65 space+ ${ entity = BAT_SPACE; } => bat_ecallback;
66 bat_comment_entity ${ entity = BAT_COMMENT; } => bat_ecallback;
72 /************************* Required for every parser *************************/
74 /* Parses a string buffer with Batch code.
76 * @param *buffer The string to parse.
77 * @param length The length of the string to parse.
78 * @param count Integer flag specifying whether or not to count lines. If yes,
79 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
80 * machine optimized for returning entity positions.
81 * @param *callback Callback function. If count is set, callback is called for
82 * every line of code, comment, or blank with 'lcode', 'lcomment', and
83 * 'lblank' respectively. Otherwise callback is called for each entity found.
85 void parse_bat(char *buffer, int length, int count,
86 void (*callback) (const char *lang, const char *entity, int s,
93 cs = (count) ? bat_en_bat_line : bat_en_bat_entity;
96 // if no newline at EOF; callback contents of last line
97 if (count) { process_last_line(BAT_LANG) }
102 /*****************************************************************************/