oga/ext/c/lexer.rl

113 lines
3.0 KiB
Plaintext
Raw Normal View History

#include "lexer.h"
2014-05-06 22:57:25 +00:00
/*
The following two macros allow the Ragel grammar to use generic function calls
without relying on the setup of the C or Java lexer. Using these macros we can
also pass along `self` to the callback functions without having to hard-code
this in to the Ragel grammar.
In the C lexer we don't need the `data` variable (since this is pulled in based
on `ts` and `te`) so the macro ignores this argument.
*/
#define callback(name, data, encoding, start, stop) \
liboga_xml_lexer_callback(self, name, encoding, start, stop);
#define callback_simple(name) \
liboga_xml_lexer_callback_simple(self, name);
VALUE oga_cLexer;
%%machine c_lexer;
2014-05-05 07:40:08 +00:00
/**
* Calls a method defined in the Ruby side of the lexer. The String value is
* created based on the values of `ts` and `te` and uses the encoding specified
* in `encoding`.
*
* @example
* rb_encoding *encoding = rb_enc_get(...);
* liboga_xml_lexer_callback(self, "on_string", encoding, ts, te);
2014-05-05 07:40:08 +00:00
*/
void liboga_xml_lexer_callback(
2014-05-06 07:41:36 +00:00
VALUE self,
const char *name,
rb_encoding *encoding,
const char *ts,
const char *te
)
{
2014-05-06 07:41:36 +00:00
VALUE value = rb_enc_str_new(ts, te - ts, encoding);
VALUE method = rb_intern(name);
2014-05-06 07:41:36 +00:00
rb_funcall(self, method, 1, value);
}
2014-05-05 07:40:08 +00:00
/**
* Calls a method defined in the Ruby side of the lexer without passing it any
* arguments.
*
* @example
* liboga_xml_lexer_callback_simple(self, "on_cdata_start");
2014-05-05 07:40:08 +00:00
*/
void liboga_xml_lexer_callback_simple(VALUE self, const char *name)
{
2014-05-06 07:41:36 +00:00
VALUE method = rb_intern(name);
2014-05-06 07:41:36 +00:00
rb_funcall(self, method, 0);
}
%% write data;
2014-05-05 07:40:08 +00:00
/**
* Lexes the input String specified in the instance variable `@data`. Lexed
* values have the same encoding as the input value. This instance variable
* is set in the Ruby layer of the lexer.
*
* The Ragel loop dispatches method calls back to Ruby land to make it easier
* to implement complex actions without having to fiddle around with C. This
* introduces a small performance overhead compared to a pure C implementation.
* However, this is worth the overhead due to it being much easier to maintain.
*/
VALUE oga_xml_lexer_advance(VALUE self)
{
2014-05-06 07:41:36 +00:00
/* Pull the data in from Ruby land. */
VALUE data_ivar = rb_ivar_get(self, rb_intern("@data"));
2014-05-06 07:41:36 +00:00
/* Make sure that all data passed back to Ruby has the proper encoding. */
rb_encoding *encoding = rb_enc_get(data_ivar);
2014-05-06 07:41:36 +00:00
char *data_str_val = StringValuePtr(data_ivar);
2014-05-06 07:41:36 +00:00
const char *p = data_str_val;
const char *pe = data_str_val + strlen(data_str_val);
const char *eof = pe;
const char *ts, *te;
2014-05-06 07:41:36 +00:00
int act = 0;
int cs = 0;
int top = 0;
2014-05-05 07:40:08 +00:00
2014-05-06 07:41:36 +00:00
/*
Fixed stack size is enough since the lexer doesn't use that many nested
fcalls.
*/
int stack[8];
2014-05-06 07:41:36 +00:00
%% write init;
%% write exec;
2014-05-06 07:41:36 +00:00
return Qnil;
}
%%{
include base_lexer "base_lexer.rl";
}%%
void Init_liboga_xml_lexer()
{
2014-05-06 07:41:36 +00:00
oga_cLexer = rb_define_class_under(oga_mXML, "Lexer", rb_cObject);
2014-05-06 07:41:36 +00:00
rb_define_method(oga_cLexer, "advance_native", oga_xml_lexer_advance, 0);
}