Пример #1
0
        static void _VisitFetchTerminals(XbnfExpression expr, HashSet <XbnfExpression> terms)
        {
            var l = expr as XbnfLiteralExpression;

            if (null != l)
            {
                terms.Add(l);
                return;
            }
            var r = expr as XbnfRegexExpression;

            if (null != r)
            {
                terms.Add(r);
                return;
            }
            var u = expr as XbnfUnaryExpression;

            if (null != u)
            {
                _VisitFetchTerminals(u.Expression, terms);
                return;
            }
            var b = expr as XbnfBinaryExpression;

            if (null != b)
            {
                _VisitFetchTerminals(b.Left, terms);
                _VisitFetchTerminals(b.Right, terms);
                return;
            }
        }
Пример #2
0
        internal static XbnfProduction Parse(ParseContext pc)
        {
            var result = new XbnfProduction();

            pc.TrySkipCCommentsAndWhiteSpace();
            // read identifier
            result.Name = ParseIdentifier(pc);
            // read attributes
            if ('<' == pc.Current)
            {
                pc.Advance();
                while (-1 != pc.Current && '>' != pc.Current)
                {
                    result.Attributes.Add(XbnfAttribute.Parse(pc));
                    pc.TrySkipCCommentsAndWhiteSpace();
                    pc.Expecting('>', ',');
                    if (',' == pc.Current)
                    {
                        pc.Advance();
                    }
                }
                pc.Expecting('>');
                pc.Advance();
            }
            pc.TrySkipCCommentsAndWhiteSpace();
            pc.Expecting(';', '=');
            if ('=' == pc.Current)
            {
                pc.Advance();
                result.Expression = XbnfExpression.Parse(pc);
            }
            pc.Expecting(';');
            pc.Advance();
            return(result);
        }
Пример #3
0
 static XbnfProduction _GetProductionForExpression(XbnfDocument d, XbnfExpression e)
 {
     for (int ic = d.Productions.Count, i = 0; i < ic; ++i)
     {
         var prod = d.Productions[i];
         if (e == prod.Expression)
         {
             return(prod);
         }
     }
     return(null);
 }
Пример #4
0
        static string _ToRegex(XbnfDocument d, XbnfExpression e)
        {
            var le = e as XbnfLiteralExpression;

            if (null != le)
            {
                return(_EscapeLiteral(XbnfNode.Escape(le.Value)));
            }
            var rxe = e as XbnfRegexExpression;

            if (null != rxe)
            {
                return(string.Concat("(", rxe.Value, ")"));
            }
            var rfe = e as XbnfRefExpression;

            if (null != rfe)
            {
                _ToRegex(d, d.Productions[rfe.Symbol].Expression);
            }
            var re = e as XbnfRepeatExpression;

            if (null != re)
            {
                if (re.IsOptional)
                {
                    return(string.Concat("(", _ToRegex(d, re.Expression), ")*"));
                }
                else
                {
                    return(string.Concat("(", _ToRegex(d, re.Expression), ")+"));
                }
            }
            var oe = e as XbnfOrExpression;

            if (null != oe)
            {
                return(string.Concat("(", _ToRegex(d, oe.Left), "|", _ToRegex(d, oe.Right), ")"));
            }
            var oc = e as XbnfConcatExpression;

            if (null != oc)
            {
                return(string.Concat(_ToRegex(d, oe.Left), _ToRegex(d, oe.Right)));
            }
            var ope = e as XbnfOptionalExpression;

            if (null != ope)
            {
                return(string.Concat("(", _ToRegex(d, ope.Expression), ")?"));
            }
            return("");
        }
Пример #5
0
 public XbnfConcatExpression(XbnfExpression left, params XbnfExpression[] right)
 {
     Left = left;
     for (int i = 0; i < right.Length; i++)
     {
         var r = right[i];
         if (null == Right)
         {
             Right = r;
         }
         else
         {
             var c = new XbnfConcatExpression();
             c.Left  = Left;
             c.Right = Right;
             Right   = null;
             Left    = c;
         }
     }
 }
Пример #6
0
 public XbnfProduction(string name, XbnfExpression expression = null)
 {
     Name       = name;
     Expression = expression;
 }
Пример #7
0
        internal static XbnfExpression Parse(ParseContext pc)
        {
            XbnfExpression current = null;
            XbnfExpression e;
            long           position;
            int            line;
            int            column;

            pc.TrySkipCCommentsAndWhiteSpace();
            position = pc.Position; line = pc.Line; column = pc.Column;
            while (-1 != pc.Current && ']' != pc.Current && ')' != pc.Current && '}' != pc.Current && ';' != pc.Current)
            {
                pc.TrySkipCCommentsAndWhiteSpace();
                position = pc.Position; line = pc.Line; column = pc.Column;
                switch (pc.Current)
                {
                case '|':
                    pc.Advance();
                    position = pc.Position; line = pc.Line; column = pc.Column;
                    current  = new XbnfOrExpression(current, Parse(pc));
                    current.SetLocation(line, column, position);
                    break;

                case '(':
                    pc.Advance();
                    position = pc.Position; line = pc.Line; column = pc.Column;
                    e        = Parse(pc);
                    current.SetLocation(line, column, position);
                    pc.Expecting(')');
                    pc.Advance();
                    e.SetLocation(line, column, position);
                    if (null == current)
                    {
                        current = e;
                    }
                    else
                    {
                        current = new XbnfConcatExpression(current, e);
                    }

                    break;

                case '[':
                    pc.Advance();
                    e = new XbnfOptionalExpression(Parse(pc));
                    e.SetLocation(line, column, position);
                    pc.TrySkipCCommentsAndWhiteSpace();
                    pc.Expecting(']');
                    pc.Advance();
                    if (null == current)
                    {
                        current = e;
                    }
                    else
                    {
                        current = new XbnfConcatExpression(current, e);
                    }

                    break;

                case '{':
                    pc.Advance();
                    e = new XbnfRepeatExpression(Parse(pc));
                    e.SetLocation(line, column, position);
                    pc.TrySkipCCommentsAndWhiteSpace();
                    pc.Expecting('}');
                    pc.Advance();
                    if ('+' == pc.Current)
                    {
                        pc.Advance();
                        ((XbnfRepeatExpression)e).IsOptional = false;
                    }
                    if (null == current)
                    {
                        current = e;
                    }
                    else
                    {
                        current = new XbnfConcatExpression(current, e);
                    }

                    break;

                case '\"':
                    e = new XbnfLiteralExpression(pc.ParseJsonString());
                    if (null == current)
                    {
                        current = e;
                    }
                    else
                    {
                        current = new XbnfConcatExpression(current, e);
                    }
                    e.SetLocation(line, column, position);
                    break;

                case '\'':
                    pc.Advance();
                    pc.ClearCapture();
                    pc.TryReadUntil('\'', '\\', false);
                    pc.Expecting('\'');
                    pc.Advance();
                    e = new XbnfRegexExpression(pc.GetCapture());
                    if (null == current)
                    {
                        current = e;
                    }
                    else
                    {
                        current = new XbnfConcatExpression(current, e);
                    }
                    e.SetLocation(line, column, position);
                    break;

                case ';':
                case ']':
                case ')':
                case '}':
                    return(current);

                default:
                    e = new XbnfRefExpression(ParseIdentifier(pc));
                    if (null == current)
                    {
                        current = e;
                    }
                    else
                    {
                        current = new XbnfConcatExpression(current, e);
                    }
                    e.SetLocation(line, column, position);
                    break;
                }
            }
            pc.TrySkipCCommentsAndWhiteSpace();
            return(current);
        }
Пример #8
0
        static IList <IList <string> > _GetDysjunctions(
            XbnfDocument d,
            ICollection <string> syms,
            IDictionary <XbnfExpression, string> tmap,
            IDictionary <string, XbnfAttributeList> attrs,
            IList <KeyValuePair <string, IList <string> > > rules,
            XbnfProduction p,
            XbnfExpression e
            )
        {
            var le = e as XbnfLiteralExpression;

            if (null != le)
            {
                var res = new List <IList <string> >();
                var l   = new List <string>();
                l.Add(tmap[le]);
                res.Add(l);
                return(res);
            }
            var rxe = e as XbnfRegexExpression;

            if (null != rxe)
            {
                var res = new List <IList <string> >();
                var l   = new List <string>();
                l.Add(tmap[rxe]);
                res.Add(l);
                return(res);
            }
            var rfe = e as XbnfRefExpression;

            if (null != rfe)
            {
                var res = new List <IList <string> >();
                var l   = new List <string>();
                l.Add(rfe.Symbol);
                res.Add(l);
                return(res);
            }
            var ce = e as XbnfConcatExpression;

            if (null != ce)
            {
                return(_GetDysConcat(d, syms, tmap, attrs, rules, p, ce));
            }

            var oe = e as XbnfOrExpression;

            if (null != oe)
            {
                return(_GetDysOr(d, syms, tmap, attrs, rules, p, oe));
            }
            var ope = e as XbnfOptionalExpression;

            if (null != ope)
            {
                return(_GetDysOptional(d, syms, tmap, attrs, rules, p, ope));
            }
            var re = e as XbnfRepeatExpression;

            if (null != re)
            {
                return(_GetDysRepeat(d, syms, tmap, attrs, rules, p, re));
            }
            throw new NotSupportedException("The specified expression type is not supported.");
        }
Пример #9
0
 public XbnfOptionalExpression(XbnfExpression expression)
 {
     Expression = expression;
 }
Пример #10
0
 public XbnfRepeatExpression(XbnfExpression expression, bool isOptional = true)
 {
     Expression = expression;
     IsOptional = isOptional;
 }