Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse *TRIM options #250

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ public static SyntaxKind getKeyword(String possibleKeyword)
case "last" -> SyntaxKind.LAST;
case "lc" -> SyntaxKind.LC;
case "le" -> SyntaxKind.LE;
case "leading" -> SyntaxKind.LEADING;
case "leave" -> SyntaxKind.LEAVE;
case "leaving" -> SyntaxKind.LEAVING;
case "left" -> SyntaxKind.LEFT;
Expand Down Expand Up @@ -568,6 +569,7 @@ public static SyntaxKind getKeyword(String possibleKeyword)
case "tp" -> SyntaxKind.TP;
case "tr" -> SyntaxKind.TR;
case "trailer" -> SyntaxKind.TRAILER;
case "trailing" -> SyntaxKind.TRAILING;
case "transaction" -> SyntaxKind.TRANSACTION;
case "transfer" -> SyntaxKind.TRANSFER;
case "translate" -> SyntaxKind.TRANSLATE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public enum SyntaxKind
SLASH(false, false, false),
BACKSLASH(false, false, false),
UNDERSCORE(false, false, false),
LEADING(true, false, false),
TRAILING(true, false, false),
SECTION_SYMBOL(false, false, false),
SEMICOLON(false, false, false),
GREATER_SIGN(false, false, false),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.amshove.natparse.natural;

import org.amshove.natparse.lexing.SyntaxKind;

import javax.annotation.Nullable;

public interface ITrimFunctionNode extends ISystemFunctionNode
{
SyntaxKind systemFunction();

@Nullable
SyntaxKind option();
}
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,11 @@ protected ISystemFunctionNode consumeSystemFunctionNode(BaseSyntaxNode node) thr
return consumeSystemFunctionWithRepParameter(node, peek().kind());
}

if (peek().kind() == SyntaxKind.TRIM)
{
return consumeTrimFunction(node);
}

var systemFunction = new SystemFunctionNode();
systemFunction.setSystemFunction(peek().kind());
consume(systemFunction);
Expand All @@ -747,6 +752,22 @@ && peek().kind() == SyntaxKind.IDENTIFIER && peek().symbolName().equals("IR"))
return systemFunction;
}

private ISystemFunctionNode consumeTrimFunction(BaseSyntaxNode node) throws ParseError
{
var trim = new TrimFunctionNode();
trim.setSystemFunction(consume(trim).kind());

consumeMandatory(trim, SyntaxKind.LPAREN);
trim.addParameter(consumeOperandNode(trim));
if (consumeOptionally(trim, SyntaxKind.COMMA))
{
trim.setOption(consumeAnyMandatory(trim, List.of(SyntaxKind.LEADING, SyntaxKind.TRAILING)).kind());
}
consumeMandatory(trim, SyntaxKind.RPAREN);
node.addNode(trim);
return trim;
}

private ISystemFunctionNode consumeSystemFunctionWithRepParameter(BaseSyntaxNode node, SyntaxKind kind) throws ParseError
{
var systemFunction = new SystemFunctionNode();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.amshove.natparse.parsing;

import org.amshove.natparse.lexing.SyntaxKind;
import org.amshove.natparse.natural.ITrimFunctionNode;

import javax.annotation.Nullable;

class TrimFunctionNode extends SystemFunctionNode implements ITrimFunctionNode
{
private SyntaxKind option;

@Nullable
@Override
public SyntaxKind option()
{
return option;
}

void setOption(SyntaxKind kind)
{
this.option = kind;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ private void checkSystemFunctionParameter(ISystemFunctionNode sysFuncNode)
}
}
}

if (sysFuncNode.systemFunction() == SyntaxKind.TRIM && sysFuncNode.parameter().hasItems())
{
var parameter = sysFuncNode.parameter().first();

var type = inferDataType(parameter);
if (type != null && type.format() != DataFormat.NONE && type.format() != DataFormat.ALPHANUMERIC && type.format() != DataFormat.UNICODE && type.format() != DataFormat.BINARY)
{
report(ParserErrors.typeMismatch("Parameter to *TRIM must be of type A, B or U", parameter));
}
}
}

private void checkDecideOnBranches(IDecideOnNode decideOn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

import javax.print.DocFlavor.STRING;

class OperandParsingTests extends AbstractParserTest<IStatementListNode>
{
protected OperandParsingTests()
Expand Down Expand Up @@ -65,6 +63,21 @@ void parseSystemFunctions()
assertThat(parameter.token().source()).isEqualTo("' Hello '");
}

@ParameterizedTest
@ValueSource(strings =
{
"LEADING", "TRAILING"
})
void parseTrimWithLeadingAndTrailing(String option)
{
var operand = parseOperand("*TRIM(' Hello ', %s)".formatted(option));
var function = assertNodeType(operand, ITrimFunctionNode.class);
assertThat(function.systemFunction()).isEqualTo(SyntaxKind.TRIM);
var parameter = assertNodeType(function.parameter().first(), ILiteralNode.class);
assertThat(parameter.token().source()).isEqualTo("' Hello '");
assertThat(function.option()).isEqualTo(SyntaxKind.valueOf(option));
}

@Test
void parseSystemFunctionsWithMultipleParameter()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
DEFINE DATA LOCAL
1 #TRIMMED (A) DYNAMIC
1 #A10 (A10)
1 #B10 (B10)
1 #U10 (U10)
1 #ADYN (A) DYNAMIC
1 #BDYN (B) DYNAMIC
1 #UDYN (U) DYNAMIC
1 #N10 (N10)
1 #I4 (I4)
END-DEFINE

#TRIMMED := *TRIM(#A10)
#TRIMMED := *TRIM(#B10)
#TRIMMED := *TRIM(#U10)
#TRIMMED := *TRIM(#ADYN)
#TRIMMED := *TRIM(#BDYN)
#TRIMMED := *TRIM(#UDYN)


#TRIMMED := *TRIM(#N10) /* !{D:ERROR:NPP037}
#TRIMMED := *TRIM(#I4) /* !{D:ERROR:NPP037}

END