Skip to content

Commit

Permalink
feat: Add support for <> operator
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Nov 5, 2023
1 parent b165927 commit 0e6cb4f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 38 deletions.
8 changes: 8 additions & 0 deletions crates/gitql-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,14 @@ fn un_expected_expression_error(tokens: &Vec<Token>, position: &usize) -> GQLErr
};
}

// `< >` the user may mean to write `<>`
if previous.kind == TokenKind::Less && current.kind == TokenKind::Greater {
return GQLError {
message: "Unexpected `< >`, do you mean `<>`?".to_owned(),
location,
};
}

// Default error message
GQLError {
message: "Can't complete parsing this expression".to_owned(),
Expand Down
68 changes: 36 additions & 32 deletions crates/gitql-parser/src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ pub fn tokenize(script: String) -> Result<Vec<Token>, GQLError> {
position += 1;

let mut kind = TokenKind::BitwiseOr;
let mut literal = "|";

if position < len && characters[position] == '|' {
let literal = if position < len && characters[position] == '|' {
position += 1;
kind = TokenKind::LogicalOr;
literal = "||";
}
"||"
} else {
"|"
};

let token = Token {
location,
Expand All @@ -284,13 +284,13 @@ pub fn tokenize(script: String) -> Result<Vec<Token>, GQLError> {

position += 1;
let mut kind = TokenKind::BitwiseAnd;
let mut literal = "&";

if position < len && characters[position] == '&' {
let literal = if position < len && characters[position] == '&' {
position += 1;
kind = TokenKind::LogicalAnd;
literal = "&&";
}
"&&"
} else {
"&"
};

let token = Token {
location,
Expand Down Expand Up @@ -348,13 +348,13 @@ pub fn tokenize(script: String) -> Result<Vec<Token>, GQLError> {
position += 1;

let mut kind = TokenKind::Dot;
let mut literal = ".";

if position < len && characters[position] == '.' {
let literal = if position < len && characters[position] == '.' {
position += 1;
kind = TokenKind::DotDot;
literal = "..";
}
".."
} else {
"."
};

let token = Token {
location,
Expand All @@ -376,17 +376,17 @@ pub fn tokenize(script: String) -> Result<Vec<Token>, GQLError> {
position += 1;

let mut kind = TokenKind::Greater;
let mut literal = ">";

if position < len && characters[position] == '=' {
let literal = if position < len && characters[position] == '=' {
position += 1;
kind = TokenKind::GreaterEqual;
literal = ">=";
">="
} else if position < len && characters[position] == '>' {
position += 1;
kind = TokenKind::BitwiseRightShift;
literal = ">>";
}
">>"
} else {
">"
};

let token = Token {
location,
Expand All @@ -408,17 +408,21 @@ pub fn tokenize(script: String) -> Result<Vec<Token>, GQLError> {
position += 1;

let mut kind = TokenKind::Less;
let mut literal = "<";

if position < len && characters[position] == '=' {
let literal = if position < len && characters[position] == '=' {
position += 1;
kind = TokenKind::LessEqual;
literal = "<=";
"<="
} else if position < len && characters[position] == '<' {
position += 1;
kind = TokenKind::BitwiseLeftShift;
literal = "<<";
}
"<<"
} else if position < len && characters[position] == '>' {
position += 1;
kind = TokenKind::BangEqual;
"<>"
} else {
"<"
};

let token = Token {
location,
Expand Down Expand Up @@ -458,13 +462,13 @@ pub fn tokenize(script: String) -> Result<Vec<Token>, GQLError> {
position += 1;

let mut kind = TokenKind::Bang;
let mut literal = "!";

if position < len && characters[position] == '=' {
let literal = if position < len && characters[position] == '=' {
position += 1;
kind = TokenKind::BangEqual;
literal = "!=";
}
"!="
} else {
"!"
};

let token = Token {
location,
Expand Down
12 changes: 6 additions & 6 deletions docs/expression/binary.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ Used to perform arithmetic operators on number types.
- `%` Modulus.

### Comparison Expression
- `=` used to check if field equal to expected value.
- `!=` used to check if field not equal to expected value.
- `>` used to check if field greater than expected value.
- `>=` used to check if field greater than or equals expected value.
- `<` used to check if field less than expected value.
- `<=` used to check if field less than or equals expected value.
- `=` used to check if two values are equals.
- `!=` or `<>` used to check if two values are not equals.
- `>` used to check value greater than other value.
- `>=` used to check if value is greater than or equals than other value
- `<` used to check if value is less than than other value.
- `<=` used to check if value is less than or equals than other value.

---

Expand Down

0 comments on commit 0e6cb4f

Please sign in to comment.