File: rhtml.rl

package info (click to toggle)
ohcount 4.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 8,580 kB
  • sloc: ansic: 7,584; javascript: 3,300; ruby: 2,363; perl: 2,065; objc: 1,325; python: 445; pascal: 398; erlang: 350; sh: 275; lisp: 272; vhdl: 150; haskell: 149; asm: 127; cs: 124; awk: 98; java: 92; php: 77; tcl: 58; xml: 57; fortran: 54; modula3: 32; makefile: 30; ada: 30; jsp: 28; sql: 18; cobol: 13; ml: 9
file content (238 lines) | stat: -rw-r--r-- 7,245 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// rhtml.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.

/************************* Required for every parser *************************/
#ifndef OHCOUNT_RHTML_PARSER_H
#define OHCOUNT_RHTML_PARSER_H

#include "../parser_macros.h"

// the name of the language
const char *RHTML_LANG = LANG_HTML;

// the languages entities
const char *rhtml_entities[] = {
  "space", "comment", "doctype",
  "tag", "entity", "any"
};

// constants associated with the entities
enum {
  RHTML_SPACE = 0, RHTML_COMMENT, RHTML_DOCTYPE,
  RHTML_TAG, RHTML_ENTITY, RHTML_ANY
};

/*****************************************************************************/

#include "css.h"
#include "javascript.h"
#include "ruby.h"

%%{
  machine rhtml;
  write data;
  include common "common.rl";
  #EMBED(css)
  #EMBED(javascript)
  #EMBED(ruby)

  # Line counting machine

  action rhtml_ccallback {
    switch(entity) {
    case RHTML_SPACE:
      ls
      break;
    case RHTML_ANY:
      code
      break;
    case INTERNAL_NL:
      emb_internal_newline(RHTML_LANG)
      break;
    case NEWLINE:
      emb_newline(RHTML_LANG)
      break;
    case CHECK_BLANK_ENTRY:
      check_blank_entry(RHTML_LANG)
    }
  }

  rhtml_comment := (
    newline %{ entity = INTERNAL_NL; } %rhtml_ccallback
    |
    ws
    |
    ^(space | [\-<]) @comment
    |
    '<' '%' @{ saw(RUBY_LANG); fcall rhtml_ruby_line; }
    |
    '<' !'%'
  )* :>> '-->' @comment @{ fgoto rhtml_line; };

  rhtml_sq_str := (
    newline %{ entity = INTERNAL_NL; } %rhtml_ccallback
    |
    ws
    |
    [^\r\n\f\t '\\<] @code
    |
    '\\' nonnewline @code
    |
    '<' '%' @{ saw(RUBY_LANG); fcall rhtml_ruby_line; }
    |
    '<' !'%'
  )* '\'' @{ fgoto rhtml_line; };
  rhtml_dq_str := (
    newline %{ entity = INTERNAL_NL; } %rhtml_ccallback
    |
    ws
    |
    [^\r\n\f\t "\\<] @code
    |
    '\\' nonnewline @code
    |
    '<' '%' @{ saw(RUBY_LANG); fcall rhtml_ruby_line; }
    |
    '<' !'%'
  )* '"' @{ fgoto rhtml_line; };

  ws_or_inl = (ws | newline @{ entity = INTERNAL_NL; } %rhtml_ccallback);

  rhtml_css_entry = '<' /style/i [^>]+ :>> 'text/css' [^>]+ '>' @code;
  rhtml_css_outry = '</' /style/i ws_or_inl* '>' @check_blank_outry @code;
  rhtml_css_line := |*
    rhtml_css_outry @{ p = ts; fret; };
    # unmodified CSS patterns
    spaces      ${ entity = CSS_SPACE; } => css_ccallback;
    css_comment;
    css_string;
    newline     ${ entity = NEWLINE;   } => css_ccallback;
    ^space      ${ entity = CSS_ANY;   } => css_ccallback;
  *|;

  rhtml_js_entry = '<' /script/i [^>]+ :>> 'text/javascript' [^>]+ '>' @code;
  rhtml_js_outry = '</' /script/i ws_or_inl* '>' @check_blank_outry @code;
  rhtml_js_line := |*
    rhtml_js_outry @{ p = ts; fret; };
    # unmodified Javascript patterns
    spaces     ${ entity = JS_SPACE; } => js_ccallback;
    js_comment;
    js_string;
    newline    ${ entity = NEWLINE;  } => js_ccallback;
    ^space     ${ entity = JS_ANY;   } => js_ccallback;
  *|;

  rhtml_ruby_entry = '<%' @code;
  rhtml_ruby_outry = '%>' @check_blank_outry @code;
  rhtml_ruby_line := |*
    rhtml_ruby_outry @{ p = ts; fret; };
    # unmodified Ruby patterns
    spaces        ${ entity = RUBY_SPACE; } => ruby_ccallback;
    ruby_comment;
    ruby_string;
    newline       ${ entity = NEWLINE;    } => ruby_ccallback;
    ^space        ${ entity = RUBY_ANY;   } => ruby_ccallback;
  *|;

  rhtml_line := |*
    rhtml_css_entry @{ entity = CHECK_BLANK_ENTRY; } @rhtml_ccallback
      @{ saw(CSS_LANG); } => { fcall rhtml_css_line; };
    rhtml_js_entry @{ entity = CHECK_BLANK_ENTRY; } @rhtml_ccallback
      @{ saw(JS_LANG); } => { fcall rhtml_js_line; };
    rhtml_ruby_entry @{ entity = CHECK_BLANK_ENTRY; } @rhtml_ccallback
      @{ saw(RUBY_LANG); } => { fcall rhtml_ruby_line; };
    # standard RHTML patterns
    spaces       ${ entity = RHTML_SPACE; } => rhtml_ccallback;
    '<!--'       @comment                   => { fgoto rhtml_comment; };
    '\''         @code                      => { fgoto rhtml_sq_str;  };
    '"'          @code                      => { fgoto rhtml_dq_str;  };
    newline      ${ entity = NEWLINE;     } => rhtml_ccallback;
    ^space       ${ entity = RHTML_ANY;   } => rhtml_ccallback;
  *|;

  # Entity machine

  action rhtml_ecallback {
    callback(RHTML_LANG, rhtml_entities[entity], cint(ts), cint(te), userdata);
  }

  rhtml_css_entry_entity = '<' /style/i [^>]+ :>> 'text/css' [^>]+ '>';
  rhtml_css_outry_entity = '</' /style/i ws_or_inl* '>';
  rhtml_css_entity := |*
    rhtml_css_outry_entity @{ fret; };
    # unmodified CSS patterns
    space+             ${ entity = CSS_SPACE;   } => css_ecallback;
    css_comment_entity ${ entity = CSS_COMMENT; } => css_ecallback;
    # TODO:
    ^space;
  *|;

  rhtml_js_entry_entity = '<' /script/i [^>]+ :>> 'text/javascript' [^>]+ '>';
  rhtml_js_outry_entity = '</' /script/i ws_or_inl* '>';
  rhtml_js_entity := |*
    rhtml_js_outry_entity @{ fret; };
    # unmodified Javascript patterns
    space+            ${ entity = JS_SPACE;   } => js_ecallback;
    js_comment_entity ${ entity = JS_COMMENT; } => js_ecallback;
    # TODO:
    ^space;
  *|;

  rhtml_ruby_entry_entity = '<%';
  rhtml_ruby_outry_entity = '%>';
  rhtml_ruby_entity := |*
    rhtml_ruby_outry_entity @{ fret; };
    # unmodified Ruby patterns
    space+              ${ entity = RUBY_SPACE;   } => ruby_ecallback;
    ruby_comment_entity ${ entity = RUBY_COMMENT; } => ruby_ecallback;
    # TODO:
    ^space;
  *|;

  rhtml_comment_entity = '<!--' any* :>> '-->';

  rhtml_entity := |*
    # TODO: rhtml_ecallback for rhtml_*_{entry,outry}_entity
    rhtml_css_entry_entity  => { fcall rhtml_css_entity;  };
    rhtml_js_entry_entity   => { fcall rhtml_js_entity;   };
    rhtml_ruby_entry_entity => { fcall rhtml_ruby_entity; };
    # standard RHTML patterns
    space+               ${ entity = RHTML_SPACE;   } => rhtml_ecallback;
    rhtml_comment_entity ${ entity = RHTML_COMMENT; } => rhtml_ecallback;
    # TODO:
    ^space;
  *|;
}%%

/************************* Required for every parser *************************/

/* Parses a string buffer with RHTML markup.
 *
 * @param *buffer The string to parse.
 * @param length The length of the string to parse.
 * @param count Integer flag specifying whether or not to count lines. If yes,
 *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
 *   machine optimized for returning entity positions.
 * @param *callback Callback function. If count is set, callback is called for
 *   every line of code, comment, or blank with 'lcode', 'lcomment', and
 *   'lblank' respectively. Otherwise callback is called for each entity found.
 */
void parse_rhtml(char *buffer, int length, int count,
                 void (*callback) (const char *lang, const char *entity, int s,
                                   int e, void *udata),
                 void *userdata
  ) {
  init

  const char *seen = 0;

  %% write init;
  cs = (count) ? rhtml_en_rhtml_line : rhtml_en_rhtml_entity;
  %% write exec;

  // if no newline at EOF; callback contents of last line
  if (count) { process_last_line(RHTML_LANG) }
}

#endif

/*****************************************************************************/