rewriter: Use mparser to detect the end of some nodes

pull/5015/head
Daniel Mensinger 6 years ago committed by Jussi Pakkanen
parent 7199cd2095
commit 5a22bb7901
  1. 18
      mesonbuild/mparser.py
  2. 35
      mesonbuild/rewriter.py

@ -262,17 +262,21 @@ class BreakNode(ElementaryNode):
pass
class ArrayNode(BaseNode):
def __init__(self, args, lineno, colno):
def __init__(self, args, lineno, colno, end_lineno, end_colno):
self.subdir = args.subdir
self.lineno = lineno
self.colno = colno
self.end_lineno = end_lineno
self.end_colno = end_colno
self.args = args
class DictNode(BaseNode):
def __init__(self, args, lineno, colno):
def __init__(self, args, lineno, colno, end_lineno, end_colno):
self.subdir = args.subdir
self.lineno = lineno
self.colno = colno
self.end_lineno = end_lineno
self.end_colno = end_colno
self.args = args
class EmptyNode(BaseNode):
@ -349,10 +353,12 @@ class MethodNode(BaseNode):
self.args = args
class FunctionNode(BaseNode):
def __init__(self, subdir, lineno, colno, func_name, args):
def __init__(self, subdir, lineno, colno, end_lineno, end_colno, func_name, args):
self.subdir = subdir
self.lineno = lineno
self.colno = colno
self.end_lineno = end_lineno
self.end_colno = end_colno
self.func_name = func_name
assert(isinstance(func_name, str))
self.args = args
@ -620,7 +626,7 @@ class Parser:
if not isinstance(left, IdNode):
raise ParseException('Function call must be applied to plain id',
self.getline(), left.lineno, left.colno)
left = FunctionNode(left.subdir, left.lineno, left.colno, left.value, args)
left = FunctionNode(left.subdir, left.lineno, left.colno, self.current.lineno, self.current.colno, left.value, args)
go_again = True
while go_again:
go_again = False
@ -641,11 +647,11 @@ class Parser:
elif self.accept('lbracket'):
args = self.args()
self.block_expect('rbracket', block_start)
return ArrayNode(args, block_start.lineno, block_start.colno)
return ArrayNode(args, block_start.lineno, block_start.colno, self.current.lineno, self.current.colno)
elif self.accept('lcurl'):
key_values = self.key_values()
self.block_expect('rcurl', block_start)
return DictNode(key_values, block_start.lineno, block_start.colno)
return DictNode(key_values, block_start.lineno, block_start.colno, self.current.lineno, self.current.colno)
else:
return self.e9()

@ -157,7 +157,7 @@ class MTypeList(MTypeBase):
super().__init__(node)
def _new_node(self):
return mparser.ArrayNode(mparser.ArgumentNode(mparser.Token('', '', 0, 0, 0, None, '')), 0, 0)
return mparser.ArrayNode(mparser.ArgumentNode(mparser.Token('', '', 0, 0, 0, None, '')), 0, 0, 0, 0)
def _new_element_node(self, value):
# Overwrite in derived class
@ -644,16 +644,16 @@ class Rewriter:
# Build src list
src_arg_node = ArgumentNode(Token('string', cmd['subdir'], 0, 0, 0, None, ''))
src_arr_node = ArrayNode(src_arg_node, 0, 0)
src_arr_node = ArrayNode(src_arg_node, 0, 0, 0, 0)
src_far_node = ArgumentNode(Token('string', cmd['subdir'], 0, 0, 0, None, ''))
src_fun_node = FunctionNode(cmd['subdir'], 0, 0, 'files', src_far_node)
src_fun_node = FunctionNode(cmd['subdir'], 0, 0, 0, 0, 'files', src_far_node)
src_ass_node = AssignmentNode(cmd['subdir'], 0, 0, '{}_src'.format(cmd['target']), src_fun_node)
src_arg_node.arguments = [StringNode(Token('string', cmd['subdir'], 0, 0, 0, None, x)) for x in cmd['sources']]
src_far_node.arguments = [src_arr_node]
# Build target
tgt_arg_node = ArgumentNode(Token('string', cmd['subdir'], 0, 0, 0, None, ''))
tgt_fun_node = FunctionNode(cmd['subdir'], 0, 0, cmd['target_type'], tgt_arg_node)
tgt_fun_node = FunctionNode(cmd['subdir'], 0, 0, 0, 0, cmd['target_type'], tgt_arg_node)
tgt_ass_node = AssignmentNode(cmd['subdir'], 0, 0, '{}_tgt'.format(cmd['target']), tgt_fun_node)
tgt_arg_node.arguments = [
StringNode(Token('string', cmd['subdir'], 0, 0, 0, None, cmd['target'])),
@ -758,31 +758,8 @@ class Rewriter:
col = node.colno
start = offsets[line] + col
end = start
if isinstance(node, ArrayNode):
if raw[end] != '[':
mlog.warning('Internal error: expected "[" at {}:{} but got "{}"'.format(line, col, raw[end]))
return
counter = 1
while counter > 0:
end += 1
if raw[end] == '[':
counter += 1
elif raw[end] == ']':
counter -= 1
end += 1
elif isinstance(node, FunctionNode):
while raw[end] != '(':
end += 1
end += 1
counter = 1
while counter > 0:
end += 1
if raw[end] == '(':
counter += 1
elif raw[end] == ')':
counter -= 1
end += 1
if isinstance(node, (ArrayNode, FunctionNode)):
end = offsets[node.end_lineno - 1] + node.end_colno
# Only removal is supported for assignments
elif isinstance(node, AssignmentNode) and i['action'] == 'rm':

Loading…
Cancel
Save