Click here to Skip to main content
15,886,842 members
Articles / Programming Languages / C#

WWW DSL using Irony - Part 1

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
1 Jun 2009CPOL4 min read 31.6K   236   30  
A Domain Specific Language for WWW operations created with Irony.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions