aboutsummaryrefslogtreecommitdiff
path: root/grammars/builtins.bpeg
blob: 68a77917c1d540a0fbe9d1fd2401a1369060a88b (plain)
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
yes = (/);
no = !(/);

# Meta-rules for acting on everything
pattern = !(/); # Not defined by default
replacement = !(/); # Not defined by default
replace-all = add-filename 1+(...@replacement) ...;
find-all = add-filename 1+find-next%\n 0-1{!<\n => "\n"};
find-next = matching-line / {..\n =>} find-next;
only-matches = 1+{...@pattern=>'@1\n'};
matching-line = add-linenum 1+(..@pattern) ..$;

is-tty = no; # Defined as either always-match or always-fail, depending on stdout
print-line-numbers = is-tty;
print-filenames = is-tty;
add-filename = 0-1(print-filenames (is-tty {=>"\033[33;1;4m@&:\033[0m\n"} / {=>"@&:\n"}));
add-linenum = 0-1(print-line-numbers (is-tty {=>"\033[2m@#\033[5G|\033[0m "} / {=>"@#| "}));

# Helper definitions (commonly used)
indent = \n|1+(\t/' ');
dedent = $ !(\n|);
indented-block = |` ..$ 0+(\n|..$);
crlf = \r\n;
cr = \r; r = \r;
anglebraces = `< 0+(anglebraces / !`>.) `>;
brackets = `[ 0+(brackets / !`].) `];
braces = `{ 0+(braces / !`}.) `};
parens = `( 0+(parens / !`).) `);
id = !<(`a-z/`A-Z/`_/`0-9) (`a-z/`A-Z/`_) 0+(`a-z/`A-Z/`_/`0-9);
word = !<(`a-z/`A-Z/`_/`0-9) 1+(`a-z/`A-Z) !>(`0-9/`_);
HEX = `0-9/`A-F;
Hex = `0-9/`a-f/`A-F;
hex = `0-9/`a-f;
number = 1+`0-9 0-1(`. 0+`0-9) / `. 1+`0-9;
int = 1+`0-9;
digit = `0-9;
Abc = `a-z/`A-Z;
ABC = `A-Z;
abc = `a-z;
esc = \e; e = \e;
tab = \t; t = \t;
nl = \n; lf = \n; n = \n;
c-block-comment = '/*' ... '*/';
c-line-comment = '//' ..$;
c-comment = c-line-comment / c-block-comment;
hash-comment = `# ..$;
comment = !(/); # No default definition, can be overridden
WS = ` /\t/\n/\r/comment;
ws = ` /\t;
$$ = !$.;
$ = !.;
^^ = !<$.;
^ = !<.;
__ = 0+(` /\t/\n/\r/comment);
_ = 0+(` /\t);