Skip to content

Commit

Permalink
Some small fixes + clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
kaleidawave committed Nov 19, 2024
1 parent 3017e37 commit cbcbe17
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 53 deletions.
10 changes: 5 additions & 5 deletions parser/examples/code_blocks_to_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
});

let content = std::fs::read_to_string(&path)?;
let content = std::fs::read_to_string(path)?;

let filters: Vec<&str> = vec!["import", "export"];

Expand All @@ -52,7 +52,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
String::new(),
|mut a, s| {
a.push_str(s);
a.push_str("\n");
a.push('\n');
a
},
);
Expand Down Expand Up @@ -82,7 +82,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let under = PathBuf::from(under);
for (header, code) in blocks {
let mut name = heading_to_rust_identifier(&header);
name.push_str(".");
name.push('.');
name.push_str(&extension);
let mut file = std::fs::File::create(under.join(name))?;
// Fix for FLow
Expand Down Expand Up @@ -247,7 +247,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
for line in code.lines() {
block.push_str("\n\t");
block.push_str(&line);
block.push_str(line);
}
// If the block is not terminated, it can change the parsing of the next one
if block.ends_with(')') {
Expand All @@ -262,7 +262,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
for line in code.lines() {
block.push_str("\n\t");
block.push_str(&line);
block.push_str(line);
}
block.push('\n');
final_blocks.push((names, block));
Expand Down
8 changes: 4 additions & 4 deletions parser/examples/duplicate_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ezno_parser::{
visiting::{Chain, ImmutableVariableOrProperty, VisitOptions, Visitor, Visitors},
ASTNode, Declaration, Expression, Module, StatementOrDeclaration, VariableField,
};
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;

struct Offsets {
pub offsets: Vec<u32>,
Expand All @@ -16,7 +16,7 @@ struct Offsets {
/// TODO abstract to library
/// TODO do for funtions and types
fn get_top_level_identifiers(m: &Module) -> (HashSet<String>, HashSet<String>) {
let (mut variables, mut types): (HashSet<_>, HashSet<_>) = Default::default();
let (mut variables, types): (HashSet<_>, HashSet<_>) = Default::default();
for item in &m.items {
match item {
StatementOrDeclaration::Declaration(Declaration::Variable(variable)) => {
Expand Down Expand Up @@ -98,7 +98,7 @@ let z = 6;
total.reserve(rest.len() * (SIZE - 1));

for i in 1..SIZE {
let name = format!("{:03}", i);
let name = format!("{i:03}");
for offset in offsets.offsets.iter().copied() {
let range = offset as usize..(offset as usize + 3);
rest.replace_range(range, &name);
Expand All @@ -107,7 +107,7 @@ let z = 6;
total.push_str(&rest);
}

eprintln!("{}", total);
eprintln!("{total}");
}

/// TODO this could be collected in the same process as above
Expand Down
8 changes: 4 additions & 4 deletions parser/examples/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ fn main() {

// new::Lexer

let s = r#"'Hello World' + class X { } + (a === 5) + something * 6 "#;
let s = r"'Hello World' + class X { } + (a === 5) + something * 6 ";

let e = ezno_parser::ast::Expression::from_string(s.into(), Default::default());

assert!(e.is_ok());
// eprintln!("{:#?}", e);

let s = r#"function bunc(a, param): number {
let s = r"function bunc(a, param): number {
if (a) {
return (a: string) => {};
}
return 2
}"#;
}";

let e = ezno_parser::ast::Expression::from_string(s.into(), Default::default());

eprintln!("{:#?}", e);
eprintln!("{e:#?}");

// let s = "";
}
23 changes: 17 additions & 6 deletions parser/examples/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let to_string_output = args.iter().any(|item| item == "--to-string");
let pretty = args.iter().any(|item| item == "--pretty");

let now = Instant::now();

let parse_options = ParseOptions {
comments,
record_keyword_positions: display_keywords,
Expand Down Expand Up @@ -77,6 +75,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
)
}

const EIGHT_MEGA_BYTES: usize = 8 * 1024 * 1024;

fn parse_path(
path: &Path,
timings: bool,
Expand All @@ -93,7 +93,18 @@ fn parse_path(

eprintln!("parsing {:?} ({:?} bytes)", path.display(), source.len());
let now = Instant::now();
let result = Module::from_string_with_options(source.clone(), parse_options.clone(), None);
let mut local_parse_options = *parse_options;
if path.extension().and_then(std::ffi::OsStr::to_str).is_some_and(|ext| ext.ends_with("js")) {
local_parse_options.type_annotations = false;
}

let on = source.clone();
let result = std::thread::Builder::new()
.stack_size(EIGHT_MEGA_BYTES)
.spawn(move || Module::from_string_with_options(on, local_parse_options, None))
.unwrap()
.join()
.unwrap();

match result {
Ok((module, state)) => {
Expand Down Expand Up @@ -127,13 +138,13 @@ fn parse_path(
}

if parse_imports {
for import in state.constant_imports.iter() {
for import in &state.constant_imports {
// Don't reparse files (+ catches cycles)
let resolved_path = path.parent().unwrap().join(import);
if fs.get_paths().contains_key(&resolved_path) {
continue;
}
let _ = parse_path(
let () = parse_path(
&resolved_path,
timings,
parse_imports,
Expand All @@ -160,7 +171,7 @@ fn parse_path(
line_column.column_start += 1;
line_column.column_end += 1;
}
eprintln!("error on {:?}", line_column);
eprintln!("error on {line_column:?}");

Err(Box::<dyn std::error::Error>::from(parse_err))
}
Expand Down
2 changes: 1 addition & 1 deletion parser/src/declarations/classes/class_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl FunctionBased for ClassConstructorBase {
// }

fn has_body(body: &Self::Body) -> bool {
body.0.is_some()
body.has_body()
}

fn header_and_name_from_reader(
Expand Down
1 change: 1 addition & 0 deletions parser/src/declarations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ impl crate::ASTNode for Declaration {
alias.position.start = start.0;
Ok(Declaration::TypeAlias(alias))
} else {
#[cfg(feature = "extras")]
if reader.is_keyword("namespace") {
let mut namespace = crate::types::namespace::Namespace::from_reader(reader)?;
namespace.is_declare = true;
Expand Down
24 changes: 14 additions & 10 deletions parser/src/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ impl Expression {
reader: &mut crate::new::Lexer,
return_precedence: u8,
) -> ParseResult<Self> {
// if reader.head % 1000 == 0 {
// dbg!(reader.head);
// }
if reader.head % 1000 == 0 {
dbg!(reader.head);
}

// TODO WIP
if reader.get_options().partial_syntax {
Expand Down Expand Up @@ -950,14 +950,18 @@ impl Expression {
slice => unreachable!("{slice:?}"),
};
top = Self::SpecialOperators(operation, position);
} else if reader.is_operator_advance("!") {
// if options.type_annotations
let position = top.get_position().union(reader.get_end());
top = Self::SpecialOperators(
SpecialOperators::NonNullAssertion(Box::new(top)),
position,
);
} else {
#[cfg(feature = "extras")]
if reader.is_operator_advance("!") {
// if options.type_annotations
let position = top.get_position().union(reader.get_end());
top = Self::SpecialOperators(
SpecialOperators::NonNullAssertion(Box::new(top)),
position,
);
continue;
}

return Ok(top);
}

Expand Down
51 changes: 33 additions & 18 deletions parser/src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ impl ASTNode for FunctionHeader {
}

fn from_reader(reader: &mut crate::new::Lexer) -> ParseResult<Self> {
#[cfg(feature = "extras")]
fn parse_location(reader: &mut crate::new::Lexer) -> Option<FunctionLocationModifier> {
if reader.is_keyword_advance("server") {
Some(FunctionLocationModifier::Server)
Expand All @@ -413,26 +414,28 @@ impl ASTNode for FunctionHeader {
let start = reader.get_start();
let is_async = reader.is_operator_advance("async");

#[cfg(feature = "extras")]
if reader.is_keyword_advance("generator") {
let location = parse_location(reader);
let _ = reader.expect_keyword("function")?;
Ok(Self::ChadFunctionHeader {
return Ok(Self::ChadFunctionHeader {
is_async,
location,
// is_generator: true,
position: start.union(reader.get_end()),
})
} else {
let location = parse_location(reader);
let _ = reader.expect_keyword("function")?;
let is_generator = reader.is_operator_advance("*");
Ok(Self::VirginFunctionHeader {
is_async,
location,
is_generator,
position: start.union(reader.get_end()),
})
});
}

#[cfg(feature = "extras")]
let location = parse_location(reader);
let _ = reader.expect_keyword("function")?;
let is_generator = reader.is_operator_advance("*");
Ok(Self::VirginFunctionHeader {
is_async,
is_generator,
position: start.union(reader.get_end()),
#[cfg(feature = "extras")]
location,
})
}

fn to_string_from_buffer<T: source_map::ToString>(
Expand Down Expand Up @@ -570,11 +573,11 @@ pub(crate) fn get_method_name<T: PropertyKeyKind + 'static>(
reader: &mut crate::new::Lexer,
) -> Result<(MethodHeader, WithComment<PropertyKey<T>>), crate::ParseError> {
let after = reader.after_identifier();
let function_header = if after.starts_with("<")
|| after.starts_with("(")
|| after.starts_with("}")
|| after.starts_with(",")
|| after.starts_with(":")
let function_header = if after.starts_with('<')
|| after.starts_with('(')
|| after.starts_with('}')
|| after.starts_with(',')
|| after.starts_with(':')
{
MethodHeader::default()
} else {
Expand Down Expand Up @@ -621,3 +624,15 @@ impl ASTNode for FunctionBody {
}
}
}

impl FunctionBody {
#[cfg(feature = "full-typescript")]
pub fn has_body(&self) {
self.0.is_some()
}

#[cfg(not(feature = "full-typescript"))]
pub fn has_body(&self) {
true
}
}
2 changes: 1 addition & 1 deletion parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl ExpressionOrStatementPosition for StatementPosition {
}

fn has_function_body(body: &Self::FunctionBody) -> bool {
body.0.is_some()
body.has_body()
}

fn is_declare(&self) -> bool {
Expand Down
8 changes: 4 additions & 4 deletions parser/tests/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ function Component(item) {

#[test]
fn regex_and_leading_decimal() {
let input = r#"
let input = r"
for (const x in 0.4) {}
for await (const [a] of 0.2) {}
for (const result of /thing/) {}
"#
"
.trim();

let module = Module::from_string(input.to_owned(), Default::default()).unwrap();
Expand All @@ -172,9 +172,9 @@ for (const result of /thing/) {}

#[test]
fn class_and_object_divides() {
let input = r#"
let input = r"
const b = class Number {} / 2
"#
"
.trim();

let module = Module::from_string(input.to_owned(), Default::default()).unwrap();
Expand Down

0 comments on commit cbcbe17

Please sign in to comment.