public override IList <IList <string> > ToDisjunctions(EbnfDocument parent, Cfg cfg)
        {
            var _listId = cfg.GetUniqueId("implicitlist");
            IDictionary <string, object> attrs = new Dictionary <string, object>();

            attrs.Add("collapse", true);
            cfg.AttributeSets.Add(_listId, attrs);
            var expr = new EbnfOrExpression(new EbnfOrExpression(new EbnfConcatExpression(Expression, new EbnfRefExpression(_listId)), Expression), null);

            foreach (var nt in expr.ToDisjunctions(parent, cfg))
            {
                CfgRule r = new CfgRule();
                r.Left = _listId;
                foreach (var s in nt)
                {
                    if (1 < r.Right.Count && null == s)
                    {
                        continue;
                    }
                    r.Right.Add(s);
                }
                if (!cfg.Rules.Contains(r))
                {
                    cfg.Rules.Add(r);
                }
            }
            return(new List <IList <string> >(new IList <string>[] { new List <string>(new string[] { _listId }) }));
        }
        public override IList <IList <string> > ToDisjunctions(EbnfDocument parent, Cfg cfg)
        {
            if (string.IsNullOrEmpty(Symbol))
            {
                throw new InvalidOperationException("The ref expression was nil.");
            }
            var l  = new List <IList <string> >();
            var ll = new List <string>();

            l.Add(ll);
            ll.Add(Symbol);
            return(l);
        }
 public override IList <IList <string> > ToDisjunctions(EbnfDocument parent, Cfg cfg)
 {
     foreach (var prod in parent.Productions)
     {
         if (Equals(prod.Value.Expression, this))
         {
             var l  = new List <IList <string> >();
             var ll = new List <string>();
             l.Add(ll);
             ll.Add(prod.Key);
             return(l);
         }
     }
     throw new InvalidOperationException("The terminal was not declared.");
 }
        public override IList <IList <string> > ToDisjunctions(EbnfDocument parent, Cfg cfg)
        {
            var l = new List <IList <string> >();

            if (null != Expression)
            {
                l.AddRange(Expression.ToDisjunctions(parent, cfg));
                var ll = new List <string>();
                if (!l.Contains(ll, OrderedCollectionEqualityComparer <string> .Default))
                {
                    l.Add(ll);
                }
            }
            return(l);
        }
Ejemplo n.º 5
0
        public override IList <IList <string> > ToDisjunctions(EbnfDocument parent, Cfg cfg)
        {
            var l = new List <IList <string> >();

            if (null == Right)
            {
                if (null == Left)
                {
                    return(l);
                }
                foreach (var ll in Left.ToDisjunctions(parent, cfg))
                {
                    l.Add(new List <string>(ll));
                }
                return(l);
            }
            else if (null == Left)
            {
                foreach (var ll in Right.ToDisjunctions(parent, cfg))
                {
                    l.Add(new List <string>(ll));
                }
                return(l);
            }
            foreach (var ll in Left.ToDisjunctions(parent, cfg))
            {
                foreach (var ll2 in Right.ToDisjunctions(parent, cfg))
                {
                    var ll3 = new List <string>();
                    ll3.AddRange(ll);
                    ll3.AddRange(ll2);
                    if (!l.Contains(ll3, OrderedCollectionEqualityComparer <string> .Default))
                    {
                        l.Add(ll3);
                    }
                }
            }
            return(l);
        }
Ejemplo n.º 6
0
        public int Generate(string wszInputFilePath, string bstrInputFileContents,
                            string wszDefaultNamespace, IntPtr[] rgbOutputFileContents,
                            out uint pcbOutput, IVsGeneratorProgress pGenerateProgress)
        {
            try
            {
                using (var stm = new MemoryStream())
                {
                    ReportProgress(pGenerateProgress, 1, 4);
                    EbnfDocument doc  = null;
                    var          msgs = new List <object>();
                    var          sw   = new StreamWriter(stm);

                    using (var sr = new StreamReader(wszInputFilePath))
                    {
                        try
                        {
                            doc = EbnfDocument.ReadFrom(sr);
                        }
                        catch (ExpectingException ex)
                        {
                            var em = string.Concat("Error parsing grammar: ", ex.Message);
                            if (!ReportMessage(pGenerateProgress, em, ex.Line, ex.Column, true))
                            {
                                msgs.Add(em);
                            }
                            WriteHeader(sw, wszInputFilePath, msgs);

                            goto done;
                        }
                        ReportProgress(pGenerateProgress, 2, 4);
                    }
                    var hasErrors = false;
                    foreach (var m in doc.Prepare(false))
                    {
                        if (!ReportMessage(pGenerateProgress, m))
                        {
                            msgs.Add(m);
                        }
                        if (EbnfErrorLevel.Error == m.ErrorLevel)
                        {
                            hasErrors = true;
                        }
                    }

                    if (hasErrors)
                    {
                        // make sure to dump the messages
                        WriteHeader(sw, wszInputFilePath, msgs);
                        goto done;
                    }
                    var name = string.Concat(Path.GetFileNameWithoutExtension(wszInputFilePath), "Parser");
                    var cfg  = doc.ToCfg();
                    foreach (var m in cfg.PrepareLL1(false))
                    {
                        if (!ReportMessage(pGenerateProgress, m))
                        {
                            msgs.Add(m);
                        }
                        if (CfgErrorLevel.Error == m.ErrorLevel)
                        {
                            hasErrors = true;
                        }
                    }
                    if (hasErrors)
                    {
                        WriteHeader(sw, wszInputFilePath, msgs);
                        goto done;
                    }
                    ReportProgress(pGenerateProgress, 3, 4);

                    var lexer = doc.ToLexer(cfg);
                    WriteHeader(sw, wszInputFilePath, msgs);
                    var hasNS = !string.IsNullOrEmpty(wszDefaultNamespace);
                    if (hasNS)
                    {
                        sw.WriteLine(string.Concat("namespace ", wszDefaultNamespace, " {"));
                    }

                    cfg.WriteCSharpTableDrivenLL1ParserClassTo(sw, name, null, lexer);
                    if (hasNS)
                    {
                        sw.WriteLine("}");
                    }
                    ReportProgress(pGenerateProgress, 4, 4);
done:

                    sw.Flush();
                    int length = (int)stm.Length;
                    rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(length);
                    Marshal.Copy(stm.GetBuffer(), 0, rgbOutputFileContents[0], length);
                    pcbOutput = (uint)length;
                }
            }
            catch (Exception ex)
            {
                string s      = string.Concat("/* ", ex.Message, " */");
                byte[] b      = Encoding.UTF8.GetBytes(s);
                int    length = b.Length;
                rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(length);
                Marshal.Copy(b, 0, rgbOutputFileContents[0], length);
                pcbOutput = (uint)length;
            }
            return(VSConstants.S_OK);
        }
Ejemplo n.º 7
0
 public abstract IList <IList <string> > ToDisjunctions(EbnfDocument parent, Cfg cfg);