1 // prolog.rl written by Paulo Moura. pmoura<att>prolog<dott>org.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_PROLOG_PARSER_H
5 #define OHCOUNT_PROLOG_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *PROLOG_LANG = LANG_PROLOG;
12 // the languages entities
13 const char *prolog_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 PROLOG_SPACE = 0, PROLOG_COMMENT, PROLOG_STRING, PROLOG_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action prolog_ccallback {
40 std_internal_newline(PROLOG_LANG)
43 std_newline(PROLOG_LANG)
47 prolog_line_comment = '%' @comment nonnewline*;
48 prolog_block_comment =
50 newline %{ entity = INTERNAL_NL; } %prolog_ccallback
54 (nonnewline - ws) @comment
56 prolog_comment = prolog_line_comment | prolog_block_comment;
58 prolog_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
59 prolog_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
60 prolog_string = prolog_sq_str | prolog_dq_str;
63 spaces ${ entity = PROLOG_SPACE; } => prolog_ccallback;
66 newline ${ entity = NEWLINE; } => prolog_ccallback;
67 ^space ${ entity = PROLOG_ANY; } => prolog_ccallback;
72 action prolog_ecallback {
73 callback(PROLOG_LANG, prolog_entities[entity], cint(ts), cint(te),
77 prolog_line_comment_entity = '%' nonnewline*;
78 prolog_block_comment_entity = '/*' any* :>> '*/';
79 prolog_comment_entity = prolog_line_comment_entity | prolog_block_comment_entity;
82 space+ ${ entity = PROLOG_SPACE; } => prolog_ecallback;
83 prolog_comment_entity ${ entity = PROLOG_COMMENT; } => prolog_ecallback;
89 /************************* Required for every parser *************************/
91 /* Parses a string buffer with Prolog code.
93 * @param *buffer The string to parse.
94 * @param length The length of the string to parse.
95 * @param count Integer flag specifying whether or not to count lines. If yes,
96 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
97 * machine optimized for returning entity positions.
98 * @param *callback Callback function. If count is set, callback is called for
99 * every line of code, comment, or blank with 'lcode', 'lcomment', and
100 * 'lblank' respectively. Otherwise callback is called for each entity found.
102 void parse_prolog(char *buffer, int length, int count,
103 void (*callback) (const char *lang, const char *entity, int s,
110 cs = (count) ? prolog_en_prolog_line : prolog_en_prolog_entity;
113 // if no newline at EOF; callback contents of last line
114 if (count) { process_last_line(PROLOG_LANG) }
119 /*****************************************************************************/