1 // php.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_PHP_PARSER_H
5 #define OHCOUNT_PHP_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *PHP_LANG = LANG_PHP;
12 // the languages entities
13 const char *php_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 PHP_SPACE = 0, PHP_COMMENT, PHP_STRING, PHP_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action php_ccallback {
40 std_internal_newline(PHP_LANG)
47 php_line_comment = ('//' | '#') @comment nonnewline*;
50 newline %{ entity = INTERNAL_NL; } %php_ccallback
54 (nonnewline - ws) @comment
56 php_comment = php_line_comment | php_block_comment;
60 newline %{ entity = INTERNAL_NL; } %php_ccallback
67 )* '\'' @commit @code;
70 newline %{ entity = INTERNAL_NL; } %php_ccallback
78 # TODO: heredoc; see ruby.rl for details.
79 php_string = php_sq_str | php_dq_str;
82 spaces ${ entity = PHP_SPACE; } => php_ccallback;
85 newline ${ entity = NEWLINE; } => php_ccallback;
86 ^space ${ entity = PHP_ANY; } => php_ccallback;
91 action php_ecallback {
92 callback(PHP_LANG, php_entities[entity], cint(ts), cint(te), userdata);
95 php_line_comment_entity = ('#' | '//') nonnewline*;
96 php_block_comment_entity = '/*' any* :>> '*/';
97 php_comment_entity = php_line_comment_entity | php_block_comment_entity;
100 space+ ${ entity = PHP_SPACE; } => php_ecallback;
101 php_comment_entity ${ entity = PHP_COMMENT; } => php_ecallback;
107 /************************* Required for every parser *************************/
109 /* Parses a string buffer with PHP code (not in HTML).
111 * @param *buffer The string to parse.
112 * @param length The length of the string to parse.
113 * @param count Integer flag specifying whether or not to count lines. If yes,
114 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
115 * machine optimized for returning entity positions.
116 * @param *callback Callback function. If count is set, callback is called for
117 * every line of code, comment, or blank with 'lcode', 'lcomment', and
118 * 'lblank' respectively. Otherwise callback is called for each entity found.
120 void parse_php(char *buffer, int length, int count,
121 void (*callback) (const char *lang, const char *entity, int s,
128 cs = (count) ? php_en_php_line : php_en_php_entity;
131 // if no newline at EOF; callback contents of last line
132 if (count) { process_last_line(PHP_LANG) }
137 /*****************************************************************************/