Provide matched text to lexer user code block
This commit is contained in:
parent
623c644e74
commit
ca8a360c0e
@ -186,11 +186,12 @@ class <%= @classname %>
|
|||||||
* Execute user code associated with a lexer pattern.
|
* Execute user code associated with a lexer pattern.
|
||||||
*
|
*
|
||||||
* @param code_id The ID of the user code block to execute.
|
* @param code_id The ID of the user code block to execute.
|
||||||
|
* @param match Matched text for this pattern.
|
||||||
*
|
*
|
||||||
* @return Token ID to accept, or _TOKEN_COUNT if the user code does
|
* @return Token ID to accept, or _TOKEN_COUNT if the user code does
|
||||||
* not explicitly return a token.
|
* not explicitly return a token.
|
||||||
*/
|
*/
|
||||||
private uint user_code(uint code_id)
|
private uint user_code(uint code_id, string match)
|
||||||
{
|
{
|
||||||
switch (code_id)
|
switch (code_id)
|
||||||
{
|
{
|
||||||
@ -268,7 +269,7 @@ class <%= @classname %>
|
|||||||
uint token_to_accept = longest_match_info.token;
|
uint token_to_accept = longest_match_info.token;
|
||||||
if (longest_match_info.code_id != 0xFFFF_FFFFu)
|
if (longest_match_info.code_id != 0xFFFF_FFFFu)
|
||||||
{
|
{
|
||||||
uint user_code_token = user_code(longest_match_info.code_id);
|
uint user_code_token = user_code(longest_match_info.code_id, m_input[m_input_position..(m_input_position + longest_match_info.length)]);
|
||||||
/* A return of _TOKEN_COUNT from user_code() means
|
/* A return of _TOKEN_COUNT from user_code() means
|
||||||
* that the user code did not explicitly return a
|
* that the user code did not explicitly return a
|
||||||
* token. So only override the token to return if the
|
* token. So only override the token to return if the
|
||||||
|
@ -300,4 +300,21 @@ EOF
|
|||||||
expect(results.status).to_not eq 0
|
expect(results.status).to_not eq 0
|
||||||
expect(results.stderr).to match %r{reduce/reduce conflict.*\(E\).*\(F\)}
|
expect(results.stderr).to match %r{reduce/reduce conflict.*\(E\).*\(F\)}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "provides matched text to user code blocks" do
|
||||||
|
write_grammar <<EOF
|
||||||
|
token id /[a-zA-Z_][a-zA-Z0-9_]*/ <<
|
||||||
|
writeln("Matched token is ", match);
|
||||||
|
>>
|
||||||
|
Start -> id;
|
||||||
|
EOF
|
||||||
|
build_parser
|
||||||
|
compile("spec/test_lexer_match_text.d")
|
||||||
|
results = run
|
||||||
|
expect(results.status).to eq 0
|
||||||
|
verify_lines(results.stdout, [
|
||||||
|
"Matched token is identifier_123",
|
||||||
|
"pass1",
|
||||||
|
])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
15
spec/test_lexer_match_text.d
Normal file
15
spec/test_lexer_match_text.d
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import testparser;
|
||||||
|
import std.stdio;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
string input = `identifier_123`;
|
||||||
|
auto parser = new Testparser.Parser(input);
|
||||||
|
assert(parser.parse() == true);
|
||||||
|
writeln("pass1");
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user