1module Deckard =
2  autoload xfm
3
4let del_str = Util.del_str
5
6let space = del /[ \t]+/ " "
7let tab = del /[ \t]+/ "\t"
8let ws = del /[\t ]*/ ""
9let word = /[^\t\n\/; ]+/
10
11let comment = del /[;]/ ";" . [label "comment" . store /[^\n]+/]
12
13let eol = del /([ \t]*([;][^\n]*)?\n)+/ "\n" . Util.indent
14let comment_or_eol =  ws . comment? . del_str "\n" . del /([ \t]*([;][^\n]*)?\n)*/ "" . Util.indent
15
16
17(*let comment_or_eol = [ label "#comment" . counter "comment" . (ws . [del /[;#]/ ";" . label "" . store /[^\n]*/ ]? . del_str "\n")]+ . Util.indent
18*)
19
20
21let domain_re = (/[^.\t\n\/; ]+(\.[^.\t\n\/; ]+)*\.?/ | ".") - "SECTION" (*quick n dirty, sorry to whoever will ever own SECTION TLD*)
22let class_re = /CLASS[0-9]+/ | "IN" | "CH" | "HS" | "NONE" | "ANY"
23let domain = [ label "domain" . store domain_re ]
24let ttl = [label "ttl" . store /[0-9]+/]
25let class = [label "class" . store class_re ]
26let type = [label "type" . store ((/[^0-9;\n \t][^\t\n\/; ]*/) - class_re) ]
27(* RFC 3597 section 5 rdata syntax is "\# 1 ab"*)
28let data_re = /((\\#[ \t])?[^ \t\n;][^\n;]*[^ \t\n;])|[^ \t\n;]/ (*Can not start nor end with whitespace but can have whitespace in the middle. Disjunction is there so we match strings of length one.*)
29let data = [label "data" . store data_re ]
30
31let ip_re = /[0-9a-f.:]+/
32let hex_re = /[0-9a-fA-F]+/
33
34
35let match_option =  "opcode" | "qtype" | "qcase" | "qname" | "subdomain" | "flags" | "rcode" | "question" | "answer" | "authority" | "additional" | "all" | "edns"
36let adjust_option = "copy_id" | "copy_query" | "raw_id" | "do_not_answer"
37let reply_option = "QR" | "TC" | "AA" | "AD" | "RD" | "RA" | "CD" | "DO" | "NOERROR" | "FORMERR" | "SERVFAIL" | "NXDOMAIN" | "NOTIMP" | "REFUSED" | "YXDOMAIN" | "YXRRSET" | "NXRRSET" | "NOTAUTH" | "NOTZONE" | "BADVERS" | "BADSIG" | "BADKEY" | "BADTIME" | "BADMODE" | "BADNAME" | "BADALG" | "BADTRUNC" | "BADCOOKIE"
38let step_option = "REPLY" | "QUERY" | "CHECK_ANSWER" | "CHECK_OUT_QUERY" | /TIME_PASSES[ \t]+ELAPSE/
39
40let mandatory = [del_str "MANDATORY" . label "mandatory" . value "true" . comment_or_eol]
41let tsig = [del_str "TSIG" . label "tsig" . space . [label "keyname" . store word] . space . [label "secret" . store word] . comment_or_eol]
42
43let match = (mandatory | tsig)* . [ label "match_present" . value "true" . del_str "MATCH" ] . [space . label "match" . store match_option ]+ . comment_or_eol
44let adjust =  (mandatory | tsig)* . del_str "ADJUST" . [space . label "adjust" . store adjust_option ]+ . comment_or_eol
45let reply =  (mandatory | tsig)* . del ("REPLY" | "FLAGS") "REPLY" .  [space . label "reply" . store reply_option ]+ . comment_or_eol
46
47
48let question = [label "record" . domain . tab . (class . tab)? . type . comment_or_eol ]
49let record = [label "record" . domain . tab . (ttl . tab)? . (class . tab)? . type . tab . data . comment_or_eol]
50
51let section_question = [ label "question" . del_str "SECTION QUESTION" .
52                       comment_or_eol . question? ]
53let section_answer = [ label "answer" . del_str "SECTION ANSWER" .
54                       comment_or_eol . record* ]
55let section_authority = [ label "authority" . del_str "SECTION AUTHORITY" .
56                          comment_or_eol . record* ]
57let section_additional = [ label "additional" . del_str "SECTION ADDITIONAL" .
58                           comment_or_eol . record* ]
59let sections = [label "section" . section_question? . section_answer? . section_authority? . section_additional?]
60let raw = [del_str "RAW" . comment_or_eol . label "raw" . store hex_re  ] . comment_or_eol
61
62(* This is quite dirty hack to match every combination of options given to entry since 'let dnsmsg = ((match | adjust | reply | mandatory | tsig)* . sections)' just is not possible *)
63
64let dnsmsg = (match . (adjust . reply? | reply . adjust?)? | adjust . (match . reply? | reply . match?)? | reply . (match . adjust? | adjust . match?)?)? . (mandatory | tsig)* . sections
65
66let entry = [label "entry" . del_str "ENTRY_BEGIN" . comment_or_eol . dnsmsg . raw? . del_str "ENTRY_END" . eol]
67
68let single_address = [ label "address" . space . store ip_re ]
69
70let addresses = [label "address" . counter "address" . [seq "address" . del_str "ADDRESS" . space . store ip_re . comment_or_eol]+]
71
72let range = [label "range" . del_str "RANGE_BEGIN" . space . [ label "from" . store /[0-9]+/] . space .
73            [ label "to" . store /[0-9]+/] . single_address? . comment_or_eol . addresses? . entry* . del_str "RANGE_END" . eol]
74
75let step = [label "step" . del_str "STEP" . space . store /[0-9]+/ . space . [label "type" . store step_option] . [space . label "timestamp" . store /[0-9]+/]? . comment_or_eol .
76           entry? ]
77
78let config_record = /[^\n]*/ - ("CONFIG_END" | /STEP.*/ | /SCENARIO.*/ | /RANGE.*/ | /ENTRY.*/)
79
80let config = [ label "config" . counter "config" . [seq "config" . store config_record . del_str "\n"]* . del_str "CONFIG_END" . comment_or_eol ]
81
82let guts = (step | range )*
83
84let scenario = [label "scenario" . del_str "SCENARIO_BEGIN" . space . store data_re . comment_or_eol . guts . del_str "SCENARIO_END" . eol]
85
86let lns = config? . scenario
87
88(* TODO: REPLAY step *)
89(* TODO: store all comments into the tree instead of ignoring them *)
90
91(*let filter = incl "/home/test/*.rpl"*)
92let filter = incl "/home/sbalazik/nic/deckard/git/sets/resolver/*.rpl"
93
94let xfm = transform lns filter
95