removed Parser._start_pos as well

This commit is contained in:
Dave Halter
2014-02-25 01:20:55 +01:00
parent add8259d7e
commit a7a8a73a2c
+28 -29
View File
@@ -41,14 +41,15 @@ class Parser(object):
tokenizer=None, top_module=None, offset=0): tokenizer=None, top_module=None, offset=0):
self.no_docstr = no_docstr self.no_docstr = no_docstr
self._start_pos = 1 + offset, 0
# initialize global Scope
self.module = pr.SubModule(module_path, self._start_pos, top_module)
self._scope = self.module
tokenizer = tokenizer or tokenize.source_tokens(source) tokenizer = tokenizer or tokenize.source_tokens(source)
self._gen = PushBackTokenizer(tokenizer) self._gen = PushBackTokenizer(tokenizer)
start_pos = 1 + offset, 0
# initialize global Scope
self.module = pr.SubModule(module_path, start_pos, top_module)
self._scope = self.module
self._top_module = top_module or self.module self._top_module = top_module or self.module
try: try:
self._parse() self._parse()
except (common.MultiLevelStopIteration, StopIteration): except (common.MultiLevelStopIteration, StopIteration):
@@ -69,7 +70,6 @@ class Parser(object):
# because of `self.module.used_names`. # because of `self.module.used_names`.
d.parent = self.module d.parent = self.module
self._start_pos = self.module.start_pos
self.module.end_pos = self._gen.current.end self.module.end_pos = self._gen.current.end
if self._gen.current.type in (tokenize.NEWLINE,): if self._gen.current.type in (tokenize.NEWLINE,):
# This case is only relevant with the FastTokenizer, because # This case is only relevant with the FastTokenizer, because
@@ -114,8 +114,8 @@ class Parser(object):
# token maybe a name or star # token maybe a name or star
return None, tok return None, tok
append((tok.string, self._start_pos)) first_pos = tok.start
first_pos = self._start_pos append((tok.string, first_pos))
while True: while True:
end_pos = tok.end end_pos = tok.end
tok = self.next() tok = self.next()
@@ -124,7 +124,7 @@ class Parser(object):
tok = self.next() tok = self.next()
if tok.type != tokenize.NAME: if tok.type != tokenize.NAME:
break break
append((tok.string, self._start_pos)) append((tok.string, tok.start))
n = pr.Name(self.module, names, first_pos, end_pos) if names else None n = pr.Name(self.module, names, first_pos, end_pos) if names else None
return n, tok return n, tok
@@ -207,12 +207,12 @@ class Parser(object):
:return: Return a Scope representation of the tokens. :return: Return a Scope representation of the tokens.
:rtype: Function :rtype: Function
""" """
first_pos = self._start_pos first_pos = self._gen.current.start
tok = self.next() tok = self.next()
if tok.type != tokenize.NAME: if tok.type != tokenize.NAME:
return None return None
fname = pr.Name(self.module, [(tok.string, self._start_pos)], self._start_pos, fname = pr.Name(self.module, [(tok.string, tok.start)], tok.start,
tok.end) tok.end)
tok = self.next() tok = self.next()
@@ -245,15 +245,15 @@ class Parser(object):
:return: Return a Scope representation of the tokens. :return: Return a Scope representation of the tokens.
:rtype: Class :rtype: Class
""" """
first_pos = self._start_pos first_pos = self._gen.current.start
cname = self.next() cname = self.next()
if cname.type != tokenize.NAME: if cname.type != tokenize.NAME:
debug.warning("class: syntax err, token is not a name@%s (%s: %s)", debug.warning("class: syntax err, token is not a name@%s (%s: %s)",
self._start_pos[0], tokenize.tok_name[cname.type], cname.string) cname.start[0], tokenize.tok_name[cname.type], cname.string)
return None return None
cname = pr.Name(self.module, [(cname.string, self._start_pos)], cname = pr.Name(self.module, [(cname.string, cname.start)],
self._start_pos, cname.end) cname.start, cname.end)
super = [] super = []
_next = self.next() _next = self.next()
@@ -262,7 +262,7 @@ class Parser(object):
_next = self.next() _next = self.next()
if _next.string != ':': if _next.string != ':':
debug.warning("class syntax: %s@%s", cname, self._start_pos[0]) debug.warning("class syntax: %s@%s", cname, _next.start[0])
return None return None
return pr.Class(self.module, cname, super, first_pos) return pr.Class(self.module, cname, super, first_pos)
@@ -295,7 +295,7 @@ class Parser(object):
self.next() self.next()
tok = self.next() tok = self.next()
first_pos = self._start_pos first_pos = tok.start
opening_brackets = ['{', '(', '['] opening_brackets = ['{', '(', '[']
closing_brackets = ['}', ')', ']'] closing_brackets = ['}', ')', ']']
@@ -396,7 +396,6 @@ class Parser(object):
#typ, tok, start_pos, end_pos = next(self._gen) #typ, tok, start_pos, end_pos = next(self._gen)
_current = next(self._gen) _current = next(self._gen)
# dedents shouldn't change positions # dedents shouldn't change positions
self._start_pos = _current.start
#self._current = typ, tok #self._current = typ, tok
return _current return _current
@@ -422,6 +421,7 @@ class Parser(object):
for tok in self.iterator: for tok in self.iterator:
token_type = tok.type token_type = tok.type
tok_str = tok.string tok_str = tok.string
first_pos = tok.start
self.module.temp_used_names = [] self.module.temp_used_names = []
# debug.dbg('main: tok=[%s] type=[%s] indent=[%s]', \ # debug.dbg('main: tok=[%s] type=[%s] indent=[%s]', \
# tok, tokenize.tok_name[token_type], start_position[0]) # tok, tokenize.tok_name[token_type], start_position[0])
@@ -429,10 +429,10 @@ class Parser(object):
# check again for unindented stuff. this is true for syntax # check again for unindented stuff. this is true for syntax
# errors. only check for names, because thats relevant here. If # errors. only check for names, because thats relevant here. If
# some docstrings are not indented, I don't care. # some docstrings are not indented, I don't care.
while self._start_pos[1] <= self._scope.start_pos[1] \ while first_pos[1] <= self._scope.start_pos[1] \
and (token_type == tokenize.NAME or tok_str in ['(', '['])\ and (token_type == tokenize.NAME or tok_str in ['(', '['])\
and self._scope != self.module: and self._scope != self.module:
self._scope.end_pos = self._start_pos self._scope.end_pos = first_pos
self._scope = self._scope.parent self._scope = self._scope.parent
if isinstance(self._scope, pr.Module) \ if isinstance(self._scope, pr.Module) \
and not isinstance(self._scope, pr.SubModule): and not isinstance(self._scope, pr.SubModule):
@@ -442,11 +442,10 @@ class Parser(object):
use_as_parent_scope = self._top_module use_as_parent_scope = self._top_module
else: else:
use_as_parent_scope = self._scope use_as_parent_scope = self._scope
first_pos = self._start_pos
if tok_str == 'def': if tok_str == 'def':
func = self._parse_function() func = self._parse_function()
if func is None: if func is None:
debug.warning("function: syntax error@%s", self._start_pos[0]) debug.warning("function: syntax error@%s", first_pos[0])
continue continue
self.freshscope = True self.freshscope = True
self._scope = self._scope.add_scope(func, self._decorators) self._scope = self._scope.add_scope(func, self._decorators)
@@ -454,7 +453,7 @@ class Parser(object):
elif tok_str == 'class': elif tok_str == 'class':
cls = self._parse_class() cls = self._parse_class()
if cls is None: if cls is None:
debug.warning("class: syntax error@%s" % self._start_pos[0]) debug.warning("class: syntax error@%s" % first_pos[0])
continue continue
self.freshscope = True self.freshscope = True
self._scope = self._scope.add_scope(cls, self._decorators) self._scope = self._scope.add_scope(cls, self._decorators)
@@ -496,7 +495,7 @@ class Parser(object):
tok_str = 'import' tok_str = 'import'
mod = None mod = None
if not mod and not relative_count or tok_str != "import": if not mod and not relative_count or tok_str != "import":
debug.warning("from: syntax error@%s", self._start_pos[0]) debug.warning("from: syntax error@%s", tok.start[0])
defunct = True defunct = True
if tok_str != 'import': if tok_str != 'import':
self._gen.push_last_back() self._gen.push_last_back()
@@ -522,7 +521,7 @@ class Parser(object):
set_stmt, tok = self._parse_statement(added_breaks=['in'], set_stmt, tok = self._parse_statement(added_breaks=['in'],
names_are_set_vars=True) names_are_set_vars=True)
if tok.string != 'in': if tok.string != 'in':
debug.warning('syntax err, for flow incomplete @%s', self._start_pos[0]) debug.warning('syntax err, for flow incomplete @%s', tok.start[0])
try: try:
statement, tok = self._parse_statement() statement, tok = self._parse_statement()
@@ -532,7 +531,7 @@ class Parser(object):
f = pr.ForFlow(self.module, s, first_pos, set_stmt) f = pr.ForFlow(self.module, s, first_pos, set_stmt)
self._scope = self._scope.add_statement(f) self._scope = self._scope.add_statement(f)
if tok is None or tok.string != ':': if tok is None or tok.string != ':':
debug.warning('syntax err, for flow started @%s', self._start_pos[0]) debug.warning('syntax err, for flow started @%s', first_pos[0])
elif tok_str in ['if', 'while', 'try', 'with'] + extended_flow: elif tok_str in ['if', 'while', 'try', 'with'] + extended_flow:
added_breaks = [] added_breaks = []
command = tok_str command = tok_str
@@ -569,10 +568,10 @@ class Parser(object):
s = self._scope.add_statement(f) s = self._scope.add_statement(f)
self._scope = s self._scope = s
if tok.string != ':': if tok.string != ':':
debug.warning('syntax err, flow started @%s', self._start_pos[0]) debug.warning('syntax err, flow started @%s', tok.start[0])
# returns # returns
elif tok_str in ['return', 'yield']: elif tok_str in ['return', 'yield']:
s = self._start_pos s = tok.start
self.freshscope = False self.freshscope = False
# add returns to the scope # add returns to the scope
func = self._scope.get_parent_until(pr.Function) func = self._scope.get_parent_until(pr.Function)
@@ -624,7 +623,7 @@ class Parser(object):
else: else:
if token_type not in [tokenize.COMMENT, tokenize.NEWLINE]: if token_type not in [tokenize.COMMENT, tokenize.NEWLINE]:
debug.warning('Token not used: %s %s %s', tok_str, debug.warning('Token not used: %s %s %s', tok_str,
tokenize.tok_name[token_type], self._start_pos) tokenize.tok_name[token_type], first_pos)
continue continue
self.no_docstr = False self.no_docstr = False