Skip to content

Commit

Permalink
Simplify a few expressions to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
sungshik committed Sep 9, 2024
1 parent 546bb13 commit e8a887c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ RscGrammar preprocess(RscGrammar rsc) {
// Replace occurrences of singleton ranges with just the corresponding
// literal. This makes it easier to identify delimiters.
return visit (rsc) {
case s: \char-class([range(char, char)]) => d
case \char-class([range(char, char)]) => d
when d := \lit("<stringChar(char)>"), isDelimiter(d)
}
}
Expand Down Expand Up @@ -132,13 +132,14 @@ list[ConversionUnit] analyze(RscGrammar rsc) {
list[Production] prodsKeywords = [prod(lex(KEYWORDS_PRODUCTION_NAME), [\alt(keywords)], {\tag("category"("keyword.control"))})];
// Return
bool isEmptyProd(prod(_, [\alt(alternatives)], _)) = alternatives == {};
set[ConversionUnit] units
= {unit(rsc, p, false, hasNewline(rsc, p), getOuterDelimiterPair(rsc, p), getInnerDelimiterPair(rsc, p, getOnlyFirst = true)) | p <- prodsNonRecursive}
+ {unit(rsc, p, true, hasNewline(rsc, p), getOuterDelimiterPair(rsc, p), getInnerDelimiterPair(rsc, p, getOnlyFirst = true)) | p <- prodsRecursive}
+ {unit(rsc, p, false, false, <nothing(), nothing()>, <nothing(), nothing()>) | p <- prodsDelimiters, !isEmptyProd(p)}
+ {unit(rsc, p, false, false, <nothing(), nothing()>, <nothing(), nothing()>) | p <- prodsKeywords, !isEmptyProd(p)};
bool isRecursive(Production p)
= p in prodsRecursive;
bool isEmptyProd(prod(_, [\alt(alternatives)], _))
= alternatives == {};
set[ConversionUnit] units = {};
units += {unit(rsc, p, isRecursive(p), hasNewline(rsc, p), getOuterDelimiterPair(rsc, p), getInnerDelimiterPair(rsc, p, getOnlyFirst = true)) | p <- prods};
units += {unit(rsc, p, false, false, <nothing(), nothing()>, <nothing(), nothing()>) | p <- prodsDelimiters + prodsKeywords, !isEmptyProd(p)};
return sort([*removeStrictPrefixes(units)]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,8 @@ bool isStrictPrefix(ConversionUnit u1, ConversionUnit u2)
= isStrictPrefix(u1.prod.symbols, u2.prod.symbols);

// TODO: This function could be moved to a separate, generic module
private bool isStrictPrefix([], [])
= false;
private bool isStrictPrefix([], [_, *_])
= true;
private bool isStrictPrefix([_, *_], [])
= false;
private bool isStrictPrefix([head1, *tail1], [head2, *tail2])
= head1 == head2 && isStrictPrefix(tail1, tail2);
private bool isStrictPrefix(list[&T] l1, list[&T] l2)
= size(l1) < size(l2) && !any(i <- [0..size(l1)], l1[i] != l2[i]);

@synopsis{
Representation of a *decomposition* of a list of units (i.e., the lists of
Expand Down

0 comments on commit e8a887c

Please sign in to comment.