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_line_comment_entity = ('--' | '#' | '//') nonnewline*;
85 sql_c_block_comment_entity = '/*' any* :>> '*/';
86 sql_block_comment_entity = '{' any* :>> '}';
87 sql_comment_entity = sql_line_comment_entity | sql_c_block_comment_entity |
88 sql_block_comment_entity;
91 space+ ${ entity = SQL_SPACE; } => sql_ecallback;
92 sql_comment_entity ${ entity = SQL_COMMENT; } => sql_ecallback;
98 /************************* Required for every parser *************************/
100 /* Parses a string buffer with SQL code.
102 * @param *buffer The string to parse.
103 * @param length The length of the string to parse.
104 * @param count Integer flag specifying whether or not to count lines. If yes,
105 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
106 * machine optimized for returning entity positions.
107 * @param *callback Callback function. If count is set, callback is called for
108 * every line of code, comment, or blank with 'lcode', 'lcomment', and
109 * 'lblank' respectively. Otherwise callback is called for each entity found.
111 void parse_sql(char *buffer, int length, int count,
112 void (*callback) (const char *lang, const char *entity, int start, int end)
117 cs = (count) ? sql_en_sql_line : sql_en_sql_entity;
120 // if no newline at EOF; callback contents of last line
121 if (count) { process_last_line(SQL_LANG) }
126 /*****************************************************************************/