port not_strings to python, add trailing space

This commit is contained in:
ochafik 2024-06-11 02:54:07 +01:00
parent 6743438607
commit 8b47473df3
3 changed files with 51 additions and 7 deletions

View file

@ -214,7 +214,7 @@ std::string not_strings(const std::vector<std::string> & strings) {
};
visit(trie);
out << " ) char* [\"]";
out << " ) char* [\"] space";
return out.str();
}

View file

@ -71,6 +71,47 @@ NON_LITERAL_SET = set('|.()[]{}*+?')
ESCAPED_IN_REGEXPS_BUT_NOT_IN_LITERALS = set('[]()|{}*+?')
def not_strings(strings):
class TrieNode:
def __init__(self):
self.children = {}
self.is_end_of_string = False
def insert(self, string):
node = self
for c in string:
node = node.children.setdefault(c, TrieNode())
node.is_end_of_string = True
trie = TrieNode()
for s in strings:
trie.insert(s)
out = ['["] ( ']
def visit(node):
rejects = []
first = True
for c, child in node.children.items():
rejects.append(c)
if child.is_end_of_string:
continue
if first:
first = False
else:
out.append(' | ')
out.append(f'[{c}] (')
visit(child)
out.append(')')
if node.children:
if not first:
out.append(' | ')
out.append(f'[^"{"".join(rejects)}]')
visit(trie)
out.append(' ) char* ["] space')
return ''.join(out)
class SchemaConverter:
def __init__(self, *, prop_order, allow_fetch, dotall, raw_pattern):
self._prop_order = prop_order
@ -471,9 +512,12 @@ class SchemaConverter:
if additional_properties == True or isinstance(additional_properties, dict):
sub_name = f'{name}{"-" if name else ""}additional'
value_rule = self.visit({} if additional_properties == True else additional_properties, f'{sub_name}-value')
key_rule = self._add_primitive('string', PRIMITIVE_RULES['string']) if not sorted_props \
else self._add_rule(f'{sub_name}-k', not_strings(sorted_props))
prop_kv_rule_names["*"] = self._add_rule(
f'{sub_name}-kv',
self._add_primitive('string', PRIMITIVE_RULES['string']) + f' ":" space {value_rule}'
f'{key_rule} ":" space {value_rule}'
)
optional_props.append("*")

View file

@ -634,7 +634,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
})""",
R"""(
a-kv ::= "\"a\"" space ":" space number
additional-k ::= ["] ( [^"a] ) char* ["]
additional-k ::= ["] ( [^"a] ) char* ["] space
additional-kv ::= additional-k ":" space string
char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
decimal-part ::= [0-9]{1,16}
@ -659,7 +659,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
R"""(
a-kv ::= "\"a\"" space ":" space number
a-rest ::= ( "," space additional-kv )*
additional-k ::= ["] ( [^"a] ) char* ["]
additional-k ::= ["] ( [^"a] ) char* ["] space
additional-kv ::= additional-k ":" space number
decimal-part ::= [0-9]{1,16}
integral-part ::= [0] | [1-9] [0-9]{0,15}
@ -682,7 +682,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
"additionalProperties": {"type": "number"}
})""",
R"""(
additional-k ::= ["] ( [a] ([l] ([s] ([^"o]) | [^"s]) | [n] ([^"d]) | [^"ln]) | [^"a] ) char* ["]
additional-k ::= ["] ( [a] ([l] ([s] ([^"o]) | [^"s]) | [n] ([^"d]) | [^"ln]) | [^"a] ) char* ["] space
additional-kv ::= additional-k ":" space number
also-kv ::= "\"also\"" space ":" space number
also-rest ::= ( "," space additional-kv )*
@ -748,7 +748,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
alternative-1 ::= bar
array ::= "[" space ( value ("," space value)* )? "]" space
bar ::= "{" space (bar-b-kv bar-b-rest | bar-additional-kv ( "," space bar-additional-kv )* )? "}" space
bar-additional-k ::= ["] ( [^"b] ) char* ["]
bar-additional-k ::= ["] ( [^"b] ) char* ["] space
bar-additional-kv ::= bar-additional-k ":" space bar-additional-value
bar-additional-value ::= object
bar-b-kv ::= "\"b\"" space ":" space number
@ -759,7 +759,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
foo ::= "{" space (foo-a-kv foo-a-rest | foo-additional-kv ( "," space foo-additional-kv )* )? "}" space
foo-a-kv ::= "\"a\"" space ":" space number
foo-a-rest ::= ( "," space foo-additional-kv )*
foo-additional-k ::= ["] ( [^"a] ) char* ["]
foo-additional-k ::= ["] ( [^"a] ) char* ["] space
foo-additional-kv ::= foo-additional-k ":" space foo-additional-value
foo-additional-value ::= object
integral-part ::= [0] | [1-9] [0-9]{0,15}