Skip to content

Commit

Permalink
Fix parsing for definitions containing separators inside strings, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hansec committed Dec 6, 2017
1 parent 99ce53e commit 76101f9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
11 changes: 10 additions & 1 deletion fortls/parse_fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
WORD_REGEX = re.compile(r'[a-z][a-z0-9_]*', re.I)
SUB_PAREN_MATCH = re.compile(r'\([a-z0-9_, ]*\)', re.I)
KIND_SPEC_MATCH = re.compile(r'\([a-z0-9_, =*]*\)', re.I)
SQ_STRING_REGEX = re.compile(r'\'[^\']*\'', re.I)
DQ_STRING_REGEX = re.compile(r'\"[^\"]*\"', re.I)
#
FIXED_COMMENT_LINE_MATCH = re.compile(r'(!|c|d|\*)')
FIXED_CONT_REGEX = re.compile(r'( [\S])')
Expand All @@ -58,11 +60,18 @@ def detect_fixed_format(file_lines):
return True


def strip_strings(in_str):
out_str = SQ_STRING_REGEX.sub('', in_str)
out_str = DQ_STRING_REGEX.sub('', out_str)
return out_str


def separate_def_list(test_str):
stripped_str = strip_strings(test_str)
paren_count = 0
def_list = []
curr_str = ''
for char in test_str:
for char in stripped_str:
if char == '(':
paren_count += 1
elif char == ')':
Expand Down
26 changes: 13 additions & 13 deletions test/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,27 @@ def check_return(result_arrary, checks):
file_path = os.path.join(test_dir, "test_prog.f08")
string += write_rpc_request(2, "textDocument/completion", {
"textDocument": {"uri": file_path},
"position": {"line": 11, "character": 6}
"position": {"line": 12, "character": 6}
})
string += write_rpc_request(3, "textDocument/completion", {
"textDocument": {"uri": file_path},
"position": {"line": 12, "character": 6}
"position": {"line": 13, "character": 6}
})
string += write_rpc_request(4, "textDocument/completion", {
"textDocument": {"uri": file_path},
"position": {"line": 17, "character": 7}
"position": {"line": 18, "character": 7}
})
string += write_rpc_request(5, "textDocument/completion", {
"textDocument": {"uri": file_path},
"position": {"line": 18, "character": 20}
"position": {"line": 19, "character": 20}
})
string += write_rpc_request(6, "textDocument/completion", {
"textDocument": {"uri": file_path},
"position": {"line": 18, "character": 42}
"position": {"line": 19, "character": 42}
})
string += write_rpc_request(7, "textDocument/completion", {
"textDocument": {"uri": file_path},
"position": {"line": 20, "character": 26}
"position": {"line": 21, "character": 26}
})
errcode, results = run_request(string)
#
Expand All @@ -193,34 +193,34 @@ def check_return(result_arrary, checks):
file_path = os.path.join(test_dir, "test_prog.f08")
string += write_rpc_request(2, "textDocument/definition", {
"textDocument": {"uri": file_path},
"position": {"line": 11, "character": 6}
"position": {"line": 12, "character": 6}
})
string += write_rpc_request(3, "textDocument/definition", {
"textDocument": {"uri": file_path},
"position": {"line": 12, "character": 6}
"position": {"line": 13, "character": 6}
})
string += write_rpc_request(4, "textDocument/definition", {
"textDocument": {"uri": file_path},
"position": {"line": 17, "character": 7}
"position": {"line": 18, "character": 7}
})
string += write_rpc_request(5, "textDocument/definition", {
"textDocument": {"uri": file_path},
"position": {"line": 18, "character": 20}
"position": {"line": 19, "character": 20}
})
string += write_rpc_request(6, "textDocument/definition", {
"textDocument": {"uri": file_path},
"position": {"line": 18, "character": 42}
"position": {"line": 19, "character": 42}
})
string += write_rpc_request(7, "textDocument/definition", {
"textDocument": {"uri": file_path},
"position": {"line": 20, "character": 26}
"position": {"line": 21, "character": 26}
})
errcode, results = run_request(string)
#
assert errcode == 0
check_return(results[1], [0, 0, "file://" + os.path.join(test_dir, "subdir", "test_fixed.f")])
check_return(results[2], [20, 20, "file://" + os.path.join(test_dir, "subdir", "test_fixed.f")])
check_return(results[3], [9, 9, "file://" + os.path.join(test_dir, "test_prog.f08")])
check_return(results[3], [10, 10, "file://" + os.path.join(test_dir, "test_prog.f08")])
check_return(results[4], [19, 19, "file://" + os.path.join(test_dir, "subdir", "test_free.f90")])
check_return(results[5], [13, 13, "file://" + os.path.join(test_dir, "subdir", "test_free.f90")])
check_return(results[6], [5, 5, "file://" + os.path.join(test_dir, "subdir", "test_free.f90")])
1 change: 1 addition & 0 deletions test/test_source/test_prog.f08
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PROGRAM test_program
USE test_free, ONLY: vector, scaled_vector, module_variable
IMPLICIT NONE
!
CHARACTER(LEN=*) :: test_str1 = "i2.2,':',i2.2", test_str2 = 'i2.2,":",i2.2'
INTEGER(4) :: n
REAL(8) :: x,y
COMPLEX(8) :: xc,yc
Expand Down

0 comments on commit 76101f9

Please sign in to comment.