protected override Range <UInt32> VisitAlternation(Alternation <Char> alternation, LengthCalculatorOptions argument)
            {
                IEnumerable <Range <UInt32> > ranges =
                    alternation.GrammarNodes.Select(node => this.Visit(node, argument));

                return(new Range <UInt32>(ranges.Min(r => r.Start), ranges.Max(r => r.End)));
            }
Example #2
0
        /// <summary>
        /// Folds an alternation node.
        /// </summary>
        /// <param name="alternation"></param>
        /// <param name="argument">The argument to be passed to the visitor method.</param>
        /// <returns>
        /// <list type="number">
        /// <item>The original node if it's to be kept</item>
        /// <item>A different node to replace the original node with</item>
        /// <item>Null if the node is to be removed</item>
        /// </list>
        /// </returns>
        protected override GrammarNode <Char>?VisitAlternation(Alternation <Char> alternation, TArgument argument)
        {
            if (alternation is null)
            {
                throw new ArgumentNullException(nameof(alternation));
            }

            GrammarNode <Char>[] nodes = alternation.GrammarNodes.Select(node => this.Visit(node, argument))
                                         .Where(node => node != null)
                                         .Select(node => node !)
                                         .ToArray();
            if (nodes.Length == 0)
            {
                return(null);
            }
            else if (nodes.Length == 1)
            {
                return(nodes[0]);
            }
            else if (nodes.SequenceEqual(alternation.GrammarNodes))
            {
                return(alternation);
            }
            else
            {
                return(new Alternation <Char>(nodes));
            }
        }
Example #3
0
        public void Initialize()
        {
            var upperCaseValueRangeLexer = ValueRange.Create('\x41', '\x5A');
            var lowerCaseValueRangeLexer = ValueRange.Create('\x61', '\x7A');

            InnerLexer = Alternation.Create(upperCaseValueRangeLexer, lowerCaseValueRangeLexer);
        }
Example #4
0
        public override ILexer <IPvFuture> Create()
        {
            // "v"
            var v = Terminal.Create(@"v", StringComparer.OrdinalIgnoreCase);

            // "."
            var dot = Terminal.Create(@".", StringComparer.Ordinal);

            // ":"
            var colon = Terminal.Create(@":", StringComparer.Ordinal);

            // 1*HEXDIG
            var hexadecimalDigitLexer = HexadecimalDigitLexerFactory.Create();
            var r = Repetition.Create(hexadecimalDigitLexer, 1, int.MaxValue);

            // unreserved / sub-delims / ":"
            var unreservedLexer             = UnreservedLexerFactory.Create();
            var subcomponentsDelimiterLexer = SubcomponentsDelimiterLexerFactory.Create();
            var a = Alternation.Create(unreservedLexer, subcomponentsDelimiterLexer, colon);

            // 1*( unreserved / sub-delims / ":" )
            var s = Repetition.Create(a, 1, int.MaxValue);

            // "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
            var innerLexer = Concatenation.Create(v, r, dot, s);

            // IPvFuture
            return(new IPvFutureLexer(innerLexer));
        }
Example #5
0
        /// <summary>
        /// Recursive function for finding sequence of Terminals and NonTerminals by rule. Result is IEBNFItem which containst sequence
        /// </summary>
        private IEBNFItem GetEBNFItem(string rule, List <NonTerminal> listOfExistedTerminals, string endNotation = null)
        {
            IEBNFItem result           = null;
            var       left             = GetStartEBNFItem(rule, listOfExistedTerminals);
            var       lengthOfLeftRule = left.Rebuild().Length;
            var       restOfRule       = rule.Substring(lengthOfLeftRule, rule.Length - lengthOfLeftRule);

            if (string.IsNullOrEmpty(restOfRule))
            {
                throw new GrammarParseException("Can't find IEBNFItem, rest of rule is null or empty. Check termination charatcter.");
            }
            var firstChar = restOfRule[0].ToString();

            if (!string.IsNullOrEmpty(endNotation) && firstChar.Equals(endNotation))
            {
                result = left;
            }
            else if (IsTermination(firstChar))
            {
                result = left;
            }
            else
            {
                var newRule = restOfRule.Substring(1, restOfRule.Length - 1);
                var right   = GetEBNFItem(newRule, listOfExistedTerminals, endNotation);
                switch (firstChar)
                {
                case Alternation.notation: result = new Alternation(left, right); break;

                case Concatenation.notation: result = new Concatenation(left, right, this._cacheLength); break;
                }
            }
            return(result);
        }
Example #6
0
        public void Initialize()
        {
            var wsp  = Grammar.Rule("WSP");
            var crlf = Grammar.Rule("CRLF");

            InnerLexer = Repetition.Create(Alternation.Create(wsp, Concatenation.Create(crlf, wsp)), 0, int.MaxValue);
        }
        public override ILexer <Reserved> Create()
        {
            var innerLexer = Alternation.Create(
                GenericDelimiterLexerFactory.Create(),
                SubcomponentsDelimiterLexerFactory.Create());

            return(new ReservedLexer(innerLexer));
        }
        public override ILexer <UriReference> Create()
        {
            var innerLexer = Alternation.Create(
                UriLexerFactory.Create(),
                RelativeReferenceLexerFactory.Create());

            return(new UriReferenceLexer(innerLexer));
        }
Example #9
0
        public override ILexer <ChunkExtensionValue> Create()
        {
            var innerLexer = Alternation.Create(
                Token.Create(),
                QuotedString.Create());

            return(new ChunkExtensionValueLexer(innerLexer));
        }
        public override ILexer <FieldVisibleCharacter> Create()
        {
            var innerLexer = Alternation.Create(
                VisibleCharacterLexerFactory.Create(),
                ObsoleteTextLexerFactory.Create());

            return(new FieldVisibleCharacterLexer(innerLexer));
        }
Example #11
0
        public override ILexer <Host> Create()
        {
            var innerLexer = Alternation.Create(
                IPLiteralLexerFactory.Create(),
                IPv4AddressLexerFactory.Create(),
                RegisteredNameLexerFactory.Create());

            return(new HostLexer(innerLexer));
        }
Example #12
0
        public override ILexer <RequiredWhiteSpace> Create()
        {
            var innerLexer = Repetition.Create(
                Alternation.Create(SpaceFactory.Create(), HorizontalTabLexerFactory.Create()),
                1,
                int.MaxValue);

            return(new RequiredWhiteSpaceLexer(innerLexer));
        }
        public override ILexer <IPLiteral> Create()
        {
            var a          = Terminal.Create(@"[", StringComparer.Ordinal);
            var b          = Terminal.Create(@"]", StringComparer.Ordinal);
            var alt        = Alternation.Create(IPv6AddressLexerFactory.Create(), IPvFutureLexerFactory.Create());
            var innerLexer = Concatenation.Create(a, alt, b);

            return(new IPLiteralLexer(innerLexer));
        }
Example #14
0
        internal static IEnumerable <PatternNode <Word, ShapeNode> > DeepCloneExceptBoundaries(this IEnumerable <PatternNode <Word, ShapeNode> > nodes)
        {
            foreach (PatternNode <Word, ShapeNode> node in nodes)
            {
                var constraint = node as Constraint <Word, ShapeNode>;
                if (constraint != null && (constraint.FeatureStruct.IsEmpty || constraint.Type() != HCFeatureSystem.Boundary))
                {
                    yield return(constraint.Clone());

                    continue;
                }

                var alternation = node as Alternation <Word, ShapeNode>;
                if (alternation != null)
                {
                    var newAlteration = new Alternation <Word, ShapeNode>(alternation.Children.DeepCloneExceptBoundaries());
                    if (newAlteration.Children.Count > 0)
                    {
                        yield return(newAlteration);
                    }
                    continue;
                }

                var group = node as Group <Word, ShapeNode>;
                if (group != null)
                {
                    var newGroup = new Group <Word, ShapeNode>(group.Name, group.Children.DeepCloneExceptBoundaries());
                    if (newGroup.Children.Count > 0)
                    {
                        yield return(newGroup);
                    }
                    continue;
                }

                var quantifier = node as Quantifier <Word, ShapeNode>;
                if (quantifier != null)
                {
                    var newQuantifier = new Quantifier <Word, ShapeNode>(quantifier.MinOccur, quantifier.MaxOccur, quantifier.Children.DeepCloneExceptBoundaries().SingleOrDefault());
                    if (newQuantifier.Children.Count > 0)
                    {
                        yield return(newQuantifier);
                    }
                    continue;
                }

                var pattern = node as Pattern <Word, ShapeNode>;
                if (pattern != null)
                {
                    var newPattern = new Pattern <Word, ShapeNode>(pattern.Name, pattern.Children.DeepCloneExceptBoundaries());
                    if (newPattern.Children.Count > 0)
                    {
                        yield return(newPattern);
                    }
                }
            }
        }
Example #15
0
        public override ILexer <Query> Create()
        {
            var alternationLexer = Alternation.Create(
                PathCharacterLexerFactory.Create(),
                Terminal.Create(@"/", StringComparer.Ordinal),
                Terminal.Create(@"?", StringComparer.Ordinal));
            var fragmentRepetitionLexer = Repetition.Create(alternationLexer, 0, int.MaxValue);

            return(new QueryLexer(fragmentRepetitionLexer));
        }
Example #16
0
            protected override Int32 VisitAlternation(Alternation <Char> alternation, Unit argument)
            {
                var hash = new HashCode();

                foreach (GrammarNode <Char> elem in alternation.GrammarNodes)
                {
                    hash.Add(elem, this.treeComparer);
                }
                return(hash.ToHashCode());
            }
Example #17
0
        public void Alternation_Multiple()
        {
            var regex = RegularExpression.Of(
                Alternation.Of(
                    Literal.For("a"), Literal.For("b"), Literal.For("c")
                    )
                ).ToRegex();

            Assert.AreEqual("a|b|c", regex.ToString());
        }
Example #18
0
        public override ILexer <TransferCodingCollection> Create()
        {
            var innerLexer = Alternation.Create(
                Terminal.Create(@"trailers", StringComparer.OrdinalIgnoreCase),
                Concatenation.Create(
                    TransferCodingLexerFactory.Create(),
                    Option.Create(TransferCodingRankLexerFactory.Create())));

            return(new TransferCodingCollectionLexer(innerLexer));
        }
Example #19
0
        public override ILexer <RequestTarget> Create()
        {
            var innerLexer = Alternation.Create(
                OriginFormLexerFactory.Create(),
                AbsoluteFormLexerFactory.Create(),
                AuthorityFormLexerFactory.Create(),
                AsteriskFormLexerFactory.Create());

            return(new RequestTargetLexer(innerLexer));
        }
Example #20
0
 public void Initialize()
 {
     InnerLexer = Alternation.Create(
         Grammar.Rule("DIGIT"),
         Terminal.Create(@"A", StringComparer.OrdinalIgnoreCase),
         Terminal.Create(@"B", StringComparer.OrdinalIgnoreCase),
         Terminal.Create(@"C", StringComparer.OrdinalIgnoreCase),
         Terminal.Create(@"D", StringComparer.OrdinalIgnoreCase),
         Terminal.Create(@"E", StringComparer.OrdinalIgnoreCase),
         Terminal.Create(@"F", StringComparer.OrdinalIgnoreCase));
 }
        public override ILexer <TransferCoding> Create()
        {
            var innerLexer = Alternation.Create(
                Terminal.Create(@"chunked", StringComparer.OrdinalIgnoreCase),
                Terminal.Create(@"compress", StringComparer.OrdinalIgnoreCase),
                Terminal.Create(@"deflate", StringComparer.OrdinalIgnoreCase),
                Terminal.Create(@"gzip", StringComparer.OrdinalIgnoreCase),
                TransferExtensionLexerFactory.Create());

            return(new TransferCodingLexer(innerLexer));
        }
Example #22
0
        public override ILexer <PathCharacter> Create()
        {
            var innerLexer = Alternation.Create(
                UnreservedLexerFactory.Create(),
                PercentEncodingLexerFactory.Create(),
                SubcomponentsDelimiterLexerFactory.Create(),
                Terminal.Create(@":", StringComparer.Ordinal),
                Terminal.Create(@"@", StringComparer.Ordinal));

            return(new PathCharacterLexer(innerLexer));
        }
Example #23
0
        public override ILexer <Path> Create()
        {
            var innerLexer = Alternation.Create(
                PathAbsoluteOrEmptyLexerFactory.Create(),
                PathAbsoluteLexerFactory.Create(),
                PathNoSchemeLexerFactory.Create(),
                PathRootlessLexerFactory.Create(),
                PathEmptyLexerFactory.Create());

            return(new PathLexer(innerLexer));
        }
        public override ILexer <ObsoleteFold> Create()
        {
            var innerLexer = Concatenation.Create(
                NewLineLexerFactory.Create(),
                Repetition.Create(
                    Alternation.Create(SpaceLexerFactory.Create(), HorizontalTabLexerFactory.Create()),
                    1,
                    int.MaxValue));

            return(new ObsoleteFoldLexer(innerLexer));
        }
        public override ILexer <QuotedText> Create()
        {
            var innerLexer = Alternation.Create(
                HorizontalTabLexerFactory.Create(),
                SpaceLexerFactory.Create(),
                Terminal.Create(@"!", StringComparer.Ordinal),
                ValueRange.Create(0x23, 0x5B, Encoding.UTF8),
                ValueRange.Create(0x5D, 0x7E, Encoding.UTF8),
                ObsoleteTextLexerFactory.Create());

            return(new QuotedTextLexer(innerLexer));
        }
        public override ILexer <CommentText> Create()
        {
            var innerLexer = Alternation.Create(
                HorizontalTabLexerFactory.Create(),
                SpaceLexerFactory.Create(),
                ValueRange.Create(0x21, 0x27, Encoding.UTF8),
                ValueRange.Create(0x2A, 0x5B, Encoding.UTF8),
                ValueRange.Create(0x5D, 0x7E, Encoding.UTF8),
                ObsoleteTextLexerFactory.Create());

            return(new CommentTextLexer(innerLexer));
        }
Example #27
0
        public override ILexer <FieldValue> Create()
        {
            var innerLexer = Repetition.Create(
                Alternation.Create(
                    FieldContentLexerFactory.Create(),
                    ObsoleteFoldLexerFactory.Create()),
                0,
                int.MaxValue)
            ;

            return(new FieldValueLexer(innerLexer));
        }
Example #28
0
        public override ILexer <Unreserved> Create()
        {
            var innerLexer = Alternation.Create(
                AlphaLexerFactory.Create(),
                DigitLexerFactory.Create(),
                Terminal.Create(@"-", StringComparer.Ordinal),
                Terminal.Create(@".", StringComparer.Ordinal),
                Terminal.Create(@"_", StringComparer.Ordinal),
                Terminal.Create(@"~", StringComparer.Ordinal));

            return(new UnreservedLexer(innerLexer));
        }
Example #29
0
        private void GetBytesh16Alt2(Alternation alternation, BytesFactoryContext ctx)
        {
            if (alternation.Ordinal == 2)
            {
                ctx.LeftAlign.Add(() => hexadecimalInt16Parser.Parse((HexadecimalInt16)alternation.Element));
                return;
            }
            var seq = (Concatenation)alternation.Element;

            ctx.LeftAlign.Add(() => hexadecimalInt16Parser.Parse((HexadecimalInt16)seq[0]));
            ctx.LeftAlign.Add(() => hexadecimalInt16Parser.Parse((HexadecimalInt16)seq[2]));
        }
        public override ILexer <QuotedPair> Create()
        {
            var htab       = HorizontalTabLexerFactory.Create();
            var sp         = SpaceLexerFactory.Create();
            var vchar      = VisibleCharacterLexerFactory.Create();
            var obsText    = ObsoleteTextLexerFactory.Create();
            var innerLexer = Concatenation.Create(
                Terminal.Create(@"\", StringComparer.Ordinal),
                Alternation.Create(htab, sp, vchar, obsText));

            return(new QuotedPairLexer(innerLexer));
        }
		public override PatternNode<ComplexConcParagraphData, ShapeNode> GeneratePattern(FeatureSystem featSys)
		{
			var group = new Group<ComplexConcParagraphData, ShapeNode>();
			Alternation<ComplexConcParagraphData, ShapeNode> alternation = null;
			bool inAlternation = false;
			foreach (ComplexConcPatternNode child in Children)
			{
				if (child is ComplexConcOrNode)
				{
					if (alternation == null)
					{
						alternation = new Alternation<ComplexConcParagraphData, ShapeNode>();
						alternation.Children.Add(group.Children.Last);
					}
					inAlternation = true;
				}
				else
				{
					if (!inAlternation && alternation != null)
					{
						group.Children.Add(alternation);
						alternation = null;
					}

					PatternNode<ComplexConcParagraphData, ShapeNode> newNode = child.GeneratePattern(featSys);
					if (inAlternation)
					{
						alternation.Children.Add(newNode);
						inAlternation = false;
					}
					else
					{
						group.Children.Add(newNode);
					}
				}
			}

			if (alternation != null)
				group.Children.Add(alternation.Children.Count == 1 ? alternation.Children.First : alternation);

			return AddQuantifier(group);
		}
        internal static IEnumerable<PatternNode<Word, ShapeNode>> DeepCloneExceptBoundaries(this IEnumerable<PatternNode<Word, ShapeNode>> nodes)
        {
            foreach (PatternNode<Word, ShapeNode> node in nodes)
            {
                var constraint = node as Constraint<Word, ShapeNode>;
                if (constraint != null && (constraint.FeatureStruct.IsEmpty || constraint.Type() != HCFeatureSystem.Boundary))
                {
                    yield return constraint.DeepClone();
                    continue;
                }

                var alternation = node as Alternation<Word, ShapeNode>;
                if (alternation != null)
                {
                    var newAlteration = new Alternation<Word, ShapeNode>(alternation.Children.DeepCloneExceptBoundaries());
                    if (newAlteration.Children.Count > 0)
                        yield return newAlteration;
                    continue;
                }

                var group = node as Group<Word, ShapeNode>;
                if (group != null)
                {
                    var newGroup = new Group<Word, ShapeNode>(group.Name, group.Children.DeepCloneExceptBoundaries());
                    if (newGroup.Children.Count > 0)
                        yield return newGroup;
                    continue;
                }

                var quantifier = node as Quantifier<Word, ShapeNode>;
                if (quantifier != null)
                {
                    var newQuantifier = new Quantifier<Word, ShapeNode>(quantifier.MinOccur, quantifier.MaxOccur, quantifier.Children.DeepCloneExceptBoundaries().SingleOrDefault());
                    if (newQuantifier.Children.Count > 0)
                        yield return newQuantifier;
                    continue;
                }

                var pattern = node as Pattern<Word, ShapeNode>;
                if (pattern != null)
                {
                    var newPattern = new Pattern<Word, ShapeNode>(pattern.Name, pattern.Children.DeepCloneExceptBoundaries());
                    if (newPattern.Children.Count > 0)
                        yield return newPattern;
                }
            }
        }