1 // pascal.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_PASCAL_PARSER_H
5 #define OHCOUNT_PASCAL_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *PASCAL_LANG = LANG_PASCAL;
12 // the languages entities
13 const char *pascal_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 PASCAL_SPACE = 0, PASCAL_COMMENT, PASCAL_STRING, PASCAL_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action pascal_ccallback {
40 std_internal_newline(PASCAL_LANG)
43 std_newline(PASCAL_LANG)
47 pascal_line_comment = '//' @comment nonnewline*;
48 pascal_old_block_comment =
50 newline %{ entity = INTERNAL_NL; } %pascal_ccallback
54 (nonnewline - ws) @code
56 pascal_turbo_block_comment =
58 newline %{ entity = INTERNAL_NL; } %pascal_ccallback
62 (nonnewline - ws) @comment
64 pascal_comment = pascal_line_comment | pascal_old_block_comment |
65 pascal_turbo_block_comment;
69 newline %{ entity = INTERNAL_NL; } %pascal_ccallback
79 spaces ${ entity = PASCAL_SPACE; } => pascal_ccallback;
82 newline ${ entity = NEWLINE; } => pascal_ccallback;
83 ^space ${ entity = PASCAL_ANY; } => pascal_ccallback;
88 action pascal_ecallback {
89 callback(PASCAL_LANG, pascal_entities[entity], cint(ts), cint(te),
93 pascal_line_comment_entity = '//' nonnewline*;
94 pascal_old_block_comment_entity = '(*' any* :>> '*)';
95 pascal_turbo_block_comment_entity = '{' any* :>> '}';
96 pascal_comment_entity = pascal_line_comment_entity |
97 pascal_old_block_comment_entity | pascal_turbo_block_comment_entity;
100 space+ ${ entity = PASCAL_SPACE; } => pascal_ecallback;
101 pascal_comment_entity ${ entity = PASCAL_COMMENT; } => pascal_ecallback;
107 /************************* Required for every parser *************************/
109 /* Parses a string buffer with Pascal code.
111 * @param *buffer The string to parse.
112 * @param length The length of the string to parse.
113 * @param count Integer flag specifying whether or not to count lines. If yes,
114 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
115 * machine optimized for returning entity positions.
116 * @param *callback Callback function. If count is set, callback is called for
117 * every line of code, comment, or blank with 'lcode', 'lcomment', and
118 * 'lblank' respectively. Otherwise callback is called for each entity found.
120 void parse_pascal(char *buffer, int length, int count,
121 void (*callback) (const char *lang, const char *entity, int s,
128 cs = (count) ? pascal_en_pascal_line : pascal_en_pascal_entity;
131 // if no newline at EOF; callback contents of last line
132 if (count) { process_last_line(PASCAL_LANG) }
137 /*****************************************************************************/