diff --git a/jsonjsc/parser.py b/jsonjsc/parser.py index dfa8397..95b3c98 100644 --- a/jsonjsc/parser.py +++ b/jsonjsc/parser.py @@ -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] = ' ' diff --git a/jsonjsc/tests/parser_tests.py b/jsonjsc/tests/parser_tests.py index 06ec773..3977172 100644 --- a/jsonjsc/tests/parser_tests.py +++ b/jsonjsc/tests/parser_tests.py @@ -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 }''' @@ -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" }''' @@ -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" @@ -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') @@ -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') @@ -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') diff --git a/setup.py b/setup.py index 5fddfc1..fdf5e4d 100644 --- a/setup.py +++ b/setup.py @@ -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",