1 // sql.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef RAGEL_SQL_PARSER
5 #define RAGEL_SQL_PARSER
7 #include "ragel_parser_macros.h"
9 // the name of the language
10 const char *SQL_LANG = "sql";
12 // the languages entities
13 const char *sql_entities[] = {
14 "space", "comment", "string", "any",
17 // constants associated with the entities
19 SQL_SPACE = 0, SQL_COMMENT, SQL_STRING, SQL_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action sql_ccallback {
40 std_internal_newline(SQL_LANG)
47 sql_line_comment = ('--' | '#' | '//') @comment nonnewline*;
50 newline %{ entity = INTERNAL_NL; } %sql_ccallback
54 (nonnewline - ws) @comment
58 newline %{ entity = INTERNAL_NL; } %sql_ccallback
62 (nonnewline - ws) @comment
64 sql_comment = sql_line_comment | sql_c_block_comment | sql_block_comment;
66 sql_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
67 sql_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
68 sql_string = sql_sq_str | sql_dq_str;
71 spaces ${ entity = SQL_SPACE; } => sql_ccallback;
74 newline ${ entity = NEWLINE; } => sql_ccallback;
75 ^space ${ entity = SQL_ANY; } => sql_ccallback;
80 action sql_ecallback {
81 callback(SQL_LANG, sql_entities[entity], cint(ts), cint(te));
84 sql_entity := 'TODO:';
87 /************************* Required for every parser *************************/
89 /* Parses a string buffer with SQL code.
91 * @param *buffer The string to parse.
92 * @param length The length of the string to parse.
93 * @param count Integer flag specifying whether or not to count lines. If yes,
94 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
95 * machine optimized for returning entity positions.
96 * @param *callback Callback function. If count is set, callback is called for
97 * every line of code, comment, or blank with 'lcode', 'lcomment', and
98 * 'lblank' respectively. Otherwise callback is called for each entity found.
100 void parse_sql(char *buffer, int length, int count,
101 void (*callback) (const char *lang, const char *entity, int start, int end)
106 cs = (count) ? sql_en_sql_line : sql_en_sql_entity;
109 // if no newline at EOF; callback contents of last line
110 if (count) { process_last_line(SQL_LANG) }
115 /*****************************************************************************/