Try:
from parsley import makeGrammar
import ometa
def check_the_rest(s):
if '-' in s:
return s[-1].isalnum()
return True
domain = makeGrammar('''
letdighyp = (letterOrDigit|'-')
# label = <letterOrDigit (letdighyp* letterOrDigit)*>
label = <letter the_rest>
the_rest = letdighyp*:r ?(check_the_rest(r)) -> r
''', dict(check_the_rest=check_the_rest))
tests = ('a', 'abcd1000', 'a-',)
for t in tests:
try:
print('✓', domain(t).label())
except ometa.runtime.ParseError as e:
print('parse failed for', t)
print(e)
(The last test should fail)
I'm using the py3 branch by vsajip.