Skip to content

Commit

Permalink
feat: Support consuming ; at the end of select or set statement
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Dec 4, 2023
1 parent 01bf1f5 commit a5e93ba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
17 changes: 16 additions & 1 deletion crates/gitql-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use gitql_ast::statement::*;
use gitql_ast::types::DataType;
use gitql_ast::types::TABLES_FIELDS_TYPES;

pub fn parse_gql(tokens: Vec<Token>, env: &mut Enviroment) -> Result<Query, GQLError> {
pub fn parse_gql(mut tokens: Vec<Token>, env: &mut Enviroment) -> Result<Query, GQLError> {
consume_optional_semicolon_if_exists(&mut tokens);

let mut position = 0;
let first_token = &tokens[position];
match &first_token.kind {
Expand Down Expand Up @@ -1866,6 +1868,19 @@ fn un_expected_expression_error(tokens: &Vec<Token>, position: &usize) -> GQLErr
}
}

/// Remove last token if it semicolon, because it's optional
fn consume_optional_semicolon_if_exists(tokens: &mut Vec<Token>) {
if tokens.is_empty() {
return;
}

if let Some(last_token) = tokens.last() {
if last_token.kind == TokenKind::Semicolon {
tokens.remove(tokens.len() - 1);
}
}
}

#[allow(clippy::borrowed_box)]
fn get_expression_name(expression: &Box<dyn Expression>) -> Result<String, ()> {
if let Some(symbol) = expression.as_any().downcast_ref::<SymbolExpression>() {
Expand Down
19 changes: 19 additions & 0 deletions crates/gitql-parser/src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub enum TokenKind {

Comma,
Dot,
Semicolon,

Ascending,
Descending,
Expand Down Expand Up @@ -537,6 +538,24 @@ pub fn tokenize(script: String) -> Result<Vec<Token>, GQLError> {
continue;
}

// Semicolon
if char == ';' {
let location = Location {
start: column_start,
end: position,
};

let token = Token {
location,
kind: TokenKind::Semicolon,
literal: ";".to_owned(),
};

tokens.push(token);
position += 1;
continue;
}

// Characters to ignoring
if char == ' ' || char == '\n' || char == '\t' {
position += 1;
Expand Down

1 comment on commit a5e93ba

@jaywgraves
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AmrDeveloper
This was tripping me up but was going to try to add it myself rather than adding an issue. Ha!

Please sign in to comment.