Skip to content

Go parser #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/target
Cargo.lock
.vscode/launch.json
.vscode/
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/source-code-parser.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 145 additions & 0 deletions src/lang/go/class_def.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
use crate::go::util::identifier::parse_identifier;
use crate::go::util::vartype::find_type;
use crate::parse::AST;
use crate::prophet::*;

pub(crate) fn parse_type(
ast: &AST,
package: &str,
path: &str,
) -> Option<ClassOrInterfaceComponent> {
let node = match ast.find_child_by_type(&["type_spec"]) {
Some(type_node) => type_node,
None => ast,
};

parse_type_internal(node, &package, path)
}

pub(crate) fn parse_type_internal(
ast: &AST,
package: &str,
path: &str,
) -> Option<ClassOrInterfaceComponent> {

//determine the type of the instance
let instance_type = match ast.find_child_by_type(&["struct_type", "interface_type"]) {
Some(node) => match &*node.r#type {
"interface_type" => InstanceType::InterfaceComponent,
_ => InstanceType::ClassComponent,
},
None => InstanceType::ClassComponent,
};

//find the name of the type
let instance_name = match ast.find_child_by_type(&["type_identifier"]) {
Some(identifier) => identifier.value.clone(),
None => "".into(),
};

//determine what type the instance is
let declaration_type = match instance_type {
InstanceType::InterfaceComponent => ContainerType::Interface,
_ => ContainerType::Class,
};

//get the component information using the path, package, instance name, and instance type
let component = ComponentInfo {
language: Language::Go,
path: path.into(),
package_name: package.into(),
instance_name: format!(
"{}::{}",
instance_name,
match instance_type {
InstanceType::InterfaceComponent => "InterfaceComponent",
_ => "ClassComponent",
}
),
instance_type: instance_type,
};

// Find bounds
let (start, end) = match ast.span {
Some(span) => (span.0 as i32, span.2 as i32),
None => (0, 0),
};

// Define default values
let stereotype = ContainerStereotype::Entity; // TODO determine properly
let mut fields = vec![];
let constructors = vec![];
let mut methods = vec![];
//let mut modifier = Modifier::new();

for child in ast.children.iter() {
match &*child.r#type {
"struct_type" => {
parse_struct_body(child, &component, &mut methods, &mut fields);
}
_ => {}
}
}

Some(ClassOrInterfaceComponent {
component: ContainerComponent {
component: component,
accessor: AccessorType::Public,
stereotype: stereotype,
methods: methods,
container_name: instance_name,
line_count: (end - start) + 1,
},
declaration_type: declaration_type,
annotations: vec![],
constructors: constructors,
field_components: fields,
})
}

fn parse_struct_body(
ast: &AST,
component: &ComponentInfo,
_methods: &mut Vec<MethodComponent>,
fields: &mut Vec<FieldComponent>,
) {
for node in ast.children.iter() {
match &*node.r#type {
"field_declaration_list" => fields.append(&mut parse_fields(node, component)),
_ => {}
}
}
}

fn parse_fields(ast: &AST, component: &ComponentInfo) -> Vec<FieldComponent> {
let mut fields = vec![];
for node in ast.children.iter() {
match &*node.r#type {
"field_declaration" => {
let field_identifier = parse_identifier(node);
let type_identifier = find_type(node);

fields.push(FieldComponent {
component: ComponentInfo {
language: Language::Go,
path: component.path.clone(),
package_name: component.package_name.clone(),
instance_name: field_identifier.clone(),
instance_type: InstanceType::FieldComponent,
},
annotations: vec![],
variables: vec![],
field_name: field_identifier,
accessor: AccessorType::Public,
is_static: false,
is_final: false,
default_value: String::new(),
r#type: type_identifier,
expression: None,
})
}
_ => {}
}
}
fields
}
193 changes: 193 additions & 0 deletions src/lang/go/function_body/expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
use crate::Language;
use crate::go::util::identifier::parse_identifier;

use crate::ast::*;
use crate::ComponentInfo;
use crate::AST;

use super::is_common_junk_tag;

pub(crate) fn parse_expr(ast: &AST, component: &ComponentInfo) -> Option<Expr> {
match &*ast.r#type {
// Variables and initialization
"identifier" | "field_identifier" => parse_ident(ast, component),
"int_literal" |
"interpreted_string_literal" | "nil" | "true" | "false" => Some(Expr::Literal(Literal::new(ast.value.clone(), Language::Go))),
"assignment_statement" => parse_assignment(ast, component),

//language specific
"binary_expression" => parse_binary(ast, component),
"expression_list" => parse_expr_stmt(ast, component),
"inc_statement" | "dec_statement" => parse_inc_dec(ast, component),

//function and method calls
"call_expression" => parse_function(ast, component),
"selector_expression" => Some(parse_dot_expr(ast, component)?.into()),

_ => None,
}
}

pub(crate) fn parse_expr_stmt(ast: &AST, component: &ComponentInfo) -> Option<Expr> {
let mut expr = None;
for comp in ast.children.iter() {
expr = parse_expr(comp, component);
if expr.is_some() {
break;
}
}
expr
}

fn parse_ident(ast: &AST, _component: &ComponentInfo) -> Option<Expr> {
let ident: Expr = Ident::new(ast.value.clone(), Language::Go).into();
Some(ident.into())
}

/// Parse an assignment expression. May contain a variable declaration
pub(crate) fn parse_assignment(ast: &AST, component: &ComponentInfo) -> Option<Expr> {
// Define attributes
let mut lhs = None;
let mut rhs = None;

// Find values
for node in ast.children.iter() {

let unknown = &*node.r#type;
if unknown == "=" {
continue;
}

let result = parse_expr(node, component);

if result.is_some() {
if lhs.is_none() {
lhs = result;
} else if rhs.is_none() {
rhs = result;
} else {
eprintln!(
"Extra parsable tag {} encountered while parsing assignment",
unknown
);
}
} else {
//log_unknown_tag(unknown, "parse_assignment");
}
}

// Assemble
if let Some(lhs) = lhs {
if let Some(rhs) = rhs {
let bin: Expr = BinaryExpr::new(Box::new(lhs.into()), "=".into(), Box::new(rhs), Language::Go).into();
Some(bin.into())
} else {
Some(lhs.into())
}
} else {
eprintln!("Assignment with no lefthand side!");
None
}
}

//parse increment or decrement statments
fn parse_inc_dec(ast: &AST, component: &ComponentInfo) -> Option<Expr> {
let name = if ast.children[0].r#type == "identifier" {
0
} else {
1
};
let op = (name + 1) % 2;

Some(
IncDecExpr::new(
op < name,
ast.children[op].r#type == "++",
Box::new(parse_expr(&ast.children[name], component)?),
Language::Go,
)
.into(),
)
}

fn parse_binary(ast: &AST, component: &ComponentInfo) -> Option<Expr> {
let mut lhs = None;
let mut op = None;
let mut rhs = None;
for child in ast.children.iter() {
if !is_common_junk_tag(&child.r#type) {
let res = Some(child);
if lhs.is_none() {
lhs = res;
} else if op.is_none() {
op = res;
} else if rhs.is_none() {
rhs = res;
break;
}
}
}

if let Some(lhs) = lhs {
if let Some(op) = op {
if let Some(rhs) = rhs {
return Some(
BinaryExpr::new(
Box::new(parse_expr(lhs, component)?),
op.value.as_str().into(),
Box::new(parse_expr(rhs, component)?),
Language::Go
)
.into(),
);
}
}
}
eprintln!("Malformed binary expression detected!");
None
}

fn parse_function(ast: &AST, component: &ComponentInfo) -> Option<Expr> {
let selector = match ast.find_child_by_type(&["selector_expression"]) {
Some(node) => node,
None => ast
};

let argument_list = match ast.find_child_by_type(&["argument_list"]) {
Some(node) => node,
None => ast
};

let args: Vec<Expr> = argument_list
.children
.iter()
.map(|arg| parse_expr(arg, component))
.flat_map(|arg| arg)
.collect();


//determine the type of function call
if selector.find_child_by_type(&["."]).is_some() {
//member functions
let function_name = parse_dot_expr(selector, component)?;

Some(CallExpr::new(Box::new(function_name.into()), args, Language::Go).into())
} else {
//regular functions
let name = Ident::new(parse_identifier(&selector.children[0]), Language::Go);
Some(CallExpr::new(Box::new(name.into()), args, Language::Go).into())
}
}

fn parse_dot_expr(node: &AST, component: &ComponentInfo) -> Option<DotExpr>{
//get the name of what called the function
//let lhs = Ident::new(parse_identifier(&node.children[0]), Language::Go);
let lhs = parse_expr(&node.children[0], component)?;
//let rhs = Ident::new(parse_identifier(&node.children[2]), Language::Go);
let rhs = parse_expr(&node.children[2], component)?;

Some(DotExpr::new(Box::new(lhs), Box::new(rhs), Language::Go))
}



Loading