Beispiel #1
0
        private List <NamedResultValue> ParseResultList(EdgeCondition begin, EdgeCondition end, Span input, out Span rest)
        {
            rest = Span.Empty;
            List <NamedResultValue> list = new List <NamedResultValue>();
            int i = input.Start;

            if (!begin(input, ref i))
            {
                ParseError("Unexpected opening character", input);
                return(null);
            }
            if (end(input, ref i))         // tuple is empty
            {
                rest = input.AdvanceTo(i); // eat through the closing brace
                return(list);
            }
            input = input.AdvanceTo(i);
            var item = ParseResult(input, out rest);

            if (item == null)
            {
                ParseError("Result expected", input);
                return(null);
            }
            list.Add(item);
            input = rest;
            while (!input.IsEmpty && _resultString[input.Start] == ',')
            {
                item = ParseResult(input.Advance(1), out rest);
                if (item == null)
                {
                    ParseError("Result expected", input);
                    return(null);
                }
                list.Add(item);
                input = rest;
            }

            i = input.Start;
            if (!end(input, ref i))    // tuple is not closed
            {
                ParseError("Unexpected list termination", input);
                rest = Span.Empty;
                return(null);
            }
            rest = input.AdvanceTo(i);
            return(list);
        }
Beispiel #2
0
        private ConstValue ParseCString(Span input, out Span rest)
        {
            rest = input;
            StringBuilder output = new StringBuilder();

            if (input.IsEmpty || _resultString[input.Start] != '\"')
            {
                ParseError("Cstring expected", input);
                return(null);
            }
            int  i        = input.Start + 1;
            bool endFound = false;

            for (; i < input.Extent; i++)
            {
                char c = _resultString[i];
                if (c == '\"')
                {
                    // closing quote, so we are (probably) done
                    i++;
                    if ((i < input.Extent) && (_resultString[i] == c))
                    {
                        // double quotes mean we emit a single quote, and carry on
                        ;
                    }
                    else
                    {
                        endFound = true;
                        break;
                    }
                }
                else if (c == '\\')
                {
                    // escaped character
                    c = _resultString[++i];
                    switch (c)
                    {
                    case 'n': c = '\n'; break;

                    case 'r': c = '\r'; break;

                    case 't': c = '\t'; break;

                    default: break;
                    }
                }
                output.Append(c);
            }
            if (!endFound)
            {
                ParseError("CString not terminated", input);
                return(null);
            }
            rest = input.AdvanceTo(i);
            return(new ConstValue(output.ToString()));
        }
Beispiel #3
0
        /// <summary>
        /// result-record ==> result-class ( "," result )*
        /// </summary>
        /// <param name="output"></param>
        public Results ParseCommandOutput(string output)
        {
            _resultString = output.Trim();
            int         comma = _resultString.IndexOf(',');
            Results     results;
            ResultClass resultClass = ResultClass.None;

            if (comma < 0)
            {
                // no comma, so entire string should be the result class
                results = new Results(ParseResultClass(output), new List <NamedResultValue>());
            }
            else
            {
                resultClass = ParseResultClass(output.Substring(0, comma));
                Span wholeString = new Span(_resultString);
                results = ParseResultList(wholeString.AdvanceTo(comma + 1), resultClass);
            }
            return(results);
        }