本文整理汇总了C#中TokenStream.PopExpected方法的典型用法代码示例。如果您正苦于以下问题:C# TokenStream.PopExpected方法的具体用法?C# TokenStream.PopExpected怎么用?C# TokenStream.PopExpected使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TokenStream
的用法示例。
在下文中一共展示了TokenStream.PopExpected方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}
示例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]);
}
示例3: 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;
}
示例4: 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);
}
示例5: 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);
}
示例6: ParseIfLikeThing
private Executable ParseIfLikeThing(TokenStream tokens, int currentIndention)
{
// This is used for if, elif, else, while, and for, because they are all similar.
Token token = tokens.Pop();
string tokenValue = token.Value;
Expression condition = null;
Token forIterator = null;
if (tokenValue == "for")
{
forIterator = tokens.Pop();
if (!Util.IsIdentifier(forIterator)) throw new ParserException(forIterator, "Expected variable name.");
tokens.PopExpected("in");
}
if (tokenValue != "else")
{
condition = ExpressionParser.Parse(tokens);
}
tokens.PopExpected(":");
IList<Executable> body = this.ParseBlock(tokens, currentIndention, false);
if (tokenValue == "for") return new ForEachLoop(token, forIterator, condition, body);
if (tokenValue == "while") return new WhileLoop(token, condition, body);
return new IfRawComponent(token, condition, body);
}
示例7: ParseReturn
private Executable ParseReturn(TokenStream tokens, int indention)
{
tokens.SkipWhitespace();
Token token = tokens.PopExpected("return");
Expression value = null;
if (tokens.HasMore && tokens.PeekType() != TokenType.NEWLINE)
{
value = ExpressionParser.Parse(tokens);
}
return new ReturnStatement(token, value);
}
示例8: 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);
//.........这里部分代码省略.........
示例9: 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;
}