1 // puppet.rl written by Ken Barber <ken@bob.sh>
2 // Based on pascal.rl by Mitchell Foral. mitchell<att>caladbolg<dott>net
4 /************************* Required for every parser *************************/
5 #ifndef OHCOUNT_PUPPET_PARSER_H
6 #define OHCOUNT_PUPPET_PARSER_H
8 #include "../parser_macros.h"
10 // the name of the language
11 const char *PUPPET_LANG = LANG_PUPPET;
13 // the languages entities
14 const char *puppet_entities[] = {
15 "space", "comment", "string", "any"
18 // constants associated with the entities
20 PUPPET_SPACE = 0, PUPPET_COMMENT, PUPPET_STRING, PUPPET_ANY
23 /*****************************************************************************/
28 include common "common.rl";
30 # Line counting machine
32 action puppet_ccallback {
41 std_internal_newline(PUPPET_LANG)
44 std_newline(PUPPET_LANG)
48 puppet_line_comment = '#' @comment nonnewline*;
49 puppet_block_comment =
51 newline %{ entity = INTERNAL_NL; } %puppet_ccallback
55 (nonnewline - ws) @code
57 puppet_comment = puppet_line_comment | puppet_block_comment;
61 newline %{ entity = INTERNAL_NL; } %puppet_ccallback
71 spaces ${ entity = PUPPET_SPACE; } => puppet_ccallback;
74 newline ${ entity = NEWLINE; } => puppet_ccallback;
75 ^space ${ entity = PUPPET_ANY; } => puppet_ccallback;
80 action puppet_ecallback {
81 callback(PUPPET_LANG, puppet_entities[entity], cint(ts), cint(te),
85 puppet_line_comment_entity = '#' nonnewline*;
86 puppet_block_comment_entity = '/*' any* :>> '*/';
87 puppet_comment_entity = puppet_line_comment_entity |
88 puppet_block_comment_entity;
91 space+ ${ entity = PUPPET_SPACE; } => puppet_ecallback;
92 puppet_comment_entity ${ entity = PUPPET_COMMENT; } => puppet_ecallback;
98 /************************* Required for every parser *************************/
100 /* Parses a string buffer with Puppet DSL code.
102 * @param *buffer The string to parse.
103 * @param length The length of the string to parse.
104 * @param count Integer flag specifying whether or not to count lines. If yes,
105 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
106 * machine optimized for returning entity positions.
107 * @param *callback Callback function. If count is set, callback is called for
108 * every line of code, comment, or blank with 'lcode', 'lcomment', and
109 * 'lblank' respectively. Otherwise callback is called for each entity found.
111 void parse_puppet(char *buffer, int length, int count,
112 void (*callback) (const char *lang, const char *entity, int s,
119 cs = (count) ? puppet_en_puppet_line : puppet_en_puppet_entity;
122 // if no newline at EOF; callback contents of last line
123 if (count) { process_last_line(PUPPET_LANG) }
128 /*****************************************************************************/