use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.
the class PackageNodeHelper method getCurrentPackage.
/**
* Get the current package from the current node.
* @return Name of current package or "user" if none was found.
*/
public String getCurrentPackage() {
// Find our parent node under MAGIK node.
final AstNode magikNode = this.node.getFirstAncestor(MagikGrammar.MAGIK);
AstNode topNode = this.node;
while (topNode.getParent() != null && topNode.getParent() != magikNode) {
topNode = topNode.getParent();
}
// Try to find via siblings.
AstNode siblingNode = topNode.getPreviousSibling();
while (siblingNode != null) {
if (siblingNode.is(MagikGrammar.PACKAGE_SPECIFICATION)) {
AstNode identifierNode = siblingNode.getFirstChild(MagikGrammar.IDENTIFIER);
return identifierNode.getTokenValue();
}
siblingNode = siblingNode.getPreviousSibling();
}
// Default to package.
return DEFAULT_PACKAGE;
}
use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.
the class ParameterNodeHelper method isOptionalParameter.
/**
* Test if parameter is an {@code _optional} parameter.
* @return True if optional, false otherwise.
*/
public boolean isOptionalParameter() {
if (this.hasModifier(MagikKeyword.OPTIONAL)) {
return true;
}
if (this.hasModifier(MagikKeyword.GATHER)) {
return false;
}
final AstNode parametersNode = this.node.getParent();
if (parametersNode.isNot(MagikGrammar.PARAMETERS)) {
throw new IllegalStateException();
}
AstNode previousSiblingNode = this.node.getPreviousSibling();
while (previousSiblingNode != null) {
if (previousSiblingNode.is(MagikGrammar.PARAMETER)) {
final ParameterNodeHelper helper = new ParameterNodeHelper(previousSiblingNode);
if (helper.hasModifier(MagikKeyword.OPTIONAL)) {
return true;
}
}
previousSiblingNode = previousSiblingNode.getPreviousSibling();
}
return false;
}
use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.
the class DefineBinaryOperatorCaseParser method isBinaryOperatorCase.
/**
* Test if node is a {@code define_binary_operator_case()}.
* @param node Node to test
* @return True if node is a {@code define_binary_operator_case()}, false otherwise.
*/
public static boolean isBinaryOperatorCase(final AstNode node) {
if (!node.is(MagikGrammar.PROCEDURE_INVOCATION)) {
return false;
}
final ProcedureInvocationNodeHelper helper = new ProcedureInvocationNodeHelper(node);
if (!helper.isProcedureInvocationOf(DEFINE_BINARY_OPERATOR_CASE)) {
return false;
}
final AstNode argumentsNode = node.getFirstChild(MagikGrammar.ARGUMENTS);
final ArgumentsNodeHelper argumentsHelper = new ArgumentsNodeHelper(argumentsNode);
final AstNode argument0Node = argumentsHelper.getArgument(0, MagikGrammar.SYMBOL);
if (argument0Node == null) {
return false;
}
final AstNode argument1Node = argumentsHelper.getArgument(1, MagikGrammar.IDENTIFIER);
if (argument1Node == null) {
return false;
}
final AstNode argument2Node = argumentsHelper.getArgument(2, MagikGrammar.IDENTIFIER);
if (argument2Node == null) {
return false;
}
// Anything will do.
final AstNode argument3Node = argumentsHelper.getArgument(3);
if (argument3Node == null) {
return false;
}
final String operatorSymbol = argument0Node.getTokenValue();
return !operatorSymbol.isEmpty();
}
use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.
the class DefineBinaryOperatorCaseParser method parseDefinitions.
/**
* Parse defitions.
* @return List of parsed definitions.
*/
public List<Definition> parseDefinitions() {
// Some sanity.
final AstNode argumentsNode = node.getFirstChild(MagikGrammar.ARGUMENTS);
final ArgumentsNodeHelper argumentsHelper = new ArgumentsNodeHelper(argumentsNode);
final AstNode argument0Node = argumentsHelper.getArgument(0, MagikGrammar.SYMBOL);
if (argument0Node == null) {
throw new IllegalStateException();
}
final AstNode argument1Node = argumentsHelper.getArgument(1, MagikGrammar.IDENTIFIER);
if (argument1Node == null) {
throw new IllegalStateException();
}
final AstNode argument2Node = argumentsHelper.getArgument(2, MagikGrammar.IDENTIFIER);
if (argument2Node == null) {
throw new IllegalStateException();
}
// Anything will do.
final AstNode argument3Node = argumentsHelper.getArgument(3);
if (argument3Node == null) {
throw new IllegalStateException();
}
final String operatorSymbol = argument0Node.getTokenValue();
if (operatorSymbol.isEmpty()) {
throw new IllegalStateException();
}
// Figure statement node.
final AstNode statementNode = node.getFirstAncestor(MagikGrammar.STATEMENT);
// Figure pakkage.
final String pakkage = this.getCurrentPakkage();
// Figure operator & lhs & rhs.
final String operator = operatorSymbol.substring(1);
final String lhs = argument1Node.getTokenValue();
final String rhs = argument2Node.getTokenValue();
final BinaryOperatorDefinition operatorDefinition = new BinaryOperatorDefinition(statementNode, pakkage, operator, lhs, rhs);
return List.of(operatorDefinition);
}
use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.
the class DefineSharedConstantParser method isDefineSharedConstant.
/**
* Test if node is a {@code define_shared_constant()}.
* @param node Node to test
* @return True if node is a {@code define_shared_variable()}, false otherwise.
*/
public static boolean isDefineSharedConstant(final AstNode node) {
if (!node.is(MagikGrammar.METHOD_INVOCATION)) {
return false;
}
final MethodInvocationNodeHelper helper = new MethodInvocationNodeHelper(node);
if (!helper.isMethodInvocationOf(DEFINE_SHARED_CONSTANT)) {
return false;
}
// Some sanity.
final AstNode parentNode = node.getParent();
final AstNode atomNode = parentNode.getFirstChild();
if (atomNode.isNot(MagikGrammar.ATOM)) {
return false;
}
// Assume this is an exemplar.
final String exemplarName = atomNode.getTokenValue();
if (exemplarName == null) {
return false;
}
final AstNode argumentsNode = node.getFirstChild(MagikGrammar.ARGUMENTS);
final ArgumentsNodeHelper argumentsHelper = new ArgumentsNodeHelper(argumentsNode);
final AstNode argument0Node = argumentsHelper.getArgument(0, MagikGrammar.SYMBOL);
final AstNode argument2Node = argumentsHelper.getArgument(2, MagikGrammar.SYMBOL);
return argument0Node != null && argument2Node != null;
}
Aggregations