public List<SpokeClass> Run(List<Class> someClasses, List<TokenMacroPiece> tp) { allMacros_ = tp; List<SpokeClass> classes = new List<SpokeClass>(); SpokeClass cl = new SpokeClass(); classes.Add(cl); cl.Name = "Array"; cl.Methods = new List<SpokeMethod>() { new SpokeMethod() { MethodFunc = (a) => { a[0].ArrayItems.Add(a[1]); return null; }, MethodName = "add", Class = cl, returnType = new SpokeType(ObjectType.Void), Parameters = new string[]{"this","v"} },new SpokeMethod() { MethodFunc = (a) => { a[0].ArrayItems.Clear(); return null; }, MethodName = "clear", returnType = new SpokeType(ObjectType.Void), Class = cl, Parameters = new string[]{"this"} }, new SpokeMethod() { MethodFunc = (a) => new SpokeObject(a[0].ArrayItems.Count), MethodName = "length", Class = cl, returnType = new SpokeType(ObjectType.Int), Parameters = new string[]{"this"} }, new SpokeMethod() { MethodFunc = (a) => { a[0].ArrayItems.RemoveAll(b => SpokeObject.Compare(b,a[1])); return null; }, MethodName = "remove", Class = cl, returnType = new SpokeType(ObjectType.Void), Parameters = new string[]{"this","v"} },new SpokeMethod() { MethodFunc = (a) => { return a[0].ArrayItems.Last(); }, MethodName = "last", Class = cl, returnType = new SpokeType(ObjectType.Null), Parameters = new string[]{"this"} },new SpokeMethod() { MethodFunc = (a) => { return a[0].ArrayItems.First(); }, MethodName = "first", returnType = new SpokeType(ObjectType.Null), Class = cl, Parameters = new string[]{"this"} }, new SpokeMethod() { MethodFunc = (a) => { a[0].ArrayItems.Insert(a[1].IntVal, a[2]); return null; }, MethodName = "insert", Class = cl, returnType = new SpokeType(ObjectType.Void), Parameters = new string[]{"this","v","v2"} } }; foreach (var @class in someClasses) { classes.Add(cl = new SpokeClass()); cl.Variables = @class.VariableNames.ToArray(); cl.Name = @class.Name; foreach (var method in @class.Methods) { SpokeMethod me; cl.Methods.Add(me = new SpokeMethod()); me.MethodName = method.Name; me.Parameters = new string[method.paramNames.Count + 1]; me.Parameters[0] = "this"; for (int index = 0; index < method.paramNames.Count; index++) { me.Parameters[index + 1] = method.paramNames[index]; } me.Class = cl; TokenEnumerator enumerator = new TokenEnumerator(method.Lines.ToArray()); #if DEBUGs sb.AppendLine("Evaluating " + cl.Name + ": " + me.MethodName); #endif me.Lines = getLines(enumerator, 2, new evalInformation()).ToArray(); me.HasYieldReturn = linesHave(me.Lines, ISpokeLine.YieldReturn); me.HasYield = linesHave(me.Lines, ISpokeLine.Yield); me.HasReturn = linesHave(me.Lines, ISpokeLine.Return); } } return classes; }
private SpokeSwitch evaluateSwitch(TokenEnumerator enumerator, int tabIndex, evalInformation inf) { enumerator.MoveNext(); SpokeSwitch sw = new SpokeSwitch((SpokeItem) eval(enumerator, tabIndex,new evalInformation(inf){ResetCurrentVal=true})); List<SpokeSwitch.Case> als = new List<SpokeSwitch.Case>(); while (true) { if (enumerator.PeakNext() is TokenTab) { enumerator.MoveNext(); if (((TokenTab) enumerator.Current).TabIndex == tabIndex + 1) { enumerator.MoveNext(); SpokeItem jf = (SpokeItem) eval(enumerator, tabIndex, new evalInformation(inf){ResetCurrentVal=true}); Debug.Assert(enumerator.Current is TokenColon,"case bad"); enumerator.MoveNext(); enumerator.MoveNext(); List<SpokeLine> gc = getLines(enumerator, tabIndex + 2, new evalInformation(inf)); als.Add(new SpokeSwitch.Case(jf,gc.ToArray())); Debug .Assert(enumerator.Current.Type == Token.NewLine || enumerator.Current.Type == Token.EndOfCodez, enumerator.Current.Type + " Isnt Newline"); } continue; } break; } sw.Cases = als.ToArray(); if (enumerator.Current.Type == Token.Tab && inf.EatTab) enumerator.MoveNext(); return sw; }
public List<SpokeLine> getLines(TokenEnumerator enumerator, int tabIndex, evalInformation inf) { int lineIndex = 0; List<SpokeLine> lines = new List<SpokeLine>(); top: #if DEBUGs sb.AppendLine("Line " + lineIndex); sb.AppendLine(""); lineIndex++; #endif if (inf.CheckMacs == 0) { Console.Write(lines.LastOrDefault() == null ? "Parsing..." : PrintExpressions.getLine(lines.Last(), 0)); } IToken token = enumerator.Current; if (token == null || token.Type == Token.EndOfCodez) { return lines; } if (token.Type == Token.NewLine) { enumerator.MoveNext(); goto top; } if (token.Type == Token.Tab && enumerator.PeakNext().Type == Token.NewLine) { enumerator.MoveNext(); enumerator.MoveNext(); goto top; } if (((TokenTab)token).TabIndex < tabIndex) { enumerator.PutBack(); return lines; } StaticMethods.Assert(((TokenTab)token).TabIndex == tabIndex, "Bad Tab"); enumerator.MoveNext(); if (enumerator.Current.Type == Token.NewLine) { enumerator.MoveNext(); goto top; } CurrentItem = null; var s = eval(enumerator, tabIndex, new evalInformation(inf) { EatTab = false, ResetCurrentVal = true }); if (s is SpokeLine) { lines.Add((SpokeLine)s); } else { throw new AbandonedMutexException("wat"); } goto top; }
public Spoke eval(TokenEnumerator enumerator, int tabIndex, evalInformation inf) { if (inf.ResetCurrentVal) { CurrentItem = null; } #if DEBUGs sb.AppendLine("Starting Eval " + enumerator.Current.Type); #endif if (inf.CheckMacs < 2) { var df = CurrentItem; CurrentItem = null; var rm = checkRunMacro(enumerator, tabIndex, inf); if (rm != null) { CurrentItem = rm; } else CurrentItem = df; } switch (enumerator.Current.Type) { case Token.Word: if (((TokenWord)enumerator.Current).Word.ToLower() == "null") { CurrentItem = new SpokeNull(); } else CurrentItem = new SpokeVariable() { Parent = CurrentItem, VariableName = ((TokenWord)enumerator.Current).Word }; enumerator.MoveNext(); break; case Token.Int: CurrentItem = new SpokeInt() { Value = ((TokenInt)enumerator.Current)._value }; enumerator.MoveNext(); break; case Token.Float: CurrentItem = new SpokeFloat() { Value = ((TokenFloat)enumerator.Current)._value }; enumerator.MoveNext(); break; case Token.String: CurrentItem = new SpokeString() { Value = ((TokenString)enumerator.Current)._value }; enumerator.MoveNext(); break; case Token.False: CurrentItem = new SpokeBool() { Value = false }; enumerator.MoveNext(); break; case Token.True: CurrentItem = new SpokeBool() { Value = true }; enumerator.MoveNext(); break; case Token.OpenParen: enumerator.MoveNext(); CurrentItem = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf)); if (enumerator.Current.Type == Token.Tab) { enumerator.MoveNext(); } if (enumerator.Current.Type != Token.CloseParen) { throw new AbandonedMutexException(); } enumerator.MoveNext(); break; case Token.Switch: return (SpokeLine)evaluateSwitch(enumerator, tabIndex, inf); case Token.If: enumerator.MoveNext(); var i_f = new SpokeIf() { Condition = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf)) }; enumerator.MoveNext(); i_f.IfLines = getLines(enumerator, tabIndex + 1, new evalInformation(inf)).ToArray(); StaticMethods.Assert(enumerator.Current.Type == Token.NewLine || enumerator.Current.Type == Token.EndOfCodez, enumerator.Current.Type + " Isnt Newline"); enumerator.MoveNext(); if (enumerator.Current.Type == Token.Tab && enumerator.PeakNext().Type == Token.Else) { enumerator.MoveNext(); enumerator.MoveNext(); i_f.ElseLines = getLines(enumerator, tabIndex + 1, new evalInformation(inf)).ToArray(); StaticMethods.Assert(enumerator.Current.Type == Token.NewLine || enumerator.Current.Type == Token.EndOfCodez, enumerator.Current.Type + " Isnt Newline"); enumerator.MoveNext(); } if (enumerator.Current.Type == Token.Tab && inf.EatTab) enumerator.MoveNext(); return i_f; break; case Token.Bar: var an = new SpokeAnonMethod() { Parent = CurrentItem }; enumerator.MoveNext(); StaticMethods.Assert(enumerator.Current.Type == Token.OpenParen, enumerator.Current.Type + " Isnt OpenParen"); List<ParamEter> parameters_ = new List<ParamEter>(); enumerator.MoveNext(); if (enumerator.Current.Type != Token.CloseParen) { pback2: bool byRe_f = false; if (((TokenWord)enumerator.Current).Word.ToLower() == "ref") { byRe_f = true; enumerator.MoveNext(); } parameters_.Add(new ParamEter() { ByRef = byRe_f, Name = ((TokenWord)enumerator.Current).Word }); enumerator.MoveNext(); switch (enumerator.Current.Type) { case Token.CloseParen: enumerator.MoveNext(); break; case Token.Comma: enumerator.MoveNext(); goto pback2; break; default: throw new ArgumentOutOfRangeException(); } } an.Parameters = parameters_.ToArray(); StaticMethods.Assert(enumerator.Current.Type == Token.AnonMethodStart, enumerator.Current.Type + " Isnt anonmethodstart"); enumerator.MoveNext(); StaticMethods.Assert(enumerator.Current.Type == Token.NewLine, enumerator.Current.Type + " Isnt Newline"); enumerator.MoveNext(); an.Lines = getLines(enumerator, tabIndex + 1, new evalInformation(inf)).ToArray(); StaticMethods.Assert(enumerator.Current.Type == Token.NewLine || enumerator.Current.Type == Token.EndOfCodez, enumerator.Current.Type + " Isnt Newline"); enumerator.MoveNext(); an.HasYield = linesHave(an.Lines, ISpokeLine.Yield); an.HasReturn = linesHave(an.Lines, ISpokeLine.Return); an.HasYieldReturn = linesHave(an.Lines, ISpokeLine.YieldReturn); StaticMethods.Assert(enumerator.Current.Type == Token.Tab && ((TokenTab)enumerator.Current).TabIndex == tabIndex, "Bad tabindex"); if (enumerator.Current.Type == Token.Tab && inf.EatTab) enumerator.MoveNext(); if (enumerator.PeakNext().Type != Token.CloseParen) { return an; } else { enumerator.MoveNext(); CurrentItem = an; } break; case Token.OpenSquare: CurrentItem = dyanmicArray(enumerator, tabIndex, inf); break; case Token.OpenCurly: CurrentItem = new SpokeConstruct(); CurrentItem = dynamicObject(enumerator, tabIndex, inf); break; case Token.Create: return createObject(enumerator, tabIndex, inf); break; case Token.Return: enumerator.MoveNext(); var r = new SpokeReturn() { Return = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf)) }; enumerator.MoveNext(); return r; case Token.Yield: enumerator.MoveNext(); if (enumerator.Current.Type == Token.Return) { enumerator.MoveNext(); var y = new SpokeYieldReturn() { YieldReturn = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf)) }; enumerator.MoveNext(); return y; } else { var y = new SpokeYield() { Yield = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf)) }; enumerator.MoveNext(); return y; } } #if DEBUGs sb.AppendLine("Checking Eval 1" + enumerator.Current.Type); #endif switch (enumerator.Current.Type) { case Token.OpenSquare: var ar = new SpokeArrayIndex(); ar.Parent = CurrentItem; enumerator.MoveNext(); ar.Index = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { BreakBeforeEqual = false, ResetCurrentVal = true }); StaticMethods.Assert(enumerator.Current.Type == Token.CloseSquare, enumerator.Current.Type + " Isnt closesquare"); enumerator.MoveNext(); if (enumerator.Current.Type == Token.OpenSquare) { } CurrentItem = ar; break; case Token.OpenParen: var meth = new SpokeMethodCall() { Parent = CurrentItem }; enumerator.MoveNext(); List<SpokeItem> param_ = new List<SpokeItem>(); param_.Add(new SpokeCurrent()); CurrentItem = null; if (enumerator.Current.Type != Token.CloseParen) { g: param_.Add((SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { ResetCurrentVal = true })); if (enumerator.Current.Type == Token.Comma) { enumerator.MoveNext(); goto g; } } enumerator.MoveNext();//closeparen meth.Parameters = param_.ToArray(); CurrentItem = meth; //loop params break; case Token.Period: var t = CurrentItem; enumerator.MoveNext(); SpokeParent g; Spoke c; CurrentItem = g = (SpokeParent)(c = eval(enumerator, tabIndex, new evalInformation(inf) { BreakBeforeEvaler = true })); //g.Parent = t; //enumerator.MoveNext(); break; } switch (enumerator.Current.Type) { case Token.Period: var t = CurrentItem; enumerator.MoveNext(); SpokeParent g; Spoke c; CurrentItem = g = (SpokeParent)(c = eval(enumerator, tabIndex, new evalInformation(inf) { BreakBeforeEvaler = true })); //g.Parent = t; //enumerator.MoveNext(); break; } #if DEBUGs sb.AppendLine("Checking Eval 2" + enumerator.Current.Type); #endif forMethods: switch (enumerator.Current.Type) { case Token.OpenParen: var meth = new SpokeMethodCall() { Parent = CurrentItem }; enumerator.MoveNext(); List<SpokeItem> param_ = new List<SpokeItem>(); param_.Add(new SpokeCurrent()); g: param_.Add((SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { ResetCurrentVal = true })); if (enumerator.Current.Type == Token.Comma) { enumerator.MoveNext(); goto g; } enumerator.MoveNext();//closeparen meth.Parameters = param_.ToArray(); CurrentItem = meth; goto forMethods; } #if DEBUGs sb.AppendLine("Checking Eval 3" + enumerator.Current.Type); #endif if (inf.BreakBeforeEvaler) { return CurrentItem; } if (!inf.DontEvalEquals) { if (enumerator.Current.Type == Token.Equal) { var equ = new SpokeEqual() { LeftSide = CurrentItem }; enumerator.MoveNext(); equ.RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { EatTab = false, ResetCurrentVal = true }); if (enumerator.Current.Type == Token.NewLine) { // enumerator.MoveNext(); //newline } return equ; } } switch (enumerator.Current.Type) { case Token.AnonMethodStart: //checkparams //getlines var an = new SpokeAnonMethod() { Parent = CurrentItem }; enumerator.MoveNext(); if (enumerator.Current.Type == Token.Bar) { enumerator.MoveNext(); StaticMethods.Assert(enumerator.Current.Type == Token.OpenParen, enumerator.Current.Type + " Isnt openparen"); List<ParamEter> parameters_ = new List<ParamEter>(); enumerator.MoveNext(); pback2: bool byRe_f = false; if (((TokenWord)enumerator.Current).Word.ToLower() == "ref") { byRe_f = true; enumerator.MoveNext(); } parameters_.Add(new ParamEter() { ByRef = byRe_f, Name = ((TokenWord)enumerator.Current).Word }); enumerator.MoveNext(); switch (enumerator.Current.Type) { case Token.CloseParen: enumerator.MoveNext(); break; case Token.Comma: enumerator.MoveNext(); goto pback2; break; default: throw new ArgumentOutOfRangeException(); } an.Parameters = parameters_.ToArray(); } else { an.RunOnVar = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { ResetCurrentVal = true, BreakBeforeEqual = false, BreakBeforeEvaler = true }); } StaticMethods.Assert(enumerator.Current.Type == Token.NewLine, enumerator.Current.Type + " Isnt Newline"); enumerator.MoveNext(); CurrentItem = null; an.Lines = getLines(enumerator, tabIndex + 1, new evalInformation(inf)).ToArray(); an.HasYield = linesHave(an.Lines, ISpokeLine.Yield); an.HasReturn = linesHave(an.Lines, ISpokeLine.Return); an.HasYieldReturn = linesHave(an.Lines, ISpokeLine.YieldReturn); StaticMethods.Assert(enumerator.Current.Type == Token.NewLine || enumerator.Current.Type == Token.EndOfCodez, enumerator.Current.Type + " Isnt Newline"); enumerator.MoveNext(); if (enumerator.Current.Type == Token.Tab && inf.EatTab) enumerator.MoveNext(); CurrentItem = an; break; } // 5*6-7+8/9+10 switch (enumerator.Current.Type) { case Token.Plus: enumerator.MoveNext(); CurrentItem = new SpokeAddition() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { BreakBeforeEqual = true, ResetCurrentVal = true }) }; break; case Token.Minus: enumerator.MoveNext(); CurrentItem = new SpokeSubtraction() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { BreakBeforeEqual = true, ResetCurrentVal = true }) }; break; case Token.Divide: enumerator.MoveNext(); CurrentItem = new SpokeDivision() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { BreakBeforeEqual = true, ResetCurrentVal = true }) }; break; case Token.Mulitply: enumerator.MoveNext(); CurrentItem = new SpokeMultiplication() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { BreakBeforeEqual = true, ResetCurrentVal = true }) }; break; } if (inf.BreakBeforeEqual) { return CurrentItem; } switch (enumerator.Current.Type) { case Token.DoubleOr: enumerator.MoveNext(); CurrentItem = new SpokeOr() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { ResetCurrentVal = true }) }; break; case Token.DoubleAnd: enumerator.MoveNext(); CurrentItem = new SpokeAnd() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { ResetCurrentVal = true }) }; break; } #if DEBUGs sb.AppendLine("Checking Eval 4" + enumerator.Current.Type); #endif SpokeItem e; switch (enumerator.Current.Type) { case Token.DoubleEqual: enumerator.MoveNext(); e = new SpokeEquality() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { BreakBeforeEqual = true, ResetCurrentVal = true }) }; CurrentItem = e; break; case Token.NotEqual: enumerator.MoveNext(); e = new SpokeNotEqual() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { BreakBeforeEqual = true, ResetCurrentVal = true }) }; CurrentItem = e; break; case Token.Less: enumerator.MoveNext(); if (enumerator.Current.Type == Token.Equal) { enumerator.MoveNext(); e = new SpokeLessThanOrEqual() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { BreakBeforeEqual = true, ResetCurrentVal = true }) }; CurrentItem = e; break; } e = new SpokeLessThan() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { BreakBeforeEqual = true, ResetCurrentVal = true }) }; CurrentItem = e; break; case Token.Greater: enumerator.MoveNext(); if (enumerator.Current.Type == Token.Equal) { enumerator.MoveNext(); e = new SpokeGreaterThanOrEqual() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { BreakBeforeEqual = true, ResetCurrentVal = true }) }; CurrentItem = e; break; } e = new SpokeGreaterThan() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { BreakBeforeEqual = true, ResetCurrentVal = true }) }; CurrentItem = e; break; } switch (enumerator.Current.Type) { case Token.DoubleOr: enumerator.MoveNext(); CurrentItem = new SpokeOr() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { ResetCurrentVal = true }) }; break; case Token.DoubleAnd: enumerator.MoveNext(); CurrentItem = new SpokeAnd() { LeftSide = CurrentItem, RightSide = (SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { ResetCurrentVal = true }) }; break; } return CurrentItem; }
internal bool TryAccept(TokenEnumerator tokenEnumerator, out SmtpCommand command) { return(_stateTable.TryAccept(tokenEnumerator, out command)); }
private SpokeArray dyanmicArray(TokenEnumerator enumerator, int tabIndex, evalInformation inf) { var ars = new SpokeArray(); List<SpokeItem> parameters = new List<SpokeItem>(); enumerator.MoveNext(); if (enumerator.Current.Type == Token.CloseSquare) { enumerator.MoveNext(); ars.Parameters = parameters.ToArray(); return (ars); } pback: parameters.Add((SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { ResetCurrentVal = true })); switch (enumerator.Current.Type) { case Token.CloseSquare: enumerator.MoveNext(); break; case Token.Comma: enumerator.MoveNext(); goto pback; break; default: throw new ArgumentOutOfRangeException(); } ars.Parameters = parameters.ToArray(); return (ars); }
/// <summary> /// Try to make a RCPT command. /// </summary> /// <param name="tokenEnumerator">The token enumerator to use when matching the command.</param> /// <param name="command">The command that was found.</param> /// <param name="errorResponse">The error response that was returned if a command could not be matched.</param> /// <returns>true if a RCPT command was found, false if not.</returns> bool TryMakeRcpt(TokenEnumerator tokenEnumerator, out SmtpCommand command, out SmtpResponse errorResponse) { return(new SmtpParser(_options, tokenEnumerator).TryMakeRcpt(out command, out errorResponse)); }
public void TestContextAndTab2() { _tokenizer = new Tokenizer(LINE_WITH_TAB2, true, true, '\\', SEPERATORS, new[] {'\''}, null); _tokenEnumerator = _tokenizer.GetTokenEnumerator(); Assert.IsTrue(_tokenEnumerator.MoveNext()); Assert.IsTrue(_tokenEnumerator.MoveNext()); ParseContext context = _tokenEnumerator.Token.Context; Assert.That(context.LineWithPosition, Is.EqualTo(LINE_WITH_TAB_WITHPOINTER2)); }
internal SmtpCommand MakeRset(TokenEnumerator enumerator) { enumerator.Consume(1); enumerator.ConsumeWhile((Token t) => t.Kind == TokenKind.Space); return(enumerator.Count > 1 ? MakeInvalid(SmtpReplyCode.SyntaxError, ""): new RsetCommand()); }
/// <summary> /// Advances the enumerator to the next command in the stream. /// </summary> /// <param name="tokenEnumerator">The token enumerator to accept the command from.</param> /// <param name="command">The command that was found.</param> /// <param name="errorResponse">The error response that indicates why a command could not be accepted.</param> /// <returns>true if a valid command was found, false if not.</returns> public bool TryAccept(TokenEnumerator tokenEnumerator, out SmtpCommand command, out SmtpResponse errorResponse) { return(_stateTable.TryAccept(tokenEnumerator, out command, out errorResponse)); }
internal SmtpCommand MakeData(TokenEnumerator enumerator) { enumerator.Consume(1); enumerator.ConsumeWhile((Token t) => t.Kind == TokenKind.Space); return(enumerator.Count > 1 ? MakeInvalid(SmtpReplyCode.SyntaxError, ""): new DataCommand(_server.OnMessageArrived)); }
public Tuple<List<Class>, List<TokenMacroPiece>> Run(string fs) { var words = getWords(fs); var lines = words.Aggregate(new List<LineToken>() { new LineToken() }, (old, n) => { old.Last().Tokens.Add(n); if (n.Type == Token.NewLine) { if (old.Any() && (old.Last().Tokens.Count == 0)) { return old; } if (old.Any() && ((old.Last().Tokens.First().Type == Token.Tab || old.Last().Tokens.First().Type == Token.NewLine) && old.Last().Tokens.Count == 1)) { old.Last().Tokens.Clear(); return old; } old.Add(new LineToken()); return old; } return old; }); foreach (var a in lines) { if (!(!a.Tokens.Any() || (a.Tokens[0].Type == Token.Class || a.Tokens[0].Type == Token.Macro || a.Tokens[0].Type == Token.Tab))) { } } for (int index = lines.Count - 1; index >= 0; index--) { var lineToken = lines[index]; if (lineToken.Tokens.Count == 0) { lines.RemoveAt(index); continue; } if (lineToken.Tokens[0].Type == Token.Tab) { if (lineToken.Tokens.Count == 1) { lines.RemoveAt(index); continue; } if (lineToken.Tokens[1].Type == Token.NewLine) { lines.RemoveAt(index); continue; } back: if (lineToken.Tokens[1].Type == Token.Tab) { ((TokenTab)lineToken.Tokens[0]).TabIndex += ((TokenTab)lineToken.Tokens[1]).TabIndex; lineToken.Tokens.RemoveAt(1); goto back; } } } int done = 0; int indes = 0; List<List<LineToken>> macros; List<TokenMacroPiece> allMacros = new List<TokenMacroPiece>(); if (lines.Any() && lines[0].Tokens[0].Type == Token.Macro) { macros = lines.Aggregate(new List<List<LineToken>>() { }, delegate(List<List<LineToken>> old, LineToken n) { if (n.Tokens.Count == 0) { return old; } indes = indes + 1; if (done > 0 || n.Tokens[0].Type == Token.Class) { if (done == 0) { done = indes - 2; } return old; } if (n.Tokens[0].Type == Token.Macro) old.Add(new List<LineToken>()); old.Last().Add(n); return old; }); for (int i = done; i >= 0; i--) { lines.RemoveAt(i); } foreach (List<LineToken> macro in macros) { TokenMacroPiece mp = new TokenMacroPiece(); allMacros.Add(mp); mp.Lines = new List<LineToken>(); var lm = macro[0]; TokenEnumerator en = new TokenEnumerator(new[] { lm }); StaticMethods.Assert(en.Current.Type == Token.Macro, "notMacro?"); en.MoveNext(); StaticMethods.Assert(en.Current.Type == Token.SemiColon, ""); en.MoveNext(); List<IToken> mps = new List<IToken>(); while (en.Current.Type != Token.SemiColon) { mps.Add(en.Current); en.MoveNext(); } mp.Macro = mps.ToArray(); StaticMethods.Assert(en.Current.Type == Token.SemiColon, ""); en.MoveNext(); StaticMethods.Assert(en.Current.Type == Token.AnonMethodStart, ""); en.MoveNext(); StaticMethods.Assert(en.Current.Type == Token.Bar, ""); en.MoveNext(); StaticMethods.Assert(en.Current.Type == Token.OpenParen, ""); en.MoveNext(); List<ParamEter> parameters_ = new List<ParamEter>(); if (en.Current.Type != Token.CloseParen) { pback2: bool byRef = false; if (((TokenWord)en.Current).Word.ToLower() == "ref") { byRef = true; en.MoveNext(); } parameters_.Add(new ParamEter() { ByRef = byRef, Name = ((TokenWord)en.Current).Word }); en.MoveNext(); switch (en.Current.Type) { case Token.CloseParen: en.MoveNext(); break; case Token.Comma: en.MoveNext(); goto pback2; break; default: throw new ArgumentOutOfRangeException(); } } mp.Parameters = parameters_.ToArray(); for (int i = 1; i < macro.Count; i++) { mp.Lines.Add(macro[i]); } } } var classes = lines.Aggregate(new List<List<LineToken>>() { }, (old, n) => { if (n.Tokens.Count == 0) { return old; } if (n.Tokens[0].Type == Token.Class) old.Add(new List<LineToken>()); old.Last().Add(n); return old; }); List<Class> someClasses = new List<Class>(); foreach (List<LineToken> @class in classes) { Class c = new Class(); someClasses.Add(c); c.Name = ((TokenWord)@class[0].Tokens[1]).Word; for (int index = 1; index < @class.Count; index++) { LineToken v = @class[index]; if (v.Tokens[0].Type != Token.Tab) throw new AbandonedMutexException(); if (v.Tokens[1].Type != Token.Def) { c.Variables.Add(v); } else { var m = new Method(); if (v.Tokens[2] is TokenOpenParen) m.Name = ".ctor"; else m.Name = ((TokenWord)v.Tokens[2]).Word; if (v.Tokens.Count != 5 + (v.Tokens[2] is TokenOpenParen ? 0 : 1))//tab def name openP closeP newline { for (int i = 3 + (v.Tokens[2] is TokenOpenParen ? 0 : 1); i < v.Tokens.Count - 2; i++) { m.paramNames.Add(((TokenWord)v.Tokens[i]).Word); i++; } } int tabl = ((TokenTab)v.Tokens[0]).TabIndex + 1; index++; for (; index < @class.Count; index++) { if (((TokenTab)@class[index].Tokens[0]).TabIndex < tabl) { index--; break; } m.Lines.Add(@class[index]); } //index++; c.Methods.Add(m); } } } StringBuilder sb = new StringBuilder(); foreach (var tokenMacroPiece in allMacros) { sb.Append(tokenMacroPiece.ToString()); } sb.AppendLine(); foreach (var someClass in someClasses) { Method ctor = someClass.Methods.FirstOrDefault(a => a.Name == ".ctor"); if (ctor == null) { someClass.Methods.Add(ctor = new Method()); ctor.Name = ".ctor"; } for (int index = 0; index < someClass.Variables.Count; index++) { var lineToken = someClass.Variables[index]; ((TokenTab)lineToken.Tokens[0]).TabIndex++; someClass.VariableNames.Add(((TokenWord)lineToken.Tokens[1]).Word); ctor.Lines.Insert(index, lineToken); } someClass.Variables.Clear(); sb.AppendLine(someClass.ToString()); } File.WriteAllText("C:\\spoke.txt", sb.ToString()); //Console.WriteLine(sb); return new Tuple<List<Class>, List<TokenMacroPiece>>(someClasses, allMacros); }
/// <summary> /// Advances the enumerator to the next command in the stream. /// </summary> /// <param name="context">The session context to use when making session based transitions.</param> /// <param name="segments">The list of array segments to read the command from.</param> /// <param name="command">The command that was found.</param> /// <param name="errorResponse">The error response that indicates why a command could not be accepted.</param> /// <returns>true if a valid command was found, false if not.</returns> bool TryMake(SmtpSessionContext context, IReadOnlyList <ArraySegment <byte> > segments, out SmtpCommand command, out SmtpResponse errorResponse) { var tokenEnumerator = new TokenEnumerator(new ByteArrayTokenReader(segments)); return(_stateMachine.TryMake(context, tokenEnumerator, out command, out errorResponse)); }
private SpokeMethodCall checkRunMacro(TokenEnumerator enumerator, int tabIndex, evalInformation inf) { //tm. for (int index = 0; ; index++) { gombo: if (index >= allMacros_.Count) break; var tokenMacroPiece = allMacros_[index]; List<SpokeItem> parameters = new List<SpokeItem>(); var tm = enumerator.Clone(); var outs = tm.OutstandingLine; bool bad = false; for (int i = 0; i < tokenMacroPiece.Macro.Length; i++) { var mp = tokenMacroPiece.Macro[i]; if (mp.Type == Token.Ampersand) { for (int j = i + 1; j < tokenMacroPiece.Macro.Length; j++) { var r = tokenMacroPiece.Macro[j]; if (!outs.Any(a => r.Type == Token.Ampersand || (a.Type == r.Type && (a.Type == Token.Word && r.Type == Token.Word ? ((TokenWord)a).Word == ((TokenWord)r).Word : true)))) { bad = true; break; } } if (bad) { break; } var frf = tokenMacroPiece.Macro.Length == i + 1 ? new TokenNewLine() : tokenMacroPiece.Macro[i + 1]; int selectedLine = 0; IToken tp = null; int selectedToken = tm.tokenIndex; if (frf.Type != Token.NewLine) { for (int j = 0; j < tm.CurrentLines.Count; j++) { for (int ic = selectedToken; ic < tm.CurrentLines[j].Tokens.Count; ic++) { var a = tm.CurrentLines[j].Tokens[ic]; if (tm.CurrentLines[j].Tokens[ic].Type == frf.Type && (a.Type == frf.Type && (a.Type == Token.Word && frf.Type == Token.Word ? ((TokenWord)a).Word == ((TokenWord)frf).Word : true))) { tp = tm.CurrentLines[j].Tokens[ic]; break; } } if (tp != null) { selectedLine = j; break; } else { selectedToken = 0; } } var bf = new TokenAmpersand(); tm.CurrentLines[selectedLine].Tokens.Insert(tm.CurrentLines[selectedLine].Tokens.IndexOf(tp), bf); try { CurrentItem = null; var d = eval(tm, tabIndex, new evalInformation(inf) { CheckMacs = inf.CheckMacs + 1, DontEvalEquals = true, BreakBeforeEqual = true }); parameters.Add((SpokeItem)d); if (d == null || (!(tm.Current.Type == Token.Ampersand || tokenMacroPiece.Macro.Length == i + 1))) { index++; goto gombo; } } catch (Exception e) { index++; goto gombo; } tm.CurrentLine.Tokens.Remove(bf); } else { try { CurrentItem = null; var d = eval(tm, tabIndex, new evalInformation(inf) { CheckMacs = inf.CheckMacs + 1, DontEvalEquals = true, BreakBeforeEqual = true }); parameters.Add((SpokeItem)d); if (d == null) { index++; goto gombo; } } catch (Exception e) { index++; goto gombo; } } } else { if (mp.Type == tm.Current.Type && (mp.Type == Token.Word && tm.Current.Type == Token.Word ? ((TokenWord)mp).Word == ((TokenWord)tm.Current).Word : true)) { tm.MoveNext(); } else { bad = true; break; } } } if (!bad) { SpokeMethodCall ambe = new SpokeMethodCall(); SpokeAnonMethod me = new SpokeAnonMethod(); me.SpecAnon = true; me.Parameters = tokenMacroPiece.Parameters; me.Lines = getLines(new TokenEnumerator(tokenMacroPiece.Lines.ToArray()), 1, inf).ToArray(); me.HasYieldReturn = linesHave(me.Lines, ISpokeLine.YieldReturn); me.HasYield = linesHave(me.Lines, ISpokeLine.Yield); me.HasReturn = linesHave(me.Lines, ISpokeLine.Return); ambe.Parent = me; parameters.Insert(0, new SpokeCurrent()); ambe.Parameters = parameters.ToArray(); enumerator.Set(tm); return ambe; } } return null; }
private SpokeConstruct createObject(TokenEnumerator enumerator, int tabIndex, evalInformation inf) { enumerator.MoveNext(); var sp = new SpokeConstruct(); StaticMethods.Assert(enumerator.Current.Type == Token.Word, enumerator.Current.Type + " Isnt word"); sp.ClassName = ((TokenWord)enumerator.Current).Word; enumerator.MoveNext(); enumerator.MoveNext();//openeparam List<SpokeItem> param_ = new List<SpokeItem>(); if (enumerator.Current.Type != Token.CloseParen) { CurrentItem = null; g: param_.Add((SpokeItem)eval(enumerator, tabIndex, new evalInformation(inf) { ResetCurrentVal = true })); if (enumerator.Current.Type == Token.Comma) { enumerator.MoveNext(); goto g; } } sp.Parameters = param_.ToArray(); enumerator.MoveNext();//closeparam CurrentItem = sp; if (enumerator.Current.Type == Token.OpenCurly) { CurrentItem = dynamicObject(enumerator, tabIndex, inf); } return (SpokeConstruct)CurrentItem; }
public void SetUp() { _tokenizer = new Tokenizer(CONTENT, true, true, '\\', SEPERATORS, new[] {'\''}, null); _tokenEnumerator = _tokenizer.GetTokenEnumerator(); }
private SpokeItem dynamicObject(TokenEnumerator enumerator, int tabIndex, evalInformation inf) { enumerator.MoveNext(); //openesquigly SpokeConstruct cons = (SpokeConstruct)CurrentItem; List<SpokeEqual> param_ = new List<SpokeEqual>(); if (enumerator.Current.Type != Token.CloseCurly) { CurrentItem = null; g: param_.Add((SpokeEqual)eval(enumerator, tabIndex, new evalInformation(inf) { ResetCurrentVal = true })); if (enumerator.Current.Type == Token.Comma) { enumerator.MoveNext(); goto g; } } enumerator.MoveNext();//closesquigly cons.SetVars = param_.Select(a => new SVarItems(((SpokeVariable)a.LeftSide).VariableName, 0, a.RightSide)).ToArray(); return cons; }
public ParseHelper(Tokenizer tokenizer) { _tokenizer = tokenizer.GetTokenEnumerator(); }
/// <summary> /// Advances the enumerator to the next command in the stream. /// </summary> /// <param name="context">The SMTP session context to allow for session based state transitions.</param> /// <param name="tokenEnumerator">The token enumerator to accept the command from.</param> /// <param name="command">The command that was found.</param> /// <param name="errorResponse">The error response that indicates why a command could not be accepted.</param> /// <returns>true if a valid command was found, false if not.</returns> public bool TryMake(SmtpSessionContext context, TokenEnumerator tokenEnumerator, out SmtpCommand command, out SmtpResponse errorResponse) { return(_stateTable.TryMake(context, tokenEnumerator, out command, out errorResponse)); }