Fixes recursion bug in disambiguate_in().
[ohcount] / src / parsers / lisp.rl
1 // lisp.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_LISP_PARSER_H
5 #define OHCOUNT_LISP_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *LISP_LANG = LANG_LISP;
11
12 // the languages entities
13 const char *lisp_entities[] = {
14   "space", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   LISP_SPACE = 0, LISP_COMMENT, LISP_STRING, LISP_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine lisp;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action lisp_ccallback {
32     switch(entity) {
33     case LISP_SPACE:
34       ls
35       break;
36     case LISP_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(LISP_LANG)
41       break;
42     case NEWLINE:
43       std_newline(LISP_LANG)
44     }
45   }
46
47   lisp_docstring = '"'{3} @comment (
48     newline %{ entity = INTERNAL_NL; } %lisp_ccallback
49     |
50     ws
51     |
52     (nonnewline - ws) @comment
53   )* :>> '"""' @comment;
54
55   lisp_line_comment = ';' @comment nonnewline*;
56
57   lisp_comment = lisp_line_comment | lisp_docstring;
58
59   lisp_empty_string =
60     '"'{2} [^"] @{fhold;} @code;
61
62   lisp_char_string =
63     '"' [^"] '"' @code;
64
65   lisp_regular_string =
66     '"' ([^"]{2}) @code (
67       newline %{ entity = INTERNAL_NL; } %lisp_ccallback
68       |
69       ws
70       |
71       [^\r\n\f\t "\\] @code
72       |
73       '\\' nonnewline @code
74     )* '"';
75
76   lisp_string = lisp_empty_string | lisp_char_string | lisp_regular_string;
77
78   lisp_line := |*
79     spaces        ${ entity = LISP_SPACE; } => lisp_ccallback;
80     lisp_comment;
81     lisp_string;
82     newline       ${ entity = NEWLINE;    } => lisp_ccallback;
83     ^space        ${ entity = LISP_ANY;   } => lisp_ccallback;
84   *|;
85
86   # Entity machine
87
88   action lisp_ecallback {
89     callback(LISP_LANG, lisp_entities[entity], cint(ts), cint(te), userdata);
90   }
91
92   lisp_comment_entity = ';' nonnewline*;
93
94   lisp_entity := |*
95     space+              ${ entity = LISP_SPACE;   } => lisp_ecallback;
96     lisp_comment_entity ${ entity = LISP_COMMENT; } => lisp_ecallback;
97     # TODO:
98     ^space;
99   *|;
100 }%%
101
102 /* Parses a string buffer with Lisp code.
103  *
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.
112  */
113 void parse_lisp(char *buffer, int length, int count,
114                 void (*callback) (const char *lang, const char *entity, int s,
115                                   int e, void *udata),
116                 void *userdata
117   ) {
118   init
119
120   %% write init;
121   cs = (count) ? lisp_en_lisp_line : lisp_en_lisp_entity;
122   %% write exec;
123
124   // if no newline at EOF; callback contents of last line
125   if (count) { process_last_line(LISP_LANG) }
126 }
127
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),
133                      void *userdata
134   ) {
135   LISP_LANG = ELISP_LANG;
136   parse_lisp(buffer, length, count, callback, userdata);
137   LISP_LANG = ORIG_LISP_LANG;
138 }
139
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,
143                                     int e, void *udata),
144                   void *userdata
145   ) {
146   LISP_LANG = SCHEME_LANG;
147   parse_lisp(buffer, length, count, callback, userdata);
148   LISP_LANG = ORIG_LISP_LANG;
149 }
150
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,
154                                      int e, void *udata),
155                    void *userdata
156   ) {
157   LISP_LANG = CLOJURE_LANG;
158   parse_lisp(buffer, length, count, callback, userdata);
159   LISP_LANG = ORIG_LISP_LANG;
160 }
161
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,
165                                      int e, void *udata),
166                    void *userdata
167   ) {
168   LISP_LANG = RACKET_LANG;
169   parse_lisp(buffer, length, count, callback, userdata);
170   LISP_LANG = ORIG_LISP_LANG;
171 }
172
173
174 #endif