Work on grammar and AST building.

This commit is contained in:
Jesse Brault 2025-04-14 08:00:05 -05:00
parent 94f496a63d
commit 1263d84802
2 changed files with 191 additions and 99 deletions

View File

@ -1,5 +1,5 @@
use pest::{iterators::{Pair, Pairs}, Parser};
use crate::parser::{DeimosParser, Rule}; use crate::parser::{DeimosParser, Rule};
use pest::{iterators::Pair, Parser};
#[derive(Debug)] #[derive(Debug)]
pub struct CompilationUnit { pub struct CompilationUnit {
@ -9,7 +9,7 @@ pub struct CompilationUnit {
#[derive(Debug)] #[derive(Debug)]
pub struct Fqn { pub struct Fqn {
identifiers: Vec<Identifier> identifiers: Vec<Identifier>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -25,7 +25,7 @@ pub enum Declaration {
is_extern: bool, is_extern: bool,
is_public: bool, is_public: bool,
type_declaration: TypeDeclaration, type_declaration: TypeDeclaration,
impl_ctor: Option<ImplCtor> impl_ctor: Option<ImplCtor>,
}, },
Module { Module {
identifier: Identifier, identifier: Identifier,
@ -37,92 +37,98 @@ pub enum Declaration {
identifier: Identifier, identifier: Identifier,
is_extern: bool, is_extern: bool,
is_public: bool, is_public: bool,
statement: Statement parameters: Vec<Parameter>,
declared_type: Option<TypeUse>,
statement: Statement,
}, },
Prop(Prop), Prop(Prop),
Field(Field) Field(Field),
} }
#[derive(Debug)] #[derive(Debug)]
pub struct TypeDeclaration { pub struct TypeDeclaration {
generics: Vec<GenericParameter>, generics: Vec<GenericParameter>,
extends: Vec<Type>, extends: Vec<TypeUse>,
declarations: Vec<Declaration>, declarations: Vec<Declaration>,
} }
impl TypeDeclaration { impl TypeDeclaration {
pub fn new(generics: Vec<GenericParameter>, extends: Vec<Type>, declarations: Vec<Declaration>) -> TypeDeclaration { pub fn new(
generics: Vec<GenericParameter>,
extends: Vec<TypeUse>,
declarations: Vec<Declaration>,
) -> TypeDeclaration {
TypeDeclaration { TypeDeclaration {
generics, generics,
extends, extends,
declarations declarations,
} }
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Identifier { pub struct Identifier {
name: String name: String,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct GenericParameter { pub struct GenericParameter {
identifier: Identifier, identifier: Identifier,
bound: Option<GenericBound> bound: Option<GenericBound>,
} }
#[derive(Debug)] #[derive(Debug)]
pub enum GenericBound { pub enum GenericBound {
Extends { Extends { fqn: Fqn },
fqn: Fqn Super { fqn: Fqn },
},
Super {
fqn: Fqn
}
} }
#[derive(Debug)] #[derive(Debug)]
pub struct GenericArgument { pub struct GenericArgument {
fqn: Fqn fqn: Fqn,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Type { pub struct TypeUse {
fqn: Fqn, fqn: Fqn,
generics: Vec<GenericArgument> generics: Vec<GenericArgument>,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct ImplCtor { pub struct ImplCtor {
args: Vec<ImplCtorArg> args: Vec<ImplCtorArg>,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct ImplCtorArg { pub struct ImplCtorArg {
is_field: bool, is_field: bool,
identifier: Identifier, identifier: Identifier,
r#type: Option<Type> r#type: Option<TypeUse>,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Prop { pub struct Prop {
identifier: Identifier, identifier: Identifier,
r#type: Type r#type: TypeUse,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Field { pub struct Field {
identifier: Identifier, identifier: Identifier,
r#type: Type r#type: TypeUse,
} }
#[derive(Debug)] #[derive(Debug)]
pub enum Statement { pub enum Statement {
BlockStatement { BlockStatement { statements: Vec<Statement> },
statements: Vec<Statement>
},
CallStatement, CallStatement,
AssignmentStatement AssignmentStatement,
}
#[derive(Debug)]
pub struct Parameter {
identifier: Identifier,
declared_type: Option<TypeUse>,
} }
fn build_field(field_pair: Pair<Rule>) -> Declaration { fn build_field(field_pair: Pair<Rule>) -> Declaration {
@ -136,27 +142,27 @@ fn build_prop(prop_pair: Pair<Rule>) -> Declaration {
fn build_impl_ctor_arg(impl_ctor_arg_pair: Pair<Rule>) -> ImplCtorArg { fn build_impl_ctor_arg(impl_ctor_arg_pair: Pair<Rule>) -> ImplCtorArg {
let mut is_field = false; let mut is_field = false;
let mut identifier: Option<Identifier> = None; let mut identifier: Option<Identifier> = None;
let mut r#type: Option<Type> = None; let mut r#type: Option<TypeUse> = None;
for pair in impl_ctor_arg_pair.into_inner() { for pair in impl_ctor_arg_pair.into_inner() {
match pair.as_rule() { match pair.as_rule() {
Rule::fld => { Rule::fld => {
is_field = true; is_field = true;
}, }
Rule::identifier => { Rule::identifier => {
identifier = Some(build_identifier(pair)); identifier = Some(build_identifier(pair));
}, }
Rule::r#type => { Rule::type_use => {
r#type = Some(build_type(pair)); r#type = Some(build_type_use(pair));
}, }
_ => panic!("Unexpected rule: {}", pair) _ => panic!("Unexpected rule: {}", pair),
} }
} }
ImplCtorArg { ImplCtorArg {
is_field, is_field,
identifier: identifier.unwrap(), identifier: identifier.unwrap(),
r#type r#type,
} }
} }
@ -169,30 +175,30 @@ fn build_impl_ctor(impl_ctor_pair: Pair<Rule>) -> ImplCtor {
Rule::impl_ctor_arg => { Rule::impl_ctor_arg => {
args.push(build_impl_ctor_arg(pair)); args.push(build_impl_ctor_arg(pair));
} }
_ => panic!("Unexpected rule: {}", pair) _ => panic!("Unexpected rule: {}", pair),
} }
} }
ImplCtor { ImplCtor { args }
args
}
} }
fn build_generic_argument(generic_argument_pair: Pair<Rule>) -> GenericArgument { fn build_generic_argument(generic_argument_pair: Pair<Rule>) -> GenericArgument {
let fqn_pair = generic_argument_pair.into_inner().next().unwrap(); let fqn_pair = generic_argument_pair.into_inner().next().unwrap();
GenericArgument { GenericArgument {
fqn: build_fqn(fqn_pair) fqn: build_fqn(fqn_pair),
} }
} }
fn build_generic_arguments_declaration(generic_arguments_declaration_pair: Pair<Rule>) -> Vec<GenericArgument> { fn build_generic_arguments_declaration(
generic_arguments_declaration_pair: Pair<Rule>,
) -> Vec<GenericArgument> {
let mut generic_arguments: Vec<GenericArgument> = vec![]; let mut generic_arguments: Vec<GenericArgument> = vec![];
for pair in generic_arguments_declaration_pair.into_inner() { for pair in generic_arguments_declaration_pair.into_inner() {
match pair.as_rule() { match pair.as_rule() {
Rule::generic_argument => { Rule::generic_argument => {
generic_arguments.push(build_generic_argument(pair)); generic_arguments.push(build_generic_argument(pair));
} }
_ => panic!("Expected only generic_argument rules. Found: {}", pair) _ => panic!("Expected only generic_argument rules. Found: {}", pair),
} }
} }
generic_arguments generic_arguments
@ -206,7 +212,7 @@ fn build_generic_parameter(generic_parameter_pair: Pair<Rule>) -> GenericParamet
} }
} }
fn build_type(type_pair: Pair<Rule>) -> Type { fn build_type_use(type_pair: Pair<Rule>) -> TypeUse {
let mut fqn: Option<Fqn> = None; let mut fqn: Option<Fqn> = None;
let mut generic_arguments_declaration: Option<Vec<GenericArgument>> = None; let mut generic_arguments_declaration: Option<Vec<GenericArgument>> = None;
@ -218,38 +224,43 @@ fn build_type(type_pair: Pair<Rule>) -> Type {
Rule::generic_arguments_declaration => { Rule::generic_arguments_declaration => {
generic_arguments_declaration = Some(build_generic_arguments_declaration(pair)); generic_arguments_declaration = Some(build_generic_arguments_declaration(pair));
} }
_ => panic!("Expected only fqn or generic_arguments_declaration rules. Found: {}", pair) _ => panic!(
"Expected only fqn or generic_arguments_declaration rules. Found: {}",
pair
),
} }
} }
Type { TypeUse {
fqn: fqn.unwrap(), fqn: fqn.unwrap(),
generics: generic_arguments_declaration.unwrap_or(vec![]) generics: generic_arguments_declaration.unwrap_or(vec![]),
} }
} }
fn build_generic_parameters_declaration(generic_parameters_declaration_pair: Pair<Rule>) -> Vec<GenericParameter> { fn build_generic_parameters_declaration(
generic_parameters_declaration_pair: Pair<Rule>,
) -> Vec<GenericParameter> {
let mut parameters: Vec<GenericParameter> = vec![]; let mut parameters: Vec<GenericParameter> = vec![];
for pair in generic_parameters_declaration_pair.into_inner() { for pair in generic_parameters_declaration_pair.into_inner() {
match pair.as_rule() { match pair.as_rule() {
Rule::generic_parameter => { Rule::generic_parameter => {
parameters.push(build_generic_parameter(pair)); parameters.push(build_generic_parameter(pair));
} }
_ => panic!("Expected only generic_parameter rule. Found: {}", pair) _ => panic!("Expected only generic_parameter rule. Found: {}", pair),
} }
} }
parameters parameters
} }
fn build_extends_list(extends_list_pair: Pair<Rule>) -> Vec<Type> { fn build_extends_list(extends_list_pair: Pair<Rule>) -> Vec<TypeUse> {
let mut extensions: Vec<Type> = vec![]; let mut extensions: Vec<TypeUse> = vec![];
for pair in extends_list_pair.into_inner() { for pair in extends_list_pair.into_inner() {
match pair.as_rule() { match pair.as_rule() {
Rule::r#type => { Rule::type_use => {
extensions.push(build_type(pair)); extensions.push(build_type_use(pair));
} }
_ => panic!("Expected only type rule. Found: {}", pair) _ => panic!("Expected only type rule. Found: {}", pair),
} }
} }
@ -259,7 +270,7 @@ fn build_extends_list(extends_list_pair: Pair<Rule>) -> Vec<Type> {
fn build_interface(is_extern: bool, is_public: bool, interface_pair: Pair<Rule>) -> Declaration { fn build_interface(is_extern: bool, is_public: bool, interface_pair: Pair<Rule>) -> Declaration {
let mut identifier: Option<Identifier> = None; let mut identifier: Option<Identifier> = None;
let mut generic_parameters: Option<Vec<GenericParameter>> = None; let mut generic_parameters: Option<Vec<GenericParameter>> = None;
let mut extends: Option<Vec<Type>> = None; let mut extends: Option<Vec<TypeUse>> = None;
let mut declarations: Vec<Declaration> = vec![]; let mut declarations: Vec<Declaration> = vec![];
for pair in interface_pair.into_inner() { for pair in interface_pair.into_inner() {
@ -290,36 +301,40 @@ fn build_interface(is_extern: bool, is_public: bool, interface_pair: Pair<Rule>)
type_declaration: TypeDeclaration::new( type_declaration: TypeDeclaration::new(
generic_parameters.unwrap_or(vec![]), generic_parameters.unwrap_or(vec![]),
extends.unwrap_or(vec![]), extends.unwrap_or(vec![]),
declarations declarations,
) ),
} }
} }
fn build_implementation(is_extern: bool, is_public: bool, implementation_pair: Pair<Rule>) -> Declaration { fn build_implementation(
is_extern: bool,
is_public: bool,
implementation_pair: Pair<Rule>,
) -> Declaration {
let mut identifier: Option<Identifier> = None; let mut identifier: Option<Identifier> = None;
let mut generic_parameters: Option<Vec<GenericParameter>> = None; let mut generic_parameters: Option<Vec<GenericParameter>> = None;
let mut impl_ctor: Option<ImplCtor> = None; let mut impl_ctor: Option<ImplCtor> = None;
let mut extends: Option<Vec<Type>> = None; let mut extends: Option<Vec<TypeUse>> = None;
let mut declarations: Vec<Declaration> = vec![]; let mut declarations: Vec<Declaration> = vec![];
for pair in implementation_pair.into_inner() { for pair in implementation_pair.into_inner() {
match pair.as_rule() { match pair.as_rule() {
Rule::identifier => { Rule::identifier => {
identifier = Some(build_identifier(pair)); identifier = Some(build_identifier(pair));
}, }
Rule::generic_parameters_declaration => { Rule::generic_parameters_declaration => {
generic_parameters = Some(build_generic_parameters_declaration(pair)); generic_parameters = Some(build_generic_parameters_declaration(pair));
}, }
Rule::impl_ctor => { Rule::impl_ctor => {
impl_ctor = Some(build_impl_ctor(pair)); impl_ctor = Some(build_impl_ctor(pair));
}, }
Rule::extends_list => { Rule::extends_list => {
extends = Some(build_extends_list(pair)); extends = Some(build_extends_list(pair));
}, }
Rule::declaration => { Rule::declaration => {
declarations.push(build_declaration(pair)); declarations.push(build_declaration(pair));
} }
_ => panic!("Unexpected rule: {}", pair) _ => panic!("Unexpected rule: {}", pair),
} }
} }
@ -330,9 +345,9 @@ fn build_implementation(is_extern: bool, is_public: bool, implementation_pair: P
type_declaration: TypeDeclaration::new( type_declaration: TypeDeclaration::new(
generic_parameters.unwrap_or(vec![]), generic_parameters.unwrap_or(vec![]),
extends.unwrap_or(vec![]), extends.unwrap_or(vec![]),
declarations declarations,
), ),
impl_ctor impl_ctor,
} }
} }
@ -340,10 +355,88 @@ fn build_module(is_extern: bool, is_public: bool, pair: Pair<Rule>) -> Declarati
todo!() todo!()
} }
fn build_function(is_extern: bool, is_public: bool, pair: Pair<Rule>) -> Declaration { fn build_parameter(pair: Pair<Rule>) -> Parameter {
let mut identifier: Option<Identifier> = None;
let mut declared_type: Option<TypeUse> = None;
for pair in pair.into_inner() {
match pair.as_rule() {
Rule::identifier => {
identifier = Some(build_identifier(pair));
}
Rule::type_use => {
declared_type = Some(build_type_use(pair));
}
_ => unreachable!(),
}
}
Parameter {
identifier: identifier.unwrap(),
declared_type,
}
}
fn build_parameters_list(pair: Pair<Rule>) -> Vec<Parameter> {
let mut parameters: Vec<Parameter> = vec![];
for pair in pair.into_inner() {
match pair.as_rule() {
Rule::parameter => {
parameters.push(build_parameter(pair));
}
_ => unreachable!(),
}
}
parameters
}
fn build_function_body(pair: Pair<Rule>) -> Statement {
todo!() todo!()
} }
fn build_function_equals_body(pair: Pair<Rule>) -> Statement {
todo!()
}
fn build_function(is_extern: bool, is_public: bool, pair: Pair<Rule>) -> Declaration {
let mut generic_parameters: Option<Vec<GenericParameter>> = None;
let mut identifier: Option<Identifier> = None;
let mut parameters: Option<Vec<Parameter>> = None;
let mut return_type: Option<TypeUse> = None;
let mut statement: Option<Statement> = None;
for pair in pair.into_inner() {
match pair.as_rule() {
Rule::generic_parameters_declaration => {
generic_parameters = Some(build_generic_parameters_declaration(pair));
}
Rule::identifier => {
identifier = Some(build_identifier(pair));
}
Rule::parameters_list => {
parameters = Some(build_parameters_list(pair));
}
Rule::type_use => {
return_type = Some(build_type_use(pair));
}
Rule::function_body => {
statement = Some(build_function_body(pair));
}
Rule::function_equals_body => {
statement = Some(build_function_equals_body(pair));
}
_ => unreachable!(),
}
}
Declaration::Function {
is_extern,
is_public,
identifier: identifier.unwrap(),
parameters: parameters.unwrap(),
declared_type: return_type,
statement: statement.unwrap(),
}
}
fn build_declaration(declaration_pair: Pair<Rule>) -> Declaration { fn build_declaration(declaration_pair: Pair<Rule>) -> Declaration {
let mut is_extern = false; let mut is_extern = false;
let mut is_public = false; let mut is_public = false;
@ -353,29 +446,32 @@ fn build_declaration(declaration_pair: Pair<Rule>) -> Declaration {
match pair.as_rule() { match pair.as_rule() {
Rule::r#extern => { Rule::r#extern => {
is_extern = true; is_extern = true;
}, }
Rule::r#pub => { Rule::r#pub => {
is_public = true; is_public = true;
} }
Rule::interface => { Rule::interface => {
declaration = Some(build_interface(is_extern, is_public, pair)); declaration = Some(build_interface(is_extern, is_public, pair));
}, }
Rule::implementation => { Rule::implementation => {
declaration = Some(build_implementation(is_extern, is_public, pair)); declaration = Some(build_implementation(is_extern, is_public, pair));
}, }
Rule::module => { Rule::module => {
declaration = Some(build_module(is_extern, is_public, pair)); declaration = Some(build_module(is_extern, is_public, pair));
}, }
Rule::function => { Rule::function => {
declaration = Some(build_function(is_extern, is_public, pair)); declaration = Some(build_function(is_extern, is_public, pair));
}, }
Rule::prop => { Rule::prop => {
declaration = Some(build_prop(pair)); declaration = Some(build_prop(pair));
} }
Rule::field => { Rule::field => {
declaration = Some(build_field(pair)); declaration = Some(build_field(pair));
} }
_ => panic!("Expected only interface, implementation, module, or function rules; found {}", pair) _ => panic!(
"Expected only interface, implementation, module, or function rules; found {}",
pair
),
} }
} }
declaration.expect("Expected declaration.") declaration.expect("Expected declaration.")
@ -383,12 +479,10 @@ fn build_declaration(declaration_pair: Pair<Rule>) -> Declaration {
fn build_identifier(pair: Pair<Rule>) -> Identifier { fn build_identifier(pair: Pair<Rule>) -> Identifier {
match pair.as_rule() { match pair.as_rule() {
Rule::identifier => { Rule::identifier => Identifier {
Identifier { name: String::from(pair.as_str()),
name: String::from(pair.as_str()) },
} _ => panic!("Expected an identifier."),
}
_ => panic!("Expected an identifier.")
} }
} }
@ -399,12 +493,10 @@ fn build_fqn(fqn_pair: Pair<Rule>) -> Fqn {
Rule::identifier => { Rule::identifier => {
identifiers.push(build_identifier(pair)); identifiers.push(build_identifier(pair));
} }
_ => panic!("Expected only identifiers.") _ => panic!("Expected only identifiers."),
} }
} }
Fqn { Fqn { identifiers }
identifiers
}
} }
fn build_compilation_unit(pair: Pair<Rule>) -> CompilationUnit { fn build_compilation_unit(pair: Pair<Rule>) -> CompilationUnit {
@ -421,13 +513,16 @@ fn build_compilation_unit(pair: Pair<Rule>)-> CompilationUnit {
declarations.push(build_declaration(pair)); declarations.push(build_declaration(pair));
} }
Rule::EOI => {} // ignore Rule::EOI => {} // ignore
_ => panic!("Expected only namespace, declaration, or EOI rules, found: {}", pair) _ => panic!(
"Expected only namespace, declaration, or EOI rules, found: {}",
pair
),
} }
} }
CompilationUnit { CompilationUnit {
namespace, namespace,
declarations declarations,
} }
} }
@ -437,10 +532,7 @@ pub fn build_ast(src: &str) -> CompilationUnit {
.next() .next()
.expect("Expcted compilation_unit."); .expect("Expcted compilation_unit.");
match pair.as_rule() { match pair.as_rule() {
Rule::compilation_unit => { Rule::compilation_unit => build_compilation_unit(pair),
build_compilation_unit(pair) _ => panic!("Expected compilation_unit rule."),
},
_ => panic!("Expected compilation_unit rule.")
} }
} }

View File

@ -11,34 +11,34 @@ extern = { "extern" }
pub = { "pub" } pub = { "pub" }
interface = { "int" ~ identifier ~ generic_parameters_declaration? ~ extends_list? ~ ( "{" ~ declaration* ~ "}" )? } interface = { "int" ~ identifier ~ generic_parameters_declaration? ~ extends_list? ~ ( "{" ~ declaration* ~ "}" )? }
extends_list = { ":" ~ type ~ ( "+" ~ type )* } extends_list = { ":" ~ type_use ~ ( "+" ~ type_use )* }
implementation = { "impl" ~ identifier ~ generic_parameters_declaration? ~ impl_ctor? ~ extends_list? ~ ( "{" ~ declaration* ~ "}" )? } implementation = { "impl" ~ identifier ~ generic_parameters_declaration? ~ impl_ctor? ~ extends_list? ~ ( "{" ~ declaration* ~ "}" )? }
impl_ctor = { "(" ~ impl_ctor_args? ~ ")" } impl_ctor = { "(" ~ impl_ctor_args? ~ ")" }
impl_ctor_args = { impl_ctor_arg ~ ( "," ~ impl_ctor_arg )* } impl_ctor_args = { impl_ctor_arg ~ ( "," ~ impl_ctor_arg )* }
impl_ctor_arg = { fld? ~ identifier ~ ( ":" ~ type )? } impl_ctor_arg = { fld? ~ identifier ~ ( ":" ~ type_use )? }
fld = { "fld" } fld = { "fld" }
module = { "mod" ~ identifier ~ "{" ~ declaration* ~ "}" } module = { "mod" ~ identifier ~ "{" ~ declaration* ~ "}" }
property = { identifier ~ ( ":" ~ type )? } property = { identifier ~ ( ":" ~ type_use )? }
function = { "fn" ~ generic_parameters_declaration? ~ identifier ~ "(" ~ args_list? ~ ")" ~ ( ":" ~ type )? ~ ( function_body | function_equals_body ) } function = { "fn" ~ generic_parameters_declaration? ~ identifier ~ "(" ~ parameters_list? ~ ")" ~ ( ":" ~ type_use )? ~ ( function_body | function_equals_body ) }
function_equals_body = { "=" ~ expression } function_equals_body = { "=" ~ expression }
function_body = { "{" ~ statement* ~ "}" } function_body = { "{" ~ statement* ~ "}" }
type = { fqn ~ generic_arguments_declaration? } type_use = { fqn ~ generic_arguments_declaration? }
generic_parameters_declaration = { "<" ~ generic_parameter ~ ( "," ~ generic_parameter )* ~ ">" } generic_parameters_declaration = { "<" ~ generic_parameter ~ ( "," ~ generic_parameter )* ~ ">" }
generic_parameter = { identifier } generic_parameter = { identifier }
generic_arguments_declaration = { "<" ~ generic_argument ~ ( "," ~ generic_argument )* ~ ">" } generic_arguments_declaration = { "<" ~ generic_argument ~ ( "," ~ generic_argument )* ~ ">" }
generic_argument = { fqn } generic_argument = { fqn }
args_list = { arg ~ ( "," ~ arg )* } parameters_list = { parameter ~ ( "," ~ parameter )* }
arg = { identifier ~ ( ":" ~ "..."? ~ type )? } parameter = { identifier ~ ( ":" ~ "..."? ~ type_use )? }
prop = { identifier ~ ":" ~ type } prop = { identifier ~ ":" ~ type_use }
field = { fld ~ identifier ~ ":" ~ type } field = { fld ~ identifier ~ ":" ~ type_use }
statement = { call_stmt } statement = { call_stmt }