1 // tex.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_TEX_PARSER_H
5 #define OHCOUNT_TEX_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *TEX_LANG = LANG_TEX;
12 // the languages entities
13 const char *tex_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 TEX_SPACE = 0, TEX_COMMENT, TEX_STRING, TEX_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action tex_ccallback {
40 std_internal_newline(TEX_LANG)
47 tex_comment = '%' @comment nonnewline*;
50 spaces ${ entity = TEX_SPACE; } => tex_ccallback;
52 newline ${ entity = NEWLINE; } => tex_ccallback;
53 ^space ${ entity = TEX_ANY; } => tex_ccallback;
58 action tex_ecallback {
59 callback(TEX_LANG, tex_entities[entity], cint(ts), cint(te), userdata);
62 tex_comment_entity = '%' nonnewline*;
65 space+ ${ entity = TEX_SPACE; } => tex_ecallback;
66 tex_comment_entity ${ entity = TEX_COMMENT; } => tex_ecallback;
72 /************************* Required for every parser *************************/
74 /* Parses a string buffer with Tex markup.
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_tex(char *buffer, int length, int count,
86 void (*callback) (const char *lang, const char *entity, int s,
93 cs = (count) ? tex_en_tex_line : tex_en_tex_entity;
96 // if no newline at EOF; callback contents of last line
97 if (count) { process_last_line(TEX_LANG) }
102 /*****************************************************************************/