-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-regex.p6
98 lines (88 loc) · 1.98 KB
/
test-regex.p6
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
use v6;
my $source-code = q:to/END/;
class ClassA::B {
# Some class FakeComment weird comment
method A { }
}
END
# Remove line comments
$source-code = $source-code.lines.map({
$_.subst(/ '#' (.+?) $ /, { '#' ~ (" " x $0.chars) })
}).join("\n");
say $source-code;
my $to = 0;
my $line-number = 0;
my @line-ranges;
for $source-code.lines -> $line {
my $length = $line.chars;
my $from = $to;
$to += $length;
@line-ranges.push: {
line-number => $line-number++,
from => $from,
to => $to
};
}
sub to-line-number(Int $position) {
for @line-ranges -> $line-range {
if $position >= $line-range<from> && $position <= $line-range<to> {
return $line-range<line-number>;
}
}
return -1;
}
# Find all package declarations
my @package-declarations = $source-code ~~ m:global/
# Declaration
('class'| 'grammar'| 'module'| 'package'| 'role')
# Whitespace
\s+
# Identifier
(\w+ ('::' \w+))
/;
for @package-declarations -> $decl {
my %record = %(
from => $decl[0].from,
to => $decl[0].pos,
line-number => to-line-number($decl[0].from) + 1,
type => ~$decl[0],
name => ~$decl[1],
);
say %record;
}
my @variable-declarations = $source-code ~~ m:global/
# Declaration
('my'| 'state')
# Whitespace
\s+
# Identifier
(( '$' | '@' | '%') \w+)
/;
for @variable-declarations -> $decl {
my %record = %(
from => $decl[0].from,
to => $decl[0].pos,
line-number => to-line-number($decl[0].from) + 1,
type => ~$decl[0],
name => ~$decl[1],
);
say %record;
}
my @routine-declarations = $source-code ~~ m:global/
# Declaration
('sub'| 'method')
# Whitespace
\s+
# Identifier
(\w+)
/;
for @routine-declarations -> $decl {
my %record = %(
from => $decl[0].from,
to => $decl[0].pos,
line-number => to-line-number($decl[0].from) + 1,
type => ~$decl[0],
name => ~$decl[1],
);
say %record;
}