From d15ab649bb2abfb835a4dea825a8906c2a3968ac Mon Sep 17 00:00:00 2001 From: Robert-M-Lucas Date: Wed, 5 Jun 2024 08:00:46 +0100 Subject: [PATCH] Improved name evaluation --- .idea/workspace.xml | 28 +- main.why | 2 +- src/root.rs | 2 +- src/root/name_resolver/name_resolvers.rs | 32 +-- src/root/name_resolver/resolve_names.rs | 1 - src/root/parser/mod.rs | 3 +- src/root/parser/parse.rs | 8 +- src/root/parser/parse_arguments.rs | 5 +- src/root/parser/parse_function.rs | 25 +- .../parser/parse_function/parse_assignment.rs | 10 +- .../parser/parse_function/parse_evaluable.rs | 262 ++++++++++++++++-- src/root/parser/parse_function/parse_if.rs | 3 +- .../parse_function/parse_initialisation.rs | 22 +- src/root/parser/parse_function/parse_line.rs | 7 +- .../parser/parse_function/parse_return.rs | 3 +- src/root/parser/parse_function/parse_while.rs | 3 +- src/root/parser/parse_impl.rs | 13 +- src/root/parser/parse_name.rs | 132 +-------- src/root/parser/parse_name_old.rs | 122 ++++++++ src/root/parser/parse_parameters.rs | 23 +- src/root/parser/parse_struct.rs | 18 +- 21 files changed, 470 insertions(+), 254 deletions(-) create mode 100644 src/root/parser/parse_name_old.rs diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f18aa56..5ad7f0c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -8,10 +8,24 @@ + - + + + + + + + + + + + + + + diff --git a/main.why b/main.why index 573de27..3102473 100644 --- a/main.why +++ b/main.why @@ -3,6 +3,6 @@ struct A { b: int } -fn main() -> intt { +fn main() !-> int { return 12; } diff --git a/src/root.rs b/src/root.rs index 5e6451d..02f8cf4 100644 --- a/src/root.rs +++ b/src/root.rs @@ -70,7 +70,7 @@ pub fn main_args(args: Args) -> Result<(), WError> { print!("Parsing... "); time!( - let parsed = parse(PathBuf::from(&args.input)).unwrap(); + let parsed = parse(PathBuf::from(&args.input))?; ); print!("Resolving Names... "); diff --git a/src/root/name_resolver/name_resolvers.rs b/src/root/name_resolver/name_resolvers.rs index 6c9578f..3926323 100644 --- a/src/root/name_resolver/name_resolvers.rs +++ b/src/root/name_resolver/name_resolvers.rs @@ -10,7 +10,6 @@ use crate::root::name_resolver::resolve_function_signatures::FunctionSignature; use crate::root::parser::parse::Location; use crate::root::shared::types::Type; use crate::root::parser::parse_function::FunctionToken; -use crate::root::parser::parse_name::UnresolvedNameToken; use crate::root::parser::parse_struct::StructToken; use crate::root::shared::common::{AddressedTypeRef, FunctionID, TypeID, TypeRef}; @@ -168,31 +167,14 @@ impl GlobalDefinitionTable { } pub fn add_type(&mut self, given_id: TypeID, definition: Box) { - // TODO: handle collisions self.type_definitions.insert(given_id, definition); } - pub fn resolve_local_name(&self, name: &UnresolvedNameToken, local_variable_table: &LocalVariableTable) -> NameResult { - let temp_name = &name.names()[0].1; - - if let Some((a, t)) = - local_variable_table.get_ref_and_type( - temp_name, &self.type_definitions - ) { - return NameResult::Variable(a, t); - } + pub fn resolve_name(&self, name: &UnresolvedNameToken, local_variable_table: &LocalVariableTable) -> NameResult { todo!() } - pub fn resolve_local_name_allow_function_calls(&self, name: &UnresolvedNameToken, local_variable_table: &mut LocalVariableTable) -> NameResult { - let temp_name = &name.names()[0].1; - - if let Some((a, t)) = - local_variable_table.get_ref_and_type( - temp_name, &self.type_definitions - ) { - return NameResult::Variable(a, t); - } + pub fn resolve_global_name(&self, name: &UnresolvedNameToken) -> NameResult { todo!() } @@ -225,16 +207,6 @@ impl GlobalDefinitionTable { return Err(WError::n(NRErrors::FunctionSubname(next.clone(), method_name.clone()), location.clone())); } - // match connector { - // NameConnectors::NonStatic => { - // if !*function_signatures.get(function).unwrap().has_self() { - // - // return Err(()); - // } - // } - // NameConnectors::Static => {} - // } - NameResultId::Function(*function) } else { diff --git a/src/root/name_resolver/resolve_names.rs b/src/root/name_resolver/resolve_names.rs index 4620f94..63dde37 100644 --- a/src/root/name_resolver/resolve_names.rs +++ b/src/root/name_resolver/resolve_names.rs @@ -11,7 +11,6 @@ use crate::root::name_resolver::resolve_type_sizes::{resolve_type_sizes, Unsized use crate::root::parser::parse::Location; use crate::root::parser::parse_function::FunctionToken; use crate::root::parser::parse_function::parse_literal::LiteralToken; -use crate::root::parser::parse_name::UnresolvedNameToken; use crate::root::parser::parse_toplevel::TopLevelTokens; use crate::root::shared::common::{LocalAddress, TypeRef}; use crate::root::shared::common::{ByteSize, FunctionID, TypeID}; diff --git a/src/root/parser/mod.rs b/src/root/parser/mod.rs index 8a279ae..741a2e0 100644 --- a/src/root/parser/mod.rs +++ b/src/root/parser/mod.rs @@ -4,9 +4,10 @@ pub mod parse_blocks; pub mod parse_comments; pub mod parse_function; pub mod parse_impl; -pub mod parse_name; +pub mod parse_name_old; pub mod parse_parameters; pub mod parse_struct; pub mod parse_toplevel; pub mod parse_util; pub mod soft_alt; +mod parse_name; diff --git a/src/root/parser/parse.rs b/src/root/parser/parse.rs index 9f6c422..56f88ed 100644 --- a/src/root/parser/parse.rs +++ b/src/root/parser/parse.rs @@ -9,6 +9,7 @@ use std::path::PathBuf; use std::rc::Rc; use color_print::cformat; use derive_getters::Getters; +use crate::root::errors::WError; use crate::root::parser::parse_toplevel::TopLevelTokens; pub type Span<'a> = LocatedSpan<&'a str, &'a Rc>; @@ -121,12 +122,15 @@ impl Display for Location { } } -pub fn parse(path: PathBuf) -> Result, ()> { +pub fn parse(path: PathBuf) -> Result, WError> { let text = fs::read_to_string(&path).unwrap(); let path = Rc::new(path); let base = Span::new_extra(&text, &path); - let (remaining, output) = parse_toplevel::parse_toplevel(base).map_err(|_| ())?; + let (remaining, output) = match parse_toplevel::parse_toplevel(base) { + Ok(v) => v, + Err(_) => todo!() + }; debug_assert!(remaining.is_empty()); Ok(output) diff --git a/src/root/parser/parse_arguments.rs b/src/root/parser/parse_arguments.rs index 50c1a5a..a2699c9 100644 --- a/src/root/parser/parse_arguments.rs +++ b/src/root/parser/parse_arguments.rs @@ -2,8 +2,9 @@ use nom::bytes::complete::{take_until, take_until1}; use nom::InputTake; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable}; +use crate::root::parser::parse_name::SimpleNameToken; -pub fn parse_arguments<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>) -> ParseResult<'a, Span<'a>, Vec> { +pub fn parse_arguments<'a, 'b>(s: Span<'a>, containing_class: Option<&SimpleNameToken>) -> ParseResult<'a, (), Vec> { let mut s = s; let mut args = Vec::new(); let mut last = false; @@ -25,5 +26,5 @@ pub fn parse_arguments<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>) - } } - Ok((s, args)) + Ok(((), args)) } \ No newline at end of file diff --git a/src/root/parser/parse_function.rs b/src/root/parser/parse_function.rs index fe885fe..33cb8ac 100644 --- a/src/root/parser/parse_function.rs +++ b/src/root/parser/parse_function.rs @@ -5,8 +5,9 @@ use nom_supreme::tag::complete::tag; use substring::Substring; use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; use crate::root::parser::parse_blocks::default_section; +use crate::root::parser::parse_function::parse_evaluable::{FullNameWithIndirectionToken, parse_full_name}; use crate::root::parser::parse_function::parse_line::{LineTokens, parse_lines}; -use crate::root::parser::parse_name::{parse_full_name, parse_simple_name, UnresolvedNameToken}; +use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; use crate::root::parser::parse_parameters::{Parameters, parse_parameters}; use crate::root::parser::parse_toplevel::{ToplevelTestFn, TopLevelTokens}; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; @@ -26,8 +27,8 @@ pub mod parse_while; #[derive(Debug, Getters, Dissolve)] pub struct FunctionToken { location: Location, - name: String, - return_type: Option<(UnresolvedNameToken, Location)>, + name: SimpleNameToken, + return_type: Option, parameters: Parameters, lines: Vec, } @@ -41,16 +42,16 @@ pub fn test_parse_function<'a>(s: Span<'a>) -> ParseResult) -> ParseResult { +pub fn parse_function<'a, 'b>(s: Span<'a>, allow_self: Option<&'b SimpleNameToken>) -> ParseResult<'a, Span<'a>, FunctionToken> { let location = Location::from_span(&s); let (s, _) = tag("fn").parse(s)?; let (s, _) = require_ignored(s)?; let (s, name) = parse_simple_name(s)?; let (s, _) = discard_ignored(s)?; - let c_owned = allow_self.as_ref().and_then(|s| Some(s.base().to_string())); - let containing_class = if let Some(s) = &c_owned { - Some(s.as_str()) - } else { None }; + // let c_owned = allow_self.as_ref().and_then(|s| Some(s.base().to_string())); + // let containing_class = if let Some(s) = &c_owned { + // Some(s.as_str()) + // } else { None }; let (s, contents) = default_section(s, '(')?; let (_, parameters) = parse_parameters(contents, allow_self)?; @@ -60,21 +61,21 @@ pub fn parse_function(s: Span, allow_self: Option) -> Parse let (s, return_type) = if let Ok((s, _)) = tag::<_, _, ErrorTree>("->")(s) { let (s, _) = discard_ignored(s)?; let location = Location::from_span(&s); - let (s, return_type) = parse_full_name(s, containing_class.and_then(|s| Some(s.to_string())))?; - (discard_ignored(s)?.0, Some((return_type, location))) + let (s, return_type) = parse_full_name(s, allow_self)?; + (discard_ignored(s)?.0, Some(return_type)) } else { (s, None) }; let (s, contents) = default_section(s, '{')?; - let (_, lines) = parse_lines(contents, containing_class)?; + let (_, lines) = parse_lines(contents, allow_self)?; Ok(( s, FunctionToken { location, - name: name.to_string(), + name, return_type, parameters, lines, diff --git a/src/root/parser/parse_function/parse_assignment.rs b/src/root/parser/parse_function/parse_assignment.rs index ec1ef7c..36ac626 100644 --- a/src/root/parser/parse_function/parse_assignment.rs +++ b/src/root/parser/parse_function/parse_assignment.rs @@ -3,16 +3,16 @@ use nom::sequence::Tuple; use nom_supreme::tag::complete::tag; use crate::root::parser::parse::{Location, ParseResult, Span}; use crate::root::parser::parse_function::parse_assigner::{AssignmentOperatorToken, parse_assigner}; -use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable}; +use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, FullNameWithIndirectionToken, parse_evaluable, parse_full_name}; use crate::root::parser::parse_function::parse_initialisation::parse_initialisation; use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens}; -use crate::root::parser::parse_name::{UnresolvedNameToken, parse_full_name}; +use crate::root::parser::parse_name::SimpleNameToken; use crate::root::parser::parse_util::discard_ignored; #[derive(Debug)] pub struct AssignmentToken { location: Location, - name: UnresolvedNameToken, + name: FullNameWithIndirectionToken, assignment_operator: AssignmentOperatorToken, value: EvaluableToken, } @@ -27,10 +27,10 @@ pub fn test_parse_assignment<'a, 'b>(s: Span<'a>) -> ParseResult<'a, Span<'a>, L Ok((s, |x, c| parse_assignment(x, c).map(|(s, x)| (s, LineTokens::Assignment(x))))) } -pub fn parse_assignment<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>) -> ParseResult<'a, Span<'a>, AssignmentToken> { +pub fn parse_assignment<'a, 'b>(s: Span<'a>, containing_class: Option<&SimpleNameToken>) -> ParseResult<'a, Span<'a>, AssignmentToken> { let (s, _) = discard_ignored(s)?; let location = Location::from_span(&s); - let (s, n) = parse_full_name(s, containing_class.and_then(|s| Some(s.to_string())))?; + let (s, n) = parse_full_name(s, containing_class)?; let (s, _) = discard_ignored(s)?; let (s, a) = parse_assigner(s)?; let (s, _) = discard_ignored(s)?; diff --git a/src/root/parser/parse_function/parse_evaluable.rs b/src/root/parser/parse_function/parse_evaluable.rs index 104e663..3b6ecc0 100644 --- a/src/root/parser/parse_function/parse_evaluable.rs +++ b/src/root/parser/parse_function/parse_evaluable.rs @@ -1,15 +1,19 @@ +use nom::Err; use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; use crate::root::parser::parse_function::parse_literal::{ - parse_literal, LiteralToken, LiteralTokens, + LiteralToken, parse_literal, }; -use crate::root::parser::parse_function::parse_operator::{parse_operator, OperatorToken}; -use crate::root::parser::parse_name::{parse_full_name, UnresolvedNameToken}; +use crate::root::parser::parse_function::parse_operator::{OperatorToken, parse_operator}; use b_box::b; use derive_getters::Getters; use nom::branch::alt; -use nom::character::complete::{char}; +use nom::bytes::complete::tag; +use nom::character::complete::char; +use crate::root::parser::parse_arguments::parse_arguments; +use crate::root::parser::parse_name::{SimpleNameToken, parse_simple_name}; use crate::root::parser::parse_blocks::default_section; use crate::root::parser::parse_util::discard_ignored; +use crate::root::shared::common::Indirection; #[derive(Debug, Getters)] pub struct EvaluableToken { @@ -17,8 +21,8 @@ pub struct EvaluableToken { token: EvaluableTokens, } -pub fn temp_from_token(s: Span, token: EvaluableTokens) -> TempEvaluableTokens { - TempEvaluableTokens::EvaluableToken(EvaluableToken { +pub fn temp_from_token(s: Span, token: EvaluableTokens) -> TempEvaluableTokensOne { + TempEvaluableTokensOne::EvaluableToken(EvaluableToken { location: Location::from_span(&s), token, }) @@ -26,27 +30,149 @@ pub fn temp_from_token(s: Span, token: EvaluableTokens) -> TempEvaluableTokens { #[derive(Debug)] pub enum EvaluableTokens { - Name(UnresolvedNameToken), + Name(SimpleNameToken, Option), + StaticAccess(Box, SimpleNameToken), + DynamicAccess(Box, SimpleNameToken), + FunctionCall(Box, SimpleNameToken, Vec), Literal(LiteralToken), InfixOperator(Box, OperatorToken, Box), PrefixOperator(OperatorToken, Box), } +#[derive(Debug, Getters)] +pub struct FullNameWithIndirectionToken { + indirection: Indirection, + inner: FullNameToken +} + +impl FullNameWithIndirectionToken { + pub fn from_simple(simple: SimpleNameToken, containing_class: Option, location: Location) -> FullNameWithIndirectionToken { + FullNameWithIndirectionToken { + indirection: Indirection(0), + inner: FullNameToken { + location, + token: FullNameTokens::Name(simple, containing_class) + } + } + } +} + +#[derive(Debug, Getters)] +pub struct FullNameToken { + location: Location, + token: FullNameTokens +} + +impl FullNameToken { + pub fn new(s: &Span, token: FullNameTokens) -> FullNameToken { + FullNameToken { + location: Location::from_span(s), + token, + } + } + + fn to_evaluable(self) -> EvaluableToken { + let (location, token) = (self.location, self.token); + let token = token.to_evaluable_token(); + EvaluableToken { + location, + token, + } + } +} + #[derive(Debug)] -enum TempEvaluableTokens { +pub enum FullNameTokens { + Name(SimpleNameToken, Option), + StaticAccess(Box, SimpleNameToken), + DynamicAccess(Box, SimpleNameToken) +} + +impl FullNameTokens { + pub fn to_evaluable_token(self) -> EvaluableTokens { + match self { + FullNameTokens::Name(n, c) => EvaluableTokens::Name(n, c), + FullNameTokens::StaticAccess(e, n) => EvaluableTokens::StaticAccess(b!(e.to_evaluable()), n), + FullNameTokens::DynamicAccess(e, n) => EvaluableTokens::DynamicAccess(b!(e.to_evaluable()), n), + } + } +} + +#[derive(Debug)] +enum TempEvaluableTokensOne { + EvaluableToken(EvaluableToken), + Operator(OperatorToken), + StaticAccess(SimpleNameToken), + DynamicAccess(SimpleNameToken), + FunctionCall(SimpleNameToken, Vec), +} + +#[derive(Debug)] +enum TempEvaluableTokensTwo { EvaluableToken(EvaluableToken), Operator(OperatorToken), } -pub fn parse_evaluable<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>, semicolon_terminated: bool) -> ParseResult<'a, Span<'a>, EvaluableToken> { +pub fn parse_full_name<'a, 'b>(s: Span<'a>, containing_class: Option<&SimpleNameToken>) -> ParseResult<'a, Span<'a>, FullNameWithIndirectionToken> { + let mut indirection: usize = 0; + let mut s = s; + loop { + let (ns, _) = discard_ignored(s)?; + + if let Ok((ns, _)) = char::('&')(ns) { + indirection += 1; + s = ns; + } + else { + s = ns; + break; + } + } + + let (s, _) = discard_ignored(s)?; + let (s, section) = parse_simple_name(s)?; + + let mut current = FullNameToken { + location: section.location().clone(), + token: FullNameTokens::Name(section, containing_class.and_then(|x| Some(x.clone()))) + }; + + let mut s = s; + + loop { + let (ns, _) = discard_ignored(s)?; + + if let Ok((ns, _)) = tag::<&str, Span, ErrorTree>("::")(ns) { + let (ns, section) = parse_simple_name(ns)?; + current = FullNameToken { + location: section.location().clone(), + token: FullNameTokens::StaticAccess(b!(current), section), + }; + s = ns; + } + if let Ok((ns, _)) = char::('.')(ns) { + let (ns, section) = parse_simple_name(ns)?; + current = FullNameToken { + location: section.location().clone(), + token: FullNameTokens::DynamicAccess(b!(current), section), + }; + s = ns; + } + return Ok((s, FullNameWithIndirectionToken { indirection: Indirection(indirection), inner: current })) + } +} + +pub fn parse_evaluable<'a, 'b>(s: Span<'a>, containing_class: Option<&SimpleNameToken>, semicolon_terminated: bool) -> ParseResult<'a, Span<'a>, EvaluableToken> { assert!(!s.is_empty()); let mut s = s; let mut evaluables = Vec::new(); + // Collect evaluable sections into initial vec loop { let (ns, _) = discard_ignored(s)?; + // Terminate on semicolon if semicolon terminated if semicolon_terminated { if let Ok((ns, _)) = char::<_, ErrorTree>(';')(ns) { s = ns; @@ -54,8 +180,8 @@ pub fn parse_evaluable<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>, s } } + // Fail if semicolon terminated but reach end of span if ns.is_empty() { - // Fail if semicolon required but ns is empty if semicolon_terminated { // ! Intentional failure char(';')(ns)?; @@ -66,20 +192,49 @@ pub fn parse_evaluable<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>, s break; } + // Recursively parse bracketed sections let ns = if let Ok((ns, _)) = default_section(s, '(') { let (ns, evaluable) = parse_evaluable(ns, containing_class.clone(), false)?; - evaluables.push(TempEvaluableTokens::EvaluableToken(evaluable)); + evaluables.push(TempEvaluableTokensOne::EvaluableToken(evaluable)); ns - } else { + } + // Parse evaluable + else { let (ns, token) = alt(( |x| { parse_literal(x) .map(|(s, t)| (s, temp_from_token(s, EvaluableTokens::Literal(t)))) }, - |x| parse_operator(x).map(|(s, t)| (s, TempEvaluableTokens::Operator(t))), + |x| parse_operator(x).map(|(s, t)| (s, TempEvaluableTokensOne::Operator(t))), |x| { - parse_full_name(x, containing_class.and_then(|s| Some(s.to_string()))) - .map(|(s, t)| (s, temp_from_token(s, EvaluableTokens::Name(t)))) + enum Kind { + Static, + Dynamic, + None + } + + let (x, kind) = tag::<&str, Span, ErrorTree>("::")(x).map(|(a, _)| (a, Kind::Static)) + .map_err(|_| + char::('.')(x).map(|a| (a, Kind::Dynamic))) + .unwrap_or((x, Kind::None)); + + let (x, section) = parse_simple_name(x)?; + + Ok(if char::('(')(x).is_ok() { + let (x, arguments) = default_section(x, '(')?; + let (_, arguments) = parse_arguments(arguments, containing_class)?; + (x, TempEvaluableTokensOne::FunctionCall(section, arguments)) + } + else { + match kind { + Kind::Static => (x, TempEvaluableTokensOne::StaticAccess(section)), + Kind::Dynamic => (x, TempEvaluableTokensOne::DynamicAccess(section)), + Kind::None => (x, TempEvaluableTokensOne::EvaluableToken(EvaluableToken { + location: section.location().clone(), + token: EvaluableTokens::Name(section, containing_class.and_then(|x| Some(x.clone()))), + })), + } + }) }, ))(ns)?; evaluables.push(token); @@ -89,6 +244,53 @@ pub fn parse_evaluable<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>, s s = ns; } + let mut new_evaluables: Vec = Vec::new(); + + for eval in evaluables { + match eval { + TempEvaluableTokensOne::StaticAccess(n) => { + match new_evaluables.pop() { + Some(TempEvaluableTokensTwo::Operator(_)) => todo!(), // Can't be operator + Some(TempEvaluableTokensTwo::EvaluableToken(e)) => { + new_evaluables.push(TempEvaluableTokensTwo::EvaluableToken(EvaluableToken { + location: e.location.clone(), + token: EvaluableTokens::StaticAccess(b!(e), n), + })) + }, + None => todo!(), // Must have previous + } + } + TempEvaluableTokensOne::DynamicAccess(n) => { + match new_evaluables.pop() { + Some(TempEvaluableTokensTwo::Operator(_)) => todo!(), // Can't be operator + Some(TempEvaluableTokensTwo::EvaluableToken(e)) => { + new_evaluables.push(TempEvaluableTokensTwo::EvaluableToken(EvaluableToken { + location: e.location.clone(), + token: EvaluableTokens::DynamicAccess(b!(e), n), + })) + }, + None => todo!(), // Must have previous + } + } + TempEvaluableTokensOne::FunctionCall(n, a) => { + match new_evaluables.pop() { + Some(TempEvaluableTokensTwo::Operator(_)) => todo!(), // Can't be operator + Some(TempEvaluableTokensTwo::EvaluableToken(e)) => { + new_evaluables.push(TempEvaluableTokensTwo::EvaluableToken(EvaluableToken { + location: e.location.clone(), + token: EvaluableTokens::FunctionCall(b!(e), n, a), + })) + }, + None => todo!(), // Must have previous + } + } + TempEvaluableTokensOne::EvaluableToken(e) => new_evaluables.push(TempEvaluableTokensTwo::EvaluableToken(e)), + TempEvaluableTokensOne::Operator(o) => new_evaluables.push(TempEvaluableTokensTwo::Operator(o)), + }; + } + + let evaluables = new_evaluables; + #[derive(Debug)] enum TempOperation { Infix(Box, OperatorToken, Box), @@ -97,9 +299,9 @@ pub fn parse_evaluable<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>, s } fn parse_prefix( - section: &[(usize, TempEvaluableTokens)], - ) -> ParseResult<&[(usize, TempEvaluableTokens)], TempOperation> { - let TempEvaluableTokens::Operator(operator) = §ion[0].1 else { + section: &[(usize, TempEvaluableTokensTwo)], + ) -> ParseResult<&[(usize, TempEvaluableTokensTwo)], TempOperation> { + let TempEvaluableTokensTwo::Operator(operator) = §ion[0].1 else { panic!() }; @@ -108,10 +310,10 @@ pub fn parse_evaluable<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>, s } let (remaining, operand) = match §ion[1] { - (p, TempEvaluableTokens::EvaluableToken(_)) => { + (p, TempEvaluableTokensTwo::EvaluableToken(_)) => { (§ion[2..], TempOperation::Value(*p)) } - (_, TempEvaluableTokens::Operator(_)) => parse_prefix(§ion[1..])?, + (_, TempEvaluableTokensTwo::Operator(_)) => parse_prefix(§ion[1..])?, }; Ok(( @@ -120,25 +322,25 @@ pub fn parse_evaluable<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>, s )) } - let enumerated: Vec<(usize, TempEvaluableTokens)> = + let enumerated: Vec<(usize, TempEvaluableTokensTwo)> = evaluables.into_iter().enumerate().collect(); let mut base = None; let mut after = Vec::new(); - let mut enumerated_slice: &[(usize, TempEvaluableTokens)] = &enumerated; + let mut enumerated_slice: &[(usize, TempEvaluableTokensTwo)] = &enumerated; let mut operator_priority = Vec::new(); while enumerated_slice.len() > 0 { let operator = if base.is_some() { match &enumerated_slice[0] { - (_, TempEvaluableTokens::Operator(op)) => { + (_, TempEvaluableTokensTwo::Operator(op)) => { operator_priority.push(op.get_priority_t()); enumerated_slice = &enumerated_slice[1..]; Some(op.clone()) } - (_, TempEvaluableTokens::EvaluableToken(_)) => { + (_, TempEvaluableTokensTwo::EvaluableToken(_)) => { // ? Expected infix connecting operator todo!() } @@ -148,11 +350,11 @@ pub fn parse_evaluable<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>, s }; let value = match &enumerated_slice[0] { - (p, TempEvaluableTokens::EvaluableToken(_)) => { + (p, TempEvaluableTokensTwo::EvaluableToken(_)) => { enumerated_slice = &enumerated_slice[1..]; TempOperation::Value(*p) } - (_, TempEvaluableTokens::Operator(_)) => { + (_, TempEvaluableTokensTwo::Operator(_)) => { let (new_slice, value) = match parse_prefix(enumerated_slice) { Ok(r) => r, Err(_) => { @@ -198,12 +400,12 @@ pub fn parse_evaluable<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>, s fn recursively_convert_temp( base: TempOperation, - evaluables: &mut Vec>, + evaluables: &mut Vec>, ) -> EvaluableToken { - fn not_operator(te: TempEvaluableTokens) -> EvaluableToken { + fn not_operator(te: TempEvaluableTokensTwo) -> EvaluableToken { match te { - TempEvaluableTokens::EvaluableToken(e) => e, - TempEvaluableTokens::Operator(_) => { + TempEvaluableTokensTwo::EvaluableToken(e) => e, + TempEvaluableTokensTwo::Operator(_) => { panic!() } } diff --git a/src/root/parser/parse_function/parse_if.rs b/src/root/parser/parse_function/parse_if.rs index 0931087..14c919b 100644 --- a/src/root/parser/parse_function/parse_if.rs +++ b/src/root/parser/parse_function/parse_if.rs @@ -6,6 +6,7 @@ use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; use crate::root::parser::parse_blocks::default_section; use crate::root::parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; use crate::root::parser::parse_function::parse_line::{parse_lines, LineTestFn, LineTokens}; +use crate::root::parser::parse_name::SimpleNameToken; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; #[derive(Debug)] @@ -24,7 +25,7 @@ pub fn test_parse_if<'a, 'b>(s: Span<'a>) -> ParseResult(s: Span<'a>, containing_class: Option<&'b str>) -> ParseResult<'a, Span<'a>, IfToken> { +pub fn parse_if<'a, 'b>(s: Span<'a>, containing_class: Option<&'b SimpleNameToken>) -> ParseResult<'a, Span<'a>, IfToken> { let (s, l) = tag("if")(s)?; let (s, _) = discard_ignored(s)?; let (s, content) = default_section(s, '(')?; diff --git a/src/root/parser/parse_function/parse_initialisation.rs b/src/root/parser/parse_function/parse_initialisation.rs index d915b3d..258bb6d 100644 --- a/src/root/parser/parse_function/parse_initialisation.rs +++ b/src/root/parser/parse_function/parse_initialisation.rs @@ -1,21 +1,17 @@ -use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; -use crate::root::parser::parse_function::parse_break::parse_break; -use crate::root::parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; +use crate::root::parser::parse::{Location, ParseResult, Span}; +use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, FullNameWithIndirectionToken, parse_evaluable, parse_full_name}; use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens}; -use crate::root::parser::parse_name::{parse_full_name, parse_simple_name, UnresolvedNameToken}; -use nom::character::complete::{char}; +use nom::character::complete::char; use nom::sequence::Tuple; -use nom::Err::Error; -use nom_supreme::error::{BaseErrorKind, Expectation}; use nom_supreme::tag::complete::tag; -use substring::Substring; +use crate::root::parser::parse_name::{SimpleNameToken, parse_simple_name}; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; #[derive(Debug)] pub struct InitialisationToken { location: Location, - name: String, - type_name: UnresolvedNameToken, + name: SimpleNameToken, + type_name: FullNameWithIndirectionToken, value: EvaluableToken, } @@ -28,14 +24,14 @@ pub fn test_parse_initialisation<'a, 'b>(s: Span<'a>) -> ParseResult(s: Span<'a>, containing_class: Option<&'b str>) -> ParseResult<'a, Span<'a>, InitialisationToken> { +pub fn parse_initialisation<'a, 'b>(s: Span<'a>, containing_class: Option<&'b SimpleNameToken>) -> ParseResult<'a, Span<'a>, InitialisationToken> { let (s, l) = tag("let")(s)?; let (s, _) = require_ignored(s)?; let (s, name) = parse_simple_name(s)?; let (s, _) = discard_ignored(s)?; let (s, _) = char(':')(s)?; let (s, _) = discard_ignored(s)?; - let (s, type_name) = parse_full_name(s, containing_class.and_then(|s| Some(s.to_string())))?; + let (s, type_name) = parse_full_name(s, containing_class)?; let (s, _) = discard_ignored(s)?; let (s, _) = char('=')(s)?; let (s, _) = discard_ignored(s)?; @@ -45,7 +41,7 @@ pub fn parse_initialisation<'a, 'b>(s: Span<'a>, containing_class: Option<&'b st s, InitialisationToken { location: Location::from_span(&l), - name: name.to_string(), + name, type_name, value, }, diff --git a/src/root/parser/parse_function/parse_line.rs b/src/root/parser/parse_function/parse_line.rs index 62fdc58..f33a40b 100644 --- a/src/root/parser/parse_function/parse_line.rs +++ b/src/root/parser/parse_function/parse_line.rs @@ -11,6 +11,7 @@ use crate::root::parser::parse_function::parse_return::{test_parse_return, Retur use crate::root::parser::parse_function::parse_while::{test_parse_while, WhileToken}; use nom::branch::alt; use nom::Parser; +use crate::root::parser::parse_name::SimpleNameToken; use crate::root::parser::parse_util::discard_ignored; #[derive(Debug)] @@ -25,9 +26,9 @@ pub enum LineTokens { } /// fn(line span, Option) -pub type LineTestFn<'a, 'b> = fn(Span<'a>, Option<&'b str>) -> ParseResult<'a, Span<'a>, LineTokens>; +pub type LineTestFn<'a, 'b> = fn(Span<'a>, Option<&'b SimpleNameToken>) -> ParseResult<'a, Span<'a>, LineTokens>; -pub fn parse_lines<'a, 'b>(contents: Span<'a>, containing_class: Option<&'b str>) -> ParseResult<'a, (), Vec> { +pub fn parse_lines<'a, 'b>(contents: Span<'a>, containing_class: Option<&'b SimpleNameToken>) -> ParseResult<'a, (), Vec> { let mut lines = Vec::new(); let mut c = contents; @@ -46,7 +47,7 @@ pub fn parse_lines<'a, 'b>(contents: Span<'a>, containing_class: Option<&'b str> Ok(((), lines)) } -pub fn parse_line<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>) -> ParseResult<'a, Span<'a>, LineTokens> { +pub fn parse_line<'a, 'b>(s: Span<'a>, containing_class: Option<&'b SimpleNameToken>) -> ParseResult<'a, Span<'a>, LineTokens> { if let Ok((_, parser)) = alt(( test_parse_break, test_parse_return, diff --git a/src/root/parser/parse_function/parse_return.rs b/src/root/parser/parse_function/parse_return.rs index ddbcb9d..6d7a708 100644 --- a/src/root/parser/parse_function/parse_return.rs +++ b/src/root/parser/parse_function/parse_return.rs @@ -5,6 +5,7 @@ use nom_supreme::tag::complete::tag; use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; use crate::root::parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens}; +use crate::root::parser::parse_name::SimpleNameToken; use crate::root::parser::parse_util::require_ignored; #[derive(Debug, Getters)] @@ -22,7 +23,7 @@ pub fn test_parse_return<'a, 'b>(s: Span<'a>) -> ParseResult(s: Span<'a>, containing_class: Option<&'b str>) -> ParseResult<'a, Span<'a>, ReturnToken> { +pub fn parse_return<'a, 'b>(s: Span<'a>, containing_class: Option<&'b SimpleNameToken>) -> ParseResult<'a, Span<'a>, ReturnToken> { let (s, l) = tag("return")(s)?; let (s, _) = require_ignored(s)?; let (s, value) = parse_evaluable(s, containing_class, true)?; diff --git a/src/root/parser/parse_function/parse_while.rs b/src/root/parser/parse_function/parse_while.rs index c0cd96c..706ab3c 100644 --- a/src/root/parser/parse_function/parse_while.rs +++ b/src/root/parser/parse_function/parse_while.rs @@ -7,6 +7,7 @@ use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; use crate::root::parser::parse_blocks::default_section; use crate::root::parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; use crate::root::parser::parse_function::parse_line::{parse_lines, LineTestFn, LineTokens}; +use crate::root::parser::parse_name::SimpleNameToken; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; #[derive(Debug)] @@ -25,7 +26,7 @@ pub fn test_parse_while<'a, 'b>(s: Span<'a>) -> ParseResult(s: Span<'a>, containing_class: Option<&'b str>) -> ParseResult<'a, Span<'a>, WhileToken> { +pub fn parse_while<'a, 'b>(s: Span<'a>, containing_class: Option<&'b SimpleNameToken>) -> ParseResult<'a, Span<'a>, WhileToken> { let (s, l) = tag("while")(s)?; let (s, _) = discard_ignored(s)?; let (s, content) = default_section(s, '(')?; diff --git a/src/root/parser/parse_impl.rs b/src/root/parser/parse_impl.rs index 3800193..bc7aa78 100644 --- a/src/root/parser/parse_impl.rs +++ b/src/root/parser/parse_impl.rs @@ -5,15 +5,15 @@ use nom_supreme::tag::complete::tag; use crate::root::parser::parse::{Location, ParseResult, Span}; use crate::root::parser::parse_blocks::default_section; -use crate::root::parser::parse_function::{parse_function, FunctionToken}; -use crate::root::parser::parse_name::{UnresolvedNameToken, parse_simple_name}; -use crate::root::parser::parse_toplevel::{TopLevelTokens, ToplevelTestFn}; +use crate::root::parser::parse_function::{FunctionToken, parse_function}; +use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; +use crate::root::parser::parse_toplevel::{ToplevelTestFn, TopLevelTokens}; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; #[derive(Debug, Getters, Dissolve)] pub struct ImplToken { location: Location, - name: String, + name: SimpleNameToken, functions: Vec, } @@ -47,8 +47,7 @@ pub fn parse_impl(s: Span) -> ParseResult { let (cs, function) = parse_function( cs, // ? Pass class name (type) to function in case needed for self - // ? Pass containing class as null as class does not contain itself - Some(UnresolvedNameToken::new_unresolved(&name, None)) + Some(&name) )?; functions.push(function); @@ -59,7 +58,7 @@ pub fn parse_impl(s: Span) -> ParseResult { s, ImplToken { location, - name: name.to_string(), + name, functions, }, )) diff --git a/src/root/parser/parse_name.rs b/src/root/parser/parse_name.rs index 24500b4..ae3bee2 100644 --- a/src/root/parser/parse_name.rs +++ b/src/root/parser/parse_name.rs @@ -1,131 +1,25 @@ -use std::path::PathBuf; -use std::rc::Rc; -use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; -use crate::root::parser::parse_function::parse_evaluable::EvaluableToken; -use derive_getters::{Dissolve, Getters}; use nom::bytes::complete::take_till; use nom::Err::Error; -use nom::InputTake; use nom_supreme::error::{BaseErrorKind, Expectation}; -use crate::root::parser::parse_arguments::parse_arguments; -use crate::root::parser::parse_blocks::{default_section, section}; -use crate::root::parser::parse_util::discard_ignored; -use crate::root::shared::common::Indirection; - -#[derive(Debug)] -pub enum NameConnectors { - NonStatic, - Static, -} +use derive_getters::Getters; +use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; -#[derive(Debug, Dissolve, Getters)] -pub struct UnresolvedNameToken { +#[derive(Debug, Getters, Clone)] +pub struct SimpleNameToken { location: Location, - containing_class: Option, - indirection: Indirection, - base: String, - names: Vec<(NameConnectors, String)>, - function_call: Option>, + name: String } -impl UnresolvedNameToken { - pub fn new_unresolved( - s: &Span, - containing_class: Option, - ) -> UnresolvedNameToken { - let location = Location::from_span(s); - let file_location = location.path().clone(); - UnresolvedNameToken { - location, - containing_class, - indirection: Indirection(0), - base: s.to_string(), - names: Vec::new(), - function_call: None - } - } - - pub fn new_unresolved_top( - s: String, - location: Location - ) -> UnresolvedNameToken { - UnresolvedNameToken { - location, - containing_class: None, - indirection: Indirection(0), - base: s, - names: Vec::new(), - function_call: None +impl SimpleNameToken { + pub fn new(s: Span) -> SimpleNameToken { + SimpleNameToken { + location: Location::from_span(&s), + name: s.to_string() } } } -pub fn parse_full_name(s: Span, containing_class: Option) -> ParseResult { - // TODO: Handle indirection - - let (s, _) = discard_ignored(s)?; - - let location = Location::from_span(&s); - - let (mut s, base_name) = parse_simple_name(s)?; - - let mut names = Vec::new(); - let mut function_call = None; - - if let Ok((ns, contents)) = default_section(s, '(') { - function_call = Some(parse_arguments(contents, containing_class.as_ref().and_then(|s| Some(s.as_str())))?.1); - s = ns; - } - else { - loop { - let ns; - let connector = if let Some(next) = s.chars().next() { - if next == '.' { - ns = s.take_split(1).0; - NameConnectors::NonStatic - } else if next == ':' && s.chars().nth(1).is_some_and(|c| c == ':') { - ns = s.take_split(2).0; - NameConnectors::Static - } - else { - break; - } - } else { - break; - }; - - let (ns, _) = discard_ignored(ns)?; - - let (ns, name) = parse_simple_name(ns)?; - // ? Shouldn't be necessary due to parse_simple_name call but still included in case of implementation change - let (ns, _) = discard_ignored(ns)?; - - names.push((connector, name.to_string())); - - if let Ok((ns, contents)) = default_section(ns, '(') { - function_call = Some(parse_arguments(contents, containing_class.as_ref().and_then(|s| Some(s.as_str())))?.1); - s = ns; - break; - } - - s = ns; - } - } - - Ok(( - s, - UnresolvedNameToken { - location, - containing_class, - indirection: Indirection(0), // TODO - base: base_name.to_string(), - names, - function_call, - }, - )) -} - -pub fn parse_simple_name(s: Span) -> ParseResult { +pub fn parse_simple_name<'a>(s: Span<'a>) -> ParseResult<'a, Span, SimpleNameToken> { let (s, n) = take_till(|c: char| c.is_whitespace() || (!c.is_alphabetic() && c != '_'))(s)?; if let Some(first) = s.chars().next() { @@ -143,6 +37,8 @@ pub fn parse_simple_name(s: Span) -> ParseResult { kind: BaseErrorKind::Expected(Expectation::Alpha), })) } else { - Ok((s, n)) + Ok((s, SimpleNameToken::new(n))) } } + + diff --git a/src/root/parser/parse_name_old.rs b/src/root/parser/parse_name_old.rs new file mode 100644 index 0000000..7fe73ea --- /dev/null +++ b/src/root/parser/parse_name_old.rs @@ -0,0 +1,122 @@ +use crate::root::parser::parse::{Location, ParseResult, Span}; +use crate::root::parser::parse_function::parse_evaluable::EvaluableToken; +use derive_getters::{Dissolve, Getters}; +use nom::InputTake; +use crate::root::parser::parse_arguments::parse_arguments; +use crate::root::parser::parse_blocks::default_section; +use crate::root::parser::parse_name; +use crate::root::parser::parse_util::discard_ignored; +use crate::root::shared::common::Indirection; + +// #[derive(Debug)] +// pub enum NameConnectors { +// NonStatic, +// Static, +// } +// +// #[derive(Debug, Dissolve, Getters)] +// pub struct UnresolvedNameToken { +// location: Location, +// containing_class: Option, +// indirection: Indirection, +// base: String, +// names: Vec<(NameConnectors, String)>, +// function_call: Option>, +// } +// +// impl UnresolvedNameToken { +// pub fn new_unresolved( +// s: &Span, +// containing_class: Option, +// ) -> UnresolvedNameToken { +// let location = Location::from_span(s); +// let file_location = location.path().clone(); +// UnresolvedNameToken { +// location, +// containing_class, +// indirection: Indirection(0), +// base: s.to_string(), +// names: Vec::new(), +// function_call: None +// } +// } +// +// pub fn new_unresolved_top( +// s: String, +// location: Location +// ) -> UnresolvedNameToken { +// UnresolvedNameToken { +// location, +// containing_class: None, +// indirection: Indirection(0), +// base: s, +// names: Vec::new(), +// function_call: None +// } +// } +// } + +// pub fn parse_full_name_old(s: Span, containing_class: Option) -> ParseResult { +// // TODO: Handle indirection +// +// let (s, _) = discard_ignored(s)?; +// +// let location = Location::from_span(&s); +// +// let (mut s, base_name) = parse_name::parse_simple_name(s)?; +// +// let mut names = Vec::new(); +// let mut function_call = None; +// +// if let Ok((ns, contents)) = default_section(s, '(') { +// function_call = Some(parse_arguments(contents, containing_class.as_ref().and_then(|s| Some(s.as_str())))?.1); +// s = ns; +// } +// else { +// loop { +// let ns; +// let connector = if let Some(next) = s.chars().next() { +// if next == '.' { +// ns = s.take_split(1).0; +// NameConnectors::NonStatic +// } else if next == ':' && s.chars().nth(1).is_some_and(|c| c == ':') { +// ns = s.take_split(2).0; +// NameConnectors::Static +// } +// else { +// break; +// } +// } else { +// break; +// }; +// +// let (ns, _) = discard_ignored(ns)?; +// +// let (ns, name) = parse_name::parse_simple_name(ns)?; +// // ? Shouldn't be necessary due to parse_simple_name call but still included in case of implementation change +// let (ns, _) = discard_ignored(ns)?; +// +// names.push((connector, name.to_string())); +// +// if let Ok((ns, contents)) = default_section(ns, '(') { +// function_call = Some(parse_arguments(contents, containing_class.as_ref().and_then(|s| Some(s.as_str())))?.1); +// s = ns; +// break; +// } +// +// s = ns; +// } +// } +// +// Ok(( +// s, +// UnresolvedNameToken { +// location, +// containing_class, +// indirection: Indirection(0), // TODO +// base: base_name.to_string(), +// names, +// function_call, +// }, +// )) +// } diff --git a/src/root/parser/parse_parameters.rs b/src/root/parser/parse_parameters.rs index ed42d95..3218d3e 100644 --- a/src/root/parser/parse_parameters.rs +++ b/src/root/parser/parse_parameters.rs @@ -1,34 +1,35 @@ use crate::root::parser::parse::{Location, ParseResult, Span}; -use crate::root::parser::parse_name::{parse_full_name, parse_simple_name, UnresolvedNameToken}; -use nom::character::complete::{char}; -use nom::Offset; +use nom::character::complete::char; +use crate::root::name_resolver::name_resolvers::NameResult; +use crate::root::parser::parse_function::parse_evaluable::{FullNameWithIndirectionToken, parse_full_name}; +use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; use crate::root::parser::parse_util::discard_ignored; -pub type Parameters = Vec<((String, Location), (UnresolvedNameToken, Location))>; +pub type Parameters = Vec<(SimpleNameToken, FullNameWithIndirectionToken)>; -pub fn parse_parameters(s: Span, mut allow_self: Option) -> ParseResult<(), Parameters> { +pub fn parse_parameters<'a, 'b>(s: Span<'a>, mut allow_self: Option<&'b SimpleNameToken>) -> ParseResult<'a, (), Parameters> { let (mut s, _) = discard_ignored(s)?; let mut parameters = Vec::new(); - let self_name = allow_self.as_ref().and_then(|s| Some(s.base().to_string())); while !s.is_empty() { let (ns, name) = parse_simple_name(s)?; - let (ns, (type_name_token, t_location)) = if allow_self.is_some() && parameters.is_empty() && *name.fragment() == "self" { - (ns, (allow_self.take().unwrap(), Location::from_span(&name))) + let (ns, p_type) = if allow_self.is_some() && parameters.is_empty() && *name.name() == "self" { + let s = allow_self.take().unwrap(); + (ns, FullNameWithIndirectionToken::from_simple(s.clone(), Some(s.clone()), name.location().clone())) } else { let (ns, _) = discard_ignored(ns)?; let (ns, _) = char(':')(ns)?; let (ns, _) = discard_ignored(ns)?; let t_location = Location::from_span(&ns); - let (ns, type_name_token) = parse_full_name(ns, self_name.clone())?; + let (ns, type_name_token) = parse_full_name(ns, allow_self)?; let (ns, _) = discard_ignored(ns)?; - (ns, (type_name_token, t_location)) + (ns, type_name_token) }; - parameters.push(((name.to_string(), Location::from_span(&name)), (type_name_token, t_location))); + parameters.push((name, p_type)); if ns.is_empty() { break; diff --git a/src/root/parser/parse_struct.rs b/src/root/parser/parse_struct.rs index 7cc733d..92d8677 100644 --- a/src/root/parser/parse_struct.rs +++ b/src/root/parser/parse_struct.rs @@ -1,23 +1,19 @@ -use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; -use crate::root::parser::parse_name::{parse_simple_name, UnresolvedNameToken}; -use crate::root::parser::parse_parameters::{parse_parameters, Parameters}; -use crate::root::parser::parse_toplevel::{TopLevelTokens, ToplevelTestFn}; +use crate::root::parser::parse::{Location, ParseResult, Span}; +use crate::root::parser::parse_parameters::{Parameters, parse_parameters}; +use crate::root::parser::parse_toplevel::{ToplevelTestFn, TopLevelTokens}; use derive_getters::{Dissolve, Getters}; -use nom::character::complete::{ satisfy}; use nom::sequence::Tuple; -use nom::Err::Error; -use nom::{IResult, Parser}; -use nom_supreme::error::{BaseErrorKind, Expectation}; +use nom::Parser; use nom_supreme::tag::complete::tag; -use substring::Substring; use crate::root::parser::parse_blocks::default_section; +use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; use crate::root::shared::common::TypeID; #[derive(Debug, Getters, Dissolve)] pub struct StructToken { location: Location, - name: String, + name: SimpleNameToken, attributes: Parameters, id: Option } @@ -50,7 +46,7 @@ pub fn parse_struct(s: Span) -> ParseResult { s, StructToken { location, - name: name.to_string(), + name, attributes: parameters, id: None },