1 // ragel_parser.c written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
6 // BEGIN parser includes
8 #include "lua_parser.h"
9 #include "ruby_parser.h"
10 #include "css_parser.h"
11 #include "javascript_parser.h"
12 #include "html_parser.h"
13 #include "java_parser.h"
14 #include "objective_c_parser.h"
15 #include "visual_basic_parser.h"
16 #include "sql_parser.h"
17 #include "actionscript_parser.h"
18 #include "ada_parser.h"
19 #include "assembler_parser.h"
20 #include "autoconf_parser.h"
21 #include "automake_parser.h"
22 #include "awk_parser.h"
23 #include "bat_parser.h"
24 //#include "boo_parser.h"
25 #include "dcl_parser.h"
26 #include "dylan_parser.h"
27 #include "ebuild_parser.h"
28 #include "exheres_parser.h"
29 #include "lisp_parser.h"
30 #include "fortranfixed_parser.h"
31 #include "fortranfree_parser.h"
32 #include "haskell_parser.h"
33 #include "makefile_parser.h"
34 #include "matlab_parser.h"
35 #include "metafont_parser.h"
36 #include "metapost_parser.h"
37 #include "pascal_parser.h"
38 //#include "perl_parser.h"
39 #include "pike_parser.h"
40 //#include "python_parser.h"
41 #include "rexx_parser.h"
42 #include "scheme_parser.h"
43 #include "shell_parser.h"
44 #include "smalltalk_parser.h"
45 #include "tcl_parser.h"
46 #include "vhdl_parser.h"
47 // END parser includes
54 char name[MAX_LANGUAGE_NAME];
55 void (*parser) (char*, int, int, void*);
58 struct language languages[] = {
62 { "csharp", parse_csharp },
64 { "ruby", parse_ruby },
66 { "javascript", parse_javascript },
67 { "html", parse_html },
68 { "java", parse_java },
69 { "objective_c", parse_objective_c },
70 { "visualbasic", parse_visual_basic },
72 { "actionscript", parse_actionscript },
74 { "assembler", parse_assembler },
75 { "autoconf", parse_autoconf },
76 { "automake", parse_automake },
79 //{ "boo", parse_boo },
81 { "dylan", parse_dylan },
82 { "ebuild", parse_ebuild },
83 { "exheres", parse_exheres },
84 { "emacslisp", parse_emacslisp },
85 { "lisp", parse_lisp },
86 { "fortranfixed", parse_fortranfixed },
87 { "fortranfree", parse_fortranfree },
88 { "haskell", parse_haskell },
89 { "make", parse_makefile },
90 { "matlab", parse_matlab },
91 { "metafont", parse_metafont },
92 { "metapost", parse_metapost },
93 { "pascal", parse_pascal },
94 //{ "perl", parse_perl },
95 { "pike", parse_pike },
96 //{ "python", parse_python },
97 { "rexx", parse_rexx },
98 { "scheme", parse_scheme },
99 { "shell", parse_shell },
100 { "smalltalk", parse_smalltalk },
101 { "tcl", parse_tcl },
102 { "vala", parse_vala },
103 { "vhdl", parse_vhdl },
108 /* Returns a language_breakdown for a given language name. */
109 LanguageBreakdown *get_language_breakdown(char *name) {
111 for (i = 0; i < pr->language_breakdown_count; i++)
112 if (strcmp(pr->language_breakdowns[i].name, name) == 0)
113 return &pr->language_breakdowns[i]; // found one
115 language_breakdown_initialize(
116 &pr->language_breakdowns[pr->language_breakdown_count],
117 name, parse_buffer_len); // create one
118 return &pr->language_breakdowns[pr->language_breakdown_count++];
121 /* Yields a line's language, semantic, and text to an optional Ruby block. */
122 void ragel_parse_yield_line(const char *lang, const char *entity, int s, int e) {
123 if (rb_block_given_p()) {
125 ary = rb_ary_new2(2);
126 rb_ary_store(ary, 0, ID2SYM(rb_intern(lang)));
127 if (strcmp(entity, "lcode") == 0)
128 rb_ary_store(ary, 1, ID2SYM(rb_intern("code")));
129 else if (strcmp(entity, "lcomment") == 0)
130 rb_ary_store(ary, 1, ID2SYM(rb_intern("comment")));
131 else if (strcmp(entity, "lblank") == 0)
132 rb_ary_store(ary, 1, ID2SYM(rb_intern("blank")));
133 rb_ary_store(ary, 2, rb_str_new(parse_buffer + s, e - s));
138 /* Callback function called for every entity in the source file discovered.
140 * Entities are defined in the parser and are things like comments, strings,
142 * This callback yields for a Ruby block if necessary:
143 * |language, semantic, line|
144 * @param *lang The language associated with the entity.
145 * @param *entity The entity discovered. There are 3 additional entities used
146 * by Ohcount for counting: lcode, lcomment, and lblank for a line of code,
147 * a whole line comment, or a blank line respectively.
148 * @param s The start position of the entity relative to the start of the
150 * @param e The end position of the entity relative to the start of the buffer
153 void ragel_parser_callback(const char *lang, const char *entity, int s, int e) {
154 LanguageBreakdown *lb = get_language_breakdown((char *) lang);
155 if (strcmp(entity, "lcode") == 0) {
156 language_breakdown_copy_code(lb, parse_buffer + s, parse_buffer + e);
157 ragel_parse_yield_line(lang, entity, s, e);
158 } else if (strcmp(entity, "lcomment") == 0) {
159 language_breakdown_copy_comment(lb, parse_buffer + s, parse_buffer + e);
160 ragel_parse_yield_line(lang, entity, s, e);
161 } else if (strcmp(entity, "lblank") == 0) {
163 ragel_parse_yield_line(lang, entity, s, e);
167 /* Tries to use an existing Ragel parser for the given language.
169 * @param *parse_result An allocated, empty ParseResult to hold parse results.
170 * @param *buffer A pointer to the buffer or character in the buffer to start
172 * @param buffer_len The length of the buffer to parse.
173 * @param *lang The language name associated with the buffer to parse.
174 * @return 1 if a Ragel parser is found, 0 otherwise.
176 int ragel_parser_parse(ParseResult *parse_result,
177 char *buffer, int buffer_len, char *lang) {
179 pr->language_breakdown_count = 0;
180 parse_buffer = buffer;
181 parse_buffer_len = buffer_len;
183 for (i = 0; strlen(languages[i].name) != 0; i++)
184 if (strcmp(languages[i].name, lang) == 0) {
185 languages[i].parser(buffer, buffer_len, 1, ragel_parser_callback);