Example #1
0
        private void CheckForUnexpectedArguments(Alias alias, AliasDefinition aliasDef, string fileName)
        {
            foreach (var argument in alias.Arguments)
            {
                if (aliasDef.Parameters.All(p => p.Name != argument.Name))
                {
                    Context.AddError(CompilerErrorFactory.UnexpectedArgument((Argument)argument,
                                                                             fileName));
                }
            }

            if (!aliasDef.HasDefaultBlockParameter)
            {
                if (alias.Entities.Any(e => !(e is Comment) && e is IMappedPair mp &&
                                       (mp.BlockType == BlockType.Default || mp.BlockType == BlockType.JsonArray)))
                {
                    Context.AddError(
                        CompilerErrorFactory.UnexpectedDefaultBlockArgument((IMappedPair)alias.Entities[0], fileName));
                }
            }

            if (!aliasDef.HasDefaultValueParameter && alias.HasValue())
            {
                Context.AddError(CompilerErrorFactory.UnexpectedDefaultValueArgument(alias,
                                                                                     fileName));
            }
        }
        private void CheckCompatibilityWithParameters(Alias alias, AliasDefinition aliasDef, string fileName)
        {
            foreach (var parameter in aliasDef.Parameters)
            {
                if (parameter.Name == "_") //Default parameter
                {
                    if (!parameter.IsValueNode)
                    {
                        if (alias.Entities.All(e => e is Comment) && alias.ValueType != ValueType.Object)
                        {
                            ReportErrorInsideChoice(() => CompilerErrorFactory.DefaultBlockArgumentIsMissing(alias,
                                                                                                             fileName));
                        }
                    }
                    else
                    {
                        if (parameter.HasValue())
                        {
                            continue;                       //if parameter has default value then skip check
                        }
                        if (!alias.HasValue())
                        {
                            ReportErrorInsideChoice(() => CompilerErrorFactory.DefaultValueArgumentIsMissing(alias,
                                                                                                             fileName));
                        }
                    }
                    continue;
                }

                //Non default parameter
                var argument = alias.Arguments.FirstOrDefault(a => a.Name == parameter.Name);
                if (argument == null)
                {
                    //Report Error if argument is missing and there is no default value for the parameter
                    if (parameter.Value == null && parameter.Entities.Count == 0 && parameter.ValueType != ValueType.Object)
                    {
                        ReportErrorInsideChoice(() => CompilerErrorFactory.ArgumentIsMissing(alias, parameter.Name,
                                                                                             fileName));
                    }

                    continue;
                }

                //Report error if type of argument (value/block) mismatch the type of parameter
                if (((Argument)argument).IsValueNode != parameter.IsValueNode)
                {
                    ReportErrorInsideChoice(() => parameter.IsValueNode
                        ? CompilerErrorFactory.ValueArgumentIsExpected((Argument)argument,
                                                                       fileName)
                        : CompilerErrorFactory.BlockArgumentIsExpected((Argument)argument,
                                                                       fileName));
                }
            }
        }
        private void ValidateAliasDefDefaultParameter(AliasDefinition aliasDef)
        {
            if (!aliasDef.HasDefaultBlockParameter && !aliasDef.HasDefaultValueParameter)
            {
                return;
            }

            var hasNonDefaultParameter = aliasDef.Parameters.Any(p => p.Name != "_");

            if (!hasNonDefaultParameter)
            {
                return;
            }

            foreach (var param in aliasDef.Parameters)
            {
                if (param.Name == "_")
                {
                    _context.AddError(CompilerErrorFactory.DefaultParameterMustBeOnly(param, aliasDef.Module.FileName));
                }
            }
        }
 public void EnterAliasDef(AliasDefinition node)
 {
     _currentModuleMember       = node;
     _currentModuleMemberNsInfo = new NsInfo(_currentModuleMember);
     ModuleMembersNsInfo.Add(_currentModuleMemberNsInfo);
 }
Example #5
0
 internal static CompilerError DuplicateAliasDefName(AliasDefinition aliasDef, string fileName)
 {
     return(Instantiate("MCE0012", new LexicalInfo(fileName, aliasDef.NameInterval.Begin.Line, aliasDef.NameInterval.Begin.Column, aliasDef.NameInterval.Begin.Index), false, aliasDef.Name));
 }
Example #6
0
        /// <inheritdoc />
        public Pair CreateMappedPair(ITextSource textSource, int nameQuotesType, Interval nameInterval,
                                     AssignmentEnum assignment,
                                     Interval assignmentInterval, int valueQuotesType, Interval valueInterval, int valueIndent)
        {
            IMappedPair pair;
            var         nameText = GetNameText(textSource, nameQuotesType, nameInterval);
            var         value    = GetValue(textSource, assignment, valueQuotesType, valueInterval, valueIndent, _context, _module);

            if (nameQuotesType > 0)
            {
                pair = new Element
                       (
                    VerifyElementName(nameText, nameInterval, _module),
                    nameQuotesType: nameQuotesType,
                    nameInterval: nameInterval,
                    assignment: assignment,
                    assignmentInterval: assignmentInterval,
                    value: value.Item1,
                    valueQuotesType: valueQuotesType,
                    valueInterval: valueInterval,
                    interpolationItems: value.Item2,
                    valueIndent: valueIndent,
                    valueType: GetValueType(assignment, value.Item1, valueQuotesType)
                       );
            }
            else if (nameText.StartsWith("@"))
            {
                var tuple = Element.GetNameAndNs(nameText.Substring(1), nameQuotesType);
                var ns    = string.IsNullOrEmpty(tuple.Item1) ? null : tuple.Item1;
                pair = new Attribute
                       (
                    VerifyName(tuple.Item2, nameInterval, _module),
                    ns,
                    nameInterval: nameInterval,
                    assignment: assignment,
                    assignmentInterval: assignmentInterval,
                    value: value.Item1,
                    valueQuotesType: valueQuotesType,
                    valueInterval: valueInterval,
                    interpolationItems: value.Item2,
                    valueIndent: valueIndent,
                    valueType: GetValueType(assignment, value.Item1, valueQuotesType)
                       );
            }
            else if (nameText.StartsWith("!$"))
            {
                pair = new AliasDefinition
                       (
                    VerifyName(nameText.Substring(2), nameInterval, _module),
                    nameInterval: nameInterval,
                    assignment: assignment,
                    assignmentInterval: assignmentInterval,
                    value: value.Item1,
                    valueQuotesType: valueQuotesType,
                    valueInterval: valueInterval,
                    interpolationItems: value.Item2,
                    valueIndent: valueIndent,
                    valueType: GetValueType(assignment, value.Item1, valueQuotesType)
                       );
            }
            else if (nameText.StartsWith("!#"))
            {
                pair = new NamespaceDefinition
                       (
                    VerifyNsName(nameText.Substring(2), nameInterval, _module),
                    nameInterval: nameInterval,
                    assignment: assignment,
                    assignmentInterval: assignmentInterval,
                    value: value.Item1,
                    valueQuotesType: valueQuotesType,
                    valueInterval: valueInterval,
                    interpolationItems: value.Item2,
                    valueIndent: valueIndent,
                    valueType: GetValueType(assignment, value.Item1, valueQuotesType)
                       );
            }
            else if (nameText.StartsWith("!%"))
            {
                pair = new Parameter
                       (
                    VerifyNsName(nameText.Substring(2), nameInterval, _module),
                    nameInterval: nameInterval,
                    assignment: assignment,
                    assignmentInterval: assignmentInterval,
                    value: value.Item1,
                    valueQuotesType: valueQuotesType,
                    valueInterval: valueInterval,
                    interpolationItems: value.Item2,
                    valueIndent: valueIndent,
                    valueType: GetValueType(assignment, value.Item1, valueQuotesType)
                       );
            }
            else if (nameText.StartsWith("!"))
            {
                pair = new Document
                       (
                    VerifyName(nameText.Substring(1), nameInterval, _module),
                    nameInterval: nameInterval,
                    assignment: assignment,
                    assignmentInterval: assignmentInterval,
                    value: value.Item1,
                    valueQuotesType: valueQuotesType,
                    valueInterval: valueInterval,
                    interpolationItems: value.Item2,
                    valueIndent: valueIndent,
                    valueType: GetValueType(assignment, value.Item1, valueQuotesType)
                       );
            }
            else if (nameText.StartsWith("$"))
            {
                pair = new Alias
                       (
                    VerifyName(nameText.Substring(1), nameInterval, _module),
                    nameInterval: nameInterval,
                    assignment: assignment,
                    assignmentInterval: assignmentInterval,
                    value: value.Item1,
                    valueQuotesType: valueQuotesType,
                    valueInterval: valueInterval,
                    interpolationItems: value.Item2,
                    valueIndent: valueIndent,
                    valueType: GetValueType(assignment, value.Item1, valueQuotesType)
                       );
            }
            else if (nameText.StartsWith("%"))
            {
                pair = new Argument
                       (
                    VerifyName(nameText.Substring(1), nameInterval, _module),
                    nameInterval: nameInterval,
                    assignment: assignment,
                    assignmentInterval: assignmentInterval,
                    value: value.Item1,
                    valueQuotesType: valueQuotesType,
                    valueInterval: valueInterval,
                    interpolationItems: value.Item2,
                    valueIndent: valueIndent,
                    valueType: GetValueType(assignment, value.Item1, valueQuotesType)
                       );
            }
            else if (nameText.StartsWith("#"))
            {
                var tuple = Element.GetNameAndNs(nameText.Substring(1), nameQuotesType);
                var ns    = string.IsNullOrEmpty(tuple.Item1) ? null : tuple.Item1;

                if (ns == null)
                {
                    pair = new Scope
                           (
                        null,
                        VerifyScopeName(nameText.Substring(1), nameInterval, _module),
                        nameInterval: nameInterval,
                        assignment: assignment,
                        assignmentInterval: assignmentInterval,
                        value: value.Item1,
                        valueQuotesType: valueQuotesType,
                        valueInterval: valueInterval,
                        interpolationItems: value.Item2,
                        valueIndent: valueIndent,
                        valueType: GetValueType(assignment, value.Item1, valueQuotesType)
                           );
                }
                else
                {
                    pair = new Scope
                           (
                        VerifyElementName(tuple.Item2, nameInterval, _module),
                        VerifyScopeName(ns, nameInterval, _module),
                        nameInterval: nameInterval,
                        assignment: assignment,
                        assignmentInterval: assignmentInterval,
                        value: value.Item1,
                        valueQuotesType: valueQuotesType,
                        valueInterval: valueInterval,
                        interpolationItems: value.Item2,
                        valueIndent: valueIndent,
                        valueType: GetValueType(assignment, value.Item1, valueQuotesType)
                           );
                }
            }
            else
            {
                var tuple = Element.GetNameAndNs(nameText, nameQuotesType);
                var ns    = string.IsNullOrEmpty(tuple.Item1) ? null : tuple.Item1;

                pair = new Element
                       (
                    VerifyElementName(tuple.Item2, nameInterval, _module),
                    VerifyScopeName(ns, nameInterval, _module),
                    nameInterval: nameInterval,
                    assignment: assignment,
                    assignmentInterval: assignmentInterval,
                    value: value.Item1,
                    valueQuotesType: valueQuotesType,
                    valueInterval: valueInterval,
                    interpolationItems: value.Item2,
                    valueIndent: valueIndent,
                    valueType: GetValueType(assignment, value.Item1, valueQuotesType)
                       );
            }
            return((Pair)pair);
        }