Ejemplo n.º 1
0
        MessageAst ParseMsg(bool endWithEOF)
        {
            MessageAst res = null;

            while (true)
            {
                if (_curToken == _errorToken)
                {
                    return(BuildError());
                }
                if (_curToken == _eOFToken)
                {
                    if (endWithEOF)
                    {
                        if (res == null)
                        {
                            return(new TextAst(""));
                        }
                        return(res);
                    }
                    return(BuildError("Unexpected end of message missing \"}\""));
                }
                MessageAst val = null;
                if (_curToken == _openBracketToken)
                {
                    AdvanceNextToken();
                    val = ParseFormat();
                }
                else if (_curToken == _hashToken)
                {
                    AdvanceNextToken();
                    val = new HashAst();
                }
                else if (_curToken == _closeBracketToken)
                {
                    if (endWithEOF)
                    {
                        return(BuildError("Unexpected \"}\". Maybe you forgot to prefix it with \"\\\"."));
                    }
                    AdvanceNextToken();
                    if (res == null)
                    {
                        return(new TextAst(""));
                    }
                    return(res);
                }
                else
                {
                    _sb.Clear();
                    while (_curToken >= 0)
                    {
                        AppendCurTokenToSb();
                        AdvanceNextToken();
                    }
                    val = new TextAst(_sb.ToString());
                }
                if (IsError(val))
                {
                    return(val);
                }
                if (res == null)
                {
                    res = val;
                }
                else
                {
                    if (res is ListAst)
                    {
                        ((ListAst)res).Add(val);
                    }
                    else
                    {
                        res = new ListAst(res, val);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        MessageAst ParseMsg(int endWithEOF)
        {
            MessageAst?res          = null;
            var        wrapByConcat = false;

            MessageAst Normalize(MessageAst?res)
            {
                if (res == null)
                {
                    return(new TextAst(""));
                }
                if (wrapByConcat && (res is ListAst list))
                {
                    return(new ConcatAst(list));
                }
                return(res);
            }

            while (true)
            {
                if (_curToken == _errorToken)
                {
                    return(BuildError());
                }

                if (_curToken == _eOFToken)
                {
                    if (endWithEOF == -2)
                    {
                        return(Normalize(res));
                    }

                    if (endWithEOF >= 0)
                    {
                        return(BuildError("Unexpected end of message. Missing end of element {/" + endWithEOF + "}"));
                    }

                    return(BuildError("Unexpected end of message. Missing \"}\""));
                }

                MessageAst val;
                if (_curToken == _openBracketToken)
                {
                    AdvanceNextToken();
                    val = ParseFormat();
                    if (val is StartElAst start)
                    {
                        var nested = ParseMsg(start.Id);
                        if (IsError(nested))
                        {
                            return(nested);
                        }
                        wrapByConcat = true;
                        val          = new ElAst {
                            Id = start.Id, Value = nested
                        };
                    }
                    else if (val is CloseElAst close)
                    {
                        if (close.Id == endWithEOF)
                        {
                            return(Normalize(res));
                        }

                        return(BuildError($"Missing {{/{endWithEOF}}}, got {{/{close.Id}}} instead."));
                    }
                    else if (val is ElAst)
                    {
                        wrapByConcat = true;
                    }
                }
                else if (_curToken == _hashToken)
                {
                    AdvanceNextToken();
                    val = new HashAst();
                }
                else if (_curToken == _closeBracketToken)
                {
                    if (endWithEOF != -1)
                    {
                        return(BuildError("Unexpected \"}\". Maybe you forgot to prefix it with \"\\\"."));
                    }

                    AdvanceNextToken();
                    return(Normalize(res));
                }
                else
                {
                    _sb.Clear();
                    while (_curToken >= 0)
                    {
                        AppendCurTokenToSb();
                        AdvanceNextToken();
                    }

                    val = new TextAst(_sb.ToString());
                }

                if (IsError(val))
                {
                    return(val);
                }
                if (res == null)
                {
                    res = val;
                }
                else
                {
                    if (res is ListAst)
                    {
                        ((ListAst)res).Add(val);
                    }
                    else
                    {
                        res = new ListAst(res, val);
                    }
                }
            }
        }