예제 #1
0
 static void _ParseIncludes(LexContext pc, IList <string> result)
 {
     pc.TrySkipCCommentsAndWhiteSpace();
     while ('@' == pc.Current)
     {
         pc.Advance();
         var s = XbnfNode.ParseIdentifier(pc);
         if ("include" == s)
         {
             pc.TrySkipCCommentsAndWhiteSpace();
             var lit = XbnfExpression.Parse(pc) as XbnfLiteralExpression;
             if (!result.Contains(lit.Value))
             {
                 result.Add(lit.Value);
             }
             pc.TryReadCCommentsAndWhitespace();
             pc.Advance();
             pc.TryReadCCommentsAndWhitespace();
         }
         else
         {
             while (-1 != pc.Current && ';' != pc.Current)
             {
                 pc.Advance();
             }
             if (';' == pc.Current)
             {
                 pc.Advance();
             }
             pc.TrySkipCCommentsAndWhiteSpace();
         }
     }
 }
예제 #2
0
        static XbnfImport _ParseIncludePart(XbnfDocument doc, LexContext pc)
        {
            pc.TrySkipCCommentsAndWhiteSpace();
            var l = pc.Line;
            var c = pc.Column;
            var p = pc.Position;

            pc.Expecting('\"');
            // borrow the parsing from XbnfExpression for this.
            var le = XbnfExpression.Parse(pc) as XbnfLiteralExpression;

            if (null == le)
            {
                throw new ExpectingException("Expecting string literal include argument", l, c, p, pc.FileOrUrl, "string literal");
            }
            var res = le.Value;

            pc.TrySkipCCommentsAndWhiteSpace();
            pc.Expecting(';');
            pc.Advance();
            var cmp    = res.ToLowerInvariant();
            var result = new XbnfImport();

            if (-1 < cmp.IndexOf("://"))
            {
                result.Document = XbnfDocument.ReadFromUrl(cmp);
            }
            else
            {
                string mdir = null;
                if (null != doc && !string.IsNullOrEmpty(doc.FileOrUrl))
                {
                    mdir = doc.FileOrUrl;
                    if (!Path.IsPathRooted(mdir))
                    {
                        mdir = Path.GetFullPath(mdir);
                    }
                    mdir = Path.GetDirectoryName(mdir);
                }
                var path = res;
                if (!Path.IsPathRooted(path))
                {
                    if (null != mdir)
                    {
                        path = Path.Combine(mdir, path);
                    }
                    else
                    {
                        path = Path.GetFullPath(path);
                    }
                }
                result.Document = XbnfDocument.ReadFrom(path);
            }
            result.SetLocation(l, c, p);
            return(result);
        }
예제 #3
0
        internal static XbnfProduction Parse(LexContext pc)
        {
            var result = new XbnfProduction();

            pc.TrySkipCCommentsAndWhiteSpace();
            var l = pc.Line;
            var c = pc.Column;
            var p = pc.Position;

            // 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('=');

            pc.Advance();
            result.Expression = XbnfExpression.Parse(pc);

            pc.Expecting(';', '=');
            result.SetLocation(l, c, p);
            if (';' == pc.Current)
            {
                pc.Advance();
                return(result);
            }
            if ('=' == pc.Current)
            {
                pc.Advance();
                pc.Expecting('>');
                pc.Advance();
                pc.TrySkipCCommentsAndWhiteSpace();
                pc.Expecting('{');
                pc.Advance();
                l = pc.Line;
                c = pc.Column;
                p = pc.Position;
                var s = XbnfDocument.ReadCode(pc);
                pc.Expecting('}');
                pc.Advance();
                result.Action = new XbnfCode(s);
                result.SetLocation(l, c, p);
            }

            return(result);
        }