Skip to content

Commit

Permalink
Hot fix for character index wrapping bug on single line block comment…
Browse files Browse the repository at this point in the history
…s with no leading characters.
  • Loading branch information
NouberNou committed Jul 9, 2018
1 parent e66132a commit d4cbbe6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
8 changes: 4 additions & 4 deletions jsonjsc/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ def parse(s):
if not in_multi_line:
if not in_string and c == '"':
in_string = True
elif in_string and c == '"' and line[ci-1] != '\\':
elif in_string and c == '"' and ci > 0 and line[ci-1] != '\\':
in_string = False
if not in_string and c == '/' and line[ci-1] == '/':
if not in_string and c == '/' and ci > 0 and line[ci-1] == '/':
line = line[:ci-1]
break
if not in_string and not in_multi_line and c == '*' and line[ci-1] == '/':
if not in_string and not in_multi_line and c == '*' and ci > 0 and line[ci-1] == '/':
line[ci] = ' '
line[ci-1] = ' '
in_multi_line = True
elif in_multi_line and c == '*' and line[ci+1] == '/':
line[ci] = ' '
line[ci + 1] = ' '
line[ci+1] = ' '
in_multi_line = False
elif in_multi_line:
line[ci] = ' '
Expand Down
49 changes: 49 additions & 0 deletions jsonjsc/tests/parser_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
// This is a comment on it's own line
}'''

TEST_COMMENT_ON_OWN_LINE_NO_TAB = r'''{
// This is a comment on it's own line
}'''

TEST_SINGLE_COMMENT_ON_LINE_WITH_JSON = r'''{
"test": 123 // this is a comment after some JSON
}'''
Expand All @@ -23,6 +27,10 @@
/* This is a block comment */
}'''

TEST_BLOCK_COMMENT_SINGLE_LINE_ALONE_NO_TAB = r'''{
/* This is a block comment */
}'''

TEST_BLOCK_COMMENT_SINGLE_LINE_BEFORE = r'''{
/* This is a block comment */"test": "message"
}'''
Expand All @@ -42,6 +50,20 @@
"test": "message"
}'''

TEST_BLOCK_COMMENT_MULTIPLE_LINES_NO_TAB = r'''{
/*
This is a block comment
*/
"test": "message"
}'''

TEST_BLOCK_COMMENT_MULTIPLE_LINES_NO_TAB_FANCY = r'''{
/*
* This is a block comment
*/
"test": "message"
}'''

TEST_COMMENTED_OUT_BLOCK_COMMENT = r'''{
///*
"test": "message"
Expand Down Expand Up @@ -77,6 +99,11 @@ def test_comment_on_own_line(self):
result = result.split('\n')
self.assertEqual(result[1], r' ')

def test_comment_on_own_line_no_tab(self):
result = parse(TEST_COMMENT_ON_OWN_LINE_NO_TAB)
result = result.split('\n')
self.assertEqual(result[1], r'')

def test_single_comment_on_line_with_json(self):
result = parse(TEST_SINGLE_COMMENT_ON_LINE_WITH_JSON)
result = result.split('\n')
Expand All @@ -92,6 +119,12 @@ def test_block_comment_single_line_alone(self):
result = result.split('\n')
self.assertEqual(result[1], r' ')

def test_block_comment_single_line_alone_no_tab(self):
result = parse(TEST_BLOCK_COMMENT_SINGLE_LINE_ALONE_NO_TAB)
result = result.split('\n')
print(result)
self.assertEqual(result[1], r' ')

def test_block_comment_single_line_before(self):
result = parse(TEST_BLOCK_COMMENT_SINGLE_LINE_BEFORE)
result = result.split('\n')
Expand All @@ -115,6 +148,22 @@ def test_block_comment_multiple_lines(self):
self.assertEqual(result[3], r' ')
self.assertEqual(result[4], r' "test": "message"')

def test_block_comment_multiple_lines_no_tab(self):
result = parse(TEST_BLOCK_COMMENT_MULTIPLE_LINES_NO_TAB)
result = result.split('\n')
self.assertEqual(result[1], r' ')
self.assertEqual(result[2], r'')
self.assertEqual(result[3], r' ')
self.assertEqual(result[4], r' "test": "message"')

def test_block_comment_multiple_lines_no_tab_fancy(self):
result = parse(TEST_BLOCK_COMMENT_MULTIPLE_LINES_NO_TAB_FANCY)
result = result.split('\n')
self.assertEqual(result[1], r' ')
self.assertEqual(result[2], r'')
self.assertEqual(result[3], r' ')
self.assertEqual(result[4], r' "test": "message"')

def test_commented_out_block_comment(self):
result = parse(TEST_COMMENTED_OUT_BLOCK_COMMENT)
result = result.split('\n')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="jsonjsc",
version="1.1.0",
version="1.1.1",
author="C. Foster",
author_email="korewananda@gmail.com",
description="A package to parse out C/JS style block and single line comments from JSON files",
Expand Down

0 comments on commit d4cbbe6

Please sign in to comment.