From 2b3a6be24d71ac722ab87ada54f1860cf64cc290 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 5 May 2014 21:19:38 +0200 Subject: [PATCH 1/2] Use liboga as a prefix in the C code. Namespaces? What are those? --- ext/liboga/lexer.rl | 52 ++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/ext/liboga/lexer.rl b/ext/liboga/lexer.rl index 2891402..62333cb 100644 --- a/ext/liboga/lexer.rl +++ b/ext/liboga/lexer.rl @@ -11,9 +11,9 @@ VALUE oga_cLexer; * * @example * rb_encoding *encoding = rb_enc_get(...); - * oga_xml_lexer_callback(self, "on_string", encoding, ts, te); + * liboga_xml_lexer_callback(self, "on_string", encoding, ts, te); */ -void oga_xml_lexer_callback( +void liboga_xml_lexer_callback( VALUE self, const char *name, rb_encoding *encoding, @@ -32,9 +32,9 @@ void oga_xml_lexer_callback( * arguments. * * @example - * oga_xml_lexer_callback_simple(self, "on_cdata_start"); + * liboga_xml_lexer_callback_simple(self, "on_cdata_start"); */ -void oga_xml_lexer_callback_simple(VALUE self, const char *name) +void liboga_xml_lexer_callback_simple(VALUE self, const char *name) { VALUE method = rb_intern(name); @@ -100,7 +100,7 @@ VALUE oga_xml_lexer_advance(VALUE self) # Machine for processing double quoted strings. string_dquote := |* ^dquote+ => { - oga_xml_lexer_callback(self, "on_string", encoding, ts, te); + liboga_xml_lexer_callback(self, "on_string", encoding, ts, te); }; dquote => { fret; }; @@ -109,7 +109,7 @@ VALUE oga_xml_lexer_advance(VALUE self) # Machine for processing single quoted strings. string_squote := |* ^squote+ => { - oga_xml_lexer_callback(self, "on_string", encoding, ts, te); + liboga_xml_lexer_callback(self, "on_string", encoding, ts, te); }; squote => { fret; }; @@ -128,7 +128,7 @@ VALUE oga_xml_lexer_advance(VALUE self) doctype_start = ' { - oga_xml_lexer_callback(self, "on_doctype_type", encoding, ts, te); + liboga_xml_lexer_callback(self, "on_doctype_type", encoding, ts, te); }; # Lex the public/system IDs as regular strings. @@ -148,11 +148,11 @@ VALUE oga_xml_lexer_advance(VALUE self) whitespace; identifier => { - oga_xml_lexer_callback(self, "on_doctype_name", encoding, ts, te); + liboga_xml_lexer_callback(self, "on_doctype_name", encoding, ts, te); }; '>' => { - oga_xml_lexer_callback_simple(self, "on_doctype_end"); + liboga_xml_lexer_callback_simple(self, "on_doctype_end"); fret; }; *|; @@ -171,7 +171,7 @@ VALUE oga_xml_lexer_advance(VALUE self) cdata_end = ']]>'; action start_cdata { - oga_xml_lexer_callback_simple(self, "on_cdata_start"); + liboga_xml_lexer_callback_simple(self, "on_cdata_start"); fcall cdata; } @@ -179,8 +179,8 @@ VALUE oga_xml_lexer_advance(VALUE self) # inside a CDATA tag is treated as plain text. cdata := |* any* cdata_end => { - oga_xml_lexer_callback(self, "on_text", encoding, ts, te - 3); - oga_xml_lexer_callback_simple(self, "on_cdata_end"); + liboga_xml_lexer_callback(self, "on_text", encoding, ts, te - 3); + liboga_xml_lexer_callback_simple(self, "on_cdata_end"); fret; }; *|; @@ -200,7 +200,7 @@ VALUE oga_xml_lexer_advance(VALUE self) comment_end = '-->'; action start_comment { - oga_xml_lexer_callback_simple(self, "on_comment_start"); + liboga_xml_lexer_callback_simple(self, "on_comment_start"); fcall comment; } @@ -208,8 +208,8 @@ VALUE oga_xml_lexer_advance(VALUE self) # inside a comment is treated as plain text (similar to CDATA tags). comment := |* any* comment_end => { - oga_xml_lexer_callback(self, "on_text", encoding, ts, te - 3); - oga_xml_lexer_callback_simple(self, "on_comment_end"); + liboga_xml_lexer_callback(self, "on_text", encoding, ts, te - 3); + liboga_xml_lexer_callback_simple(self, "on_comment_end"); fret; }; *|; @@ -222,20 +222,20 @@ VALUE oga_xml_lexer_advance(VALUE self) xml_decl_end = '?>'; action start_xml_decl { - oga_xml_lexer_callback_simple(self, "on_xml_decl_start"); + liboga_xml_lexer_callback_simple(self, "on_xml_decl_start"); fcall xml_decl; } # Machine that processes the contents of an XML declaration tag. xml_decl := |* xml_decl_end => { - oga_xml_lexer_callback_simple(self, "on_xml_decl_end"); + liboga_xml_lexer_callback_simple(self, "on_xml_decl_end"); fret; }; # Attributes and their values (e.g. version="1.0"). identifier => { - oga_xml_lexer_callback(self, "on_attribute", encoding, ts, te); + liboga_xml_lexer_callback(self, "on_attribute", encoding, ts, te); }; dquote => { fcall string_dquote; }; @@ -253,7 +253,7 @@ VALUE oga_xml_lexer_advance(VALUE self) # namespace (if any). Remaining work is delegated to a dedicated # machine. action start_element { - oga_xml_lexer_callback(self, "on_element_start", encoding, ts + 1, te); + liboga_xml_lexer_callback(self, "on_element_start", encoding, ts + 1, te); fcall element_head; } @@ -270,12 +270,12 @@ VALUE oga_xml_lexer_advance(VALUE self) whitespace | '='; newline => { - oga_xml_lexer_callback_simple(self, "on_newline"); + liboga_xml_lexer_callback_simple(self, "on_newline"); }; # Attribute names. identifier => { - oga_xml_lexer_callback(self, "on_attribute", encoding, ts, te); + liboga_xml_lexer_callback(self, "on_attribute", encoding, ts, te); }; # Attribute values. @@ -299,23 +299,23 @@ VALUE oga_xml_lexer_advance(VALUE self) # Enter the body of the tag. If HTML mode is enabled and the current # element is a void element we'll close it and bail out. '>' => { - oga_xml_lexer_callback_simple(self, "on_element_open_end"); + liboga_xml_lexer_callback_simple(self, "on_element_open_end"); }; # Regular closing tags. '' => { - oga_xml_lexer_callback_simple(self, "on_element_end"); + liboga_xml_lexer_callback_simple(self, "on_element_end"); }; # Self closing elements that are not handled by the HTML mode. '/>' => { - oga_xml_lexer_callback_simple(self, "on_element_end"); + liboga_xml_lexer_callback_simple(self, "on_element_end"); }; # Note that this rule should be declared at the very bottom as it # will otherwise take precedence over the other rules. ^('<' | '>')+ => { - oga_xml_lexer_callback(self, "on_text", encoding, ts, te); + liboga_xml_lexer_callback(self, "on_text", encoding, ts, te); }; *|; }%% From c30d3a762733329d3c5c1f64a7b13e65aaf97cb3 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 6 May 2014 00:23:07 +0200 Subject: [PATCH 2/2] Half-assed JRuby boilerplate. Blowing my brains out over getting this fat pig to do what I want but we're getting there. --- MANIFEST | 16 +++++++-------- Rakefile | 15 ++++++++++---- ext/liboga/LibogaService.java | 30 ++++++++++++++++++++++++++++ ext/liboga/org/liboga/xml/Lexer.java | 27 +++++++++++++++++++++++++ oga.gemspec | 8 +++++++- task/lexer.rake | 2 +- 6 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 ext/liboga/LibogaService.java create mode 100644 ext/liboga/org/liboga/xml/Lexer.java diff --git a/MANIFEST b/MANIFEST index 5b2c86a..f9a806f 100644 --- a/MANIFEST +++ b/MANIFEST @@ -5,14 +5,14 @@ README.md doc/DCO.md doc/changelog.md doc/css/common.css -ext/liboga/extconf.rb -ext/liboga/lexer.c -ext/liboga/lexer.h -ext/liboga/lexer.rl -ext/liboga/liboga.c -ext/liboga/liboga.h -ext/liboga/xml.c -ext/liboga/xml.h +ext/c/liboga/extconf.rb +ext/c/liboga/lexer.c +ext/c/liboga/lexer.h +ext/c/liboga/lexer.rl +ext/c/liboga/liboga.c +ext/c/liboga/liboga.h +ext/c/liboga/xml.c +ext/c/liboga/xml.h lib/oga.rb lib/oga/html/parser.rb lib/oga/version.rb diff --git a/Rakefile b/Rakefile index a25c691..21aeff4 100644 --- a/Rakefile +++ b/Rakefile @@ -1,11 +1,20 @@ require 'bundler/gem_tasks' require 'digest/sha2' require 'rake/clean' -require 'rake/extensiontask' require 'cliver' GEMSPEC = Gem::Specification.load('oga.gemspec') +if RUBY_PLATFORM == 'java' + require 'rake/javaextensiontask' + + Rake::JavaExtensionTask.new('liboga', GEMSPEC) +else + require 'rake/extensiontask' + + Rake::ExtensionTask.new('liboga', GEMSPEC) +end + PARSER_OUTPUT = 'lib/oga/xml/parser.rb' CLEAN.include( @@ -16,7 +25,7 @@ CLEAN.include( 'profile/samples/**/*.txt', 'lib/liboga.*', 'tmp', - 'ext/liboga/lexer.c' + 'ext/c/liboga/lexer.c' ) FILE_LIST = FileList.new( @@ -31,8 +40,6 @@ FILE_LIST = FileList.new( 'ext/**/*.*' ) -Rake::ExtensionTask.new('liboga', GEMSPEC) - Dir['./task/*.rake'].each do |task| import(task) end diff --git a/ext/liboga/LibogaService.java b/ext/liboga/LibogaService.java new file mode 100644 index 0000000..a132aaf --- /dev/null +++ b/ext/liboga/LibogaService.java @@ -0,0 +1,30 @@ +package org.liboga; + +import java.io.IOException; + +import org.jruby.Ruby; +import org.jruby.RubyModule; +import org.jruby.RubyClass; +import org.jruby.runtime.load.BasicLibraryService; +import org.jruby.runtime.load.Library; + +public class LibogaService implements BasicLibraryService +{ + public boolean basicLoad(final Ruby runtime) throws IOException + { + // Calling getModule on an instance of RubyModule? Nah fuck + // that, that would be too easy. + RubyModule xml = (RubyModule) runtime.getModule("Oga") + .getConstant("XML"); + + RubyClass lexer = xml.defineClassUnder( + "Lexer", + runtime.getObject(), + runtime.getObject().getAllocator() + ); + + lexer.defineAnnotatedMethods(org.liboga.xml.Lexer.class); + + return true; + } +} diff --git a/ext/liboga/org/liboga/xml/Lexer.java b/ext/liboga/org/liboga/xml/Lexer.java new file mode 100644 index 0000000..e8b79cd --- /dev/null +++ b/ext/liboga/org/liboga/xml/Lexer.java @@ -0,0 +1,27 @@ +package org.liboga.xml; + +import java.io.IOException; + +import org.jruby.Ruby; +import org.jruby.RubyModule; +import org.jruby.RubyClass; +import org.jruby.RubyObject; +import org.jruby.anno.JRubyClass; +import org.jruby.anno.JRubyMethod; +import org.jruby.runtime.ThreadContext; +import org.jruby.runtime.builtin.IRubyObject; + +@JRubyClass(name="Oga::XML::Lexer", parent="Object") +public class Lexer extends RubyObject +{ + public Lexer(Ruby runtime, RubyClass klass) + { + super(runtime, klass); + } + + @JRubyMethod + public IRubyObject advance_native(ThreadContext context) + { + return context.getRuntime().getNil(); + } +} diff --git a/oga.gemspec b/oga.gemspec index 40407f4..9b286eb 100644 --- a/oga.gemspec +++ b/oga.gemspec @@ -12,7 +12,13 @@ Gem::Specification.new do |s| s.files = File.read(File.expand_path('../MANIFEST', __FILE__)).split("\n") - s.extensions = ['ext/liboga/extconf.rb'] + if RUBY_PLATFORM == 'java' + s.files << 'lib/liboga.jar' + + s.platform = 'java' + else + s.extensions = ['ext/c/liboga/extconf.rb'] + end s.has_rdoc = 'yard' s.required_ruby_version = '>= 1.9.3' diff --git a/task/lexer.rake b/task/lexer.rake index 8823bea..9d3fc91 100644 --- a/task/lexer.rake +++ b/task/lexer.rake @@ -25,4 +25,4 @@ rule '.c' => '.rl' do |task| end desc 'Generates the lexers' -task :lexer => ['ext/liboga/lexer.c'] +task :lexer => ['ext/c/liboga/lexer.c']