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 // END parser includes
32 char name[MAX_LANGUAGE_NAME];
33 void (*parser) (char*, int, int, void*);
36 struct language languages[] = {
40 { "csharp", parse_csharp },
42 { "ruby", parse_ruby },
44 { "javascript", parse_javascript },
45 { "html", parse_html },
46 { "java", parse_java },
47 { "objective_c", parse_objective_c },
48 { "visualbasic", parse_visual_basic },
50 { "actionscript", parse_actionscript },
52 { "assembler", parse_assembler },
53 { "autoconf", parse_autoconf },
54 { "automake", parse_automake },
57 //{ "boo", parse_boo },
62 /* Returns a language_breakdown for a given language name. */
63 LanguageBreakdown *get_language_breakdown(char *name) {
65 for (i = 0; i < pr->language_breakdown_count; i++)
66 if (strcmp(pr->language_breakdowns[i].name, name) == 0)
67 return &pr->language_breakdowns[i]; // found one
69 language_breakdown_initialize(
70 &pr->language_breakdowns[pr->language_breakdown_count],
71 name, parse_buffer_len); // create one
72 return &pr->language_breakdowns[pr->language_breakdown_count++];
75 /* Yields a line's language, semantic, and text to an optional Ruby block. */
76 void ragel_parse_yield_line(const char *lang, const char *entity, int s, int e) {
77 if (rb_block_given_p()) {
80 rb_ary_store(ary, 0, ID2SYM(rb_intern(lang)));
81 if (strcmp(entity, "lcode") == 0)
82 rb_ary_store(ary, 1, ID2SYM(rb_intern("code")));
83 else if (strcmp(entity, "lcomment") == 0)
84 rb_ary_store(ary, 1, ID2SYM(rb_intern("comment")));
85 else if (strcmp(entity, "lblank") == 0)
86 rb_ary_store(ary, 1, ID2SYM(rb_intern("blank")));
87 rb_ary_store(ary, 2, rb_str_new(parse_buffer + s, e - s));
92 /* Callback function called for every entity in the source file discovered.
94 * Entities are defined in the parser and are things like comments, strings,
96 * This callback yields for a Ruby block if necessary:
97 * |language, semantic, line|
98 * @param *lang The language associated with the entity.
99 * @param *entity The entity discovered. There are 3 additional entities used
100 * by Ohcount for counting: lcode, lcomment, and lblank for a line of code,
101 * a whole line comment, or a blank line respectively.
102 * @param s The start position of the entity relative to the start of the
104 * @param e The end position of the entity relative to the start of the buffer
107 void ragel_parser_callback(const char *lang, const char *entity, int s, int e) {
108 LanguageBreakdown *lb = get_language_breakdown((char *) lang);
109 if (strcmp(entity, "lcode") == 0) {
110 language_breakdown_copy_code(lb, parse_buffer + s, parse_buffer + e);
111 ragel_parse_yield_line(lang, entity, s, e);
112 } else if (strcmp(entity, "lcomment") == 0) {
113 language_breakdown_copy_comment(lb, parse_buffer + s, parse_buffer + e);
114 ragel_parse_yield_line(lang, entity, s, e);
115 } else if (strcmp(entity, "lblank") == 0) {
117 ragel_parse_yield_line(lang, entity, s, e);
121 /* Tries to use an existing Ragel parser for the given language.
123 * @param *parse_result An allocated, empty ParseResult to hold parse results.
124 * @param *buffer A pointer to the buffer or character in the buffer to start
126 * @param buffer_len The length of the buffer to parse.
127 * @param *lang The language name associated with the buffer to parse.
128 * @return 1 if a Ragel parser is found, 0 otherwise.
130 int ragel_parser_parse(ParseResult *parse_result,
131 char *buffer, int buffer_len, char *lang) {
133 pr->language_breakdown_count = 0;
134 parse_buffer = buffer;
135 parse_buffer_len = buffer_len;
137 for (i = 0; strlen(languages[i].name) != 0; i++)
138 if (strcmp(languages[i].name, lang) == 0) {
139 languages[i].parser(buffer, buffer_len, 1, ragel_parser_callback);