Skip to content

Commit 899fb3d

Browse files
authored
Merge pull request #18 from SWAT-engineering/utility-modules-for-lists-and-maps
Utility modules for lists and maps
2 parents 8557e38 + b0a2942 commit 899fb3d

File tree

7 files changed

+53
-59
lines changed

7 files changed

+53
-59
lines changed

rascal-textmate-core/src/main/rascal/lang/rascal/grammar/analyze/Delimiters.rsc

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,11 @@ import Grammar;
88
import ParseTree;
99
import util::Maybe;
1010

11-
import Prelude;
12-
1311
import lang::rascal::grammar::Util;
12+
import util::ListUtil;
1413

1514
alias DelimiterPair = tuple[Maybe[Symbol] begin, Maybe[Symbol] end];
1615

17-
data Direction // Traverse lists of symbols (in productions)...
18-
= forward() // - ...from left to right;
19-
| backward() // - ...from right to left.
20-
;
21-
22-
@synopsis{
23-
Reorder a list according to the specified direction
24-
}
25-
26-
list[&T] reorder(list[&T] l, forward()) = l;
27-
list[&T] reorder(list[&T] l, backward()) = reverse(l);
28-
2916
@synopsis{
3017
Gets the unique leftmost delimiter (`begin`) and the unique rightmost
3118
delimiter `end`, if any, that occur **inside** productions of symbol `s`

rascal-textmate-core/src/main/rascal/lang/rascal/grammar/analyze/Symbols.rsc

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,9 @@ import util::Math;
2222
import util::Maybe;
2323

2424
import lang::rascal::grammar::Util;
25+
import util::ListUtil;
2526
import util::MaybeUtil;
2627

27-
@synopsis{
28-
Representation of a traversal direction along a list of symbols
29-
}
30-
31-
data Direction // Traverse lists of symbols (in productions)...
32-
= forward() // - ...from left to right;
33-
| backward() // - ...from right to left.
34-
;
35-
36-
private list[&T] reorder(list[&T] l, forward()) = l;
37-
private list[&T] reorder(list[&T] l, backward()) = reverse(l);
38-
3928
@synopsis{
4029
Computes the *last* set of symbol `s` in grammar `g`
4130
}
@@ -58,8 +47,8 @@ private map[Symbol, Maybe[set[Symbol]]] firstBySymbol(Grammar g, bool(Symbol) pr
5847
5948
Maybe[set[Symbol]] firstOf([])
6049
= just({});
61-
Maybe[set[Symbol]] firstOf([Symbol h, *Symbol t])
62-
= \set: just({\empty(), *_}) := ret[delabel(h)]
50+
Maybe[set[Symbol]] firstOf([h, *t])
51+
= Maybe[set[Symbol]] \set: just({\empty(), *_}) := ret[delabel(h)]
6352
? util::MaybeUtil::union(\set, firstOf(t))
6453
: ret[delabel(h)];
6554

rascal-textmate-core/src/main/rascal/lang/textmate/Conversion.rsc

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import lang::textmate::ConversionConstants;
2121
import lang::textmate::ConversionUnit;
2222
import lang::textmate::Grammar;
2323
import lang::textmate::NameGeneration;
24+
import util::ListUtil;
25+
import util::MapUtil;
2426

2527
alias RscGrammar = Grammar;
2628

@@ -400,20 +402,6 @@ private list[Symbol] toTerminals(set[Segment] segs) {
400402
return terminals;
401403
}
402404
403-
// TODO: This function could be moved to a separate, generic module
404-
private list[&T] dupLast(list[&T] l)
405-
= reverse(dup(reverse(l))); // TODO: Optimize/avoid `reverse`-ing?
406-
407-
// TODO: This function could be moved to a separate, generic module
408-
private map[&K, list[&V]] insertIn(map[&K, list[&V]] m, map[&K, &V] values)
409-
// Updates the mapping of each key `k` in map of lists `m` to be the union
410-
// of: (1) the existing list `m[k]`, and (2) the new elements-to-be-inserted
411-
// `values[k]`. For instance:
412-
// - m = ("foo": [1, 2, 3], "bar": [], "baz": [1, 2])
413-
// - values = ("foo": [4, 5], "bar": [123], "qux": [3, 4])
414-
// - return = ("foo": [1, 2, 3, 4, 5], "bar": [123], "baz": [1, 2])
415-
= (k: m[k] + (k in values ? [values[k]] : []) | k <- m);
416-
417405
private TmRule toTmRule(RegExp re)
418406
= match(
419407
re.string,

rascal-textmate-core/src/main/rascal/lang/textmate/ConversionUnit.rsc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import lang::rascal::grammar::analyze::Delimiters;
1515
import lang::textmate::ConversionConstants;
1616
import lang::textmate::Grammar;
1717
import lang::textmate::NameGeneration;
18+
import util::ListUtil;
1819

1920
@synopsis{
2021
Representation of a production in a Rascal grammar to be converted to a rule
@@ -164,10 +165,6 @@ set[ConversionUnit] removeStrictPrefixes(set[ConversionUnit] units)
164165
bool isStrictPrefix(ConversionUnit u1, ConversionUnit u2)
165166
= isStrictPrefix(u1.prod.symbols, u2.prod.symbols);
166167

167-
// TODO: This function could be moved to a separate, generic module
168-
private bool isStrictPrefix(list[&T] l1, list[&T] l2)
169-
= size(l1) < size(l2) && !any(i <- [0..size(l1)], l1[i] != l2[i]);
170-
171168
@synopsis{
172169
Representation of a *decomposition* of a list of units (i.e., the lists of
173170
symbols of their productions) into their maximally common *prefix*

rascal-textmate-core/src/main/rascal/lang/textmate/Grammar.rsc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,6 @@ str toJSON(TmGrammar g, int indent = 2, loc l = |unknown:///|) {
7979
return asJSON(g, indent = indent);
8080
}
8181

82-
@synopsis{
83-
Adds a TextMate rule to both the repository and the patterns of TextMate
84-
grammar `g`
85-
}
86-
87-
TmGrammar addRule(TmGrammar g, TmRule r)
88-
= g [repository = g.repository + (r.name: r)]
89-
[patterns = appendIfAbsent(g.patterns, include("#<r.name>"))];
90-
91-
// TODO: This function could be moved to a separate, generic module
92-
private list[&T] appendIfAbsent(list[&T] vs, &T v)
93-
= v in vs ? vs : vs + v;
94-
9582
@synopsis{
9683
Converts list of strings `names` (typically categories) to a map of captures
9784
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module util::ListUtil
2+
3+
import List;
4+
5+
@synopsis{
6+
Representation of a traversal direction along a list
7+
}
8+
9+
data Direction // Traverse lists...
10+
= forward() // - ...from left to right;
11+
| backward() // - ...from right to left.
12+
;
13+
14+
@synopsis{
15+
Reorder a list according to the specified direction
16+
}
17+
18+
list[&T] reorder(list[&T] l, forward()) = l;
19+
list[&T] reorder(list[&T] l, backward()) = reverse(l);
20+
21+
@synopsis{
22+
Removes multiple occurrences of elements in a list. The last occurrence
23+
remains (cf. `List::dup`).
24+
}
25+
26+
list[&T] dupLast(list[&T] l) = reverse(dup(reverse(l))); // TODO: Optimize/avoid `reverse`-ing?
27+
28+
@synopsis{
29+
Checks if list `l1` is a strict prefix of list `l2`
30+
}
31+
32+
bool isStrictPrefix(list[&T] l1, list[&T] l2)
33+
= size(l1) < size(l2) && l1 == l2[..size(l1)];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module util::MapUtil
2+
3+
@synopsis{
4+
Updates the mapping of each key `k` in map of lists `m` to be the union of:
5+
(1) the existing list `m[k]`, and (2) the new elements-to-be-inserted
6+
`values[k]`. For instance:
7+
- m = ("foo": [1, 2, 3], "bar": [], "baz": [1, 2])
8+
- values = ("foo": [4, 5], "bar": [123], "qux": [3, 4])
9+
- return = ("foo": [1, 2, 3, 4, 5], "bar": [123], "baz": [1, 2])
10+
}
11+
12+
map[&K, list[&V]] insertIn(map[&K, list[&V]] m, map[&K, &V] values)
13+
= (k: m[k] + (k in values ? [values[k]] : []) | k <- m);

0 commit comments

Comments
 (0)