Skip to content

Commit

Permalink
lexer: add namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
garritfra committed Dec 1, 2023
1 parent 44d45c8 commit b53e55a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub enum TokenKind {
Percent,
/// ":"
Colon,
/// "::"
DoubleColon,
/// ";"
SemiColon,
/// "."
Expand Down Expand Up @@ -281,7 +283,13 @@ impl Cursor<'_> {
}
_ => Assign,
},
':' => Colon,
':' => match self.first() {
':' => {
self.bump();
DoubleColon
}
_ => Colon,
},
';' => SemiColon,
',' => Comma,
'<' => match self.first() {
Expand Down
51 changes: 51 additions & 0 deletions src/lexer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,57 @@ fn test_functions() {
);
}

#[test]
fn test_namespaces() {
let mut tokens = tokenize("foo::bar").unwrap().into_iter().filter(|t| {
t.kind != TokenKind::Whitespace
&& t.kind != TokenKind::Tab
&& t.kind != TokenKind::CarriageReturn
});

assert_eq!(
tokens.next().unwrap(),
Token {
len: 3,
kind: TokenKind::Identifier("foo".to_string()),
raw: "foo".to_owned(),
pos: Position {
raw: 2,
line: 1,
offset: 2
}
}
);

assert_eq!(
tokens.next().unwrap(),
Token {
len: 2,
kind: TokenKind::DoubleColon,
raw: "::".to_owned(),
pos: Position {
raw: 4,
line: 1,
offset: 4
}
}
);

assert_eq!(
tokens.next().unwrap(),
Token {
len: 3,
kind: TokenKind::Identifier("bar".to_string()),
raw: "bar".to_owned(),
pos: Position {
raw: 7,
line: 1,
offset: 7
}
}
);
}

#[test]
fn test_comments() {
let mut tokens = tokenize(
Expand Down

0 comments on commit b53e55a

Please sign in to comment.