1 // metapost.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_METAPOST_PARSER_H
5 #define OHCOUNT_METAPOST_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *METAPOST_LANG = LANG_METAPOST;
12 // the languages entities
13 const char *metapost_entities[] = {
14 "space", "comment", "string", "any",
17 // constants associated with the entities
19 METAPOST_SPACE = 0, METAPOST_COMMENT, METAPOST_STRING, METAPOST_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action metapost_ccallback {
40 std_internal_newline(METAPOST_LANG)
43 std_newline(METAPOST_LANG)
47 metapost_comment = '%' @{ fhold; } @comment nonnewline*;
49 metapost_string = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
52 spaces ${ entity = METAPOST_SPACE; } => metapost_ccallback;
55 newline ${ entity = NEWLINE; } => metapost_ccallback;
56 ^space ${ entity = METAPOST_ANY; } => metapost_ccallback;
61 action metapost_ecallback {
62 callback(METAPOST_LANG, metapost_entities[entity], cint(ts), cint(te),
66 metapost_comment_entity = '%' nonnewline*;
69 space+ ${ entity = METAPOST_SPACE; } => metapost_ecallback;
70 metapost_comment_entity ${ entity = METAPOST_COMMENT; } => metapost_ecallback;
76 /************************* Required for every parser *************************/
78 /* Parses a string buffer with Metapost code.
80 * @param *buffer The string to parse.
81 * @param length The length of the string to parse.
82 * @param count Integer flag specifying whether or not to count lines. If yes,
83 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
84 * machine optimized for returning entity positions.
85 * @param *callback Callback function. If count is set, callback is called for
86 * every line of code, comment, or blank with 'lcode', 'lcomment', and
87 * 'lblank' respectively. Otherwise callback is called for each entity found.
89 void parse_metapost(char *buffer, int length, int count,
90 void (*callback) (const char *lang, const char *entity,
91 int s, int e, void *udata),
97 cs = (count) ? metapost_en_metapost_line : metapost_en_metapost_entity;
100 // if no newline at EOF; callback contents of last line
101 if (count) { process_last_line(METAPOST_LANG) }
106 /*****************************************************************************/