Skip to content

Commit

Permalink
Few fixes
Browse files Browse the repository at this point in the history
- Fix capitalisation in `ForLoopStatementInitialiser`
- Fix for #154
  • Loading branch information
kaleidawave committed Jun 3, 2024
1 parent bdac106 commit 4e19531
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 109 deletions.
8 changes: 4 additions & 4 deletions checker/src/synthesis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl crate::ASTImplementation for EznoParser {

type VariableField<'_a> = parser::VariableField;

type ForStatementInitiliser<'_a> = parser::statements::ForLoopStatementinitialiser;
type ForStatementInitiliser<'_a> = parser::statements::ForLoopStatementInitialiser;

fn module_from_string(
// TODO remove
Expand Down Expand Up @@ -163,13 +163,13 @@ impl crate::ASTImplementation for EznoParser {
checking_data: &mut crate::CheckingData<T, Self>,
) {
match for_loop_initialiser {
parser::statements::ForLoopStatementinitialiser::VariableDeclaration(declaration) => {
parser::statements::ForLoopStatementInitialiser::VariableDeclaration(declaration) => {
// TODO is this correct & the best
hoist_variable_declaration(declaration, environment, checking_data);
synthesise_variable_declaration(declaration, environment, checking_data, false);
}
parser::statements::ForLoopStatementinitialiser::VarStatement(_) => todo!(),
parser::statements::ForLoopStatementinitialiser::Expression(_) => todo!(),
parser::statements::ForLoopStatementInitialiser::VarStatement(_) => todo!(),
parser::statements::ForLoopStatementInitialiser::Expression(_) => todo!(),
}
}

Expand Down
39 changes: 29 additions & 10 deletions parser/src/declarations/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use iterator_endiate::EndiateIteratorExt;

use crate::{
derive_ASTNode, errors::parse_lexing_error, expressions::operators::COMMA_PRECEDENCE,
throw_unexpected_token_with_token, ASTNode, Expression, ParseOptions, ParseResult, Span,
TSXKeyword, TSXToken, Token, TokenReader, TypeAnnotation, VariableField, WithComment,
throw_unexpected_token_with_token, ASTNode, Expression, ParseError, ParseErrors, ParseOptions,
ParseResult, Span, TSXKeyword, TSXToken, Token, TokenReader, TypeAnnotation, VariableField,
WithComment,
};
use visitable_derive::Visitable;

Expand Down Expand Up @@ -270,10 +271,19 @@ impl ASTNode for VariableDeclaration {
break;
}
}
VariableDeclaration::LetDeclaration {
position: start.union(declarations.last().unwrap().get_position()),
declarations,
}

let position = if let Some(last) = declarations.last() {
start.union(last.get_position())
} else {
let position = start.with_length(3);
if options.partial_syntax {
position
} else {
return Err(ParseError::new(ParseErrors::ExpectedDeclaration, position));
}
};

VariableDeclaration::LetDeclaration { position, declarations }
}
VariableDeclarationKeyword::Const => {
state.append_keyword_at_pos(start.0, TSXKeyword::Const);
Expand All @@ -297,10 +307,19 @@ impl ASTNode for VariableDeclaration {
break;
}
}
VariableDeclaration::ConstDeclaration {
position: start.union(declarations.last().unwrap().get_position()),
declarations,
}

let position = if let Some(last) = declarations.last() {
start.union(last.get_position())
} else {
let position = start.with_length(3);
if options.partial_syntax {
position
} else {
return Err(ParseError::new(ParseErrors::ExpectedDeclaration, position));
}
};

VariableDeclaration::ConstDeclaration { position, declarations }
}
})
}
Expand Down
160 changes: 82 additions & 78 deletions parser/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,84 +32,7 @@ pub enum ParseErrors<'a> {
NonStandardSyntaxUsedWithoutEnabled,
ExpectRule,
InvalidRegexFlag,
}

#[allow(missing_docs)]
#[derive(Debug)]
pub enum LexingErrors {
SecondDecimalPoint,
NumberLiteralCannotHaveDecimalPoint,
NumberLiteralBaseSpecifierMustPrecededWithZero,
InvalidCharacterInJSXTag(char),
UnbalancedJSXClosingTags,
ExpectedClosingAngleAtEndOfSelfClosingTag,
InvalidCharacterInAttributeKey(char),
UnexpectedCharacter(derive_finite_automaton::InvalidCharacter),
EmptyAttributeName,
ExpectedJSXEndTag,
NewLineInStringLiteral,
ExpectedEndToMultilineComment,
ExpectedEndToStringLiteral,
UnexpectedEndToNumberLiteral,
InvalidNumeralItemBecauseOfLiteralKind,
ExpectedEndToRegexLiteral,
ExpectedEndToJSXLiteral,
ExpectedEndToTemplateLiteral,
InvalidExponentUsage,
InvalidUnderscore,
CannotLoadLargeFile(usize),
ExpectedDashInComment,
}

impl Display for LexingErrors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LexingErrors::SecondDecimalPoint => {
f.write_str("Second decimal point found in number literal")
}
LexingErrors::NumberLiteralCannotHaveDecimalPoint => {
f.write_str("Number literal with specified base cannot have decimal point")
}
LexingErrors::NumberLiteralBaseSpecifierMustPrecededWithZero => {
f.write_str("Number literal base character must be proceeded with a zero")
}
LexingErrors::InvalidCharacterInJSXTag(chr) => {
write!(f, "Invalid character {chr:?} in JSX tag")
}
LexingErrors::ExpectedClosingAngleAtEndOfSelfClosingTag => {
f.write_str("Expected closing angle at end of self closing JSX tag")
}
LexingErrors::InvalidCharacterInAttributeKey(chr) => {
write!(f, "Invalid character {chr:?} in JSX attribute name")
}
LexingErrors::EmptyAttributeName => f.write_str("Empty JSX attribute name"),
LexingErrors::ExpectedJSXEndTag => f.write_str("Expected JSX end tag"),
LexingErrors::NewLineInStringLiteral => {
f.write_str("String literals cannot contain new lines")
}
LexingErrors::ExpectedEndToMultilineComment => {
f.write_str("Unclosed multiline comment")
}
LexingErrors::ExpectedEndToStringLiteral => f.write_str("Unclosed string literal"),
LexingErrors::UnexpectedEndToNumberLiteral => f.write_str("Unclosed number literal"),
LexingErrors::ExpectedEndToRegexLiteral => f.write_str("Unclosed regex literal"),
LexingErrors::ExpectedEndToJSXLiteral => f.write_str("Unclosed JSX literal"),
LexingErrors::ExpectedEndToTemplateLiteral => f.write_str("Unclosed template literal"),
LexingErrors::UnexpectedCharacter(err) => Display::fmt(err, f),
LexingErrors::UnbalancedJSXClosingTags => f.write_str("Too many closing JSX tags"),
LexingErrors::InvalidExponentUsage => f.write_str("Two e in number literal"),
LexingErrors::InvalidUnderscore => f.write_str("Numeric separator in invalid place"),
LexingErrors::InvalidNumeralItemBecauseOfLiteralKind => {
f.write_str("Invalid item in binary, hex or octal literal")
}
LexingErrors::CannotLoadLargeFile(size) => {
write!(f, "Cannot parse {size:?} byte file (4GB maximum)")
}
LexingErrors::ExpectedDashInComment => {
f.write_str("JSX comments must have two dashes after `<!` start")
}
}
}
ExpectedDeclaration,
}

impl<'a> Display for ParseErrors<'a> {
Expand Down Expand Up @@ -203,6 +126,87 @@ impl<'a> Display for ParseErrors<'a> {
ParseErrors::InvalidRegexFlag => {
write!(f, "Regexp flags must be one of 'd', 'g', 'i', 'm', 's', 'u' or 'y'")
}
ParseErrors::ExpectedDeclaration => {
write!(f, "Expected identifier after variable declaration keyword")
}
}
}
}

#[allow(missing_docs)]
#[derive(Debug)]
pub enum LexingErrors {
SecondDecimalPoint,
NumberLiteralCannotHaveDecimalPoint,
NumberLiteralBaseSpecifierMustPrecededWithZero,
InvalidCharacterInJSXTag(char),
UnbalancedJSXClosingTags,
ExpectedClosingAngleAtEndOfSelfClosingTag,
InvalidCharacterInAttributeKey(char),
UnexpectedCharacter(derive_finite_automaton::InvalidCharacter),
EmptyAttributeName,
ExpectedJSXEndTag,
NewLineInStringLiteral,
ExpectedEndToMultilineComment,
ExpectedEndToStringLiteral,
UnexpectedEndToNumberLiteral,
InvalidNumeralItemBecauseOfLiteralKind,
ExpectedEndToRegexLiteral,
ExpectedEndToJSXLiteral,
ExpectedEndToTemplateLiteral,
InvalidExponentUsage,
InvalidUnderscore,
CannotLoadLargeFile(usize),
ExpectedDashInComment,
}

impl Display for LexingErrors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LexingErrors::SecondDecimalPoint => {
f.write_str("Second decimal point found in number literal")
}
LexingErrors::NumberLiteralCannotHaveDecimalPoint => {
f.write_str("Number literal with specified base cannot have decimal point")
}
LexingErrors::NumberLiteralBaseSpecifierMustPrecededWithZero => {
f.write_str("Number literal base character must be proceeded with a zero")
}
LexingErrors::InvalidCharacterInJSXTag(chr) => {
write!(f, "Invalid character {chr:?} in JSX tag")
}
LexingErrors::ExpectedClosingAngleAtEndOfSelfClosingTag => {
f.write_str("Expected closing angle at end of self closing JSX tag")
}
LexingErrors::InvalidCharacterInAttributeKey(chr) => {
write!(f, "Invalid character {chr:?} in JSX attribute name")
}
LexingErrors::EmptyAttributeName => f.write_str("Empty JSX attribute name"),
LexingErrors::ExpectedJSXEndTag => f.write_str("Expected JSX end tag"),
LexingErrors::NewLineInStringLiteral => {
f.write_str("String literals cannot contain new lines")
}
LexingErrors::ExpectedEndToMultilineComment => {
f.write_str("Unclosed multiline comment")
}
LexingErrors::ExpectedEndToStringLiteral => f.write_str("Unclosed string literal"),
LexingErrors::UnexpectedEndToNumberLiteral => f.write_str("Unclosed number literal"),
LexingErrors::ExpectedEndToRegexLiteral => f.write_str("Unclosed regex literal"),
LexingErrors::ExpectedEndToJSXLiteral => f.write_str("Unclosed JSX literal"),
LexingErrors::ExpectedEndToTemplateLiteral => f.write_str("Unclosed template literal"),
LexingErrors::UnexpectedCharacter(err) => Display::fmt(err, f),
LexingErrors::UnbalancedJSXClosingTags => f.write_str("Too many closing JSX tags"),
LexingErrors::InvalidExponentUsage => f.write_str("Two e in number literal"),
LexingErrors::InvalidUnderscore => f.write_str("Numeric separator in invalid place"),
LexingErrors::InvalidNumeralItemBecauseOfLiteralKind => {
f.write_str("Invalid item in binary, hex or octal literal")
}
LexingErrors::CannotLoadLargeFile(size) => {
write!(f, "Cannot parse {size:?} byte file (4GB maximum)")
}
LexingErrors::ExpectedDashInComment => {
f.write_str("JSX comments must have two dashes after `<!` start")
}
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions parser/src/statements/for_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl ASTNode for ForLoopStatement {

#[derive(Debug, Clone, PartialEq, Visitable)]
#[apply(derive_ASTNode)]
pub enum ForLoopStatementinitialiser {
pub enum ForLoopStatementInitialiser {
VariableDeclaration(VariableDeclaration),
VarStatement(VarVariableStatement),
Expression(MultipleExpression),
Expand All @@ -92,7 +92,7 @@ pub enum ForLoopCondition {
position: Span,
},
Statements {
initialiser: Option<ForLoopStatementinitialiser>,
initialiser: Option<ForLoopStatementInitialiser>,
condition: Option<MultipleExpression>,
afterthought: Option<MultipleExpression>,
position: Span,
Expand Down Expand Up @@ -165,26 +165,26 @@ impl ASTNode for ForLoopCondition {
peek
{
let declaration = VariableDeclaration::from_reader(reader, state, options)?;
Some(ForLoopStatementinitialiser::VariableDeclaration(declaration))
Some(ForLoopStatementInitialiser::VariableDeclaration(declaration))
} else if let Some(Token(TSXToken::Keyword(TSXKeyword::Var), _)) = peek {
let stmt = VarVariableStatement::from_reader(reader, state, options)?;
Some(ForLoopStatementinitialiser::VarStatement(stmt))
Some(ForLoopStatementInitialiser::VarStatement(stmt))
} else if let Some(Token(TSXToken::SemiColon, _)) = peek {
None
} else {
let expr = MultipleExpression::from_reader(reader, state, options)?;
Some(ForLoopStatementinitialiser::Expression(expr))
Some(ForLoopStatementInitialiser::Expression(expr))
};

let semi_colon_one = reader.expect_next(TSXToken::SemiColon)?;
let start = initialiser.as_ref().map_or(semi_colon_one, |init| match init {
ForLoopStatementinitialiser::VariableDeclaration(item) => {
ForLoopStatementInitialiser::VariableDeclaration(item) => {
item.get_position().get_start()
}
ForLoopStatementinitialiser::VarStatement(item) => {
ForLoopStatementInitialiser::VarStatement(item) => {
item.get_position().get_start()
}
ForLoopStatementinitialiser::Expression(item) => {
ForLoopStatementInitialiser::Expression(item) => {
item.get_position().get_start()
}
});
Expand Down Expand Up @@ -314,19 +314,19 @@ impl ASTNode for ForLoopCondition {
}

fn initialiser_to_string<T: source_map::ToString>(
initialiser: &ForLoopStatementinitialiser,
initialiser: &ForLoopStatementInitialiser,
buf: &mut T,
options: &crate::ToStringOptions,
local: crate::LocalToStringInformation,
) {
match initialiser {
ForLoopStatementinitialiser::VariableDeclaration(stmt) => {
ForLoopStatementInitialiser::VariableDeclaration(stmt) => {
stmt.to_string_from_buffer(buf, options, local);
}
ForLoopStatementinitialiser::Expression(expr) => {
ForLoopStatementInitialiser::Expression(expr) => {
expr.to_string_from_buffer(buf, options, local);
}
ForLoopStatementinitialiser::VarStatement(stmt) => {
ForLoopStatementInitialiser::VarStatement(stmt) => {
stmt.to_string_from_buffer(buf, options, local);
}
}
Expand Down
20 changes: 15 additions & 5 deletions parser/src/statements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
declarations::variable::{declarations_to_string, VariableDeclarationItem},
derive_ASTNode,
tokens::token_as_identifier,
ParseError, ParseErrors,
};
use derive_enum_from_into::{EnumFrom, EnumTryInto};
use derive_partial_eq_extras::PartialEqExtras;
Expand All @@ -19,7 +20,7 @@ use super::{
TSXKeyword, TSXToken, Token, TokenReader,
};
use crate::errors::parse_lexing_error;
pub use for_statement::{ForLoopCondition, ForLoopStatement, ForLoopStatementinitialiser};
pub use for_statement::{ForLoopCondition, ForLoopStatement, ForLoopStatementInitialiser};
pub use if_statement::*;
pub use switch_statement::{SwitchBranch, SwitchStatement};
pub use try_catch_statement::TryCatchStatement;
Expand Down Expand Up @@ -362,10 +363,19 @@ impl ASTNode for VarVariableStatement {
break;
}
}
Ok(VarVariableStatement {
position: start.union(declarations.last().unwrap().get_position()),
declarations,
})

let position = if let Some(last) = declarations.last() {
start.union(last.get_position())
} else {
let position = start.with_length(3);
if options.partial_syntax {
position
} else {
return Err(ParseError::new(ParseErrors::ExpectedDeclaration, position));
}
};

Ok(VarVariableStatement { position, declarations })
}

fn to_string_from_buffer<T: source_map::ToString>(
Expand Down

0 comments on commit 4e19531

Please sign in to comment.