1 // python.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_PYTHON_PARSER_H
5 #define OHCOUNT_PYTHON_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *PYTHON_LANG = LANG_PYTHON;
12 // the languages entities
13 const char *python_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 PYTHON_SPACE = 0, PYTHON_COMMENT, PYTHON_STRING, PYTHON_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action python_ccallback {
40 std_internal_newline(PYTHON_LANG)
43 std_newline(PYTHON_LANG)
47 python_line_comment = ('#' | '//') @comment nonnewline*;
48 python_block_comment =
50 newline %{ entity = INTERNAL_NL; } %python_ccallback
54 (nonnewline - ws) @comment
58 newline %{ entity = INTERNAL_NL; } %python_ccallback
62 (nonnewline - ws) @comment
63 )* :>> '\'\'\'' @comment;
66 newline %{ entity = INTERNAL_NL; } %python_ccallback
70 (nonnewline - ws) @comment
71 )* :>> '"""' @comment;
72 python_comment = python_line_comment | python_block_comment |
73 python_sq_doc_str | python_dq_doc_str;
76 '\'' ([^'] | '\'' [^'] @{ fhold; }) @{ fhold; } # make sure it's not '''
77 ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
79 '"' ([^"] | '"' [^"] @{ fhold; }) @{ fhold; } # make sure it's not """
80 ([^\r\n\f"\\] | '\\' nonnewline)* '"';
81 python_string = (python_sq_str | python_dq_str) @code;
84 spaces ${ entity = PYTHON_SPACE; } => python_ccallback;
87 newline ${ entity = NEWLINE; } => python_ccallback;
88 ^space ${ entity = PYTHON_ANY; } => python_ccallback;
93 action python_ecallback {
94 callback(PYTHON_LANG, python_entities[entity], cint(ts), cint(te),
98 python_line_comment_entity = ('#' | '//') nonnewline*;
99 python_block_comment_entity = '/*' any* :>> '*/';
100 python_sq_doc_str_entity = '\'\'\'' any* :>> '\'\'\'';
101 python_dq_doc_str_entity = '"""' any* :>> '"""';
102 python_comment_entity = python_line_comment_entity |
103 python_block_comment_entity | python_sq_doc_str_entity |
104 python_dq_doc_str_entity;
107 space+ ${ entity = PYTHON_SPACE; } => python_ecallback;
108 python_comment_entity ${ entity = PYTHON_COMMENT; } => python_ecallback;
114 /************************* Required for every parser *************************/
116 /* Parses a string buffer with Python 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_python(char *buffer, int length, int count,
128 void (*callback) (const char *lang, const char *entity, int s,
135 cs = (count) ? python_en_python_line : python_en_python_entity;
138 // if no newline at EOF; callback contents of last line
139 if (count) { process_last_line(PYTHON_LANG) }
144 /*****************************************************************************/