better parser errors

This commit is contained in:
Masashi 2025-12-15 21:18:25 +05:30
commit f83a8e3ca1
13 changed files with 639 additions and 78 deletions

View file

@ -0,0 +1,22 @@
# Attribute errors
# Missing attribute name
@ 42
# Invalid attribute argument
@deprecated invalid
# Missing equals in key-value attribute
@version "1.0"
# Missing closing parenthesis in attribute
@deprecated("old", reason = "new"
# Invalid attribute syntax (missing value after =)
@version =
# Attribute on invalid location
fn test() -> int
@inline
let x = 5
x

View file

@ -0,0 +1,47 @@
# Bracket and parenthesis mismatch errors
# Missing closing parenthesis in function call
fn test() -> int
let f = lambda(x) x
f(5
42
# Missing closing bracket in array access
fn test() -> int
let arr = [1, 2, 3]
arr[0
42
# Missing closing brace in struct literal
fn test() -> int
struct Point
x: int
y: int
end
let p = Point { x: 5, y: 10
p.x
# Missing closing parenthesis in tuple
fn test() -> int
let t = (1, 2, 3
42
# Missing closing angle bracket in generic
fn test() -> int
let v = Vec<int
42
# Extra closing parenthesis
fn test() -> int
let x = (5))
x
# Mismatched brackets
fn test() -> int
let x = [1, 2, 3}
x[0]
# Missing opening parenthesis in expression
fn test() -> int
let x = 5 + 3)
x

View file

@ -0,0 +1,49 @@
# Control flow errors
# Missing condition in if
fn test() -> int
if
42
else
0
# Missing body in if
fn test() -> int
if true
else
0
# Missing variable in for loop
fn test() -> int
for in [1, 2, 3]
item
0
# Missing 'in' keyword in for loop
fn test() -> int
for item [1, 2, 3]
item
0
# Missing iterable in for loop
fn test() -> int
for item in
item
0
# Invalid while syntax
fn test() -> int
while
42
# Missing parameters in lambda
fn test() -> int
let f = lambda
42
f()
# Missing closing parenthesis in lambda
fn test() -> int
let f = lambda(x
x
f(5)

View file

@ -0,0 +1,29 @@
# Enum syntax errors
# Missing enum name
enum
Red
Green
end
# Missing variant name
enum Color
Red
Green
end
# Invalid variant syntax (missing parentheses for tuple variant)
enum Result
Ok int
Err string
end
# Missing comma between variants
enum Color
Red Green
end
# Missing end keyword
enum Color
Red
Green

View file

@ -0,0 +1,43 @@
# Expression and statement errors
# Missing variable name in let
fn test() -> int
let = 5
42
# Missing equals in assignment
fn test() -> int
let x 5
x
# Missing expression after operator
fn test() -> int
5 +
# Invalid array syntax (missing comma)
fn test() -> int
let arr = [1 2 3]
arr[0]
# Missing closing bracket in array
fn test() -> int
let arr = [1, 2, 3
arr[0]
# Invalid struct literal (missing field value)
fn test() -> int
struct Point
x: int
y: int
end
let p = Point { x: 5, y }
p.x
# Missing colon in struct literal
fn test() -> int
struct Point
x: int
y: int
end
let p = Point { x 5, y: 10 }
p.x

View file

@ -0,0 +1,24 @@
# Function syntax errors
# Missing function name
fn -> int
42
# Missing parameter type annotation
fn add(x, y: int) -> int
x + y
# Missing comma in parameters
fn add(x: int y: int) -> int
x + y
# Missing closing parenthesis
fn add(x: int, y: int -> int
x + y
# Missing return type arrow
fn add(x: int, y: int) int
x + y
# Invalid function body (missing expression)
fn test() -> int

View file

@ -0,0 +1,41 @@
# Pattern matching errors
# Invalid pattern in match
fn test() -> int
match 42
x => x
end
# Missing colon in struct pattern
fn test() -> int
struct Point
x: int
y: int
end
let p = Point { x: 5, y: 10 }
match p
Point { x 5, y: 10 } => 1
end
# Invalid enum pattern
fn test() -> int
enum Result
Ok(int)
Err(string)
end
let r = Result::Ok(42)
match r
Result::Ok value => value
end
# Missing pattern in match arm
fn test() -> int
match 42
=> 0
end
# Missing fat arrow in match arm
fn test() -> int
match 42
x 0
end

View file

@ -0,0 +1,30 @@
# Struct syntax errors
# Missing struct name
struct
x: int
y: int
end
# Missing field type
struct Point
x
y: int
end
# Missing colon in field
struct Point
x int
y: int
end
# Missing end keyword
struct Point
x: int
y: int
# Invalid field separator (semicolon instead of comma)
struct Point
x: int;
y: int
end

View file

@ -0,0 +1,45 @@
# Top-level declaration errors
# Invalid top-level token
invalid_token
# Incomplete use statement
use
# Missing path in use
use as alias
# Invalid use syntax
use "path" invalid
# Incomplete load statement
load
# Missing alias in load
load "lib"
# Invalid load syntax
load "lib" invalid alias
# Incomplete extern declaration
extern
# Missing return type in extern
extern add(x: int, y: int)
# Invalid extern syntax
extern add(x: int, y: int) -> int invalid
# Incomplete impl declaration
impl
# Missing methods in impl
impl MyType
end
# Incomplete trait declaration
trait
# Missing methods in trait
trait MyTrait
end

View file

@ -0,0 +1,29 @@
# Type annotation errors
# Invalid type name
fn test() -> invalidtype
42
# Missing closing bracket in array type
fn test() -> [int
[]
# Missing type parameter
fn test() -> Option
none
# Invalid generic syntax
fn test() -> Vec int
[]
# Missing closing angle bracket
fn test() -> HashMap<string, int
{}
# Invalid pointer syntax
fn test() -> *int
42
# Invalid function type syntax
fn test() -> fn int -> int
42