//===================================================================== /// <summary> /// Writes a file that documents a GrammarProd object /// </summary> public void WriteGrammarFile(String fileName, GrammarProd g) { System.Console.Out.WriteLine("writing " + fileName); StreamWriter w = new StreamWriter(fileName); w.WriteLine("<html>\r\n<head>" + "<link rel=stylesheet type=\"text/css\" " + "href=\"../libref.css\">" + "<title>" + g.Name + "</title>" // + "<script type=\"text/javascript\" " // + "src=\"../libref.js\"></script>" // + "</head><body onload=\"frmLd()\">"); + "</head><body>"); this.WriteFileTitle(w, "GrammarProd", g.Name, null, null); g.MatchObjects.Sort(); foreach (ClassDef c in g.MatchObjects) { WriteObjectDecl(w, c, true); if (c.GrammarRule != null) w.WriteLine("<div class=gramrule>" + EncodeEntities(c.GrammarRule) + "</div>"); } this.WriteHtmlFooter(w); w.Close(); }
//===================================================================== /// <summary> /// Processes a line which appears to define a grammar object. /// "grammar" identifier "(" identifier ")" ":" [token-list] /// ":" [class-identifier-list] /// </summary> private void ProcessGrammar(String line, String origLine) { // note: the keyword "grammar" has already been parsed ClassDef cr = new ClassDef(Path.GetFileName(CurrentFileName), LineNumber); cr.Name = GetNextToken(ref line); cr.Description = LastComment; cr.IsGrammar = true; String origLine2 = origLine; bool isVerbRule = (GetNextToken(ref origLine2) == "VerbRule"); LastComment = ""; if (cr.Name == "") return; // must not be a class after all // parse the tag, if present if (cr.Name == "(" || GetNextToken(ref line) == "(") { // store the tagged version of the name String tag = GetNextToken(ref line); if (GetNextToken(ref line) != ")") return; if (isVerbRule) cr.Name = "VerbRule"; cr.Name += "(" + tag + ")"; // if we created this with VerbRule, remember this if (isVerbRule) { origLine = origLine.Trim(); int idx = origLine.IndexOf(")"); cr.OrigDef = origLine.Substring(0, idx + 1); } } // check for the ":" if (GetNextToken(ref line) != ":" && !isVerbRule) return; // must not be a grammar statement after all // skip the production token list, which probably spans // multiple lines String rule = ""; for (char qu = '\0' ; ; ) { int idx; bool found = false; // scan to the ":", or to end of line for (idx = 0 ; idx < line.Length ; ++idx) { switch (line[idx]) { case '\\': // skip the next character ++idx; break; case ':': // stop unless in a string if (qu == '\0') found = true; break; case '\'': case '"': if (qu != '\0') { if (qu == line[idx]) qu = '\0'; } else qu = line[idx]; break; } // if we found the ':', stop here if (found) break; } // add up to the end of this scan to the rule if (found) { // found it - add up to the ':' to the rule rule += line.Substring(0, idx).TrimEnd(); // continue from just past the ':' line = line.Substring(idx + 1); // we're done scanning the rule break; } // didn't find it - add this line and a newline to the rule if (line.Trim() != "") rule += line + "\u0001"; // continue to the next line of the rule line = GetNextLine(); } // now parse the class list line = line.Trim(); while (line != "") // collect base class names { // get the token cr.BaseClasses.Add(GetNextToken(ref line)); // check for and skip the comma if (GetNextToken(ref line) != ",") break; } // add the grammar list to the class cr.GrammarRule = rule; SymbolTable.SetObjectFileName(cr); SymbolTable.Classes.Add(cr); this.CurrentSourceFile.Classes.Add(cr); CurrClassOrObj = cr; // get the GrammarProd name - this is just the class name // sans the (tag) part int n; String gpName; if ((n = cr.Name.IndexOf('(')) >= 0) gpName = cr.Name.Substring(0, n); else gpName = cr.Name; // find the existing GrammarProd GrammarProd g = SymbolTable.FindGrammarProd(gpName); // if we didn't find one, create it if (g == null) { g = new GrammarProd(Path.GetFileName(CurrentFileName), LineNumber); g.Name = gpName; SymbolTable.SetObjectFileName(g); SymbolTable.GrammarProds.Add(g); } // add me to the GrammarProd's list of match objects g.MatchObjects.Add(cr); cr.GrammarProdObj = g; //if (isVerbRule) //{ // while (!line.Contains("action")) // { // line = GetNextLine(); // if (line.Contains(";")) // break; // } // if (line.Contains("action")) // { // while (GetNextToken(ref line) != "action") // ; // if (GetNextToken(ref line) != "=") // return; // String action = GetNextToken(ref line); // } //} }