Skip to content

Commit 884c88a

Browse files
Merge pull request #9 from re-masashi/main
Miscellaneous fixes in an attempt to fix #8
2 parents 6da9fe3 + 63fcacf commit 884c88a

6 files changed

Lines changed: 235 additions & 87 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

scribe-parser/src/parser/container_declarations.rs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,21 @@ where
314314
destructor = self.parse_block(cursor);
315315
}
316316
_ => {
317-
cursor.advance_kind(); // hard skip
317+
eprintln!(
318+
"Warning: Unexpected token {:?} in struct method block. Skipping to next fn/const/~/}}.",
319+
cursor.peek_kind()
320+
);
321+
// Skip to next meaningful token (fn, const, ~, or })
322+
while !matches!(
323+
cursor.peek_kind(),
324+
Some(TokenKind::Fn)
325+
| Some(TokenKind::Const)
326+
| Some(TokenKind::BitNot)
327+
| Some(TokenKind::RBrace)
328+
| None
329+
) && !cursor.at_end() {
330+
cursor.advance_kind();
331+
}
318332
}
319333
}
320334
}
@@ -401,43 +415,30 @@ where
401415
break;
402416
}
403417

404-
// Check for "name: type" syntax
405-
if cursor.peek_kind() == Some(TokenKind::Ident)
406-
&& cursor.peek_kind_n(1) == Some(TokenKind::Colon)
407-
{
408-
let name = cursor.consume_ident().unwrap();
409-
cursor.advance_kind(); // consume ':'
418+
// Parse struct field - always use "name: type" format
419+
if cursor.peek_kind() == Some(TokenKind::Ident) {
420+
let name = cursor.consume_ident().unwrap_or_else(|| {
421+
panic!("Expected field name, found: {:?}", cursor.peek_kind())
422+
});
410423

411-
let param_type = parse_type(cursor, self.context.clone(), self.bump);
424+
if cursor.expect_kind(TokenKind::Colon) {
425+
let param_type = parse_type(cursor, self.context.clone(), self.bump);
412426

413-
let normal_param = self.bump.alloc_value(NormalParam {
414-
is_mut: false,
415-
is_move: false,
416-
name,
417-
type_annotation: param_type,
418-
visibility: Visibility::Public,
419-
default_value: None,
420-
});
427+
let normal_param = self.bump.alloc_value(NormalParam {
428+
is_mut: false,
429+
is_move: false,
430+
name,
431+
type_annotation: param_type,
432+
visibility: Visibility::Public,
433+
default_value: None,
434+
});
421435

422-
fields.push(Param::Normal(normal_param));
436+
fields.push(Param::Normal(normal_param));
437+
} else {
438+
panic!("Expected ':' after field name, found: {:?}", cursor.peek_kind());
439+
}
423440
} else {
424-
let name = cursor
425-
.consume_ident()
426-
.unwrap_or_else(|| panic!("Expected field name before type, found: {:?}", cursor.peek_kind()));
427-
428-
cursor.expect_kind(TokenKind::Colon);
429-
let param_type = parse_type(cursor, self.context.clone(), self.bump);
430-
431-
let normal_param = self.bump.alloc_value(NormalParam {
432-
is_mut: false,
433-
is_move: false,
434-
name,
435-
type_annotation: param_type,
436-
visibility: Visibility::Public,
437-
default_value: None,
438-
});
439-
440-
fields.push(Param::Normal(normal_param));
441+
panic!("Expected field name (identifier), found: {:?}", cursor.peek_kind());
441442
}
442443

443444
// Handle comma separator

scribe-parser/src/parser/declaration_parser.rs

Lines changed: 134 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,48 @@ where
460460
break;
461461
}
462462

463+
// Handle const generic parameters: const N: usize
464+
// Skip const generics entirely as Generic struct doesn't support const value types yet
465+
if cursor.peek_kind() == Some(TokenKind::Const) {
466+
cursor.advance_kind(); // consume 'const'
467+
468+
// Skip const generic name (N)
469+
if cursor.peek_kind() == Some(TokenKind::Ident) {
470+
cursor.consume_ident();
471+
}
472+
473+
// Skip type annotation (: usize) if present
474+
if cursor.peek_kind() == Some(TokenKind::Colon) {
475+
cursor.advance_kind(); // consume ':'
476+
// Skip type annotation tokens
477+
while cursor.peek_kind() != Some(TokenKind::Comma)
478+
&& cursor.peek_kind() != Some(TokenKind::Gt)
479+
&& !cursor.at_end()
480+
{
481+
cursor.advance_kind();
482+
}
483+
}
484+
485+
// Skip to comma or closing angle bracket
486+
match cursor.peek_kind() {
487+
Some(TokenKind::Comma) => {
488+
cursor.advance_kind();
489+
continue;
490+
}
491+
Some(TokenKind::Gt) => break,
492+
_ => {
493+
// Try to recover
494+
while cursor.peek_kind() != Some(TokenKind::Comma)
495+
&& cursor.peek_kind() != Some(TokenKind::Gt)
496+
&& !cursor.at_end()
497+
{
498+
cursor.advance_kind();
499+
}
500+
continue;
501+
}
502+
}
503+
}
504+
463505
let name = cursor.consume_ident()
464506
.unwrap_or_else(|| panic!(
465507
"Expected generic parameter name, found {:?}",
@@ -647,20 +689,15 @@ where
647689
) -> Option<&'bump [Param<'a, 'bump>]> {
648690
let mut params: SmallVec<Param<'a, 'bump>, 8> = SmallVec::new();
649691

650-
loop {
692+
loop {
651693
match cursor.peek_kind() {
652694
Some(TokenKind::RParen) => {
653695
cursor.advance_kind(); // consume ')'
654696
break;
655697
}
656-
Some(TokenKind::Mut)
657-
| Some(TokenKind::Ident)
658-
| Some(TokenKind::I32)
659-
| Some(TokenKind::I64)
660-
| Some(TokenKind::Str)
661-
| Some(TokenKind::Void)
662-
| Some(TokenKind::This) => {
663-
// Check for mut modifier
698+
Some(TokenKind::Mul) => {
699+
// Check for pointer types (*mut this, *this)
700+
cursor.advance_kind(); // consume '*'
664701
let is_mut = if cursor.peek_kind() == Some(TokenKind::Mut) {
665702
cursor.advance_kind(); // consume 'mut'
666703
true
@@ -676,9 +713,32 @@ where
676713
type_annotation: None,
677714
});
678715
params.push(Param::This(this_param));
679-
continue;
716+
// Handle comma after *this or *mut this
717+
if cursor.peek_kind() == Some(TokenKind::Comma) {
718+
cursor.advance_kind();
719+
}
680720
}
681-
721+
// Skip if we had * but not followed by this
722+
continue;
723+
}
724+
Some(TokenKind::Mut)
725+
| Some(TokenKind::Ident)
726+
| Some(TokenKind::I32)
727+
| Some(TokenKind::I64)
728+
| Some(TokenKind::Str)
729+
| Some(TokenKind::Void)
730+
| Some(TokenKind::I8)
731+
| Some(TokenKind::I16)
732+
| Some(TokenKind::I128)
733+
| Some(TokenKind::U8)
734+
| Some(TokenKind::U16)
735+
| Some(TokenKind::U32)
736+
| Some(TokenKind::U64)
737+
| Some(TokenKind::U128)
738+
| Some(TokenKind::F32)
739+
| Some(TokenKind::F64)
740+
| Some(TokenKind::Boolean)
741+
| Some(TokenKind::Char) => {
682742
// Try to parse in different formats
683743
let (name, param_type, is_this) = if cursor.peek_kind()
684744
== Some(TokenKind::Ident)
@@ -724,6 +784,16 @@ where
724784
(first_ident, Type::infer(), false)
725785
}
726786
}
787+
Some(TokenKind::I8) => {
788+
cursor.advance_kind(); // consume type
789+
let name = cursor.consume_ident()?;
790+
(name, Type::i8(), false)
791+
}
792+
Some(TokenKind::I16) => {
793+
cursor.advance_kind(); // consume type
794+
let name = cursor.consume_ident()?;
795+
(name, Type::i16(), false)
796+
}
727797
Some(TokenKind::I32) => {
728798
cursor.advance_kind(); // consume type
729799
let name = cursor.consume_ident()?;
@@ -734,11 +804,61 @@ where
734804
let name = cursor.consume_ident()?;
735805
(name, Type::i64(), false)
736806
}
807+
Some(TokenKind::I128) => {
808+
cursor.advance_kind(); // consume type
809+
let name = cursor.consume_ident()?;
810+
(name, Type::i128(), false)
811+
}
812+
Some(TokenKind::U8) => {
813+
cursor.advance_kind(); // consume type
814+
let name = cursor.consume_ident()?;
815+
(name, Type::u8(), false)
816+
}
817+
Some(TokenKind::U16) => {
818+
cursor.advance_kind(); // consume type
819+
let name = cursor.consume_ident()?;
820+
(name, Type::u16(), false)
821+
}
822+
Some(TokenKind::U32) => {
823+
cursor.advance_kind(); // consume type
824+
let name = cursor.consume_ident()?;
825+
(name, Type::u32(), false)
826+
}
827+
Some(TokenKind::U64) => {
828+
cursor.advance_kind(); // consume type
829+
let name = cursor.consume_ident()?;
830+
(name, Type::u64(), false)
831+
}
832+
Some(TokenKind::U128) => {
833+
cursor.advance_kind(); // consume type
834+
let name = cursor.consume_ident()?;
835+
(name, Type::u128(), false)
836+
}
837+
Some(TokenKind::F32) => {
838+
cursor.advance_kind(); // consume type
839+
let name = cursor.consume_ident()?;
840+
(name, Type::f32(), false)
841+
}
842+
Some(TokenKind::F64) => {
843+
cursor.advance_kind(); // consume type
844+
let name = cursor.consume_ident()?;
845+
(name, Type::f64(), false)
846+
}
737847
Some(TokenKind::Str) => {
738848
cursor.advance_kind(); // consume type
739849
let name = cursor.consume_ident()?;
740850
(name, Type::string(), false)
741851
}
852+
Some(TokenKind::Boolean) => {
853+
cursor.advance_kind(); // consume type
854+
let name = cursor.consume_ident()?;
855+
(name, Type::boolean(), false)
856+
}
857+
Some(TokenKind::Char) => {
858+
cursor.advance_kind(); // consume type
859+
let name = cursor.consume_ident()?;
860+
(name, Type::char(), false)
861+
}
742862
Some(TokenKind::Void) => {
743863
cursor.advance_kind(); // consume type
744864
let name = cursor.consume_ident()?;
@@ -751,14 +871,14 @@ where
751871
if is_this {
752872
// Create a ThisParam instead of NormalParam
753873
let this_param = self.bump.alloc_value(ThisParam {
754-
is_mut,
874+
is_mut: false,
755875
is_move: false,
756876
type_annotation: None,
757877
});
758878
params.push(Param::This(this_param));
759879
} else {
760880
let normal_param = self.bump.alloc_value(NormalParam {
761-
is_mut,
881+
is_mut: false,
762882
is_move: false,
763883
name,
764884
type_annotation: param_type,
@@ -768,6 +888,7 @@ where
768888
params.push(Param::Normal(normal_param));
769889
}
770890

891+
// Handle comma separator
771892
if cursor.peek_kind() == Some(TokenKind::Comma) {
772893
cursor.advance_kind();
773894
}

scribe-parser/src/parser/pratt_expr_parser.rs

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -210,48 +210,33 @@ where
210210
let name = cursor.consume_ident().unwrap();
211211

212212
// Check for struct initialization: Type { field: value, ... }
213-
// Only treat as struct init if we see named fields (ident: pattern)
214-
// AND the identifier looks like a type name (typically starts with uppercase)
213+
// Treat as struct init if we see named fields (ident: pattern)
215214
if cursor.peek_kind() == Some(TokenKind::LBrace) {
216-
let name_str = self.context.resolve_string(&name);
217-
// Check if name looks like a type (starts with uppercase letter)
218-
let looks_like_type = name_str.chars().next().map_or(false, |c| c.is_uppercase());
219-
220-
eprintln!("Debug: parse_prefix - Ident followed by LBrace, name={}, looks_like_type={}",
221-
name_str, looks_like_type);
222-
223-
if looks_like_type {
224-
// Look ahead to check if this is a named field pattern
225-
let checkpoint = cursor.checkpoint();
215+
// Look ahead to check if this is a named field pattern
216+
let checkpoint = cursor.checkpoint();
217+
cursor.advance_kind(); // consume '{'
218+
219+
let is_named_fields = if cursor.peek_kind() == Some(TokenKind::RBrace) {
220+
// Empty struct init is valid
221+
true
222+
} else if cursor.peek_kind() == Some(TokenKind::Ident) {
223+
cursor.advance_kind(); // skip ident
224+
cursor.peek_kind() == Some(TokenKind::Colon)
225+
} else {
226+
false
227+
};
228+
cursor.restore(checkpoint);
229+
230+
if is_named_fields {
226231
cursor.advance_kind(); // consume '{'
227-
228-
let is_named_fields = if cursor.peek_kind() == Some(TokenKind::RBrace) {
229-
// Empty struct init is valid
230-
true
231-
} else if cursor.peek_kind() == Some(TokenKind::Ident) {
232-
cursor.advance_kind(); // skip ident
233-
cursor.peek_kind() == Some(TokenKind::Colon)
234-
} else {
235-
false
236-
};
237-
cursor.restore(checkpoint);
238-
239-
eprintln!("Debug: parse_prefix - is_named_fields={}", is_named_fields);
240-
241-
if is_named_fields {
242-
cursor.advance_kind(); // consume '{'
243-
let args = self.parse_class_init_args(cursor);
244-
self.bump.alloc_value(Expr::StructInit {
245-
callee: self.bump.alloc_value_immutable(Expr::Ident { name, span }),
246-
arguments: self.bump.alloc_slice_copy(&args),
247-
positional: true,
248-
span,
249-
})
250-
} else {
251-
self.bump.alloc_value(Expr::Ident { name, span })
252-
}
232+
let args = self.parse_class_init_args(cursor);
233+
self.bump.alloc_value(Expr::StructInit {
234+
callee: self.bump.alloc_value_immutable(Expr::Ident { name, span }),
235+
arguments: self.bump.alloc_slice_copy(&args),
236+
positional: true,
237+
span,
238+
})
253239
} else {
254-
eprintln!("Debug: parse_prefix - Not a type name, returning ident");
255240
self.bump.alloc_value(Expr::Ident { name, span })
256241
}
257242
} else {

0 commit comments

Comments
 (0)