def test_from_enum_transform(self): class Pet(enum.Enum): CAT = "cat" DOG = "dog" pet = from_enum(Pet, transform=lambda s: s.lower()) self.assertEqual(pet.parse("cat"), Pet.CAT) self.assertEqual(pet.parse("CAT"), Pet.CAT)
def test_from_enum_int(self): class Position(enum.Enum): FIRST = 1 SECOND = 2 position = from_enum(Position) self.assertEqual(position.parse("1"), Position.FIRST) self.assertEqual(position.parse("2"), Position.SECOND) self.assertRaises(ParseError, position.parse, "foo")
def test_from_enum_string(self): class Pet(enum.Enum): CAT = "cat" DOG = "dog" pet = from_enum(Pet) self.assertEqual(pet.parse("cat"), Pet.CAT) self.assertEqual(pet.parse("dog"), Pet.DOG) self.assertRaises(ParseError, pet.parse, "foo")
# Signed numbers: # (Extra compared to spec, to cope with need to produce signed numeric values) signedNumberChange = lambda s, num: (-1) if s == "-" else (+1) sign = regex("[-+]?") signedIntLit = seq(sign, intLit).combine(signedNumberChange) signedFloatLit = seq(sign, floatLit).combine(signedNumberChange) # Constant # put fullIdent at end to disabmiguate from boolLit constant = signedIntLit | signedFloatLit | strLit | boolLit | fullIdent # Syntax syntax = lexeme("syntax") >> EQ >> quote >> string("proto3") << quote + SEMI # Import Statement import_option = from_enum(ImportOption) import_ = seq( lexeme("import") >> import_option.optional().tag('option'), lexeme(strLit).tag('identifier') << SEMI).combine_dict(Import) # Package package = seq(lexeme("package") >> fullIdent << SEMI).map(Package) # Option optionName = (ident | (LPAREN >> fullIdent << RPAREN)) + (string(".") + ident).many().concat() option = seq( lexeme("option") >> optionName.tag('name'), EQ >> constant.tag('value') << SEMI,
from mcmd.script.model.types_ import InputType from mcmd.script.parser.errors import ScriptSyntaxError # subparsers _double_quoted_string = regex(r'"[^"\\]*(?:\\[\S\s][^"\\]*)*"').map(lambda s: s[1:-1].replace(r'\"', '"')) _single_quoted_string = regex(r"'[^'\\]*(?:\\[\S\s][^'\\]*)*'").map(lambda s: s[1:-1].replace(r"\'", "'")) _text = _single_quoted_string.map(Template) | _double_quoted_string.map(Template) _boolean = string('true').result(True) | string('false').result(False) _padding = whitespace.optional() _equals = _padding >> string('=') >> _padding _bool_assignment = _equals >> _boolean.desc('boolean') _text_assignment = _equals >> _text.desc('quoted text') _assignment = _bool_assignment | _text_assignment _message = _padding >> string(':') >> _padding >> _text _value_name = regex(r'[a-zA-Z0-9_]+').desc('value name consisting of letters, numbers and/or underscores') _input_type = from_enum(InputType) _wait_message = _message | regex('.*').map(lambda s: s.strip()).map(Template) # $value name [= "text"] @generate def _value_declaration(): yield string('value') yield whitespace name = yield _value_name value = yield _assignment.optional() return Value.from_untyped_value(name=name, value=value) # $input type name [: "message"] @generate
ABCCat = typing.Union[ str, ABCCatFunctor, ABCCatBot, ] """ The type representing categories in the ABC Treebank. """ ABCCat_ABCCatPlus = ABCCatPlus[ABCCat] KeyakiTree = core.TypedTree[KeyakiCat, str] KeyakiTree_ABCCatPlus = core.TypedTree[KeyakiCat_ABCCatPlus, str] ABCTree_ABCCatPlus = core.TypedTree[ABCCat_ABCCatPlus, str] parser_ABCCatBot: parsy.Parser = parsy.from_enum(ABCCatBot) parser_ABCCatBase: parsy.Parser = parsy.regex(r"[^/\\<>#\s]+") @parsy.generate def parser_ABCCatFunctor_Left() -> parsy.Parser: que = yield ( parser_ABCCatBot | parser_ABCCatBase | parser_ABCCat_group ).sep_by( parsy.string("\\"), min = 2, ) return functools.reduce( lambda c, a: ABCCatFunctor(