365 lines
16 KiB
Rust
365 lines
16 KiB
Rust
////////////////////////////
|
|
// Automatically Generated//
|
|
////////////////////////////
|
|
use crate::logger::messages::Message;
|
|
use crate::parsing::lexer::lex;
|
|
use crate::parsing::lexer::lex_tokens::LexToken;
|
|
use crate::parsing::parser::parse;
|
|
use crate::parsing::parser::parsed_statement::ParsedStatement;
|
|
use crate::span::Span;
|
|
|
|
fn ignore_error(_msg: Message, _: Span) {
|
|
}
|
|
|
|
fn panic_on_error(msg: Message, _: Span) {
|
|
std::panic::panic_any(msg.stringify());
|
|
}
|
|
#[test]
|
|
fn integration_class_with_many_statements() {
|
|
let script = "final shared class Foobar {
|
|
int _a;
|
|
int _b;
|
|
|
|
Foobar(int a, int b) {
|
|
_a = a;
|
|
_b = b;
|
|
}
|
|
|
|
int GetA(){
|
|
return _a;
|
|
}
|
|
|
|
int Add() {
|
|
return _a + _b;
|
|
}
|
|
|
|
class Inner {
|
|
int a;
|
|
}
|
|
}";
|
|
let lexed_tokens = lex(script, &mut panic_on_error);
|
|
println!("Lexed tokens JSON: {}", serde_json::to_string(&lexed_tokens).unwrap());
|
|
|
|
let parsed_tree = parse(lexed_tokens, &mut panic_on_error);
|
|
println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap());
|
|
let expected_tree: Box<ParsedStatement> =
|
|
serde_json::from_str(r#"{
|
|
"Script": {
|
|
"statements": [
|
|
{
|
|
"ClassDeclaration": {
|
|
"modifiers": 10,
|
|
"name": "Foobar",
|
|
"inherits": [],
|
|
"statements": [
|
|
{
|
|
"Var": {
|
|
"modifier": 0,
|
|
"var_type": {
|
|
"Type": {
|
|
"is_const": false,
|
|
"scope": null,
|
|
"datatype": {
|
|
"DataTypePrimType": {
|
|
"prim_type": "Int32"
|
|
}
|
|
},
|
|
"modifiers": []
|
|
}
|
|
},
|
|
"identifier": "_a",
|
|
"assignment": null
|
|
}
|
|
},
|
|
{
|
|
"Var": {
|
|
"modifier": 0,
|
|
"var_type": {
|
|
"Type": {
|
|
"is_const": false,
|
|
"scope": null,
|
|
"datatype": {
|
|
"DataTypePrimType": {
|
|
"prim_type": "Int32"
|
|
}
|
|
},
|
|
"modifiers": []
|
|
}
|
|
},
|
|
"identifier": "_b",
|
|
"assignment": null
|
|
}
|
|
},
|
|
{
|
|
"FuncDeclaration": {
|
|
"modifiers": 0,
|
|
"field_mod": null,
|
|
"is_destructor": false,
|
|
"is_constructor": true,
|
|
"returns_reference": false,
|
|
"return_type": {
|
|
"Type": {
|
|
"is_const": false,
|
|
"scope": null,
|
|
"datatype": {
|
|
"DataTypeIdentifier": {
|
|
"identifier": "Foobar"
|
|
}
|
|
},
|
|
"modifiers": []
|
|
}
|
|
},
|
|
"name": "Foobar",
|
|
"param_list": {
|
|
"ParamList": {
|
|
"parameters": [
|
|
{
|
|
"parameter_type": {
|
|
"Type": {
|
|
"is_const": false,
|
|
"scope": null,
|
|
"datatype": {
|
|
"DataTypePrimType": {
|
|
"prim_type": "Int32"
|
|
}
|
|
},
|
|
"modifiers": []
|
|
}
|
|
},
|
|
"type_mod": null,
|
|
"identifier": "a",
|
|
"default": null
|
|
},
|
|
{
|
|
"parameter_type": {
|
|
"Type": {
|
|
"is_const": false,
|
|
"scope": null,
|
|
"datatype": {
|
|
"DataTypePrimType": {
|
|
"prim_type": "Int32"
|
|
}
|
|
},
|
|
"modifiers": []
|
|
}
|
|
},
|
|
"type_mod": null,
|
|
"identifier": "b",
|
|
"default": null
|
|
}
|
|
]
|
|
}
|
|
},
|
|
"is_const": false,
|
|
"func_attr": 0,
|
|
"block": {
|
|
"StatBlock": {
|
|
"statements": [
|
|
{
|
|
"Assignment": {
|
|
"left": {
|
|
"VarAccess": {
|
|
"scope": null,
|
|
"identifier": "_a"
|
|
}
|
|
},
|
|
"operator": "Assignment",
|
|
"right": {
|
|
"VarAccess": {
|
|
"scope": null,
|
|
"identifier": "a"
|
|
}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"Assignment": {
|
|
"left": {
|
|
"VarAccess": {
|
|
"scope": null,
|
|
"identifier": "_b"
|
|
}
|
|
},
|
|
"operator": "Assignment",
|
|
"right": {
|
|
"VarAccess": {
|
|
"scope": null,
|
|
"identifier": "b"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"FuncDeclaration": {
|
|
"modifiers": 0,
|
|
"field_mod": null,
|
|
"is_destructor": false,
|
|
"is_constructor": false,
|
|
"returns_reference": false,
|
|
"return_type": {
|
|
"Type": {
|
|
"is_const": false,
|
|
"scope": null,
|
|
"datatype": {
|
|
"DataTypePrimType": {
|
|
"prim_type": "Int32"
|
|
}
|
|
},
|
|
"modifiers": []
|
|
}
|
|
},
|
|
"name": "GetA",
|
|
"param_list": {
|
|
"ParamList": {
|
|
"parameters": []
|
|
}
|
|
},
|
|
"is_const": false,
|
|
"func_attr": 0,
|
|
"block": {
|
|
"StatBlock": {
|
|
"statements": [
|
|
{
|
|
"ReturnStatement": {
|
|
"expression": {
|
|
"VarAccess": {
|
|
"scope": null,
|
|
"identifier": "_a"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"FuncDeclaration": {
|
|
"modifiers": 0,
|
|
"field_mod": null,
|
|
"is_destructor": false,
|
|
"is_constructor": false,
|
|
"returns_reference": false,
|
|
"return_type": {
|
|
"Type": {
|
|
"is_const": false,
|
|
"scope": null,
|
|
"datatype": {
|
|
"DataTypePrimType": {
|
|
"prim_type": "Int32"
|
|
}
|
|
},
|
|
"modifiers": []
|
|
}
|
|
},
|
|
"name": "Add",
|
|
"param_list": {
|
|
"ParamList": {
|
|
"parameters": []
|
|
}
|
|
},
|
|
"is_const": false,
|
|
"func_attr": 0,
|
|
"block": {
|
|
"StatBlock": {
|
|
"statements": [
|
|
{
|
|
"ReturnStatement": {
|
|
"expression": {
|
|
"BinaryExpr": {
|
|
"left": {
|
|
"VarAccess": {
|
|
"scope": null,
|
|
"identifier": "_a"
|
|
}
|
|
},
|
|
"operator": "Addition",
|
|
"right": {
|
|
"VarAccess": {
|
|
"scope": null,
|
|
"identifier": "_b"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"ClassDeclaration": {
|
|
"modifiers": 0,
|
|
"name": "Inner",
|
|
"inherits": [],
|
|
"statements": [
|
|
{
|
|
"Var": {
|
|
"modifier": 0,
|
|
"var_type": {
|
|
"Type": {
|
|
"is_const": false,
|
|
"scope": null,
|
|
"datatype": {
|
|
"DataTypePrimType": {
|
|
"prim_type": "Int32"
|
|
}
|
|
},
|
|
"modifiers": []
|
|
}
|
|
},
|
|
"identifier": "a",
|
|
"assignment": null
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}"#).unwrap();
|
|
assert_eq!(parsed_tree, expected_tree);
|
|
}
|
|
// A substring of a script should never panic, even though it might be completely invalid.
|
|
#[test]
|
|
fn integration_class_with_many_statements_substring() {
|
|
let mut script = "final shared class Foobar {
|
|
int _a;
|
|
int _b;
|
|
|
|
Foobar(int a, int b) {
|
|
_a = a;
|
|
_b = b;
|
|
}
|
|
|
|
int GetA(){
|
|
return _a;
|
|
}
|
|
|
|
int Add() {
|
|
return _a + _b;
|
|
}
|
|
|
|
class Inner {
|
|
int a;
|
|
}
|
|
}".to_string();
|
|
for _ in 0..script.len() {
|
|
script.pop();
|
|
let lexed_tokens = lex(script.as_str(), &mut ignore_error);
|
|
let _parsed_tree = parse(lexed_tokens, &mut ignore_error);
|
|
}
|
|
|
|
}
|
|
|