1 // shell.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_SHELL_PARSER_H
5 #define OHCOUNT_SHELL_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *SHELL_LANG = LANG_SHELL;
12 // the languages entities
13 const char *shell_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 SHELL_SPACE = 0, SHELL_COMMENT, SHELL_STRING, SHELL_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action shell_ccallback {
40 std_internal_newline(SHELL_LANG)
43 std_newline(SHELL_LANG)
47 shell_comment = '#' @comment nonnewline*;
51 newline %{ entity = INTERNAL_NL; } %shell_ccallback
61 newline %{ entity = INTERNAL_NL; } %shell_ccallback
69 # TODO: heredocs; see ruby.rl for details
70 shell_string = shell_sq_str | shell_dq_str;
73 spaces ${ entity = SHELL_SPACE; } => shell_ccallback;
76 newline ${ entity = NEWLINE; } => shell_ccallback;
77 ^space ${ entity = SHELL_ANY; } => shell_ccallback;
82 action shell_ecallback {
83 callback(SHELL_LANG, shell_entities[entity], cint(ts), cint(te), userdata);
86 shell_comment_entity = '#' nonnewline*;
89 space+ ${ entity = SHELL_SPACE; } => shell_ecallback;
90 shell_comment_entity ${ entity = SHELL_COMMENT; } => shell_ecallback;
96 /************************* Required for every parser *************************/
98 /* Parses a string buffer with Shell code.
100 * @param *buffer The string to parse.
101 * @param length The length of the string to parse.
102 * @param count Integer flag specifying whether or not to count lines. If yes,
103 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
104 * machine optimized for returning entity positions.
105 * @param *callback Callback function. If count is set, callback is called for
106 * every line of code, comment, or blank with 'lcode', 'lcomment', and
107 * 'lblank' respectively. Otherwise callback is called for each entity found.
109 void parse_shell(char *buffer, int length, int count,
110 void (*callback) (const char *lang, const char *entity, int s,
117 cs = (count) ? shell_en_shell_line : shell_en_shell_entity;
120 // if no newline at EOF; callback contents of last line
121 if (count) { process_last_line(SHELL_LANG) }
126 /*****************************************************************************/