Exemple #1
0
        public IList <EbnfMessage> Prepare(bool throwIfErrors = true)
        {
            var result = new List <EbnfMessage>();
            var msgs   = Validate(false);

            result.AddRange(msgs);
            var hasError = false;

            foreach (var msg in msgs)
            {
                if (EbnfErrorLevel.Error == msg.ErrorLevel)
                {
                    hasError = true;
                    break;
                }
            }
            if (!hasError)
            {
                result.AddRange(DeclareImplicitTerminals());
            }

            if (throwIfErrors)
            {
                EbnfException.ThrowIfErrors(result);
            }
            return(result);
        }
Exemple #2
0
        public IList <EbnfMessage> Validate(bool throwIfErrors = false)
        {
            var result    = new List <EbnfMessage>();
            var refCounts = new Dictionary <string, int>(EqualityComparer <string> .Default);

            foreach (var prod in Productions)
            {
                refCounts.Add(prod.Key, 0);
            }
            foreach (var prod in Productions)
            {
                _ValidateExpression(prod.Value.Expression, refCounts, result);
            }
            foreach (var rc in refCounts)
            {
                if (0 == rc.Value)
                {
                    var    prod = Productions[rc.Key];
                    object o;
                    var    isHidden = prod.Attributes.TryGetValue("hidden", out o) && o is bool && (bool)o;
                    if (!isHidden && !Equals(rc.Key, StartProduction))
                    {
                        result.Add(new EbnfMessage(EbnfErrorLevel.Warning, 2, string.Concat("Unreferenced production \"", rc.Key, "\""),
                                                   prod.Line, prod.Column, prod.Position));
                    }
                }
            }
            if (throwIfErrors)
            {
                EbnfException.ThrowIfErrors(result);
            }
            return(result);
        }