public static IEnumerable <Token> RemoveSpacesTokens(this IEnumerable <Token> tokens) { var x = tokens.ToList(); x.RemoveAll(y => y == SpaceToken.Create() || y is NewLineToken); return(x); }
public void Write(StringBuilder builder, SpaceToken spaceToken) { if (builder == null || spaceToken == null) { return; } // can be multiple spaces or a tab depending on the situation. builder.Append(" "); }
public void SpaceTokenCtor() { string value = " "; int position = 22; string tokenValue = $@"[space:{position}]"; SpaceToken token = new SpaceToken(position); Assert.AreEqual(tokenValue, token.TokenValue); Assert.AreEqual(value, token.Value); Assert.AreEqual(position, token.Position); }
public void ParseSourceWithSpaceTokenShouldIncludeSpaceTokens() { String source = "source code"; IList <TokenBase> input = new List <TokenBase>(); input.Add(TokenCreator.Create <GenericToken>("source", null)); TokenCreator.Advance(1); input.Add(TokenCreator.Create <GenericToken>("code", null)); SpaceToken space = new SpaceToken(1, 7, 6); IEnumerable <TokenBase> parsed = _parser.Parse(source, input); Assert.AreEqual(space, parsed.ElementAt(1)); }
public bool TryLexWhitespace(string line, int lineIndex, ref int charIndex) { var match = Regex.Match(line, @"^(\s+)"); if (!match.Success) { return(false); } var spaceToken = new SpaceToken() { Line = lineIndex, Char = charIndex, Text = match.Groups[0].Value }; Tokens.Add(spaceToken); charIndex += spaceToken.Text.Length; return(true); }
public void ParseSourceWithConsecutiveSpaceTokenShouldIncludeConsecutiveSpaceTokens() { String source = "source code"; IList <TokenBase> input = new List <TokenBase>(); input.Add(TokenCreator.Create <GenericToken>("source", null)); TokenCreator.Advance(3); input.Add(TokenCreator.Create <GenericToken>("code", null)); SpaceToken spaceAlpha = new SpaceToken(1, 7, 6); SpaceToken spaceBravo = new SpaceToken(1, 8, 7); SpaceToken spaceCharlie = new SpaceToken(1, 9, 8); IEnumerable <TokenBase> parsed = _parser.Parse(source, input); Assert.AreEqual(spaceAlpha, parsed.ElementAt(1)); Assert.AreEqual(spaceBravo, parsed.ElementAt(2)); Assert.AreEqual(spaceCharlie, parsed.ElementAt(3)); }
static public async Task ActAsync(TargetSpaceCtx ctx) { // 1 fear if invaders are present. if (ctx.HasInvaders) { ctx.AddFear(1); } // For each disease, Push 2 explorer / town / dahan. await ctx.Pusher .AddGroup(ctx.Disease.Count, Invader.Explorer, Invader.Town, TokenType.Dahan) .AddCustomMoveAction(async(_, from, to) => { // 1 disease may move with each Pushed piece. var option = new SpaceToken(from, TokenType.Disease); var diseaseToken = await ctx.Decision(Select.TokenFromManySpaces.ToGather(1, to, new[] { option }, Present.Done)); if (diseaseToken != null) { await ctx.Move(option.Token, option.Space, to); } }) .MoveN(); }
private static async Task DoFireDamageToMultipleTargets(TargetSpaceCtx ctx, int fireDamage) { await ctx.DamageInvaders(1); // because they targetted a land, only select invaders from that land. --fireDamage; var spacesWithPresenceAndBlight = ctx.Self.Presence.Spaces .Select(s => ctx.Target(s)) .Where(x => x.HasBlight) .ToArray(); // ! don't .ToArray() this because we want it to re-execute each time. var invaderTokens = spacesWithPresenceAndBlight .SelectMany(ctx => ctx.Tokens.Invaders().Select(t => new SpaceToken(ctx.Space, t))); while (fireDamage > 0 && invaderTokens.Any()) { SpaceToken token = await ctx.Decision(new Select.TokenFromManySpaces($"Apply fire damage. ({fireDamage} remaining)", invaderTokens, Present.Always)); await ctx.Target(token.Space).Invaders.ApplyDamageTo1(1, (HealthToken)token.Token); --fireDamage; } }
public Token GetToken() { State = LexicAnalyserState.Initial; Value = HasNext ? Character.ToString() : ""; Token token = null; while (HasNext && token == null) { switch (State) { case LexicAnalyserState.Initial: HandleInitial(); break; case LexicAnalyserState.NonTerminal: token = new NonTerminalToken(Value); CurrentIndex++; break; case LexicAnalyserState.Space: token = new SpaceToken(); CurrentIndex++; break; case LexicAnalyserState.Terminal: token = new TerminalToken(Value); CurrentIndex++; break; case LexicAnalyserState.Empty: token = new EmptyToken(); CurrentIndex++; break; case LexicAnalyserState.NewLine: token = new NewLineToken(Value); CurrentIndex++; break; case LexicAnalyserState.Identifier: token = new IdentifierToken(Value); CurrentIndex++; break; case LexicAnalyserState.Number: token = new NumberToken(Value); CurrentIndex++; break; case LexicAnalyserState.Var: token = new VarToken(); CurrentIndex++; break; case LexicAnalyserState.Write: token = new WriteToken(); CurrentIndex++; break; case LexicAnalyserState.Read: token = new ReadToken(); CurrentIndex++; break; case LexicAnalyserState.If: token = new IfToken(); CurrentIndex++; break; case LexicAnalyserState.End: token = new EndToken(); CurrentIndex++; break; case LexicAnalyserState.Then: token = new ThenToken(); CurrentIndex++; break; case LexicAnalyserState.Begin: token = new BeginToken(); CurrentIndex++; break; case LexicAnalyserState.While: token = new WhileToken(); CurrentIndex++; break; case LexicAnalyserState.Do: token = new DoToken(); CurrentIndex++; break; case LexicAnalyserState.SemiColon: token = new SemiColonToken(); CurrentIndex++; break; case LexicAnalyserState.OpenParentheses: token = new OpenParenthesesToken(); CurrentIndex++; break; case LexicAnalyserState.CloseParentheses: token = new CloseParenthesesToken(); CurrentIndex++; break; case LexicAnalyserState.Plus: token = new PlusToken(); CurrentIndex++; break; case LexicAnalyserState.Sub: token = new SubToken(); CurrentIndex++; break; case LexicAnalyserState.Great: token = new GreatToken(); CurrentIndex++; break; case LexicAnalyserState.Less: token = new LessToken(); CurrentIndex++; break; case LexicAnalyserState.Equal: token = new EqualToken(); CurrentIndex++; break; case LexicAnalyserState.Repeat: token = new RepeatToken(); CurrentIndex++; break; case LexicAnalyserState.Until: token = new UntilToken(); CurrentIndex++; break; case LexicAnalyserState.Attribution: token = new AttributionToken(); CurrentIndex++; break; case LexicAnalyserState.NotEqual: token = new NotEqualToken(); CurrentIndex++; break; case LexicAnalyserState.GreatOrEqual: token = new GreatOrEqualToken(); CurrentIndex++; break; case LexicAnalyserState.LessOrEqual: token = new LessOrEqualToken(); CurrentIndex++; break; default: throw new ArgumentOutOfRangeException(); } } return(token); }