Add smoke screen tests for parser; add some function grammar rules.

This commit is contained in:
Jesse Brault 2025-05-07 15:05:39 -05:00
parent 68de104595
commit 1a365481ab
2 changed files with 74 additions and 8 deletions

View File

@ -2,7 +2,7 @@
CompilationUnit = { SOI ~ Namespace? ~ ModuleLevelDeclaration* ~ EOI }
Namespace = { "ns" ~ FullyQualifiedName }
CommonDeclaration = { Interface | Class | Module | Function }
CommonDeclaration = { Interface | Class | Module | Function | OperatorFunction }
ModuleLevelDeclaration = { Declare? ~ Public? ~ CommonDeclaration }
InterfaceLevelDeclaration = { CommonDeclaration }
ClassLevelDeclaration = { Declare? ~ Public? ~ ( CommonDeclaration | ClassMember ) }
@ -27,6 +27,9 @@ Public = { "pub" }
Field = { "fld" }
Implementation = { "impl" }
Mutable = { "mut" }
Consume = { "cons" }
Reference = { "ref" }
Define = { "def" }
// Fqn and identifier
FullyQualifiedName = { Identifier ~ ( "::" ~ Identifier )* }
@ -47,13 +50,45 @@ GenericArguments = { "<" ~ FullyQualifiedName ~ ( "," ~ FullyQualifiedName )* ~
GenericParameters = { "<" ~ Identifier ~ ( "," ~ Identifier )* ~ ">" }
// Function
Function = { Implementation?
Function = { ( Implementation | Define )?
~ ( Consume | Mutable )?
~ Reference?
~ "fn"
~ Identifier
~ GenericParameters?
~ Identifier
~ "(" ~ Parameters? ~ ")"
~ TypeAnnotation? ~ ( FunctionEqualsBody | BlockStatement )
~ ReturnType?
~ ( FunctionAlias | FunctionEqualsBody | BlockStatement )?
}
ReturnType = { "->" ~ Reference? ~ TypeUse }
FunctionAlias = { "alias" ~ FullyQualifiedName }
OperatorFunction = { ( Implementation | Define )?
~ ( Consume | Mutable )?
~ Reference?
~ "op"
~ Operator
~ "(" ~ Parameters? ~ ")"
~ ReturnType?
~ ( FunctionAlias | FunctionEqualsBody | BlockStatement )?
}
Operator = { Or
| And
| EqualTo
| NotEqualTo
| Greater
| Less
| GreaterEqual
| LessEqual
| Add
| Subtract
| Multiply
| Divide
| Modulo
| CallOp
}
CallOp = { "()" }
Parameters = { Parameter ~ ( "," ~ Parameter )* }
Parameter = { Identifier ~ TypeAnnotation? }
@ -69,8 +104,10 @@ VariableDeclaration = { "let" ~ Mutable? ~ Identifier ~ TypeAnnotation? ~ ( "="
// Expressions
Expression = { OrExpression }
OrExpression = { AndExpression ~ ( "||" ~ AndExpression )* }
AndExpression = { EqualityExpression ~ ( "&&" ~ EqualityExpression )* }
OrExpression = { AndExpression ~ ( Or ~ AndExpression )* }
AndExpression = { EqualityExpression ~ ( And ~ EqualityExpression )* }
Or = { "||" }
And = { "&&" }
EqualityExpression = { ComparisonExpression ~ ( ( EqualTo | NotEqualTo ) ~ ComparisonExpression )* }
EqualTo = { "==" }

View File

@ -51,7 +51,10 @@ mod deimos_parser_tests {
}
fn parse(rule: Rule, input: &str) -> Pair<Rule> {
let pair = DeimosParser::parse(rule, input).unwrap().next().unwrap();
let pair = DeimosParser::parse(rule, input)
.expect("Parsing failed.")
.next()
.unwrap();
dbg!(&pair);
pair
}
@ -94,4 +97,30 @@ mod deimos_parser_tests {
let call = call_expression_pairs.next().unwrap();
match_rule!(call; Rule::Call);
}
mod smoke_screen_tests {
use crate::parser::deimos_parser_tests::parse;
use crate::parser::Rule;
#[test]
fn simple_interface() {
parse(Rule::CompilationUnit, "pub int Simple { fn foo() -> Void }");
}
#[test]
fn interface_with_op() {
parse(
Rule::CompilationUnit,
"pub int Callable { op () () -> Void }",
);
}
#[test]
fn interface_with_alias() {
parse(
Rule::CompilationUnit,
"pub int Callable {\n fn call() -> Void\n op () () -> Void alias call\n}",
);
}
}
}