Skip to content

Commit

Permalink
lex: handle the list initialization
Browse files Browse the repository at this point in the history
Signed-off-by: FedericoBruzzone <[email protected]>
Co-authored-by: Federico Guerinoni <[email protected]>
  • Loading branch information
FedericoBruzzone and guerinoni committed Aug 14, 2024
1 parent 63a045c commit 085657d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
3 changes: 1 addition & 2 deletions examples/first.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ _x_int: int = 0
_x_float: float = 0.1
_x_bool: bool = true
_x_bool2: bool = false
c_char: char = 'c'
c_char2: char = 'c'
x_str: str = "hello"
x_list: [int] = [1, 2, 3]
2 changes: 1 addition & 1 deletion examples/test.fs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
x: str = "hello"
x_list: [int] = [1, 2, 3]
34 changes: 25 additions & 9 deletions src/lexer/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use super::Lexer;
use super::LexerError;
use crate::lexer::token::Token;
use crate::lexer::token::TokenKind;
use crate::lexer::token::TokenKind::TokenCloseBrace;
use crate::lexer::token::TokenKind::TokenCloseBracket;
use crate::lexer::token::TokenKind::TokenCloseParen;
use crate::lexer::token::TokenKind::TokenDoubleQuote;
use crate::lexer::token::TokenKind::TokenSingleQuote;
use std::fmt::Debug;
Expand Down Expand Up @@ -98,7 +101,7 @@ impl State for StateStart {
)),
Some(_) => Err(LexerError::UnexpectedToken(Token::new(
TokenKind::TokenUnknown,
"".to_string(),
cursor.source().content()[cursor.index()..cursor.offset() + 1].to_string(),
cursor.location().clone(),
))),
None => Ok(Lexer::proceed(Box::new(StateEOF), TransitionKind::Consume)),
Expand All @@ -112,6 +115,11 @@ pub struct StateString;
impl State for StateString {
fn visit(&self, cursor: &mut Cursor) -> Result<Transition, LexerError> {
match cursor.peek() {
Some(c) if c.eq(&'\n') => Err(LexerError::UnexpectedToken(Token::new(
TokenKind::TokenUnknown,
"\\n".to_string(),
cursor.location().clone(),
))),
Some(c) if c.ne(&'"') => Ok(Lexer::proceed(
Box::new(StateString),
TransitionKind::AdvanceOffset,
Expand Down Expand Up @@ -172,12 +180,10 @@ impl State for StateNumber {
let lexeme = cursor.source().content()[cursor.index()..cursor.offset()].to_string();
let location = cursor.location().clone();
let token_kind = TokenKind::from(&lexeme);
Ok(Transition {
state: Box::new(StateStart),
transition_kind: TransitionKind::EmitToken(Token::new(
token_kind, lexeme, location,
)),
})
Ok(Lexer::proceed(
Box::new(StateStart),
TransitionKind::EmitToken(Token::new(token_kind, lexeme, location)),
))
}
}
}
Expand Down Expand Up @@ -214,7 +220,10 @@ pub struct StateSymbol;

impl StateSymbol {
fn is_symbol(c: char) -> bool {
matches!(c, ':' | '=' | '\n')
matches!(
c,
':' | '=' | '\n' | '(' | ')' | '{' | '}' | '[' | ']' | ','
)
}
}

Expand All @@ -227,7 +236,14 @@ impl State for StateSymbol {

// NOTE: if a '\n' is found and it was scanning another "symbol" token, the previous was mangled, and only the '\n' is emitted,
// we need to handle the previous token since can be at the end of the line
if [TokenSingleQuote, TokenDoubleQuote].contains(&token_kind) {
let valid_last_token = vec![
TokenCloseBracket,
TokenCloseParen,
TokenCloseBrace,
TokenDoubleQuote,
TokenSingleQuote,
];
if valid_last_token.contains(&token_kind) {
return Ok(Lexer::proceed(
Box::new(StateStart),
TransitionKind::EmitToken(Token::new(
Expand Down
44 changes: 36 additions & 8 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const COLON: &str = ":";
const ASSIGN: &str = "=";
const SINGLE_QUOTE: &str = "'";
const DOUBLE_QUOTE: &str = "\"";
const OPEN_PAREN: &str = "(";
const CLOSE_PAREN: &str = ")";
const OPEN_BRACKET: &str = "{";
const CLOSE_BRACKET: &str = "}";
const OPEN_BRACE: &str = "[";
const CLOSE_BRACE: &str = "]";
const COMMA: &str = ",";

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum Literal {
Expand All @@ -28,14 +35,21 @@ pub enum TokenKind {
TokenKeyword,
TokenType,
TokenComment,
TokenSpace, // ' '
TokenTab, // \t
TokenNewLine, // \n
TokenColon, // :
TokenAssign, // =
TokenSingleQuote, // '
TokenDoubleQuote, // "
TokenEOF, // End of file
TokenSpace, // ' '
TokenTab, // \t
TokenNewLine, // \n
TokenColon, // :
TokenAssign, // =
TokenSingleQuote, // '
TokenDoubleQuote, // "
TokenOpenParen, // (
TokenCloseParen, // )
TokenOpenBrace, // {
TokenCloseBrace, // }
TokenOpenBracket, // [
TokenCloseBracket, // ]
TokenComma, // ,
TokenEOF, // End of file
TokenUnknown,
}

Expand Down Expand Up @@ -70,6 +84,13 @@ impl TokenKind {
ASSIGN => Some(TokenKind::TokenAssign),
SINGLE_QUOTE => Some(TokenKind::TokenSingleQuote),
DOUBLE_QUOTE => Some(TokenKind::TokenDoubleQuote),
OPEN_PAREN => Some(TokenKind::TokenOpenParen),
CLOSE_PAREN => Some(TokenKind::TokenCloseParen),
OPEN_BRACE => Some(TokenKind::TokenOpenBrace),
CLOSE_BRACE => Some(TokenKind::TokenCloseBrace),
OPEN_BRACKET => Some(TokenKind::TokenOpenBracket),
CLOSE_BRACKET => Some(TokenKind::TokenCloseBracket),
COMMA => Some(TokenKind::TokenComma),
_ => None,
}
}
Expand Down Expand Up @@ -264,6 +285,13 @@ impl std::fmt::Display for TokenKind {
TokenKind::TokenAssign => write!(f, "TokenAssign"),
TokenKind::TokenSingleQuote => write!(f, "TokenTick"),
TokenKind::TokenDoubleQuote => write!(f, "TokenDoubleTick"),
TokenKind::TokenOpenParen => write!(f, "TokenOpenParen"),
TokenKind::TokenCloseParen => write!(f, "TokenCloseParen"),
TokenKind::TokenOpenBrace => write!(f, "TokenOpenBrace"),
TokenKind::TokenCloseBrace => write!(f, "TokenCloseBrace"),
TokenKind::TokenOpenBracket => write!(f, "TokenOpenBracket"),
TokenKind::TokenCloseBracket => write!(f, "TokenCloseBracket"),
TokenKind::TokenComma => write!(f, "TokenComma"),
TokenKind::TokenEOF => write!(f, "TokenEOF"),
TokenKind::TokenUnknown => write!(f, "TokenUnknown"),
}
Expand Down

0 comments on commit 085657d

Please sign in to comment.