1 // lisp.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_LISP_PARSER_H
5 #define OHCOUNT_LISP_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *LISP_LANG = LANG_LISP;
12 // the languages entities
13 const char *lisp_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 LISP_SPACE = 0, LISP_COMMENT, LISP_STRING, LISP_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action lisp_ccallback {
40 std_internal_newline(LISP_LANG)
43 std_newline(LISP_LANG)
47 lisp_docstring = '"'{3} @comment (
48 newline %{ entity = INTERNAL_NL; } %lisp_ccallback
52 (nonnewline - ws) @comment
53 )* :>> '"""' @comment;
55 lisp_line_comment = ';' @comment nonnewline*;
57 lisp_comment = lisp_line_comment | lisp_docstring;
60 '"'{2} [^"] @{fhold;} @code;
67 newline %{ entity = INTERNAL_NL; } %lisp_ccallback
76 lisp_string = lisp_empty_string | lisp_char_string | lisp_regular_string;
79 spaces ${ entity = LISP_SPACE; } => lisp_ccallback;
82 newline ${ entity = NEWLINE; } => lisp_ccallback;
83 ^space ${ entity = LISP_ANY; } => lisp_ccallback;
88 action lisp_ecallback {
89 callback(LISP_LANG, lisp_entities[entity], cint(ts), cint(te), userdata);
92 lisp_comment_entity = ';' nonnewline*;
95 space+ ${ entity = LISP_SPACE; } => lisp_ecallback;
96 lisp_comment_entity ${ entity = LISP_COMMENT; } => lisp_ecallback;
102 /* Parses a string buffer with Lisp code.
104 * @param *buffer The string to parse.
105 * @param length The length of the string to parse.
106 * @param count Integer flag specifying whether or not to count lines. If yes,
107 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
108 * machine optimized for returning entity positions.
109 * @param *callback Callback function. If count is set, callback is called for
110 * every line of code, comment, or blank with 'lcode', 'lcomment', and
111 * 'lblank' respectively. Otherwise callback is called for each entity found.
113 void parse_lisp(char *buffer, int length, int count,
114 void (*callback) (const char *lang, const char *entity, int s,
121 cs = (count) ? lisp_en_lisp_line : lisp_en_lisp_entity;
124 // if no newline at EOF; callback contents of last line
125 if (count) { process_last_line(LISP_LANG) }
128 const char *ELISP_LANG = LANG_EMACSLISP;
129 const char *ORIG_LISP_LANG = LANG_LISP;
130 void parse_emacslisp(char *buffer, int length, int count,
131 void (*callback) (const char *lang, const char *entity,
132 int s, int e, void *udata),
135 LISP_LANG = ELISP_LANG;
136 parse_lisp(buffer, length, count, callback, userdata);
137 LISP_LANG = ORIG_LISP_LANG;
140 const char *SCHEME_LANG = LANG_SCHEME;
141 void parse_scheme(char *buffer, int length, int count,
142 void (*callback) (const char *lang, const char *entity, int s,
146 LISP_LANG = SCHEME_LANG;
147 parse_lisp(buffer, length, count, callback, userdata);
148 LISP_LANG = ORIG_LISP_LANG;
151 const char *CLOJURE_LANG = LANG_CLOJURE;
152 void parse_clojure(char *buffer, int length, int count,
153 void (*callback) (const char *lang, const char *entity, int s,
157 LISP_LANG = CLOJURE_LANG;
158 parse_lisp(buffer, length, count, callback, userdata);
159 LISP_LANG = ORIG_LISP_LANG;
162 const char *RACKET_LANG = LANG_RACKET;
163 void parse_racket(char *buffer, int length, int count,
164 void (*callback) (const char *lang, const char *entity, int s,
168 LISP_LANG = RACKET_LANG;
169 parse_lisp(buffer, length, count, callback, userdata);
170 LISP_LANG = ORIG_LISP_LANG;