Beispiel #1
0
        private IEnumerator <Parser> ReadArguments(RantCompiler compiler, TokenReader reader, List <RST> arguments)
        {
            var actions = new List <RST>();

            Action <RST> argActionCallback = action => actions.Add(action);

            compiler.SetNextActionCallback(argActionCallback);
            compiler.AddContext(CompileContext.FunctionEndContext);
            compiler.AddContext(CompileContext.ArgumentSequence);

            while (compiler.NextContext == CompileContext.ArgumentSequence)
            {
                reader.SkipSpace();
                var startToken = reader.PeekToken();
                yield return(Get <SequenceParser>());

                // Don't wrap single nodes in a sequence, it's unnecessary
                if (actions.Count == 1)
                {
                    arguments.Add(actions[0]);
                }
                else
                {
                    arguments.Add(new RstSequence(actions, startToken.ToLocation()));
                }

                actions.Clear();
            }

            compiler.LeaveContext();
        }
Beispiel #2
0
        public override IEnumerator <Parser> Parse(RantCompiler compiler, CompileContext context, TokenReader reader,
                                                   Action <RST> actionCallback)
        {
            Token token;

            while (!reader.End)
            {
                token = reader.ReadToken();

                switch (token.Type)
                {
                case R.LeftAngle:
                    yield return(Get <QueryParser>());

                    break;

                case R.LeftSquare:
                    yield return(Get <TagParser>());

                    break;

                case R.LeftCurly:
                    reader.SkipSpace();
                    yield return(Get <BlockParser>());

                    break;

                case R.Pipe:
                    if (context == CompileContext.BlockSequence)
                    {
                        yield break;
                    }
                    goto default;                             // Print it if we're not in a block

                case R.RightCurly:
                    if (context == CompileContext.BlockSequence)
                    {
                        compiler.LeaveContext();
                        yield break;
                    }
                    compiler.SyntaxError(token, false, "err-compiler-unexpected-token", token.Value);
                    break;

                // end of argument
                case R.Semicolon:
                    if (context == CompileContext.ArgumentSequence)
                    {
                        yield break;
                    }
                    // this is probably just a semicolon in text
                    actionCallback(new RstText(token.Value, token.ToLocation()));
                    break;

                case R.RightSquare:
                    // end of arguments / direct object in query
                    switch (context)
                    {
                    case CompileContext.ArgumentSequence:
                    case CompileContext.SubroutineBody:
                    case CompileContext.QueryComplement:
                        compiler.LeaveContext();
                        yield break;
                    }
                    compiler.SyntaxError(token, false, "err-compiler-unexpected-token", token.Value);
                    break;

                case R.RightAngle:
                    compiler.SyntaxError(token, false, "err-compiler-unexpected-token", token.Value);
                    break;

                // the end of a block weight, maybe
                case R.RightParen:
                    if (context == CompileContext.BlockWeight)
                    {
                        reader.SkipSpace();
                        compiler.LeaveContext();
                        yield break;
                    }
                    actionCallback(new RstText(token.Value, token.ToLocation()));
                    break;

                case R.Whitespace:
                    switch (context)
                    {
                    case CompileContext.BlockSequence:
                        switch (reader.PeekType())
                        {
                        case R.Pipe:
                        case R.RightCurly:
                            continue;                                                     // Ignore whitespace at the end of block elements
                        }
                        goto default;

                    default:
                        actionCallback(new RstText(token.Value, token.ToLocation()));
                        break;
                    }
                    break;

                case R.EscapeSequenceChar:                         // Handle escape sequences
                    actionCallback(new RstEscape(token.ToLocation(), 1, false, token.Value[0]));
                    break;

                case R.EscapeSequenceUnicode:
                {
                    short codePoint;
                    if (!short.TryParse(token.Value, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out codePoint))
                    {
                        compiler.SyntaxError(reader.PrevToken, false, "err-compiler-invalid-escape-unicode", reader.PrevToken.Value);
                        break;
                    }
                    actionCallback(new RstEscape(token.ToLocation(), 1, true, Convert.ToChar(codePoint)));
                    break;
                }

                case R.EscapeSequenceSurrogatePair:
                {
                    uint surrogatePairCodePoint;
                    if (!uint.TryParse(token.Value, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out surrogatePairCodePoint) ||
                        surrogatePairCodePoint < 0x10000)
                    {
                        compiler.SyntaxError(reader.PrevToken, false, "err-compiler-invalid-escape-surrogate", token.Value);
                        break;
                    }

                    surrogatePairCodePoint -= 0x10000;
                    ushort highCodePoint = (ushort)(0xD800 + ((surrogatePairCodePoint & 0xFFC00) >> 10));
                    ushort lowCodePoint = (ushort)(0xDC00 + (surrogatePairCodePoint & 0x003FF));
                    char   low, high;
                    if (!char.IsSurrogatePair(high = Convert.ToChar(highCodePoint), low = Convert.ToChar(lowCodePoint)))
                    {
                        compiler.SyntaxError(reader.PrevToken, false, "err-compiler-invalid-escape-surrogate", token.Value);
                        break;
                    }
                    actionCallback(new RstEscape(token.ToLocation(), 1, true, high, low));
                    break;
                }

                case R.EscapeSequenceQuantifier:
                {
                    if (!Util.ParseInt(token.Value, out int quantity) || quantity <= 0)
                    {
                        compiler.SyntaxError(token, false, "err-compiler-escape-bad-quantity");
                        break;
                    }
                    switch (reader.PeekType())
                    {
                    case R.EscapeSequenceChar:
                        actionCallback(new RstEscape(token.ToLocation(), quantity, false, reader.ReadToken().Value[0]));
                        break;

                    case R.EscapeSequenceUnicode:
                    {
                        short codePoint;
                        if (!short.TryParse(reader.ReadToken().Value, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out codePoint))
                        {
                            compiler.SyntaxError(reader.PrevToken, false, "err-compiler-invalid-escape-unicode", reader.PrevToken.Value);
                            break;
                        }
                        actionCallback(new RstEscape(token.ToLocation(), quantity, true, Convert.ToChar(codePoint)));
                        break;
                    }

                    case R.EscapeSequenceSurrogatePair:
                    {
                        var  pairToken = reader.ReadToken();
                        uint surrogatePairCodePoint;
                        if (!uint.TryParse(pairToken.Value, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out surrogatePairCodePoint) ||
                            surrogatePairCodePoint < 0x10000)
                        {
                            compiler.SyntaxError(reader.PrevToken, false, "err-compiler-invalid-escape-surrogate", pairToken.Value);
                            break;
                        }

                        surrogatePairCodePoint -= 0x10000;
                        ushort highCodePoint = (ushort)(0xD800 + ((surrogatePairCodePoint & 0xFFC00) >> 10));
                        ushort lowCodePoint = (ushort)(0xDC00 + (surrogatePairCodePoint & 0x3FF));
                        char   low, high;
                        if (!char.IsSurrogatePair(high = Convert.ToChar(highCodePoint), low = Convert.ToChar(lowCodePoint)))
                        {
                            compiler.SyntaxError(reader.PrevToken, false, "err-compiler-invalid-escape-surrogate", pairToken.Value);
                            break;
                        }
                        actionCallback(new RstEscape(token.ToLocation(), quantity, true, high, low));
                        break;
                    }
                    }
                    break;
                }

                case R.EOF:
                    if (context != CompileContext.DefaultSequence)
                    {
                        compiler.SyntaxError(token, true, "err-compiler-eof");
                    }
                    yield break;

                default:                         // Handle text
                    actionCallback(new RstText(token.Value, token.ToLocation()));
                    break;
                }
            }

            if (reader.End && context != CompileContext.DefaultSequence)
            {
                compiler.SyntaxError(reader.PrevToken, true, "err-compiler-eof");
            }
        }
Beispiel #3
0
        public override IEnumerator <Parser> Parse(RantCompiler compiler, CompileContext context, TokenReader reader,
                                                   Action <RST> actionCallback)
        {
            var blockStartToken = reader.PrevLooseToken;
            var items           = new List <RST>();
            var actions         = new List <RST>();

            // "why are these not lists or arrays" i yell into the void, too lazy to find out why
            List <_ <int, double> > constantWeights = null;
            List <_ <int, RST> >    dynamicWeights  = null;
            int blockNumber = 0;

            void itemCallback(RST action) => actions.Add(action);

            compiler.AddContext(CompileContext.BlockEndSequence);
            compiler.AddContext(CompileContext.BlockSequence);

            while (compiler.NextContext == CompileContext.BlockSequence)
            {
                // block weight
                if (reader.PeekLooseToken().Type == R.LeftParen)
                {
                    constantWeights = constantWeights ?? (constantWeights = new List <_ <int, double> >());
                    dynamicWeights  = dynamicWeights ?? (dynamicWeights = new List <_ <int, RST> >());

                    var firstToken = reader.ReadLooseToken();

                    var sequence = new List <RST>();
                    void weightCallback(RST rst) => sequence.Add(rst);

                    compiler.SetNextActionCallback(weightCallback);
                    compiler.AddContext(CompileContext.BlockWeight);
                    yield return(Get <SequenceParser>());

                    // Constant
                    if (sequence.TrueForAll(rst => rst is RstText))
                    {
                        var sb = new StringBuilder();
                        foreach (var rst in sequence)
                        {
                            sb.Append((rst as RstText).Text);
                        }
                        string txt = sb.ToString();
                        if (!Util.ParseDouble(txt, out double doubleValue))
                        {
                            compiler.SyntaxError(reader.PrevLooseToken, false, "err-compiler-invalid-constweight");
                        }
                        else
                        {
                            constantWeights.Add(new _ <int, double>(blockNumber, doubleValue));
                        }
                    }
                    // Dynamic
                    else
                    {
                        if (sequence.Count == 0)
                        {
                            compiler.SyntaxError(firstToken, false, "err-compiler-empty-weight");
                        }
                        else
                        {
                            dynamicWeights.Add(new _ <int, RST>(blockNumber, new RstSequence(sequence, sequence[0].Location)));
                        }
                    }
                }

                reader.SkipSpace();

                compiler.SetNextActionCallback(itemCallback);
                var startToken = reader.PeekToken();
                yield return(Get <SequenceParser>());

                // Don't wrap single nodes in a sequence, it's unnecessary
                items.Add(actions.Count == 1 ? actions[0] : new RstSequence(actions, startToken.ToLocation()));
                actions.Clear();
                blockNumber++;
            }

            compiler.LeaveContext();
            compiler.SetNextActionCallback(actionCallback);

            actionCallback(new RstBlock(blockStartToken.ToLocation(), items, dynamicWeights, constantWeights));
        }