1 // mp_with_tex.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_MPTEX_PARSER_H
5 #define OHCOUNT_MPTEX_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *MPTEX_LANG = LANG_METAPOST;
12 // the languages entities
13 const char *mptex_entities[] = {
14 "space", "comment", "string", "any"
18 // constants associated with the entities
20 MPTEX_SPACE = 0, MPTEX_COMMENT, MPTEX_STRING, MPTEX_ANY
23 /*****************************************************************************/
30 include common "common.rl";
33 # Line counting machine
35 action mptex_ccallback {
44 std_internal_newline(MPTEX_LANG)
47 std_newline(MPTEX_LANG)
49 case CHECK_BLANK_ENTRY:
50 check_blank_entry(MPTEX_LANG)
54 mptex_comment = '%' @{ fhold; } @comment nonnewline*;
56 mptex_string = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
58 mptex_tex_entry = ('verbatimtex' | 'btex') @code;
59 mptex_tex_outry = 'etex' @check_blank_outry @code;
61 mptex_tex_outry @{ p = ts; fret; };
62 # unmodified Tex patterns
63 spaces ${ entity = TEX_SPACE; } => tex_ccallback;
65 newline ${ entity = NEWLINE; } => tex_ccallback;
66 ^space ${ entity = TEX_ANY; } => tex_ccallback;
70 mptex_tex_entry @{ entity = CHECK_BLANK_ENTRY; } @mptex_ccallback
71 @{ saw(TEX_LANG); } => { fcall mptex_tex_line; };
72 # standard Metapost patterns
73 spaces ${ entity = MPTEX_SPACE; } => mptex_ccallback;
76 newline ${ entity = NEWLINE; } => mptex_ccallback;
77 ^space ${ entity = MPTEX_ANY; } => mptex_ccallback;
82 action mptex_ecallback {
83 callback(MPTEX_LANG, mptex_entities[entity], cint(ts), cint(te), userdata);
86 mptex_tex_entry_entity = 'verbatimtex' | 'btex';
87 mptex_tex_outry_entity = 'etex';
88 mptex_tex_entity := |*
89 mptex_tex_outry_entity @{ fret; };
90 # unmodified Tex patterns
91 space+ ${ entity = TEX_SPACE; } => tex_ecallback;
92 tex_comment_entity ${ entity = TEX_COMMENT; } => tex_ecallback;
97 mptex_comment_entity = '%' nonnewline*;
100 # TODO: mptex_ecallback for mptex_*_{entry,outry}_entity
101 mptex_tex_entry_entity => { fcall mptex_tex_entity; };
102 # standard mptex patterns
103 space+ ${ entity = MPTEX_SPACE; } => mptex_ecallback;
104 mptex_comment_entity ${ entity = MPTEX_COMMENT; } => mptex_ecallback;
110 /************************* Required for every parser *************************/
112 /* Parses a string buffer with Metapost code.
114 * @param *buffer The string to parse.
115 * @param length The length of the string to parse.
116 * @param count Integer flag specifying whether or not to count lines. If yes,
117 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
118 * machine optimized for returning entity positions.
119 * @param *callback Callback function. If count is set, callback is called for
120 * every line of code, comment, or blank with 'lcode', 'lcomment', and
121 * 'lblank' respectively. Otherwise callback is called for each entity found.
123 void parse_mptex(char *buffer, int length, int count,
124 void (*callback) (const char *lang, const char *entity, int s,
131 cs = (count) ? mptex_en_mptex_line : mptex_en_mptex_entity;
134 // if no newline at EOF; callback contents of last line
135 if (count) { process_last_line(MPTEX_LANG) }
140 /*****************************************************************************/