Skip to content

Commit

Permalink
remove Expr::StatementList
Browse files Browse the repository at this point in the history
  • Loading branch information
Calcoph committed Oct 7, 2023
1 parent 70ce6f4 commit fbbe9e8
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 239 deletions.
12 changes: 8 additions & 4 deletions hexparser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cell::RefCell;
use std::{collections::HashMap, ops::Range};
use std::path::Path;
use m_parser::Statement;
use nom::IResult;
use nom::bytes::complete::{tag as just, take_until};
use nom::character::complete::{multispace1, alpha1, alphanumeric1, not_line_ending};
Expand Down Expand Up @@ -37,7 +38,11 @@ pub struct ImCompleteSemanticToken {
pub token_type: usize,
}

pub fn type_inference(expr: &Spanned<Expr>, symbol_type_table: &mut HashMap<Range<usize>, Value>) {
pub fn type_inference(expr: &Spanned<Vec<Spanned<Statement>>>, symbol_type_table: &mut HashMap<Range<usize>, Value>) {
// TODO
}

fn type_inference_expr(expr: &Spanned<Expr>, symbol_type_table: &mut HashMap<Range<usize>, Value>) {
match &expr.0 {
Expr::Error => {}
Expr::Value {..} => {}
Expand All @@ -48,7 +53,6 @@ pub fn type_inference(expr: &Spanned<Expr>, symbol_type_table: &mut HashMap<Rang
Expr::Ternary {..} => (), // TODO
Expr::NamespaceAccess {..} => (), // TODO
Expr::Unary {..} => (), // TODO
Expr::StatementList {..} => (), // TODO
Expr::Access {..} => (), // TODO
Expr::Attribute {..} => (), // TODO
Expr::AttributeArgument {..} => (), // TODO
Expand All @@ -66,7 +70,7 @@ pub fn parse(
src: &str,
includeable_folders: &Vec<String>
) -> (
Spanned<Expr>,
Spanned<Vec<Spanned<Statement>>>,
Vec<RecoveredError>,
Vec<ImCompleteSemanticToken>,
) {
Expand Down Expand Up @@ -630,4 +634,4 @@ impl DefinedToken {
}
}
}
*/
*/
43 changes: 21 additions & 22 deletions hexparser/src/m_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ pub enum Statement {
Func {
name: Spanned<String>,
args: Spanned<Vec<Spanned<FuncArgument>>>,
body: Box<Spanned<Expr>>
body: Spanned<Vec<Spanned<Statement>>>
},
Struct {
name: Spanned<String>,
body: Box<Spanned<Expr>>,
body: Spanned<Vec<Spanned<Statement>>>,
template_parameters: Vec<Spanned<Expr>>
},
Namespace {
name: Box<Spanned<Expr>>,
body: Box<Spanned<Expr>>
body: Spanned<Vec<Spanned<Statement>>>
},
Enum {
name: Spanned<String>,
Expand All @@ -99,7 +99,7 @@ pub enum Statement {
},
Bitfield {
name: Spanned<String>,
body: Box<Spanned<Expr>>
body: Spanned<Vec<Spanned<Statement>>>
},
Using {
new_name: Spanned<String>,
Expand All @@ -109,7 +109,7 @@ pub enum Statement {
Error,
Union {
name: Spanned<String>,
body: Box<Spanned<Expr>>,
body: Spanned<Vec<Spanned<Statement>>>,
template_parameters: Vec<Spanned<Expr>>
},
ArrayDefinition {
Expand Down Expand Up @@ -140,15 +140,15 @@ pub enum Statement {
branches: Vec<MatchBranch>
},
TryCatch {
try_block: Box<Spanned<Expr>>,
catch_block: Option<Box<Spanned<Expr>>>
try_block: Spanned<Vec<Spanned<Statement>>>,
catch_block: Spanned<Vec<Spanned<Statement>>>
},
If {
test: Box<Spanned<Expr>>,
consequent: Spanned<Vec<Spanned<Statement>>>,
},
IfBlock {
ifs: Box<Spanned<Expr>>,
ifs: Spanned<Vec<Spanned<Statement>>>,
alternative: Spanned<Vec<Spanned<Statement>>>
},
}
Expand All @@ -172,7 +172,6 @@ pub enum Expr {
Error,
Value{ val: Value },
ExprList { list: Vec<Spanned<Self>> },
StatementList { list: Vec<Spanned<Statement>> },
UnnamedParameter { type_: Spanned<HexType> },
Local { name: Spanned<String> },
Unary {
Expand Down Expand Up @@ -1205,15 +1204,15 @@ pub(crate) fn parse_struct<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, S
)
))
),
|(name, template_parameters, inheritance, (body, body_span)), span| { // TODO: Take into account the inheritance.
|(name, template_parameters, inheritance, body), span| { // TODO: Take into account the inheritance.
let template_parameters = match template_parameters {
Some(t) => t,
None => Vec::new()
};
(
Statement::Struct {
name,
body: Box::new((Expr::StatementList { list: body }, body_span)),
body,
template_parameters
},
span
Expand All @@ -1236,15 +1235,15 @@ fn parse_union<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned<State
)
))
),
|(name, template_parameters, (body, body_span)), span| {
|(name, template_parameters, body), span| {
let template_parameters = match template_parameters {
Some(t) => t,
None => Vec::new()
};
(
Statement::Union {
name,
body: Box::new((Expr::StatementList { list: body }, body_span)),
body,
template_parameters,
},
span
Expand Down Expand Up @@ -1442,17 +1441,17 @@ fn parse_bitfield<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned<St
)
),
|(name, body), span| {
let body = Box::new({
let body = {
let span = match body.len() {
0 => span.clone(),
_ => body.get(0).unwrap().1.start..body.get(body.len()-1).unwrap().1.end
};

(
Expr::StatementList { list: body },
body,
span
)
});
};
(
Statement::Bitfield {
name,
Expand Down Expand Up @@ -1541,7 +1540,7 @@ pub(crate) fn parse_namespace<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b
)
)
),
|(name, (body, body_span)), span| {
|(name, body), span| {
let name = Box::new(name.into_iter()
.fold((Expr::Value { val: Value::Null }, 0..1), |(accum, acc_span), (next, next_span)| {
match accum {
Expand All @@ -1563,7 +1562,7 @@ pub(crate) fn parse_namespace<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b
(
Statement::Namespace {
name,
body: Box::new((Expr::StatementList { list: body }, body_span))
body
},
span
)
Expand Down Expand Up @@ -1858,7 +1857,7 @@ fn numeric<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned<Expr>> {
)(input)
}

fn parser<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned<Expr>> {
fn parser<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned<Vec<Spanned<Statement>>>> {
map_with_span(
many_until(
|input: Tokens<'a, 'b>| match statements(input) {
Expand All @@ -1875,7 +1874,7 @@ fn parser<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a, 'b, Spanned<Expr>> {
},
eof
),
|(list, _), span| (Expr::StatementList { list }, span)
|(list, _), span| (list, span)
)(input)
}

Expand All @@ -1888,9 +1887,9 @@ fn recover_err<'a, 'b>(e: &TokError<'a, 'b>) -> Tokens<'a, 'b> {
}

// Hashmap contains the names of named expressions and their clones
pub(crate) fn token_parse(tokens: Vec<TokSpan>) -> Spanned<Expr> {
pub(crate) fn token_parse(tokens: Vec<TokSpan>) -> Spanned<Vec<Spanned<Statement>>> {
let ex = match tokens.len() {
0 => (Expr::Value { val: Value::Null }, 0..0),
0 => (vec![], 0..0),
_ => parser(Tokens::new(&tokens, tokens[0].extra.0)).expect("Unrecovered error happened in parser").1
};
//let ex = (Expr::Dollar, 0..1);
Expand Down
13 changes: 7 additions & 6 deletions hexparser/src/m_parser/code_block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ where
};
(
Statement::IfBlock {
ifs: Box::new((Expr::StatementList { list: v }, ifs.1)),
ifs: (v, ifs.1),
alternative
},
span
Expand Down Expand Up @@ -160,13 +160,14 @@ where
)))
)
),
|((try_block, try_span), catch_block), span| {
let catch_block = catch_block.map(|(catch_block, catch_span)| {
Box::new((Expr::StatementList { list: catch_block }, catch_span))
});
|(try_block, catch_block), span| {
let catch_block = match catch_block {
Some(a) => a,
None => (vec![], span.clone())
};

(Statement::TryCatch {
try_block: Box::new((Expr::StatementList { list: try_block }, try_span)),
try_block,
catch_block,
}, span) // TODO
}
Expand Down
4 changes: 2 additions & 2 deletions hexparser/src/m_parser/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ pub(crate) fn function_definition<'a, 'b>(input: Tokens<'a, 'b>) -> TokResult<'a
)
)
),
|(name, (args, (list, body_span))), span| (
|(name, (args, body)), span| (
Statement::Func {
name,
args,
body: Box::new((Expr::StatementList { list }, body_span))
body
},
span
)
Expand Down
19 changes: 0 additions & 19 deletions hexparser/src/simple_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,6 @@ impl SimpleDebug for Expr {
};
print!(")")
},
Expr::StatementList { list } => {
print!("E::StatementList(");
for (i, _) in list {
i.dbg(0);
print!(", ");
};
print!(")")
},
Expr::UnnamedParameter { .. } => print!("E::UnnamedParameter"),
Expr::Local { .. } => print!("E::Local"),
Expr::Unary { .. } => print!("E::Unary"),
Expand Down Expand Up @@ -582,17 +574,6 @@ impl SimpleDebug for Expr {
};
println!(")")
},
Expr::StatementList { list } => {
println!("E::StatementList(");
for (i, _) in list {
i.dbg(indentation+1);
println!(",")
};
for _ in 0..indentation {
print!(" ");
};
println!(")")
},
Expr::UnnamedParameter { .. } => println!("E::UnnamedParameter"),
Expr::Local { .. } => println!("E::Local"),
Expr::Unary { .. } => println!("E::Unary"),
Expand Down
6 changes: 4 additions & 2 deletions hexparser/src/test_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ fn debug_test() {
String::from("%programfiles%/imhex")
];

let ((expr, _), errs, _) = parse(test_str, &includeable_folders);
let ((stmnts, _), errs, _) = parse(test_str, &includeable_folders);
for err in errs {
println!("{}",&test_str[err.0.start..err.0.end-1]);
dbg!(err.1);
}

use simple_debug::SimpleDebug;

expr.dbg(1);
for stmnt in stmnts {
stmnt.0.dbg(1);
}
}
Loading

0 comments on commit fbbe9e8

Please sign in to comment.