def _parse_string(source: Source, string_regex=BASIC_STRING, separator='"') -> str: source.expect(separator) res = [] while True: if not source.consume_regex(string_regex): raise DoesNotMatch('Invalid string starting from {text}' .format(text=source._text[:100])) res.append(source.last_consumed) # start of some escape character if not source.consume('\\'): break # do nothing if new line chars encountered # corresponds to \ if source.consume_regex(NEWLINE_ESCAPE_CHARS_REGEX): pass # read unicode characters elif source.consume_regex(SHORT_UNICODE_REGEX) or source.consume_regex(LONG_UNICODE_REGEX): res.append(chr(int(source.last_consumed, 16))) else: # fail if no escape character follows source.expect_regex(ESCAPE_CHARS_REGEX) res.append(ESCAPES_MAPPING[source.last_consumed]) source.expect(separator) return ''.join(res)
def _parse_table_name(source: Source) -> Tuple[str, ...]: source.consume_regex(WHITESPACE_REGEX) keys = [parse_keyword(source)] source.consume_regex(WHITESPACE_REGEX) while source.consume('.'): source.consume_regex(WHITESPACE_REGEX) key = parse_keyword(source) keys.append(key) source.consume_regex(WHITESPACE_REGEX) return tuple(keys)
def _parse_inline_table(source: Source) -> InlineTable: table = InlineTable() source.expect('{') if not source.consume('}'): source.consume_regex(WHITESPACE_REGEX) kv_entry = parse_kv_entry(source) if source.last_consumed == '}': raise InvalidTomlError('Cannot have nested inline tables.') table[kv_entry.key] = kv_entry.val while source.consume(','): source.consume_regex(WHITESPACE_REGEX) kv_entry = parse_kv_entry(source) table[kv_entry.key] = kv_entry.val source.consume_regex(WHITESPACE_REGEX) source.expect('}') return table