Commit 17fabe5a authored by Robert Bradshaw's avatar Robert Bradshaw

Make raising a KeyError not the default for parsing names.

parent 82245e93
...@@ -532,26 +532,22 @@ def p_atom(s): ...@@ -532,26 +532,22 @@ def p_atom(s):
def p_name(s, name): def p_name(s, name):
pos = s.position() pos = s.position()
if not s.compile_time_expr: if not s.compile_time_expr and name in s.compile_time_env:
try: value = s.compile_time_env.lookup_here(name)
value = s.compile_time_env.lookup_here(name) rep = repr(value)
except KeyError: if isinstance(value, bool):
pass return ExprNodes.BoolNode(pos, value = value)
elif isinstance(value, int):
return ExprNodes.IntNode(pos, value = rep)
elif isinstance(value, long):
return ExprNodes.IntNode(pos, value = rep, longness = "L")
elif isinstance(value, float):
return ExprNodes.FloatNode(pos, value = rep)
elif isinstance(value, (str, unicode)):
return ExprNodes.StringNode(pos, value = value)
else: else:
rep = repr(value) error(pos, "Invalid type for compile-time constant: %s"
if isinstance(value, bool): % value.__class__.__name__)
return ExprNodes.BoolNode(pos, value = value)
elif isinstance(value, int):
return ExprNodes.IntNode(pos, value = rep)
elif isinstance(value, long):
return ExprNodes.IntNode(pos, value = rep, longness = "L")
elif isinstance(value, float):
return ExprNodes.FloatNode(pos, value = rep)
elif isinstance(value, (str, unicode)):
return ExprNodes.StringNode(pos, value = value)
else:
error(pos, "Invalid type for compile-time constant: %s"
% value.__class__.__name__)
return ExprNodes.NameNode(pos, name = name) return ExprNodes.NameNode(pos, name = name)
def p_cat_string_literal(s): def p_cat_string_literal(s):
......
from Cython.Plex.Scanners cimport Scanner from Cython.Plex.Scanners cimport Scanner
cdef class CompileTimeScope:
cdef public entries
cdef public outer
cdef class PyrexScanner(Scanner): cdef class PyrexScanner(Scanner):
cdef public context cdef public context
cdef public list included_files cdef public list included_files
......
...@@ -176,7 +176,7 @@ resword_dict = build_resword_dict() ...@@ -176,7 +176,7 @@ resword_dict = build_resword_dict()
#------------------------------------------------------------------ #------------------------------------------------------------------
class CompileTimeScope(object): class CompileTimeScope:
def __init__(self, outer = None): def __init__(self, outer = None):
self.entries = {} self.entries = {}
...@@ -187,6 +187,9 @@ class CompileTimeScope(object): ...@@ -187,6 +187,9 @@ class CompileTimeScope(object):
def lookup_here(self, name): def lookup_here(self, name):
return self.entries[name] return self.entries[name]
def __contains__(self, name):
return name in self.entries
def lookup(self, name): def lookup(self, name):
try: try:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment