示例#1
0
        public LexDocument Clone()
        {
            var result = new LexDocument();

            foreach (var attrs in AttributeSets)
            {
                var d = new LexAttributeList();
                result.AttributeSets.Add(attrs.Key, d);
                foreach (var attr in attrs.Value)
                {
                    d.Add(attr.Clone());
                }
            }
            var ic = Rules.Count;

            for (var i = 0; i < ic; ++i)
            {
                result.Rules.Add(Rules[i].Clone());
            }
            return(result);
        }
示例#2
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);
        }