Ejemplo n.º 1
0
        public override object VisitStructuredTemplateBody([NotNull] LGFileParser.StructuredTemplateBodyContext context)
        {
            var result   = new JObject();
            var typeName = context.structuredBodyNameLine().STRUCTURE_NAME().GetText();

            result[LGType] = typeName;

            var bodys = context.structuredBodyContentLine();

            foreach (var body in bodys)
            {
                var isKVPairBody = body.keyValueStructureLine() != null;
                if (isKVPairBody)
                {
                    // make it insensitive
                    var property = body.keyValueStructureLine().STRUCTURE_IDENTIFIER().GetText().ToLower();
                    var value    = VisitStructureValue(body.keyValueStructureLine());
                    result[property] = JToken.FromObject(value);
                }
                else
                {
                    // When the same property exists in both the calling template as well as callee, the content in caller will trump any content in
                    var propertyObject = JObject.FromObject(EvalExpression(body.objectStructureLine().GetText()));

                    // Full reference to another structured template is limited to the structured template with same type
                    if (propertyObject[LGType] != null && propertyObject[LGType].ToString() == typeName)
                    {
                        foreach (var item in propertyObject)
                        {
                            if (result.Property(item.Key) == null)
                            {
                                result[item.Key] = item.Value;
                            }
                        }
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public override object VisitStructuredTemplateBody([NotNull] LGFileParser.StructuredTemplateBodyContext context)
        {
            var result   = new JObject();
            var typeName = context.structuredBodyNameLine().STRUCTURED_CONTENT().GetText().Trim();

            result["$type"] = typeName;

            var bodys = context.structuredBodyContentLine().STRUCTURED_CONTENT();

            foreach (var body in bodys)
            {
                var line = body.GetText().Trim();

                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                var start = line.IndexOf('=');
                if (start > 0)
                {
                    // make it insensitive
                    var property    = line.Substring(0, start).Trim().ToLower();
                    var originValue = line.Substring(start + 1).Trim();

                    var valueArray = EscapeSeperatorRegex.Split(originValue);
                    if (valueArray.Length == 1)
                    {
                        result[property] = EvalText(originValue);
                    }
                    else
                    {
                        var valueList = new JArray();
                        foreach (var item in valueArray)
                        {
                            valueList.Add(EvalText(item.Trim()));
                        }

                        result[property] = valueList;
                    }
                }
                else if (IsPureExpression(line))
                {
                    // [MyStruct
                    // Text = foo
                    // {ST2()}
                    // ]

                    // When the same property exists in both the calling template as well as callee, the content in caller will trump any content in the callee.
                    var propertyObject = JObject.FromObject(EvalExpression(line));

                    // Full reference to another structured template is limited to the structured template with same type
                    if (propertyObject["$type"] != null && propertyObject["$type"].ToString() == typeName)
                    {
                        foreach (var item in propertyObject)
                        {
                            if (result.Property(item.Key) == null)
                            {
                                result[item.Key] = item.Value;
                            }
                        }
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
            public override List <Diagnostic> VisitStructuredTemplateBody([NotNull] LGFileParser.StructuredTemplateBodyContext context)
            {
                var result = new List <Diagnostic>();

                var typeName = context.structuredBodyNameLine().STRUCTURED_CONTENT().GetText().Trim();

                if (!structuredNameRegex.IsMatch(typeName))
                {
                    result.Add(BuildLGDiagnostic($"Structured type {typeName} is invalid. Letter, number, '_', '-' and '.' is allowed.", context: context.structuredBodyContentLine()));
                }

                if (context.structuredBodyEndLine() == null)
                {
                    result.Add(BuildLGDiagnostic($"structured LG missing ending ']'", context: context.structuredBodyContentLine()));
                }

                var bodys = context.structuredBodyContentLine()?.STRUCTURED_CONTENT();

                if (bodys == null || bodys.Length == 0 || bodys.All(u => string.IsNullOrEmpty(u.GetText())))
                {
                    result.Add(BuildLGDiagnostic($"Structured content is empty", context: context.structuredBodyContentLine()));
                }
                else
                {
                    foreach (var body in bodys)
                    {
                        var line = body.GetText().Trim();
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            var start = line.IndexOf('=');
                            if (start < 0 && !Evaluator.IsPureExpression(line))
                            {
                                result.Add(BuildLGDiagnostic($"Structured content does not support", context: context.structuredBodyContentLine()));
                            }
                            else
                            {
                                if (start > 0)
                                {
                                    var property    = line.Substring(0, start).Trim().ToLower();
                                    var originValue = line.Substring(start + 1).Trim();
                                    var valueArray  = Evaluator.EscapeSeperatorRegex.Split(originValue);

                                    if (valueArray.Length == 1)
                                    {
                                        result.AddRange(CheckText(originValue, context.structuredBodyContentLine()));
                                    }
                                    else
                                    {
                                        foreach (var item in valueArray)
                                        {
                                            result.AddRange(CheckText(item.Trim(), context.structuredBodyContentLine()));
                                        }
                                    }
                                }
                                else if (Evaluator.IsPureExpression(line))
                                {
                                    result.AddRange(CheckExpression(line, context.structuredBodyContentLine()));
                                }
                            }
                        }
                    }
                }

                return(result);
            }
Ejemplo n.º 4
0
 public override AnalyzerResult VisitStructuredTemplateBody([NotNull] LGFileParser.StructuredTemplateBodyContext context)
 {
     // TODO
     return(base.VisitStructuredTemplateBody(context));
 }