-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpmtools_grammar.py
80 lines (64 loc) · 1.8 KB
/
cpmtools_grammar.py
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
CPMTOOLS = r"""
// Bring in some standard notions
%import common.INT
%import common.CNAME
%import common.LETTER
%import common.DIGIT
%import common.CR
%import common.LF
%import common.WS_INLINE
// Discard WS between tokens
%ignore WS_INLINE
// Newline placement is significant to grammar
EOL: CR? LF
// FF + EOL + WS_INLINE account for all WS
FF: /\f/
%ignore FF
// Track comments with lexer callback
COMMENT: /#.*/
%ignore COMMENT
// Parser grammar starts here
//
diskdef: EOL* DISKDEF defname EOL (pair|EOL)+ END EOL -> end
defname: DEFNAME
DISKDEF: "diskdef"
DEFNAME: (LETTER|DIGIT) ("."|"_"|"-"|LETTER|DIGIT)*
END: "end"
pair: known
| unknown
unknown: /\S+/ /.+/ EOL
known: IPARM INT EOL -> intparm
| SKEWTAB LISTVAL EOL -> listparm
| OFFSET INT [MULTIPLIER [TRAILING]] EOL -> unitparm
| LIBDISK CNAME EOL -> strparm
| OS OSVAL EOL -> strparm
IPARM.1: "seclen"
| "tracks"
| "sectrk"
| "dirblks"
| "blocksize"
| "boottrk"
| "maxdir"
| "skew"
| "logicalextents"
// Give 'skewtab' higher priority than 'skew'
SKEWTAB.2: "skewtab"
OFFSET.1: "offset"
LIBDISK.1: "libdsk:format"
OS.1: "os"
LISTVAL: INT ("," INT)*
OSVAL: "2.2"|"3"|"isx"|"p2dos"|"zsys"
MULTIPLIER: /(k|m|t|s)/i
TRAILING: /\S+/
"""
# Create dictionary to hook lexer actions
def build_callbacks(obj):
return {'COMMENT': obj.lex_comment,
'INLINE': obj.lex_comment,
'EOL': obj.lex_eol,
'DISKDEF': obj.lex_identifier,
'IPARM': obj.lex_identifier,
'SKEWTAB': obj.lex_identifier,
'OFFSET': obj.lex_identifier,
'LIBDISK': obj.lex_identifier,
'OS': obj.lex_identifier}