Beispiel #1
0
        private int _getInsertIndex(ParserArray confElement)
        {
            if (_writeArrays.Count == 0)
            {
                return(_allLines.Count + 1);
            }

            var last = _writeArrays.LastOrDefault(p => !p.Added);

            if (last == null)
            {
                return(_allLines.Count + 1);
            }

            int lineIndex = last.Line + last.Length;

            int arrayIndex = _writeArrays.IndexOf(confElement);

            arrayIndex++;

            while (arrayIndex < _writeArrays.Count && _writeArrays[arrayIndex].Added)
            {
                arrayIndex++;
            }

            if (arrayIndex < _writeArrays.Count - 1)
            {
                return(_writeArrays[arrayIndex].Line);
            }

            return(lineIndex);
        }
Beispiel #2
0
        public void Delete(string sKey)
        {
            if (!_list)
            {
                ParserKeyValue conf = _writeKeyValuesFind(sKey);

                if (conf != null)
                {
                    for (int i = 0; i < conf.Length; i++)
                    {
                        _allLines[i + conf.Line - 1] = null;
                    }
                }
            }
            else
            {
                ParserArray conf = _writeArraysFind(sKey);

                if (conf != null)
                {
                    for (int i = 0; i < conf.Length; i++)
                    {
                        _allLines[i + conf.Line - 1] = null;
                    }
                }
            }
        }
Beispiel #3
0
        public void Delete <TKey>(TKey key)
        {
            var sKey = key.ToString();

            if (!_list)
            {
                ParserKeyValue conf = _writeKeyValues.FirstOrDefault(p => p.Key == sKey);

                if (conf != null)
                {
                    for (int i = 0; i < conf.Length; i++)
                    {
                        _allLines[i + conf.Line - 1] = null;
                    }
                }
            }
            else
            {
                ParserArray conf = _writeArrays.FirstOrDefault(p => p[_idKey] == sKey);

                if (conf != null)
                {
                    for (int i = 0; i < conf.Length; i++)
                    {
                        _allLines[i + conf.Line - 1] = null;
                    }
                }
            }
        }
Beispiel #4
0
        public void Write(string key, string line)
        {
            if (!_list)
            {
                ParserKeyValue conf = _writeKeyValues.FirstOrDefault(p => p.Key == key);

                if (conf == null)
                {
                    // Add a new one!
                    _writeKeyValues.Add(new ParserKeyValue(key, Int32.MaxValue)
                    {
                        Value  = new ParserString(line, Int32.MaxValue),
                        Added  = true,
                        Length = 1
                    });

                    return;
                }

                conf.Modified = true;
                conf.Value    = new ParserString(line, conf.Value.Line);
            }
            else
            {
                ParserArray conf = _writeArrays.FirstOrDefault(p => p[_idKey] == key);

                if (conf == null)
                {
                    _writeArrays.Add(new ParserArray(Int32.MaxValue)
                    {
                        Objects = new List <ParserObject> {
                            new ParserKeyValue(_idKey, Int32.MaxValue)
                            {
                                Value = new ParserString(key, Int32.MaxValue),
                            },
                            new ParserKeyValue("Content__", Int32.MaxValue)
                            {
                                Value = new ParserString(line, Int32.MaxValue),
                            }
                        },
                        Added  = true,
                        Length = 1
                    });

                    return;
                }

                conf.Modified = true;
                conf.Objects  = new List <ParserObject> {
                    new ParserKeyValue(_idKey, Int32.MaxValue)
                    {
                        Value = new ParserString(key, Int32.MaxValue),
                    },
                    new ParserKeyValue("Content__", Int32.MaxValue)
                    {
                        Value = new ParserString(line, Int32.MaxValue),
                    }
                };
            }
        }
Beispiel #5
0
        private void _readNode(YamlFileData file, ParserObject parent, int indent, YamlListType listType)
        {
            string word_s = null;

            while (file.CanRead)
            {
                char c = file.PeekChar();

                switch (c)
                {
                case '#':
                    file.SkipLine();
                    continue;

                case '\r':                              // Ignore character
                    file.Position++;
                    continue;

                case '\n':
                    file.Position++;
                    file.NextLine();
                    continue;

                case '-':
                    file.SetupParent(parent, indent);

                    if (listType == YamlListType.NotDefined)
                    {
                        listType = YamlListType.Array;
                    }

                    // Validate parent indent
                    switch (parent.ParserType)
                    {
                    case ParserTypes.List:
                    case ParserTypes.Array:
                        if (file.CurrentLineIndent < parent.ChildrenIndent)
                        {
                            return;
                        }

                        if (file.CurrentLineIndent > parent.ChildrenIndent)
                        {
                            throw file.GetException("Unexpected indent (parent indent: " + parent.Indent + ", parent child indent: " + parent.ChildrenIndent + ", current indent: " + file.CurrentLineIndent + ").");
                        }

                        break;
                    }

                    if (!(file.Position + 2 < file.Length && file.Data[file.Position + 1] == ' ' && file.IsLetter((char)file.Data[file.Position + 2])))
                    {
                        throw file.GetException("Expected a space after the hyphen for the list declaration.");
                    }

                    // Array declaration
                    ParserArray array = new ParserArray(file.LineNumber);
                    array.Indent            = file.CurrentLineIndent;
                    file.CurrentLineIndent += 2;
                    array.ChildrenIndent    = file.CurrentLineIndent;
                    file.Position          += 2;

                    _readNode(file, array, file.CurrentLineIndent, YamlListType.NotDefined);

                    switch (parent.ParserType)
                    {
                    case ParserTypes.List:
                        ((ParserList)parent).AddElement(array);
                        break;

                    case ParserTypes.Array:                                     // Used for copy pasting only
                        ((ParserArray)parent).AddElement(array);
                        break;

                    default:
                        throw file.GetException("Unexpected parent node type. It can either be a list or an array, found a '" + parent.ParserType + "'.");
                    }

                    continue;

                case ' ':
                    file.CurrentLineIndent++;
                    file.Position++;
                    continue;

                case ':':
                    if (string.IsNullOrEmpty(word_s))
                    {
                        throw file.GetException("Missing declaration key before ':'.");
                    }

                    file.Position++;
                    file.Trim();

                    ParserKeyValue keyValue = new ParserKeyValue(word_s, file.LineNumber);
                    keyValue.Indent = parent.Indent;
                    word_s          = null;

                    // List declaration
                    if (file.EoL())
                    {
                        ParserList list = new ParserList(file.LineNumber);
                        list.Indent         = -1;
                        list.ChildrenIndent = -1;
                        keyValue.Value      = list;

                        _readNode(file, list, file.CurrentLineIndent, YamlListType.NotDefined);
                    }
                    else if (file.Data[file.Position] == '[')                               // Aggregate parsing, does not support multi-line
                    {
                        file.Position++;
                        ParserAggregate aggregate = new ParserAggregate(file.LineNumber);
                        aggregate.Parent = parent;

                        while (file.CanRead)
                        {
                            c = file.PeekChar();

                            if (c == '\r' || c == '\n')
                            {
                                throw file.GetException("Unexpected syntax; multi-line aggregate arrays are not supported.");
                            }

                            if (c == ']')
                            {
                                break;
                            }

                            word_s = c == '\"' ? file.ReadValue() : file.ReadWord();
                            aggregate.AddElement(new ParserString(word_s.Trim(' '), file.LineNumber));
                            c = file.PeekChar();

                            while (c != '\n' && (c == ',' || c == ' ' || c == '\r'))
                            {
                                file.Position++;
                                c = file.PeekChar();
                            }
                        }

                        word_s         = null;
                        keyValue.Value = aggregate;
                    }
                    else                                // KeyValue, get the line number first!
                    {
                        var parserString = new ParserString(null, file.LineNumber);
                        parserString.Value  = file.ReadValue();
                        parserString.Length = file.ValueLength;
                        keyValue.Value      = parserString;
                    }

                    switch (parent.ParserType)
                    {
                    case ParserTypes.List:
                        ((ParserList)parent).AddElement(keyValue);
                        break;

                    case ParserTypes.Array:
                        ((ParserArray)parent).AddElement(keyValue);
                        break;
                    }

                    continue;

                default:
                    file.SetupParent(parent, indent);

                    if (listType == YamlListType.NotDefined)
                    {
                        listType = YamlListType.KeyValue;
                    }

                    // Validate parent indent
                    switch (parent.ParserType)
                    {
                    case ParserTypes.List:
                        if (file.CurrentLineIndent < parent.ChildrenIndent)
                        {
                            return;
                        }

                        if (file.CurrentLineIndent == parent.ChildrenIndent && listType != YamlListType.KeyValue)
                        {
                            return;
                        }

                        if (file.CurrentLineIndent > parent.ChildrenIndent)
                        {
                            throw file.GetException("Unexpected indent while reading key (parent indent: " + parent.Indent + ", parent child indent: " + parent.ChildrenIndent + ", current indent: " + file.CurrentLineIndent + ").");
                        }

                        break;

                    case ParserTypes.Array:
                        if (file.CurrentLineIndent < parent.ChildrenIndent)
                        {
                            return;
                        }

                        if (file.CurrentLineIndent > parent.ChildrenIndent)
                        {
                            throw file.GetException("Unexpected indent while reading key (parent indent: " + parent.Indent + ", parent child indent: " + parent.ChildrenIndent + ", current indent: " + file.CurrentLineIndent + ").");
                        }

                        break;
                    }

                    word_s = file.ReadKey();

                    if (word_s.Length == 0)
                    {
                        throw file.GetException("Null-length word. This is most likely caused by an unexpected character in a string.");
                    }

                    file.Trim();
                    continue;
                }
            }
        }
Beispiel #6
0
        public void Write2(string key, string line)
        {
            if (!_list)
            {
                ParserKeyValue conf = _writeKeyValues.FirstOrDefault(p => p.Key == key);

                if (conf == null)
                {
                    // Add a new one!
                    _writeKeyValues.Add(new ParserKeyValue(key, Int32.MaxValue)
                    {
                        Value  = new ParserString(line, Int32.MaxValue),
                        Added  = true,
                        Length = 1
                    });

                    return;
                }

                // Erase what's there first
                ParserArray array = (ParserArray)conf.Value;

                if (array != null)
                {
                    for (int i = 0; i < array.Length; i++)
                    {
                        _allLines[i + array.Line - 1] = null;
                    }
                }

                conf.Modified = true;
                conf.Value    = new ParserString(line, conf.Value.Line);
            }
            else
            {
                ParserArray conf = _writeArrays.FirstOrDefault(p => p[_idKey] == key);

                if (conf == null)
                {
                    _writeArrays.Add(new ParserArray(Int32.MaxValue)
                    {
                        Objects = new List <ParserObject> {
                            new ParserKeyValue(_idKey, Int32.MaxValue)
                            {
                                Value = new ParserString(key, Int32.MaxValue),
                            },
                            new ParserKeyValue("Content__", Int32.MaxValue)
                            {
                                Value = new ParserString(line, Int32.MaxValue),
                            }
                        },
                        Added  = true,
                        Length = 1
                    });

                    return;
                }

                conf.Modified = true;
                conf.Objects  = new List <ParserObject> {
                    new ParserKeyValue(_idKey, Int32.MaxValue)
                    {
                        Value = new ParserString(key, Int32.MaxValue),
                    },
                    new ParserKeyValue("Content__", Int32.MaxValue)
                    {
                        Value = new ParserString(line, Int32.MaxValue),
                    }
                };
            }
        }
Beispiel #7
0
        private void _parse(byte[] buffer)
        {
            try {
                _buffer      = buffer;
                _bufferIndex = 0;

                while (_bufferIndex < buffer.Length)
                {
                    switch ((char)buffer[_bufferIndex])
                    {
                    case '/':
                        if (buffer[_bufferIndex + 1] == '/')
                        {
                            _skipLine();
                            continue;
                        }

                        if (buffer[_bufferIndex + 1] == '*')
                        {
                            _skipCommentBlock();
                            continue;
                        }
                        break;

                    case ':':
                        ParserKeyValue keyValue = new ParserKeyValue(_word, Line);

                        if (_latest == null && Output != null)
                        {
                            // The file contains multiple arrays
                            ParserArray tempList = new ParserArray(Line);
                            tempList.AddElement(Output);
                            Output.Parent = tempList;
                            Output        = tempList;
                            _latest       = tempList;
                        }

                        if (_latest == null)
                        {
                            Output  = keyValue;
                            _latest = Output;
                        }
                        else
                        {
                            keyValue.Parent = _latest;

                            switch (_latest.ParserType)
                            {
                            case ParserTypes.Array:
                                ((ParserArray)_latest).AddElement(keyValue);
                                _latest = keyValue;
                                break;

                            default:
                                throw new Exception("Expected an Array.");
                            }
                        }

                        break;

                    case '<':
                        _readMultilineQuote();

                        if (_latest != null)
                        {
                            switch (_latest.ParserType)
                            {
                            case ParserTypes.KeyValue:
                                ((ParserKeyValue)_latest).Value = new ParserString(_word, Line);
                                _latest.Length = Line - _latest.Line + 1;
                                _latest        = _latest.Parent;
                                break;

                            default:
                                throw new Exception("Expected a KeyValue.");
                            }
                        }
                        break;

                    case '(':
                        ParserList list = new ParserList(Line);

                        if (_latest == null)
                        {
                            throw new Exception("Trying to open a List type without a parent.");
                        }

                        switch (_latest.ParserType)
                        {
                        case ParserTypes.List:
                            ((ParserList)_latest).AddElement(list);
                            list.Parent = _latest;
                            _latest     = list;
                            break;

                        case ParserTypes.KeyValue:
                            ((ParserKeyValue)_latest).Value = list;
                            list.Parent = _latest;
                            _latest     = list;
                            break;

                        default:
                            throw new Exception("Expected a KeyValue.");
                        }

                        break;

                    case '{':
                        ParserArray array = new ParserArray(Line);

                        if (_latest == null)
                        {
                            // Used for copy pasting inputs, create a temporary list
                            Output = new ParserKeyValue("copy_paste", Line)
                            {
                                Value = new ParserList(Line)
                            };

                            _latest = Output["copy_paste"];
                        }

                        switch (_latest.ParserType)
                        {
                        case ParserTypes.List:
                            ((ParserList)_latest).AddElement(array);
                            array.Parent = _latest;
                            _latest      = array;
                            break;

                        case ParserTypes.KeyValue:
                            ((ParserKeyValue)_latest).Value = array;
                            array.Parent = _latest;
                            _latest      = array;
                            break;

                        default:
                            throw new Exception("Expected a List.");
                        }

                        break;

                    case '[':
                        ParserAggregate aggregate = new ParserAggregate(Line);

                        if (_latest == null)
                        {
                            throw new Exception("Trying to open an Aggregate type without a parent.");
                        }

                        switch (_latest.ParserType)
                        {
                        case ParserTypes.KeyValue:
                            ((ParserKeyValue)_latest).Value = aggregate;
                            _latest.Length   = Line - _latest.Line + 1;
                            aggregate.Parent = _latest;
                            _latest          = aggregate;
                            break;

                        default:
                            throw new Exception("Expected a KeyValue.");
                        }

                        break;

                    case ']':
                    case ')':
                    case '}':
                        if (_latest == null)
                        {
                            throw new Exception("Trying to close a statement without knowing its beginning.");
                        }

                        switch (_latest.ParserType)
                        {
                        case ParserTypes.Aggregate:
                        case ParserTypes.Array:
                        case ParserTypes.List:
                            _latest.Length = Line - _latest.Line + 1;
                            _latest        = _latest.Parent;

                            if (_latest is ParserKeyValue)
                            {
                                _latest = _latest.Parent;
                            }
                            break;

                        case ParserTypes.KeyValue:
                            _latest        = _latest.Parent;
                            _latest.Length = Line - _latest.Line + 1;
                            break;

                        default:
                            throw new Exception("Expected a KeyValue or an Array.");
                        }

                        break;

                    case '\"':
                        _readQuote();

                        if (_latest == null)
                        {
                            throw new Exception("Trying to read a quote without a parent.");
                        }

                        switch (_latest.ParserType)
                        {
                        case ParserTypes.KeyValue:
                            ((ParserKeyValue)_latest).Value = new ParserString(_word, Line);
                            _latest.Length = Line - _latest.Line + 1;
                            _latest        = _latest.Parent;
                            break;

                        case ParserTypes.List:
                            ((ParserList)_latest).AddElement(new ParserString(_word, Line));
                            break;

                        default:
                            throw new Exception("Expected a KeyValue.");
                        }

                        continue;

                    case ',':
                    case '\t':
                    case ' ':
                    case '\r':
                        break;

                    case '\n':
                        Line++;
                        break;

                    default:
                        _readWord();

                        if (_word == "")
                        {
                            throw new Exception("Null-length word. This is most likely caused by an unexpected character in a string.");
                        }

                        if (_buffer[_bufferIndex] == ':')
                        {
                            continue;
                        }

                        if (_latest != null)
                        {
                            switch (_latest.ParserType)
                            {
                            case ParserTypes.KeyValue:
                                ((ParserKeyValue)_latest).Value = new ParserString(_word, Line);
                                _latest.Length = Line - _latest.Line + 1;
                                _latest        = _latest.Parent;
                                break;

                            case ParserTypes.List:
                            case ParserTypes.Aggregate:
                                ((ParserArrayBase)_latest).AddElement(new ParserString(_word, Line));
                                break;

                            default:
                                // It will be handled by the ':' parsing.
                                break;
                            }
                        }

                        continue;
                    }

                    _bufferIndex++;
                }
            }
            catch (Exception err) {
                throw new Exception("Failed to parse " + _file + " at line " + Line + ", position " + LinePosition, err);
            }
        }