feat: generate unified AST and editor contracts from ASDL
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 7m55s

This commit is contained in:
2026-10-19 14:31:28 +08:00
parent be4f1add58
commit f5268d2e09
40 changed files with 2517 additions and 172 deletions
+37 -22
View File
@@ -3,7 +3,8 @@ use std::fmt;
/// Half-open UTF-8 byte range in the original source, including trivia between
/// the first and last syntax token. EOF has an empty range at source.len().
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Span {
pub start: usize,
pub end: usize,
@@ -14,6 +15,16 @@ impl Span {
Self { start, end }
}
/// Safely read this range, rejecting reversed, out-of-bounds or split UTF-8 ranges.
pub fn slice(self, source: &str) -> Option<&str> {
source.get(self.start..self.end)
}
pub fn location(self, source: &str) -> Option<SourceLocation> {
self.slice(source)?;
Some(source_location(source, self.start))
}
pub(crate) fn through(self, last: Self) -> Self {
Self::new(self.start, last.end)
}
@@ -51,27 +62,7 @@ impl Diagnostic {
}
pub fn location(&self, source: &str) -> SourceLocation {
let mut location = SourceLocation { line: 1, column: 1 };
let mut previous_cr = false;
for (offset, character) in source.char_indices() {
if offset >= self.span.start {
break;
}
match character {
'\r' => {
location.line += 1;
location.column = 1;
}
'\n' if previous_cr => {}
'\n' => {
location.line += 1;
location.column = 1;
}
_ => location.column += 1,
}
previous_cr = character == '\r';
}
location
source_location(source, self.span.start)
}
}
@@ -86,3 +77,27 @@ impl fmt::Display for Diagnostic {
}
impl Error for Diagnostic {}
fn source_location(source: &str, position: usize) -> SourceLocation {
let mut location = SourceLocation { line: 1, column: 1 };
let mut previous_cr = false;
for (offset, character) in source.char_indices() {
if offset >= position {
break;
}
match character {
'\r' => {
location.line += 1;
location.column = 1;
}
'\n' if previous_cr => {}
'\n' => {
location.line += 1;
location.column = 1;
}
_ => location.column += 1,
}
previous_cr = character == '\r';
}
location
}