Add $token() user code block expansion
This commit is contained in:
parent
43fb74fe4b
commit
66d654b6b9
@ -184,7 +184,7 @@ class <%= @classname %>
|
||||
<% @grammar.patterns.each do |pattern| %>
|
||||
<% if pattern.code_id %>
|
||||
case <%= pattern.code_id %>u: {
|
||||
<%= pattern.code %>
|
||||
<%= expand_code(pattern.code) %>
|
||||
} break;
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
@ -142,6 +142,19 @@ class Propane
|
||||
end
|
||||
end
|
||||
|
||||
# Expand expansions in user code block.
|
||||
#
|
||||
# @param code [String]
|
||||
# User code block.
|
||||
#
|
||||
# @return [String]
|
||||
# Expanded user code block.
|
||||
def expand_code(code)
|
||||
code.gsub(/\$token\(([$\w]+)\)/) do |match|
|
||||
"TOKEN_#{Token.code_name($1)}"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -169,4 +169,27 @@ EOF
|
||||
"pass2",
|
||||
])
|
||||
end
|
||||
|
||||
it "supports returning tokens from pattern code blocks" do
|
||||
write_grammar <<EOF
|
||||
token abc;
|
||||
/def/ <<
|
||||
writeln("def!");
|
||||
>>
|
||||
/ghi/ <<
|
||||
writeln("ghi!");
|
||||
return $token(abc);
|
||||
>>
|
||||
Start -> abc;
|
||||
EOF
|
||||
build_parser
|
||||
compile("spec/test_return_token_from_pattern.d")
|
||||
results = run
|
||||
expect(results.status).to eq 0
|
||||
verify_lines(results.stdout, [
|
||||
"def!",
|
||||
"ghi!",
|
||||
"def!",
|
||||
])
|
||||
end
|
||||
end
|
||||
|
14
spec/test_return_token_from_pattern.d
Normal file
14
spec/test_return_token_from_pattern.d
Normal file
@ -0,0 +1,14 @@
|
||||
import testparser;
|
||||
import std.stdio;
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
string input = "defghidef";
|
||||
auto parser = new Testparser.Parser(cast(const(ubyte) *)input.ptr, input.length);
|
||||
assert(parser.parse() == true);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user