Skip to content

Commit 8def02a

Browse files
committedJun 20, 2024
Allow escaped strings in json_schema.py
1 parent 371313e commit 8def02a

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed
 

Diff for: ‎outlines/fsm/json_schema.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@
1010
from referencing._core import Resolver
1111
from referencing.jsonschema import DRAFT202012
1212

13-
STRING_INNER = r'([^"\\\x00-\x1f\x7f-\x9f]|\\\\)'
14-
STRING = f'"{STRING_INNER}*"'
1513
INTEGER = r"(-)?(0|[1-9][0-9]*)"
1614
NUMBER = rf"({INTEGER})(\.[0-9]+)?([eE][+-][0-9]+)?"
1715
BOOLEAN = r"(true|false)"
1816
NULL = r"null"
1917
WHITESPACE = r"[ ]?"
2018

19+
# Disallow control sequences, e.g. "\n"
20+
_ANY_CHAR_NO_CTRL_SEQ = r'[^"\\\x00-\x1F\x7F-\x9F]'
21+
# Allow valid escapes, e.g. "\\n"
22+
# Disallow invalid escapes, e.g. "\escape"
23+
_VALID_ESCAPES = r'\\["\\/bfnrt]|\\u[0-9a-fA-F]{4}'
24+
STRING_INNER = rf"({_ANY_CHAR_NO_CTRL_SEQ}|{_VALID_ESCAPES})"
25+
STRING = f'"{STRING_INNER}*"'
26+
2127
type_to_regex = {
2228
"string": STRING,
2329
"integer": INTEGER,

Diff for: ‎tests/fsm/test_json_schema.py

+4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ def test_match_number(pattern, does_match):
124124
('"quoted_string"', True),
125125
(r'"escape_\character"', False),
126126
(r'"double_\\escape"', True),
127+
('"\n"', False),
128+
('"\\n"', True),
129+
(r'"unescaped " quote"', False),
130+
(r'"escaped \" quote"', True),
127131
],
128132
),
129133
# String with maximum length

0 commit comments

Comments
 (0)