Пример #1
0
        internal static bool IsDefined(this IPreprocessorCLogicalOrConditionExp expression, IOilexerGrammarProductionRuleEntry currentEntry, IList <IOilexerGrammarTokenEntry> availableStock, ProductionRuleTemplateArgumentSeries argumentLookup, IOilexerGrammarProductionRuleTemplateEntry entry, OilexerGrammarFile file, ICompilerErrorCollection errors)
        {
            if (expression.Left == null && expression.Right.Left == null && expression.Right.Right.Rule == 3 && expression.Right.Right.PreCPrimary.Rule == 4)
            {
                string name = expression.Right.Right.PreCPrimary.Identifier.Name;
                if (argumentLookup.ContainsParameter(name))
                {
                    IProductionRuleTemplatePart iprtp = argumentLookup.GetParameter(name);
                    if (iprtp.SpecialExpectancy == TemplatePartExpectedSpecial.Rule)
                    {
                        IProductionRuleSeries iprs = argumentLookup[name];
                        if (iprs.Count == 1 && iprs[0].Count == 1)
                        {
                            IProductionRuleItem ipri = iprs[0][0];
                            if (ipri != null)
                            {
                                if (ipri is IRuleReferenceProductionRuleItem)
                                {
                                    name = ((IRuleReferenceProductionRuleItem)(ipri)).Reference.Name;
                                }
                                else if (ipri is ISoftReferenceProductionRuleItem)
                                {
                                    //No guarantee that just being a soft-reference guarantees
                                    //lack of definition.

                                    //Reason: if another template defines this later,
                                    //it exists, but hasn't been resolved. -- It will be later in the expansion/resolution phase.
                                    name = ((ISoftReferenceProductionRuleItem)(ipri)).PrimaryName;
                                }
                            }
                        }
                    }
                    else
                    {
                        errors.SourceError(OilexerGrammarCore.CompilerErrors.IsDefinedTemplateParameterMustExpectRule, new LineColumnPair(expression.Line, expression.Column), LineColumnPair.Zero, new Uri(entry.FileName, UriKind.RelativeOrAbsolute), name);
                    }
                }
                foreach (IOilexerGrammarEntry ientry in file.ToArray())
                {
                    if (ientry is IOilexerGrammarProductionRuleEntry && (!(ientry is IOilexerGrammarProductionRuleTemplateEntry)))
                    {
                        if (((IOilexerGrammarProductionRuleEntry)ientry).Name == name)
                        {
                            return(true);
                        }
                    }
                }
                return(false);
            }
            errors.SourceError(OilexerGrammarCore.CompilerErrors.InvalidDefinedTarget, new LineColumnPair(expression.Line, expression.Column), LineColumnPair.Zero, new Uri(entry.FileName, UriKind.RelativeOrAbsolute), expression.ToString());
            return(false);
        }
Пример #2
0
        internal static void Expand(this IPreprocessorAddRuleDirective directive, IOilexerGrammarProductionRuleEntry currentEntry, IList <IOilexerGrammarTokenEntry> availableStock, ProductionRuleTemplateArgumentSeries argumentLookup, IOilexerGrammarProductionRuleTemplateEntry entry, OilexerGrammarFile file, ICompilerErrorCollection errors)
        {
            string search = directive.InsertTarget;

            if (search != null && search != string.Empty)
            {
                OilexerGrammarProductionRuleEntry foundItem = null;
                if (argumentLookup.ContainsParameter(search))
                {
                    IProductionRuleTemplatePart part = argumentLookup.GetParameter(search);
                    IProductionRuleSeries       iprs = argumentLookup[part];
                    if (iprs.Count == 1 && iprs[0].Count == 1)
                    {
                        IProductionRuleItem ipri = iprs[0][0];
                        if (ipri is ISoftReferenceProductionRuleItem)
                        {
                            ISoftReferenceProductionRuleItem n = (ISoftReferenceProductionRuleItem)ipri;
                            search = n.PrimaryName;
                        }
                        else if (ipri is IRuleReferenceProductionRuleItem)
                        {
                            foundItem = (OilexerGrammarProductionRuleEntry)((IRuleReferenceProductionRuleItem)(ipri)).Reference;
                        }
                    }
                }
                if (foundItem == null)
                {
                    foreach (IOilexerGrammarEntry ie in file)
                    {
                        if (ie is OilexerGrammarProductionRuleEntry && ((OilexerGrammarProductionRuleEntry)ie).Name == search)
                        {
                            foundItem = ie as OilexerGrammarProductionRuleEntry;
                            break;
                        }
                    }
                }
                if (foundItem == null)
                {
                    errors.SourceError(OilexerGrammarCore.CompilerErrors.UndefinedAddRuleTarget, new LineColumnPair(directive.Line, directive.Column), LineColumnPair.Zero, new Uri(entry.FileName, UriKind.RelativeOrAbsolute), string.Join <IProductionRule>(" | ", directive.Rules), search);
                    return;
                }
                foreach (IProductionRule ipr in directive.Rules)
                {
                    foundItem.Add(ipr.Expand(currentEntry, availableStock, argumentLookup, entry, file, errors));
                }
            }
        }
Пример #3
0
        internal static void Expand(this IPreprocessorDefineRuleDirective directive, IOilexerGrammarProductionRuleEntry currentEntry, IList <IOilexerGrammarTokenEntry> availableStock, ProductionRuleTemplateArgumentSeries argumentLookup, IOilexerGrammarProductionRuleTemplateEntry entry, OilexerGrammarFile file, ICompilerErrorCollection errors)
        {
            string search = directive.DeclareTarget;

            if (search != null && search != string.Empty)
            {
                if (argumentLookup.ContainsParameter(search))
                {
                    IProductionRuleTemplatePart part = argumentLookup.GetParameter(search);
                    IProductionRuleSeries       iprs = argumentLookup[part];
                    if (iprs.Count == 1 && iprs[0].Count == 1)
                    {
                        IProductionRuleItem ipri = iprs[0][0];
                        if (ipri is ISoftReferenceProductionRuleItem)
                        {
                            ISoftReferenceProductionRuleItem n = (ISoftReferenceProductionRuleItem)ipri;
                            search = n.PrimaryName;
                        }
                    }
                }
                /* ToDo: Evaluate the depth necessary to institute a lock associated to the base list on the oilexer grammar file type. */
                IOilexerGrammarEntry[] fileElements;
                lock (file)
                    fileElements = file.ToArray();
                foreach (IOilexerGrammarEntry ie in fileElements)
                {
                    if (ie is IOilexerGrammarNamedEntry && ((IOilexerGrammarNamedEntry)ie).Name == search)
                    {
                        errors.SourceError(OilexerGrammarCore.CompilerErrors.DuplicateTermDefined, new LineColumnPair(directive.Line, directive.Column), LineColumnPair.Zero, new Uri(entry.FileName, UriKind.RelativeOrAbsolute), search);
                        return;
                    }
                }
                OilexerGrammarProductionRuleEntry insertedItem = new OilexerGrammarProductionRuleEntry(search, entry.ScanMode, entry.FileName, directive.Column, directive.Line, directive.Position);
                lock (file)
                    file.Add(insertedItem);
                foreach (IProductionRule ipr in directive.DefinedRules)
                {
                    IProductionRule expanded = ipr.Expand(currentEntry, availableStock, argumentLookup, entry, file, errors);
                    insertedItem.Add(expanded);
                }
            }
        }
Пример #4
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));
            }
        }
Пример #5
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);
        }