X-Git-Url: https://code.kerkeslager.com/?p=ton;a=blobdiff_plain;f=don%2Fstring.py;fp=don%2Fstring.py;h=15ffc1027b3e744807fec97b82ac148eab857e92;hp=c7913872940c6ee9c57d6b3f27a8a46a9f5c5405;hb=d863068322ef1404cd5b95a8f21b3d2836f942bf;hpb=ce06e94ee5d876d364bad2e9cb19ccb5d9da8314 diff --git a/don/string.py b/don/string.py index c791387..15ffc10 100644 --- a/don/string.py +++ b/don/string.py @@ -58,5 +58,42 @@ def serialize(o): return _STRING_SERIALIZERS[o.tag](o.value) +def _make_constant_parser(constant, value): + def parser(s): + if s.startswith(constant): + result = _shared.ParseResult( + success = True, + value = value, + remaining = s[len(constant):], + ) + return result + + return _shared._FAILED_PARSE_RESULT + + return parser + +_PARSERS = [ + _make_constant_parser('null', None), + _make_constant_parser('true', True), + _make_constant_parser('false', False), +] + +def _object_parser(source): + for parser in _PARSERS: + result = parser(source) + + if result.success: + return result + + return _shared._FAILED_PARSE_RESULT + +def _parse(parser, source): + result = parser(source) + + if result.success and result.remaining.strip() == '': + return result.value + + raise Exception('Unparsed trailing characters: "{}"'.format(result.remaining)) + def deserialize(s): - pass + return _parse(_object_parser, s)