public GraphNode (GraphNode parent, int state, char c) { State = state; Char = c; if (parent != null) { parent.Children.Add (this); Name = parent.Name + c; } else { Name = "&"; } }
public static void Main (string[] args) { int maxEntityLength = 0; int state = 0; using (var json = new JsonTextReader (new StreamReader ("HtmlEntities.json"))) { while (json.Read ()) { string name, value; if (json.TokenType == JsonToken.StartObject) continue; if (json.TokenType != JsonToken.PropertyName) break; name = (string) json.Value; // trim leading '&' and trailing ';' name = name.TrimStart ('&').TrimEnd (';'); if (!json.Read () || json.TokenType != JsonToken.StartObject) break; // read to the "codepoints" property if (!json.Read () || json.TokenType != JsonToken.PropertyName) break; // skip the array of integers... if (!json.Read () || json.TokenType != JsonToken.StartArray) break; while (json.Read ()) { if (json.TokenType == JsonToken.EndArray) break; } // the property should be "characters" - this is what we want if (!json.Read () || json.TokenType != JsonToken.PropertyName) break; value = json.ReadAsString (); var node = Root; for (int i = 0; i < name.Length; i++) { bool found = false; for (int j = 0; j < node.Children.Count; j++) { if (node.Children[j].Char == name[i]) { node = node.Children[j]; found = true; break; } } if (!found) { node = new GraphNode (node, ++state, name[i]); continue; } } if (node.Value == null) { FinalStates.Add (node.State, node); node.Value = value; } maxEntityLength = Math.Max (maxEntityLength, name.Length); if (!json.Read () || json.TokenType != JsonToken.EndObject) break; } } using (var output = new StreamWriter ("HtmlEntityDecoder.g.cs")) { output.WriteLine ("// WARNING: This file is auto-generated. DO NOT EDIT!"); output.WriteLine (); output.WriteLine ("namespace HtmlKit {"); output.WriteLine ("\tpublic partial class HtmlEntityDecoder {"); output.WriteLine ("\t\tconst int MaxEntityLength = {0};", maxEntityLength); output.WriteLine (); GeneratePushNamedEntityMethod (output); output.WriteLine (); GenerateGetNamedEntityValueMethod (output); output.WriteLine ("\t}"); output.WriteLine ("}"); } }
static void GenerateSwitchLabels (GraphNode node) { foreach (var child in node.Children) { SortedDictionary<int, InnerSwitchLabel> states; InnerSwitchLabel inner; if (!OuterSwitchLabels.TryGetValue (child.Char, out states)) OuterSwitchLabels[child.Char] = states = new SortedDictionary<int, InnerSwitchLabel> (); if (!states.TryGetValue (node.State, out inner)) states[node.State] = new InnerSwitchLabel (node.State, child.State, string.Format ("{0} -> {1}", node.Name, child.Name)); GenerateSwitchLabels (child); } }
public static void Main(string[] args) { int maxEntityLength = 0; int state = 0; using (var json = new JsonTextReader(new StreamReader("HtmlEntities.json"))) { while (json.Read()) { string name, value; if (json.TokenType == JsonToken.StartObject) { continue; } if (json.TokenType != JsonToken.PropertyName) { break; } name = (string)json.Value; // trim leading '&' name = name.Substring(1); if (!json.Read() || json.TokenType != JsonToken.StartObject) { break; } // read to the "codepoints" property if (!json.Read() || json.TokenType != JsonToken.PropertyName) { break; } // skip the array of integers... if (!json.Read() || json.TokenType != JsonToken.StartArray) { break; } while (json.Read()) { if (json.TokenType == JsonToken.EndArray) { break; } } // the property should be "characters" - this is what we want if (!json.Read() || json.TokenType != JsonToken.PropertyName) { break; } value = json.ReadAsString(); var node = Root; for (int i = 0; i < name.Length; i++) { bool found = false; for (int j = 0; j < node.Children.Count; j++) { if (node.Children[j].Char == name[i]) { node = node.Children[j]; found = true; break; } } if (!found) { node = new GraphNode(node, ++state, name[i]); continue; } } if (node.Value == null) { FinalStates.Add(node.State, node); node.Value = value; } maxEntityLength = Math.Max(maxEntityLength, name.Length + 1); if (!json.Read() || json.TokenType != JsonToken.EndObject) { break; } } } GenerateSwitchLabels(Root); using (var output = new StreamWriter("HtmlEntityDecoder.g.cs")) { output.WriteLine("// WARNING: This file is auto-generated. DO NOT EDIT!"); output.WriteLine(); output.WriteLine("using System.Collections.Generic;"); output.WriteLine(); output.WriteLine("namespace HtmlKit {"); output.WriteLine("\tpublic partial class HtmlEntityDecoder"); output.WriteLine("\t{"); output.WriteLine("\t\tconst int MaxEntityLength = {0};", maxEntityLength); output.WriteLine(); GenerateStaticConstructor(output); output.WriteLine(); GenerateBinarySearchMethod(output); output.WriteLine(); GeneratePushNamedEntityMethod(output); output.WriteLine(); GenerateGetNamedEntityValueMethod(output); output.WriteLine("\t}"); output.WriteLine("}"); } }