Skip to content

Commit

Permalink
Continued nom parser implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Lucas committed Apr 10, 2024
1 parent d8cec8f commit c0780c8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/root/nom_parser/parse_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use nom::character::complete::char;
use nom::sequence::Tuple;
use nom_supreme::tag::complete::tag;

// ! BROKEN

pub fn braced_section(s: Span) -> ParseResult {
(char('{'), take_until("}"), char('}'))
.parse(s)
Expand Down
2 changes: 0 additions & 2 deletions src/root/nom_parser/parse_function/parse_assigner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ const ASSIGNERS: [(&str, AssignmentOperatorTokens); 2] = [
];

pub fn parse_assigner(s: Span) -> ParseResult<Span, AssignmentOperatorToken> {
let (s, _) = multispace0(s)?;

let (ns, a) = alt_many(ASSIGNERS.map(|(t, o)| move |x| tag(t)(x).map(|(s, _)| (s, o.clone()))))
.parse(s)?;

Expand Down
35 changes: 31 additions & 4 deletions src/root/nom_parser/parse_function/parse_assignment.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
use crate::root::nom_parser::parse::Location;
use crate::root::nom_parser::parse_function::parse_assigner::AssignmentOperatorToken;
use crate::root::nom_parser::parse_function::parse_evaluable::EvaluableToken;
use nom::character::complete::{multispace0, multispace1};
use nom::sequence::Tuple;
use nom_supreme::tag::complete::tag;
use crate::root::nom_parser::parse::{Location, ParseResult, Span};
use crate::root::nom_parser::parse_function::parse_assigner::{AssignmentOperatorToken, parse_assigner};
use crate::root::nom_parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable};
use crate::root::nom_parser::parse_function::parse_initialisation::parse_initialisation;
use crate::root::nom_parser::parse_function::parse_line::{LineTestFn, LineTokens};
use crate::root::nom_parser::parse_name::{NameToken, parse_full_name};

#[derive(Debug)]
pub struct AssignmentToken {
location: Location,
name: String,
name: NameToken,
assignment_operator: AssignmentOperatorToken,
value: EvaluableToken,
}

// TODO: Find good way to implement?
pub fn test_parse_assignment<'a>(s: Span<'a>) -> ParseResult<Span, LineTestFn<'a>> {
Ok((s, |x| parse_assignment(x).map(|(s, x)| (s, LineTokens::Assignment(x)))))
}

pub fn parse_assignment(s: Span) -> ParseResult<Span, AssignmentToken> {
let (s, _) = multispace0(s)?;
let location = Location::from_span(s);
let (s, n) = parse_full_name(s)?;
let (s, _) = multispace0(s)?;
let (s, a) = parse_assigner(s)?;
let (s, _) = multispace0(s)?;
let (s, e) = parse_evaluable(s, true)?;
Ok((s, AssignmentToken {
location,
name: n,
assignment_operator: a,
value: e,
}))
}
3 changes: 2 additions & 1 deletion src/root/nom_parser/parse_function/parse_line.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::root::nom_parser::parse::{ErrorTree, ParseResult, Span};
use crate::root::nom_parser::parse_function::parse_assignment::AssignmentToken;
use crate::root::nom_parser::parse_function::parse_assignment::{AssignmentToken, test_parse_assignment};
use crate::root::nom_parser::parse_function::parse_break::{test_parse_break, BreakToken};
use crate::root::nom_parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken};
use crate::root::nom_parser::parse_function::parse_if::{test_parse_if, IfToken};
Expand Down Expand Up @@ -50,6 +50,7 @@ pub fn parse_line(s: Span) -> ParseResult<Span, LineTokens> {
test_parse_initialisation,
test_parse_while,
test_parse_if,
test_parse_assignment,
))
.parse(s)
{
Expand Down
3 changes: 3 additions & 0 deletions src/root/nom_parser/parse_name.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::root::nom_parser::parse::{ErrorTree, Location, ParseResult, Span};
use crate::root::nom_parser::parse_function::parse_evaluable::EvaluableToken;
use nom::bytes::complete::take_till;
use nom::character::complete::multispace0;
use nom::Err::Error;
use nom::InputTake;
use nom_supreme::error::{BaseErrorKind, Expectation};
Expand All @@ -22,6 +23,8 @@ pub struct NameToken {
pub fn parse_full_name(s: Span) -> ParseResult<Span, NameToken> {
// TODO: Handle function calls

let (s, _) = multispace0(s)?;

let location = Location::from_span(s);

let (mut s, base_name) = parse_simple_name(s)?;
Expand Down

0 comments on commit c0780c8

Please sign in to comment.