1 // jam.rl written by Scott Lawrence. bytbox@gmail.com
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_JAM_PARSER_H
5 #define OHCOUNT_JAM_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *JAM_LANG = LANG_JAM;
12 // the languages entities
13 const char *jam_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 JAM_SPACE = 0, JAM_COMMENT, JAM_STRING, JAM_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action jam_ccallback {
40 std_internal_newline(JAM_LANG)
47 jam_comment = '#' @comment nonnewline*;
51 newline %{ entity = INTERNAL_NL; } %jam_ccallback
61 newline %{ entity = INTERNAL_NL; } %jam_ccallback
69 # TODO: heredocs; see ruby.rl for details
70 jam_string = jam_sq_str | jam_dq_str;
73 spaces ${ entity = JAM_SPACE; } => jam_ccallback;
76 newline ${ entity = NEWLINE; } => jam_ccallback;
77 ^space ${ entity = JAM_ANY; } => jam_ccallback;
82 action jam_ecallback {
83 callback(JAM_LANG, jam_entities[entity], cint(ts), cint(te), userdata);
86 jam_comment_entity = '#' nonnewline*;
89 space+ ${ entity = JAM_SPACE; } => jam_ecallback;
90 jam_comment_entity ${ entity = JAM_COMMENT; } => jam_ecallback;
96 /************************* Required for every parser *************************/
98 /* Parses a string buffer with Jam 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_jam(char *buffer, int length, int count,
110 void (*callback) (const char *lang, const char *entity, int s,
117 cs = (count) ? jam_en_jam_line : jam_en_jam_entity;
120 // if no newline at EOF; callback contents of last line
121 if (count) { process_last_line(JAM_LANG) }
126 /*****************************************************************************/