internal static ISemanticNode And(BranchParseNode branch, Func <BranchParseNode, ISemanticNode> recurse) { branch = branch.Unwrap(); ISemanticNode left = recurse(branch.GetDescendant(0)); ISemanticNode right = recurse(branch.GetDescendant(1)); return(new BranchSemanticNode((int)EbnfNodeType.And, left, right)); }
internal static ISemanticNode Regex(BranchParseNode branch, Func <BranchParseNode, ISemanticNode> recurse) { branch = branch.Unwrap(); var text = branch.Leaf.MatchedText; text = text .Substring(1, text.Length - 2) .Replace(@"\\", @"\") .Replace(@"\/", @"/"); var startIndex = branch.Leaf.StartIndex; return(new LeafSemanticNode((int)EbnfNodeType.Regex, startIndex, text)); }
static ISemanticNode DefaultAction(BranchParseNode branch, Func <BranchParseNode, ISemanticNode> recurse) { if (branch.Rule is NamedRule rule) { throw new Exception($"No action specified for named rule: {rule.Name}"); } branch = branch.Unwrap(); if (branch.Rule is NamedRule) { return(recurse(branch)); } var children = branch.Elements .Select(n => { if (n.Rule is NamedRule) { return(recurse(n)); } else { return(DefaultAction(n, recurse)); } }) .Where(c => c != null) .ToList(); if (children.Count == 1) { return(children[0]); } else { return(new BranchSemanticNode(0, branch.StartIndex, children)); } }