internal override unsafe void ReadToken(ParseContext context) { if (context.Str[context.Pos] != ',') { return; } TokenType lastType = context._lastTokenType; if (lastType != TokenType.Int32Operand && lastType != TokenType.RandomOperand && lastType != TokenType.RightParenthesis) { context.SetFail("错误的逗号。"); return; } while (!context.StackEmpty && context.StackTop.Type != TokenType.LeftParenthesis) { context.Enqueue(context.Pop()); } if (context.StackTop.Type != TokenType.LeftParenthesis) { context.SetFail("逗号位置错误,或不匹配的括号。"); return; } context._lastTokenType = Type; ++context.Pos; }
internal override void ReadToken(ParseContext context) { TokenType lastToken = context._lastTokenType; bool isNegative = context._lastTokenType == TokenType.Negative; long value = GetABSInt32FixPos(context); if (value <= -1L) { if (isNegative) { context.SetFail("错误的“-”号。"); } return; } if (lastToken == TokenType.Int32Operand || lastToken == TokenType.RandomOperand || lastToken == TokenType.Function || lastToken == TokenType.RightParenthesis) { context.SetFail("常数操作符位置错误。"); return; } context._lastTokenType = Type; context.Enqueue(new Int32Operand { Value = (isNegative ? (0L - value) : value) }); }
internal override unsafe void ReadToken(ParseContext context) { if (context.Str[context.Pos] != 'd' && context.Str[context.Pos] != 'D') { return; } context.Pos++; long dice = Int32Operand.GetABSInt32FixPos(context); TokenType lastToken = context._lastTokenType; if (lastToken == TokenType.RightParenthesis || lastToken == TokenType.RandomOperand || lastToken == TokenType.Function || (dice != 2L && dice != 4L && dice != 6L && dice != 8L && dice != 10L && dice != 12L && dice != 20L && dice != 100L)) { context.SetFail("错误的骰子指令。"); return; } context._lastTokenType = TokenType.RandomOperand; if (lastToken != TokenType.Int32Operand) { context._containsDice = true; context.Enqueue(Create(1L, dice)); return; } long r = (context.QueueEnd as Int32Operand).Value; if (r < 1L || r > 100L) { context.SetFail("骰子数量限制为1~100."); return; } context._containsDice = true; context.Replace(Create(r, dice)); }
internal override unsafe void ReadToken(ParseContext context) { char currentChar = context.Str[context.Pos]; if (currentChar != ')' && currentChar != ')') { return; } TokenType lastToken = context._lastTokenType; if (lastToken == TokenType.LeftParenthesis) { context.SetFail("空括号。"); return; } if (lastToken != TokenType.RandomOperand && lastToken != TokenType.Int32Operand && lastToken != TokenType.RightParenthesis) { context.SetFail("错误的右括号。"); return; } while (!context.StackEmpty) { TokenType topType = context.StackTop.Type; if (topType == TokenType.LeftParenthesis) { context._lastTokenType = Type; context.Pop(); context.Pos++; if (!context.StackEmpty && context.StackTop.Type == TokenType.Function) { context.Enqueue(context.Pop()); } return; } if (topType != TokenType.BinaryOperator) { break; } context.Enqueue(context.Pop()); } context.SetFail("不匹配的括号。"); }
private static void UpdateBinaryOperatorNotFixPos(BinaryOperator token, ParseContext context) { while (!context.StackEmpty) { Token top = context.StackTop; if (top.Type != TokenType.BinaryOperator || token.Precedence < (top as BinaryOperator).Precedence) { break; } context.Enqueue(context.Pop()); } context.Push(token); }