Beispiel #1
0
        private static IProductionRuleItem Deliteralize <T, TLiteral>(this ILiteralProductionRuleItem <T> literal, IOilexerGrammarProductionRuleEntry currentEntry, IList <IOilexerGrammarTokenEntry> availableStock, OilexerGrammarFile file, Func <ILiteralProductionRuleItem <T>, TLiteral> createNewLiteral, Func <TLiteral, IOilexerGrammarTokenEntry, IProductionRuleItem> createLiteralReference, ICompilerErrorCollection errors)
            where TLiteral :
        ILiteralTokenItem <T>
        {
            {
                ITokenItem foundItem = null;
                IOilexerGrammarTokenEntry literalContainerEntry = null;
                if ((foundItem = FindItemFromStock <T>(literal, currentEntry, availableStock, out literalContainerEntry, file, false)) != null)
                {
                    var result = createLiteralReference(((TLiteral)(foundItem)), literalContainerEntry);
                    ((LiteralProductionRuleItem <T>)literal).CloneData(result);
                    return(result);
                }
            }
            IOilexerGrammarTokenEntry extractionsToken;

            if ((extractionsToken = availableStock.OilexerGrammarFindScannableEntry("__EXTRACTIONS")) == null)
            {
                extractionsToken = new OilexerGrammarTokenEntry("__EXTRACTIONS", new TokenExpressionSeries(new ITokenExpression[0], literal.Line, literal.Column, literal.Position, currentEntry.FileName), currentEntry.ScanMode, currentEntry.FileName, literal.Column, literal.Line, literal.Position, false, new List <OilexerGrammarTokens.IdentifierToken>(), false);
                availableStock.Add(extractionsToken);
                file.Add(extractionsToken);
            }
            TLiteral tokenLiteralItem = createNewLiteral(literal);

            ((TokenExpressionSeries)extractionsToken.Branches).Add(new TokenExpression(new List <ITokenItem>(new ITokenItem[] { tokenLiteralItem }), currentEntry.FileName, literal.Column, literal.Line, literal.Position));
            var tokenReferenceItem = createLiteralReference(((TLiteral)(tokenLiteralItem)), extractionsToken);

            ((LiteralProductionRuleItem <T>)literal).CloneData(tokenReferenceItem);
            return(tokenReferenceItem);
        }
 private static ITokenItem ResolveTokenExpressionItem(this ITokenItem item, IOilexerGrammarTokenEntry entry, OilexerGrammarFile file, ICompilerErrorCollection errors)
 {
     if (item is ITokenGroupItem)
     {
         ((ITokenGroupItem)(item)).ResolveTokenExpressionSeries(entry, file, errors);
         return item;
     }
     else if (item is ISoftReferenceTokenItem)
     {
         return ((ISoftReferenceTokenItem)(item)).ResolveSoftReference(entry, file, errors);
     }
     else if (item is ICommandTokenItem)
     {
         ((ICommandTokenItem)(item)).ResolveSoftReference(entry, file, errors);
         return item;
     }
     else if (item is ILiteralStringTokenItem)
     {
         ILiteralStringTokenItem ilsti = ((ILiteralStringTokenItem)item);
         if (ilsti.Value.Length == 1)
         {
             LiteralCharTokenItem result = new LiteralCharTokenItem(ilsti.Value[0], ilsti.CaseInsensitive, ilsti.Column, ilsti.Line, ilsti.Position);
             ((LiteralStringTokenItem)(item)).CloneData(result);
             return result;
         }
     }//*/
     return item;
 }
Beispiel #3
0
        private static int GetFixedLength(ITokenItem item)
        {
            int currentResult = -1;

            if (item is ILiteralTokenItem)
            {
                currentResult = GetFixedLength((ILiteralTokenItem)item);
            }
            else if (item is ISubtractionCommandTokenItem)
            {
                currentResult = GetFixedLength(((ISubtractionCommandTokenItem)item).Left);
            }
            else if (!(item is ICommandTokenItem))
            {
                currentResult = 1;
            }
            else if (item is ITokenGroupItem)
            {
                currentResult = GetFixedLength((ITokenExpressionSeries)item);
            }
            if (currentResult == -1)
            {
                return(-1);
            }
            else
            {
                return(AdjustFixedLength(currentResult, item.RepeatOptions));
            }
        }
        private static void Deform(ITokenItem item, ICaptureTokenStructuralItem structuralItem, ITokenExpression owner)
        {
            /* *
             * Items reduced to their max are for recognizer sections..
             * */
            if ((item.RepeatOptions.Options & ScannableEntryItemRepeatOptions.MaxReduce) == ScannableEntryItemRepeatOptions.MaxReduce && string.IsNullOrEmpty(item.Name))
            {
                return;
            }
            switch (item.RepeatOptions.Options & (ScannableEntryItemRepeatOptions.OneOrMore | ScannableEntryItemRepeatOptions.ZeroOrMore | ScannableEntryItemRepeatOptions.ZeroOrOne))
            {
            case ScannableEntryItemRepeatOptions.ZeroOrOne:
                structuralItem.Optional = true;
                break;

            case ScannableEntryItemRepeatOptions.ZeroOrMore:
            case ScannableEntryItemRepeatOptions.OneOrMore:
                if (structuralItem.ResultType == ResultedDataType.Character)
                {
                    if (structuralItem is ICaptureTokenCharRangeStructuralItem)
                    {
                        var ctcrsi = (ICaptureTokenCharRangeStructuralItem)structuralItem;
                        if (!ctcrsi.HasSiblings)
                        {
                            ctcrsi.ResultType = ResultedDataType.String;
                            return;
                        }
                    }
                }
                structuralItem.Optional = true;
                //structuralItem.Rank++;
                break;
            }
        }
 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 int ITokenItemSort(ITokenItem x, ITokenItem y)
        {
            if (x is AppearancesToken)
            {
                return(-1);
            }
            else if (y is AppearancesToken)
            {
                return(1);
            }

            SpritesToken a = (SpritesToken)x;
            SpritesToken b = (SpritesToken)y;

            return(a.firstspriteid.CompareTo(b.firstspriteid));
        }
Beispiel #7
0
        private static ITokenItem FindItemFromStock <T>(ILiteralProductionRuleItem <T> literal, IOilexerGrammarProductionRuleEntry currentEntry, IList <IOilexerGrammarTokenEntry> availableStock, out IOilexerGrammarTokenEntry entry, OilexerGrammarFile file, bool followReferences)
        {
            entry = null;
            ITokenItem foundItem = null;

            foreach (IOilexerGrammarTokenEntry ite in availableStock)
            {
                if (ite.ForcedRecognizer)
                {
                    continue;
                }
                ITokenItem iti = ite.FindTokenItemByValue <T>(literal.Value, file, followReferences);
                if (iti != null)
                {
                    foundItem = iti;
                    entry     = ite;
                    break;
                }
            }
            return(foundItem);
        }
 internal void ReclassifyToken(OilexerGrammarTokens.IdentifierToken identifierToken, ITokenItem primary)
 {
     if (reclassifications.ContainsKey(identifierToken))
     {
         return;
     }
     if (primary is ILiteralCharTokenItem)
     {
         reclassifications.Add(identifierToken, OilexerGrammarTokenType.CharacterLiteral);
     }
     else if (primary is ILiteralStringTokenItem)
     {
         reclassifications.Add(identifierToken, OilexerGrammarTokenType.StringLiteral);
     }
 }
 public void ResolvedDualPartToTokenItem(ISoftReferenceTokenItem item, IOilexerGrammarTokenEntry primary, ITokenItem secondary)
 {
     if (item.PrimaryToken != null)
     {
         this.handler.ReclassifyToken(item.PrimaryToken, primary);
     }
     if (item.SecondaryToken != null)
     {
         this.handler.ReclassifyToken(item.SecondaryToken, secondary);
     }
 }
Beispiel #10
0
        internal static object Evaluate(this IPreprocessorCEqualityExp expression, IOilexerGrammarProductionRuleEntry currentEntry, IList <IOilexerGrammarTokenEntry> availableStock, ProductionRuleTemplateArgumentSeries argumentLookup, IOilexerGrammarProductionRuleTemplateEntry entry, OilexerGrammarFile file, ICompilerErrorCollection errors)
        {
            IOilexerGrammarTokenEntry lookup;
            ILiteralTokenItem         reference = null;

            //Hack #1.
            if (expression.Rule == 1 || expression.Rule == 2)
            {
                string name = null;
                //If the left side is a parameter reference and the right side
                //is an identifier...
                if (expression.PreCEqualityExp.Rule == 3 && expression.PreCPrimary.Rule == 4 && expression.PreCEqualityExp.PreCPrimary.Rule == 4 &&
                    argumentLookup.ContainsParameter(name = expression.PreCEqualityExp.PreCPrimary.Identifier.Name))
                {
                    //Obtain the parameter
                    IProductionRuleTemplatePart part = argumentLookup.GetParameter(name);
                    if (part.SpecialExpectancy == TemplatePartExpectedSpecial.None && part.ExpectedSpecific != null)
                    {
                        //If the specific expectency is a production rule reference...
                        if (part.ExpectedSpecific is ITokenReferenceProductionRuleItem)
                        {
                            //Lookup the expectency.
                            ILiteralTokenItem secondHalf = null;
                            lookup = ((ITokenReferenceProductionRuleItem)(part.ExpectedSpecific)).Reference;
                            //Lookup the right-hand requirement for the condition.
                            ITokenItem sRef = lookup.FindTokenItem(expression.PreCPrimary.Identifier.Name);
                            if (sRef is ILiteralTokenItem)
                            {
                                reference = ((ILiteralTokenItem)(sRef));
                            }
                            else
                            {
                                goto notValidReference;
                            }
                            //Obtain the expression series for the left-hand side.
                            IProductionRuleSeries series = argumentLookup[part];
                            //If it's a single unit.
                            if (series.Count == 1 && series[0].Count == 1)
                            {
                                //If it's a soft-reference item...
                                IProductionRuleItem e = series[0][0];
                                if (e is ISoftReferenceProductionRuleItem)
                                {
                                    ISoftReferenceProductionRuleItem sre = ((ISoftReferenceProductionRuleItem)e);
                                    if (((sre.SecondaryName == null || sre.SecondaryName == string.Empty)) && sre.PrimaryName != null)
                                    {
                                        secondHalf = (ILiteralTokenItem)lookup.FindTokenItemByValue(sre.PrimaryName, file, true);
                                    }
                                }
                                //If they used the fully qualified name...
                                else if (e is ILiteralReferenceProductionRuleItem)
                                {
                                    ILiteralReferenceProductionRuleItem lr = ((ILiteralReferenceProductionRuleItem)e);
                                    //So much easier...
                                    secondHalf = lr.Literal;
                                }
                                if (expression.Rule == 1)
                                {
                                    return(secondHalf == reference);
                                }
                                else if (expression.Rule == 2)
                                {
                                    return(secondHalf != reference);
                                }
                            }
                        }
                    }
                }
                else if (expression.PreCEqualityExp.Rule == 3 && expression.PreCPrimary.Rule == 5 && expression.PreCEqualityExp.PreCPrimary.Rule == 4 &&
                         expression.PreCEqualityExp.PreCPrimary.Identifier.Name.ToLower() == "index")
                {
                    if (expression.Rule == 1)
                    {
                        return(expression.PreCPrimary.Number.GetCleanValue() == argumentLookup.Index);
                    }
                    else if (expression.Rule == 2)
                    {
                        return(expression.PreCPrimary.Number.GetCleanValue() != argumentLookup.Index);
                    }
                }
            }
notValidReference:
            switch (expression.Rule)
            {
            case 1:     //PreprocessorCEqualityExp "==" PreprocessorCPrimary
                return(expression.PreCEqualityExp.Evaluate(currentEntry, availableStock, argumentLookup, entry, file, errors) == expression.PreCPrimary.Evaluate(currentEntry, availableStock, argumentLookup, entry, file, errors));

            case 2:     //PreprocessorCEqualityExp "!=" PreprocessorCPrimary
                return(expression.PreCEqualityExp.Evaluate(currentEntry, availableStock, argumentLookup, entry, file, errors) != expression.PreCPrimary.Evaluate(currentEntry, availableStock, argumentLookup, entry, file, errors));

            case 3:     //PreprocessorCPrimary
                return(expression.PreCPrimary.Evaluate(currentEntry, availableStock, argumentLookup, entry, file, errors));
            }
            return(false);
        }
        private static ICaptureTokenStructuralItem BuildStructureFor(InlinedTokenEntry entry, ITokenExpressionSeries expressionSeries, ITokenExpression expression, ITokenItem item, IOilexerGrammarFile source)
        {
            ICaptureTokenStructuralItem result = null;

            if (item is ITokenGroupItem)
            {
                var tokenGroup = ((ITokenGroupItem)(item));
                result = BuildStructureFor(entry, tokenGroup, source);
                if (result.ResultType == ResultedDataType.None && !string.IsNullOrEmpty(item.Name))
                {
                    if (tokenGroup.Count == 1 && tokenGroup[0].Count == 1)
                    {
                        var singleItem = tokenGroup[0][0];
                        if (singleItem is ILiteralCharTokenItem || singleItem is ILiteralCharReferenceTokenItem)
                        {
                            if (singleItem.RepeatOptions == ScannableEntryItemRepeatInfo.None && tokenGroup.RepeatOptions == ScannableEntryItemRepeatInfo.None)
                            {
                                result.ResultType = ResultedDataType.Character;
                            }
                            else
                            {
                                result.ResultType = ResultedDataType.String;
                            }
                        }
                        else
                        {
                            result.ResultType = ResultedDataType.String;
                        }
                    }
                    else
                    {
                        result.ResultType = ResultedDataType.String;
                    }
                }
                bool groupOptional = tokenGroup != null &&
                                     ((tokenGroup.RepeatOptions.Options & ScannableEntryItemRepeatOptions.ZeroOrMore) == ScannableEntryItemRepeatOptions.ZeroOrMore ||
                                      (tokenGroup.RepeatOptions.Options & ScannableEntryItemRepeatOptions.ZeroOrOne) == ScannableEntryItemRepeatOptions.ZeroOrOne ||
                                      (((tokenGroup.RepeatOptions.Options & ScannableEntryItemRepeatOptions.Specific) == ScannableEntryItemRepeatOptions.Specific) &&
                                       (!tokenGroup.RepeatOptions.Min.HasValue || tokenGroup.RepeatOptions.Min.Value == 0)));
                if (groupOptional)
                {
                    result.GroupOptional = groupOptional;
                }
                if (item.Name.IsEmptyOrNull())
                {
                    result.ResultType = ResultedDataType.PassThrough;

                    /*
                     * if ((item.RepeatOptions.Options & ScannableEntryItemRepeatOptions.ZeroOrMore) == ScannableEntryItemRepeatOptions.ZeroOrMore ||
                     *  (item.RepeatOptions.Options & ScannableEntryItemRepeatOptions.ZeroOrOne) == ScannableEntryItemRepeatOptions.ZeroOrOne ||
                     *  ((item.RepeatOptions.Options & ScannableEntryItemRepeatOptions.Specific) == ScannableEntryItemRepeatOptions.Specific &&
                     *  (!item.RepeatOptions.Min.HasValue || item.RepeatOptions.Min.Value == 0)))
                     *  foreach (var element in ((ICaptureTokenStructure)result).Values)
                     *      element.Optional = true;*/
                }

                ((ControlledCollection <ITokenSource>)(result.Sources)).baseList.Add(item);
            }
            else if (item is ILiteralTokenItem)
            {
                result = new CaptureTokenLiteralStructuralItem((ILiteralTokenItem)item);
                if (entry.CaptureKind == RegularCaptureType.Transducer)
                {
                    result.ResultType = ResultedDataType.EnumerationItem;
                }
                else if (entry.CaptureKind == RegularCaptureType.ContextfulTransducer)
                {
                    result.ResultType = ResultedDataType.FlagEnumerationItem;
                }
            }
            else if (item is ICharRangeTokenItem)
            {
                result = new CaptureTokenCharRangeStructuralItem((ICharRangeTokenItem)item);
                if (entry.CaptureKind == RegularCaptureType.Transducer)
                {
                    result.ResultType = ResultedDataType.EnumerationItem;
                }
                else if (entry.CaptureKind == RegularCaptureType.ContextfulTransducer)
                {
                    result.ResultType = ResultedDataType.FlagEnumerationItem;
                }
            }

            Deform(item, result, expression);

            return(result);
        }