2 // derived from code written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
4 /************************* Required for every parser *************************/
5 #ifndef OHCOUNT_FORTH_PARSER_H
6 #define OHCOUNT_FORTH_PARSER_H
8 #include "../parser_macros.h"
10 // the name of the language
11 const char *FORTH_LANG = LANG_FORTH;
13 // the languages entities
14 const char *forth_entities[] = {
15 "space", "comment", "string", "any",
18 // constants associated with the entities
20 FORTH_SPACE = 0, FORTH_COMMENT, FORTH_STRING, FORTH_ANY
23 /*****************************************************************************/
28 include common "common.rl";
30 # Line counting machine
32 action forth_ccallback {
41 std_internal_newline(FORTH_LANG)
44 std_newline(FORTH_LANG)
48 forth_line_comment = '\\' @comment nonnewline*;
51 newline %{ entity = INTERNAL_NL; } %forth_ccallback
55 (nonnewline - ws) @comment
57 forth_comment = forth_line_comment | forth_block_comment;
59 forth_string = '"' @code ([^\r\n\f"])* '"';
62 spaces ${ entity = FORTH_SPACE; } => forth_ccallback;
65 newline ${ entity = NEWLINE; } => forth_ccallback;
66 ^space ${ entity = FORTH_ANY; } => forth_ccallback;
71 action forth_ecallback {
72 callback(FORTH_LANG, forth_entities[entity], cint(ts), cint(te), userdata);
75 forth_line_comment_entity = '\\' nonnewline*;
76 forth_block_comment_entity = '(' any* :>> ')';
77 forth_comment_entity = forth_line_comment_entity | forth_block_comment_entity;
80 space+ ${ entity = FORTH_SPACE; } => forth_ecallback;
81 forth_comment_entity ${ entity = FORTH_COMMENT; } => forth_ecallback;
87 /************************* Required for every parser *************************/
89 /* Parses a string buffer with Forth code.
91 * @param *buffer The string to parse.
92 * @param length The length of the string to parse.
93 * @param count Integer flag specifying whether or not to count lines. If yes,
94 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
95 * machine optimized for returning entity positions.
96 * @param *callback Callback function. If count is set, callback is called for
97 * every line of code, comment, or blank with 'lcode', 'lcomment', and
98 * 'lblank' respectively. Otherwise callback is called for each entity found.
100 void parse_forth(char *buffer, int length, int count,
101 void (*callback) (const char *lang, const char *entity, int s,
108 cs = (count) ? forth_en_forth_line : forth_en_forth_entity;
111 // if no newline at EOF; callback contents of last line
112 if (count) { process_last_line(FORTH_LANG) }
117 /*****************************************************************************/