Skip to content

Commit

Permalink
strip C-style comments in the preprocessor
Browse files Browse the repository at this point in the history
Using '#' for comments sometime confused the preprocessor. It also made
no sense to have comment support in the parser itself, so instead
C-style comments ('//') are being stipped by the preprocessor directly.
  • Loading branch information
sandhose committed Sep 8, 2021
1 parent 457b99b commit e698bcc
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 96 deletions.
78 changes: 58 additions & 20 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ clap_generate = "3.0.0-beta.4"
atty = "0.2.14"
nom = "7.0.0"
anyhow = "1.0.43"
thiserror = "1.0.28"
thiserror = "1.0.29"
3 changes: 2 additions & 1 deletion emulator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "MIT"

[dependencies]
bitflags = "1.3.2"
thiserror = "1.0.28"
thiserror = "1.0.29"
nom = "7.0.0"
tracing = "0.1.26"
tracing-subscriber = "0.2.20"
Expand All @@ -19,3 +19,4 @@ parse-display = "0.5.1"

[dev-dependencies]
indoc = "1.0.3"
pretty_assertions = "0.7.2"
1 change: 1 addition & 0 deletions emulator/src/parser/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ fn parse_parenthesis<'a, Error: ParseError<&'a str>>(
#[cfg(test)]
mod tests {
use nom::Finish;
use pretty_assertions::assert_eq;

use super::*;

Expand Down
72 changes: 11 additions & 61 deletions emulator/src/parser/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use nom::{
branch::alt,
bytes::complete::{escaped, tag},
character::complete::{char, line_ending, none_of, not_line_ending, one_of, space0, space1},
bytes::complete::escaped,
character::complete::{char, line_ending, none_of, one_of, space0, space1},
combinator::{all_consuming, cut, eof, map, opt, peek, value},
error::context,
multi::separated_list1,
Expand Down Expand Up @@ -105,7 +105,6 @@ impl<L> std::fmt::Display for LineContent<L> {
pub(crate) struct Line<L> {
pub symbols: Vec<Located<String, L>>,
pub content: Option<Located<LineContent<L>, L>>,
comment: Option<Located<String, L>>,
}

impl<L: Clone> AstNode<L> for Line<L> {
Expand All @@ -124,12 +123,6 @@ impl<L: Clone> AstNode<L> for Line<L> {

children.extend(self.content.iter().map(|c| c.to_node()));

children.extend(
self.comment
.iter()
.map(|c| Node::new(NodeKind::Comment, c.location.clone()).content(c.inner.clone())),
);

children
}
}
Expand All @@ -147,15 +140,6 @@ impl<L> std::fmt::Display for Line<L> {
write!(f, " ")?;
}
write!(f, "{}", c.inner)?;
had_something = true;
}

if let Some(ref c) = self.comment {
if had_something {
write!(f, "\t{}", c.inner)?;
} else {
write!(f, "{}", c.inner)?;
}
}

Ok(())
Expand Down Expand Up @@ -305,15 +289,6 @@ fn parse_line_content<'a, Error: ParseError<&'a str>>(
))(input)
}

/// Parses an inline comment
fn parse_comment<'a, Error: ParseError<&'a str>>(
input: &'a str,
) -> IResult<&'a str, String, Error> {
let (input, _) = peek(tag("#"))(input)?;
let (input, comment) = not_line_ending(input)?;
Ok((input, comment.into()))
}

/// Parses symbol definitions
fn parse_symbol_definition<'a, Error: ParseError<&'a str>>(
input: &'a str,
Expand Down Expand Up @@ -348,20 +323,8 @@ fn parse_line<'a, Error: ParseError<&'a str>>(
let content = content.map(|line| line.with_location((input, start, rest))); // Save location information
let (rest, _) = space0(rest)?;

// Extract the comment
let start = rest;
let (rest, comment) = opt(parse_comment)(rest)?;
let comment = comment.map(|comment| comment.with_location((input, start, rest))); // Save location information

// Build the line
Ok((
rest,
Line {
symbols,
content,
comment,
},
))
Ok((rest, Line { symbols, content }))
}

fn split_lines<'a, Error: ParseError<&'a str>>(
Expand Down Expand Up @@ -397,6 +360,8 @@ pub(crate) fn parse_program<'a, Error: ParseError<&'a str>>(

#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;

use crate::runtime::Reg;

use super::super::location::Locatable;
Expand All @@ -415,18 +380,6 @@ mod tests {
assert_eq!(line, Line::default());
}

#[test]
fn parse_comment_line_test() {
let line = fully_parsed(parse_line("# hello"));
assert_eq!(
line,
Line {
comment: Some("# hello".to_string().with_location((0, 7))),
..Default::default()
}
);
}

#[test]
fn parse_symbol_line_test() {
let line = fully_parsed(parse_line("hello:world : duplicate: duplicate: "));
Expand All @@ -447,7 +400,7 @@ mod tests {
#[test]
fn parse_full_line_test() {
use super::super::expression::Node;
let line = fully_parsed(parse_line("foo: bar: .space 30 + 5 # comment"));
let line = fully_parsed(parse_line("foo: bar: .space 30 + 5"));
assert_eq!(
line,
Line {
Expand All @@ -466,7 +419,6 @@ mod tests {
}
.with_location((10, 13))
),
comment: Some("# comment".to_string().with_location((24, 9))),
}
);
}
Expand Down Expand Up @@ -496,7 +448,7 @@ this has escaped chars: \r \n \t \""#;
let input = r#"
str: .space "some multiline \
string"
main: # beginning of program
main:
add %a, %b
reset
"#;
Expand All @@ -519,15 +471,13 @@ main: # beginning of program
}
.with_location((5, 32))
),
..Default::default()
}
.with_location((1, 37)),
Line {
symbols: vec!["main".to_string().with_location((0, 5))],
comment: Some("# beginning of program".to_string().with_location((6, 22))),
..Default::default()
}
.with_location((39, 28)),
.with_location((39, 5)),
Line {
content: Some(
LineContent::Instruction {
Expand All @@ -541,7 +491,7 @@ main: # beginning of program
),
..Default::default()
}
.with_location((68, 14)),
.with_location((45, 14)),
Line {
content: Some(
LineContent::Instruction {
Expand All @@ -552,8 +502,8 @@ main: # beginning of program
),
..Default::default()
}
.with_location((83, 9)),
Line::default().with_location((93, 8)),
.with_location((60, 9)),
Line::default().with_location((70, 8)),
]
}
);
Expand Down
Loading

0 comments on commit e698bcc

Please sign in to comment.