44
55module lang ::textmate ::Conversion
66
7+ import IO ;
8+
79import Grammar ;
810import ParseTree ;
911import String ;
@@ -107,20 +109,26 @@ list[ConversionUnit] analyze(RscGrammar rsc, str name) {
107109 str jobLabel = "Analyzing<name == "" ? "" : " (<name > )" > " ;
108110 jobStart (jobLabel , work = 4 );
109111
110- // Define auxiliary predicates
111- bool isCyclic (Production p , set [Production ] ancestors , _)
112- = p in ancestors ;
113- bool isNonEmpty (prod (def , _, _), _, _)
114- = !tryParse (rsc , delabel (def ), "" );
115- bool hasCategory (prod (_, _, attributes ), _, _)
116- = /\tag ("category" (_)) := attributes ;
117-
118112 // Analyze dependencies among productions
119113 jobStep (jobLabel , "Analyzing productions" );
120114 Graph [Production ] graph = toGraph (rsc );
121- list [Production ] prods = deps (graph ).retainProds (isNonEmpty ).retainProds (hasCategory ).getProds ();
122- list [Production ] prodsNonRecursive = prods & deps (graph ).removeProds (isCyclic , true ).getProds ();
123- list [Production ] prodsRecursive = prods - prodsNonRecursive ;
115+ Production marker = prod (\empty (), [], {});
116+
117+ bool hasCategory (Production p )
118+ = /\tag ("category" (_)) := p ;
119+ bool hasActiveCategory (Production p )
120+ = hasCategory (p ) && marker in getClosestAncestors (graph , hasCategory , p , \default = just (marker ));
121+ bool isNonEmpty (prod (def , _, _))
122+ = !tryParse (rsc , delabel (def ), "" );
123+
124+ list [Production ] prods = deps (graph )
125+ .retainProds (hasActiveCategory )
126+ .retainProds (isNonEmpty )
127+ .getProds ();
128+
129+ // for (p <- prods) {
130+ // println("<p.def>");
131+ // }
124132
125133 // Analyze delimiters
126134 jobStep (jobLabel , "Analyzing delimiters" );
@@ -137,17 +145,18 @@ list[ConversionUnit] analyze(RscGrammar rsc, str name) {
137145
138146 // Prepare units
139147 jobStep (jobLabel , "Preparing units" );
140-
141- bool isRecursive (Production p )
142- = p in prodsRecursive ;
143148 bool isEmptyProd (prod (_, [\alt (alternatives )], _))
144149 = alternatives == {};
145-
150+
146151 set [ConversionUnit ] units = {};
147- units += {unit (rsc , p , isRecursive (p ), hasNewline (rsc , p ), getOuterDelimiterPair (rsc , p ), getInnerDelimiterPair (rsc , p , getOnlyFirst = true )) | p <- prods };
152+ units += {unit (rsc , p , isRecursive (rsc , p ), hasNewline (rsc , p ), getOuterDelimiterPair (rsc , p ), getInnerDelimiterPair (rsc , p , getOnlyFirst = true )) | p <- prods };
148153 units += {unit (rsc , p , false , false , <nothing (), nothing ()> , <nothing (), nothing ()> ) | p <- prodsDelimiters + prodsKeywords , !isEmptyProd (p )};
149154 list [ConversionUnit ] ret = sort ([*removeStrictPrefixes (units )]);
150155
156+ // for (u <- units) {
157+ // println("<u.prod.def>: <u.recursive>");
158+ // }
159+
151160 // Return
152161 jobEnd (jobLabel );
153162 return ret ;
0 commit comments