Parse compound names in function handles

This commit is contained in:
Alexander Luzgarev 2018-04-01 20:00:16 +02:00
parent b033cb6f7d
commit e5d0b0fe2e
5 changed files with 64 additions and 10 deletions

View File

@ -358,7 +358,20 @@ namespace Parser.Tests
var actual = sut.ParseExpression(); var actual = sut.ParseExpression();
Assert.IsInstanceOf<NamedFunctionHandleNode>(actual); Assert.IsInstanceOf<NamedFunctionHandleNode>(actual);
var f = (NamedFunctionHandleNode) actual; var f = (NamedFunctionHandleNode) actual;
Assert.AreEqual("sqrt", f.IdentifierName.Token.PureToken.LiteralText); Assert.AreEqual(1, f.FunctionName.Names.Count);
Assert.AreEqual("sqrt", f.FunctionName.Names[0].Token.PureToken.LiteralText);
Assert.AreEqual(text, actual.FullText);
}
[Test]
public void ParseFunctionHandleWithCompoundName()
{
var text = "@a.b.c";
var sut = CreateParser(text);
var actual = sut.ParseExpression();
Assert.IsInstanceOf<NamedFunctionHandleNode>(actual);
var f = (NamedFunctionHandleNode) actual;
Assert.AreEqual(3, f.FunctionName.Names.Count);
Assert.AreEqual(text, actual.FullText); Assert.AreEqual(text, actual.FullText);
} }

View File

@ -554,15 +554,32 @@ namespace Parser
} }
} }
private CompoundNameNode ParseCompoundName()
{
var lastToken = EatToken(TokenKind.Identifier);
var firstName = Factory.IdentifierName(lastToken);
var nodes = new List<SyntaxNode> {firstName};
while (CurrentToken.Kind == TokenKind.Dot
&& !lastToken.TrailingTrivia.Any())
{
var dot = Factory.Token(EatToken());
nodes.Add(dot);
lastToken = EatToken(TokenKind.Identifier);
nodes.Add(Factory.IdentifierName(lastToken));
}
return Factory.CompoundName(nodes);
}
private FunctionHandleNode ParseFunctionHandle() private FunctionHandleNode ParseFunctionHandle()
{ {
var atSign = EatToken(); var atSign = EatToken();
if (CurrentToken.Kind == TokenKind.Identifier) if (CurrentToken.Kind == TokenKind.Identifier)
{ {
var identifierName = EatToken(TokenKind.Identifier); var compoundName = ParseCompoundName();
return Factory.NamedFunctionHandle( return Factory.NamedFunctionHandle(
Factory.Token(atSign), Factory.Token(atSign),
Factory.IdentifierName(identifierName)); compoundName);
} else if (CurrentToken.Kind == TokenKind.OpeningBracket) } else if (CurrentToken.Kind == TokenKind.OpeningBracket)
{ {
var inputs = ParseFunctionInputDescription(); var inputs = ParseFunctionInputDescription();

View File

@ -323,6 +323,17 @@ namespace Parser
return result; return result;
} }
public CompoundNameNode CompoundName(List<SyntaxNode> nodes)
{
var result = new CompoundNameNode(
nodes,
nodes
.OfType<IdentifierNameNode>()
.ToList());
SetParent(result);
return result;
}
public ArrayLiteralExpressionNode ArrayLiteralExpression( public ArrayLiteralExpressionNode ArrayLiteralExpression(
TokenNode openingSquareBracket, TokenNode openingSquareBracket,
ArrayElementListNode elements, ArrayElementListNode elements,
@ -544,17 +555,17 @@ namespace Parser
public NamedFunctionHandleNode NamedFunctionHandle( public NamedFunctionHandleNode NamedFunctionHandle(
TokenNode atSign, TokenNode atSign,
IdentifierNameNode identifierName) CompoundNameNode functionName)
{ {
var children = new List<SyntaxNode> var children = new List<SyntaxNode>
{ {
atSign, atSign,
identifierName functionName
}; };
var result = new NamedFunctionHandleNode( var result = new NamedFunctionHandleNode(
children, children,
atSign, atSign,
identifierName); functionName);
SetParent(result); SetParent(result);
return result; return result;
} }

View File

@ -451,6 +451,19 @@ namespace Parser
} }
public class CompoundNameNode : ExpressionNode
{
public List<IdentifierNameNode> Names;
public CompoundNameNode(
List<SyntaxNode> children,
List<IdentifierNameNode> names
) : base(children)
{
Names = names;
}
}
public class MemberAccessNode : ExpressionNode public class MemberAccessNode : ExpressionNode
{ {
public SyntaxNode LeftOperand { get; } public SyntaxNode LeftOperand { get; }
@ -598,15 +611,15 @@ namespace Parser
public class NamedFunctionHandleNode : FunctionHandleNode public class NamedFunctionHandleNode : FunctionHandleNode
{ {
public TokenNode AtSign { get; } public TokenNode AtSign { get; }
public IdentifierNameNode IdentifierName { get; } public CompoundNameNode FunctionName { get; }
public NamedFunctionHandleNode( public NamedFunctionHandleNode(
List<SyntaxNode> children, List<SyntaxNode> children,
TokenNode atSign, TokenNode atSign,
IdentifierNameNode identifierName) : base(children) CompoundNameNode functionName) : base(children)
{ {
AtSign = atSign; AtSign = atSign;
IdentifierName = identifierName; FunctionName = functionName;
} }
} }

View File

@ -11,7 +11,7 @@ namespace ProjectConsole
class Program class Program
{ {
//private const string BaseDirectory = @"C:\Program Files\MATLAB\R2018a\toolbox\matlab\"; //private const string BaseDirectory = @"C:\Program Files\MATLAB\R2018a\toolbox\matlab\";
private const string BaseDirectory = @"/Applications/MATLAB_R2017b.app/toolbox/matlab/guide/"; private const string BaseDirectory = @"/Applications/MATLAB_R2017b.app/toolbox/matlab/";
private static HashSet<string> skipFiles = new HashSet<string> private static HashSet<string> skipFiles = new HashSet<string>
{ {