示例#1
0
        /// <summary>
        /// Reads representation string and divides it into modules and content in brackets that follows each name
        /// (or null if there is no bracket)
        /// </summary>
        /// <param name="representation">
        /// string containing only module names and possibly one bracket behind each name
        /// </param>
        /// <returns>List of doubles, first item contains module name, second content of it bracket</returns>
        /// <exception cref="ParserException">Thrown if representation doesn't contain valid modules represenation</exception>
        internal List <Tuple <string, string> > SeparateModulesRepresentations(string representation)
        {
            if (representation == null)
            {
                return(new List <Tuple <string, string> >());
            }

            var modules = new List <Tuple <string, string> >();

            try
            {
                int index = 0;
                while (index < representation.Length)
                {
                    string name = HelperMethods.ReadModuleName(representation, index, out index);
                    index++;
                    string details = null;
                    if (index < representation.Length && representation[index] == '(')
                    {
                        details = HelperMethods.ReadBracketContent(representation, index, out index);
                        index++;
                    }

                    modules.Add(new Tuple <string, string>(name, details));
                }

                return(modules);
            }
            catch (ArgumentException)
            {
                throw new ParserException("representation cannot be parsed into modules");
            }
        }
示例#2
0
        /// <summary>
        /// Parser source module ID and param names based on its representation
        /// </summary>
        /// <param name="representation">string containing one module name and up to one bracket with param names</param>
        /// <param name="sourceId">id of module in representation</param>
        /// <param name="paramNames">names of param in bracket if present</param>
        /// <exception cref="ParserException">Thrown if unknown symbols are found</exception>
        internal void ParseSourceModule(string representation, out int sourceId, out string[] paramNames)
        {
            string moduleName = HelperMethods.ReadModuleName(representation, 0, out int endIndex);

            sourceId = _moduleParser.GetModuleId(moduleName);
            endIndex++;

            if (endIndex < representation.Length && representation[endIndex] == '(')
            {
                string param = HelperMethods.ReadBracketContent(representation, endIndex, out endIndex);
                if (endIndex != representation.Length - 1)
                {
                    throw new ParserException("Unknown content at source module at index: " + (endIndex + 1));
                }

                paramNames = param.Split(',');
            }
            else
            {
                paramNames = new string[0];
            }
        }
示例#3
0
        /// <summary>
        /// Creates context condition from text representation
        /// </summary>
        /// <param name="sourceModuleSection">string representing module between left and right context</param>
        /// <param name="leftContextSection">string with names of modules which should be on left side of source</param>
        /// <param name="rightContextSection">string with names of modules which should be on right side of source</param>
        /// <returns>Delegate which returns true if given GenerationIndex meets condition described by provided
        /// representation</returns>
        /// <exception cref="ParserException">Thrown if some module in left or right context contains bracket</exception>
        internal Rule <T> .ContextCondition ParseContextCondition(string sourceModuleSection, string leftContextSection,
                                                                  string rightContextSection)
        {
            int sourceId = _moduleParser.GetModuleId(
                HelperMethods.ReadModuleName(sourceModuleSection, 0, out var tmp));

            var leftContext    = _moduleParser.ParseModules(leftContextSection);
            var leftContextIds = new List <int>();

            foreach (var module in leftContext)
            {
                var details = module.Parameters;
                if (details != null && details.Length > 0)
                {
                    throw new ParserException("Parameters are not allowed in context conditions");
                }

                leftContextIds.Add(module.Id);
            }

            var rightContext    = _moduleParser.ParseModules(rightContextSection);
            var rightContextIds = new List <int>();

            foreach (var module in rightContext)
            {
                var details = module.Parameters;
                if (details != null && details.Length > 0)
                {
                    throw new ParserException("Parameters are not allowed in context conditions");
                }

                rightContextIds.Add(module.Id);
            }

            return(generationIndex =>
            {
                if (generationIndex.Module.Id != sourceId)
                {
                    return false;
                }
                int start = generationIndex.Index;
                //Not enough modules to the left of source module
                if (leftContextIds.Count > start)
                {
                    return false;
                }
                //Not enough modules to the right of source module
                if (rightContextIds.Count > generationIndex.Count - start - 1)
                {
                    return false;
                }

                for (int i = 0; i < leftContextIds.Count; i++)
                {
                    if (generationIndex[start - i - 1].Id != leftContextIds[leftContext.Length - i - 1])
                    {
                        return false;
                    }
                }

                for (int i = 0; i < rightContextIds.Count; i++)
                {
                    if (generationIndex[start + i + 1].Id != rightContextIds[i])
                    {
                        return false;
                    }
                }

                return true;
            });
        }