pub mod ast;
#[doc(hidden)]
pub mod lexer;
mod span;
pub use span::*;
use chumsky::Parser;
use ast::{File, ParsingData, ParsingState, complete_file_parser};
use lexer::{LexingError, lexer, make_chumsky_input};
#[derive(Debug, Clone, Copy)]
pub struct SourceFile<'src> {
pub path: &'src str,
pub text: &'src str,
}
#[derive(Debug, Clone)]
pub struct ParsedFile {
pub path: String,
pub ast: File,
pub data: ParsingData,
}
impl ParsedFile {
pub fn string(&self, id: ast::StringId) -> Option<&str> {
self.data.get(id)
}
}
#[derive(Debug, Clone)]
pub struct ParseDiagnostic {
pub span: Span,
pub message: String,
}
#[derive(Debug, Clone)]
pub struct ParseFailure {
pub path: String,
pub lexical: Vec<LexingError>,
pub syntactic: Vec<ParseDiagnostic>,
}
impl std::fmt::Display for ParseFailure {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.lexical.is_empty() {
write!(
formatter,
"failed to parse `{}` with {} syntax error(s)",
self.path,
self.syntactic.len()
)
} else {
write!(
formatter,
"failed to lex `{}` with {} error(s)",
self.path,
self.lexical.len()
)
}
}
}
impl std::error::Error for ParseFailure {}
pub fn parse_file(source: SourceFile<'_>) -> Result<ParsedFile, ParseFailure> {
let (spans, tokens, tokens_data) = match lexer(source.text) {
Ok(output) => output,
Err((_, errors)) => {
return Err(ParseFailure {
path: source.path.to_owned(),
lexical: errors,
syntactic: Vec::new(),
});
}
};
let state = ParsingState::new();
let result = {
let input = make_chumsky_input(&spans, &tokens);
complete_file_parser(&tokens_data, &state)
.parse(input)
.into_result()
};
match result {
Ok(ast) => {
let data = state
.try_terminate()
.expect("all parser-owned ParsingState clones must be dropped");
Ok(ParsedFile {
path: source.path.to_owned(),
ast,
data,
})
}
Err(errors) => Err(ParseFailure {
path: source.path.to_owned(),
lexical: Vec::new(),
syntactic: errors
.into_iter()
.map(|error| ParseDiagnostic {
span: *error.span(),
message: format!("{error:?}"),
})
.collect(),
}),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_empty_file() {
let parsed = parse_file(SourceFile {
path: "test://empty.lt",
text: "",
})
.expect("empty source is a valid file");
assert_eq!(parsed.path, "test://empty.lt");
assert!(parsed.ast.file_contract_declarations.is_empty());
assert!(parsed.ast.contract_definition_content.statements.is_empty());
}
#[test]
fn parses_empty_parentheses_independently_of_whitespace() {
for text in ["( )", "acs run: fn( ) -> ( )", "acs ( ): ( )"] {
parse_file(SourceFile {
path: "test://empty-parentheses.lt",
text,
})
.unwrap_or_else(|error| panic!("`{text}` must parse: {error:?}"));
}
}
#[test]
fn rejects_contract_paths_with_a_trailing_dot() {
let failure = parse_file(SourceFile {
path: "test://trailing-dot.lt",
text: "acs value: module.",
})
.expect_err("a symbol path must end with a path component");
assert!(!failure.syntactic.is_empty());
}
#[test]
fn rejects_array_sizes_that_do_not_fit_usize() {
let failure = parse_file(SourceFile {
path: "test://oversized-array.lt",
text: "let values: [i32; 18446744073709551616]",
})
.expect_err("an oversized array length must not be truncated");
assert!(!failure.syntactic.is_empty());
}
#[test]
fn parses_expression_statement() {
let parsed = parse_file(SourceFile {
path: "test://expression.lt",
text: "42",
})
.expect("integer expression must parse");
assert_eq!(parsed.ast.contract_definition_content.statements.len(), 1);
}
#[test]
fn parses_structure_literals_with_optional_field_commas() {
for text in [
"Pair { first: 1 second: 2 }",
"module.Pair { first: 1, second: 2, }",
] {
let parsed = parse_file(SourceFile {
path: "test://structure-literal.lt",
text,
})
.expect("structure literal must parse");
let ast::ExpressionKind::Source(ast::ExpressionSource::Structure(structure)) =
&only_expression(&parsed).kind
else {
panic!("expected structure literal for `{text}`")
};
assert_eq!(structure.fields.len(), 2);
}
}
#[test]
fn parses_enum_match_arms_with_optional_commas() {
for text in [
"match state { State.Empty => 0 State.Wide(payload) => payload }",
"match state { State.Empty => 0, State.Wide(payload) => payload, }",
] {
let parsed = parse_file(SourceFile {
path: "test://match.lt",
text,
})
.expect("enum match expression must parse");
let ast::ExpressionKind::Source(ast::ExpressionSource::Match(match_expression)) =
&only_expression(&parsed).kind
else {
panic!("expected match expression")
};
assert_eq!(match_expression.arms.len(), 2);
assert!(match_expression.arms[0].pattern.binding.is_none());
assert!(match_expression.arms[1].pattern.binding.is_some());
}
}
#[test]
fn adjacent_calls_are_separate_statements_without_terminators() {
let parsed = parse_file(SourceFile {
path: "test://adjacent.lt",
text: "func1()func2()",
})
.expect("a new construct ends the preceding valid statement");
assert_eq!(parsed.ast.contract_definition_content.statements.len(), 2);
}
#[test]
fn postfix_tokens_continue_the_current_statement() {
let parsed = parse_file(SourceFile {
path: "test://postfix.lt",
text: "value.method()[0].field",
})
.expect("postfix operations must continue an expression");
assert_eq!(parsed.ast.contract_definition_content.statements.len(), 1);
}
#[test]
fn parenthesized_assignment_root_keeps_postfix_place_projections() {
let parsed = parse_file(SourceFile {
path: "test://parenthesized-place.lt",
text: "(*pointer).field[0] = 42",
})
.unwrap();
let ast::Statement::Assignment(assignment) =
&parsed.ast.contract_definition_content.statements[0]
else {
panic!("expected assignment")
};
assert!(matches!(
assignment.access.entry,
ast::AccessExpressionEntry::Parenthesized(_)
));
assert_eq!(assignment.access.accessors.len(), 2);
}
#[test]
fn assignment_preserves_every_leading_dereference_projection() {
let parsed = parse_file(SourceFile {
path: "test://nested-assignment.lt",
text: "***value = 42",
})
.unwrap();
let ast::Statement::Assignment(assignment) =
&parsed.ast.contract_definition_content.statements[0]
else {
panic!("expected assignment")
};
assert_eq!(assignment.dereference_count, 3);
}
#[test]
fn assignment_rhs_ends_before_an_adjacent_construct() {
let parsed = parse_file(SourceFile {
path: "test://assignment.lt",
text: "value = func1()func2()",
})
.expect("the second call must start after the valid assignment");
assert_eq!(parsed.ast.contract_definition_content.statements.len(), 2);
assert!(matches!(
parsed.ast.contract_definition_content.statements[0],
ast::Statement::Assignment(_)
));
}
fn only_expression(parsed: &ParsedFile) -> &ast::Expression {
let statements = &parsed.ast.contract_definition_content.statements;
assert_eq!(statements.len(), 1);
match &statements[0] {
ast::Statement::Expression(expression) => expression,
other => panic!("expected expression statement, got {other:?}"),
}
}
#[test]
fn multiplication_has_higher_precedence_than_addition() {
let parsed = parse_file(SourceFile {
path: "test://precedence.lt",
text: "1 + 2 * 3",
})
.unwrap();
let ast::ExpressionKind::Binary {
operator, right, ..
} = &only_expression(&parsed).kind
else {
panic!("expected top-level binary expression")
};
assert_eq!(operator.kind, ast::BinaryExpressionOperatorKind::Add);
assert!(matches!(
right.kind,
ast::ExpressionKind::Binary {
operator: ast::BinaryExpressionOperator {
kind: ast::BinaryExpressionOperatorKind::Multiply,
..
},
..
}
));
}
#[test]
fn binary_operators_are_left_associative() {
let parsed = parse_file(SourceFile {
path: "test://associativity.lt",
text: "a - b - c",
})
.unwrap();
let ast::ExpressionKind::Binary { left, operator, .. } = &only_expression(&parsed).kind
else {
panic!("expected top-level subtraction")
};
assert_eq!(operator.kind, ast::BinaryExpressionOperatorKind::Subtract);
assert!(matches!(
left.kind,
ast::ExpressionKind::Binary {
operator: ast::BinaryExpressionOperator {
kind: ast::BinaryExpressionOperatorKind::Subtract,
..
},
..
}
));
}
#[test]
fn parses_unary_equality_bool_and_char_expressions() {
let parsed = parse_file(SourceFile {
path: "test://unary.lt",
text: "!true == false",
})
.unwrap();
let ast::ExpressionKind::Binary { left, operator, .. } = &only_expression(&parsed).kind
else {
panic!("expected equality expression")
};
assert_eq!(operator.kind, ast::BinaryExpressionOperatorKind::Equal);
assert!(matches!(left.kind, ast::ExpressionKind::Unary { .. }));
let chars = parse_file(SourceFile {
path: "test://char.lt",
text: "'a''b'",
})
.unwrap();
assert_eq!(chars.ast.contract_definition_content.statements.len(), 2);
}
#[test]
fn adjacent_construct_starts_after_complete_binary_expression() {
let parsed = parse_file(SourceFile {
path: "test://binary-boundary.lt",
text: "first + second next()",
})
.expect("next call must begin after the complete binary expression");
assert_eq!(parsed.ast.contract_definition_content.statements.len(), 2);
}
#[test]
fn started_continuation_cannot_silently_end_a_statement() {
for text in ["value.", "value +", "func("] {
let failure = parse_file(SourceFile {
path: "test://incomplete.lt",
text,
})
.expect_err("an incomplete continuation must be a syntax error");
assert!(
!failure.syntactic.is_empty(),
"source unexpectedly parsed: {text}"
);
}
}
#[test]
fn semicolon_is_optional_but_supported_for_library_compatibility() {
let without = parse_file(SourceFile {
path: "test://without.lt",
text: "func1()func2()",
})
.unwrap();
let with = parse_file(SourceFile {
path: "test://with.lt",
text: "func1();func2();",
})
.unwrap();
assert_eq!(without.ast.contract_definition_content.statements.len(), 2);
assert_eq!(with.ast.contract_definition_content.statements.len(), 2);
}
#[test]
fn reports_syntax_errors_with_source_span() {
let failure = parse_file(SourceFile {
path: "test://syntax.lt",
text: "let",
})
.expect_err("incomplete declaration must fail parsing");
assert!(failure.lexical.is_empty());
assert!(!failure.syntactic.is_empty());
assert!(failure.syntactic[0].span.start <= failure.syntactic[0].span.end);
}
#[test]
fn parses_all_core_library_sources() {
let library = crate::core_lib::generate_core_lib();
assert_eq!(library.scripts.len(), 10);
for (path, script) in library.scripts {
parse_file(SourceFile {
path: &path,
text: &script.content,
})
.unwrap_or_else(|failure| panic!("failed to parse {path}: {failure:#?}"));
}
}
#[test]
fn parses_generic_and_specialized_access_aliases() {
let parsed = parse_file(SourceFile {
path: "test://generic-alias.lt",
text: "acs function<'a, T>: module.function<'a, T> acs OptionType: Option<Type>",
})
.expect("generic access aliases must parse");
let declarations = &parsed.ast.contract_definition_content.contract_accesses;
assert_eq!(declarations.len(), 2);
let parameters = declarations[0]
.generic_declaration_list
.as_ref()
.expect("generic alias parameters");
assert_eq!(parameters.items.1.len() + 1, 2);
assert!(declarations[1].generic_declaration_list.is_none());
let ast::ContractSource::Access(target) = &declarations[1].source else {
panic!("specialized alias target must be a path")
};
assert!(target.parts.0.generic_list.is_some());
}
#[test]
fn parses_associated_type_requirements_and_bindings() {
let parsed = parse_file(SourceFile {
path: "test://associated-types.lt",
text: "pub acs ZeroablePrimitive: trait {
acs NonZeroInner: type
}
acs ext: extension for U32 {
impl ZeroablePrimitive {
acs NonZeroInner: type = NonZeroU32
}
}",
})
.expect("associated types must parse");
let trait_definition = &parsed.ast.contract_definition_content.contract_accesses[0];
let ast::ContractSource::Declaration(trait_declaration) = &trait_definition.source else {
panic!("trait declaration expected")
};
let requirement = &trait_declaration
.definition
.as_ref()
.expect("trait body")
.content
.contract_accesses[0];
assert!(matches!(
requirement.source,
ast::ContractSource::Declaration(ref declaration)
if matches!(
declaration.kind,
ast::ContractDeclarationKind::AssociatedTypeOnly(ref type_)
if type_.value_type.is_none()
)
));
}
#[test]
fn parses_named_and_file_wide_extensions() {
let parsed = parse_file(SourceFile {
path: "test://extensions.lt",
text: "as extension for Target acs target_extension: extension for Target { impl Eq acs extra: fn() }",
})
.expect("both extension forms must parse");
assert!(matches!(
parsed.ast.file_contract_declarations[0].kind,
ast::ContractDeclarationKind::ExtensionOnly(_)
));
let named = parsed
.ast
.contract_definition_content
.contract_accesses
.first()
.expect("named extension");
assert!(matches!(
named.source,
ast::ContractSource::Declaration(ref declaration)
if matches!(declaration.kind, ast::ContractDeclarationKind::ExtensionOnly(_))
));
}
#[test]
fn preserves_primitive_trait_implementations_in_ast() {
let parsed = parse_file(SourceFile {
path: "core://primitive.lt",
text: crate::core_lib::PRIMITIVE_MODULE_FILE_CONTENT,
})
.unwrap();
let i8_declaration = parsed
.ast
.contract_definition_content
.contract_accesses
.iter()
.find(|declaration| {
parsed.data.strings[declaration.ident.string.0 as usize].as_str() == "i8"
})
.expect("core i8 declaration");
let ast::ContractSource::Definition(definition) = &i8_declaration.source else {
panic!("i8 must have an inline definition")
};
assert_eq!(definition.content.trait_implementations.len(), 10);
assert!(
definition
.content
.trait_implementations
.iter()
.any(|implementation| implementation.definition.is_none())
);
}
#[test]
fn reports_lexical_errors_without_syntax_noise() {
let failure = parse_file(SourceFile {
path: "test://invalid.lt",
text: "@",
})
.expect_err("invalid token must fail parsing");
assert_eq!(failure.path, "test://invalid.lt");
assert_eq!(failure.lexical.len(), 1);
assert!(failure.syntactic.is_empty());
}
}