Skip to content

Commit 444969f

Browse files
authored
Simplify Lexer by removing the lexer state (#34)
1 parent 033e45e commit 444969f

File tree

12 files changed

+754
-554
lines changed

12 files changed

+754
-554
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ Metrics/ClassLength:
3939
Exclude:
4040
- ext/erbx/test/**/*_test.rb
4141

42+
Metrics/BlockLength:
43+
Exclude:
44+
- ext/erbx/test/**/*_test.rb
45+
4246
Layout/LineLength:
4347
Max: 120
4448
Exclude:

ext/erbx/test/extractor/extract_html_test.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ class ExtractHTMLTest < Minitest::Spec
3737
</ul>
3838
HTML
3939

40-
assert_equal " \n\n<ul>\n \n <li> </li>\n \n</ul>\n",
41-
actual
40+
assert_equal(
41+
" \n\n<ul>\n \n <li> </li>\n \n</ul>\n",
42+
actual
43+
)
4244
end
4345
end
4446
end

ext/erbx/test/extractor/extract_ruby_test.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ExtractRubyTest < Minitest::Spec
1313
test "basic loud" do
1414
ruby = ERBX.extract_ruby("<h1><%= RUBY_VERSION %></h1>")
1515

16-
assert_equal " = RUBY_VERSION ", ruby
16+
assert_equal " RUBY_VERSION ", ruby
1717
end
1818

1919
test "with newlines" do
@@ -26,7 +26,7 @@ class ExtractRubyTest < Minitest::Spec
2626
assert_equal " \n RUBY_VERSION \n \n", actual
2727
end
2828

29-
xtest "nested" do
29+
test "nested" do
3030
actual = ERBX.extract_ruby(<<~HTML)
3131
<% array = [1, 2, 3] %>
3232
@@ -37,15 +37,7 @@ class ExtractRubyTest < Minitest::Spec
3737
</ul>
3838
HTML
3939

40-
expected = ERBX.extract_ruby(<<-RUBY)
41-
array = [1, 2, 3]
42-
43-
44-
array.each do |item|
45-
= item
46-
end
47-
48-
RUBY
40+
expected = " array = [1, 2, 3] \n\n \n array.each do |item| \n item \n end \n \n"
4941

5042
assert_equal expected, actual
5143
end

ext/erbx/test/lexer/erb_test.rb

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module Lexer
66
class ERBTest < Minitest::Spec
7-
test "erb silent" do
7+
test "erb <% %>" do
88
result = ERBX.lex(%(<% 'hello world' %>))
99

1010
expected = %w[
@@ -15,9 +15,11 @@ class ERBTest < Minitest::Spec
1515
]
1616

1717
assert_equal expected, result.array.items.map(&:type)
18+
assert_equal "<%", result.array.items.find { |token| token.type == "TOKEN_ERB_START" }&.value
19+
assert_equal "%>", result.array.items.find { |token| token.type == "TOKEN_ERB_END" }&.value
1820
end
1921

20-
test "erb loud" do
22+
test "erb <%= %>" do
2123
result = ERBX.lex(%(<%= "hello world" %>))
2224

2325
expected = %w[
@@ -28,9 +30,11 @@ class ERBTest < Minitest::Spec
2830
]
2931

3032
assert_equal expected, result.array.items.map(&:type)
33+
assert_equal "<%=", result.array.items.find { |token| token.type == "TOKEN_ERB_START" }&.value
34+
assert_equal "%>", result.array.items.find { |token| token.type == "TOKEN_ERB_END" }&.value
3135
end
3236

33-
test "erb <%-" do
37+
test "erb <%- %>" do
3438
result = ERBX.lex(%(<%- "Test" %>))
3539

3640
expected = %w[
@@ -41,6 +45,8 @@ class ERBTest < Minitest::Spec
4145
]
4246

4347
assert_equal expected, result.array.items.map(&:type)
48+
assert_equal "<%-", result.array.items.find { |token| token.type == "TOKEN_ERB_START" }&.value
49+
assert_equal "%>", result.array.items.find { |token| token.type == "TOKEN_ERB_END" }&.value
4450
end
4551

4652
test "erb <%- -%>" do
@@ -54,6 +60,8 @@ class ERBTest < Minitest::Spec
5460
]
5561

5662
assert_equal expected, result.array.items.map(&:type)
63+
assert_equal "<%-", result.array.items.find { |token| token.type == "TOKEN_ERB_START" }&.value
64+
assert_equal "-%>", result.array.items.find { |token| token.type == "TOKEN_ERB_END" }&.value
5765
end
5866

5967
test "erb <%# %>" do
@@ -67,9 +75,11 @@ class ERBTest < Minitest::Spec
6775
]
6876

6977
assert_equal expected, result.array.items.map(&:type)
78+
assert_equal "<%#", result.array.items.find { |token| token.type == "TOKEN_ERB_START" }&.value
79+
assert_equal "%>", result.array.items.find { |token| token.type == "TOKEN_ERB_END" }&.value
7080
end
7181

72-
xtest "erb <%% %%>" do
82+
test "erb <%% %%>" do
7383
result = ERBX.lex(%(<%% "Test" %%>))
7484

7585
expected = %w[
@@ -80,102 +90,106 @@ class ERBTest < Minitest::Spec
8090
]
8191

8292
assert_equal expected, result.array.items.map(&:type)
93+
assert_equal "<%%", result.array.items.find { |token| token.type == "TOKEN_ERB_START" }&.value
94+
assert_equal "%%>", result.array.items.find { |token| token.type == "TOKEN_ERB_END" }&.value
8395
end
8496

85-
xtest "erb output inside HTML attribute value" do
97+
test "erb output inside HTML attribute value" do
8698
result = ERBX.lex(%(<article id="<%= dom_id(article) %>"></article>))
8799

88100
expected = %w[
89101
TOKEN_HTML_TAG_START
90-
TOKEN_HTML_TAG_NAME
102+
TOKEN_IDENTIFIER
91103
TOKEN_WHITESPACE
92-
TOKEN_HTML_ATTRIBUTE_NAME
93-
TOKEN_HTML_EQUALS
94-
TOKEN_HTML_QUOTE
104+
TOKEN_IDENTIFIER
105+
TOKEN_EQUALS
106+
TOKEN_QUOTE
95107
TOKEN_ERB_START
96108
TOKEN_ERB_CONTENT
97109
TOKEN_ERB_END
98-
TOKEN_HTML_QUOTE
110+
TOKEN_QUOTE
111+
TOKEN_HTML_TAG_END
112+
TOKEN_HTML_TAG_START_CLOSE
113+
TOKEN_IDENTIFIER
99114
TOKEN_HTML_TAG_END
100-
TOKEN_HTML_CLOSE_TAG_START
101-
TOKEN_HTML_TAG_NAME
102-
TOKEN_ERB_END
103115
TOKEN_EOF
104116
]
105117

106118
assert_equal expected, result.array.items.map(&:type)
107119
end
108120

109-
xtest "erb output inside HTML attribute value with value before" do
121+
test "erb output inside HTML attribute value with value before" do
110122
result = ERBX.lex(%(<div class="bg-black <%= "text-white" %>"></div>))
111123

112124
expected = %w[
113125
TOKEN_HTML_TAG_START
114-
TOKEN_HTML_TAG_NAME
126+
TOKEN_IDENTIFIER
127+
TOKEN_WHITESPACE
128+
TOKEN_IDENTIFIER
129+
TOKEN_EQUALS
130+
TOKEN_QUOTE
131+
TOKEN_IDENTIFIER
115132
TOKEN_WHITESPACE
116-
TOKEN_HTML_ATTRIBUTE_NAME
117-
TOKEN_HTML_EQUALS
118-
TOKEN_HTML_QUOTE
119-
TOKEN_HTML_ATTRIBUTE_VALUE
120133
TOKEN_ERB_START
121134
TOKEN_ERB_CONTENT
122135
TOKEN_ERB_END
123-
TOKEN_HTML_QUOTE
136+
TOKEN_QUOTE
137+
TOKEN_HTML_TAG_END
138+
TOKEN_HTML_TAG_START_CLOSE
139+
TOKEN_IDENTIFIER
124140
TOKEN_HTML_TAG_END
125-
TOKEN_HTML_CLOSE_TAG_START
126-
TOKEN_HTML_TAG_NAME
127-
TOKEN_ERB_END
128141
TOKEN_EOF
129142
]
130143

131144
assert_equal expected, result.array.items.map(&:type)
132145
end
133146

134-
xtest "erb output inside HTML attribute value with value before and after" do
147+
test "erb output inside HTML attribute value with value before and after" do
135148
result = ERBX.lex(%(<div class="bg-black <%= "text-white" %>"></div>))
136149

137150
expected = %w[
138151
TOKEN_HTML_TAG_START
139-
TOKEN_HTML_TAG_NAME
152+
TOKEN_IDENTIFIER
153+
TOKEN_WHITESPACE
154+
TOKEN_IDENTIFIER
155+
TOKEN_EQUALS
156+
TOKEN_QUOTE
157+
TOKEN_IDENTIFIER
140158
TOKEN_WHITESPACE
141-
TOKEN_HTML_ATTRIBUTE_NAME
142-
TOKEN_HTML_EQUALS
143-
TOKEN_HTML_QUOTE
144-
TOKEN_HTML_ATTRIBUTE_VALUE
145159
TOKEN_ERB_START
146160
TOKEN_ERB_CONTENT
147161
TOKEN_ERB_END
148-
TOKEN_HTML_ATTRIBUTE_VALUE
149-
TOKEN_HTML_QUOTE
162+
TOKEN_QUOTE
163+
TOKEN_HTML_TAG_END
164+
TOKEN_HTML_TAG_START_CLOSE
165+
TOKEN_IDENTIFIER
150166
TOKEN_HTML_TAG_END
151-
TOKEN_HTML_CLOSE_TAG_START
152-
TOKEN_HTML_TAG_NAME
153-
TOKEN_ERB_END
154167
TOKEN_EOF
155168
]
156169

157170
assert_equal expected, result.array.items.map(&:type)
158171
end
159172

160-
xtest "erb output inside HTML attribute value with value and after" do
173+
test "erb output inside HTML attribute value with value and after" do
161174
result = ERBX.lex(%(<div class="bg-black <%= "text-white" %>"></div>))
162175

163176
expected = %w[
164177
TOKEN_HTML_TAG_START
165-
TOKEN_HTML_TAG_NAME
178+
TOKEN_IDENTIFIER
179+
TOKEN_WHITESPACE
180+
TOKEN_IDENTIFIER
181+
TOKEN_EQUALS
182+
TOKEN_QUOTE
183+
TOKEN_IDENTIFIER
166184
TOKEN_WHITESPACE
167-
TOKEN_HTML_ATTRIBUTE_NAME
168-
TOKEN_HTML_EQUALS
169-
TOKEN_HTML_QUOTE
170185
TOKEN_ERB_START
171186
TOKEN_ERB_CONTENT
172187
TOKEN_ERB_END
173-
TOKEN_HTML_ATTRIBUTE_VALUE
174-
TOKEN_HTML_QUOTE
188+
TOKEN_QUOTE
189+
TOKEN_HTML_TAG_END
190+
TOKEN_HTML_TAG_START_CLOSE
191+
TOKEN_IDENTIFIER
175192
TOKEN_HTML_TAG_END
176-
TOKEN_HTML_CLOSE_TAG_START
177-
TOKEN_HTML_TAG_NAME
178-
TOKEN_ERB_END
179193
TOKEN_EOF
180194
]
181195

0 commit comments

Comments
 (0)