当前位置: 首页>>代码示例>>C#>>正文


C# TokenStream.PopIfPresent方法代码示例

本文整理汇总了C#中TokenStream.PopIfPresent方法的典型用法代码示例。如果您正苦于以下问题:C# TokenStream.PopIfPresent方法的具体用法?C# TokenStream.PopIfPresent怎么用?C# TokenStream.PopIfPresent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TokenStream的用法示例。


在下文中一共展示了TokenStream.PopIfPresent方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ParseLambda

		public static Expression ParseLambda(TokenStream tokens)
		{
			tokens.SkipWhitespace();
			if (tokens.IsNext("lambda"))
			{
				Token lambdaToken = tokens.PopExpected("lambda");
				List<Token> argNames = new List<Token>();
				List<Expression> argValues = new List<Expression>();

				if (!tokens.PopIfPresent(":"))
				{
					bool nextAllowed = true;
					while (!tokens.PopIfPresent(":"))
					{
						if (!nextAllowed) tokens.PopExpected(":"); // throws
						Token argName = tokens.Pop();
						if (!Util.IsIdentifier(argName)) throw new ParserException(argName, "Invalid lambda arg name.");
						Expression argValue = null;
						if (tokens.PopIfPresent("="))
						{
							argValue = ExpressionParser.Parse(tokens);
						}

						argNames.Add(argName);
						argValues.Add(argValue);

						nextAllowed = tokens.PopIfPresent(",");
					}
				}
			}

			return ParseTernary(tokens);
		}
开发者ID:blakeohare,项目名称:crython,代码行数:33,代码来源:ExpressionParser.cs

示例2: ParseImport

		private Executable ParseImport(TokenStream tokens, int indention)
		{
			tokens.SkipWhitespace();

			List<Token> fromChain = null;
			List<Token> importChain = null;
			List<Token> asChain = null;
			Token firstToken = null;

			if (tokens.PeekValue() == "from")
			{
				firstToken = tokens.Pop();
				fromChain = ParseDotChainForImport(tokens);
			}

			firstToken = firstToken ?? tokens.PopExpected("import");
			importChain = ParseDotChainForImport(tokens);

			if (tokens.PopIfPresent("as"))
			{
				asChain = ParseDotChainForImport(tokens);
				if (asChain.Count > 1) throw new ParserException(asChain[0], "Expected: variable");
			}

			return new ImportStatement(firstToken, importChain, fromChain, asChain == null ? null : asChain[0]);
		}
开发者ID:blakeohare,项目名称:crython,代码行数:26,代码来源:ExecutableParser.cs

示例3: ParseBooleanOr

		public static Expression ParseBooleanOr(TokenStream tokens)
		{
			Expression left = ParseBooleanAnd(tokens);
			if (tokens.PopIfPresent("or"))
			{
				Expression right = ParseBooleanOr(tokens);
				return new BooleanCombinator(left, right, "or");
			}
			return left;
		}
开发者ID:blakeohare,项目名称:crython,代码行数:10,代码来源:ExpressionParser.cs

示例4: ParseBooleanAnd

		public static Expression ParseBooleanAnd(TokenStream tokens)
		{
			tokens.SkipWhitespace();
			Expression left = ParseBooleanNot(tokens);
			if (tokens.PopIfPresent("and"))
			{
				Expression right = ParseBooleanAnd(tokens);
				return new BooleanCombinator(left, right, "and");
			}
			return left;
		}
开发者ID:blakeohare,项目名称:crython,代码行数:11,代码来源:ExpressionParser.cs

示例5: ParseTernary

		public static Expression ParseTernary(TokenStream tokens)
		{
			Expression left = ParseBooleanOr(tokens);
			if (tokens.PopIfPresent("if"))
			{
				Expression condition = ParseBooleanOr(tokens);
				tokens.PopExpected("else");
				Expression right = ParseTernary(tokens);
				return new Ternary(condition, left, right);
			}
			return left;
		}
开发者ID:blakeohare,项目名称:crython,代码行数:12,代码来源:ExpressionParser.cs

示例6: ParseDotChainForImport

		private List<Token> ParseDotChainForImport(TokenStream tokens)
		{
			List<Token> output = new List<Token>() {tokens.Pop()};
			if (output[0].Value != "*" && !Util.IsIdentifier(output[0]))
			{
				throw new ParserException(output[0], "Invalid import statement.");
			}

			while (tokens.PopIfPresent("."))
			{
				Token next = tokens.Pop();
				if (next.Value != "*" && !Util.IsIdentifier(next))
				{
					throw new ParserException(next, "Invalid import statement.");
				}
				output.Add(next);
			}

			return output;
		}
开发者ID:blakeohare,项目名称:crython,代码行数:20,代码来源:ExecutableParser.cs

示例7: ParseDef

		private Executable ParseDef(TokenStream tokens, int currentIndent)
		{
			Token defToken = tokens.PopExpected("def");
			Token nameToken = tokens.Pop();
			if (!Util.IsIdentifier(nameToken))
			{
				throw new ParserException(nameToken, "Invalid function name.");
			}

			tokens.PopExpected("(");
			List<Token> args = new List<Token>();
			List<Expression> argValues = new List<Expression>();
			while (!tokens.PopIfPresent(")"))
			{
				if (args.Count > 0)
				{
					tokens.PopExpected(",");
				}
				Token argToken = tokens.Pop();
				if (!Util.IsIdentifier(argToken)) throw new ParserException(argToken, "Invalid argument name.");
				Expression argValue = null;
				if (tokens.PopIfPresent("="))
				{
					argValue = ExpressionParser.Parse(tokens);
				}
				args.Add(argToken);
				argValues.Add(argValue);
			}
			tokens.PopExpected(":");

			IList<Executable> body = this.ParseBlock(tokens, currentIndent, false);
			return new FunctionDefinition(defToken, nameToken, args, argValues, body);
		}
开发者ID:blakeohare,项目名称:crython,代码行数:33,代码来源:ExecutableParser.cs

示例8: ParseClass

		private Executable ParseClass(TokenStream tokens, int currentIndention)
		{
			Token classToken = tokens.PopExpected("class");
			Token nameToken = tokens.Pop();
			if (!Util.IsIdentifier(nameToken)) throw new ParserException(nameToken, "Invalid class name.");
			Token baseClassName = null;
			if (tokens.PopIfPresent("("))
			{
				baseClassName = tokens.Pop();
				if (!Util.IsIdentifier(baseClassName)) throw new ParserException(baseClassName, "Invalid base class name.");
				tokens.PopExpected(")");
			}
			tokens.PopExpected(":");
			IList<Executable> members = this.ParseBlock(tokens, currentIndention, false);
			List<Executable> membersFiltered = new List<Executable>();
			foreach (Executable member in members)
			{
				if (member is PassStatement)
				{
					// this is fine.
				}
				else if (!(member is FunctionDefinition))
				{
					throw new ParserException(member.FirstToken, "Non function members of classes are not supported yet.");
				}
				else
				{
					membersFiltered.Add(member);
				}
			}

			return new ClassDefinition(classToken, nameToken, baseClassName, membersFiltered);
		}
开发者ID:blakeohare,项目名称:crython,代码行数:33,代码来源:ExecutableParser.cs

示例9: ParseRootEntity

        private static Expression ParseRootEntity(TokenStream tokens)
        {
            tokens.SkipWhitespace();
            string next = tokens.PeekValue();
            if (next == "True" || next == "False")
            {
                Token token = tokens.Pop();
                return new BooleanConstant(token, next == "True");
            }
            else if (next == "None")
            {
                Token token = tokens.Pop();
                return new NullConstant(token);
            }
            else if (next.StartsWith("'") || next.StartsWith("\""))
            {
                Token token = tokens.Pop();
                int quoteSize = next.StartsWith("'''") || next.StartsWith("\"\"\"") ? 3 : 1;
                return new StringConstant(token, Util.RemoveEscapeSequences(token, next.Substring(quoteSize, next.Length - quoteSize * 2)));
            }
            else if (next.StartsWith("r'") || next.StartsWith("r\""))
            {
                Token token = tokens.Pop();
                int quoteSize = next.StartsWith("r'''") || next.StartsWith("r\"\"\"") ? 3 : 1;

                return new StringConstant(token, next.Substring(quoteSize + 1, next.Length - quoteSize * 2 - 1));
            }
            else if (next == "(")
            {
                // Tuples or parentehsis
                Token parenthesisToken = tokens.Pop();
                List<Expression> parenthesisExpressions = new List<Expression>();
                bool nextAllowed = true;
                while (!tokens.PopIfPresent(")"))
                {
                    if (!nextAllowed) tokens.PopExpected(")"); // throws
                    parenthesisExpressions.Add(Parse(tokens));
                    nextAllowed = tokens.PopIfPresent(",");
                    tokens.SkipWhitespace();
                }

                if (parenthesisExpressions.Count > 1 || nextAllowed)
                {
                    return new InlineTuple(parenthesisToken, parenthesisExpressions);
                }
                else
                {
                    return new ParenthesisGroup(parenthesisToken, parenthesisExpressions[0]);
                }
            }
            else if (next == "[")
            {
                Token bracketToken = tokens.Pop();
                List<Expression> listItems = new List<Expression>();
                bool nextAllowed = true;
                while (!tokens.PopIfPresent("]"))
                {
                    if (!nextAllowed) tokens.PopExpected("]"); // throws
                    listItems.Add(Parse(tokens));
                    nextAllowed = tokens.PopIfPresent(",");
                    tokens.SkipWhitespace();
                }
                return new InlineList(bracketToken, listItems);
            }
            else if (next == "{")
            {
                Token braceToken = tokens.Pop();
                List<Expression> dictionaryKeys = new List<Expression>();
                List<Expression> dictionaryValues = new List<Expression>();
                bool nextAllowed = true;
                while (!tokens.PopIfPresent("}"))
                {
                    if (!nextAllowed) tokens.PopExpected("}"); // throws
                    dictionaryKeys.Add(Parse(tokens));
                    tokens.PopExpected(":");
                    dictionaryValues.Add(Parse(tokens));
                    nextAllowed = tokens.PopIfPresent(",");
                    tokens.SkipWhitespace();
                }
                return new InlineDictionary(braceToken, dictionaryKeys, dictionaryValues);
            }
            else if (next.StartsWith("0x") || next.StartsWith("0o") || next.StartsWith("0X") || next.StartsWith("0O"))
            {
                Token integerToken = tokens.Pop();
                int radix = next.ToLowerInvariant().StartsWith("0x") ? 16 : 8;
                string stringValue = next.Substring(2);
                if (stringValue.Length == 0)
                {
                    throw new ParserException(integerToken, "Invalid base " + radix + " constant.");
                }
                int value = Util.ParseNumber(integerToken, stringValue, radix);
                return new IntegerConstant(integerToken, value);
            }
            else if (next[0] >= '0' && next[0] <= '9')
            {
                Token numberToken = tokens.Pop();
                if (next.Contains('.') || next.Contains('e') || next.Contains('E'))
                {
                    double value = Util.ParseDouble(numberToken);
                    return new FloatConstant(numberToken, value);
//.........这里部分代码省略.........
开发者ID:blakeohare,项目名称:pyweek-sentientstorage,代码行数:101,代码来源:ExpressionParser.cs

示例10: ParseEntityWithSuffix

        private static Expression ParseEntityWithSuffix(TokenStream tokens)
        {
            Expression expression = ParseRootEntity(tokens);
            bool nextAllowed;
            bool keepGoing = true;
            string next = tokens.PeekValue();

            while (keepGoing)
            {
                switch (next)
                {
                    case "[":
                        // indexing or slicing
                        Token bracketToken = tokens.Pop();
                        List<Expression> sliceComponents = new List<Expression>();
                        nextAllowed = true;
                        while (!tokens.PopIfPresent("]"))
                        {
                            if (!nextAllowed) tokens.PopExpected("]"); // throws

                            if (tokens.IsNext(":"))
                            {
                                sliceComponents.Add(null);
                            }
                            else
                            {
                                sliceComponents.Add(Parse(tokens));
                            }
                            nextAllowed = tokens.PopIfPresent(":");
                        }
                        if (nextAllowed)
                        {
                            sliceComponents.Add(null);
                        }
                        if (sliceComponents.Count == 0)
                        {
                            throw new ParserException(bracketToken, "Unexpected token.");
                        }
                        else if (sliceComponents.Count == 1)
                        {
                            expression = new IndexExpression(expression, bracketToken, sliceComponents[0]);
                        }
                        else if (sliceComponents.Count <= 3)
                        {
                            expression = new SliceExpression(expression, bracketToken, sliceComponents);
                        }
                        else
                        {
                            throw new ParserException(bracketToken, "Slice expression has too many components.");
                        }
                        break;
                    case "(":
                        // function call
                        Token openParen = tokens.Pop();
                        nextAllowed = true;
                        List<Expression> args = new List<Expression>();
                        while (!tokens.PopIfPresent(")"))
                        {
                            if (!nextAllowed) tokens.PopExpected(")"); // throws
                            args.Add(Parse(tokens));
                            nextAllowed = tokens.PopIfPresent(",");
                        }

                        expression = new FunctionInvocation(expression, args);
                        break;
                    case ".":
                        // dot field
                        Token dotToken = tokens.Pop();
                        Token fieldToken = tokens.Pop();
                        if (!Util.IsIdentifier(fieldToken))
                        {
                            throw new ParserException(fieldToken, "Invalid field.");
                        }
                        expression = new DotField(expression, dotToken, fieldToken, fieldToken.Value);
                        break;
                    default:
                        keepGoing = false;
                        break;
                }
                next = tokens.PeekValue();
            }

            return expression;
        }
开发者ID:blakeohare,项目名称:pyweek-sentientstorage,代码行数:84,代码来源:ExpressionParser.cs


注:本文中的TokenStream.PopIfPresent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。