-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.rkt
140 lines (116 loc) · 5.81 KB
/
parser.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#lang brag
;;Copyright 2019 Jonathan Simpson
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
magic : (query | named-query | /EOL)+
query : line (level+ (line | clear-line))*
level : /">"
line : offset /HWS type /HWS test (/HWS message?)? /EOL
clear-line : offset /HWS "clear" (/HWS test)? /EOL
named-query : name-line (level+ (line | clear-line))*
name-line : offset /HWS name-type /HWS MAGIC-NAME (/HWS message?)? /EOL
offset : absoffset | reloffset | indoffset
@absoffset : INTEGER ; An absolute offset from the start of the file.
reloffset : /"&" INTEGER ; The offset relative to the last match offset one level up. Not allowed at level == 0
@indoffset : indoff | relindoff
indoff : /"(" offset1 [ size ] [ op disp ] /")" ;; Read the file at <offset1> of width <size>.
;; If size is not specified, assume a long.
;; If <op> is given, then preform that
;; operation on the result and the <disp>.
@offset1 : absoffset | reloffset
size : byte | leshort | beshort | lelong | belong | melong | ubyte | uleshort | ubeshort | ulelong | ubelong | umelong
byte : ".B" | ".b" | ".C" | ".c" ; A one-byte value.
leshort : ".s" | ".h" ; A two-byte little-endian value.
beshort : ".S" | ".H" ; A two-byte big-endian value.
lelong : ".l" ; A four-byte little-endian value.
belong : ".L" ; A four-byte big-endian value.
melong : ".m" ; A four-byte middle-endian value.
ubyte : ",B" | ",b" | ",C" | ",c" ; A one-byte value.
uleshort : ",s" | ",h" ; A two-byte little-endian value.
ubeshort : ",S" | ",H" ; A two-byte big-endian value.
ulelong : ",l" ; A four-byte little-endian value.
ubelong : ",L" ; A four-byte big-endian value.
umelong : ",m" ; A four-byte middle-endian value.
op : [ invert ] ( "+" | "-" | "*" | "/" | "%" | "&" | "|" | "^" )
invert : "~" ; Flip the bits on result of the <op>.
disp : INTEGER | memvalue
memvalue : /"(" INTEGER /")"
;; INTEGER is interpreted as an absolute or
;; relative offset matching that of <offset1>.
;; Read the file at the resulting offset with
;; the same size as <offset1>
relindoff : /"&" indoff ; add <indoff> to the last match offset at one level up
type : ( numeric | strtype | default | use | indirect )
@unsigned : "u"
;; The value is unsigned.
;; This affects the sign extension of numeric
;; types and the '<' and '>' compares. It is
;; intended for numeric types, but allowed on
;; all types.
numeric : [ unsigned ] ( numtype | datetype ) [ nummask ]
@numtype : byt | short | long | quad | float | double
@byt : "byte"
@short : "short" | "beshort" | "leshort"
@long : "long" | "lelong" | "belong" | "melong"
@quad : "quad" | "lequad" | "bequad"
@float : "float" | "befloat" | "lefloat"
@double : "double" | "bedouble" | "ledouble"
@datetype : udate32 | ldate32 | udate64 | ldate64 | wdate64
@udate32 : "date" | "bedate" | "ledate" | "medate" ;; UTC dates
@ldate32 : "ldate" | "beldate" | "leldate" | "meldate" ;; local dates
@udate64 : "qdate" | "leqdate" | "beqdate" ;; UTC dates
@ldate64 : "qldate" | "leqldate" | "beqldate" ;; local dates
@wdate64 : "qwdate" | "leqwdate" | "beqwdate" ;; windows dates
nummask : op INTEGER
@strtype : regex | search | string8 | string16 | pstring
regex : /"regex" [ /"/" regflag2 ]
regflag : "c" | "s" | "l"
@regflag2 : regflag+ | ( regcnt [ /"/" ] regflag* )
regcnt : INTEGER ; The number of lines or bytes to search. If this missing or zero, the rest of the file is searched.
search : /"search" [ /"/" srchflag ] [ /"/" srchflag ]
@srchflag : strflag+ | srchcnt
srchcnt : INTEGER ; The number of search tries. If this is missing or zero, the rest of the file is searched.
string8 : "string" [ /"/" strflag+ ]
strflag : "b" | "c" | "C" | "t" | "T" | "w" | "W" | "s"
string16 : "bestring16" | "lestring16"
pstring : "pstring" [ /"/" ( ( pstrflag [ pstrjflag ] ) | pstrjflag ) ]
pstrflag : "B" | "H" | "h" | "L" | "l"
pstrjflag : "J"
use : "use"
indirect : "indirect" [ /"/" "r" ]
default : "default"
;; This is intended to be used with the
;; <truetest> ("x" below). It is matched if
;; there has been no previous match at its
;; level or none since the last default at
;; that level. It is useful for implementing
;; switch-like and if/else constructions.
test : numtest | strtest | truetest | use-name ; Test to preform on <type> read from file.
numtest : [ compare ] INTEGER ; If compare is missing, "=" is assumed.
strtest : [ compare ] STRING
;; If compare is missing, "=" is assumed.
;; Note: If the STRING begins with a <compare>
;; character, the <compare> field cannot be
;; omitted.
@compare : "=" | "!" | "<" | ">" | "&" | "^"
truetest : "x" ; This always returns true. To test for the string "x" use "=x".
use-name : MAGIC-NAME
message : [ nospflag ] ( STRING | FMT_STRING ) ; Message to print if test result is true.
nospflag : "%x08" | "\b" ; Do not insert a space before the message. By default, messages are separated by a " ".
name-type : "name"
;HWS : " " | "\t"
;EOL : "\n"
;FMTSTR : STRING
;INTEGER : ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")+
;STRING : ("A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z")+
;FMTSTR : <printf format string with exactly one % construct>