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 } CompilationUnit = { SOI ~ Namespace? ~ ModuleLevelDeclaration* ~ EOI }
Namespace = { "ns" ~ FullyQualifiedName } Namespace = { "ns" ~ FullyQualifiedName }
CommonDeclaration = { Interface | Class | Module | Function } CommonDeclaration = { Interface | Class | Module | Function | OperatorFunction }
ModuleLevelDeclaration = { Declare? ~ Public? ~ CommonDeclaration } ModuleLevelDeclaration = { Declare? ~ Public? ~ CommonDeclaration }
InterfaceLevelDeclaration = { CommonDeclaration } InterfaceLevelDeclaration = { CommonDeclaration }
ClassLevelDeclaration = { Declare? ~ Public? ~ ( CommonDeclaration | ClassMember ) } ClassLevelDeclaration = { Declare? ~ Public? ~ ( CommonDeclaration | ClassMember ) }
@ -27,6 +27,9 @@ Public = { "pub" }
Field = { "fld" } Field = { "fld" }
Implementation = { "impl" } Implementation = { "impl" }
Mutable = { "mut" } Mutable = { "mut" }
Consume = { "cons" }
Reference = { "ref" }
Define = { "def" }
// Fqn and identifier // Fqn and identifier
FullyQualifiedName = { Identifier ~ ( "::" ~ Identifier )* } FullyQualifiedName = { Identifier ~ ( "::" ~ Identifier )* }
@ -47,13 +50,45 @@ GenericArguments = { "<" ~ FullyQualifiedName ~ ( "," ~ FullyQualifiedName )* ~
GenericParameters = { "<" ~ Identifier ~ ( "," ~ Identifier )* ~ ">" } GenericParameters = { "<" ~ Identifier ~ ( "," ~ Identifier )* ~ ">" }
// Function // Function
Function = { Implementation? Function = { ( Implementation | Define )?
~ ( Consume | Mutable )?
~ Reference?
~ "fn" ~ "fn"
~ Identifier
~ GenericParameters? ~ GenericParameters?
~ Identifier
~ "(" ~ Parameters? ~ ")" ~ "(" ~ 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 )* } Parameters = { Parameter ~ ( "," ~ Parameter )* }
Parameter = { Identifier ~ TypeAnnotation? } Parameter = { Identifier ~ TypeAnnotation? }
@ -69,8 +104,10 @@ VariableDeclaration = { "let" ~ Mutable? ~ Identifier ~ TypeAnnotation? ~ ( "="
// Expressions // Expressions
Expression = { OrExpression } Expression = { OrExpression }
OrExpression = { AndExpression ~ ( "||" ~ AndExpression )* } OrExpression = { AndExpression ~ ( Or ~ AndExpression )* }
AndExpression = { EqualityExpression ~ ( "&&" ~ EqualityExpression )* } AndExpression = { EqualityExpression ~ ( And ~ EqualityExpression )* }
Or = { "||" }
And = { "&&" }
EqualityExpression = { ComparisonExpression ~ ( ( EqualTo | NotEqualTo ) ~ ComparisonExpression )* } EqualityExpression = { ComparisonExpression ~ ( ( EqualTo | NotEqualTo ) ~ ComparisonExpression )* }
EqualTo = { "==" } EqualTo = { "==" }

View File

@ -51,7 +51,10 @@ mod deimos_parser_tests {
} }
fn parse(rule: Rule, input: &str) -> Pair<Rule> { 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); dbg!(&pair);
pair pair
} }
@ -83,7 +86,7 @@ mod deimos_parser_tests {
match_rule!(call; Rule::Call); match_rule!(call; Rule::Call);
}); });
} }
#[test] #[test]
fn identifier_call_as_call_expression() { fn identifier_call_as_call_expression() {
let pair = parse(Rule::CallExpression, "foo()"); let pair = parse(Rule::CallExpression, "foo()");
@ -94,4 +97,30 @@ mod deimos_parser_tests {
let call = call_expression_pairs.next().unwrap(); let call = call_expression_pairs.next().unwrap();
match_rule!(call; Rule::Call); 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}",
);
}
}
} }