Skip to content

Commit 1d6894e

Browse files
committed
First take on Ruby extension
1 parent 9696e84 commit 1d6894e

File tree

26 files changed

+292
-64
lines changed

26 files changed

+292
-64
lines changed

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ PATH
22
remote: .
33
specs:
44
erbx (0.0.1)
5+
ffi
56

67
GEM
78
remote: https://rubygems.org/
89
specs:
10+
ffi (1.17.0)
11+
ffi (1.17.0-x86_64-darwin)
912
maxitest (5.5.0)
1013
minitest (>= 5.14.0, < 5.24.0)
1114
minitest (5.23.1)

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ test_sources = $(wildcard test/*.c)
88
test_objects = $(test_sources:.c=.o)
99
non_main_objects = $(filter-out src/main.o, $(objects))
1010

11+
soext ?= $(shell ruby -e 'puts RbConfig::CONFIG["SOEXT"]')
12+
lib_name = lib$(exec).$(soext)
13+
ruby_extension = ext/erbx/$(lib_name)
14+
1115
os := $(shell uname -s)
1216

1317
flags = -g -Wall -fPIC
1418

1519
ifeq ($(os),Linux)
1620
test_cflags = $(flags) -I/usr/include/check
1721
test_ldflags = -L/usr/lib/x86_64-linux-gnu -lcheck -lm -lsubunit
18-
lib_name = $(exec).so
19-
ruby_extension = ext/erbx/$(lib_name)
2022
endif
2123

2224
ifeq ($(os),Darwin)
2325
test_cflags = $(flags) -I/usr/local/include
2426
test_ldflags = -L/usr/local/lib -lcheck -lm
25-
lib_name = lib$(exec).so
26-
ruby_extension = ext/erbx/$(lib_name)
2727
endif
2828

2929
all: $(exec) $(lib_name) test
@@ -33,7 +33,7 @@ $(exec): $(objects)
3333

3434
$(lib_name): $(objects)
3535
gcc -shared $(objects) $(flags) -o $(lib_name)
36-
cp $(lib_name) $(ruby_extension)
36+
# cp $(lib_name) $(ruby_extension)
3737

3838
%.o: %.c include/%.h
3939
gcc -c $(flags) $< -o $@

erbx.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# frozen_string_literal: true
22

3-
require_relative "lib/erbx/version"
4-
53
Gem::Specification.new do |spec|
64
spec.name = "erbx"
7-
spec.version = ERBX::VERSION
5+
spec.version = "0.0.1"
86
spec.authors = ["Marco Roth"]
97
spec.email = ["marco.roth@intergga.ch"]
108

@@ -26,6 +24,8 @@ Gem::Specification.new do |spec|
2624
spec.metadata["source_code_uri"] = "https://github.com/marcoroth/erbx"
2725
spec.metadata["changelog_uri"] = "https://github.com/marcoroth/erbx/releases"
2826

27+
spec.add_dependency "ffi"
28+
2929
spec.add_development_dependency "rake", "~> 13.2"
3030
spec.add_development_dependency "rake-compiler", "~> 1.2"
3131
spec.add_development_dependency "maxitest"

examples/ruby/test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
require "erbx"
22

3-
puts ERBX::Compiler.new.compile("<html><html>")
3+
puts ERBX::Lexer.lex("<html><html>")

ext/erbx/extconf.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
end
1111

1212
expected_functions = [
13-
"erbx_compile",
14-
"erbx_compile_file",
13+
"erbx_lex",
14+
"erbx_lex_file",
1515
]
1616

1717
expected_functions.each do |expected_function|

ext/erbx/extension.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ static const char *check_string(VALUE value) {
1616
return RSTRING_PTR(value);
1717
}
1818

19-
VALUE rb_erbx_compile(VALUE self, VALUE source) {
19+
VALUE rb_erbx_lex(VALUE self, VALUE source) {
2020
const char *string = check_string(source);
2121

22-
erbx_compile((char *) string);
22+
erbx_lex((char *) string);
2323

2424
return Qnil;
2525
}
2626

2727
void Init_erbx() {
28-
VALUE ERBX = rb_define_module("ERBX");
29-
VALUE Compiler = rb_define_class_under(ERBX, "Compiler", rb_cObject);
28+
VALUE ERBX = rb_define_module("LibERBX");
29+
VALUE Lexer = rb_define_class_under(ERBX, "Lexer", rb_cObject);
3030

31-
rb_define_method(Compiler, "compile", rb_erbx_compile, 1);
31+
rb_define_method(Lexer, "lex", rb_erbx_lex, 1);
3232
}

ext/erbx/test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
require "./erbx"
22

3-
ERBX::Compiler.new.compile("<html><html>")
3+
ERBX::Lexer.lex("<html><html>")

ext/erbx/test/compiler_test.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

ext/erbx/test/lexer_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "test_helper"
4+
5+
class LexerTest < Minitest::Test
6+
def test_lexer
7+
assert_equal "test", ERBX::Lexer.lex("<html></html>")
8+
end
9+
end

lib/erbx.rb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
1-
require "erbx/erbx"
2-
require "erbx/version"
1+
require "erbx/liberbx"
2+
require "erbx/liberbx/buffer"
3+
require "erbx/liberbx/array"
4+
require "erbx/liberbx/token"
5+
6+
require "erbx/lex_result"
7+
8+
module ERBX
9+
VERSION = LibERBX.erbx_version.read_string
10+
11+
def self.lex_to_buffer(source)
12+
LibERBX::Buffer.with do |output|
13+
LibERBX.erbx_lex_to_buffer(source, output.pointer)
14+
15+
output.read
16+
end
17+
end
18+
19+
def self.lex(source)
20+
LexResult.new(
21+
LibERBX.erbx_lex(source)
22+
)
23+
end
24+
25+
def self.lex_file(path)
26+
lex(File.read(path))
27+
end
28+
end

0 commit comments

Comments
 (0)