|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Irony.Compiler;
using Irony.Runtime;
using System.Text.RegularExpressions;
using Irony.Compiler.AST;
namespace WWWDSL
{
public class CaseNode : AstNode
{
private AstNode regexNode_;
private AstNode successNode_;
public CaseNode(NodeArgs args, AstNode regexNode, AstNode successNode)
: base(args)
{
ChildNodes.Clear();
regexNode_ = regexNode;
AddChild("regex", regexNode_);
successNode_ = successNode;
if (successNode_.IsEmpty())
successNode_ = null;
AddChild("success", successNode_);
}
protected override void DoEvaluate(EvaluationContext context)
{
regexNode_.Evaluate(context);
Regex r = new Regex(((string)context.CurrentResult).Trim(), RegexOptions.Singleline);
Match m = r.Match(((SwitchNode)Parent.Parent).Value);
if (m.Success)
{
Regex rv = new Regex("[?]<(?<groupName>\\w+)>", RegexOptions.Singleline);
MatchCollection mv = rv.Matches(r.ToString());
foreach (Match match in mv)
{
string varName = match.Groups["groupName"].Value;
if (Scope.Slots.Keys.Contains(varName))
context.CurrentFrame.Locals[Scope.Slots[varName].Index] = m.Groups[varName].Value;
}
if (successNode_ != null) successNode_.Evaluate(context);
}
context.CurrentResult = m.Success;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.