本文整理汇总了C#中Parser.ParseExpression方法的典型用法代码示例。如果您正苦于以下问题:C# Parser.ParseExpression方法的具体用法?C# Parser.ParseExpression怎么用?C# Parser.ParseExpression使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Parser
的用法示例。
在下文中一共展示了Parser.ParseExpression方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Parse
public IExpression Parse(Parser parser, IExpression left, Token token)
{
var thenArm = parser.ParseExpression();
parser.Consume(TokenType.Colon);
var elseArm = parser.ParseExpression(Precedence.Conditional - 1);
return new ConditionalExpression(left, thenArm, elseArm);
}
示例2: Parse
public override Statement Parse(Parser parser, Lexer.Token current)
{
string name = null;
if (parser.Peek().Type == "IDENTIFIER")
{
name = parser.Current.Lexeme;
parser.Consume("IDENTIFIER");
}
parser.Consume("LEFTPAREN");
var parameters = ParseParameterList(parser).ToArray();
parser.Consume("RIGHTPAREN");
var token = parser.Peek();
if (token.Type == "LEFTBRACE")
{
var body = parser.ParseNext();
return new FunctionDefinitionExpr(new IdentifierExpr(name), parameters, (Statement)body,
new IdentifierExpr("UNKNOWN"));
}
else if (token.Type == "RIGHTARROW")
{
parser.Consume("RIGHTARROW");
var body = parser.ParseExpression(0);
parser.Consume("SEMICOLON");
return new LambdaDefinitionExpr(new IdentifierExpr(name), parameters, body,
new IdentifierExpr("UNKNOWN"));
}
throw new ParseException("Malformed function defintion");
}
示例3: Parse
public IExpression Parse(Parser parser, IExpression left, Token token)
{
// To handle right-associative operators like "^", we allow a slightly
// lower precedence when parsing the right-hand side. This will let a
// parselet with the same precedence appear on the right, which will then
// take *this* parselet's result as its left-hand argument.
var right = parser.ParseExpression(_precedence - (_right ? 1 : 0));
return new OperatorExpression(left, _operator, right);
}
示例4: Parse
public override Expression Parse(Parser p, Expression left, Token t)
{
return new BinaryOperatorExpr
{
Op = t.Type,
Left = left,
Right = p.ParseExpression(Precedence),
Position = left.Position
};
}
示例5: Parse
public Expression Parse(Parser parser, Expression lhs, Token current)
{
var thenExpression = parser.ParseNext();
parser.Consume("COLON");
var elseExpression = parser.ParseExpression(Precedence - 1);
return new ConditionalExpr(lhs, thenExpression, elseExpression);
}
示例6: ShouldGetVariableFromStringWithRealCoefficientAndDivisionSymbol
public void ShouldGetVariableFromStringWithRealCoefficientAndDivisionSymbol()
{
var str = "2/4*x";
var target = new Parser();
Expression result = target.ParseExpression(str);
Assert.AreEqual("x", result.Variable.Name);
Assert.AreEqual(0.5, result.Coefficient);
}
示例7: ShouldGetVariableFromStringSimple
public void ShouldGetVariableFromStringSimple()
{
var str = "x";
var target = new Parser();
Expression result = target.ParseExpression(str);
Assert.AreEqual("x",result.Variable.Name);
Assert.AreEqual(1, result.Coefficient);
}
示例8: ShouldGetVariableFromStringOnlyTo4NumberPastPoint
public void ShouldGetVariableFromStringOnlyTo4NumberPastPoint()
{
var str = "2/7*x";
var target = new Parser();
Expression result = target.ParseExpression(str);
Assert.AreEqual("x", result.Variable.Name);
Assert.AreEqual(0.286, result.Coefficient);
}
示例9: Parse
public override Statement Parse(Parser parser, Lexer.Token current)
{
parser.Consume("LEFTPAREN");
var condition = parser.ParseExpression(0);
parser.Consume("RIGHTPAREN");
var trueBlock = ParseStatement(parser);
var falseBlock = parser.ConsumeOptional("ELSE") ? ParseStatement(parser) : null;
return new IfStmt(condition, trueBlock, falseBlock);
}
示例10: Parse
public IExpression Parse(Parser parser, IExpression left, Token token)
{
var right = parser.ParseExpression((int)Precedence.Assignment - 1);
if (!(left is NameExpression))
{
throw new ParseException("The left-hand side of an assignment must be a name.");
}
var name = ((NameExpression)left).Name;
return new AssignExpression(name, right);
}
示例11: ParseParameterList
private List<VarDefinitionStmt> ParseParameterList(Parser parser)
{
var parameters = new List<VarDefinitionStmt>();
if (parser.Peek().Type == "RIGHTPAREN")
return parameters;
while (true)
{
var name = parser.ParseExpression(0);
if(!(name is IdentifierExpr))
throw new ParseException("Expected parameter name");
Expression type = null;
if (parser.ConsumeOptional("COLON"))
{
type = parser.ParseExpression(0);
if (!(type is IdentifierExpr))
throw new ParseException("Expected parameter type");
}
else
{
type = new IdentifierExpr("dynamic");
}
parameters.Add(new VarDefinitionStmt((IdentifierExpr)name, (IdentifierExpr)type, false, null));
if (parser.Peek().Type == "RIGHTPAREN")
break;
parser.Consume("COMMA");
}
return parameters;
}
示例12: ParseArguments
private IEnumerable<Expression> ParseArguments(Parser parser)
{
if (parser.Peek().Type == "RIGHTPAREN")
yield break;
while (true)
{
yield return parser.ParseExpression(0);
if (parser.Peek().Type == "RIGHTPAREN")
yield break;
parser.Consume("COMMA");
}
}
示例13: Parse
public override Statement Parse(Parser parser, Lexer.Token current)
{
parser.Consume("LEFTPAREN");
var condition = parser.ParseExpression(0);
parser.Consume("RIGHTPAREN");
var block = ParseStatement(parser);// parser.ParseNext();
if (!(block is Statement))
throw new ParseException("Invalid statement in block.");
/*
if (!(block is IBlockStatement))
parser.Consume("SEMICOLON");
*/
return new WhileStmt(condition, block as Statement);
}
示例14: Parse
public IExpression Parse(Parser parser, IExpression left, Token token)
{
// Parse the comma-separated arguments until we hit, ")".
var args = new List<IExpression>();
// There may be no arguments at all.
if (!parser.Match(TokenType.RightParen))
{
do
{
args.Add(parser.ParseExpression());
}
while (parser.Match(TokenType.Comma));
parser.Consume(TokenType.RightParen);
}
return new CallExpression(left, args);
}
示例15: Parse
public override Statement Parse(Parser parser, Lexer.Token current)
{
var name = parser.Current.Lexeme;
parser.Consume("IDENTIFIER");
string type = null;
if (parser.Peek().Type == "COLON")
type = ParseTypeSpecified(parser);
Expression initialValue = null;
if (parser.ConsumeOptional("ASSIGNMENT"))
initialValue = parser.ParseExpression(0);
else if(_constVariables)
throw new ParseException("Const variable declarations must have an initialiser.");
parser.Consume("SEMICOLON");
return new VarDefinitionStmt(new IdentifierExpr(name), new IdentifierExpr(type), _constVariables, initialValue);
}