1 /************************* Required for every parser *************************/
2 #ifndef OHCOUNT_STRATEGO_PARSER_H
3 #define OHCOUNT_STRATEGO_PARSER_H
5 #include "../parser_macros.h"
7 // the name of the language
8 const char *STRATEGO_LANG = LANG_STRATEGO;
10 // the languages entities
11 const char *stratego_entities[] = {
12 "space", "comment", "string", "any"
15 // constants associated with the entities
17 STRATEGO_SPACE = 0, STRATEGO_COMMENT, STRATEGO_STRING, STRATEGO_ANY
20 /*****************************************************************************/
25 include common "common.rl";
27 # Line counting machine
29 action stratego_ccallback {
38 std_internal_newline(STRATEGO_LANG)
41 std_newline(STRATEGO_LANG)
45 stratego_line_comment =
47 escaped_newline %{ entity = INTERNAL_NL; } %stratego_ccallback
51 (nonnewline - ws) @comment
54 stratego_block_comment =
56 newline %{ entity = INTERNAL_NL; } %stratego_ccallback
60 (nonnewline - ws) @comment
63 stratego_comment = stratego_line_comment | stratego_block_comment;
67 escaped_newline %{ entity = INTERNAL_NL; } %stratego_ccallback
83 stratego_string = stratego_dq_str | stratego_char_str;
86 spaces ${ entity = STRATEGO_SPACE; } => stratego_ccallback;
89 newline ${ entity = NEWLINE; } => stratego_ccallback;
90 ^space ${ entity = STRATEGO_ANY; } => stratego_ccallback;
95 action stratego_ecallback {
96 callback(STRATEGO_LANG, stratego_entities[entity], cint(ts), cint(te),
100 stratego_line_comment_entity = '//' (escaped_newline | nonnewline)*;
101 stratego_block_comment_entity = '/*' any* :>> '*/';
102 stratego_comment_entity = stratego_line_comment_entity | stratego_block_comment_entity;
103 stratego_string_entity = dq_str_with_escapes | ('\'' (('\\' [.] '\'') | ([^'\\] '\'')));
105 stratego_entity := |*
106 space+ ${ entity = STRATEGO_SPACE; } => stratego_ecallback;
107 stratego_comment_entity ${ entity = STRATEGO_COMMENT; } => stratego_ecallback;
108 stratego_string_entity ${ entity = STRATEGO_STRING; } => stratego_ecallback;
114 /************************* Required for every parser *************************/
116 /* Parses a string buffer with Stratego code.
118 * @param *buffer The string to parse.
119 * @param length The length of the string to parse.
120 * @param count Integer flag specifying whether or not to count lines. If yes,
121 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
122 * machine optimized for returning entity positions.
123 * @param *callback Callback function. If count is set, callback is called for
124 * every line of code, comment, or blank with 'lcode', 'lcomment', and
125 * 'lblank' respectively. Otherwise callback is called for each entity found.
127 void parse_stratego(char *buffer, int length, int count,
128 void (*callback) (const char *lang, const char *entity,
129 int s, int e, void *udata),
135 cs = (count) ? stratego_en_stratego_line : stratego_en_stratego_entity;
138 // if no newline at EOF; callback contents of last line
139 if (count) { process_last_line(STRATEGO_LANG) }
144 /*****************************************************************************/