我正试图让域名的语法有限。语法在http://tools.ietf.org/html/rfc1035第2.3.1节中定义。它的一个子集在
下面<label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
<let-dig-hyp> ::= <let-dig> | "-"
<let-dig> ::= <letter> | <digit>
<letter> ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case
<digit> ::= any one of the ten digits 0 through 9
我的尝试如下。我正在尝试匹配label
from parsley import makeGrammar
import ometa
domain = makeGrammar('''
letdighyp = (letterOrDigit|-)
label = letterOrDigit letdighyp+ letterOrDigit
''', {})
tests = ('abcd1000',)
for t in tests:
try:
print domain(t).label()
except ometa.runtime.ParseError as e:
print 'parse failed for', t
print e
跑步给了我
parse failed for abcd1000
abcd1000
^
Parse error at line 2, column 0: expected EOF. trail: [digit letdig letdighyp]
我做错了什么?
P.S。
label = letterOrDigit letdighyp+ letterOrDigit
是我无法工作的路线。如果最后一个letterOrDigit不存在,它匹配字符串。
答案 0 :(得分:0)
尝试:
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)
(最后一次测试失败)
我正在vsajip使用py3分支。