Looks like I broke compilation???

This commit is contained in:
Roberto Alsina 2023-07-14 13:41:20 -03:00
parent 817eea895a
commit ca9ef0d4f7
7 changed files with 69 additions and 10 deletions

View File

@ -1,12 +1,15 @@
require "./spec_helper" require "./spec_helper"
include Shortcodes
describe "Shortcodes" do describe "Shortcodes" do
# TODO: Write tests # TODO: Write tests
it "works" do it "works" do
result = Shortcodes.parse("foo{{% bar %}}baz{{% /bar %}}qux") parse("foo{{% bar %}}baz{{% /bar %}}qux")
result.sccount.should eq 1 # p! result.shortcodes
result.shortcodes[0].matching.should eq 1 # result.shortcodes.size.should eq 1
# result.shortcodes[0].args.size.should eq 0
# result.shortcodes[0].name.should eq "bar"
end end
end end

View File

@ -1,3 +1,4 @@
all: shortcodes.o
shortcodes.c: shortcodes.rl shortcodes.c: shortcodes.rl
ragel -G2 shortcodes.rl -o shortcodes.c ragel -G2 shortcodes.rl -o shortcodes.c
clean: clean:

1
src/run_tests.cr Normal file
View File

@ -0,0 +1 @@
require "../spec/**"

View File

@ -8,7 +8,7 @@
sc_result parse(char *input) { sc_result parse(char *input, unsigned int len) {
#line 15 "shortcodes.c" #line 15 "shortcodes.c"
@ -25,7 +25,7 @@ static const int shortcode_en_main = 141;
char *start = input; char *start = input;
char *p = input; char *p = input;
char *pe = p + strlen(input); char *pe = p + len;
sc_result result; sc_result result;
shortcode *sc_list = result.sc; shortcode *sc_list = result.sc;

View File

@ -1,5 +1,5 @@
@[Link(ldflags: "#{__DIR__}/shortcodes.o")] @[Link(ldflags: "#{__DIR__}/shortcodes.o")]
lib Shortcodes lib LibShortcodes
struct Chunk struct Chunk
start : UInt32 start : UInt32
len : UInt32 len : UInt32
@ -27,5 +27,59 @@ lib Shortcodes
errcount : UInt32 errcount : UInt32
end end
fun parse(input : Pointer(LibC::Char)) : ScResult fun parse(input : Pointer(LibC::Char), len : UInt32) : ScResult
end
module Shortcodes
struct Arg
property name : String = ""
property value : String = ""
def initialize(@name, @value)
end
end
struct Shortcode
property name : String = ""
property data : String = ""
property matching : Int32 = 0
property args : Array(Arg) = [] of Arg
def initialize(@name, @data, @matching, @args)
end
end
struct Result
property shortcodes : Array(Shortcode) = [] of Shortcode
end
def extract(c : LibShortcodes::Chunk, s : String)
s[c.start, c.len]
end
def parse(input : String)
p! 111
r = LibShortcodes.parse(input.to_unsafe, input.bytesize)
p! 222
result = Result.new
r.shortcodes.each_with_index.map do |sc, i|
next if i >= r.sccount
args = [] of Arg
sc.argnames.each_with_index.map do |_, j|
next if i >= sc.argcount
args << Arg.new(
extract(sc.argnames[j], input),
extract(sc.argvals[j], input),
)
end
result.shortcodes << Shortcode.new(
extract(sc.name, input),
extract(sc.data, input),
sc.matching,
args,
)
end
r
end
end end

View File

@ -57,4 +57,4 @@ Example:
#define ERR_MISMATCHED_BRACKET 2 #define ERR_MISMATCHED_BRACKET 2
sc_result parse(char *); sc_result parse(char *, unsigned int);

View File

@ -110,7 +110,7 @@
}%% }%%
sc_result parse(char *input) { sc_result parse(char *input, unsigned int len) {
%%write data; %%write data;
char *eof, *ts, *te = 0; char *eof, *ts, *te = 0;
@ -118,7 +118,7 @@ sc_result parse(char *input) {
char *start = input; char *start = input;
char *p = input; char *p = input;
char *pe = p + strlen(input); char *pe = p + len;
sc_result result; sc_result result;
shortcode *sc_list = result.sc; shortcode *sc_list = result.sc;