public override ParseGraphNode BuildParseGraph(RuntimeState state) { ParseGraphNode parseGraph = body.BuildParseGraph(state); if (options.Count == 0) return parseGraph; OptionsNode optionsNode = new OptionsNode(Source, parseGraph); foreach (Option option in options) { string optionKey = option.optionKey.name; object optionValue; if (option.optionValue == null) optionValue = true; else optionValue = option.optionValue.Get(state); switch (optionKey) { case "buildTextNodes": optionsNode.BuildTextNodes.Value = ConvertNode.ToBool(optionValue); break; case "dropPrecedence": optionsNode.DropPrecedence.Value = ConvertNode.ToBool(optionValue); break; case "recursive": { if (ConvertNode.ToBool(optionValue)) optionsNode.RecursionBehaviour.Value = RecursionBehaviour.Recursive; else optionsNode.RecursionBehaviour.Value = RecursionBehaviour.None; } break; case "leftRecursive": optionsNode.RecursionBehaviour.Value = RecursionBehaviour.LeftRecursive; break; case "rightRecursive": optionsNode.RecursionBehaviour.Value = RecursionBehaviour.RightRecursive; break; case "whitespace": optionsNode.Whitespace.Value = Pattern.PatternForType((Type) optionValue); break; case "exclude": optionsNode.Exclude.Value = Pattern.PatternForType((Type) optionValue); break; } } return optionsNode; }
public ConcretePattern(Source source, string name, ParseGraphNode parseGraph) : base(source, name) { SetParseGraph(parseGraph); recursionBehaviour = RecursionBehaviour.Recursive; OptionsNode options = ParseGraph as OptionsNode; while (options != null) { if (options.RecursionBehaviour.HasValue) { recursionBehaviour = options.RecursionBehaviour.Value; break; } options = options.Body as OptionsNode; } recurseNode = new PatternNode(source, this, true); }
private ParseGraphNode ParseNode() { string token = Read(); if ((token == "s") || (token == "a")) { Read("("); List <ParseGraphNode> nodes = new List <ParseGraphNode>(); while (Peek() != ")") { nodes.Add(ParseNode()); } Read(")"); if (token == "s") { return(new SeqNode(null, nodes)); } else if (token == "a") { return(new AltNode(null, nodes, false)); } } else if ((token == "?") || (token == "*") || (token == "+")) { Read("("); ParseGraphNode body = ParseNode(); Read(")"); return(new RepNode(null, body, Reps.ForName(token[0]))); } else if (token == "&") { Read("("); ParseGraphNode body = ParseNode(); Read(")"); return(new AndNode(null, body)); } else if (token == "!") { Read("("); ParseGraphNode body = ParseNode(); Read(")"); return(new NotNode(null, body)); } else if (token == "r") { Read("("); char min = TextEscape.Unquote(Read())[0]; char max = TextEscape.Unquote(Read())[0]; Read(")"); return(new CharNode(null, new CharRange(min, max))); } else if (token == "l") { Read("("); string label = Read(); ParseGraphNode body = ParseNode(); Read(")"); return(new LabelNode(null, label, body)); } else if (token == "t") { Read("("); ParseGraphNode body = ParseNode(); Read(")"); return(new TokenNode(null, body)); } else if (token == "o") { Read("("); Dictionary <string, object> options = new Dictionary <string, object>(); while (Peek() == "(") { Read(); string optionName = Read(); object optionValue = ReadValue(); options[optionName] = optionValue; Read(")"); } ParseGraphNode body = ParseNode(); Read(")"); OptionsNode optionsNode = new OptionsNode(null, body); foreach (string key in options.Keys) { switch (key) { case "buildTextNodes": optionsNode.BuildTextNodes.Value = (bool)options[key]; break; case "dropPrecedence": optionsNode.DropPrecedence.Value = (bool)options[key]; break; case "recursive": { if ((bool)options[key]) { optionsNode.RecursionBehaviour.Value = RecursionBehaviour.Recursive; } else { optionsNode.RecursionBehaviour.Value = RecursionBehaviour.None; } } break; case "leftRecursive": optionsNode.RecursionBehaviour.Value = RecursionBehaviour.LeftRecursive; break; case "rightRecursive": optionsNode.RecursionBehaviour.Value = RecursionBehaviour.RightRecursive; break; case "whitespace": { object value = options[key]; if (value == null) { optionsNode.Whitespace.Value = null; } else { optionsNode.Whitespace.Value = Pattern.PatternForType((Type)value); } } break; case "exclude": optionsNode.Exclude.Value = Pattern.PatternForType((Type)options[key]); break; } } return(optionsNode); } else if (token == "any") { return(new AnyNode(null)); } else if ((token[0] >= '0') && (token[0] <= '9')) { return(new PatternNode(null, patterns[Convert.ToInt32(token)], false)); } else if (token[0] == '\'') { string text = TextEscape.Unquote(token); if (text.Length == 1) { return(new CharNode(null, text[0])); } else { return(new TextNode(null, text)); } } throw new Exception(token); }
private void AddNode(TreeIter parent, OptionsNode optionsNode) { parent = store.AppendValues(parent, "options", optionsNode); Dictionary<string, object> options = new Dictionary<string, object>(); if (optionsNode.BuildTextNodes.HasValue) options["buildTextNodes"] = optionsNode.BuildTextNodes.Value; if (optionsNode.RecursionBehaviour.HasValue) options["recursionBehaviour"] = optionsNode.RecursionBehaviour.Value; if (optionsNode.DropPrecedence.HasValue) options["dropPrecedence"] = optionsNode.DropPrecedence.Value; if (optionsNode.Whitespace.HasValue) options["whitespace"] = optionsNode.Whitespace.Value; if (optionsNode.Exclude.HasValue) options["exclude"] = optionsNode.Exclude.Value; foreach (KeyValuePair<string, object> option in options) store.AppendValues(parent, option.Key + " = " + ObjectViewerDirectory.GetDescription(option.Value), optionsNode); AddNode(parent, optionsNode.Body); }
private ParseGraphNode ParseNode() { string token = Read(); if ((token == "s") || (token == "a")) { Read("("); List<ParseGraphNode> nodes = new List<ParseGraphNode>(); while (Peek() != ")") nodes.Add(ParseNode()); Read(")"); if (token == "s") return new SeqNode(null, nodes); else if (token == "a") return new AltNode(null, nodes, false); } else if ((token == "?") || (token == "*") || (token == "+")) { Read("("); ParseGraphNode body = ParseNode(); Read(")"); return new RepNode(null, body, Reps.ForName(token[0])); } else if (token == "&") { Read("("); ParseGraphNode body = ParseNode(); Read(")"); return new AndNode(null, body); } else if (token == "!") { Read("("); ParseGraphNode body = ParseNode(); Read(")"); return new NotNode(null, body); } else if (token == "r") { Read("("); char min = TextEscape.Unquote(Read())[0]; char max = TextEscape.Unquote(Read())[0]; Read(")"); return new CharNode(null, new CharRange(min, max)); } else if (token == "l") { Read("("); string label = Read(); ParseGraphNode body = ParseNode(); Read(")"); return new LabelNode(null, label, body); } else if (token == "t") { Read("("); ParseGraphNode body = ParseNode(); Read(")"); return new TokenNode(null, body); } else if (token == "o") { Read("("); Dictionary<string, object> options = new Dictionary<string, object>(); while (Peek() == "(") { Read(); string optionName = Read(); object optionValue = ReadValue(); options[optionName] = optionValue; Read(")"); } ParseGraphNode body = ParseNode(); Read(")"); OptionsNode optionsNode = new OptionsNode(null, body); foreach (string key in options.Keys) { switch (key) { case "buildTextNodes": optionsNode.BuildTextNodes.Value = (bool) options[key]; break; case "dropPrecedence": optionsNode.DropPrecedence.Value = (bool) options[key]; break; case "recursive": { if ((bool) options[key]) optionsNode.RecursionBehaviour.Value = RecursionBehaviour.Recursive; else optionsNode.RecursionBehaviour.Value = RecursionBehaviour.None; } break; case "leftRecursive": optionsNode.RecursionBehaviour.Value = RecursionBehaviour.LeftRecursive; break; case "rightRecursive": optionsNode.RecursionBehaviour.Value = RecursionBehaviour.RightRecursive; break; case "whitespace": { object value = options[key]; if (value == null) optionsNode.Whitespace.Value = null; else optionsNode.Whitespace.Value = Pattern.PatternForType((Type) value); } break; case "exclude": optionsNode.Exclude.Value = Pattern.PatternForType((Type) options[key]); break; } } return optionsNode; } else if (token == "any") { return new AnyNode(null); } else if ((token[0] >= '0') && (token[0] <= '9')) { return new PatternNode(null, patterns[Convert.ToInt32(token)], false); } else if (token[0] == '\'') { string text = TextEscape.Unquote(token); if (text.Length == 1) return new CharNode(null, text[0]); else return new TextNode(null, text); } throw new Exception(token); }