Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions 12 Lib/test/test_peg_generator/test_c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,15 @@ def test_soft_keywords_parse(self) -> None:
self.check_input_strings_for_grammar(valid_cases, invalid_cases)
"""
self.run_test(grammar_source, test_source)

def test_soft_keywords_lookahead(self) -> None:
grammar_source = """
start: &"if" "if" expr '+' expr NEWLINE
expr: NAME
"""
test_source = """
valid_cases = ["if if + if"]
invalid_cases = ["if if"]
self.check_input_strings_for_grammar(valid_cases, invalid_cases)
"""
self.run_test(grammar_source, test_source)
9 changes: 9 additions & 0 deletions 9 Parser/pegen/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,15 @@ _PyPegen_lookahead_with_name(int positive, expr_ty (func)(Parser *), Parser *p)
return (res != NULL) == positive;
}

int
_PyPegen_lookahead_with_string(int positive, expr_ty (func)(Parser *, const char*), Parser *p, const char* arg)
{
int mark = p->mark;
void *res = func(p, arg);
p->mark = mark;
return (res != NULL) == positive;
}

int
_PyPegen_lookahead_with_int(int positive, Token *(func)(Parser *, int), Parser *p, int arg)
{
Expand Down
1 change: 1 addition & 0 deletions 1 Parser/pegen/pegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ int _PyPegen_is_memoized(Parser *p, int type, void *pres);

int _PyPegen_lookahead_with_name(int, expr_ty (func)(Parser *), Parser *);
int _PyPegen_lookahead_with_int(int, Token *(func)(Parser *, int), Parser *, int);
int _PyPegen_lookahead_with_string(int , expr_ty (func)(Parser *, const char*), Parser *, const char*);
int _PyPegen_lookahead(int, void *(func)(Parser *), Parser *);

Token *_PyPegen_expect_token(Parser *p, int type);
Expand Down
11 changes: 9 additions & 2 deletions 11 Tools/peg_generator/pegen/c_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class NodeTypes(Enum):
STRING_TOKEN = 2
GENERIC_TOKEN = 3
KEYWORD = 4
CUT_OPERATOR = 5
SOFT_KEYWORD = 5
CUT_OPERATOR = 6


BASE_NODETYPES = {
Expand Down Expand Up @@ -123,7 +124,7 @@ def soft_keyword_helper(self, value: str) -> FunctionCall:
function="_PyPegen_expect_soft_keyword",
arguments=["p", value],
return_type="expr_ty",
nodetype=NodeTypes.NAME_TOKEN,
nodetype=NodeTypes.SOFT_KEYWORD,
comment=f"soft_keyword='{value}'",
)

Expand Down Expand Up @@ -217,6 +218,12 @@ def lookahead_call_helper(self, node: Lookahead, positive: int) -> FunctionCall:
arguments=[positive, call.function, *call.arguments],
return_type="int",
)
elif call.nodetype == NodeTypes.SOFT_KEYWORD:
return FunctionCall(
function=f"_PyPegen_lookahead_with_string",
arguments=[positive, call.function, *call.arguments],
return_type="int",
)
elif call.nodetype in {NodeTypes.GENERIC_TOKEN, NodeTypes.KEYWORD}:
return FunctionCall(
function=f"_PyPegen_lookahead_with_int",
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.