Exemple #1
0
            /// <summary>
            /// Create one token if one textTokenList matches.
            /// </summary>
            /// <param name="owner">Owner of new token to create.</param>
            /// <param name="reference">Character to start match.</param>
            /// <param name="createToken">Call back method to create one token.</param>
            /// <param name="textTokenList">One of these tokens has to match.</param>
            public static void CreateToken(FileText owner, Storage.Character reference, Action <FileText, string, Storage.Character, Storage.Character> createToken, params string[] textTokenList)
            {
                foreach (var textToken in textTokenList)
                {
                    if (reference.Owner.Text.Length > reference.TextIndex + textToken.Length - 1)
                    {
                        if (reference.Owner.Text.Substring(reference.TextIndex, textToken.Length) == textToken)
                        {
                            var referenceEnd = (Storage.Character)reference.Owner.List[reference.TextIndex + textToken.Length - 1];

                            // Create before
                            int length = owner.List.Count;

                            // Create
                            createToken(owner, textToken, reference, referenceEnd);

                            // Create after
                            int lengthNew = owner.List.Count;

                            // Create validate
                            UtilFramework.Assert(lengthNew - length == 1); // Created one token
                            UtilFramework.Assert(owner.Last is Token);     // Create of type token

                            break;
                        }
                    }
                }
            }
Exemple #2
0
 /// <summary>
 /// Constructor for syntax component with reference range.
 /// </summary>
 public Syntax(Component owner, Syntax referenceBegin, Syntax referenceEnd)
     : base(owner)
 {
     UtilFramework.Assert(!(referenceBegin == null ^ referenceEnd == null));
     ReferenceBegin = referenceBegin;
     ReferenceEnd   = referenceEnd;
 }
Exemple #3
0
 /// <summary>
 /// Removes this component from owners list.
 /// </summary>
 public void Remove()
 {
     if (Owner != null)
     {
         UtilFramework.Assert(Owner.List.Remove(this));
         Owner = null;
     }
 }
Exemple #4
0
 public void CreateSyntaxTree(Component owner, Syntax reference, params Type[] syntaxFactoryStopList)
 {
     UtilFramework.Assert(owner != null && reference != null);
     Owner     = owner;
     Reference = reference;
     foreach (var type in syntaxFactoryStopList)
     {
         SyntaxFactoryStopList.Add(SyntaxFactoryList.Single(item => item.GetType() == type));
     }
 }
Exemple #5
0
            /// <summary>
            /// Create syntax tree.
            /// </summary>
            /// <param name="owner">Owner of new syntax tree.</param>
            /// <param name="referenceList">Reference list to walk through to create new syntax components.</param>
            /// <param name="reference">Reference component in referenceList to start with or null to start with first component.</param>
            /// <param name="syntaxFactoryList">Factories to create syntax components.</param>
            /// <param name="syntaxFactoryStopList">If referenceList hits one of this factories, walk through stops.</param>
            private static void CreateSyntaxTree(Component owner, List <Component> referenceList, Syntax reference, List <Syntax> syntaxFactoryList, List <Syntax> syntaxFactoryStopList)
            {
                int referenceIndex = 0;

                if (reference != null)
                {
                    UtilFramework.Assert(referenceList.Contains(reference));
                    referenceIndex = referenceList.IndexOf(reference);
                }

                // Loop through reference list starting at item.
                for (int index = referenceIndex; index < referenceList.Count; index++)
                {
                    Syntax item = (Syntax)referenceList[index];

                    // Create syntax from factory
                    Syntax syntax = null;
                    // Syntax factory used to create syntax
                    Syntax syntaxFactory = null;
                    foreach (var syntaxFactoryItem in syntaxFactoryList)
                    {
                        // Create before
                        int    syntaxLength = owner.List.Count;
                        Syntax syntaxLast   = (Syntax)owner.Last;

                        // Create
                        var result = new CreateSyntaxResult(referenceList, syntaxFactoryList);
                        syntaxFactoryItem.CreateSyntax(owner, item, result);
                        if (result.Reference != null)
                        {
                            CreateSyntaxTree(result.Owner, result.ReferenceList, result.Reference, result.SyntaxFactoryList, result.SyntaxFactoryStopList);
                        }

                        // Create after
                        int    syntaxLengthNew = owner.List.Count;
                        Syntax syntaxLastNew   = (Syntax)owner.Last;

                        // Create result get
                        UtilFramework.Assert(syntaxLengthNew - syntaxLength == 0 || syntaxLengthNew - syntaxLength == 1); // Zero or one token added.
                        if (syntaxLengthNew - syntaxLength == 1)
                        {
                            syntax = syntaxLastNew;
                        }
                        else
                        {
                            if (syntaxLast != syntaxLastNew)
                            {
                                syntax = syntaxLastNew;
                            }
                        }

                        // Create result
                        if (syntax != null)
                        {
                            UtilFramework.Assert(syntax.GetType() == syntaxFactoryItem.GetType());
                            syntaxFactory = syntaxFactoryItem;
                            break;
                        }
                    }

                    // Created syntax
                    UtilFramework.Assert(syntax != null);
                    var syntaxPrevious = (Syntax)syntax.PreviousAll;
                    if (syntaxPrevious != null)
                    {
                        // Make sure every reference component is covered.
                        UtilFramework.Assert(syntaxPrevious.ReferenceEnd.Index + 1 == syntax.ReferenceBegin.Index);
                    }
                    int referenceEndIndex = referenceList.IndexOf(syntax.ReferenceEndAll);
                    UtilFramework.Assert(index <= referenceEndIndex && referenceEndIndex < referenceList.Count);
                    // Move index to new end
                    index = referenceEndIndex;
                    if (syntaxFactoryStopList.Contains(syntaxFactory))
                    {
                        syntax.Remove();
                        break;
                    }
                }
            }