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

master
Deukhoofd 8 months ago
parent 32636c7724
commit 4e096069ad
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
  1. 5
      src/binding/mod.rs
  2. 38
      src/binding/type_registry.rs
  3. 255
      src/integration_tests/add_function.rs
  4. 3
      src/integration_tests/build.rs
  5. 762
      src/integration_tests/class_with_many_statements.rs
  6. 33
      src/integration_tests/empty_class_declaration.rs
  7. 94
      src/integration_tests/enum_definition.rs
  8. 3
      src/integration_tests/mod.rs
  9. 41
      src/integration_tests/multiple_inheritance_class.rs
  10. 254
      src/integration_tests/test_cases/add_function/parsed_tree.json
  11. 762
      src/integration_tests/test_cases/class_with_many_statements/parsed_tree.json
  12. 32
      src/integration_tests/test_cases/empty_class_declaration/parsed_tree.json
  13. 94
      src/integration_tests/test_cases/enum_definition/parsed_tree.json
  14. 40
      src/integration_tests/test_cases/multiple_inheritance_class/parsed_tree.json
  15. 9
      src/lib.rs
  16. 941
      src/parsing/parser/mod.rs
  17. 24
      src/parsing/parser/parsed_statement.rs
  18. 119
      src/parsing/parser/parser_tests.rs
  19. 10
      src/types/mod.rs
  20. 33
      src/types/script_type.rs
  21. 22
      src/types/script_type_namespace.rs
  22. 22
      src/types/type_library.rs

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

@ -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
}

@ -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": []
"statements": [
{
"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": 0,
"end": 3
}
},
"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
"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"
}
},
{
"parameter_type": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"modifiers": []
}
},
"type_mod": null,
"identifier": "b",
"default": null
"span": {
"start": 8,
"end": 11
}
]
}
},
"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"
}
}
}
}
},
"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.

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

@ -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": {
"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": []
}
},
"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": {
"DataTypePrimType": {
"prim_type": "Int32"
}
"is_const": false,
"scope": null,
"datatype": {
"data": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"modifiers": []
"span": {
"start": 64,
"end": 67
}
},
"modifiers": []
}
},
"span": {
"start": 64,
"end": 67
}
},
"identifier": "_a",
"assignment": null
}
},
{
"Var": {
"modifier": 0,
"var_type": {
"type_mod": null,
"identifier": "a",
"default": null
},
{
"parameter_type": {
"data": {
"Type": {
"is_const": false,
"scope": null,
"datatype": {
"DataTypePrimType": {
"prim_type": "Int32"
}
"is_const": false,
"scope": null,
"datatype": {
"data": {
"DataTypePrimType": {
"prim_type": "Int32"
}
},
"modifiers": []
"span": {
"start": 71,
"end": 74
}
},
"modifiers": []
}
},
"span": {
"start": 71,
"end": 74
}
},
"identifier": "_b",
"assignment": null
}
"type_mod": null,
"identifier": "b",
"default": 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": []
"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
}
}
}
},
"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
}
]
"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
}
}
}
},
"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"
}
}
}
}
]
}
"span": {
"start": 104,
"end": 110
}
}
}
]
}
},
{
"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": []
"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
}
}
}
},
"is_const": false,
"func_attr": 0,
"block": {
"StatBlock": {
"statements": [
{
"ReturnStatement": {
"expression": {
"VarAccess": {
"scope": null,
"identifier": "_a"
}
}
}
}
]
}
"span": {
"start": 143,
"end": 153
}
}
}
]
}
},
{
"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"
"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
}
},
"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"
}
}
}
}
}
},
"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": [
{
"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
"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);
}

@ -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.

@ -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);
}

@ -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;

@ -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.

@ -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": []
"statements": [
{
"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": 0,
"end": 3
}
},
"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
"modifiers": []
}
},
"span": {
"start": 0,
"end": 3
}