1 // metapost.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef RAGEL_METAPOST_PARSER
5 #define RAGEL_METAPOST_PARSER
7 #include "ragel_parser_macros.h"
9 // the name of the language
10 const char *METAPOST_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));
65 metapost_entity := 'TODO:';
68 /************************* Required for every parser *************************/
70 /* Parses a string buffer with Metapost code.
72 * @param *buffer The string to parse.
73 * @param length The length of the string to parse.
74 * @param count Integer flag specifying whether or not to count lines. If yes,
75 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
76 * machine optimized for returning entity positions.
77 * @param *callback Callback function. If count is set, callback is called for
78 * every line of code, comment, or blank with 'lcode', 'lcomment', and
79 * 'lblank' respectively. Otherwise callback is called for each entity found.
81 void parse_metapost(char *buffer, int length, int count,
82 void (*callback) (const char *lang, const char *entity, int start, int end)
87 cs = (count) ? metapost_en_metapost_line : metapost_en_metapost_entity;
90 // if no newline at EOF; callback contents of last line
91 if (count) { process_last_line(METAPOST_LANG) }
96 /*****************************************************************************/