def t_STRING_LITERAL(t): last_quote_group = 'end_dquote' if t.value[0] == '"' else 'end_quote' if t.lexer.lexmatch.group(last_quote_group) != t.value[0]: return RulesToken(t, unterminated=True) try: return RulesToken(t, value=decode_baon_string_literal(t.value)) except Exception as e: return RulesToken(t, error=e)
def test_decode_baon_string_literal(self): for input, output in ( ('"double quotes"', 'double quotes'), ("'single quotes'", 'single quotes'), ('"dodgy"quote"', 'dodgy"quote'), ('"escaped""double quote"', 'escaped"double quote'), ("'escaped'single quote'", "escaped'single quote"), ('"raw\n\tchars\u1234"', "raw\n\tchars\u1234"), ('"backslash\\does\\not\\escape\\"', "backslash\\does\\not\\escape\\"), ('"no unicode\\u1234escape"', 'no unicode\\u1234escape'), ): with self.subTest(input=input): self.assertEqual(decode_baon_string_literal(input), output) with self.assertRaises(StringLiteralNotQuotedProperlyError): decode_baon_string_literal('unquoted') with self.assertRaises(StringLiteralNotQuotedProperlyError): decode_baon_string_literal('"unterminated') with self.assertRaises(StringLiteralNotQuotedProperlyError): decode_baon_string_literal('"mismatch\'')