Parse function handles

This commit is contained in:
Alexander Luzgarev 2018-04-01 18:35:54 +02:00
parent 01dda0cf6f
commit a550b4f96c
4 changed files with 58 additions and 0 deletions

View File

@ -350,5 +350,15 @@ namespace Parser.Tests
Assert.AreEqual(text, actual.FullText); Assert.AreEqual(text, actual.FullText);
} }
[Test]
public void ParseFunctionHandle()
{
var text = "@sqrt";
var sut = CreateParser(text);
var actual = sut.ParseExpression();
Assert.IsInstanceOf<FunctionHandleNode>(actual);
var f = (FunctionHandleNode) actual;
Assert.AreEqual("sqrt", f.IdentifierName.Token.PureToken.LiteralText);
}
} }
} }

View File

@ -549,6 +549,15 @@ namespace Parser
throw new ArgumentException(nameof(kind)); throw new ArgumentException(nameof(kind));
} }
} }
private FunctionHandleNode ParseFunctionHandle()
{
var atSign = EatToken();
var identifierName = EatToken(TokenKind.Identifier);
return Factory.FunctionHandle(
Factory.Token(atSign),
Factory.IdentifierName(identifierName));
}
private ExpressionNode ParseSubExpression(ParseOptions options, Precedence precedence) private ExpressionNode ParseSubExpression(ParseOptions options, Precedence precedence)
{ {
@ -561,6 +570,10 @@ namespace Parser
var operand = ParseSubExpression(options, newPrecedence); var operand = ParseSubExpression(options, newPrecedence);
lhs = Factory.UnaryPrefixOperationExpression(Factory.Token(operation), operand); lhs = Factory.UnaryPrefixOperationExpression(Factory.Token(operation), operand);
} }
else if (CurrentToken.Kind == TokenKind.At)
{
return ParseFunctionHandle();
}
else else
{ {
lhs = ParseTerm(options); lhs = ParseTerm(options);

View File

@ -541,5 +541,23 @@ namespace Parser
SetParent(result); SetParent(result);
return result; return result;
} }
public FunctionHandleNode FunctionHandle(
TokenNode atSign,
IdentifierNameNode identifierName)
{
var children = new List<SyntaxNode>
{
atSign,
identifierName
};
var result = new FunctionHandleNode(
children,
atSign,
identifierName);
SetParent(result);
return result;
}
} }
} }

View File

@ -586,4 +586,21 @@ namespace Parser
ClosingBracket = closingBracket; ClosingBracket = closingBracket;
} }
} }
public class FunctionHandleNode : ExpressionNode
{
public TokenNode AtSign { get; }
public IdentifierNameNode IdentifierName { get; }
public FunctionHandleNode(
List<SyntaxNode> children,
TokenNode atSign,
IdentifierNameNode identifierName) : base(children)
{
AtSign = atSign;
IdentifierName = identifierName;
}
}
} }