Skip to content

Commit

Permalink
Merge pull request #232 from MarkusAmshove/natparse/assert-single-sta…
Browse files Browse the repository at this point in the history
…tement

Actually assert that only one statement is parsed
  • Loading branch information
MarkusAmshove authored May 7, 2023
2 parents b9fa445 + a192634 commit a4fdbac
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 32 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ private StatementNode onError() throws ParseError
consumeMandatory(onError, SyntaxKind.ON);
consumeMandatory(onError, SyntaxKind.ERROR);
onError.setBody(statementList(SyntaxKind.END_ERROR));
consumeMandatory(onError, SyntaxKind.END_ERROR);
checkForEmptyBody(onError);
return onError;
}
Expand Down Expand Up @@ -1226,10 +1227,16 @@ private StatementNode processSql() throws ParseError
var processSql = new ProcessSqlNode();
consumeMandatory(processSql, SyntaxKind.PROCESS);
consumeMandatory(processSql, SyntaxKind.SQL);
while (!isAtEnd() && !isStatementStart() && !isStatementEndOrBranch())
consumeMandatoryIdentifier(processSql); // ddm name

consumeMandatory(processSql, SyntaxKind.LESSER_SIGN);
consumeMandatory(processSql, SyntaxKind.LESSER_SIGN);
while (!isAtEnd() && !peekKind(SyntaxKind.GREATER_SIGN))
{
consume(processSql);
}
consumeMandatory(processSql, SyntaxKind.GREATER_SIGN);
consumeMandatory(processSql, SyntaxKind.GREATER_SIGN);

return processSql;
}
Expand Down Expand Up @@ -3078,9 +3085,14 @@ private FindNode find() throws ParseError
consumeLiteral(find);
}

var descriptor = consumeIdentifierTokenOnly(); // TODO(expressions): Must be ISearchCriteriaNode
var descriptorNode = new DescriptorNode(descriptor);
find.addNode(descriptorNode);
if (peekKind(SyntaxKind.STRING_LITERAL))
{
consumeLiteralNode(find);
}
else
{
find.addNode(conditionNode());
}
}

if (!hasNoBody)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.amshove.natparse.parsing;

import org.amshove.natparse.IDiagnostic;
import org.amshove.natparse.NodeUtil;
import org.amshove.natparse.ReadOnlyList;
import org.amshove.natparse.lexing.SyntaxKind;
import org.amshove.natparse.natural.*;
Expand Down Expand Up @@ -82,6 +83,12 @@ private void checkVariableReference(IVariableReferenceNode variableReference)
return;
}

if (NodeUtil.findFirstParentOfType(variableReference, IFindNode.class) != null)
{
// Can't correctly type check DDM fields yet, because DDM fields don't have their type loaded
return;
}

if (variableReference.dimensions().hasItems() && !target.isArray())
{
if (!isPeriodicGroup(target))// periodic groups need to have their index specified. not allowed for "normal" groups
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,21 @@ void parseFind()
""", IFindNode.class);

assertThat(findStatement.viewReference()).isNotNull();
assertThat(findStatement.descendants()).anyMatch(n -> n instanceof IDescriptorNode);
assertThat(findStatement.descendants()).hasSize(8);
assertThat(findStatement.descendants()).anyMatch(n -> n instanceof IConditionNode);
}

@Test
void parseFindWithSetName()
{
var findStatement = assertParsesSingleStatement("""
FIND THE-VIEW WITH 'COMPLETE-SET'
IF TRUE
IGNORE
END-IF
END-FIND
""", IFindNode.class);

assertThat(findStatement.viewReference()).isNotNull();
}

@Test
Expand All @@ -857,8 +870,7 @@ void parseFindWithNumberLimit()
""", IFindNode.class);

assertThat(findStatement.viewReference()).isNotNull();
assertThat(findStatement.descendants()).anyMatch(n -> n instanceof IDescriptorNode);
assertThat(findStatement.descendants()).hasSize(11);
assertThat(findStatement.descendants()).anyMatch(n -> n instanceof IConditionNode);
}

@Test
Expand All @@ -869,7 +881,7 @@ void parseFindWithoutBody()
""", IFindNode.class);

assertThat(findStatement.viewReference()).isNotNull();
assertThat(findStatement.descendants()).anyMatch(n -> n instanceof IDescriptorNode);
assertThat(findStatement.descendants()).anyMatch(n -> n instanceof IConditionNode);
assertThat(findStatement.descendants()).hasSize(5);
}

Expand Down Expand Up @@ -1641,13 +1653,11 @@ void parseDb2Delete()
@ValueSource(strings =
{
"PROCESS SQL DB2-TABLE <<SET :CURR-SERV = CURRENT SERVER>>",
"PROCESS SQL DB2-TABLE <<CONNECT TO :LOCATION>",
"PROCESS SQL DB2-TABLE <<CONNECT TO :LOCATION>>",
})
void parseProcessSql(String statement)
{
assertParsesSingleStatement("""
%s
""".formatted(statement), IProcessSqlNode.class);
assertParsesSingleStatement(statement, IProcessSqlNode.class);
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.amshove.natparse.natural.IStatementListNode;
import org.amshove.natparse.natural.IStatementNode;
import org.amshove.natparse.natural.ITokenNode;
import org.amshove.testhelpers.IntegrationTest;

import java.util.stream.Collectors;
Expand All @@ -19,21 +20,32 @@ protected StatementParseTest()
protected <T extends IStatementNode> T assertParsesSingleStatement(String source, Class<T> nodeType)
{
var result = super.assertParsesWithoutDiagnostics(source);
// assertHasSingleStatement(result);
assertHasSingleStatement(result);
return assertNodeType(result.statements().first(), nodeType);
}

protected <T extends IStatementNode> T assertParsesSingleStatementWithDiagnostic(String source, Class<T> nodeType, ParserError expectedDiagnostic)
{
var result = super.assertDiagnostic(source, expectedDiagnostic);
// assertHasSingleStatement(result);
assertHasSingleStatement(result);
return assertNodeType(result.statements().first(), nodeType);
}

private void assertHasSingleStatement(IStatementListNode statementList)
{
assertThat(statementList)
.as("Expected single statement but got: " + statementList.statements().stream().map(s -> s.getClass().getSimpleName()).collect(Collectors.joining(", ")))
.as("Expected single statement but got: " + statementList.statements().stream().map(this::formatStatementName).collect(Collectors.joining(", ")))
.hasSize(1);
}

private String formatStatementName(IStatementNode node)
{
var className = node.getClass().getSimpleName();
if (node instanceof ITokenNode tokenNode)
{
return "%s(%s)".formatted(className, tokenNode.token().kind());
}

return className;
}
}

0 comments on commit a4fdbac

Please sign in to comment.