Example #1
0
        /// <summary>
        /// Register structure info (used when file is not being read)
        /// </summary>
        /// <param name="structure">Structure info</param>
        internal void RegisterStructure(StructureInfo structure)
        {
            // crawl all sections
            foreach (var section in structure.Sections)
            {
                // Create section and extract comment
                var newSection = new InnerSection(section.Name, section.DefaultComment);
                knownSections.Add(section.Name, newSection);

                // crawl all options
                foreach (var option in section.Options)
                {
                    // Skip optional options
                    if (option.IsOptional)
                    {
                        continue;
                    }

                    // Create option and extract comment
                    var newOption = new InnerOption(option.Name, null);
                    newOption.Comment = option.DefaultComment;
                    newSection.Options.Add(option.Name, newOption);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Set option info and it's value.
        /// NOTE: Only options which are set via this call can be ADDED into output (readed options are included automatically).
        /// </summary>
        /// <param name="info">Structure describing option format</param>
        /// <param name="value">Structure wearing option value</param>
        internal void SetOption(OptionInfo info, OptionValue value)
        {
            // Check inner constraints
            checkValidity(info, value);

            // obtain qNames
            QualifiedSectionName qSect = value.Name.Section;
            QualifiedOptionName  qOpt  = value.Name;

            // Check that section exists
            if (!knownSections.ContainsKey(qSect))
            {
                throw new ParserException(
                          userMsg: "error, unknown section",
                          developerMsg: "tried to set option in unknown section, sections needs to be retrieved from RegisterStructure() or from the actual file");
            }

            // Ensure option exists
            if (!knownSections[qSect].Options.ContainsKey(qOpt))
            {
                InnerOption newOpt = new InnerOption(qOpt, null);
                knownSections[qSect].Options.Add(qOpt, newOpt);
                newOpt.Comment = info.DefaultComment;
            }

            // along the way, check the comment
            checkOptionCommentAndSeen(info, knownSections);

            // Pass value into option
            InnerOption     curOpt = knownSections[qSect].Options[qOpt];
            IValueConverter converter;

            try
            {
                converter = Converters.ConfigConverters.getConverter(info);
            }
            catch (Exception ex)
            {
                throw new ParserException(userMsg: "Error when deserializing value", developerMsg: "Unsupported value tried to be deserialized", inner: ex);
            }

            if (info.IsContainer)
            {
                curOpt.strValues.Clear();
                foreach (var elem in ConfigRW.ConfigCreation.StructureFactory.GetContainerElements(value.ConvertedValue))
                {
                    curOpt.strValues.Add(converter.Serialize(elem));
                }
            }
            else
            {
                curOpt.strValues.Clear();
                curOpt.strValues.Add(converter.Serialize(value.ConvertedValue));
            }
        }
Example #3
0
        /// <summary>
        /// Parsing process, processing single trimmed FULL-line as option
        /// </summary>
        /// <param name="oneLine">Line to be processed</param>
        /// <param name="curLine">Current line number, for when exception occurs</param>
        /// <param name="curSection">Currently processed section</param>
        /// <param name="knownSections">Dictionary of all pre-parsed sections</param>
        internal static void processSingleLineAsOption(string oneLine, uint curLine, QualifiedSectionName curSection, Dictionary <QualifiedSectionName, InnerSection> knownSections)
        {
            // no section started yet
            if (curSection == null)
            {
                throw new ParserExceptionWithinConfig(
                          userMsg: "Error when parsing file on line " + curLine + " unexpected element outside of section",
                          developerMsg: "Unexpected start of the file on line " + curLine + ", option with no section",
                          line: curLine,
                          section: curSection.ID);
            }

            try
            {
                Lexer lineLexer = new Lexer(oneLine, knownSections);
                var   lexes     = lineLexer.GetLexes();

                var lxName = lexes[0];
                lexes.RemoveAt(0);
                if (lxName.Item1 != Lexer.Lexes.T_Identifier)
                {
                    throw new ParserExceptionWithinConfig(
                              userMsg: "Error when parsing file on line " + curLine,
                              developerMsg: "Lexer process does not provided option identifier",
                              line: curLine,
                              section: curSection.ID);
                }

                QualifiedOptionName qName = new QualifiedOptionName(curSection, lxName.Item2);
                if (knownSections[curSection].Options.ContainsKey(qName))
                {
                    throw new ParserExceptionWithinConfig(
                              userMsg: "Error when parsing file on line " + curLine + ", duplicit option",
                              developerMsg: "Duplicit option ID occurred inside this section",
                              line: curLine,
                              section: curSection.ID,
                              option: qName.ID);
                }

                InnerOption newOpt = new InnerOption(qName, curLine);
                knownSections[curSection].Options.Add(qName, newOpt);

                foreach (var lexeme in lexes)
                {
                    if (lexeme.Item1 == Lexer.Lexes.T_Comment && lexeme == lexes[lexes.Count - 1])
                    {
                        newOpt.Comment = lexeme.Item2;
                    }
                    else if (lexeme.Item1 == Lexer.Lexes.T_ValuePart)
                    {
                        newOpt.strValues.Add(lexeme.Item2);
                    }
                    else
                    {
                        throw new ParserExceptionWithinConfig(
                                  userMsg: "Error when parsing file on line " + curLine,
                                  developerMsg: "unexpected lexeme pop off the lexer",
                                  line: curLine,
                                  section: curSection.ID,
                                  option: qName.ID);
                    }
                }
            }
            catch (ParserException ex)
            {
                throw new ParserExceptionWithinConfig(
                          userMsg: "Error when parsing file on line " + curLine,
                          developerMsg: "Exception ocurred in Lexer process",
                          line: curLine,
                          section: curSection.ID,
                          inner: ex);
            }
            catch (Exception ex)
            {
                throw new ParserExceptionWithinConfig(
                          userMsg: "Error when parsing file on line " + curLine,
                          developerMsg: "Unexpected exception ocurred in Lexer process",
                          line: curLine,
                          section: curSection.ID,
                          inner: ex);
            }
        }
Example #4
0
        /// <summary>
        /// Extracts value from internal representation into final form
        /// internal representation existent and provided to the method
        /// </summary>
        /// <param name="info">Option for which to extract the value</param>
        /// <param name="opt">Inner representaiton of desired option with desired value</param>
        /// <returns>Value in expected format</returns>
        private static object extractValue(OptionInfo info, InnerOption opt)
        {
            // Check container/singleUnit consistenci
            if (!info.IsContainer && opt.strValues.Count > 1)
            {
                throw new ParserExceptionWithinConfig(
                          userMsg: "Error when retrieving expected value for option '{1}::{2}' at line {0}",
                          developerMsg: "internal representation contains more values, while desired is only one for option '{1}::{2}' at line {0}",
                          line: opt.Line,
                          option: info.Name.ID,
                          section: info.Name.Section.ID
                          );
            }

            // Obtain converter
            IValueConverter convertor;

            try
            {
                convertor = Converters.ConfigConverters.getConverter(info);
            }
            catch (Exception ex)
            {
                throw new ParserExceptionWithinConfig(
                          userMsg: "Cannot find convertor, possibly due to invalid type specified for option '{1}::{2}' at line {0}",
                          developerMsg: "Convertor retrieving method thrown an exception, possibly due to invalid type specified for option '{1}::{2}' at line {0}",
                          line: opt.Line,
                          option: info.Name.ID,
                          section: info.Name.Section.ID,
                          inner: ex);
            }

            // Value extraction
            try
            {
                // Container
                if (info.IsContainer)
                {
                    List <object> outElements = new List <object>();
                    foreach (string value in opt.strValues)
                    {
                        outElements.Add(convertor.Deserialize(value));
                    }

                    return(ConfigRW.ConfigCreation.StructureFactory.CreateContainer(info, outElements));
                }
                // Single value
                else
                {
                    return(convertor.Deserialize(opt.strValues.Count > 0 ? opt.strValues[0] : ""));
                }
            }
            catch (Exception ex)
            {
                throw new ParserExceptionWithinConfig(
                          userMsg: "Type conversion failed for container option '{1}::{2}' at line {0}",
                          developerMsg: "An exception occurred during extraction of value from inner representaion for container option '{1}::{2}' at line {0}",
                          line: opt.Line,
                          option: info.Name.ID,
                          section: info.Name.Section.ID,
                          inner: ex);
            }
        }