Visitar URL original
bpo-40222: Mark exception table function in the dis module as private by pablogsal · Pull Request #95960 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
6a99107
Parsing VM initial structure
gvanrossum May 26, 2020
8ffc315
Hook things up so we can test it
gvanrossum May 27, 2020
a9ba946
Add debugging printf()s in anger; fix bugs
gvanrossum May 27, 2020
8635a4c
Implement actions
gvanrossum May 27, 2020
1a34aeb
Support optional tokens
gvanrossum May 27, 2020
b4a2292
Support optional rules
gvanrossum May 27, 2020
b1f2a03
Add vmreadme.md; OP_SUCCESS has an argument
gvanrossum May 28, 2020
50aa868
Do optional items differently (with a postfix op)
gvanrossum May 28, 2020
320266b
Compute start/end line/col numbers; add some ideas to vmreadme.md
gvanrossum May 28, 2020
5bf3f9c
Tighten the code; add some speculation to vmreadme
gvanrossum May 28, 2020
816db75
Add OP_NOOP; add enums for rules & actions
gvanrossum May 28, 2020
d2a980f
Implement loops
gvanrossum May 29, 2020
2befb12
Add a few more rules to the grammar
gvanrossum May 29, 2020
175a127
Drop debug printf()s, more flexibility in parse_string()
gvanrossum May 29, 2020
f3f1665
Add memoization, some debug niceties
gvanrossum May 29, 2020
bda3517
Inline helper functions
gvanrossum May 29, 2020
881d756
Explain OP_OPTIONAL better
gvanrossum May 29, 2020
94cfb95
Skeleton of code generator
pablogsal May 29, 2020
4ba6c61
Simplify structure of OP_SUCCESS
gvanrossum May 29, 2020
db628e1
Move opcodes around
gvanrossum May 29, 2020
7798629
Add a 'grammar' for operations
gvanrossum May 29, 2020
412c741
Move generated part of vm.h into vmparse.h
gvanrossum May 29, 2020
020fbd1
Merge branch 'pegenvm_generator' into pegenvm
lysnikolaou May 29, 2020
8c7cffc
Merge branch 'master' into pegenvm
lysnikolaou May 29, 2020
ef1fabd
Clean skeleton of vm_generator
pablogsal May 30, 2020
b497b23
Merge branch 'pegenvm' of github.com:we-like-parsers/cpython into peg…
lysnikolaou May 30, 2020
8eab7e0
Better formatting of generated file; remove unneeded indentation
lysnikolaou May 30, 2020
4a5f823
Add OP_LOOP_COLLECT_NONEMPTY -- used for a+
gvanrossum May 30, 2020
3cee73c
Expand description of root rules
gvanrossum May 30, 2020
9b96df0
Initial support for repeat_0
pablogsal May 30, 2020
2f44ee9
Fix name rules for repeat0 nodes
pablogsal May 30, 2020
6dc7092
Eliminate OP_LOOP_START
gvanrossum May 30, 2020
9e7e12e
Do fewer reallocs (at the cost of an extra int per frame)
gvanrossum May 30, 2020
52f5a75
Speculate how to implement a.b+
gvanrossum May 30, 2020
3b93237
Make memo rule types distinct from token types
gvanrossum May 30, 2020
33522ae
Fix small issues in vmreadme.pm
gvanrossum May 30, 2020
cbd45a5
Add generation of root rules (very coarssely)
gvanrossum May 31, 2020
1a6531d
Add enum for rule types (R_)
gvanrossum May 31, 2020
0226dd5
Generate actions (primitively)
gvanrossum May 31, 2020
c64ff2f
Implement code generation for keywords
lysnikolaou May 31, 2020
e2c4a36
Refactor add_opcode to optionally accept a second argument
lysnikolaou May 31, 2020
21d8b83
Translate item names in actions; use the generated vmparse.h!
gvanrossum May 31, 2020
6fe4f0e
Fix mypy (in vm_generator)
gvanrossum May 31, 2020
6205002
Avoid name conflict for 'f'
gvanrossum May 31, 2020
f72c7b6
Generate code for repeat1 loops
gvanrossum Jun 1, 2020
fc3d4c4
Implement delimited loops (b.a+)
gvanrossum Jun 1, 2020
6c50468
Generate code for delimited loop
gvanrossum Jun 1, 2020
a10babb
Implement soft keywords (hand-written and code generation) (#129)
lysnikolaou Jun 1, 2020
b13169b
Update generated vmparse.h
gvanrossum Jun 1, 2020
c2a7cf6
Fix code generation for if_stmt
gvanrossum Jun 1, 2020
a862a69
Implement lookahead ops
gvanrossum Jun 1, 2020
ab863df
Generate code for lookaheads (only one token supported!)
gvanrossum Jun 1, 2020
1be4b62
Implement left-recursion (with hand-coded vmparse.h)
gvanrossum Jun 2, 2020
b53c2a4
Code generation for left-recursive rules
gvanrossum Jun 2, 2020
7a59aa0
Allow specifying different grammars
gvanrossum Jun 2, 2020
cac4149
Generate code for 'cut'
gvanrossum Jun 2, 2020
180dfff
Support groups and optional in code generator
gvanrossum Jun 2, 2020
e01e643
There's no need to special-case -> in actions
gvanrossum Jun 2, 2020
13c3bbb
Treat TYPE_COMMENT as a token (since it is)
gvanrossum Jun 3, 2020
65304e6
Generate code for Grammar/parser.gram
gvanrossum Jun 3, 2020
10f7be1
Group every opcode with its argument (#131)
pablogsal Jun 3, 2020
a9a4115
Add vm target to pegen script to generate the vm parser (#130)
lysnikolaou Jun 3, 2020
5bb2f57
Selective memoization
gvanrossum Jun 3, 2020
ba8783b
Don't call is_memoized in OP_RETURN_LEFT_REC
gvanrossum Jun 3, 2020
bccd5c8
Different way of doing left-recursion
gvanrossum Jun 5, 2020
2a138cc
Merge remote-tracking branch 'upstream/master' into pegenvm
gvanrossum Aug 16, 2020
2394b96
Remove leftover conflict markers
gvanrossum Aug 16, 2020
be34499
Fix deps for vm.o
gvanrossum Aug 16, 2020
b9394e4
Fix includes for vm.c
gvanrossum Aug 16, 2020
b64113b
Regenerated vmparse.h
gvanrossum Aug 16, 2020
9afa67e
Merge remote-tracking branch 'pegen/pegenvm'
pablogsal Aug 1, 2022
a65604e
bpo-40222: Mark exception table function in the dis module as private
pablogsal Aug 13, 2022
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
Prev Previous commit
Next Next commit
Implement loops
  • Loading branch information
gvanrossum committed May 29, 2020
commit d2a980fa338a011d285a74698570f7f3b24ebc2a
58 changes: 52 additions & 6 deletions Parser/pegen/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ push_frame(Stack *stack, Rule *rule)
assert(stack->top < 100);
Frame *f = &stack->frames[stack->top++];
f->rule = rule;
f->mark = stack->p->mark;
f->ialt = 0;
f->iop = 0;
f->cut = 0;
f->mark = stack->p->mark;
f->ncollected = 0;
f->collection = NULL;
f->ival = 0;
return f;
}
Expand All @@ -26,11 +28,28 @@ static Frame *
pop_frame(Stack *stack)
{
assert(stack->top > 1);
Frame *f = &stack->frames[--stack->top - 1];
Frame *f = &stack->frames[--stack->top]; // Frame being popped
if (f->collection) {
PyMem_Free(f->collection);
}
f = &stack->frames[stack->top - 1]; // New top of stack
printf(" pop %s\n", f->rule->name);
return f;
}

static asdl_seq *
make_asdl_seq(Parser *p, void *collection[], int ncollected)
{
asdl_seq *seq = _Py_asdl_seq_new(ncollected, p->arena);
if (!seq) {
return NULL;
}
for (int i = 0; i < ncollected; i++) {
asdl_seq_SET(seq, i, collection[i]);
}
return seq;
}

static void *
run_vm(Parser *p, Rule rules[], int start)
{
Expand All @@ -43,8 +62,8 @@ run_vm(Parser *p, Rule rules[], int start)
if (p->mark == p->fill)
_PyPegen_fill_token(p);
for (int i = 0; i < stack.top; i++) printf(" ");
printf("Rule: %s; ialt=%d; iop=%d; op=%s; p^='%s'\n",
f->rule->name, f->ialt, f->iop, opcode_names[f->rule->opcodes[f->iop]],
printf("Rule: %s; ialt=%d; iop=%d; op=%s; mark=%d; token=%d; p^='%s'\n",
f->rule->name, f->ialt, f->iop, opcode_names[f->rule->opcodes[f->iop]], p->mark, p->tokens[p->mark]->type,
p->fill > p-> mark ? PyBytes_AsString(p->tokens[p->mark]->bytes) : "<UNSEEN>");
switch (f->rule->opcodes[f->iop++]) {
case OP_NOOP:
Expand All @@ -60,6 +79,33 @@ run_vm(Parser *p, Rule rules[], int start)
goto top;
case OP_OPTIONAL:
goto top;
case OP_LOOP_START:
f->ncollected = 0;
f->collection = PyMem_Malloc(0);
if (!f->collection) {
return PyErr_NoMemory();
}
goto top;
case OP_LOOP_ITERATE:
f->mark = p->mark;
assert(f->ival == 1);
v = f->vals[0];
assert(v);
f->collection = PyMem_Realloc(f->collection, (f->ncollected + 1) * sizeof(void *));
if (!f->collection) {
return NULL;
}
f->collection[f->ncollected++] = v;
f->iop = f->rule->alts[f->ialt] + 1; // Skip OP_LOOP_START operation
f->ival = 0;
goto top;
case OP_LOOP_COLLECT:
v = make_asdl_seq(p, f->collection, f->ncollected);
f = pop_frame(&stack);
if (!v) {
return PyErr_NoMemory();
}
break;
case OP_TOKEN:
oparg = f->rule->opcodes[f->iop++];
v = _PyPegen_expect_token(p, oparg);
Expand All @@ -76,10 +122,10 @@ run_vm(Parser *p, Rule rules[], int start)
case OP_SUCCESS:
oparg = f->rule->opcodes[f->iop++];
v = call_action(p, f, oparg);
if (v) {
if (v || PyErr_Occurred()) {
return v;
}
// fallthrough
return RAISE_SYNTAX_ERROR("Final action failed");
case OP_FAILURE:
return RAISE_SYNTAX_ERROR("A syntax error");
default:
Expand Down
44 changes: 33 additions & 11 deletions Parser/pegen/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ typedef enum _opcodes {
OP_CUT,
OP_OPTIONAL,
OP_LOOP_START,
OP_LOOP_COLLECT,
OP_LOOP_ITERATE,
OP_FAILURE,
// The rest have an argument
OP_TOKEN,
OP_RULE,
OP_RETURN,
OP_LOOP_CONTINUE,
OP_LOOP_COLLECT,
OP_SUCCESS,
} Opcode;

Expand All @@ -24,13 +24,13 @@ char *opcode_names[] = {
"OP_CUT",
"OP_OPTIONAL",
"OP_LOOP_START",
"OP_LOOP_COLLECT",
"OP_LOOP_ITERATE",
"OP_FAILURE",
// The rest have an argument
"OP_TOKEN",
"OP_RULE",
"OP_RETURN",
"OP_LOOP_CONTINUE",
"OP_LOOP_COLLECT",
"OP_SUCCESS",
};

Expand All @@ -42,10 +42,12 @@ typedef struct _rule {

typedef struct _frame {
Rule *rule;
int mark;
int ialt;
int iop;
int cut;
int mark;
int ncollected;
void **collection;
int ival;
void *vals[10];
} Frame;
Expand All @@ -58,9 +60,9 @@ typedef struct _stack {

/* Toy grammar

NOTE: [expr] is optional to test OP_RULE_OPTIONAL.
NOTE: [expr] is right-recursive.

start: stmt ENDMARKER
start: stmt* ENDMARKER
stmt: expr NEWLINE
expr: term '+' expr | term
term: NAME | NUMBER
Expand All @@ -73,6 +75,7 @@ static KeywordToken *reserved_keywords[] = {

enum {
R_START,
R_STMTS,
R_STMT,
R_EXPR,
R_TERM,
Expand All @@ -90,19 +93,32 @@ enum {
static Rule all_rules[] = {

{"start",
{0, 4, -1},
{0, 6, -1},
{
OP_RULE, R_STMT,
OP_RULE, R_STMTS,
OP_TOKEN, ENDMARKER,
OP_SUCCESS, A_START_0,

OP_FAILURE,
},
},

{"stmt*",
{0, 4, -1},
{
OP_LOOP_START,
OP_RULE, R_STMT,
OP_LOOP_ITERATE,

OP_LOOP_COLLECT,
},
},

{"stmt",
{0, -1},
{
OP_RULE, R_EXPR,
OP_TOKEN, NEWLINE,
OP_RETURN, A_STMT_0,
},
},
Expand Down Expand Up @@ -139,15 +155,21 @@ call_action(Parser *p, Frame *f, int iaction)
Token *t = p->tokens[f->mark];
int _start_lineno = t->lineno;
int _start_col_offset = t->col_offset;
t = p->tokens[p->mark - 1];
if (p->mark > 0) {
t = p->tokens[p->mark - 1];
}
else {
// Debug anomaly rather than crashing
printf("p->mark=%d, p->fill=%d, f->mark=%d\n", p->mark, p->fill, f->mark);
}
int _end_lineno = t->end_lineno;
int _end_col_offset = t->end_col_offset;

switch (iaction) {
case A_START_0:
return _PyPegen_make_module(p, f->vals[0]);
case A_STMT_0:
return _PyPegen_singleton_seq(p, _Py_Expr(f->vals[0], EXTRA));
return _Py_Expr(f->vals[0], EXTRA);
case A_EXPR_0:
if (f->vals[2])
return _Py_BinOp(f->vals[0], Add, f->vals[2], EXTRA);
Expand Down
Loading