Updated comment in lua.rl.
[ohcount] / ext / ohcount_native / ragel_parsers / lua.rl
1 /************************* Required for every parser *************************/
2 #include "ragel_parser_macros.h"
3
4 // the name of the language
5 const char *LUA_LANG = "lua";
6
7 // the languages entities
8 const char *lua_entities[] = {
9   "space", "comment", "string", "number",
10   "identifier", "operator", "newline"
11 };
12
13 // constants associated with the entities
14 enum {
15   LUA_SPACE = 0, LUA_COMMENT, LUA_STRING, LUA_NUMBER,
16   LUA_IDENTIFIER, LUA_OPERATOR, LUA_NEWLINE
17 };
18
19 // do not change the following variables
20
21 // used for newlines inside patterns like strings and comments that can have
22 // newlines in them
23 #define INTERNAL_NL -1
24
25 // required by Ragel
26 int cs, act;
27 char *p, *pe, *eof, *ts, *te;
28
29 // used for calculating offsets from buffer start for start and end positions
30 char *buffer_start;
31 #define cint(c) ((int) (c - buffer_start))
32
33 // state flags for line and comment counting
34 int whole_line_comment;
35 int line_contains_code;
36
37 // the beginning of a line in the buffer for line and comment counting
38 char *line_start;
39
40 // state variable for the current entity being matched
41 int entity;
42
43 /*****************************************************************************/
44
45 %%{
46   machine lua;
47   write data;
48   include common "common.rl";
49
50   action lua_callback {
51     switch(entity) {
52     case LUA_SPACE:
53       ls
54       break;
55     //case LUA_COMMENT:
56     //case LUA_STRING:
57     case LUA_NUMBER:
58     case LUA_IDENTIFIER:
59     case LUA_OPERATOR:
60       code
61       break;
62     case INTERNAL_NL:
63       std_internal_newline(LUA_LANG)
64       break;
65     case LUA_NEWLINE:
66       std_newline(LUA_LANG)
67     }
68   }
69
70   action lua_long_ec_res { equal_count = 0; }
71   action lua_long_ec_inc { equal_count++; }
72   action lua_long_ec_dec { equal_count--; }
73
74   lua_long_comment =
75     '--' @comment ('[' >lua_long_ec_res '='* $lua_long_ec_inc '[') (
76       newline %{ entity = INTERNAL_NL; } %lua_callback
77       |
78       ws
79       |
80       (nonnewline - ws) @comment
81     )* :>> (']' '='* $lua_long_ec_dec ']' when { equal_count == 0 });
82   lua_line_comment = '--' @comment nonnewline*;
83   lua_comment = lua_long_comment | lua_line_comment;
84
85   lua_long_string =
86     ('[' >lua_long_ec_res '='* $lua_long_ec_inc '[') @code (
87       newline %{ entity = INTERNAL_NL; } %lua_callback
88       |
89       ws
90       |
91       (nonnewline - ws) @code
92     )* :>> (']' '='* $lua_long_ec_dec ']' when { equal_count == 0 });
93   lua_sq_str =
94     '\'' @code (
95       newline %{ entity = INTERNAL_NL; } %lua_callback
96       |
97       ws
98       |
99       [^\t '\\] @code
100       |
101       '\\' nonnewline @code
102     )* '\'';
103   lua_dq_str =
104     '"' @code (
105       newline %{ entity = INTERNAL_NL; } %lua_callback
106       |
107       ws
108       |
109       [^\t "\\] @code
110       |
111       '\\' nonnewline @code
112     )* '"';
113   lua_string = lua_sq_str | lua_dq_str | lua_long_string;
114
115   lua_integer = '-'? (hex_num | dec_num);
116   lua_number = float | lua_integer;
117
118   lua_identifier = ([a-zA-Z] | '_') (alnum + '_')*;
119
120   lua_operator = '~=' | [+\-*/%^#=<>,;:,.{}\[\]()];
121
122   lua_line := |*
123     spaces         ${ entity = LUA_SPACE;      } => lua_callback;
124     lua_comment    ${ entity = LUA_COMMENT;    } => lua_callback;
125     lua_string     ${ entity = LUA_STRING;     } => lua_callback;
126     lua_number     ${ entity = LUA_NUMBER;     } => lua_callback;
127     lua_identifier ${ entity = LUA_IDENTIFIER; } => lua_callback;
128     lua_operator   ${ entity = LUA_OPERATOR;   } => lua_callback;
129     newline        ${ entity = LUA_NEWLINE;    } => lua_callback;
130   *|;
131 }%%
132
133 /* Parses a string buffer with Lua code.
134  *
135  * @param *buffer The string to parse.
136  * @param length The length of the string to parse.
137  * @param count Integer flag specifying whether or not to count lines. If yes,
138  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
139  *   machine optimized for returning entity positions.
140  * @param *callback Callback function. If count is set, callback is called for
141  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
142  *   'lblank' respectively. Otherwise callback is called for each entity found.
143  */
144 void parse_lua(char *buffer, int length, int count,
145   void (*callback) (const char *lang, const char *entity, int start, int end)
146   ) {
147   p = buffer;
148   pe = buffer + length;
149   eof = pe;
150
151   buffer_start = buffer;
152   whole_line_comment = 0;
153   line_contains_code = 0;
154   line_start = 0;
155   entity = 0;
156
157   int equal_count = 0;
158
159   %% write init;
160   if (count)
161     %% write exec lua_line;
162
163   // if no newline at EOF; callback contents of last line
164   process_last_line(LUA_LANG)
165 }