示例#1
0
        //private static IOilexerGrammarProductionRuleEntry currentEntry = null;
        public static void FinalLink(this OilexerGrammarFile file, ICompilerErrorCollection errors)
        {
            //currentEntry = null;
            IList <IOilexerGrammarProductionRuleEntry> original = ruleEntries.ToList();
            //When '__EXTRACTED' is added, ruleEntries invalidates.
            var availableStock = file.GetTokens().Cast <IOilexerGrammarTokenEntry>().ToList();

            foreach (IOilexerGrammarProductionRuleEntry rule in original)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                rule.Deliteralize(availableStock, file, errors);
            }
            file.ExpungeUnusedTokens();

            foreach (IOilexerGrammarProductionRuleEntry rule in ruleEntries)
            {
                rule.FinalLink(file, errors);
            }
            var namedEntries = from s in file
                               where s is IOilexerGrammarNamedEntry
                               select(IOilexerGrammarNamedEntry) s;

            var namedDuplicates = (from s in namedEntries
                                   let duplicates = (from s2 in namedEntries
                                                     where s != s2
                                                     where s.Name == s2.Name
                                                     select s2).ToArray()
                                                    where duplicates.Length > 0
                                                    select new { Entry = s, Duplicates = duplicates });
            var passedDuplicates = new List <IOilexerGrammarNamedEntry>();

            foreach (var duplLookup in namedDuplicates)
            {
                if (passedDuplicates.Contains(duplLookup.Entry))
                {
                    continue;
                }
                passedDuplicates.Add(duplLookup.Entry);
                var mainError = errors.SourceModelError(OilexerGrammarCore.CompilerErrors.DuplicateEntryError, new LineColumnPair(duplLookup.Entry.Line, duplLookup.Entry.Column), new LineColumnPair(duplLookup.Entry.Line, duplLookup.Entry.Column + duplLookup.Entry.Name.Length), new Uri(duplLookup.Entry.FileName, UriKind.RelativeOrAbsolute), duplLookup.Entry, new string[] { duplLookup.Entry.Name });
                foreach (var duplicate in duplLookup.Duplicates)
                {
                    passedDuplicates.Add(duplicate);
                    errors.SourceModelError(OilexerGrammarCore.CompilerErrors.DuplicateEntryReference, new LineColumnPair(duplicate.Line, duplicate.Column), new LineColumnPair(duplicate.Line, duplicate.Column + duplicate.Name.Length), new Uri(duplicate.FileName, UriKind.RelativeOrAbsolute), duplicate, mainError, new string[] { duplicate.Name });
                }
            }
            file.ExpungeComments();
            foreach (var rule in ruleEntries)
            {
                if (rule.IsRuleCollapsePoint)
                {
                    rule.ValidateCollapsePoint(errors);
                }
            }
        }
 private static ITokenItem ResolveSoftReference(this ISoftReferenceTokenItem item, IOilexerGrammarTokenEntry entry, OilexerGrammarFile file, ICompilerErrorCollection errors)
 {
     IOilexerGrammarTokenEntry tokenE = tokenEntries.OilexerGrammarFindScannableEntry(item.PrimaryName);
     if (tokenE != null)
     {
         if (item.SecondaryName != null)
         {
             ITokenItem iti = tokenE.FindTokenItem(item.SecondaryName);
             if (iti != null)
             {
                 if (iti is ILiteralCharTokenItem)
                 {
                     LiteralCharReferenceTokenItem result = new LiteralCharReferenceTokenItem(tokenE, ((ILiteralCharTokenItem)(iti)), item.Column, item.Line, item.Position);
                     if (!(string.IsNullOrEmpty(item.Name)))
                         result.Name = item.Name;
                     if (resolutionAid != null)
                         resolutionAid.ResolvedDualPartToTokenItem(item, tokenE, iti);
                     return result;
                 }
                 else if (iti is ILiteralStringTokenItem)
                 {
                     LiteralStringReferenceTokenItem result = new LiteralStringReferenceTokenItem(tokenE, ((ILiteralStringTokenItem)(iti)), item.Column, item.Line, item.Position);
                     if (!(string.IsNullOrEmpty(item.Name)))
                         result.Name = item.Name;
                     if (resolutionAid != null)
                         resolutionAid.ResolvedDualPartToTokenItem(item, tokenE, iti);
                     return result;
                 }
                 else
                 {
                     errors.SourceModelError<ISoftReferenceTokenItem>(OilexerGrammarCore.CompilerErrors.UndefinedTokenReference, new LineColumnPair(item.Line, item.Column), new LineColumnPair(item.Line, item.Column + item.PrimaryName.Length), new Uri(entry.FileName, UriKind.RelativeOrAbsolute), item, string.Format(" '{0}'", item.PrimaryName));
                 }
             }
         }
         else
         {
             TokenReferenceTokenItem result = new TokenReferenceTokenItem(tokenE, item.Column, item.Line, item.Position);
             ((SoftReferenceTokenItem)(item)).CloneData(result);
             if (resolutionAid != null)
                 resolutionAid.ResolvedSinglePartToToken(item, tokenE);
             return result;
         }
     }
     else if (item.SecondaryName == null)
         errors.SourceModelError<ISoftReferenceTokenItem>(OilexerGrammarCore.CompilerErrors.UndefinedTokenReference, new LineColumnPair(item.Line, item.Column), new LineColumnPair(item.Line, item.Column + item.PrimaryName.Length), new Uri(entry.FileName, UriKind.RelativeOrAbsolute), item, string.Format(" '{0}'", item.PrimaryName));
     else
         errors.SourceModelError<ISoftReferenceTokenItem>(OilexerGrammarCore.CompilerErrors.UndefinedTokenReference, new LineColumnPair(item.Line, item.Column), new LineColumnPair(item.SecondaryToken.Line, item.SecondaryToken.Column + item.SecondaryToken.Length), new Uri(entry.FileName, UriKind.RelativeOrAbsolute), item, string.Format(" '{0}'", item.PrimaryName));
     return item;
 }
 private static void ResolveProductionRuleTemplate(this IOilexerGrammarProductionRuleTemplateEntry entry, OilexerGrammarFile file, ICompilerErrorCollection errors)
 {
     foreach (ProductionRuleTemplatePart part in entry.Parts)
     {
         IOilexerGrammarTokenEntry reference = null;
         if (part.SpecialExpectancy == TemplatePartExpectedSpecial.None && part.ExpectedSpecific != null && part.ExpectedSpecific is ISoftReferenceProductionRuleItem)
         {
             var softExpect = part.ExpectedSpecific as ISoftReferenceProductionRuleItem;
             if ((reference = tokenEntries.OilexerGrammarFindScannableEntry(softExpect.PrimaryName)) != null)
             {
                 part.ExpectedSpecific = new TokenReferenceProductionRuleItem(reference, part.Column, part.Line, part.Position);
                 if (resolutionAid != null)
                     resolutionAid.ResolvedSinglePartToToken(softExpect, reference);
             }
             else
                 errors.SourceModelError<ISoftReferenceProductionRuleItem>(OilexerGrammarCore.CompilerErrors.UndefinedTokenReference, new LineColumnPair(part.ExpectedSpecific.Column, part.ExpectedSpecific.Line), LineColumnPair.Zero, new Uri(entry.FileName, UriKind.RelativeOrAbsolute), softExpect, string.Format(" '{0}'", softExpect.PrimaryName));
             return;
         }
     }
 }
 private static void ResolveToken(this IOilexerGrammarTokenEntry entry, OilexerGrammarFile file, ICompilerErrorCollection errors)
 {
     List<IOilexerGrammarTokenEntry> lowerTokens = new List<IOilexerGrammarTokenEntry>();
     if (entry.LowerPrecedenceTokens == null)
     {
         foreach (var s in entry.LowerPrecedenceNames)
         {
             var match = (from generalEntry in tokenEntries
                          let tokenEntry = generalEntry as IOilexerGrammarTokenEntry
                          where tokenEntry != null &&
                                 tokenEntry.Name == s.Name
                          select tokenEntry).FirstOrDefault();
             if (match == null)
             {
                 errors.SourceModelError<IOilexerGrammarTokenEntry>(OilexerGrammarCore.CompilerErrors.UndefinedTokenReference, new LineColumnPair(s.Line, s.Column), new LineColumnPair(s.Line, s.Column + s.Name.Length), new Uri(entry.FileName, UriKind.RelativeOrAbsolute), entry, string.Format(@", lower precedence: '{0}'", s.Name));
                 break;
             }
             lowerTokens.Add(match);
         }
         ((OilexerGrammarTokenEntry)entry).LowerPrecedenceTokens = lowerTokens.ToArray();
     }
     entry.Branches.ResolveTokenExpressionSeries(entry, file, errors);
 }
示例#5
0
        internal static void Expand(this IPreprocessorThrowDirective directive, IOilexerGrammarProductionRuleEntry currentEntry, IList <IOilexerGrammarTokenEntry> availableStock, ProductionRuleTemplateArgumentSeries argumentLookup, IOilexerGrammarProductionRuleTemplateEntry entry, OilexerGrammarFile file, ICompilerErrorCollection errors)
        {
            string[] errorData = new string[directive.Arguments.Length];
            int      index     = 0;
            //List<Tuple<string, int, int>> errorLocations = new List<Tuple<string, int, int>>();
            List <Tuple <IProductionRule, ISoftReferenceProductionRuleItem> > errorLocations = new List <Tuple <IProductionRule, ISoftReferenceProductionRuleItem> >();

            foreach (var item in directive.Arguments)
            {
                if (item.TokenType == OilexerGrammarTokenType.Identifier)
                {
                    var idItem = item as OilexerGrammarTokens.IdentifierToken;
                    if (!argumentLookup.ContainsParameter(idItem.Name))
                    {
                        errorData[index++] = idItem.Name;
                        continue;
                    }
                    else
                    {
                        var parameter           = argumentLookup.GetParameter(idItem.Name);
                        var parameterDataSeries = argumentLookup[parameter];
                        if (parameterDataSeries.Count == 1 &&
                            parameterDataSeries[0].Count == 1)
                        {
                            var pd0           = parameterDataSeries[0];
                            var parameterData = pd0[0];
                            if (parameterData is ISoftReferenceProductionRuleItem)
                            {
                                var specificData = (ISoftReferenceProductionRuleItem)parameterData;
                                if (specificData.SecondaryToken == null)
                                {
                                    errorData[index] = specificData.PrimaryName;
                                }
                                else
                                {
                                    errorData[index] = string.Format("{0}.{1}", specificData.PrimaryName, specificData.SecondaryName);
                                }
                                errorLocations.Add(Tuple.Create(pd0, specificData));
                            }
                        }
                    }
                }
                else if (item.TokenType == OilexerGrammarTokenType.CharacterLiteral)
                {
                    errorData[index] = ((OilexerGrammarTokens.CharLiteralToken)(item)).GetCleanValue().ToString();
                }
                else if (item.TokenType == OilexerGrammarTokenType.StringLiteral)
                {
                    errorData[index] = ((OilexerGrammarTokens.StringLiteralToken)(item)).GetCleanValue();
                }
                index++;
            }
            if (errorLocations.Count > 0)
            {
                foreach (var errorLocation in errorLocations)
                {
                    errors.SourceModelError(OilexerGrammarCore.CompilerErrors.LanguageDefinedError, new LineColumnPair(errorLocation.Item2.Line, errorLocation.Item2.Column), new LineColumnPair(errorLocation.Item2.Line, errorLocation.Item2.Column + (errorLocation.Item2.SecondaryToken == null ? errorLocation.Item2.PrimaryName.Length : errorLocation.Item2.PrimaryName.Length + errorLocation.Item2.SecondaryName.Length)), new Uri(errorLocation.Item1.FileName, UriKind.RelativeOrAbsolute), directive, errorLocation.Item1, errorLocation.Item2, new string[] { directive.Reference.Number.ToString(), string.Format(directive.Reference.Message, errorData) });
                }
            }
            else
            {
                errors.SourceError(OilexerGrammarCore.CompilerErrors.LanguageDefinedError, new LineColumnPair(directive.Line, directive.Column), LineColumnPair.Zero, new Uri(entry.FileName, UriKind.RelativeOrAbsolute), directive.Reference.Number.ToString(), string.Format(directive.Reference.Message, errorData));
            }
        }