Skip to content

Commit

Permalink
Partial source fixes
Browse files Browse the repository at this point in the history
- Fix partial syntax loop (added test)
- Fix issue in code_blocks_to_script
- Add diagnostics counting to code_blocks_to_script
- Also remove >1 empty line printing
  • Loading branch information
kaleidawave committed Aug 16, 2024
1 parent 2ef0ec5 commit 3d80a77
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion checker/tests/partial_source.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "ezno-parser")]
#[test]
fn type_mappings() {
fn partial_checking() {
use ezno_checker::{check_project, synthesis, TypeCheckOptions};

// Below source has several issues
Expand Down
18 changes: 16 additions & 2 deletions parser/examples/code_blocks_to_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.find_map(|item| matches!(item[0].as_str(), "--out").then_some(item[1].clone()));

let repeat = args.windows(2).find_map(|item| {
matches!(item[0].as_str(), "--repeat")
.then_some(item[1].parse::<u16>().expect("--repeat must be integer"))
matches!(item[0].as_str(), "--repeat").then(|| match item[1].parse::<u16>() {
Ok(value) => value,
Err(err) => panic!("--repeat cannot be {item}, {err:?}", item = item[1]),
})
});

let content = std::fs::read_to_string(&path)?;
Expand All @@ -36,6 +38,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut lines = content.lines();
let mut current = String::default();

let mut reading_list = false;
let mut list_count = 0;

while let Some(line) = lines.next() {
if line.starts_with("```ts") {
let code = lines.by_ref().take_while(|line| !line.starts_with("```")).fold(
Expand All @@ -49,11 +54,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

if !filters.iter().any(|filter| code.contains(filter)) {
blocks.push((std::mem::take(&mut current), code));
reading_list = true;
} else {
reading_list = false;
}
} else if let Some(header) = line.strip_prefix("#### ") {
current = header.to_owned();
reading_list = false;
} else if reading_list && line.trim_start().starts_with("- ") {
list_count += 1;
}
}

eprintln!("Found {} blocks, with {} diagnostics", blocks.len(), list_count);

blocks
} else {
todo!("parse module, split by statement braced")
Expand Down
2 changes: 2 additions & 0 deletions parser/examples/pretty_printing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ function x(a: { something: string, another: number, third: "yes" }, b: Array<{ e
console.log("here 2")
}
const x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 100, 5000, 1000, 122200, 100];
}
"#;
Expand Down
17 changes: 17 additions & 0 deletions parser/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ pub fn statements_and_declarations_to_string<T: source_map::ToString>(
options: &crate::ToStringOptions,
local: crate::LocalToStringInformation,
) {
let mut last_was_empty = false;
for (at_end, item) in items.iter().endiate() {
if !options.pretty {
if let StatementOrDeclaration::Statement(Statement::Expression(
Expand All @@ -433,6 +434,22 @@ pub fn statements_and_declarations_to_string<T: source_map::ToString>(
}
}

if options.pretty {
// Don't print more than two lines in a row
if let StatementOrDeclaration::Statement(
Statement::AestheticSemiColon(_) | Statement::Empty(_),
) = item
{
if last_was_empty {
continue;
} else {
last_was_empty = true;
}
} else {
last_was_empty = false;
}
}

if let (false, StatementOrDeclaration::Declaration(dec)) =
(options.include_type_annotations, item)
{
Expand Down
7 changes: 7 additions & 0 deletions parser/src/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,13 @@ impl MultipleExpression {
}
}

#[must_use]
pub fn get_rhs(&self) -> &Expression {
match self {
MultipleExpression::Multiple { rhs, .. } | MultipleExpression::Single(rhs) => rhs,
}
}

pub(crate) fn to_string_on_left<T: source_map::ToString>(
&self,
buf: &mut T,
Expand Down
14 changes: 13 additions & 1 deletion parser/src/statements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,22 @@ impl ASTNode for Statement {
TSXToken::SemiColon => {
Ok(Statement::AestheticSemiColon(reader.next().unwrap().get_span()))
}
TSXToken::EOS => {
reader.next();
Ok(Statement::Empty(Span { start: 0, end: 0, source: () }))
}
// Finally ...!
_ => {
let expr = MultipleExpression::from_reader(reader, state, options)?;
Ok(Statement::Expression(expr))
if let (true, Expression::Marker { .. }) = (options.partial_syntax, expr.get_rhs())
{
Err(ParseError::new(
ParseErrors::ExpectedIdentifier,
reader.next().unwrap().get_span(),
))
} else {
Ok(Statement::Expression(expr))
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions parser/tests/partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,16 @@ function y(c: ) {

assert_eq!(output, input);
}

#[test]
fn invalid_syntax() {
let sources = [("", true), ("][", false), ("{}}", false)];

for (source, is_okay) in sources.into_iter() {
let result = Module::from_string(
source.to_owned(),
ParseOptions { partial_syntax: true, ..Default::default() },
);
assert_eq!(result.is_ok(), is_okay, "Source = {:?} (parsed {:?})", source, result);
}
}

0 comments on commit 3d80a77

Please sign in to comment.