rebus 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/rebus/compiler.rb +100 -0
- data/lib/rebus/context.rb +17 -0
- data/lib/rebus/version.rb +3 -0
- data/lib/rebus.rb +50 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: baa46618c42ed2cbe33450f45f0cb8a47aa8a579ae6b32cb35033335b67c48b8
|
4
|
+
data.tar.gz: 689d4892b290ee8664529fc938b69946d6665fa6d55bb7fbb3ad41781e6444dd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ccc9d6db824f8cc01acb1b824614bf0fdb59d0c53d0763ea20caa44731dda006d01181c6f266d9d2c844900760c967edbbac6d1fd5da95dc718612ed4859aeb6
|
7
|
+
data.tar.gz: 0d66b98e982ed01a3bd00db4f9493a92389cda0a9d0f6fed8af0c5970c496b931a9e9a91aad82bd2a127bd6891557d64317e059dc8872999b6e3d41b32ae0326
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
require 'modeling'
|
3
|
+
|
4
|
+
module Rebus
|
5
|
+
class Compiler
|
6
|
+
|
7
|
+
model :code_prefix, :comment_prefix, :context do
|
8
|
+
@code_prefix ||= Rebus.code_prefix
|
9
|
+
@comment_prefix ||= Rebus.comment_prefix
|
10
|
+
reset_lines
|
11
|
+
end
|
12
|
+
|
13
|
+
def reset_lines
|
14
|
+
@current_line = 1
|
15
|
+
@string_lines = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def compile source
|
19
|
+
return compile source do;end if !block_given?
|
20
|
+
|
21
|
+
reset_lines
|
22
|
+
parsed = parse source
|
23
|
+
error = nil
|
24
|
+
|
25
|
+
begin
|
26
|
+
if source.is_a? File
|
27
|
+
context.instance_eval parsed, source.path
|
28
|
+
else
|
29
|
+
context.instance_eval parsed
|
30
|
+
end
|
31
|
+
rescue ScriptError, StandardError => err
|
32
|
+
error = err
|
33
|
+
end
|
34
|
+
if error
|
35
|
+
raise prepare_error error
|
36
|
+
end
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
def parse source
|
41
|
+
source = case source
|
42
|
+
when String
|
43
|
+
StringIO.new source
|
44
|
+
when IO
|
45
|
+
source
|
46
|
+
else
|
47
|
+
raise "Unsupported source #{source.class}"
|
48
|
+
end
|
49
|
+
|
50
|
+
escaped_code_prefix = Regexp.escape @code_prefix
|
51
|
+
line_regexp = /^\s*(#{@comment_prefix})?(#{escaped_code_prefix})?\s*(.+)\s*$/
|
52
|
+
source.each_line.map{ parse_line _1, line_regexp }.compact.join "\n"
|
53
|
+
end
|
54
|
+
|
55
|
+
def parse_line line, regexp
|
56
|
+
if line =~ regexp
|
57
|
+
if $1 # comment
|
58
|
+
@current_line += 1
|
59
|
+
return ""
|
60
|
+
else
|
61
|
+
if $2 # code
|
62
|
+
@current_line += 1
|
63
|
+
return $3
|
64
|
+
else # string
|
65
|
+
@string_lines << @current_line + 1
|
66
|
+
@current_line += 3
|
67
|
+
return "yield <<-\"#{@code_prefix}\".chop\n#{$3}\n#{@code_prefix}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
else # blank line
|
71
|
+
@current_line += 1
|
72
|
+
return "yield ''"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def prepare_error error
|
79
|
+
lines = error.message.split("\n")
|
80
|
+
path_regexp = /^(.*:)(\d+):(.*)/
|
81
|
+
if lines[0] =~ path_regexp
|
82
|
+
lineno = $2.to_i
|
83
|
+
lineno -= @string_lines.map{ (lineno <=> _1) + 1 }.sum
|
84
|
+
backtrace = [$1 + lineno.to_s, *error.backtrace]
|
85
|
+
message = "#{$3}\n#{lines[1..]&.join "\n"}"
|
86
|
+
elsif error.backtrace[0] =~ path_regexp
|
87
|
+
lineno = $2.to_i
|
88
|
+
lineno -= @string_lines.map{ _1 <= lineno ? _1 < lineno ? 2 : 1 : 0 }.sum
|
89
|
+
backtrace = [$1 + lineno.to_s, *error.backtrace[1..]]
|
90
|
+
message = "#{lines&.join "\n"}"
|
91
|
+
else
|
92
|
+
backtrace = error.backtrace
|
93
|
+
message = error.message
|
94
|
+
end
|
95
|
+
new_error = SyntaxError.new message
|
96
|
+
new_error.set_backtrace backtrace
|
97
|
+
return new_error
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rebus
|
2
|
+
class Context
|
3
|
+
def to_s
|
4
|
+
"#{inspect}:Context"
|
5
|
+
end
|
6
|
+
|
7
|
+
def inspect
|
8
|
+
"[#{instance_variables.join ", "}]"
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.generate **variables
|
12
|
+
Class.new(self) do
|
13
|
+
model *variables.keys
|
14
|
+
end.new(*variables.values)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/rebus.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require_relative 'rebus/compiler'
|
2
|
+
require_relative 'rebus/context'
|
3
|
+
|
4
|
+
module Rebus
|
5
|
+
class << self
|
6
|
+
attr_accessor :code_prefix
|
7
|
+
attr_accessor :comment_prefix
|
8
|
+
attr_accessor :home
|
9
|
+
|
10
|
+
def compile_file filename, context = nil, **na, &block
|
11
|
+
if !File.absolute_path? filename
|
12
|
+
home = self.home || File.dirname(File.absolute_path?($0) ? $0 : Dir.getwd + "/" + $0)
|
13
|
+
filename = home + "/" + filename
|
14
|
+
end
|
15
|
+
|
16
|
+
File.open filename do |file|
|
17
|
+
compile file, context, **na, &block
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def compile source, context = nil, **na, &block
|
22
|
+
case context
|
23
|
+
when nil
|
24
|
+
context = Context.generate **(block&.binding&.then{|b| b.local_variables.map{|v| [v, b.local_variable_get(v)]}.to_h} || {}), **na
|
25
|
+
compile source, Compiler.new(context: context), &block
|
26
|
+
when Binding
|
27
|
+
context = Context.generate **context.local_variables.map{ [_1, context.local_variable_get(_1)] }.to_h, **na
|
28
|
+
compile source, Compiler.new(context: context), &block
|
29
|
+
when Hash
|
30
|
+
context = Context.generate **context, **na
|
31
|
+
compile source, Compiler.new(context: context), &block
|
32
|
+
when Compiler
|
33
|
+
if block
|
34
|
+
context.compile source, &block
|
35
|
+
else
|
36
|
+
array = []
|
37
|
+
context.compile source do |line|
|
38
|
+
array << line
|
39
|
+
end
|
40
|
+
array.join "\n"
|
41
|
+
end
|
42
|
+
else
|
43
|
+
compile source, Compiler.new(context: context), &block
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
self.code_prefix = "|>"
|
49
|
+
self.comment_prefix = "#[^{@$]"
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rebus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Łukasz Pomietło
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-03-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: modeling
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.0.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.4
|
27
|
+
description: "Universal template compiler based on ruby dynamic evaluation feature.
|
28
|
+
\nMinimalistic design, customizable tokens, comments support, easy debugging.\n"
|
29
|
+
email: oficjalnyadreslukasza@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/rebus.rb
|
35
|
+
- lib/rebus/compiler.rb
|
36
|
+
- lib/rebus/context.rb
|
37
|
+
- lib/rebus/version.rb
|
38
|
+
homepage: https://github.com/lpogic/rebus
|
39
|
+
licenses:
|
40
|
+
- Zlib
|
41
|
+
metadata:
|
42
|
+
documentation_uri: https://github.com/lpogic/rebus/blob/main/doc/wiki/README.md
|
43
|
+
homepage_uri: https://github.com/lpogic/rebus
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 3.2.2
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubygems_version: 3.5.6
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: ruby stencil compiler
|
63
|
+
test_files: []
|