Skip to content

Commit

Permalink
Use util::Monitor instead of println to report conversion progress
Browse files Browse the repository at this point in the history
  • Loading branch information
sungshik committed Sep 25, 2024
1 parent 5eda17f commit 5444141
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions rascal-textmate-core/src/main/rascal/lang/textmate/Conversion.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
module lang::textmate::Conversion

import Grammar;
import IO;
import ParseTree;
import String;
import util::Maybe;
import util::Monitor;

import lang::oniguruma::Conversion;
import lang::oniguruma::RegExp;
Expand Down Expand Up @@ -45,8 +45,8 @@ alias RscGrammar = Grammar;
may contain additional meta-data needed during the transformation stage.
}

TmGrammar toTmGrammar(RscGrammar rsc, ScopeName scopeName, NameGeneration nameGeneration = long())
= transform(analyze(preprocess(rsc)), nameGeneration = nameGeneration) [scopeName = scopeName];
TmGrammar toTmGrammar(RscGrammar rsc, str name, NameGeneration nameGeneration = long())
= transform(analyze(preprocess(rsc), name), name, nameGeneration = nameGeneration);

@synopsis{
Preprocess Rascal grammar `rsc` to make it suitable for analysis and
Expand Down Expand Up @@ -103,7 +103,9 @@ RscGrammar preprocess(RscGrammar rsc) {
In stage 3, the set of all keywords that occur in `rsc` is created.
}

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

// Define auxiliary predicates
bool isCyclic(Production p, set[Production] ancestors, _)
Expand All @@ -114,26 +116,28 @@ list[ConversionUnit] analyze(RscGrammar rsc) {
= /\tag("category"(_)) := attributes;
// Analyze dependencies among productions
println("[LOG] Analyzing dependencies among productions");
jobStep(jobLabel, "Analyzing productions");
Graph[Production] graph = toGraph(rsc);
list[Production] prods = deps(graph).retainProds(isNonEmpty).retainProds(hasCategory).getProds();
list[Production] prodsNonRecursive = prods & deps(graph).removeProds(isCyclic, true).getProds();
list[Production] prodsRecursive = prods - prodsNonRecursive;
// Analyze delimiters
println("[LOG] Analyzing delimiters");
jobStep(jobLabel, "Analyzing delimiters");
set[Symbol] delimiters
= removeStrictPrefixes({s | /Symbol s := rsc, isDelimiter(delabel(s))})
- {s | p <- prods, /just(s) := getOuterDelimiterPair(rsc, p)}
- {s | p <- prods, /just(s) := getInnerDelimiterPair(rsc, p, getOnlyFirst = true)};
list[Production] prodsDelimiters = [prod(lex(DELIMITERS_PRODUCTION_NAME), [\alt(delimiters)], {})];
// Analyze keywords
println("[LOG] Analyzing keywords");
jobStep(jobLabel, "Analyzing keywords");
set[Symbol] keywords = {s | /Symbol s := rsc, isKeyword(delabel(s))};
list[Production] prodsKeywords = [prod(lex(KEYWORDS_PRODUCTION_NAME), [\alt(keywords)], {\tag("category"("keyword.control"))})];
// Return
// Prepare units
jobStep(jobLabel, "Preparing units");
bool isRecursive(Production p)
= p in prodsRecursive;
bool isEmptyProd(prod(_, [\alt(alternatives)], _))
Expand All @@ -142,7 +146,11 @@ list[ConversionUnit] analyze(RscGrammar rsc) {
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)]);
list[ConversionUnit] ret = sort([*removeStrictPrefixes(units)]);
// Return
jobEnd(jobLabel);
return ret;
}
@synopsis{
Expand All @@ -156,24 +164,27 @@ list[ConversionUnit] analyze(RscGrammar rsc) {
2. composition of TextMate rules into a TextMate grammar.
}
TmGrammar transform(list[ConversionUnit] units, NameGeneration nameGeneration = long()) {
TmGrammar transform(list[ConversionUnit] units, str name, NameGeneration nameGeneration = long()) {
str jobLabel = "Transforming<name == "" ? "" : " (<name>)">";
jobStart(jobLabel, work = 2);
// Transform productions to rules
println("[LOG] Transforming productions to rules");
jobStep(jobLabel, "Transforming productions to rules");
units = addNames(units, nameGeneration);
units = addInnerRules(units);
units = addOuterRules(units);
// Transform rules to grammar
println("[LOG] Transforming rules to grammar");
jobStep(jobLabel, "Transforming rules to grammar");
set[TmRule] innerRules = {*u.innerRules | u <- units};
set[TmRule] outerRules = {*u.outerRules | u <- units};
Repository repository = ("<r.name>": r | TmRule r <- innerRules + outerRules);
list[TmRule] patterns = dupLast([include("#<r.name>") | u <- units, r <- getTopLevelRules(u)]);
TmGrammar tm = lang::textmate::Grammar::grammar(repository, "", patterns);
// Return
return tm;
jobEnd(jobLabel);
return tm[scopeName = name];
}
private list[ConversionUnit] addNames(list[ConversionUnit] units, NameGeneration nameGeneration) {
Expand Down

0 comments on commit 5444141

Please sign in to comment.