示例#1
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 ':':
                        LibconfigKeyValue keyValue = new LibconfigKeyValue(_word, Line);

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

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

                            switch (_latest.ConfType)
                            {
                            case LibconfigTypes.Array:
                                ((LibconfigArray)_latest).AddElement(keyValue);
                                _latest = keyValue;
                                break;

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

                        break;

                    case '<':
                        _readMultilineQuote();

                        if (_latest != null)
                        {
                            switch (_latest.ConfType)
                            {
                            case LibconfigTypes.KeyValue:
                                ((LibconfigKeyValue)_latest).Value = new LibconfigString(_word, Line);
                                _latest.Length = Line - _latest.Line + 1;
                                _latest        = _latest.Parent;
                                break;

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

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

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

                        switch (_latest.ConfType)
                        {
                        case LibconfigTypes.List:
                            ((LibconfigList)_latest).AddElement(list);
                            list.Parent = _latest;
                            _latest     = list;
                            break;

                        case LibconfigTypes.KeyValue:
                            ((LibconfigKeyValue)_latest).Value = list;
                            list.Parent = _latest;
                            _latest     = list;
                            break;

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

                        break;

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

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

                            _latest = Output["copy_paste"];
                        }

                        switch (_latest.ConfType)
                        {
                        case LibconfigTypes.List:
                            ((LibconfigList)_latest).AddElement(array);
                            array.Parent = _latest;
                            _latest      = array;
                            break;

                        case LibconfigTypes.KeyValue:
                            ((LibconfigKeyValue)_latest).Value = array;
                            array.Parent = _latest;
                            _latest      = array;
                            break;

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

                        break;

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

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

                        switch (_latest.ConfType)
                        {
                        case LibconfigTypes.KeyValue:
                            ((LibconfigKeyValue)_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.ConfType)
                        {
                        case LibconfigTypes.Aggregate:
                        case LibconfigTypes.Array:
                        case LibconfigTypes.List:
                            _latest.Length = Line - _latest.Line + 1;
                            _latest        = _latest.Parent;

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

                        case LibconfigTypes.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.ConfType)
                        {
                        case LibconfigTypes.KeyValue:
                            ((LibconfigKeyValue)_latest).Value = new LibconfigString(_word, Line);
                            _latest.Length = Line - _latest.Line + 1;
                            _latest        = _latest.Parent;
                            break;

                        case LibconfigTypes.List:
                            ((LibconfigList)_latest).AddElement(new LibconfigString(_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.ConfType)
                            {
                            case LibconfigTypes.KeyValue:
                                ((LibconfigKeyValue)_latest).Value = new LibconfigString(_word, Line);
                                _latest.Length = Line - _latest.Line + 1;
                                _latest        = _latest.Parent;
                                break;

                            case LibconfigTypes.List:
                            case LibconfigTypes.Aggregate:
                                ((LibconfigArrayBase)_latest).AddElement(new LibconfigString(_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);
            }
        }
示例#2
0
 public void AddElement(LibconfigObject obj)
 {
     Objects.Add(obj);
 }