public string ExtractName(LUFileParser.NewEntitySectionContext parseTree)
        {
            var entityName = string.Empty;

            if (parseTree.newEntityDefinition().newEntityLine().newEntityName() != null)
            {
                entityName = parseTree.newEntityDefinition().newEntityLine().newEntityName().GetText().Trim();
            }
            else if (parseTree.newEntityDefinition().newEntityLine().newEntityNameWithWS() != null)
            {
                entityName = parseTree.newEntityDefinition().newEntityLine().newEntityNameWithWS().GetText().Trim();
            }
            else
            {
                Errors.Add(
                    Diagnostic.BuildDiagnostic(
                        message: "Invalid entity line, did you miss entity name after @",
                        context: parseTree.newEntityDefinition().newEntityLine()));
            }

            if (!string.IsNullOrEmpty(entityName) && entityName.IndexOfAny(_invalidCharsInIntentOrEntityName) >= 0)
            {
                Errors.Add(
                    Diagnostic.BuildDiagnostic(
                        message: $"Invalid entity line, entity name {entityName} cannot contain any of the following characters: [<, >, *, %, &, :, \\, $]",
                        context: parseTree.newEntityDefinition().newEntityLine()));
                return(null);
            }
            else
            {
                return(entityName);
            }
        }
        public List <string> ExtractPhraseList(LUFileParser.NewEntitySectionContext parseTree)
        {
            var synonymsOrPhraseList = new List <string>();

            if (parseTree.newEntityDefinition().newEntityListbody() != null)
            {
                foreach (var errorItemStr in parseTree.newEntityDefinition().newEntityListbody().errorString())
                {
                    if (!string.IsNullOrEmpty(errorItemStr.GetText().Trim()))
                    {
                        Errors.Add(
                            Diagnostic.BuildDiagnostic(
                                message: "Invalid list entity line, did you miss '-' at line begin?",
                                context: errorItemStr));
                    }
                }

                foreach (var normalItemStr in parseTree.newEntityDefinition().newEntityListbody().normalItemString())
                {
                    synonymsOrPhraseList.Add(normalItemStr.GetText());
                }
            }

            if (!string.IsNullOrEmpty(Type) && Type.IndexOf('=') > -1 && synonymsOrPhraseList.Count == 0)
            {
                var errorMsg = $"no synonyms list found for list entity definition: \"{parseTree.newEntityDefinition().newEntityLine().GetText()}\"";
                var error    = Diagnostic.BuildDiagnostic(
                    message: errorMsg,
                    context: parseTree.newEntityDefinition().newEntityLine(),
                    severity: Diagnostic.WARN);
                Errors.Add(error);
            }

            return(synonymsOrPhraseList);
        }
        public string ExtractType(LUFileParser.NewEntitySectionContext parseTree)
        {
            if (parseTree.newEntityDefinition().newEntityLine().newEntityType() != null)
            {
                return(parseTree.newEntityDefinition().newEntityLine().newEntityType().GetText().Trim());
            }

            return(null);
        }
        public string ExtractFeatures(LUFileParser.NewEntitySectionContext parseTree)
        {
            if (parseTree.newEntityDefinition().newEntityLine().newEntityUsesFeatures() != null)
            {
                return(parseTree.newEntityDefinition().newEntityLine().newEntityUsesFeatures().newEntityRoleOrFeatures().GetText().Trim());
            }

            return(null);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="NewEntitySection"/> class.
        /// </summary>
        /// <param name="parseTree">The new entity context from the parse tree.</param>
        public NewEntitySection(LUFileParser.NewEntitySectionContext parseTree)
        {
            if (parseTree == null)
            {
                throw new ArgumentNullException(nameof(parseTree));
            }

            SectionType         = SectionType.NewEntitySection;
            Errors              = new List <Error>();
            Name                = ExtractName(parseTree);
            Type                = ExtractType(parseTree);
            Roles               = ExtractRoles(parseTree);
            Features            = ExtractFeatures(parseTree);
            CompositeDefinition = ExtractCompositeDefinition(parseTree);
            RegexDefinition     = ExtractRegexDefinition(parseTree);
            if (string.Equals(Type, "list", StringComparison.Ordinal))
            {
                SynonymsList = ExtractSynonyms(parseTree);
            }
            else
            {
                ListBody = ExtractPhraseList(parseTree);
            }

            string secTypeStr = $"{SectionType}";

            Id = $"{char.ToLower(secTypeStr[0], System.Globalization.CultureInfo.InvariantCulture) + secTypeStr.Substring(1)}_{Name}";
            var startPosition = new Position {
                Line = parseTree.Start.Line, Character = parseTree.Start.Column
            };
            var stopPosition = new Position {
                Line = parseTree.Stop.Line, Character = parseTree.Stop.Column + parseTree.Stop.Text.Length
            };

            Range = new Range {
                Start = startPosition, End = stopPosition
            };
        }
 /// <summary>
 /// Visit a parse tree produced by <see cref="LUFileParser.newEntitySection"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitNewEntitySection([NotNull] LUFileParser.NewEntitySectionContext context)
 {
     return(VisitChildren(context));
 }
        public List <SynonymElement> ExtractSynonyms(LUFileParser.NewEntitySectionContext parseTree)
        {
            var synonymsOrPhraseList = new List <SynonymElement>();

            if (parseTree.newEntityDefinition().newEntityListbody() != null)
            {
                foreach (var errorItemStr in parseTree.newEntityDefinition().newEntityListbody().errorString())
                {
                    if (!string.IsNullOrEmpty(errorItemStr.GetText().Trim()))
                    {
                        Errors.Add(
                            Diagnostic.BuildDiagnostic(
                                message: "Invalid list entity line, did you miss '-' at line begin?",
                                context: errorItemStr));
                    }
                }

                var bodyElement = new SynonymElement();
                foreach (var normalItemStr in parseTree.newEntityDefinition().newEntityListbody().normalItemString())
                {
                    var trimedItemStr        = normalItemStr.GetText().Trim();
                    var normalizedValueMatch = Regex.Match(trimedItemStr, @"(?: |\t)*-(?: |\t)*(.*)(?: |\t)*:$");
                    if (normalizedValueMatch.Success)
                    {
                        if (bodyElement.NormalizedValue != null)
                        {
                            // This is not the first value in the list
                            synonymsOrPhraseList.Add(bodyElement);
                            bodyElement = new SynonymElement();
                        }

                        bodyElement.NormalizedValue = normalizedValueMatch.Groups[1].Value.Trim();
                    }
                    else
                    {
                        var index   = trimedItemStr.IndexOf('-');
                        var synonym = trimedItemStr.Remove(index, 1);
                        bodyElement.Synonyms.Add(synonym.Trim());

                        if (bodyElement.NormalizedValue == null)
                        {
                            bodyElement.NormalizedValue = synonym.Trim();
                        }
                    }
                }

                if (bodyElement.NormalizedValue != null)
                {
                    // There was at least one
                    synonymsOrPhraseList.Add(bodyElement);
                }
            }

            if (!string.IsNullOrEmpty(Type) && Type.IndexOf('=') > -1 && synonymsOrPhraseList.Count == 0)
            {
                var errorMsg = $"no synonyms list found for list entity definition: \"{parseTree.newEntityDefinition().newEntityLine().GetText()}\"";
                var error    = Diagnostic.BuildDiagnostic(
                    message: errorMsg,
                    context: parseTree.newEntityDefinition().newEntityLine(),
                    severity: Diagnostic.WARN);
                Errors.Add(error);
            }

            return(synonymsOrPhraseList);
        }
 /// <summary>
 /// Exit a parse tree produced by <see cref="LUFileParser.newEntitySection"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitNewEntitySection([NotNull] LUFileParser.NewEntitySectionContext context)
 {
 }