Skip to content

Commit

Permalink
Simplify and unify how native code (JavaScript, Java, C) is loaded. M…
Browse files Browse the repository at this point in the history
…otivation: easier to run test suite over different native impls.
  • Loading branch information
aslakhellesoy committed Jun 26, 2012
1 parent 9ba07cb commit 6446921
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 77 deletions.
7 changes: 5 additions & 2 deletions java/src/main/java/gherkin/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ public void parse(String gherkin, String featureURI, Integer lineOffset) {
this.featureURI = featureURI;
this.lineOffset = lineOffset;
pushMachine(machineName);
lexer.scan(gherkin);
popMachine();
try {
lexer.scan(gherkin);
} finally {
popMachine();
}
}

public I18n getI18nLanguage() {
Expand Down
9 changes: 2 additions & 7 deletions lib/gherkin/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,8 @@ def c(listener)
end

def rb(listener)
require 'gherkin/rb_lexer'
RbLexer[underscored_iso_code].new(listener)
end

def js(listener)
require 'gherkin/js_lexer'
JsLexer[underscored_iso_code].new(listener)
require "gherkin/rb_lexer/#{underscored_iso_code}"
RbLexer.const_get(underscored_iso_code.capitalize).new(listener)
end

def underscored_iso_code
Expand Down
20 changes: 0 additions & 20 deletions lib/gherkin/js_lexer.rb

This file was deleted.

3 changes: 2 additions & 1 deletion lib/gherkin/native/therubyracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ def native_impl(lib)
class << self
def new(*args)
js = {
'Gherkin::Formatter::JSONFormatter' => 'js/lib/gherkin/formatter/json_formatter.js'
'Gherkin::Formatter::JSONFormatter' => 'js/lib/gherkin/formatter/json_formatter.js',
'Gherkin::RbLexer::En' => 'js/lib/gherkin/lexer/en.js'
}[self.name]
if(js)
Proxy.new(js, *args)
Expand Down
23 changes: 12 additions & 11 deletions lib/gherkin/parser/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ def initialize(formatter, raise_on_error=true, machine_name='root', force_ruby=f
@raise_on_error = raise_on_error
@machine_name = machine_name
@machines = []
push_machine(@machine_name)
@lexer = Gherkin::Lexer::I18nLexer.new(self, force_ruby)
end

def parse(gherkin, feature_uri, line_offset)
@formatter.uri(feature_uri)
@line_offset = line_offset
@lexer.scan(gherkin)
push_machine(@machine_name)
begin
@lexer.scan(gherkin)
ensure
pop_machine
end
end

def i18n_language
Expand All @@ -39,17 +43,14 @@ def errors
@lexer.errors
end

# Doesn't yet fall back to super
def method_missing(method, *args)
# TODO: Catch exception and call super
event(method.to_s, args[-1])
@listener.__send__(method, *args)
if method == :eof
pop_machine
push_machine(@machine_name)
[:comment, :tag, :feature, :background, :scenario, :scenario_outline, :examples, :step, :doc_string, :row, :eof].each do |m|
define_method(m) do |*args|
if(event(m.to_s, args[-1]))
@listener.__send__(m, *args)
end
end
end

def event(ev, line)
l = line ? @line_offset+line : nil
machine.event(ev, l) do |state, legal_events|
Expand Down
8 changes: 0 additions & 8 deletions lib/gherkin/rb_lexer.rb

This file was deleted.

8 changes: 4 additions & 4 deletions ragel/lexer.js.rl.erb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ var Lexer = function(listener) {
for(e in events) {
var event = events[e];
if(typeof listener[event] != 'function') {
"Error. No " + event + " function exists on " + JSON.stringify(listener);
throw "Error. No " + event + " function exists on " + JSON.stringify(listener);
}
}
this.listener = listener;
Expand Down Expand Up @@ -208,8 +208,8 @@ Lexer.prototype.current_line_content = function(data, p) {
};

// Node.js export
if(typeof exports !== 'undefined') {
exports.Lexer = Lexer;
if(typeof module !== 'undefined') {
module.exports = Lexer;
}
// Require.js export
if (typeof define !== 'undefined') {
Expand All @@ -224,4 +224,4 @@ if (typeof define !== 'undefined') {
}
}

})();
})();
4 changes: 3 additions & 1 deletion ragel/lexer.rb.rl.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require 'gherkin/lexer/i18n_lexer'
require 'gherkin/native'

module Gherkin
module RbLexer
class <%= @i18n.underscored_iso_code.capitalize %> #:nodoc:
native_impl('gherkin')

%%{
machine lexer;

Expand Down
23 changes: 0 additions & 23 deletions spec/gherkin/js_lexer_spec.rb

This file was deleted.

0 comments on commit 6446921

Please sign in to comment.