From ca9ef0d4f7ff9b561e9f0ecb9c0532b01ada15ec Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Fri, 14 Jul 2023 13:41:20 -0300 Subject: [PATCH] Looks like I broke compilation??? --- spec/shortcodes_spec.cr | 9 ++++--- src/Makefile | 1 + src/run_tests.cr | 1 + src/shortcodes.c | 4 +-- src/shortcodes.cr | 58 +++++++++++++++++++++++++++++++++++++++-- src/shortcodes.h | 2 +- src/shortcodes.rl | 4 +-- 7 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 src/run_tests.cr diff --git a/spec/shortcodes_spec.cr b/spec/shortcodes_spec.cr index 4d9f778..4faa920 100644 --- a/spec/shortcodes_spec.cr +++ b/spec/shortcodes_spec.cr @@ -1,12 +1,15 @@ require "./spec_helper" +include Shortcodes describe "Shortcodes" do # TODO: Write tests it "works" do - result = Shortcodes.parse("foo{{% bar %}}baz{{% /bar %}}qux") - result.sccount.should eq 1 - result.shortcodes[0].matching.should eq 1 + parse("foo{{% bar %}}baz{{% /bar %}}qux") + # p! result.shortcodes + # result.shortcodes.size.should eq 1 + # result.shortcodes[0].args.size.should eq 0 + # result.shortcodes[0].name.should eq "bar" end end diff --git a/src/Makefile b/src/Makefile index a22d229..fe32e2c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,3 +1,4 @@ +all: shortcodes.o shortcodes.c: shortcodes.rl ragel -G2 shortcodes.rl -o shortcodes.c clean: diff --git a/src/run_tests.cr b/src/run_tests.cr new file mode 100644 index 0000000..a567ffd --- /dev/null +++ b/src/run_tests.cr @@ -0,0 +1 @@ +require "../spec/**" diff --git a/src/shortcodes.c b/src/shortcodes.c index b89e8cf..fe4203d 100644 --- a/src/shortcodes.c +++ b/src/shortcodes.c @@ -8,7 +8,7 @@ -sc_result parse(char *input) { +sc_result parse(char *input, unsigned int len) { #line 15 "shortcodes.c" @@ -25,7 +25,7 @@ static const int shortcode_en_main = 141; char *start = input; char *p = input; - char *pe = p + strlen(input); + char *pe = p + len; sc_result result; shortcode *sc_list = result.sc; diff --git a/src/shortcodes.cr b/src/shortcodes.cr index 9f70028..3e07af5 100644 --- a/src/shortcodes.cr +++ b/src/shortcodes.cr @@ -1,5 +1,5 @@ @[Link(ldflags: "#{__DIR__}/shortcodes.o")] -lib Shortcodes +lib LibShortcodes struct Chunk start : UInt32 len : UInt32 @@ -27,5 +27,59 @@ lib Shortcodes errcount : UInt32 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 diff --git a/src/shortcodes.h b/src/shortcodes.h index c662f56..d29c415 100644 --- a/src/shortcodes.h +++ b/src/shortcodes.h @@ -57,4 +57,4 @@ Example: #define ERR_MISMATCHED_BRACKET 2 -sc_result parse(char *); +sc_result parse(char *, unsigned int); diff --git a/src/shortcodes.rl b/src/shortcodes.rl index 1588bce..f1d536f 100644 --- a/src/shortcodes.rl +++ b/src/shortcodes.rl @@ -110,7 +110,7 @@ }%% -sc_result parse(char *input) { +sc_result parse(char *input, unsigned int len) { %%write data; char *eof, *ts, *te = 0; @@ -118,7 +118,7 @@ sc_result parse(char *input) { char *start = input; char *p = input; - char *pe = p + strlen(input); + char *pe = p + len; sc_result result; shortcode *sc_list = result.sc;