public override Verb CreateVerb(string[] tokens) { var variable = tokens[3]; Color(position, tokens[1].Length, Whitespaces); Color(tokens[2].Length, Structures); Color(variable.Length, Variables); Color(tokens[4].Length, Structures); if (GetExpression(source, NextPosition, Comma()).If(out var init, out var i) && GetExpression(source, i, CommaOrCloseParenthesis()).If(out var condition, out var j)) { var index = j; Block increment; if (freeParser.Scan(source, index, "^ |sp| ','")) { freeParser.ColorAll(Structures); index = freeParser.Position; var pIncrement = GetExpression(source, index, Comma()); if (!pIncrement.If(out increment, out index)) { return(null); } if (freeParser.Scan(source, index, "^ |sp| ')'")) { freeParser.ColorAll(Structures); index = freeParser.Position; } else { return(null); } } else { if (freeParser.Scan(source, index, "^ |sp| ')'")) { freeParser.ColorAll(Structures); index = freeParser.Position; var builder = new CodeBuilder(); builder.Variable(variable); builder.Verb(new Add()); builder.Value(1); increment = builder.Block; } else { return(null); } } overridePosition = index; var value = new LoopRange(variable, init, true, condition, increment, none <Block>()); result.Value = value; return(value.PushedVerb); } return(null); }
public override Verb CreateVerb(string[] tokens) { var fieldName = tokens[2]; Color(position, tokens[1].Length, KeyWords); Color(fieldName.Length, Variables); Color(tokens[3].Length, Structures); if (GetExpression(source, NextPosition, PassAlong(REGEX_GUARD_OR_END, false)).If(out var expression, out var index)) { var currentIndex = index; var guardBlock = none <Block>(); if (freeParser.Scan(source, currentIndex, "^ |sp| 'guard' /b")) { freeParser.ColorAll(KeyWords); currentIndex = freeParser.Position; if (GetExpression(source, currentIndex, EndOfLine()).If(out var guard, out var i)) { currentIndex = i; guardBlock = guard.Some(); } } if (endOfLineParser.Scan(source, currentIndex)) { currentIndex = endOfLineParser.Position; } if (GetBlock(source, currentIndex, true).If(out var ifTrue, out var j)) { currentIndex = j; var ifFalse = none <Block>(); if (guardBlock.IsNone && freeParser.Scan(source, currentIndex, "^ /(|tabs| 'else') (/r /n | /r | /n) ")) { freeParser.ColorAll(KeyWords); currentIndex = freeParser.Position; if (GetBlock(source, currentIndex, true).If(out var elseBlock, out var elseIndex)) { currentIndex = elseIndex; guardBlock = elseBlock.Some(); } } if (guardBlock.IsSome) { ifTrue.LastIsReturn.Must().BeTrue().OrThrow("Maybe", () => "return required"); } overridePosition = currentIndex; return(new Maybe(fieldName, expression, ifTrue, ifFalse, guardBlock) { Index = position }); } } return(null); }
public override Verb CreateVerb(string[] tokens) { var sourceRecord = tokens[5]; var oneLine = tokens[2] == "("; Color(position, tokens[1].Length, Whitespaces); Color(tokens[2].Length, Structures); Color(tokens[3].Length, KeyWords); Color(tokens[4].Length, KeyWords); Color(sourceRecord.Length, Variables); var index = NextPosition; var members = new Hash <string, Thunk>(); if (oneLine) { var parser = new OneLineMemberParser(); while (index < source.Length && parser.Scan(source, index)) { members[parser.MemberName] = parser.Thunk; if (freeParser.Scan(source, parser.Position, "^ |sp| ','")) { index = freeParser.Position; continue; } if (freeParser.Scan(source, parser.Position, "^ |sp| ')'")) { overridePosition = freeParser.Position; return(new CreateRecord(members, sourceRecord)); } } } else { var endOfLineParser = new EndOfLineParser(); if (endOfLineParser.Scan(source, index)) { index = endOfLineParser.Position; } AdvanceTabs(); var parser = new MultiLineMemberParser(); while (index < source.Length && parser.Scan(source, index)) { index = parser.Position; members[parser.MemberName] = parser.Thunk; } RegressTabs(); overridePosition = index; return(new CreateRecord(members, sourceRecord)); } return(null); }
public override Verb CreateVerb(string[] tokens) { Color(position, tokens[1].Length, KeyWords); var index = NextPosition; if (GetExpressionThenBlock(source, index).If(out var conditionExpression, out var resultBlock, out var newIndex)) { index = newIndex; var _if = new If(conditionExpression, resultBlock); var sourceLength = source.Length; var currentIf = _if; while (index < sourceLength && parser.Scan(source, index, "^ /(|tabs| 'elseif') /b")) { parser.ColorAll(KeyWords); index = parser.Position; if (GetExpressionThenBlock(source, index).If(out conditionExpression, out resultBlock, out newIndex)) { index = newIndex; var elseIf = new If(conditionExpression, resultBlock); currentIf.Next = elseIf; currentIf = elseIf; } } if (parser.Scan(source, index, "^ /(|tabs| 'else') (/r /n | /r | /n) ")) { parser.ColorAll(KeyWords); index = parser.Position; if (GetBlock(source, index, true).If(out resultBlock, out newIndex)) { _if.ElseBlock = resultBlock; index = newIndex; } else { _if.ElseBlock = null; } } var endParser = new EndParser(); if (endParser.Scan(source, index)) { index = endParser.Position; } overridePosition = index; return(new IfExecute(_if) { Index = position }); } return(null); }
public override Verb CreateVerb(string[] tokens) { var visibility = ParseVisibility(tokens[2].Trim()); var type = tokens[3]; var fieldName = tokens[5]; var readOnly = type == "val"; Color(position, tokens[1].Length, Whitespaces); Color(tokens[2].Length, KeyWords); Color(type.Length, KeyWords); Color(tokens[4].Length, Whitespaces); Color(fieldName.Length, Variables); if (parser.Scan(source, NextPosition, "^ /s* ','")) { var fieldListParser = new FieldListParser(); if (fieldListParser.Parse(source, parser.Position).If(out var fieldNames, out var index)) { overridePosition = index; var fields = new string[fieldNames.Length + 1]; System.Array.Copy(fieldNames, 0, fields, 1, fieldNames.Length); fields[0] = fieldName; return(new CreateFields(readOnly, fields, visibility) { Index = position }); } return(null); } return(new CreateField(readOnly, fieldName, visibility) { Index = position }); }
public override Verb CreateVerb(string[] tokens) { Color(position, length, KeyWords); if (parser.Parse(source, NextPosition) is Some <(string[], int)> some) { (var fields, var index) = some.Value; var parameters = new Parameters(fields); if (freeParser.Scan(source, index, "^ /s* 'in' /b")) { index = freeParser.Position; freeParser.ColorAll(KeyWords); if (GetExpressionThenBlock(source, index) is Some <(Block, Block, int)> some2) { (var expression, var block, var blkIndex) = some2.Value; overridePosition = blkIndex; return(new ForExecute(parameters, expression, block) { Index = position }); } } } return(null); }
public override Verb CreateVerb(string[] tokens) { Color(position, tokens[1].Length, Structures); Color(tokens[2].Length, KeyWords); var index = NextPosition; Comprehension current = null; while (index < source.Length) { if (arraySubComprehensionParser.Scan(source, index)) { index = arraySubComprehensionParser.Position; var comprehension = (Comprehension)arraySubComprehensionParser.Value; if (current == null) { current = comprehension; } else { current.PushDownInnerComprehension(comprehension); } } else { return(null); } if (freeParser.Scan(source, index, "^ ' '* ','")) { index = freeParser.Position; freeParser.ColorAll(Structures); continue; } if (freeParser.Scan(source, index, "^ ' '* ')'") && current != null) { overridePosition = freeParser.Position; freeParser.ColorAll(Structures); result.Value = current; return(new Push(current)); } return(null); } return(null); }
public override Verb CreateVerb(string[] tokens) { Color(position, length, KeyWords); if (parser.Parse(source, NextPosition).If(out var fields, out var index)) { var parameters = new Parameters(fields); if (freeParser.Scan(source, index, "^ /s* 'in' /b")) { index = freeParser.Position; freeParser.ColorAll(KeyWords); if (GetExpression(source, index, EndOfLineConsuming()).If(out var expression, out var i)) { index = i; var block = GetBlock(source, index, true); if (block.IsSome) { index = block.Value.Item2; var firstParser = new FirstParser(); var first = when(firstParser.Scan(source, index), () => firstParser.Block); if (first.IsSome) { index = firstParser.Position; } var middleParser = new MiddleParser(); var middle = when(middleParser.Scan(source, index), () => middleParser.Block); if (middle.IsSome) { index = middleParser.Position; } else { return(null); } var lastParser = new LastParser(); var last = when(lastParser.Scan(source, index), () => lastParser.Block); if (last.IsSome) { index = lastParser.Position; } overridePosition = index; return(new Iterate(parameters, expression, first, middle.Value, last, block.Value.Item1) { Index = position }); } } } } return(null); }
public static IMaybe <(Block block, int position)> GetOneOrMultipleBlock(string source, int index, string stopPattern = " ^ /s * ':' /s+") { var parser = new FreeParser(); if (parser.Scan(source, index, "^ ' '* 'then' /b")) { return(none <(Block, int)>()); } if (parser.Scan(source, index, stopPattern)) { parser.ColorAll(Structures); return(OneLineStatement(source, parser.Position)); } var oneOrMultipleBlock = GetBlock(source, index, true); oneOrMultipleBlock.Must().HaveValue().OrThrow(); return(oneOrMultipleBlock); }
public override Verb CreateVerb(string[] tokens) { if (fieldListParser.Parse(source, position).If(out var fields, out var i)) { var index = i; var parameters = new Parameters(fields); if (freeParser.Scan(source, index, "^ ' '* 'in' /b")) { index = freeParser.Position; freeParser.ColorAll(KeyWords); if (GetExpression(source, index, PassAlong("^ ' '* 'do' /b", true, KeyWords)).If(out var arrayExp, out var j)) { index = j; var newSource = new Block(); var ifExpression = new Block(); var addingToIf = false; foreach (var verb in arrayExp.AsAdded) { if (verb is If) { addingToIf = true; continue; } if (addingToIf) { ifExpression.Add(verb); } else { newSource.Add(verb); } } if (GetExpression(source, index, PassAlong("^ ' '* [',)']", false)).If(out var yieldExp, out var k)) { overridePosition = k; var comprehension = new Comprehension(yieldExp, parameters, new Region()) { ArrayBlock = newSource }; if (ifExpression.Count > 0) { comprehension.SetIf(ifExpression); } result.Value = comprehension; return(new NullOp()); } } } } return(null); }
public static IMaybe <(Block, int)> GetOneOrMultipleBlock(string source, int index, string stopPattern = " ^ /s * ':' /s+") { var parser = new FreeParser(); if (parser.Scan(source, index, "^ ' '* 'then' /b")) { return(none <(Block, int)>()); } if (parser.Scan(source, index, stopPattern)) { parser.ColorAll(Structures); return(OneLineStatement(source, parser.Position)); } var oneOrMultipleBlock = GetBlock(source, index, true); Assert(oneOrMultipleBlock.IsSome, "Statement parser", "Couldn't determine block"); return(oneOrMultipleBlock); }
public static IMaybe <(Block, int)> Parse(string source, int index) { return(when(parser.Scan(source, index, "^ /(/s* 'if') /(/s* '(')"), () => { var tokens = parser.Tokens; Color(index, tokens[1].Length, KeyWords); Color(tokens[2].Length, Structures); index = parser.Result.Position; return GetExpression(source, index, CloseParenthesis()); })); }
public override Verb CreateVerb(string[] tokens) { var variable = tokens[2]; Color(position, tokens[1].Length, KeyWords); Color(variable.Length, Variables); Color(tokens[3].Length, Structures); var index = NextPosition; if (GetExpression(source, index, LoopWhile()).If(out var initialization, out index)) { var builder = new CodeBuilder(); builder.AssignToNewField(false, variable, initialization); var block = builder.Block; block.Expression = false; initialization = block; if (parser.Scan(source, index, "^ /(' '*) /('while' | 'until') /b")) { var direction = parser.Tokens[2]; var isWhile = direction == "while"; parser.Colorize(Whitespaces, KeyWords); index = parser.Position; if (GetExpression(source, index, LoopThen()).If(out var condition, out index)) { condition.Expression = false; if (GetExpression(source, index, LoopEnd()).If(out var increment, out index)) { increment.Expression = false; builder.Clear(); builder.Variable(variable); builder.Assign(); builder.Inline(increment); increment = builder.Block; if (GetOneOrMultipleBlock(source, index).If(out var body, out index)) { overridePosition = index; return(new Loop(initialization, isWhile, condition, increment, body) { Index = position }); } } } } } return(null); }
protected IMaybe <int> isStopping(int index) { if (freeParser.Scan(source, index, stop.Pattern)) { if (stop.Consume) { freeParser.ColorAll(stop.Color); return(freeParser.Position.Some()); } return(index.Some()); } return(none <int>()); }
public override Verb CreateVerb(string[] tokens) { var hasPoint = tokens[1].Trim().IsNotEmpty(); var message = tokens[2]; Color(position, tokens[1].Length, Structures); Color(message.Length, Messaging); Color(1, Structures); return(GetExpression(source, NextPosition, CloseBracket(), true).Map((argumentExp, i) => { var index = i; var fieldName = ""; if (freeParser.Scan(source, index, $"^ {REGEX_ASSIGN}")) { index = freeParser.Position; freeParser.Colorize(Whitespaces, Structures, Structures); var op = freeParser.Tokens[2]; return GetExpression(source, index, EndOfLine()).Map((expression, j) => { overridePosition = j; if (!hasPoint) { fieldName = message; message = "item"; } if (op.IsNotEmpty()) { var combined = CombineOperation(message, op, argumentExp, expression); if (combined.IsNone) { return null; } expression = combined.Value; } var arguments = new Arguments(); arguments.AddBlockArgument(argumentExp); arguments.AddBlockArgument(expression); return hasPoint ? new SendMessage(SetterName(message), arguments) : new SendMessageToField(fieldName, SetterName(message), arguments); }, () => null); } overridePosition = index; return new SendMessage(GetterName(message), new Arguments(argumentExp)); }, () => null)); }
public override Verb CreateVerb(string[] tokens) { Color(position, length, Structures); var index = NextPosition; var stack = new Stack <Parameters>(); stack.Push(firstParameters); while (index < source.Length) { if (parametersParser.Parse(source, index).If(out var parameters, out var i)) { index = i; stack.Push(parameters); if (freeParser.Scan(source, index, pattern)) { index = freeParser.Position; freeParser.ColorAll(Structures); continue; } break; } } if (index < source.Length && functionBodyParser.Parse(source, index).If(out var block, out var j)) { var anyLambda = none <Lambda>(); while (stack.Count > 0) { var parameters = stack.Pop(); anyLambda = anyLambda.Map(l => getLambda(parameters, l)).DefaultTo(() => new Lambda(new Region(), block, parameters, false)).Some(); } if (anyLambda.If(out var lambda)) { overridePosition = j; return(new CreateFunction(functionName, lambda, false, visibility, overriding, null) { Index = position }); } } return(null); }
public override Verb CreateVerb(string[] tokens) { var variable = tokens[4]; Color(position, tokens[1].Length, Whitespaces); Color(tokens[2].Length, Structures); Color(tokens[3].Length, KeyWords); Color(variable.Length, Variables); Color(tokens[5].Length, Structures); var index = NextPosition; if (GetExpression(source, index, LoopWhile()).If(out var init, out index)) { if (parser.Scan(source, index, "^ /(' '*) /('while' | 'until') /b")) { var direction = parser.Tokens[2]; var positive = direction == "while"; parser.Colorize(Whitespaces, KeyWords); index = parser.Position; if (GetExpression(source, index, LoopThen()).If(out var condition, out index)) { condition.Expression = false; if (GetExpression(source, index, Yield()).If(out var increment, out index) && GetExpression(source, index, CloseParenthesis()).If(out var yielding, out index)) { overridePosition = index; var someYielding = maybe(yielding.Count > 0, () => yielding); var value = new LoopRange(variable, init, positive, condition, increment, someYielding); result.Value = value; return(value.PushedVerb); } } } } return(null); }
public override Verb CreateVerb(string[] tokens) { Color(position, length, KeyWords); if (parser.Parse(source, NextPosition).If(out var fields, out var index)) { var parameters = new Parameters(fields); if (freeParser.Scan(source, index, "^ /s* 'in' /b")) { index = freeParser.Position; freeParser.ColorAll(KeyWords); if (GetExpressionThenBlock(source, index).If(out var expression, out var block, out var blkIndex)) { overridePosition = blkIndex; return(new ForExecute(parameters, expression, block) { Index = position }); } } } return(null); }
public override Verb CreateVerb(string[] tokens) { if (fieldListParser.Parse(source, position).If(out var fields, out var i)) { var index = i; var parameters = new Parameters(fields); if (freeParser.Scan(source, index, "^ /s* 'in' /b")) { index = freeParser.Position; freeParser.ColorAll(KeyWords); var stop = ComprehensionEnd(); if (GetExpression(source, index, stop).If(out var generatorSource, out var j)) { if (!freeParser.Scan(source, j, stop.Pattern)) { return(null); } index = freeParser.Position; var token = freeParser.Tokens[1]; var ifBlock = none <Block>(); switch (token) { case ",": Last = false; freeParser.ColorAll(Structures); break; case "if": freeParser.ColorAll(KeyWords); if (GetExpression(source, index, stop).If(out var block, out var k)) { ifBlock = block.Some(); index = k; freeParser.Scan(source, index, stop.Pattern); index = freeParser.Position; token = freeParser.Tokens[0].TrimLeft(); switch (token) { case ",": Last = false; freeParser.ColorAll(Structures); break; case "if": return(null); case ":": Last = true; freeParser.ColorAll(Structures); break; } } break; case ":": Last = true; freeParser.ColorAll(Structures); break; } overridePosition = index; result.Value = new NSInnerComprehension(parameters, generatorSource, ifBlock); return(new NullOp()); } } } return(null); }
public override Verb CreateVerb(string[] tokens) { var optionalLength = tokens[2].Length; var optional = tokens[2].Trim() == "optional"; if (!InClassDefinition && optional) { return(null); } Color(position, tokens[1].Length, Whitespaces); Color(optionalLength, KeyWords); var type = tokens[3].Trim(); Color(tokens[3].Length, KeyWords); var name = tokens[4]; var isStandard = tokens[5] == "("; Color(name.Length, isStandard ? Invokeables : Messaging); name = LongToMangledPrefix(type, name); Color(tokens[5].Length, Structures); var index = position + length; if (isStandard) { var parameterListParser = new ParameterListParser2(); if (parameterListParser.Parse(source, index).If(out var list, out var newIndex)) { var parameterCount = list.Count; var endParser = new EndParser(); index = newIndex; if (endParser.Scan(source, index)) { index = endParser.Position; } overridePosition = index; result.Value = new Signature(name, parameterCount, optional); return(new NullOp()); } return(null); } var builder = new StringBuilder(); var messageParameterParser = new MessageParameterParser(); var variableParser = new VariableParser(); var parameterList = new List <Parameter>(); if (variableParser.Scan(source, index)) { var variable = (Variable)variableParser.Result.Value; var parameter = new Parameter(variable.Name); builder.Append(name); builder.Append("_"); parameterList.Add(parameter); index = variableParser.Result.Position; } else { return(null); } while (messageParameterParser.Scan(source, index)) { var parameter = new Parameter(messageParameterParser.ParameterName); parameterList.Add(parameter); builder.Append(messageParameterParser.MessageName); builder.Append("_"); index = messageParameterParser.Result.Position; } if (freeParser.Scan(source, index, REGEX_END1)) { freeParser.ColorAll(Structures); overridePosition = freeParser.Position; name = builder.ToString(); result.Value = new Signature(name, parameterList.Count, optional); return(new NullOp()); } return(null); }