Skip to content

Commit c1ededb

Browse files
committed
Rename function lookup to make its name more consistent with another function
1 parent 562dc55 commit c1ededb

File tree

7 files changed

+49
-53
lines changed

7 files changed

+49
-53
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ RegExp toRegExp(Grammar g, list[Symbol] symbols, set[Attr] attributes) {
6060
RegExp toRegExp(Grammar g, \label(_, symbol))
6161
= toRegExp(g, symbol);
6262
RegExp toRegExp(Grammar g, \parameter(_, _)) {
63-
throw "Presumably unreachable..."; } // Covered by `lookup` (which substitutes actuals for formals)
63+
throw "Presumably unreachable..."; } // Covered by `prodsOf` (which substitutes actuals for formals)
6464
6565
// `ParseTree`: Start
6666
RegExp toRegExp(Grammar g, \start(symbol))
6767
= toRegExp(g, symbol);
6868
6969
// `ParseTree`: Non-terminals
7070
RegExp toRegExp(Grammar g, Symbol s)
71-
= infix("|", [toRegExp(g, p) | p <- lookup(g, s)]) when isNonTerminalType(s);
71+
= infix("|", [toRegExp(g, p) | p <- prodsOf(g, s)]) when isNonTerminalType(s);
7272
7373
// `ParseTree`: Terminals
7474
RegExp toRegExp(Grammar _, \lit(string))
@@ -103,7 +103,7 @@ RegExp toRegExp(Grammar g, \conditional(symbol, conditions)) {
103103
prefixConditions = [c | c <- conditions, isPrefixCondition(c)];
104104
suffixConditions = [c | c <- conditions, isSuffixCondition(c)];
105105
deleteConditions = [c | c <- conditions, isDeleteCondition(c)];
106-
106+
107107
// Convert except conditions (depends on previous conversion)
108108
if (_ <- exceptConditions) {
109109
if (/\choice(symbol, alternatives) := g) {
@@ -112,7 +112,7 @@ RegExp toRegExp(Grammar g, \conditional(symbol, conditions)) {
112112
= \label(l, _) := def
113113
? \except(l) notin exceptConditions
114114
: true;
115-
115+
116116
re = infix("|", toRegExps(g, {a | a <- alternatives, keep(a)}));
117117
}
118118
}
@@ -130,7 +130,7 @@ RegExp toRegExp(Grammar g, \conditional(symbol, conditions)) {
130130
// Convert delete conditions (depends on previous conversions)
131131
if (_ <- deleteConditions) {
132132
RegExp delete = infix("|", [toRegExp(g, s) | \delete(s) <- deleteConditions]);
133-
133+
134134
// TODO: Explain this complicated conversion...
135135
str string = "(?=(?\<head\><re.string>)(?\<tail\>.*)$)(?!(?:<delete.string>)\\k\<tail\>$)\\k\<head\>";
136136
list[str] categories = ["", *re.categories, "", *delete.categories];
@@ -196,7 +196,7 @@ str encode(int char) = preEncoded[char] ? "\\x{<toHex(char)>}";
196196
private set[int] charRange(str from, str to) = {*[charAt(from, 0)..charAt(to, 0) + 1]};
197197
198198
private str toHex(int i)
199-
= i < 16
199+
= i < 16
200200
? hex[i]
201201
: toHex(i / 16) + toHex(i % 16);
202202

rascal-textmate-core/src/main/rascal/lang/rascal/grammar/Util.rsc

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ bool tryParse(Grammar g, Symbol s, str input, bool allowAmbiguity = false) {
4040
bool isRecursive(Grammar g, Symbol s, set[Symbol] checking = {})
4141
= s in checking
4242
? true
43-
: any(p <- lookup(g, delabel(s)),
44-
/Symbol child := p.symbols,
43+
: any(p <- prodsOf(g, delabel(s)),
44+
/Symbol child := p.symbols,
4545
isRecursive(g, child, checking = checking + s));
4646
4747
@synopsis{
@@ -72,7 +72,7 @@ alias Pointer = tuple[Production p, int index];
7272
7373
```
7474
lexical X = Y;
75-
lexical Y = alt1: "[" "[" "[" Z1 "]" "]" "]" | alt2: "<" Z2 ">";
75+
lexical Y = alt1: "[" "[" "[" Z1 "]" "]" "]" | alt2: "<" Z2 ">";
7676
lexical Z1 = "foo" "bar";
7777
lexical Z2 = "baz";
7878
```
@@ -82,7 +82,7 @@ alias Pointer = tuple[Production p, int index];
8282
- `<X,0>`
8383
- `<Y.alt1,3>`
8484
- `<Z1,1>`
85-
85+
8686
The list of pointers to `"qux"` is just empty.
8787
}
8888
@@ -94,7 +94,7 @@ list[Pointer] find(Grammar g, Production p, Symbol s, Direction dir = forward())
9494
if (ith == needle) {
9595
return [<haystack, i>];
9696
}
97-
for (isNonTerminalType(ith), child <- lookup(g, ith)) {
97+
for (isNonTerminalType(ith), child <- prodsOf(g, ith)) {
9898
if (list[Pointer] l: [_, *_] := doFind(doing + haystack, child, s)) {
9999
return [<haystack, i>] + l;
100100
}
@@ -108,30 +108,26 @@ list[Pointer] find(Grammar g, Production p, Symbol s, Direction dir = forward())
108108
}
109109
110110
@synopsis{
111-
Lookdowns a list of productions for symbol `s` in grammar `g`
111+
Gets the list of productions that contain symbol `s` in grammar `g`
112112
}
113113
114-
// TODO: Rename this function because the current name makes little sense in
115-
// isolation (it's supposed to be the opposite of `lookup`, but in that sense,
116-
// the directions are illogical)
117-
118-
set[Production] lookdown(Grammar g, Symbol s)
114+
set[Production] prodsWith(Grammar g, Symbol s)
119115
= {parent | /parent: prod(_, /Symbol _: s, _) := g};
120116
121117
@synopsis{
122-
Lookups a list of productions for symbol `s` in grammar `g`, replacing
118+
Gets the list of productions of symbol `s` in grammar `g`, replacing
123119
formal parameters with actual parameters when needed
124120
}
125121
126-
list[Production] lookup(Grammar g, s: \parameterized-sort(name, actual))
122+
list[Production] prodsOf(Grammar g, s: \parameterized-sort(name, actual))
127123
= [subst(p, formal, actual) | /p: prod(\parameterized-sort(name, formal), _, _) := g.rules[s] ? []]
128124
+ [subst(p, formal, actual) | /p: prod(label(_, \parameterized-sort(name, formal)), _, _) := g.rules[s] ? []];
129125
130-
list[Production] lookup(Grammar g, s: \parameterized-lex(name, actual))
126+
list[Production] prodsOf(Grammar g, s: \parameterized-lex(name, actual))
131127
= [subst(p, formal, actual) | /p: prod(\parameterized-lex(name, formal), _, _) := g.rules[s] ? []]
132128
+ [subst(p, formal, actual) | /p: prod(label(_, \parameterized-lex(name, formal)), _, _) := g.rules[s] ? []];
133129
134-
default list[Production] lookup(Grammar g, Symbol s)
130+
default list[Production] prodsOf(Grammar g, Symbol s)
135131
= [p | /p: prod(s, _, _) := g.rules[s] ? []]
136132
+ [p | /p: prod(label(_, s), _, _) := g.rules[s] ? []];
137133
@@ -143,7 +139,7 @@ default list[Production] lookup(Grammar g, Symbol s)
143139
&T subst(&T t, list[Symbol] from, list[Symbol] to)
144140
= subst(t, toMapUnique(zip2(from, to)))
145141
when size(from) == size(to);
146-
142+
147143
private &T subst(&T t, map[Symbol, Symbol] m)
148144
= visit (t) { case Symbol s => m[s] when s in m };
149145

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private map[Production, set[str]] getCategoriesByProduction(Grammar g) {
3333
3434
// If the new categories of `p` are different from the old ones, then
3535
// propagate these changes to the children of `p`
36-
for (old != new, /Symbol s := p.symbols, child <- lookup(g, delabel(s))) {
36+
for (old != new, /Symbol s := p.symbols, child <- prodsOf(g, delabel(s))) {
3737
doGet(child, new);
3838
}
3939
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ DelimiterPair getInnerDelimiterPair(Grammar g, Symbol s, bool getOnlyFirst = fal
4949
```
5050
lexical X = Y;
5151
lexical Y = Y1 | Y2;
52-
lexical Y1 = "[" Z "]";
52+
lexical Y1 = "[" Z "]";
5353
lexical Y2 = "[" Z ")" [a-z];
5454
lexical Z = [a-z];
5555
```
@@ -83,7 +83,7 @@ private map[Symbol, Maybe[Symbol]] getInnerDelimiterBySymbol(Grammar g, Directio
8383
@memo
8484
private map[Production, Maybe[Symbol]] getInnerDelimiterByProduction(Grammar g, Direction direction, bool getOnlyFirst = false) {
8585
map[Production, Maybe[Symbol]] ret = (p: nothing() | /p: prod(_, _, _) := g);
86-
86+
8787
solve (ret) {
8888
for (p <- ret, ret[p] == nothing()) {
8989
for (s <- reorder(p.symbols, direction)) {
@@ -108,7 +108,7 @@ private map[Production, Maybe[Symbol]] getInnerDelimiterByProduction(Grammar g,
108108
}
109109
110110
private set[Production] getChildren(Grammar g, Symbol s)
111-
= {*lookup(g, s)};
111+
= {*prodsOf(g, s)};
112112
113113
@synopsis{
114114
Gets the unique rightmost delimiter (`begin`) and the unique leftmost
@@ -122,7 +122,7 @@ private set[Production] getChildren(Grammar g, Symbol s)
122122
```
123123
lexical X = Y;
124124
lexical Y = Y1 | Y2;
125-
lexical Y1 = "[" Z "]";
125+
lexical Y1 = "[" Z "]";
126126
lexical Y2 = "[" Z ")" [a-z];
127127
lexical Z = [a-z];
128128
```
@@ -166,7 +166,7 @@ private map[Symbol, Maybe[Symbol]] getOuterDelimiterBySymbol(Grammar g, Directio
166166
ret[s] = unique(delimiters);
167167
}
168168
}
169-
169+
170170
return ret;
171171
}
172172

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private map[Production, Maybe[set[Segment]]] getSegmentsByProduction(Grammar g)
5555
}
5656
5757
private Maybe[set[Segment]] getSegmentsWithEnvironment(
58-
Grammar g, list[Symbol] symbols,
58+
Grammar g, list[Symbol] symbols,
5959
map[Production, Maybe[set[Segment]]] env) {
6060
6161
// General idea: Recursively traverse `symbols` from left to right, while
@@ -73,9 +73,9 @@ private Maybe[set[Segment]] getSegmentsWithEnvironment(
7373
set[Symbol] nested = {s | /Symbol s := head};
7474
7575
Maybe[set[Segment]] finished = get(running, [], final = tail == []);
76-
76+
7777
// If the head contains a non-terminal, then: (1) finish the running
78-
// segment; (2) lookup the segments of the non-terminals in the
78+
// segment; (2) look up the segments of the non-terminals in the
7979
// environment, if any; (3) compute the segments of the tail. Return the
8080
// union of 1-3.
8181
if (any(s <- nested, isNonTerminalType(s))) {
@@ -85,15 +85,15 @@ private Maybe[set[Segment]] getSegmentsWithEnvironment(
8585
sets += finished;
8686
8787
// (2)
88-
sets += for (s <- nested, isNonTerminalType(s), p <- lookup(g, s)) {
88+
sets += for (s <- nested, isNonTerminalType(s), p <- prodsOf(g, s)) {
8989
9090
bool isInitial(Segment seg)
9191
= seg.initial && running.initial && running.symbols == [];
9292
bool isFinal(Segment seg)
9393
= seg.final && tail == [];
9494
Segment update(Segment seg)
9595
= seg[initial = isInitial(seg)][final = isFinal(seg)];
96-
96+
9797
append just(segs) := env[p] ? just({update(seg) | seg <- segs}) : nothing();
9898
}
9999
@@ -103,21 +103,21 @@ private Maybe[set[Segment]] getSegmentsWithEnvironment(
103103
// Return union
104104
return (sets[0] | union(it, \set) | \set <- sets[1..]);
105105
}
106-
106+
107107
// If the head doesn't contain a non-terminal, but it has a newline,
108108
// then: (1) finish the running segment; (2) compute the segments of the
109109
// tail. Return the union of 1-2. Note: the head, as it has a newline,
110110
// is ignored and won't be part of any segment.
111111
else if (any(s <- nested, hasNewline(g, s))) {
112112
return union(finished, get(segment([]), tail));
113113
}
114-
114+
115115
// If the head doesn't contain a non-terminal, and if it doesn't have a
116116
// newline, then add the head to the running segment and proceed with
117117
// the tail.
118118
else {
119119
Segment old = running;
120-
Segment new = old[symbols = old.symbols + head];
120+
Segment new = old[symbols = old.symbols + head];
121121
return get(new, tail);
122122
}
123123
}
@@ -130,7 +130,7 @@ private Maybe[set[Segment]] getSegmentsWithEnvironment(
130130
}
131131
132132
bool hasNewline(Grammar g, Symbol s) {
133-
return any(p <- lookup(g, delabel(s)), hasNewline(g, p));
133+
return any(p <- prodsOf(g, delabel(s)), hasNewline(g, p));
134134
}
135135
136136
@synopsis{
@@ -149,7 +149,7 @@ private map[Production, bool] hasNewlineByProduction(Grammar g) {
149149
for (p <- ret, !ret[p]) {
150150
set[Symbol] nonTerminals = {s | /Symbol s := p.symbols, isNonTerminalType(s)};
151151
ret[p] = ret[p] || any(/r: range(_, _) := p.symbols, hasNewline(r))
152-
|| any(s <- nonTerminals, Production child <- lookup(g, s), ret[child]);
152+
|| any(s <- nonTerminals, Production child <- prodsOf(g, s), ret[child]);
153153
}
154154
}
155155
@@ -165,7 +165,7 @@ private map[Production, bool] hasNewlineByProduction(Grammar g) {
165165
166166
bool hasNewline(str s)
167167
= LF in chars(s);
168-
168+
169169
bool hasNewline(range(begin, end))
170170
= begin <= LF && LF <= end;
171171

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private map[Symbol, Maybe[set[Symbol]]] firstBySymbol(Grammar g, bool(Symbol) pr
5656
for (s <- ret, nothing() == ret[s]) {
5757
if (predicate(s)) {
5858
ret[s] = just({s});
59-
} else if (list[Production] prods: [_, *_] := lookup(g, s)) {
59+
} else if (list[Production] prods: [_, *_] := prodsOf(g, s)) {
6060
ret[s] = (just({}) | union(it, firstOf(reorder(p.symbols, dir))) | p <- prods);
6161
} else {
6262
ret[s] = just({\empty()});
@@ -84,7 +84,7 @@ set[Symbol] follow(Grammar g, Symbol s)
8484
@memo
8585
private map[Symbol, Maybe[set[Symbol]]] followBySymbol(Grammar g, bool(Symbol) predicate, Direction dir) {
8686
map[Symbol, Maybe[set[Symbol]]] ret = (delabel(s): nothing() | s <- g.rules); // Non-terminals
87-
87+
8888
Maybe[set[Symbol]] followOf(Symbol parent, [])
8989
= ret[delabel(parent)];
9090
Maybe[set[Symbol]] followOf(Symbol parent, [h, *t])

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ private RscGrammar replaceLegacySemanticTokenTypes(RscGrammar rsc)
131131
- one synthetic *delimiters* production;
132132
- zero-or-more *user-defined* productions (from `rsc`);
133133
- one synthetic *keywords* production.
134-
134+
135135
Each production in the list (including the synthetic ones) is *suitable for
136136
conversion* to a TextMate rule. A production is "suitable for conversion"
137137
when it satisfies each of the following conditions:
138138
- it is non-recursive;
139139
- it does not match newlines;
140140
- it does not match the empty word;
141141
- it has a `@category` tag.
142-
142+
143143
See the walkthrough for further motivation and examples.
144144
}
145145

@@ -187,7 +187,7 @@ list[ConversionUnit] analyze(RscGrammar rsc, str name) {
187187
188188
// If each parent of `p` has a category, then ignore `p` (the parents of
189189
// `p` will be used for highlighting instead)
190-
set[Production] parents = lookdown(rsc, delabel(p.def));
190+
set[Production] parents = prodsWith(rsc, delabel(p.def));
191191
if (!any(parent <- parents, NO_CATEGORY in getCategories(rsc, parent))) {
192192
continue;
193193
}
@@ -295,7 +295,7 @@ private list[ConversionUnit] addInnerRules(list[ConversionUnit] units) {
295295
bool guard = nothing() := u.innerDelimiters.begin;
296296
TmRule r = toTmRule(toRegExp(u.rsc, u.prod, guard = guard))
297297
[name = "/inner/single/<u.name>"];
298-
298+
299299
rules = insertIn(rules, (u: r));
300300
}
301301
@@ -311,18 +311,18 @@ private list[ConversionUnit] addInnerRules(list[ConversionUnit] units) {
311311
312312
// Simple case: each unit does have an `end` inner delimiter
313313
if (_ <- group && all(u <- group, just(_) := u.innerDelimiters.end)) {
314-
315-
// Create a set of pointers to the first (resp. last) occurrence
314+
315+
// Create a set of pointers to the first (resp. last) occurrence
316316
// of `pivot` in each unit, when `pivot` is a `begin` delimiter
317317
// (resp. an `end` delimiter) of the group. If `pivot` occurs
318318
// elsewhere in the grammar as well, then skip the conversion
319319
// of these multi-line units to a begin/end pattern. This is to
320320
// avoid tokenization mistakes in which the other occurrences of
321321
// `pivot` in the input are mistakenly interpreted as the
322322
// beginning or ending of a unit in the group.
323-
323+
324324
Symbol pivot = key.val;
325-
325+
326326
set[Pointer] pointers = {};
327327
pointers += pivot in begins ? {*find(rsc, u.prod, pivot, dir = forward()) [-1..] | u <- group} : {};
328328
pointers += pivot in ends ? {*find(rsc, u.prod, pivot, dir = backward())[-1..] | u <- group} : {};
@@ -342,7 +342,7 @@ private list[ConversionUnit] addInnerRules(list[ConversionUnit] units) {
342342
toRegExp(rsc, [\alt(ends)], {t}),
343343
[toTmRule(toRegExp(rsc, [s], {t})) | s <- toTerminals(segs)])
344344
[name = "/inner/multi/<intercalate(",", [u.name | u <- group])>"];
345-
345+
346346
rules = insertIn(rules, (u: r | u <- group));
347347
}
348348
@@ -370,7 +370,7 @@ private list[ConversionUnit] addInnerRules(list[ConversionUnit] units) {
370370
// and an `end` delimiter, then generate a
371371
// begin/end pattern to highlight these delimiters
372372
// and all content in between.
373-
373+
374374
set[Segment] segs = getSegments(rsc, suffix);
375375
segs = {removeBeginEnd(seg, {begin}, {end}) | seg <- segs};
376376
@@ -379,7 +379,7 @@ private list[ConversionUnit] addInnerRules(list[ConversionUnit] units) {
379379
toRegExp(rsc, [end], {t}),
380380
[toTmRule(toRegExp(rsc, [s], {t})) | s <- toTerminals(segs)]);
381381
}
382-
382+
383383
else {
384384
// If the suffix has a `begin` delimiter, but not
385385
// an `end` delimiter, then generate a match pattern
@@ -475,7 +475,7 @@ private list[ConversionUnit] addOuterRules(list[ConversionUnit] units) {
475475
toRegExp(rsc, [\alt(ends)], {}),
476476
[include("#<r.name>") | TmRule r <- innerRules])
477477
[name = "/outer/<begin.string>"];
478-
478+
479479
rules = insertIn(rules, (u: r | u <- group));
480480
}
481481
}

0 commit comments

Comments
 (0)