Ejemplo n.º 1
0
        private void RenderTimer_Tick(object sender, EventArgs e)
        {
            RenderTimer.Enabled = false;
            CharFA <string> fa = null;

            try
            {
                var ast = RegexExpression.Parse(Regex.Text);
                fa = ast.ToFA("Accept");
            }
            catch (Exception ex)
            {
                Status.Text = string.Format("Error Parsing Regex: {0}", ex.Message);
            }
            if (null != fa)
            {
                // mark our states for displaying the DFA later
                foreach (var ffa in fa.FillClosure())
                {
                    ffa.Tag = ffa;
                }

                try
                {
                    var kvp = fa.Lex(Input.Text.GetEnumerator(), "#ERROR");
                    if (kvp.Value.Length != Input.Text.Length)
                    {
                        Status.Text = string.Format("Input error at {0}", kvp.Value.Length);
                    }
                    else
                    {
                        Status.Text = "Successfully Lexed.";
                    }
                }
                catch (Exception eex)
                {
                    Status.Text = string.Format("Input error: {0}", eex.Message);
                }
                var options = new CharFA <string> .DotGraphOptions();

                options.DebugString = Input.Text;
                var dfa = fa.ToDfa();
                dfa.TrimDuplicates();
                try
                {
                    using (var stm = fa.RenderToStream("jpg", false, options))
                        NfaGraph.Image = Image.FromStream(stm);
                } catch
                {
                }
                options.StatePrefix    = @"Q";
                options.DebugSourceNfa = fa;
                try
                {
                    using (var stm = dfa.RenderToStream("jpg", false, options))
                        DfaGraph.Image = Image.FromStream(stm);
                }
                catch { }
            }
        }
Ejemplo n.º 2
0
 public LexRule(string left, string right)
 {
     Left  = left;
     Right = RegexExpression.Parse(right);
 }
Ejemplo n.º 3
0
        internal static LexDocument Parse(ParseContext pc)
        {
            var result = new LexDocument();

            while (-1 != pc.Current)
            {
                var line     = pc.Line;
                var column   = pc.Column;
                var position = pc.Position;
                LexNode.SkipCommentsAndWhitespace(pc);
                while ('\n' == pc.Current)
                {
                    pc.Advance();
                    LexNode.SkipCommentsAndWhitespace(pc);
                }
                var id = LexNode.ParseIdentifier(pc);
                if (string.IsNullOrEmpty(id))
                {
                    pc.Advance();
                    LexNode.SkipCommentsAndWhitespace(pc);
                    continue;
                }
                LexNode.SkipCommentsAndWhitespace(pc);

                pc.Expecting(':', '-', '=');
                if (':' == pc.Current)                 // attribute set
                {
                    pc.Advance();
                    var d = new LexAttributeList();
                    while (-1 != pc.Current && '\n' != pc.Current)
                    {
                        var attr = LexAttribute.Parse(pc);
                        d.Add(attr);

                        LexNode.SkipCommentsAndWhitespace(pc);
                        pc.Expecting('\n', ',', -1);
                        if (',' == pc.Current)
                        {
                            pc.Advance();
                        }
                    }
                    result.AttributeSets.Add(id, d);
                    LexNode.SkipCommentsAndWhitespace(pc);
                }
                else if ('=' == pc.Current)
                {
                    pc.Advance();
                    LexNode.SkipCommentsAndWhitespace(pc);
                    pc.Expecting('\'');
                    pc.Advance();
                    var l = pc.CaptureBuffer.Length;
                    pc.TryReadUntil('\'', '\\', false);
                    pc.Expecting('\'');
                    pc.Advance();
                    var rx = pc.GetCapture(l);
                    // make sure to capture the line numbers properly:
                    var rpc = ParseContext.Create(rx);
                    rpc.EnsureStarted();
                    rpc.SetLocation(pc.Line, pc.Column, pc.Position);
                    var rule = new LexRule(id, RegexExpression.Parse(rpc));
                    rule.SetLocation(line, column, position);
                    result.Rules.Add(rule);
                }
                else if ('-' == pc.Current)
                {
                    pc.TrySkipUntil('\n', true);
                }
                LexNode.SkipCommentsAndWhitespace(pc);
                if ('\n' == pc.Current)
                {
                    pc.Advance();
                }
            }
            return(result);
        }