Skip to content

Commit

Permalink
Add the new analysis of categories to the converter
Browse files Browse the repository at this point in the history
  • Loading branch information
sungshik committed Oct 8, 2024
1 parent 52297f1 commit eb9b78a
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions rascal-textmate-core/src/main/rascal/lang/textmate/Conversion.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

module lang::textmate::Conversion

import IO;

import Grammar;
import ParseTree;
import String;
Expand All @@ -15,6 +13,7 @@ import util::Monitor;
import lang::oniguruma::Conversion;
import lang::oniguruma::RegExp;
import lang::rascal::grammar::Util;
import lang::rascal::grammar::analyze::Categories;
import lang::rascal::grammar::analyze::Delimiters;
import lang::rascal::grammar::analyze::Dependencies;
import lang::rascal::grammar::analyze::Newlines;
Expand Down Expand Up @@ -169,28 +168,36 @@ private RscGrammar replaceLegacySemanticTokenTypes(RscGrammar rsc)

list[ConversionUnit] analyze(RscGrammar rsc, str name) {
str jobLabel = "Analyzing<name == "" ? "" : " (<name>)">";
jobStart(jobLabel, work = 4);
jobStart(jobLabel, work = 6);

// Analyze dependencies among productions
// Analyze productions
jobStep(jobLabel, "Analyzing productions");
Graph[Production] graph = toGraph(rsc);
Production marker = prod(\empty(), [], {});

bool hasCategory(Production p)
= /\tag("category"(_)) := p;
bool hasActiveCategory(Production p)
= hasCategory(p) && marker in getClosestAncestors(graph, hasCategory, p, \default = just(marker));
bool isNonEmpty(prod(def, _, _))
= !tryParse(rsc, delabel(def), "");
list[Production] prods = deps(graph)
.retainProds(hasActiveCategory)
.retainProds(isNonEmpty)
.getProds();
list[Production] prods = [p | /p: prod(_, _, _) <- rsc];
// Analyze categories
jobStep(jobLabel, "Analyzing categories");
prods = for (p <- prods) {
// If `p` has 0 or >=2 categories, then ignore `p` (unclear which
// category should be used for highlighting)
set[str] categories = getCategories(rsc, p);
if ({_} !:= categories || {NO_CATEGORY} == categories) {
continue;
}
// for (p <- prods) {
// println("<p.def>");
// }
// If each parent of `p` has a category, then ignore `p` (the parents of
// `p` will be used for highlighting instead)
set[Production] parents = lookdown(rsc, delabel(p.def));
if (!any(parent <- parents, NO_CATEGORY in getCategories(rsc, parent))) {
continue;
}
append p;
}
// Analyze emptiness
jobStep(jobLabel, "Analyzing emptiness");
prods = [p | p <- prods, !tryParse(rsc, delabel(p.def), "")];
// Analyze delimiters
jobStep(jobLabel, "Analyzing delimiters");
Expand All @@ -215,10 +222,6 @@ list[ConversionUnit] analyze(RscGrammar rsc, str name) {
units += {unit(rsc, p, false, false, <nothing(), nothing()>, <nothing(), nothing()>) | p <- prodsDelimiters + prodsKeywords, !isEmptyProd(p)};
list[ConversionUnit] ret = sort([*removeStrictPrefixes(units)]);
// for (u <- units) {
// println("<u.prod.def>: <u.recursive>");
// }
// Return
jobEnd(jobLabel);
return ret;
Expand Down

0 comments on commit eb9b78a

Please sign in to comment.