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

Improve graphql parser code #2173

Closed
Closed
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
6 changes: 3 additions & 3 deletions ballerina/common_utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ isolated function getOutputObject(Data data, ErrorDetail[] errors) returns Outpu
}

isolated function getTypeName(parser:ArgumentNode argumentNode) returns string {
parser:ArgumentType kind = argumentNode.getKind();
int kind = argumentNode.getKind();
if kind == parser:T_INT {
return INT;
} else if kind == parser:T_FLOAT {
Expand All @@ -158,7 +158,7 @@ isolated function getTypeName(parser:ArgumentNode argumentNode) returns string {
}
}

isolated function getArgumentTypeKind(string argType) returns parser:ArgumentType {
isolated function getArgumentTypeKind(string argType) returns int {
if argType == INT {
return parser:T_INT;
} else if argType == STRING {
Expand Down Expand Up @@ -232,7 +232,7 @@ isolated function getErrorDetailRecord(string message, Location|Location[] locat
};
}

isolated function getArgumentTypeIdentifierFromType(__Type argType) returns parser:ArgumentType {
isolated function getArgumentTypeIdentifierFromType(__Type argType) returns int {
if argType.kind == NON_NULL {
return getArgumentTypeIdentifierFromType(<__Type> argType?.ofType);
} else if argType.kind == LIST {
Expand Down
26 changes: 13 additions & 13 deletions ballerina/field_validator_visitor.bal
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class FieldValidatorVisitor {
isolated function visistAnyValue(parser:ArgumentNode argumentNode, __InputValue schemaArg) {
parser:ArgumentNode modifiedArgNode = self.nodeModifierContext.getModifiedArgumentNode(argumentNode);
parser:ArgumentValue|parser:ArgumentValue[] value = modifiedArgNode.getValue();
if value is () && schemaArg.'type.kind == NON_NULL {
if value == () && schemaArg.'type.kind == NON_NULL {
string expectedTypeName = getTypeNameFromType(schemaArg.'type);
string message = string `${expectedTypeName} cannot represent non ${expectedTypeName} value: null`;
ErrorDetail errorDetail = getErrorDetailRecord(message, modifiedArgNode.getValueLocation());
Expand Down Expand Up @@ -188,7 +188,7 @@ class FieldValidatorVisitor {
}
self.modifyArgumentNode(argNode, value = value);
if !isProvidedField {
if subInputValue.'type.kind == NON_NULL && subInputValue.defaultValue is () {
if subInputValue.'type.kind == NON_NULL && subInputValue.defaultValue == () {
string inputFieldName = getInputObjectFieldFormPath(self.argumentPath, subInputValue.name);
string message = string `Field "${inputFieldName}" of required type ` +
string `"${getTypeNameFromType(subInputValue.'type)}" was not provided.`;
Expand Down Expand Up @@ -264,7 +264,7 @@ class FieldValidatorVisitor {
fieldName);
self.modifyArgumentNode(argumentNode, variableValue = clonedVariableValue);
self.removePath();
} else if variableValue is () {
} else if variableValue == () {
self.validateArgumentValue(variableValue, modifiedArgNode.getValueLocation(), getTypeName(modifiedArgNode),
schemaArg);
} else {
Expand All @@ -273,7 +273,7 @@ class FieldValidatorVisitor {
expectedTypeName = getTypeNameFromType(schemaArg.'type);
}
string listError = getListElementError(self.argumentPath);
string value = variableValue is () ? "null" : variableValue.toString();
string value = variableValue == () ? "null" : variableValue.toString();
string message = string `${listError}${expectedTypeName} cannot represent non ${expectedTypeName} value:` +
string ` ${value}`;
ErrorDetail errorDetail = getErrorDetailRecord(message, modifiedArgNode.getValueLocation());
Expand All @@ -283,7 +283,7 @@ class FieldValidatorVisitor {

isolated function validateArgumentValue(parser:ArgumentValue value, Location valueLocation, string actualTypeName,
__InputValue schemaArg) {
if value is () {
if value == () {
if schemaArg.'type.kind == NON_NULL {
string listError = getListElementError(self.argumentPath);
string message = string `${listError}Expected value of type "${getTypeNameFromType(schemaArg.'type)}"` +
Expand Down Expand Up @@ -360,12 +360,12 @@ class FieldValidatorVisitor {
self.validateListVariableValue(fieldValueClone, subInputValue, location, fieldName);
variableValues[subInputValue.name] = fieldValueClone;
self.removePath();
} else if fieldValue is () {
} else if fieldValue == () {
string expectedTypeName = getOfTypeName(inputValue.'type);
self.validateArgumentValue(fieldValue, location, expectedTypeName, subInputValue);
}
} else {
if subInputValue.'type.kind == NON_NULL && inputValue?.defaultValue is () {
if subInputValue.'type.kind == NON_NULL && inputValue?.defaultValue == () {
string inputField = getInputObjectFieldFormPath(self.argumentPath, subInputValue.name);
string message = string `Field "${inputField}" of required type ` +
string `"${getTypeNameFromType(subInputValue.'type)}" was not provided.`;
Expand Down Expand Up @@ -400,7 +400,7 @@ class FieldValidatorVisitor {
foreach int i in 0 ..< variableValues.length() {
self.updatePath(i);
json listItemValue = variableValues[i];
if listItemValue is () {
if listItemValue == () {
string expectedTypeName = getOfTypeName(listItemInputValue.'type);
self.validateArgumentValue(listItemValue, location, expectedTypeName, listItemInputValue);
} else if getOfTypeName(listItemInputValue.'type) == subgraph:ANY {
Expand Down Expand Up @@ -545,7 +545,7 @@ class FieldValidatorVisitor {
}

foreach __InputValue inputValue in notFoundInputValues {
if inputValue.'type.kind == NON_NULL && inputValue?.defaultValue is ()
if inputValue.'type.kind == NON_NULL && inputValue?.defaultValue == ()
&& getOfType(inputValue.'type).name != UPLOAD {
string message = getMissingRequiredArgError(fieldNode, inputValue);
self.errors.push(getErrorDetailRecord(message, fieldNode.getLocation()));
Expand All @@ -560,7 +560,7 @@ class FieldValidatorVisitor {
}
string fragmentOnTypeName = fragmentNode.getOnType();
__Type? fragmentOnType = getTypeFromTypeArray(self.schema.types, fragmentOnTypeName);
if fragmentOnType is () {
if fragmentOnType == () {
string message = string `Unknown type "${fragmentOnTypeName}".`;
ErrorDetail errorDetail = getErrorDetailRecord(message, fragmentNode.getLocation());
self.errors.push(errorDetail);
Expand Down Expand Up @@ -592,7 +592,7 @@ class FieldValidatorVisitor {
foreach parser:ArgumentValue 'field in inputObjectFields {
if 'field is parser:ArgumentNode {
int? index = definedFields.indexOf('field.getName());
if index is () {
if index == () {
string message = string `Field "${'field.getName()}" is not defined by type ` +
string `"${node.getName()}".`;
self.errors.push(getErrorDetailRecord(message, 'field.getLocation()));
Expand Down Expand Up @@ -665,7 +665,7 @@ class FieldValidatorVisitor {
return self.errors.length() > 0 ? self.errors : ();
}

private isolated function modifyArgumentNode(parser:ArgumentNode originalNode, parser:ArgumentType? kind = (),
private isolated function modifyArgumentNode(parser:ArgumentNode originalNode, int? kind = (),
parser:ArgumentValue|parser:ArgumentValue[] value = (),
json variableValue = ()) {
parser:ArgumentNode previouslyModifiedNode = self.nodeModifierContext.getModifiedArgumentNode(originalNode);
Expand Down Expand Up @@ -699,7 +699,7 @@ isolated function getRequierdFieldFromType(__Type parentType, __Type[] typeArray
parser:FieldNode fieldNode) returns __Field? {
__Field[] fields = getFieldsArrayFromType(parentType);
__Field? requiredField = getFieldFromFieldArray(fields, fieldNode.getName());
if requiredField is () {
if requiredField == () {
if fieldNode.getName() == SCHEMA_FIELD && parentType.name == QUERY_TYPE_NAME {
__Type fieldType = <__Type>getTypeFromTypeArray(typeArray, SCHEMA_TYPE_NAME);
requiredField = createField(SCHEMA_FIELD, fieldType);
Expand Down
12 changes: 6 additions & 6 deletions ballerina/modules/parser/argument_node.bal
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public readonly class ArgumentNode {
private Location location;
private ArgumentValue|ArgumentValue[] value;
private Location valueLocation;
private ArgumentType kind;
private int kind;
private string? variableName;
private json variableValue;
private boolean variableDefinition;
private boolean containsInvalidValue;

public isolated function init(string name, Location location, ArgumentType kind,
public isolated function init(string name, Location location, int kind,
boolean isVarDef = false, Location? valueLocation = (),
ArgumentValue|ArgumentValue[] value = (), string? variableName = (),
boolean containsInvalidValue = false, json variableValue = ()) {
Expand All @@ -54,7 +54,7 @@ public readonly class ArgumentNode {
return self.location;
}

public isolated function getKind() returns ArgumentType {
public isolated function getKind() returns int {
return self.kind;
}

Expand Down Expand Up @@ -82,12 +82,12 @@ public readonly class ArgumentNode {
return self.containsInvalidValue;
}

public isolated function modifyWith(ArgumentType? kind = (), ArgumentValue|ArgumentValue[] value = (),
public isolated function modifyWith(int? kind = (), ArgumentValue|ArgumentValue[] value = (),
Location? valueLocation = (), boolean? isVarDef = (),
json variableValue = (), boolean? containsInvalidValue = ())
json variableValue = (), boolean? containsInvalidValue = ())
returns ArgumentNode {

ArgumentType kindParam = kind is () ? self.kind : kind;
int kindParam = kind is () ? self.kind : kind;
boolean isVarDefParam = isVarDef is () ? self.variableDefinition : isVarDef;
Location? valueLocationParam = valueLocation is () ? self.valueLocation : valueLocation;
ArgumentValue|ArgumentValue[] valueParam = value is () ? self.value : value;
Expand Down
10 changes: 5 additions & 5 deletions ballerina/modules/parser/char_reader.bal
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@

public class CharReader {
private CharIterator iterator;
private string:Char?[] buffer;
private string?[] buffer;

public isolated function init(string document) {
self.iterator = document.iterator();
self.buffer = [];
}

public isolated function peek(int into = 0) returns string:Char? {
public isolated function peek(int into = 0) returns string? {
if self.buffer.length() > into {
return self.buffer[into];
}
int startIndex = self.buffer.length();
foreach int i in startIndex ... into {
string:Char? char = self.next();
string? char = self.next();
self.buffer.push(char);
}
return self.buffer[into];
}

public isolated function read() returns string:Char? {
public isolated function read() returns string? {
if self.buffer.length() > 0 {
return self.buffer.shift();
}
return self.next();
}

isolated function next() returns string:Char? {
isolated function next() returns string? {
CharIteratorNode? next = self.iterator.next();
if next is () {
return;
Expand Down
73 changes: 25 additions & 48 deletions ballerina/modules/parser/constants.bal
Original file line number Diff line number Diff line change
Expand Up @@ -42,62 +42,39 @@ const FRAGMENT = "fragment";
const ON = "on";
public const NULL = "null";

enum Digit {
ZERO = "0",
ONE = "1",
TWO = "2",
THREE = "3",
FOUR = "4",
FIVE = "5",
SIX = "6",
SEVEN = "7",
EIGHT = "8",
NINE = "9"
}

enum Exp {
EXP_SIMPLE = "e",
EXP_CAPITAL = "E"
}

enum WhiteSpace {
SPACE = " ",
TAB = "\t"
}

enum LineTerminator {
NEW_LINE = "\n",
LINE_RETURN = "\r",
EOF = ""
}

const TRUE = "true";
const FALSE = "false";
const EOF = "";

// Parser Types
const T_EOF = 0;

public const T_IDENTIFIER = 1;
public const T_STRING = 2;
public const T_INT = 3;
public const T_FLOAT = 4;
public const T_BOOLEAN = 5;
public const T_COMMENT = 6;
public const T_OPEN_BRACE = 7;
public const T_CLOSE_BRACE = 8;
public const T_OPEN_PARENTHESES = 9;
public const T_CLOSE_PARENTHESES = 10;
public const T_COLON = 11;
public const T_COMMA = 12;
public const T_NEW_LINE = 13;
public const T_WHITE_SPACE = 14;
public const T_ELLIPSIS = 15;
public const T_DOLLAR = 16;
public const T_EQUAL = 17;
public const T_EXCLAMATION = 18;
public const T_OPEN_BRACKET = 19;
public const T_CLOSE_BRACKET = 20;
public const T_AT = 21;
public const T_INPUT_OBJECT = 22;
public const T_LIST = 23;
public const T_INPUT_OBJECT = 6;
public const T_LIST = 7;

public const T_OPEN_BRACE = 8;
public const T_CLOSE_BRACE = 9;
public const T_OPEN_PARENTHESES = 10;
public const T_CLOSE_PARENTHESES = 11;
public const T_OPEN_BRACKET = 12;
public const T_CLOSE_BRACKET = 13;

public const T_COLON = 14;
public const T_DOLLAR = 15;
public const T_EQUAL = 16;
public const T_EXCLAMATION = 17;
public const T_AT = 18;

public const T_COMMENT = 19;
public const T_COMMA = 20;
public const T_NEW_LINE = 21;
public const T_WHITE_SPACE = 22;

public const T_EOF = 23;

public const T_ELLIPSIS = 24;
public const ANONYMOUS_OPERATION = "<anonymous>";
Loading
Loading