summaryrefslogtreecommitdiffstats
path: root/Parser/parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'Parser/parser.c')
-rw-r--r--Parser/parser.c88
1 files changed, 31 insertions, 57 deletions
diff --git a/Parser/parser.c b/Parser/parser.c
index 227b918..b753a17 100644
--- a/Parser/parser.c
+++ b/Parser/parser.c
@@ -6,12 +6,12 @@
/* XXX To do: error recovery */
#include "Python.h"
+#include "pgenheaders.h"
#include "token.h"
#include "grammar.h"
#include "node.h"
#include "parser.h"
#include "errcode.h"
-#include "graminit.h"
#ifdef Py_DEBUG
@@ -35,9 +35,9 @@ s_reset(stack *s)
#define s_empty(s) ((s)->s_top == &(s)->s_base[MAXSTACK])
static int
-s_push(stack *s, const dfa *d, node *parent)
+s_push(register stack *s, dfa *d, node *parent)
{
- stackentry *top;
+ register stackentry *top;
if (s->s_top == s->s_base) {
fprintf(stderr, "s_push: parser stack overflow\n");
return E_NOMEM;
@@ -52,7 +52,7 @@ s_push(stack *s, const dfa *d, node *parent)
#ifdef Py_DEBUG
static void
-s_pop(stack *s)
+s_pop(register stack *s)
{
if (s_empty(s))
Py_FatalError("s_pop: parser stack underflow -- FATAL");
@@ -105,13 +105,11 @@ PyParser_Delete(parser_state *ps)
/* PARSER STACK OPERATIONS */
static int
-shift(stack *s, int type, char *str, int newstate, int lineno, int col_offset,
- int end_lineno, int end_col_offset)
+shift(register stack *s, int type, char *str, int newstate, int lineno, int col_offset)
{
int err;
assert(!s_empty(s));
- err = PyNode_AddChild(s->s_top->s_parent, type, str, lineno, col_offset,
- end_lineno, end_col_offset);
+ err = PyNode_AddChild(s->s_top->s_parent, type, str, lineno, col_offset);
if (err)
return err;
s->s_top->s_state = newstate;
@@ -119,15 +117,13 @@ shift(stack *s, int type, char *str, int newstate, int lineno, int col_offset,
}
static int
-push(stack *s, int type, const dfa *d, int newstate, int lineno, int col_offset,
- int end_lineno, int end_col_offset)
+push(register stack *s, int type, dfa *d, int newstate, int lineno, int col_offset)
{
int err;
- node *n;
+ register node *n;
n = s->s_top->s_parent;
assert(!s_empty(s));
- err = PyNode_AddChild(n, type, (char *)NULL, lineno, col_offset,
- end_lineno, end_col_offset);
+ err = PyNode_AddChild(n, type, (char *)NULL, lineno, col_offset);
if (err)
return err;
s->s_top->s_state = newstate;
@@ -138,38 +134,34 @@ push(stack *s, int type, const dfa *d, int newstate, int lineno, int col_offset,
/* PARSER PROPER */
static int
-classify(parser_state *ps, int type, const char *str)
+classify(parser_state *ps, int type, char *str)
{
grammar *g = ps->p_grammar;
- int n = g->g_ll.ll_nlabels;
+ register int n = g->g_ll.ll_nlabels;
if (type == NAME) {
- const label *l = g->g_ll.ll_label;
- int i;
+ register char *s = str;
+ register label *l = g->g_ll.ll_label;
+ register int i;
for (i = n; i > 0; i--, l++) {
if (l->lb_type != NAME || l->lb_str == NULL ||
- l->lb_str[0] != str[0] ||
- strcmp(l->lb_str, str) != 0)
+ l->lb_str[0] != s[0] ||
+ strcmp(l->lb_str, s) != 0)
continue;
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
-#if 0
- /* Leaving this in as an example */
- if (!(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) {
- if (str[0] == 'w' && strcmp(str, "with") == 0)
- break; /* not a keyword yet */
- else if (str[0] == 'a' && strcmp(str, "as") == 0)
- break; /* not a keyword yet */
+ if (ps->p_flags & CO_FUTURE_PRINT_FUNCTION &&
+ s[0] == 'p' && strcmp(s, "print") == 0) {
+ break; /* no longer a keyword */
}
#endif
-#endif
D(printf("It's a keyword\n"));
return n - i;
}
}
{
- const label *l = g->g_ll.ll_label;
- int i;
+ register label *l = g->g_ll.ll_label;
+ register int i;
for (i = n; i > 0; i--, l++) {
if (l->lb_type == type && l->lb_str == NULL) {
D(printf("It's a token we know\n"));
@@ -183,8 +175,6 @@ classify(parser_state *ps, int type, const char *str)
}
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
-#if 0
-/* Leaving this in as an example */
static void
future_hack(parser_state *ps)
{
@@ -224,16 +214,13 @@ future_hack(parser_state *ps)
}
}
}
-#endif
#endif /* future keyword */
int
-PyParser_AddToken(parser_state *ps, int type, char *str,
- int lineno, int col_offset,
- int end_lineno, int end_col_offset,
- int *expected_ret)
+PyParser_AddToken(register parser_state *ps, register int type, char *str,
+ int lineno, int col_offset, int *expected_ret)
{
- int ilabel;
+ register int ilabel;
int err;
D(printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str));
@@ -246,43 +233,34 @@ PyParser_AddToken(parser_state *ps, int type, char *str,
/* Loop until the token is shifted or an error occurred */
for (;;) {
/* Fetch the current dfa and state */
- const dfa *d = ps->p_stack.s_top->s_dfa;
- state *s = &d->d_state[ps->p_stack.s_top->s_state];
+ register dfa *d = ps->p_stack.s_top->s_dfa;
+ register state *s = &d->d_state[ps->p_stack.s_top->s_state];
D(printf(" DFA '%s', state %d:",
d->d_name, ps->p_stack.s_top->s_state));
/* Check accelerator */
if (s->s_lower <= ilabel && ilabel < s->s_upper) {
- int x = s->s_accel[ilabel - s->s_lower];
+ register int x = s->s_accel[ilabel - s->s_lower];
if (x != -1) {
if (x & (1<<7)) {
/* Push non-terminal */
int nt = (x >> 8) + NT_OFFSET;
int arrow = x & ((1<<7)-1);
- if (nt == func_body_suite && !(ps->p_flags & PyCF_TYPE_COMMENTS)) {
- /* When parsing type comments is not requested,
- we can provide better errors about bad indentation
- by using 'suite' for the body of a funcdef */
- D(printf(" [switch func_body_suite to suite]"));
- nt = suite;
- }
- const dfa *d1 = PyGrammar_FindDFA(
+ dfa *d1 = PyGrammar_FindDFA(
ps->p_grammar, nt);
if ((err = push(&ps->p_stack, nt, d1,
- arrow, lineno, col_offset,
- end_lineno, end_col_offset)) > 0) {
+ arrow, lineno, col_offset)) > 0) {
D(printf(" MemError: push\n"));
return err;
}
- D(printf(" Push '%s'\n", d1->d_name));
+ D(printf(" Push ...\n"));
continue;
}
/* Shift the token */
if ((err = shift(&ps->p_stack, type, str,
- x, lineno, col_offset,
- end_lineno, end_col_offset)) > 0) {
+ x, lineno, col_offset)) > 0) {
D(printf(" MemError: shift.\n"));
return err;
}
@@ -296,13 +274,11 @@ PyParser_AddToken(parser_state *ps, int type, char *str,
d->d_name,
ps->p_stack.s_top->s_state));
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
-#if 0
if (d->d_name[0] == 'i' &&
strcmp(d->d_name,
"import_stmt") == 0)
future_hack(ps);
#endif
-#endif
s_pop(&ps->p_stack);
if (s_empty(&ps->p_stack)) {
D(printf(" ACCEPT.\n"));
@@ -316,12 +292,10 @@ PyParser_AddToken(parser_state *ps, int type, char *str,
if (s->s_accept) {
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
-#if 0
if (d->d_name[0] == 'i' &&
strcmp(d->d_name, "import_stmt") == 0)
future_hack(ps);
#endif
-#endif
/* Pop this dfa and try again */
s_pop(&ps->p_stack);
D(printf(" Pop ...\n"));