-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
160 lines (151 loc) · 4.51 KB
/
main.js
File metadata and controls
160 lines (151 loc) · 4.51 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
define(function (require, exports, module) {
'use strict';
var LanguageManager = brackets.getModule("language/LanguageManager");
CodeMirror.defineMode("packetfilter", function () {
// The code below is based on Puppet syntax highlighter from https://github.com/nextrevision/brackets-puppet-syntax
var words = {};
// Takes a string of words separated by spaces and adds them as
// keys with the value of the first argument 'style'
function define(style, string) {
var split = string.split(' ');
for (var i = 0; i < split.length; i++) {
words[split[i]] = style;
}
}
// Additional themes => https://github.com/MiguelCastillo/Brackets-Themes
// keyword atom number def variable variable-2 variable-3 property operator comment string string-2
// meta qualifier builtin bracket tag attribute header quote hr link special
define('keyword', 'altq anchor antispoof binat nat pass block queue rdr match scrub table set in out rdr-to divert-to route-to reply-to nat-to');
define('atom', 'all any yes no drop return');
define('builtin', 'proto inet inet6 tcp udp icmp icmp6 port other carp pfsync');
define('def', 'label user file timeout limit optimization block-policy loginterface require-order skip synproxy state parent bandwidth static-port');
define('tag', 'tag tagged');
define('qualifier','quick persist');
define('attribute','log');
// After finding a start of a string ('|") this function attempts to find the end;
// If a variable is encountered along the way, we display it differently when it
// is encapsulated in a double-quoted string.
function tokenString(stream, state) {
var current, prev, found_var = false;
while (!stream.eol() && (current = stream.next()) != state.pending) {
if (current === '$' && prev != '\\' && state.pending == '"') {
found_var = true;
break;
}
prev = current;
}
if (found_var) {
stream.backUp(1);
}
if (current == state.pending) {
state.continueString = false;
} else {
state.continueString = true;
}
return "string";
}
function tokenize(stream, state) {
// Matches one whole word
var word = stream.match(/[\w\-]+/, false);
// Finally advance the stream
var ch = stream.next();
// Have we found a variable ?
if (ch === '$') {
if (stream.match(/^[a-zA-Z0-9_\-]+/)) {
// If so, and its in a string, assign it a different color
return state.continueString ? 'variable-2' : 'variable';
}
// Otherwise return an invalid variable
return "error";
}
// Should we still be looking for the end of a string?
if (state.continueString) {
// If so, go through the loop again
stream.backUp(1);
return tokenString(stream, state);
}
if (word && words.hasOwnProperty(word)) {
// Negates the initial next()
stream.backUp(1);
// Acutally move the stream
stream.match(/[\w\-]+/);
return words[word];
}
// Match comments
if (ch == "#") {
stream.skipToEnd();
return "comment";
}
// Have we found a string?
if (ch == "'" || ch == '"') {
// Store the type (single or double)
state.pending = ch;
// Perform the looping function to find the end
return tokenString(stream, state);
}
// Match brackets
if (ch == '{' || ch == '}') {
return 'bracket';
}
// Match tables
if (ch == '<') {
if (stream.match(/^[a-zA-Z0-9_\-\.]+>/)) {
return 'special';
}
}
// Match subnets
if (ch == '/') {
stream.match(/[0-9]+/);
return 'property';
}
// Match numbers and IPs
if (ch.match(/[0-9]/)) {
stream.eatWhile(/[0-9\.]+/);
return 'number';
}
// Match the '=' operator
if (ch == '=') {
return "operator";
}
if (ch == ':') {
return "operator";
}
if (ch == '>') {
return "operator";
}
if (ch == '<') {
return "operator";
}
if (ch == '!') {
return "operator";
}
if (ch == ',') {
return null;
}
// Keep advancing through all the rest
stream.eatWhile(/[\w-]/);
// Return unformatted for everything else
return null;
}
return {
startState: function () {
var state = {};
state.continueString = false;
state.pending = false;
return state;
},
token: function (stream, state) {
// Strip the spaces
if (stream.eatSpace()) return null;
// Go through the main process
return tokenize(stream, state);
}
};
});
LanguageManager.defineLanguage("packetfilter", {
name: "Packet Filter",
mode: "packetfilter",
fileNames: ["pf.conf"],
lineComment: ["#"]
});
});