Super basic work on binder, rework of parser to include their spans

master
Deukhoofd 2022-04-08 16:55:45 +02:00
parent 32636c7724
commit 4e096069ad
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
22 changed files with 2504 additions and 1206 deletions

5
src/binding/mod.rs Normal file
View File

@ -0,0 +1,5 @@
use crate::parsing::parser::parsed_statement::ParsedStatement;
mod type_registry;
pub fn bind(_statement: Box<ParsedStatement>) {}

View File

@ -0,0 +1,38 @@
use crate::parsing::parser::parsed_statement::{ParsedStatement, ParsedStatementData};
use crate::types::script_type::ScriptType;
use crate::types::script_type_namespace::ScriptTypeNamespace;
use crate::types::type_library::TypeLibrary;
pub fn register_types(statement: &ParsedStatement) -> TypeLibrary {
let mut library = TypeLibrary::new();
library = find_types_in_statement(statement, library, None);
library
}
fn find_types_in_statement(
statement: &ParsedStatement,
mut library: TypeLibrary,
active_namespace: Option<ScriptTypeNamespace>,
) -> TypeLibrary {
match &statement.data {
ParsedStatementData::Script { statements } => {
for statement in statements {
library = find_types_in_statement(statement.as_ref(), library, None);
}
}
ParsedStatementData::ClassDeclaration { name, .. } => {
let script_type = ScriptType::new();
if let Some(mut ns) = active_namespace {
ns.register(name, script_type);
} else {
library
.get_global_namespace_mut()
.register(name, script_type);
};
}
_ => {}
}
library
}

View File

@ -257,102 +257,187 @@ fn integration_add_function() {
println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap());
let expected_tree: Box<ParsedStatement> =
serde_json::from_str(r#"{
"data": {
"Script": {
"statements": [
{
"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": [
{
"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
}
]
}
},
"statements": [
{
"data": {
"FuncDeclaration": {
"modifiers": 0,
"field_mod": null,
"is_destructor": false,
"is_constructor": false,
"returns_reference": false,
"return_type": {
"data": {
"Type": {
"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"
}
}
}
}
}
}
]
"scope": null,
"datatype": {
"data": {
"DataTypePrimType": {
"prim_type": "Int32"
}
}
},
"span": {
"start": 0,
"end": 3
}
},
"modifiers": []
}
},
"span": {
"start": 0,
"end": 3
}
},
"name": "add",
"param_list": {
"data": {
"ParamList": {
"parameters": [
{
"parameter_type": {
"data": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"data": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"span": {
"start": 8,
"end": 11
}
},
"modifiers": []
}
},
"span": {
"start": 8,
"end": 11
}
},
"type_mod": null,
"identifier": "a",
"default": null
},
{
"parameter_type": {
"data": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"data": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"span": {
"start": 15,
"end": 18
}
},
"modifiers": []
}
},
"span": {
"start": 15,
"end": 18
}
},
"type_mod": null,
"identifier": "b",
"default": null
}
]
}
},
"span": {
"start": 7,
"end": 21
}
},
"is_const": false,
"func_attr": 0,
"block": {
"data": {
"StatBlock": {
"statements": [
{
"data": {
"ReturnStatement": {
"expression": {
"data": {
"BinaryExpr": {
"left": {
"data": {
"VarAccess": {
"scope": null,
"identifier": "a"
}
},
"span": {
"start": 35,
"end": 36
}
},
"operator": "Addition",
"right": {
"data": {
"VarAccess": {
"scope": null,
"identifier": "b"
}
},
"span": {
"start": 39,
"end": 40
}
}
}
},
"span": {
"start": 35,
"end": 40
}
}
}
},
"span": {
"start": 28,
"end": 41
}
}
]
}
},
"span": {
"start": 22,
"end": 43
}
}
}
]
},
"span": {
"start": 0,
"end": 43
}
}
]
}
}"#).unwrap();
},
"span": {
"start": 0,
"end": 43
}
}
"#).unwrap();
assert_eq!(parsed_tree, expected_tree);
}
// A substring of a script should never panic, even though it might be completely invalid.

View File

@ -12,7 +12,8 @@ fn main() {
mod_file,
r#"////////////////////////////
// Automatically Generated//
////////////////////////////"#
////////////////////////////
"#
)
.unwrap();

View File

@ -44,290 +44,548 @@ fn integration_class_with_many_statements() {
println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap());
let expected_tree: Box<ParsedStatement> =
serde_json::from_str(r#"{
"data": {
"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"
}
}
}
}
]
}
"statements": [
{
"data": {
"ClassDeclaration": {
"modifiers": 10,
"name": "Foobar",
"inherits": [],
"statements": [
{
"data": {
"Var": {
"modifier": 0,
"var_type": {
"data": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"data": {
"DataTypePrimType": {
"prim_type": "Int32"
}
}
},
"span": {
"start": 32,
"end": 35
}
},
"modifiers": []
}
},
{
"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
}
}
]
}
"span": {
"start": 32,
"end": 35
}
]
},
"identifier": "_a",
"assignment": null
}
},
"span": {
"start": 32,
"end": 39
}
},
{
"data": {
"Var": {
"modifier": 0,
"var_type": {
"data": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"data": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"span": {
"start": 44,
"end": 47
}
},
"modifiers": []
}
},
"span": {
"start": 44,
"end": 47
}
},
"identifier": "_b",
"assignment": null
}
},
"span": {
"start": 44,
"end": 51
}
},
{
"data": {
"FuncDeclaration": {
"modifiers": 0,
"field_mod": null,
"is_destructor": false,
"is_constructor": true,
"returns_reference": false,
"return_type": {
"data": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"data": {
"DataTypeIdentifier": {
"identifier": "Foobar"
}
},
"span": {
"start": 57,
"end": 63
}
},
"modifiers": []
}
},
"span": {
"start": 57,
"end": 63
}
},
"name": "Foobar",
"param_list": {
"data": {
"ParamList": {
"parameters": [
{
"parameter_type": {
"data": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"data": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"span": {
"start": 64,
"end": 67
}
},
"modifiers": []
}
},
"span": {
"start": 64,
"end": 67
}
},
"type_mod": null,
"identifier": "a",
"default": null
},
{
"parameter_type": {
"data": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"data": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"span": {
"start": 71,
"end": 74
}
},
"modifiers": []
}
},
"span": {
"start": 71,
"end": 74
}
},
"type_mod": null,
"identifier": "b",
"default": null
}
]
}
},
"span": {
"start": 63,
"end": 77
}
},
"is_const": false,
"func_attr": 0,
"block": {
"data": {
"StatBlock": {
"statements": [
{
"data": {
"Assignment": {
"left": {
"data": {
"VarAccess": {
"scope": null,
"identifier": "_a"
}
},
"span": {
"start": 88,
"end": 90
}
},
"operator": "Assignment",
"right": {
"data": {
"VarAccess": {
"scope": null,
"identifier": "a"
}
},
"span": {
"start": 93,
"end": 94
}
}
}
},
"span": {
"start": 88,
"end": 94
}
},
{
"data": {
"Assignment": {
"left": {
"data": {
"VarAccess": {
"scope": null,
"identifier": "_b"
}
},
"span": {
"start": 104,
"end": 106
}
},
"operator": "Assignment",
"right": {
"data": {
"VarAccess": {
"scope": null,
"identifier": "b"
}
},
"span": {
"start": 109,
"end": 110
}
}
}
},
"span": {
"start": 104,
"end": 110
}
}
]
}
},
"span": {
"start": 78,
"end": 117
}
}
}
},
"span": {
"start": 57,
"end": 117
}
},
{
"data": {
"FuncDeclaration": {
"modifiers": 0,
"field_mod": null,
"is_destructor": false,
"is_constructor": false,
"returns_reference": false,
"return_type": {
"data": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"data": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"span": {
"start": 123,
"end": 126
}
},
"modifiers": []
}
},
"span": {
"start": 123,
"end": 126
}
},
"name": "GetA",
"param_list": {
"data": {
"ParamList": {
"parameters": []
}
},
"span": {
"start": 131,
"end": 133
}
},
"is_const": false,
"func_attr": 0,
"block": {
"data": {
"StatBlock": {
"statements": [
{
"data": {
"ReturnStatement": {
"expression": {
"data": {
"VarAccess": {
"scope": null,
"identifier": "_a"
}
},
"span": {
"start": 150,
"end": 152
}
}
}
},
"span": {
"start": 143,
"end": 153
}
}
]
}
},
"span": {
"start": 133,
"end": 159
}
}
}
},
"span": {
"start": 123,
"end": 159
}
},
{
"data": {
"FuncDeclaration": {
"modifiers": 0,
"field_mod": null,
"is_destructor": false,
"is_constructor": false,
"returns_reference": false,
"return_type": {
"data": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"data": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"span": {
"start": 165,
"end": 168
}
},
"modifiers": []
}
},
"span": {
"start": 165,
"end": 168
}
},
"name": "Add",
"param_list": {
"data": {
"ParamList": {
"parameters": []
}
},
"span": {
"start": 172,
"end": 174
}
},
"is_const": false,
"func_attr": 0,
"block": {
"data": {
"StatBlock": {
"statements": [
{
"data": {
"ReturnStatement": {
"expression": {
"data": {
"BinaryExpr": {
"left": {
"data": {
"VarAccess": {
"scope": null,
"identifier": "_a"
}
},
"span": {
"start": 192,
"end": 194
}
},
"operator": "Addition",
"right": {
"data": {
"VarAccess": {
"scope": null,
"identifier": "_b"
}
},
"span": {
"start": 197,
"end": 199
}
}
}
},
"span": {
"start": 192,
"end": 199
}
}
}
},
"span": {
"start": 185,
"end": 200
}
}
]
}
},
"span": {
"start": 175,
"end": 206
}
}
}
},
"span": {
"start": 165,
"end": 206
}
},
{
"data": {
"ClassDeclaration": {
"modifiers": 0,
"name": "Inner",
"inherits": [],
"statements": [
{
"data": {
"Var": {
"modifier": 0,
"var_type": {
"data": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"data": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"span": {
"start": 234,
"end": 237
}
},
"modifiers": []
}
},
"span": {
"start": 234,
"end": 237
}
},
"identifier": "a",
"assignment": null
}
},
"span": {
"start": 234,
"end": 240
}
}
]
}
},
"span": {
"start": 212,
"end": 246
}
}
]
}
]
},
"span": {
"start": 0,
"end": 248
}
}
]
}
},
"span": {
"start": 0,
"end": 248
}
}"#).unwrap();
assert_eq!(parsed_tree, expected_tree);
}

View File

@ -79,19 +79,32 @@ fn integration_empty_class_declaration() {
println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap());
let expected_tree: Box<ParsedStatement> =
serde_json::from_str(r#"{
"data": {
"Script": {
"statements": [
{
"ClassDeclaration": {
"modifiers": 0,
"name": "Foo",
"inherits": [],
"statements": []
}
"statements": [
{
"data": {
"ClassDeclaration": {
"modifiers": 0,
"name": "Foo",
"inherits": [],
"statements": []
}
]
},
"span": {
"start": 0,
"end": 12
}
}
]
}
}"#).unwrap();
},
"span": {
"start": 0,
"end": 12
}
}
"#).unwrap();
assert_eq!(parsed_tree, expected_tree);
}
// A substring of a script should never panic, even though it might be completely invalid.

View File

@ -398,45 +398,69 @@ fn integration_enum_definition() {
println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap());
let expected_tree: Box<ParsedStatement> =
serde_json::from_str(r#"{
"data": {
"Script": {
"statements": [
{
"EnumDeclaration": {
"modifiers": 0,
"identifier": "TestEnum",
"base_type": {
"DataTypePrimType": {
"prim_type": "UInt8"
}
},
"values": [
[
"a",
null
],
[
"b",
null
],
[
"c",
null
],
[
"d",
{
"IntegerLiteral": 128
}
],
[
"e",
null
]
]
"statements": [
{
"data": {
"EnumDeclaration": {
"modifiers": 0,
"identifier": "TestEnum",
"base_type": {
"data": {
"DataTypePrimType": {
"prim_type": "UInt8"
}
},
"span": {
"start": 16,
"end": 21
}
},
"values": [
[
"a",
null
],
[
"b",
null
],
[
"c",
null
],
[
"d",
{
"data": {
"IntegerLiteral": 128
},
"span": {
"start": 53,
"end": 56
}
}
],
[
"e",
null
]
]
}
]
},
"span": {
"start": 0,
"end": 65
}
}
]
}
},
"span": {
"start": 0,
"end": 65
}
}"#).unwrap();
assert_eq!(parsed_tree, expected_tree);
}

View File

@ -1,6 +1,7 @@
////////////////////////////
// Automatically Generated//
////////////////////////////mod enum_definition;
////////////////////////////
mod enum_definition;
mod multiple_inheritance_class;
mod empty_class_declaration;
mod class_with_many_statements;

View File

@ -155,23 +155,36 @@ fn integration_multiple_inheritance_class() {
println!("Parsed Tree JSON: {}", serde_json::to_string(&parsed_tree).unwrap());
let expected_tree: Box<ParsedStatement> =
serde_json::from_str(r#"{
"data": {
"Script": {
"statements": [
{
"ClassDeclaration": {
"modifiers": 0,
"name": "Foo",
"inherits": [
"Zom",
"Aar",
"Bar"
],
"statements": []
}
"statements": [
{
"data": {
"ClassDeclaration": {
"modifiers": 0,
"name": "Foo",
"inherits": [
"Zom",
"Aar",
"Bar"
],
"statements": []
}
]
},
"span": {
"start": 0,
"end": 28
}
}
]
}
}"#).unwrap();
},
"span": {
"start": 0,
"end": 28
}
}
"#).unwrap();
assert_eq!(parsed_tree, expected_tree);
}
// A substring of a script should never panic, even though it might be completely invalid.

View File

@ -1,97 +1,181 @@
{
"data": {
"Script": {
"statements": [
{
"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": [
{
"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
}
]
}
},
"statements": [
{
"data": {
"FuncDeclaration": {
"modifiers": 0,
"field_mod": null,
"is_destructor": false,
"is_constructor": false,
"returns_reference": false,
"return_type": {
"data": {
"Type": {
"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"
}
}
}
}
}
}
]
"scope": null,
"datatype": {
"data": {
"DataTypePrimType": {
"prim_type": "Int32"
}
}
},
"span": {
"start": 0,
"end": 3
}
},