private static List <ImportSection> ExtractImportSections(LUFileParser.FileContext fileContext)
        {
            if (fileContext == null)
            {
                return(new List <ImportSection>());
            }

            var importSections     = fileContext.paragraph().Select(x => x.importSection()).Where(x => x != null);
            var importSectionsList = importSections.Select(x => new ImportSection(x)).ToList();

            return(importSectionsList);
        }
        private static List <QnaSection> ExtractQnaSections(LUFileParser.FileContext fileContext)
        {
            if (fileContext == null)
            {
                return(new List <QnaSection>());
            }

            var qnaSections     = fileContext.paragraph().Select(x => x.qnaSection()).Where(x => x != null);
            var qnaSectionsList = qnaSections.Select(x => new QnaSection(x)).ToList();

            return(qnaSectionsList);
        }
        private static List <NewEntitySection> ExtractNewEntitiesSections(LUFileParser.FileContext fileContext)
        {
            if (fileContext == null)
            {
                return(new List <NewEntitySection>());
            }

            var newEntitySections     = fileContext.paragraph().Select(x => x.newEntitySection()).Where(x => x != null && x.newEntityDefinition() != null);
            var newEntitySectionsList = newEntitySections.Select(x => new NewEntitySection(x)).ToList();

            return(newEntitySectionsList);
        }
        private static List <SimpleIntentSection> ExtractSimpleIntentSections(LUFileParser.FileContext fileContext)
        {
            if (fileContext == null)
            {
                return(new List <SimpleIntentSection>());
            }

            var simpleIntentSections     = fileContext.paragraph().Select(x => x.simpleIntentSection()).Where(x => x != null && x.intentDefinition() != null);
            var simpleIntentSectionsList = simpleIntentSections.Select(x => new SimpleIntentSection(x)).ToList();

            return(simpleIntentSectionsList);
        }
        private static List <SectionEntity> ExtractEntitiesSections(LUFileParser.FileContext fileContext)
        {
            if (fileContext == null)
            {
                return(new List <SectionEntity>());
            }

            var aux                = fileContext.paragraph();
            var entitySections     = fileContext.paragraph().Select(x => x.entitySection()).Where(x => x != null && x.entityDefinition() != null);
            var entitySectionsList = entitySections.Select(x => new SectionEntity(x)).ToList();

            return(entitySectionsList);
        }
        private static List <ModelInfoSection> ExtractModelInfoSections(LUFileParser.FileContext fileContext)
        {
            if (fileContext == null)
            {
                return(new List <ModelInfoSection>());
            }

            var context           = fileContext;
            var modelInfoSections = context.paragraph().Select(x => x.modelInfoSection()).Where(x => x != null);

            var modelInfoSectionList = modelInfoSections.Select(x => new ModelInfoSection(x)).ToList();

            return(modelInfoSectionList);
        }
 /// <summary>
 /// Visit a parse tree produced by <see cref="LUFileParser.file"/>.
 /// <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 VisitFile([NotNull] LUFileParser.FileContext context)
 {
     return(VisitChildren(context));
 }
        private static LuResource ExtractFileContent(LUFileParser.FileContext fileContent, string content, List <Error> errors)
        {
            var sections = new List <Section>();

            try
            {
                var modelInfoSections = ExtractModelInfoSections(fileContent);
                foreach (var section in modelInfoSections)
                {
                    errors.AddRange(section.Errors);
                }

                sections.AddRange(modelInfoSections);
            }
            catch (Exception err)
            {
                errors.Add(
                    Diagnostic.BuildDiagnostic(
                        message: $"Error happened when parsing model info section: {err.Message}"));
            }

            try
            {
                var isSectionEnabled = IsSectionEnabled(sections);

                var nestedIntentSections = ExtractNestedIntentSections(fileContent);
                foreach (var section in nestedIntentSections)
                {
                    errors.AddRange(section.Errors);
                }

                if (isSectionEnabled)
                {
                    sections.AddRange(nestedIntentSections);
                }
                else
                {
                    foreach (var section in nestedIntentSections)
                    {
                        var emptyIntentSection = new SimpleIntentSection();
                        emptyIntentSection.Name = section.Name;
                        emptyIntentSection.Id   = $"{emptyIntentSection.SectionType}_{emptyIntentSection.Name}";

                        // get the end character index
                        // this is default value
                        // it will be reset in function extractSectionBody()
                        var endCharacter = section.Name.Length + 2;

                        var range = new Range {
                            Start = section.Range.Start, End = new Position {
                                Line = section.Range.Start.Line, Character = endCharacter
                            }
                        };
                        emptyIntentSection.Range = range;
                        var errorMsg = $"no utterances found for intent definition: \"# {emptyIntentSection.Name}\"";
                        var error    = Diagnostic.BuildDiagnostic(
                            message: errorMsg,
                            range: emptyIntentSection.Range,
                            severity: Diagnostic.WARN);

                        errors.Add(error);
                        sections.Add(emptyIntentSection);

                        foreach (var subSection in section.SimpleIntentSections)
                        {
                            sections.Add(subSection);
                            errors.AddRange(subSection.Errors);
                        }
                    }
                }
            }
            catch (Exception err)
            {
                errors.Add(
                    Diagnostic.BuildDiagnostic(
                        message: $"Error happened when parsing nested intent section: {err.Message}"));
            }

            try
            {
                var simpleIntentSections = ExtractSimpleIntentSections(fileContent);
                foreach (var section in simpleIntentSections)
                {
                    errors.AddRange(section.Errors);
                }

                sections.AddRange(simpleIntentSections);
            }
            catch (Exception err)
            {
                errors.Add(
                    Diagnostic.BuildDiagnostic(
                        message: $"Error happened when parsing simple intent section: {err.Message}"));
            }

            try
            {
                var entitySections = ExtractEntitiesSections(fileContent);
                foreach (var section in entitySections)
                {
                    errors.AddRange(section.Errors);
                }

                sections.AddRange(entitySections);
            }
            catch (Exception err)
            {
                errors.Add(
                    Diagnostic.BuildDiagnostic(
                        message: $"Error happened when parsing entities: {err.Message}"));
            }

            try
            {
                var newEntitySections = ExtractNewEntitiesSections(fileContent);
                foreach (var section in newEntitySections)
                {
                    errors.AddRange(section.Errors);
                }

                sections.AddRange(newEntitySections);
            }
            catch (Exception err)
            {
                errors.Add(
                    Diagnostic.BuildDiagnostic(
                        message: $"Error happened when parsing new entities: {err.Message}"));
            }

            try
            {
                var importSections = ExtractImportSections(fileContent);
                foreach (var section in importSections)
                {
                    errors.AddRange(section.Errors);
                }

                sections.AddRange(importSections);
            }
            catch (Exception err)
            {
                errors.Add(
                    Diagnostic.BuildDiagnostic(
                        message: $"Error happened when parsing import section: {err.Message}"));
            }

            try
            {
                var qnaSections = ExtractQnaSections(fileContent);
                foreach (var section in qnaSections)
                {
                    errors.AddRange(section.Errors);
                }

                sections.AddRange(qnaSections);
            }
            catch (Exception err)
            {
                errors.Add(
                    Diagnostic.BuildDiagnostic(
                        message: $"Error happened when parsing qna section: {err.Message}"));
            }

            sections = ReconstructIntentSections(sections);

            ExtractSectionBody(sections, content);
            var result = new LuResource(sections, content, errors);

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