Add lexer user code expansions to access lexer match text positions
This commit is contained in:
1 parent
1729546d69
commit
ca9c23f96b
7 files changed
+221
-34
No files matched your search
@@ -910,6 +910,57 @@ EOF
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
it "allows lexer code blocks to access the matched token positions" do
|
||||
case language
|
||||
when "c", "cpp"
|
||||
write_grammar <<EOF
|
||||
<<
|
||||
#include <stdio.h>
|
||||
>>
|
||||
context_user_fields <<
|
||||
p_position_t last_start;
|
||||
p_position_t last_end;
|
||||
>>
|
||||
drop /\\s+/;
|
||||
token word /[a-z]+/ <<
|
||||
${context.last_start} = ${position};
|
||||
${context.last_end} = ${end_position};
|
||||
>>
|
||||
token stop /!/ <<
|
||||
$terminate(42);
|
||||
>>
|
||||
Start -> Words;
|
||||
Words -> ;
|
||||
Words -> word Words;
|
||||
Words -> stop Words;
|
||||
EOF
|
||||
when "d"
|
||||
write_grammar <<EOF
|
||||
context_user_fields <<
|
||||
p_position_t last_start;
|
||||
p_position_t last_end;
|
||||
>>
|
||||
drop /\\s+/;
|
||||
token word /[a-z]+/ <<
|
||||
${context.last_start} = ${position};
|
||||
${context.last_end} = ${end_position};
|
||||
>>
|
||||
token stop /!/ <<
|
||||
$terminate(42);
|
||||
>>
|
||||
Start -> Words;
|
||||
Words -> ;
|
||||
Words -> word Words;
|
||||
Words -> stop Words;
|
||||
EOF
|
||||
end
|
||||
run_propane(language: language)
|
||||
compile("spec/test_lexer_positions.#{language}", language: language)
|
||||
results = run_test(language: language)
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
it "allows the user to terminate the parser" do
|
||||
write_grammar <<EOF
|
||||
token a;
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
#include "testparser.h"
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char const * input = "abc\n defg hi\n!";
|
||||
p_context_t * context = p_context_new((uint8_t const *)input, strlen(input));
|
||||
p_token_info_t token_info;
|
||||
|
||||
/* First token "abc" on row 1, cols 1-3. */
|
||||
assert(p_lex(context, &token_info) == P_SUCCESS);
|
||||
assert(token_info.token == TOKEN_word);
|
||||
assert(context->last_start.row == 1u);
|
||||
assert(context->last_start.col == 1u);
|
||||
assert(context->last_end.row == 1u);
|
||||
assert(context->last_end.col == 3u);
|
||||
/* The lexer code block observed the same positions reported to the caller. */
|
||||
assert(context->last_start.row == token_info.position.row);
|
||||
assert(context->last_start.col == token_info.position.col);
|
||||
assert(context->last_end.row == token_info.end_position.row);
|
||||
assert(context->last_end.col == token_info.end_position.col);
|
||||
|
||||
/* Second token "defg" on row 2, cols 3-6. */
|
||||
assert(p_lex(context, &token_info) == P_SUCCESS);
|
||||
assert(token_info.token == TOKEN_word);
|
||||
assert(context->last_start.row == 2u);
|
||||
assert(context->last_start.col == 3u);
|
||||
assert(context->last_end.row == 2u);
|
||||
assert(context->last_end.col == 6u);
|
||||
|
||||
/* Third token "hi" on row 2, cols 8-9. */
|
||||
assert(p_lex(context, &token_info) == P_SUCCESS);
|
||||
assert(token_info.token == TOKEN_word);
|
||||
assert(context->last_start.row == 2u);
|
||||
assert(context->last_start.col == 8u);
|
||||
assert(context->last_end.row == 2u);
|
||||
assert(context->last_end.col == 9u);
|
||||
|
||||
/* The "!" stop token terminates the lexer. The context input text position
|
||||
* must not be updated when the lexer user code requests termination, so it
|
||||
* still points at the "!" token on row 3, col 1. */
|
||||
assert(p_lex(context, &token_info) == P_USER_TERMINATED);
|
||||
assert(p_user_terminate_code(context) == 42u);
|
||||
assert(context->text_position.row == 3u);
|
||||
assert(context->text_position.col == 1u);
|
||||
|
||||
p_context_delete(context);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import testparser;
|
||||
import std.stdio;
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
string input = "abc\n defg hi\n!";
|
||||
p_context_t * context = p_context_new(input);
|
||||
p_token_info_t token_info;
|
||||
|
||||
/* First token "abc" on row 1, cols 1-3. */
|
||||
assert(p_lex(context, &token_info) == P_SUCCESS);
|
||||
assert(token_info.token == TOKEN_word);
|
||||
assert(context.last_start == p_position_t(1, 1));
|
||||
assert(context.last_end == p_position_t(1, 3));
|
||||
/* The lexer code block observed the same positions reported to the caller. */
|
||||
assert(context.last_start == token_info.position);
|
||||
assert(context.last_end == token_info.end_position);
|
||||
|
||||
/* Second token "defg" on row 2, cols 3-6. */
|
||||
assert(p_lex(context, &token_info) == P_SUCCESS);
|
||||
assert(token_info.token == TOKEN_word);
|
||||
assert(context.last_start == p_position_t(2, 3));
|
||||
assert(context.last_end == p_position_t(2, 6));
|
||||
|
||||
/* Third token "hi" on row 2, cols 8-9. */
|
||||
assert(p_lex(context, &token_info) == P_SUCCESS);
|
||||
assert(token_info.token == TOKEN_word);
|
||||
assert(context.last_start == p_position_t(2, 8));
|
||||
assert(context.last_end == p_position_t(2, 9));
|
||||
|
||||
/* The "!" stop token terminates the lexer. The context input text position
|
||||
* must not be updated when the lexer user code requests termination, so it
|
||||
* still points at the "!" token on row 3, col 1. */
|
||||
assert(p_lex(context, &token_info) == P_USER_TERMINATED);
|
||||
assert(p_user_terminate_code(context) == 42u);
|
||||
assert(context.text_position == p_position_t(3, 1));
|
||||
}
|
||||
Reference in new issue
Block a user