Skip to content

Commit

Permalink
fix parsing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed Oct 27, 2023
1 parent 9aa48ab commit d04256d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ term = {lparen ~ toplevel_expression ~ rparen | atomic_value}

assignment_expression = {(assignment_prefix | index_assignment_prefix) ~ toplevel_expression}
expression = {function_assignment|assignment_expression|toplevel_expression}
comment = _{"//" ~ (!eol ~ ANY)*}
eol = @{NEWLINE+|";"|EOI}
line = {(comment | (expression ~ (decorator ~ identifier)? ~ comment?)) ~ eol}
script = {eol? ~ line+ | eol}
line = {WHITESPACE* ~ eol | WHITESPACE* ~ ((expression ~ (decorator ~ identifier)?)) ~ eol?}
script = {line* ~ EOI}

// Value errors
errors = {
Expand All @@ -100,4 +98,6 @@ error_unterminated_paren = @{lparen ~ ANY*}
error_unexpected_decorator = @{decorator ~ identifier ~ WHITESPACE* ~ (!WHITESPACE~!eol~ANY)+ | SOI ~ decorator ~ ANY*}
error_unexpected_postfix = @{(factorial ~ !"=") ~ ANY*}

WHITESPACE = _{" " | "\t" | "\\\n"}
WHITESPACE = _{" " | "\t" | "\\\n" | comment}
comment = _{"//" ~ (!eol ~ ANY)* | "/*" ~ (!eol ~ !"*/" ~ ANY)* ~ "*/"}
eol = @{NEWLINE|";"}
16 changes: 13 additions & 3 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl LavendeuxHandler for Handler {
type RuleHandler = fn(token: &mut Token, state: &mut ParserState) -> Option<Error>;
fn handler_table() -> HashMap<Rule, RuleHandler> {
HashMap::from([
(Rule::EOI, rule_eoi as RuleHandler),
(Rule::script, rule_script as RuleHandler),
(Rule::line, rule_line as RuleHandler),
(Rule::term, rule_term as RuleHandler),
Expand All @@ -127,16 +128,25 @@ fn handler_table() -> HashMap<Rule, RuleHandler> {
.collect()
}

fn rule_eoi(token: &mut Token, _state: &mut ParserState) -> Option<Error> {
token.set_text("");
None
}

/// A series of lines
fn rule_script(token: &mut Token, _state: &mut ParserState) -> Option<Error> {
// Concatenate output from all child tokens (lines)
println!("{:?}", token);

token.set_text(
&token
.children()
.iter()
.map(|t| {
t.text().to_string()
+ if !t.children().is_empty() {
+ if !t.children().is_empty()
&& t.children().last().unwrap().rule() == Rule::eol
{
t.children().last().unwrap().text()
} else {
""
Expand All @@ -146,8 +156,8 @@ fn rule_script(token: &mut Token, _state: &mut ParserState) -> Option<Error> {
.join(""),
);

// Set script value if there is only one line
if token.children().len() == 1 {
// Set script value
if token.children().len() > 0 {
token.set_value(token.child(0).unwrap().value());
}

Expand Down
5 changes: 5 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,11 @@ mod test_token {
assert_token_value!("false ? 1 : 2", Value::Integer(2));
assert_token_value!("false ? 1/0 : 2", Value::Integer(2));

// Comments
assert_token_value!("/* test */ true", Value::from(true));
assert_token_value!("/* test */ true // test", Value::from(true));
assert_token_text!("/* test */ 15 @hex// test", "0xf");

// arrays
assert_token_value!(
"[10, 12] + [1.2, 1.3]",
Expand Down

0 comments on commit d04256d

Please sign in to comment.