コード例 #1
0
ファイル: Parser.cs プロジェクト: xgnit/EditorGuidelines
        private static bool TryParseStrokeParameters(TokenEnumerator tokensEnumerator, out StrokeParameters strokeParameters)
        {
            strokeParameters = null;

            // Pixel width (stroke thickness)
            var token = tokensEnumerator.Current;

            if (!token.EndsWith("px", StringComparison.Ordinal))
            {
                return(false);
            }

            if (!double.TryParse(token.Substring(0, token.Length - 2), out var strokeThickness))
            {
                return(false);
            }

            if (strokeThickness < 0 || strokeThickness > 50)
            {
                return(false);
            }

            strokeParameters = new StrokeParameters
            {
                Brush           = new SolidColorBrush(Colors.Black),
                StrokeThickness = strokeThickness
            };

            if (!tokensEnumerator.MoveNext())
            {
                return(true);
            }

            // Line style
            token = tokensEnumerator.Current;
            if (Enum.TryParse <LineStyle>(token, ignoreCase: true, out var lineStyle))
            {
                strokeParameters.LineStyle = lineStyle;
            }

            if (!tokensEnumerator.MoveNext())
            {
                return(true);
            }

            // Color
            token = tokensEnumerator.Current;
            if (TryParseColor(token, out var color))
            {
                strokeParameters.Brush = new SolidColorBrush(color);
            }

            // Ignore trailing tokens.
            return(true);
        }
コード例 #2
0
        private IdentifierToken AcceptIdentifier()
        {
            var identifier = tokens.Current as IdentifierToken;

            if (!(identifier is null))
            {
                tokens.MoveNext();
            }

            return(identifier);
        }
コード例 #3
0
        public void TestContextAndTab()
        {
            _tokenizer       = new Tokenizer(LINE_WITH_TAB, true, true, '\\', SEPERATORS, new[] { '\'' }, null);
            _tokenEnumerator = _tokenizer.GetTokenEnumerator();
            Assert.IsTrue(_tokenEnumerator.MoveNext());
            ParseContext context = _tokenEnumerator.Token.Context;

            Assert.That(context.LineWithPosition, Is.EqualTo(LINE_WITH_TAB_WITHPOINTER));
        }
コード例 #4
0
ファイル: TokenEnumeratorTests.cs プロジェクト: Tom01098/Ibis
        public void Enumerate()
        {
            var tokens = new List <Token>
            {
                new IdentifierToken("x"),
                new SymbolToken(SymbolType.Equals),
                new IdentifierToken("3")
            };

            var enumerator = new TokenEnumerator(tokens.ToArray());

            Assert.AreEqual(new IdentifierToken("x"), enumerator.Current);
            Assert.AreEqual(true, enumerator.MoveNext());
            Assert.AreEqual(new SymbolToken(SymbolType.Equals), enumerator.Current);
            Assert.AreEqual(true, enumerator.MoveNext());
            Assert.AreEqual(new IdentifierToken("3"), enumerator.Current);
            Assert.AreEqual(false, enumerator.MoveNext());
            Assert.AreEqual(null, enumerator.Current);
        }
コード例 #5
0
ファイル: TokenEnumeratorTests.cs プロジェクト: Tom01098/Ibis
        public void NestedBacktracking()
        {
            var tokens = new List <Token>
            {
                new IdentifierToken("x"),
                new SymbolToken(SymbolType.Equals),
                new IdentifierToken("3")
            };

            var enumerator = new TokenEnumerator(tokens.ToArray());

            Assert.AreEqual(new IdentifierToken("x"), enumerator.Current);
            Assert.AreEqual(true, enumerator.MoveNext());
            enumerator.SetBacktrackPoint();
            Assert.AreEqual(new SymbolToken(SymbolType.Equals), enumerator.Current);
            Assert.AreEqual(true, enumerator.MoveNext());
            enumerator.SetBacktrackPoint();
            Assert.AreEqual(new IdentifierToken("3"), enumerator.Current);
            Assert.AreEqual(false, enumerator.MoveNext());
            enumerator.Backtrack();
            Assert.AreEqual(new IdentifierToken("3"), enumerator.Current);
            Assert.AreEqual(false, enumerator.MoveNext());
            enumerator.Backtrack();
            Assert.AreEqual(new SymbolToken(SymbolType.Equals), enumerator.Current);
            Assert.AreEqual(true, enumerator.MoveNext());
            Assert.AreEqual(new IdentifierToken("3"), enumerator.Current);
            Assert.AreEqual(false, enumerator.MoveNext());
            Assert.AreEqual(null, enumerator.Current);
        }
コード例 #6
0
        /// <summary>Compare strings.</summary>
        public int Compare(string x, string y)
        {
            // Token enumerators
            TokenEnumerator x_etor = new TokenEnumerator(x), y_etor = new TokenEnumerator(y);

            // Has next tokens
            bool x_next = x_etor.MoveNext(), y_next = y_etor.MoveNext();
            // Get tokens
            Token x_token = x_etor.Current, y_token = y_etor.Current;

            // Iterate
            while (x_next && y_next)
            {
                // Equal length
                if (x_token.Length == y_token.Length || x_token.Kind == Kind.Numeric || y_token.Kind == Kind.Numeric)
                {
                    // Compare tokens
                    int d = tokenComparer.Compare(x_token, y_token);
                    // Difference
                    if (d != 0)
                    {
                        return(d);
                    }
                    // Read more
                    x_next  = x_etor.MoveNext();
                    y_next  = y_etor.MoveNext();
                    x_token = x_etor.Current;
                    y_token = y_etor.Current;
                }
                // x is shorter
                else if (x_token.Length < y_token.Length)
                {
                    Token y_left = y_token.Substring(0, x_token.Length);
                    // Compare tokens
                    int d = tokenComparer.Compare(x_token, y_left);
                    // Difference
                    if (d != 0)
                    {
                        return(d);
                    }
                    // Read more
                    y_token = y_token.Substring(x_token.Length, y_token.Length - x_token.Length);
                    x_next  = x_etor.MoveNext();
                    x_token = x_etor.Current;
                }
                else
                // y is shorter
                {
                    Token x_left = x_token.Substring(0, y_token.Length);
                    // Compare tokens
                    int d = tokenComparer.Compare(x_left, y_token);
                    // Difference
                    if (d != 0)
                    {
                        return(d);
                    }
                    // Read more
                    x_token = x_token.Substring(y_token.Length, x_token.Length - y_token.Length);
                    y_next  = y_etor.MoveNext();
                    y_token = y_etor.Current;
                }
            }

            // Has more x, end of y.
            if (x_next && !y_next)
            {
                return(1);
            }
            // End of x, has more x.
            if (!x_next && y_next)
            {
                return(-1);
            }
            // Equals
            return(0);
        }
コード例 #7
0
ファイル: ParseContextTest.cs プロジェクト: rslijp/sharptiles
        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));
        }
コード例 #8
0
        private bool IsCreateStatement(ParseContext context, out SectionType type, out int endIndex)
        {
            using var k = new TokenEnumerator(context, TokenType.Keyword);

            endIndex = -1;
            type     = default;

            // CREATE
            if (!k.MoveNext() || !Create.EqualsIgnoreCase(k.Current))
            {
                return(false);
            }

            if (!k.MoveNext())
            {
                return(false);
            }

            // .. [OR REPLACE]
            if (Or.EqualsIgnoreCase(k.Current))
            {
                if (!k.MoveNext() || !Replace.EqualsIgnoreCase(k.Current) || !k.MoveNext())
                {
                    return(false);
                }
            }

            endIndex = k.Index;

            if (Procedure.EqualsIgnoreCase(k.Current))
            {
                type = SectionType.CreateProcedure;
                return(true);
            }

            if (Function.EqualsIgnoreCase(k.Current))
            {
                type = SectionType.CreateFunction;
                return(true);
            }

            if (Package.EqualsIgnoreCase(k.Current))
            {
                type = SectionType.CreatePackage;
                return(true);
            }

            if (Trigger.EqualsIgnoreCase(k.Current))
            {
                type = SectionType.CreateTrigger;
                return(true);
            }

            if (Type.EqualsIgnoreCase(k.Current))
            {
                type = SectionType.CreateType;
                return(true);
            }

            return(false);
        }
コード例 #9
0
 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;
	}
コード例 #10
0
		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;



		}
コード例 #11
0
		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;
		}
コード例 #12
0
		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);
		}
コード例 #13
0
		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;
		}
コード例 #14
0
		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; 
		}
コード例 #15
0
        public void EscapedToken()
        {
            Assert.IsTrue(_tokenEnumerator.MoveNext());
            Assert.IsTrue(_tokenEnumerator.MoveNext());
            Assert.IsTrue(_tokenEnumerator.MoveNext());
            Assert.That(_tokenEnumerator.Token.Type, Is.EqualTo(TokenType.Seperator));

            ParseContext context = _tokenEnumerator.Token.Context;

            Assert.That(context.Text, Is.EqualTo(CONTENT));
            Assert.That(context.Line, Is.EqualTo(LINE2));
            Assert.That(context.LineNumber, Is.EqualTo(2));
            Assert.That(context.Context.Count, Is.EqualTo(4));
            Assert.That(context.LineWithPosition, Is.EqualTo(LINE2_WITHPOINTER));
        }
コード例 #16
0
        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);
        }