summaryrefslogtreecommitdiff
path: root/project2/proj2_s4498062/dns/regexes.py
diff options
context:
space:
mode:
authorCamil Staps2016-05-02 22:51:37 +0200
committerCamil Staps2016-05-02 22:51:37 +0200
commit28e067fc983aa8241c782b04406abd9c538a0986 (patch)
treed16857831f340829c7be65a73e89a508101061d0 /project2/proj2_s4498062/dns/regexes.py
parentpylint project 2 (diff)
Project 2: simple DNS resolver
Diffstat (limited to 'project2/proj2_s4498062/dns/regexes.py')
-rw-r--r--project2/proj2_s4498062/dns/regexes.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/project2/proj2_s4498062/dns/regexes.py b/project2/proj2_s4498062/dns/regexes.py
new file mode 100644
index 0000000..48c28c9
--- /dev/null
+++ b/project2/proj2_s4498062/dns/regexes.py
@@ -0,0 +1,29 @@
+"""Regexes used in the DNS protocol"""
+
+def grpm(regex):
+ """Make a matching group"""
+ return grp(regex, matching=True)
+
+def grp(regex, matching=False):
+ """Make a group"""
+ return r'(' + (r'' if matching else r'?:') + regex + r')'
+
+def opt(regex):
+ """Make an optional group"""
+ return grp(grp(regex) + r'?')
+
+def regex_opt_r(*regexes):
+ """Make a group that matches one of the given regexes"""
+ return grp(r'|'.join(regexes))
+
+DIGIT = r'\d'
+LETTER = r'[a-zA-Z]'
+LETDIG = grp(regex_opt_r(DIGIT, LETTER))
+LETDIGHYP = grp(regex_opt_r(LETDIG, r'-'))
+LDHSTR = grp(LETDIGHYP + r'+')
+LABEL = grp(LETTER + opt(opt(LDHSTR) + LETDIG))
+SUBDOMAIN = grp(grpm(grp(LABEL + r'\.') + r'*') + grpm(LABEL))
+DOMAIN = regex_opt_r(SUBDOMAIN, r' ')
+
+IP = r'(?:(?:\d{1,3}\.){3}\d{1,3})'
+