Repository: sdispater/tomlkit (python)

Issue:
Apostrophe (single-quote) in key string causes problems
This code sequence demonstrates the problem:
```
import tomlkit

dat = {"BJ's Restaurant": {'account':'dining'}}

toml = tomlkit.dumps(dat)
print(toml)

dat2 = tomlkit.loads(toml)
```
This gives me this output (Python 3.7.6):
```
["BJ's Restaurant"]
account = "dining"

Traceback (most recent call last):
  File "j:\tmp.py", line 17, in <module>
    dat2 = tomlkit.loads(toml)
  File "c:\ProgramData\Miniconda3\envs\hledger\lib\site-packages\tomlkit\api.py", line 34, in loads
    return parse(string)
  File "c:\ProgramData\Miniconda3\envs\hledger\lib\site-packages\tomlkit\api.py", line 51, in parse
    return Parser(string).parse()
  File "c:\ProgramData\Miniconda3\envs\hledger\lib\site-packages\tomlkit\parser.py", line 153, in parse
    key, value = self._parse_table()
  File "c:\ProgramData\Miniconda3\envs\hledger\lib\site-packages\tomlkit\parser.py", line 927, in _parse_table
    name_parts = tuple(self._split_table_name(name))
  File "c:\ProgramData\Miniconda3\envs\hledger\lib\site-packages\tomlkit\parser.py", line 227, in _split_table_name
    raise self.parse_error()
tomlkit.exceptions.ParseError: TOML parse error at line 1 col 18
```
The TOML generated by dumps(dat) shows in the first two lines, and seems correct. However loads() blows up as shown, calling out column 18 which would be the end of the key string. 
For the TOML source below, loads() calls out column 0, which is possibly a different bug:
```
[xyz]
[xyz."BJ's Restaurant"]
account = "dining"
---------------
  File "c:\ProgramData\Miniconda3\envs\hledger\lib\site-packages\tomlkit\parser.py", line 227, in _split_table_name
    raise self.parse_error()
tomlkit.exceptions.ParseError: TOML parse error at line 2 col 0
```

I have experimented with work-arounds such as enclosing the string in triple-quotes or escaping the apostrophe with a backslash. So far, I haven't found anything reliable.

Pull request:
Fix parse errors when single quotes are present in a table name
Fixes #70

Patch:
diff --git a/tomlkit/parser.py b/tomlkit/parser.py
index ecb2a75..0ff26f0 100644
--- a/tomlkit/parser.py
+++ b/tomlkit/parser.py
@@ -219,7 +219,12 @@ class Parser:
                 continue
             elif c in {"'", '"'}:
                 if in_name:
-                    if t == KeyType.Literal and c == '"':
+                    if (
+                        t == KeyType.Literal
+                        and c == '"'
+                        or t == KeyType.Basic
+                        and c == "'"
+                    ):
                         current += c
                         continue
 


Test patch:
diff --git a/tests/examples/table_names_with_string_delimiters.toml b/tests/examples/table_names_with_string_delimiters.toml
new file mode 100644
index 0000000..8efd3ef
--- /dev/null
+++ b/tests/examples/table_names_with_string_delimiters.toml
@@ -0,0 +1,5 @@
+['Special "table"']
+foo = "bar"
+
+["BJ's Restaurant"]
+account = "dining"
diff --git a/tests/test_api.py b/tests/test_api.py
index 86b589b..a19bdda 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -53,6 +53,7 @@ def json_serial(obj):
         "newline_in_strings",
         "preserve_quotes_in_string",
         "string_slash_whitespace_newline",
+        "table_names_with_string_delimiters",
     ],
 )
 def test_parse_can_parse_valid_toml_files(example, example_name):