herb 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. checksums.yaml +5 -5
  2. data/{LICENSE → LICENSE.txt} +4 -3
  3. data/Makefile +121 -0
  4. data/README.md +102 -107
  5. data/Rakefile +184 -0
  6. data/exe/herb +5 -0
  7. data/ext/herb/error_helpers.c +302 -0
  8. data/ext/herb/error_helpers.h +15 -0
  9. data/ext/herb/extconf.rb +75 -0
  10. data/ext/herb/extension.c +110 -0
  11. data/ext/herb/extension.h +6 -0
  12. data/ext/herb/extension_helpers.c +117 -0
  13. data/ext/herb/extension_helpers.h +24 -0
  14. data/ext/herb/nodes.c +936 -0
  15. data/ext/herb/nodes.h +12 -0
  16. data/herb.gemspec +49 -0
  17. data/lib/herb/ast/node.rb +61 -0
  18. data/lib/herb/ast/nodes.rb +1542 -0
  19. data/lib/herb/ast.rb +6 -0
  20. data/lib/herb/cli.rb +164 -0
  21. data/lib/herb/errors.rb +352 -0
  22. data/lib/herb/lex_result.rb +20 -0
  23. data/lib/herb/libherb/array.rb +48 -0
  24. data/lib/herb/libherb/ast_node.rb +47 -0
  25. data/lib/herb/libherb/buffer.rb +53 -0
  26. data/lib/herb/libherb/extract_result.rb +17 -0
  27. data/lib/herb/libherb/lex_result.rb +29 -0
  28. data/lib/herb/libherb/libherb.rb +49 -0
  29. data/lib/herb/libherb/parse_result.rb +17 -0
  30. data/lib/herb/libherb/token.rb +43 -0
  31. data/lib/herb/libherb.rb +32 -0
  32. data/lib/herb/location.rb +42 -0
  33. data/lib/herb/parse_result.rb +26 -0
  34. data/lib/herb/position.rb +36 -0
  35. data/lib/herb/project.rb +361 -0
  36. data/lib/herb/range.rb +40 -0
  37. data/lib/herb/result.rb +21 -0
  38. data/lib/herb/token.rb +43 -0
  39. data/lib/herb/token_list.rb +11 -0
  40. data/lib/herb/version.rb +5 -0
  41. data/lib/herb.rb +21 -68
  42. data/src/analyze.c +989 -0
  43. data/src/analyze_helpers.c +241 -0
  44. data/src/analyzed_ruby.c +35 -0
  45. data/src/array.c +137 -0
  46. data/src/ast_node.c +81 -0
  47. data/src/ast_nodes.c +866 -0
  48. data/src/ast_pretty_print.c +588 -0
  49. data/src/buffer.c +199 -0
  50. data/src/errors.c +740 -0
  51. data/src/extract.c +110 -0
  52. data/src/herb.c +103 -0
  53. data/src/html_util.c +143 -0
  54. data/src/include/analyze.h +36 -0
  55. data/src/include/analyze_helpers.h +43 -0
  56. data/src/include/analyzed_ruby.h +33 -0
  57. data/src/include/array.h +33 -0
  58. data/src/include/ast_node.h +35 -0
  59. data/src/include/ast_nodes.h +303 -0
  60. data/src/include/ast_pretty_print.h +17 -0
  61. data/src/include/buffer.h +36 -0
  62. data/src/include/errors.h +125 -0
  63. data/src/include/extract.h +20 -0
  64. data/src/include/herb.h +32 -0
  65. data/src/include/html_util.h +13 -0
  66. data/src/include/io.h +9 -0
  67. data/src/include/json.h +28 -0
  68. data/src/include/lexer.h +13 -0
  69. data/src/include/lexer_peek_helpers.h +23 -0
  70. data/src/include/lexer_struct.h +32 -0
  71. data/src/include/location.h +25 -0
  72. data/src/include/macros.h +10 -0
  73. data/src/include/memory.h +12 -0
  74. data/src/include/parser.h +22 -0
  75. data/src/include/parser_helpers.h +33 -0
  76. data/src/include/position.h +22 -0
  77. data/src/include/pretty_print.h +53 -0
  78. data/src/include/prism_helpers.h +18 -0
  79. data/src/include/range.h +23 -0
  80. data/src/include/ruby_parser.h +6 -0
  81. data/src/include/token.h +25 -0
  82. data/src/include/token_matchers.h +21 -0
  83. data/src/include/token_struct.h +51 -0
  84. data/src/include/util.h +25 -0
  85. data/src/include/version.h +6 -0
  86. data/src/include/visitor.h +11 -0
  87. data/src/io.c +30 -0
  88. data/src/json.c +205 -0
  89. data/src/lexer.c +284 -0
  90. data/src/lexer_peek_helpers.c +59 -0
  91. data/src/location.c +41 -0
  92. data/src/main.c +162 -0
  93. data/src/memory.c +53 -0
  94. data/src/parser.c +704 -0
  95. data/src/parser_helpers.c +161 -0
  96. data/src/position.c +33 -0
  97. data/src/pretty_print.c +242 -0
  98. data/src/prism_helpers.c +50 -0
  99. data/src/range.c +38 -0
  100. data/src/ruby_parser.c +47 -0
  101. data/src/token.c +194 -0
  102. data/src/token_matchers.c +32 -0
  103. data/src/util.c +128 -0
  104. data/src/visitor.c +321 -0
  105. metadata +126 -82
  106. data/test/helper.rb +0 -7
  107. data/test/helpers_test.rb +0 -25
  108. data/test/parsing_test.rb +0 -110
data/lib/herb/token.rb ADDED
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Herb
4
+ class Token
5
+ attr_reader :value, :range, :location, :type
6
+
7
+ def initialize(value, range, location, type)
8
+ @value = value
9
+ @range = range
10
+ @location = location
11
+ @type = type
12
+ end
13
+
14
+ def to_hash
15
+ {
16
+ value: value,
17
+ range: range&.to_a,
18
+ location: location&.to_hash,
19
+ type: type,
20
+ }
21
+ end
22
+
23
+ def to_json(*args)
24
+ to_hash.to_json(*args)
25
+ end
26
+
27
+ def tree_inspect
28
+ %("#{value}" #{location.tree_inspect})
29
+ end
30
+
31
+ def value_inspect
32
+ if type == "TOKEN_EOF"
33
+ "<EOF>".inspect
34
+ else
35
+ value.inspect
36
+ end
37
+ end
38
+
39
+ def inspect
40
+ %(#<Herb::Token type="#{type}" value=#{value_inspect} range=#{range.tree_inspect} start=#{location.start.tree_inspect} end=#{location.end.tree_inspect}>)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delegate"
4
+
5
+ module Herb
6
+ class TokenList < SimpleDelegator
7
+ def inspect
8
+ "#{itself.map(&:inspect).join("\n").force_encoding("utf-8")}\n"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Herb
4
+ VERSION = "0.1.0"
5
+ end
data/lib/herb.rb CHANGED
@@ -1,79 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (c) 2016 Francesco Rodríguez
4
- # Copyright (c) 2011-2016 Michel Martens (https://github.com/soveran/mote)
5
- #
6
- # Permission is hereby granted, free of charge, to any person obtaining a copy
7
- # of this software and associated documentation files (the "Software"), to deal
8
- # in the Software without restriction, including without limitation the rights
9
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- # copies of the Software, and to permit persons to whom the Software is
11
- # furnished to do so, subject to the following conditions:
12
- #
13
- # The above copyright notice and this permission notice shall be included in
14
- # all copies or substantial portions of the Software.
15
- #
16
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
- # THE SOFTWARE.
3
+ require_relative "herb/range"
4
+ require_relative "herb/position"
5
+ require_relative "herb/location"
23
6
 
24
- module Herb
25
- PATTERN = /(<%)\s+(.*?)\s+%>(?:\n)|(<%==?)(.*?)%>/m
26
-
27
- def self.parse(template, vars = [], context = self)
28
- terms = template.split(PATTERN)
29
- parts = "proc do |params = {}, __o = ''|\n".dup
30
-
31
- vars.each { |var| parts << sprintf("%s = params[%p]\n", var, var) }
32
-
33
- while (term = terms.shift)
34
- parts << parse_expression(terms, term)
35
- end
7
+ require_relative "herb/token"
8
+ require_relative "herb/token_list"
36
9
 
37
- parts << "__o; end"
10
+ require_relative "herb/result"
11
+ require_relative "herb/lex_result"
12
+ require_relative "herb/parse_result"
38
13
 
39
- compile(context, parts)
40
- end
14
+ require_relative "herb/ast"
15
+ require_relative "herb/ast/node"
16
+ require_relative "herb/ast/nodes"
41
17
 
42
- def self.parse_expression(terms, term)
43
- case term
44
- when "<%" then terms.shift << "\n"
45
- when "<%=" then "__o << Herb.h((" + terms.shift << ").to_s)\n"
46
- when "<%==" then "__o << (" + terms.shift << ").to_s\n"
47
- else "__o << " + term.dump << "\n"
48
- end
49
- end
18
+ require_relative "herb/errors"
50
19
 
51
- def self.compile(context, parts)
52
- context.instance_eval(parts)
53
- end
20
+ require_relative "herb/cli"
21
+ require_relative "herb/project"
54
22
 
55
- HTML_ESCAPE = {
56
- "&" => "&amp;",
57
- ">" => "&gt;",
58
- "<" => "&lt;",
59
- '"' => "&#39;",
60
- "'" => "&#34;"
61
- }.freeze
23
+ require_relative "herb/version"
62
24
 
63
- UNSAFE = /[&"'><]/
64
-
65
- def self.h(str)
66
- str.gsub(UNSAFE, HTML_ESCAPE)
67
- end
68
-
69
- module Helpers
70
- def herb(file, params = {}, context = self)
71
- herb_cache[file] ||= Herb.parse(File.read(file), params.keys, context)
72
- herb_cache[file].call(params)
73
- end
25
+ begin
26
+ require_relative "herb/#{RUBY_VERSION.split(".")[...2].join(".")}/herb"
27
+ rescue LoadError
28
+ require_relative "herb/herb"
29
+ end
74
30
 
75
- def herb_cache
76
- Thread.current[:herb_cache] ||= {}
77
- end
78
- end
31
+ module Herb
79
32
  end