本文整理汇总了C#中Parser.MatchAndTake方法的典型用法代码示例。如果您正苦于以下问题:C# Parser.MatchAndTake方法的具体用法?C# Parser.MatchAndTake怎么用?C# Parser.MatchAndTake使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Parser
的用法示例。
在下文中一共展示了Parser.MatchAndTake方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Parse
public Expression Parse(Parser parser, Token token, out bool trailingSemicolon)
{
trailingSemicolon = false;
parser.Take(TokenType.LeftParen);
var expression = parser.ParseExpession();
parser.Take(TokenType.RightParen);
parser.Take(TokenType.LeftBrace);
var branches = new List<SwitchExpression.Branch>();
BlockExpression defaultBlock = null;
while (!parser.Match(TokenType.RightBrace))
{
var conditions = new List<Expression>();
while (parser.MatchAndTake(TokenType.Case))
{
var condition = parser.ParseExpession();
conditions.Add(condition);
parser.Take(TokenType.Colon);
}
if (conditions.Count > 0)
{
var block = ParseBlock(parser);
var branch = new SwitchExpression.Branch(conditions, block);
branches.Add(branch);
continue;
}
if (parser.MatchAndTake(TokenType.Default))
{
parser.Take(TokenType.Colon);
var block = ParseBlock(parser);
defaultBlock = block;
break;
}
var errorToken = parser.Peek();
throw new MondCompilerException(errorToken.FileName, errorToken.Line, "Expected Case or Default but got {0}", errorToken.Type);
}
parser.Take(TokenType.RightBrace);
return new SwitchExpression(token, expression, branches, defaultBlock);
}
示例2: Parse
public Expression Parse(Parser parser, Expression left, Token token)
{
var args = new List<Expression>();
if (!parser.MatchAndTake(TokenType.RightParen))
{
do
{
args.Add(parser.ParseExpession());
} while (parser.MatchAndTake(TokenType.Comma));
parser.Take(TokenType.RightParen);
}
return new CallExpression(token, left, args);
}
示例3: Parse
public Expression Parse(Parser parser, Token token, out bool trailingSemicolon)
{
trailingSemicolon = true;
var declarations = new List<VarExpression.Declaration>();
do
{
var identifier = parser.Take(TokenType.Identifier);
Expression initializer = null;
if (parser.MatchAndTake(TokenType.Assign))
{
initializer = parser.ParseExpession();
}
var declaration = new VarExpression.Declaration(identifier.Contents, initializer);
declarations.Add(declaration);
} while (parser.MatchAndTake(TokenType.Comma));
return new VarExpression(token, declarations);
}
示例4: Parse
public Expression Parse(Parser parser, Token token, out bool trailingSemicolon)
{
trailingSemicolon = false;
var first = true;
var branches = new List<IfExpression.Branch>();
IfExpression.Branch elseBranch = null;
do
{
var isDefaultBlock = !first && !parser.MatchAndTake(TokenType.If);
first = false;
Expression condition = null;
if (!isDefaultBlock)
{
parser.Take(TokenType.LeftParen);
condition = parser.ParseExpession();
parser.Take(TokenType.RightParen);
}
var block = new ScopeExpression(parser.ParseBlock());
var branch = new IfExpression.Branch(condition, block);
if (isDefaultBlock)
elseBranch = branch;
else
branches.Add(branch);
if (isDefaultBlock)
break;
} while (parser.MatchAndTake(TokenType.Else));
return new IfExpression(token, branches, elseBranch);
}
示例5: Parse
public Expression Parse(Parser parser, Token token, out bool trailingSemicolon)
{
trailingSemicolon = false;
string name = null;
var arguments = new List<string>();
BlockExpression body;
// optional name
if (parser.Match(TokenType.Identifier))
{
name = parser.Take(TokenType.Identifier).Contents;
}
// parse argument list
parser.Take(TokenType.LeftParen);
if (!parser.Match(TokenType.RightParen))
{
while (true)
{
var identifier = parser.Take(TokenType.Identifier);
arguments.Add(identifier.Contents);
if (parser.Match(TokenType.RightParen))
break;
parser.Take(TokenType.Comma);
}
}
parser.Take(TokenType.RightParen);
// parse body
if (parser.MatchAndTake(TokenType.Pointy))
{
body = new BlockExpression(new List<Expression>
{
new ReturnExpression(token, parser.ParseExpession())
});
}
else
{
body = parser.ParseBlock(false);
}
return new FunctionExpression(token, name, arguments, body);
}