Skip to content

Commit

Permalink
Implement take_prefix_or_upper
Browse files Browse the repository at this point in the history
  • Loading branch information
purefunctor committed Dec 13, 2024
1 parent 343485f commit a98396e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
54 changes: 49 additions & 5 deletions crates/lexing/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<'a> Lexer<'a> {
if is_lower_start(i) {
self.take_lower()
} else if i.is_letter_uppercase() {
self.take_upper()
self.take_prefix_or_upper()
} else if is_operator(i) {
self.take_operator()
} else if i.is_whitespace() {
Expand Down Expand Up @@ -171,10 +171,54 @@ impl<'a> Lexer<'a> {
}

#[inline]
fn take_upper(&mut self) {
let position = self.position();
self.take_while(|c| c.is_letter());
self.lexed.push(SyntaxKind::UPPER, position, None)
fn take_prefix_or_upper(&mut self) {
let prefix = self.position();
let mut has_prefix = false;

// It's best to follow through this block with a few examples:
//
//>================================================================
//
// Hooks.do => [PREFIX, LOWER]
// Main.main => [UPPER, LOWER]
//
// 1. (A) takes 'Hooks'
// 2. (B) takes '.'
// 3. (D) pushes [PREFIX] and finishes
//
//>================================================================
//
// List.Cons => [PREFIX, UPPER]
//
// 1. (A) takes 'List'
// 2. (B) takes '.'
// 3. (A) takes 'Cons'
// 4. (B) takes nothing
// 5. (C) pushes [PREFIX, UPPER] and finishes
//
//>================================================================
loop {
// (D)
if !self.first().is_letter_uppercase() && has_prefix {
return self.lexed.push(SyntaxKind::PREFIX, prefix, None);
}

// (A)
let proper = self.position();
self.take_while(|c| c.is_letter());

// (B)
if self.first() == '.' {
self.take();
has_prefix = true;
} else {
// (C)
if has_prefix {
self.lexed.push(SyntaxKind::PREFIX, prefix, None);
}
return self.lexed.push(SyntaxKind::UPPER, proper, None);
}
}
}

#[inline]
Expand Down
1 change: 1 addition & 0 deletions crates/lexing/tests/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ macro_rules! lexer_tests {

lexer_tests!(
keyword => "ado as case class data derive do else false foreign hiding if import in infix infixl infixr instance let module newtype of then true type where",
prefixed => "Hooks.do Main.main List.Cons",
operator_purs => "<- ← -> → => ⇒ :: ∷ ∀ = . \\ | @",
operator_source => "=>> >>= >=> && || : ++",
integer_leading_zero => "0",
Expand Down
16 changes: 16 additions & 0 deletions crates/lexing/tests/snapshots/lexer__prefixed.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: crates/lexing/tests/lexer.rs
expression: tokens
snapshot_kind: text
---
[
PREFIX,
DO,
WHITESPACE,
PREFIX,
LOWER,
WHITESPACE,
PREFIX,
UPPER,
END_OF_FILE,
]
1 change: 1 addition & 0 deletions crates/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum SyntaxKind {
BLOCK_COMMENT,

// Names
PREFIX,
UPPER,
LOWER,
OPERATOR,
Expand Down

0 comments on commit a98396e

Please sign in to comment.