Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DataModelTypeBuilder"/> class.
        /// </summary>
        /// <param name="decl">The declaration declaring this type.</param>
        protected DataModelTypeBuilder(GrammarSymbol decl)
        {
            this.DeclSymbol     = decl;
            this.G4DeclaredName = decl.GetLogicalText();
            this.SummaryText    = decl.Annotations.GetAnnotationValue("summary");
            this.RemarksText    = decl.Annotations.GetAnnotationValue("remarks");
            this.CSharpName
                = decl.Annotations.GetAnnotationValue("className")
                  ?? LinguisticTransformer.ToCSharpName(this.G4DeclaredName);

            this.InterfaceName = decl.Annotations.GetAnnotationValue("interface");

            this.Pattern = decl.Annotations.GetAnnotationValue("pattern");

            this.RootObject = decl.Annotations.GetAnnotationValue("rootObject") != null;

            this.G4DeclaredValues = new List <string>();

            string serializedValuesText = decl.Annotations.GetAnnotationValue("serializedValues");

            if (!string.IsNullOrEmpty(serializedValuesText))
            {
                this.SerializedValues = new List <string>(serializedValuesText.Split(','));
            }
            else
            {
                this.SerializedValues = new List <string>();
            }
        }
        public void CompileProduction(GrammarSymbol production)
        {
            Debug.Assert(production.Kind == SymbolKind.Production);
            GrammarSymbol decl = production[0];

            Debug.Assert(decl.Kind == SymbolKind.ProductionDecl);
            if (decl.FirstToken != production.FirstToken)
            {
                // Honestly I don't know why "fragment" productions are even
                // in the grammar if they're not supposed to be produced; but this is what
                // the original MSR code did and we need to parse their grammar files.
                Debug.Assert(production.FirstToken.GetText() == "fragment");
                return;
            }

            string productionName = decl.GetLogicalText();

            if (s_basicBuiltinTypes.Any(type => type.G4DeclaredName == productionName))
            {
                // Builtins don't matter in the grammar.
                return;
            }

            GrammarSymbol productionIs = production[1];

            switch (productionIs.Kind)
            {
            case SymbolKind.String:
                this.CompileEnumValueType(decl, productionIs.FirstToken);
                break;

            case SymbolKind.Identifier:
            case SymbolKind.ZeroOrMoreQuantifier:
            case SymbolKind.OneOrMoreQuantifier:
            case SymbolKind.ZeroOrOneQuantifier:
                this.CompileStandardType(decl, ImmutableArray.Create(productionIs));
                break;

            case SymbolKind.Group:
                this.CompileStandardType(decl, productionIs.Children);
                break;

            case SymbolKind.Alternation:
                if (productionIs.Children.All(child => child.Kind == SymbolKind.String))
                {
                    this.CompileEnumValueType(decl, productionIs.Children);
                    break;
                }
                else if (productionIs.Children.All(child => child.Kind == SymbolKind.Identifier))
                {
                    this.CompileBaseType(decl, productionIs.Children);
                    break;
                }

                goto default;

            default:
                throw new G4ParseFailureException(production.GetLocation(), Strings.UnrecognizedDataModel, productionName);
            }
        }
        /// <summary>
        /// Initializes a <see cref="DataModelMetadata"/> instance from the given grammar.
        /// </summary>
        /// <exception cref="ArgumentException">
        /// Thrown when the supplied <see cref="GrammarSymbol"/> is not a valid grammar.
        /// </exception>
        /// <param name="grammar">
        /// The grammar from which the <see cref="DataModelMetadata"/> shall be generated.
        /// </param>
        /// <returns>
        /// A <see cref="DataModelMetadata"/> containing data from the grammar <paramref name="grammar"/>.
        /// </returns>
        public static DataModelMetadata FromGrammar(GrammarSymbol grammar)
        {
            if (grammar.Kind != SymbolKind.Grammar)
            {
                throw new ArgumentException("FromGrammar requires a grammar.");
            }

            GrammarSymbol decl        = grammar.Children[0];
            GrammarSymbol grammarId   = decl.Children[0];
            string        grammarName = grammarId.GetLogicalText();

            System.Collections.Immutable.ImmutableArray <Annotation> annotations = grammarId.Annotations;
            string nameSpace = annotations.GetAnnotationValue("namespace") ?? grammarName;

            return(new DataModelMetadata(grammarName, nameSpace, annotations.HasAnnotation("generateLocations"), !annotations.HasAnnotation("noGenerateEquals")));
        }
        private static List <string> GetStringValuesAfter(ImmutableArray <GrammarSymbol> groupMemberList, int idx)
        {
            var result = new List <string>();

            for (; idx < groupMemberList.Length; ++idx)
            {
                GrammarSymbol current = groupMemberList[idx];
                if (current.Kind != SymbolKind.String)
                {
                    break;
                }

                result.Add(current.GetLogicalText());
            }

            return(result);
        }
 private void CompileBaseType(GrammarSymbol decl, ImmutableArray <GrammarSymbol> children)
 {
     _compiledBases.Add(decl.GetLogicalText(),
                        new DataModelBaseTypeBuilder(decl, children.Select(child => child.GetLogicalText())));
 }
Ejemplo n.º 6
0
        internal void AddMember(GrammarSymbol declSymbol, int rank, bool required, string delimeter = null)
        {
            string declaredName   = declSymbol.GetLogicalText();
            string annotationName = declSymbol.Annotations.GetAnnotationValue("name");
            string pattern        = declSymbol.Annotations.GetAnnotationValue("pattern");
            string minimum        = declSymbol.Annotations.GetAnnotationValue("minimum");
            string minItems       = declSymbol.Annotations.GetAnnotationValue("minItems");
            string uniqueItems    = declSymbol.Annotations.GetAnnotationValue("uniqueItems");
            string defaultValue   = declSymbol.Annotations.GetAnnotationValue("default");
            string cSharpName     = annotationName ?? LinguisticTransformer.ToCSharpName(declaredName);
            string serializedName = declSymbol.Annotations.GetAnnotationValue("serializedName");

            if (serializedName == null)
            {
                serializedName = LinguisticTransformer.ToJsonName(cSharpName);
            }

            if (serializedName == null)
            {
                serializedName = LinguisticTransformer.ToJsonName(declaredName);
            }

            string argumentName = declSymbol.Annotations.GetAnnotationValue("argumentName");

            if (argumentName == null)
            {
                if (annotationName == null)
                {
                    argumentName = LinguisticTransformer.ToArgumentName(declaredName);
                }
                else
                {
                    argumentName = LinguisticTransformer.ToArgumentName(annotationName);
                }
            }

            foreach (DataModelMember existingMember in this.Members)
            {
                if (existingMember.CSharpName == cSharpName)
                {
                    throw new G4ParseFailureException(declSymbol.GetLocation(), Strings.DuplicateCSharpMemberName, cSharpName);
                }

                if (existingMember.SerializedName == serializedName)
                {
                    throw new G4ParseFailureException(declSymbol.GetLocation(), Strings.DuplicateJsonMemberName, serializedName);
                }

                if (existingMember.ArgumentName == argumentName)
                {
                    throw new G4ParseFailureException(declSymbol.GetLocation(), Strings.DuplicateArgumentName, argumentName);
                }
            }

            DataModelMember newMember = new DataModelMember(
                declaredName,
                cSharpName,
                serializedName,
                declSymbol.Annotations.GetAnnotationValue("summary"),
                argumentName,
                pattern,
                minimum,
                minItems,
                uniqueItems,
                defaultValue,
                rank,
                required
                );


            this.Members.Add(newMember);
            this.ToStringEntries.Add(new ToStringEntry(delimeter, newMember));
        }