use std::collections::{HashMap, HashSet};
use super::ast::{
AssignOp, ChoiceOpt, Expr, IdentRef, IdentityDecl, Import, LabelCall, LabelDecl, LabelRef,
MatchArm, Module, Param, PathElem, Pos, STATEMENT_METHODS, StableId, Stmt, StrPart, TyName,
TypeDeclAst, TypeDeclKind, VarDecl, VariantAst,
};
use super::expr::{
ExprParser, Line, PErr, PResult, Seg, err, first_word, is_ident_continue, is_ident_start,
unexpected, unexpected_char, unexpected_eol,
};
use super::keywords::{is_block_statement, is_declaration, is_reserved, starts_block};
use crate::diag::{DiagArg, DiagCode, Diagnostic, Severity};
mod block;
mod branch;
mod continuation;
mod decl;
pub(crate) mod format;
pub(crate) mod schema;
pub(crate) type IdentMap = HashMap<String, HashSet<String>>;
pub(crate) const ANY_IDENTITY: &str = "*";
pub(crate) fn scan_imports(source: &str) -> Vec<String> {
let source = source.strip_prefix('\u{feff}').unwrap_or(source);
let mut out = Vec::new();
for l in source.split('\n') {
let t = l.trim_start_matches([' ', '\t']);
if let Some(rest) = t.strip_prefix("import")
&& rest.starts_with([' ', '\t', '{'])
{
scan_import_tree(rest, &mut Vec::new(), &mut out, 0);
}
}
out
}
fn scan_import_tree<'a>(
s: &'a str,
prefix: &mut Vec<String>,
out: &mut Vec<String>,
depth: usize,
) -> Option<&'a str> {
const WS: [char; 2] = [' ', '\t'];
if depth > 16 {
return None;
}
let s = s.trim_start_matches(WS);
if let Some(mut r) = s.strip_prefix('{') {
loop {
r = scan_import_tree(r, prefix, out, depth + 1)?.trim_start_matches(WS);
match r.strip_prefix(',') {
Some(n) => {
let n = n.trim_start_matches(WS);
if let Some(end) = n.strip_prefix('}') {
return Some(end);
}
r = n;
}
None => return r.strip_prefix('}'),
}
}
}
let base = prefix.len();
let mut cur = s;
let result = loop {
let w = first_word(cur);
if w.is_empty() {
break None;
}
prefix.push(crate::ident::normalize_name(w));
cur = cur[w.len()..].trim_start_matches(WS);
if let Some(r) = cur.strip_prefix("::") {
let r = r.trim_start_matches(WS);
if r.starts_with('{') {
break scan_import_tree(r, prefix, out, depth + 1);
}
cur = r;
continue;
}
out.push(prefix.join("::"));
break Some(match cur.strip_prefix("as") {
Some(a) if !a.starts_with(is_ident_continue) => {
let a = a.trim_start_matches(WS);
&a[first_word(a).len()..]
}
_ => cur,
});
};
prefix.truncate(base);
result
}
pub(crate) fn scan_identities(source: &str) -> HashSet<String> {
let source = source.strip_prefix('\u{feff}').unwrap_or(source);
let mut out = HashSet::new();
for l in source.split('\n') {
let t = l.trim_start_matches([' ', '\t']);
if let Some(rest) = t.strip_prefix("identity")
&& rest.starts_with([' ', '\t'])
{
let name = first_word(rest.trim_start_matches([' ', '\t']));
if !name.is_empty() {
out.insert(crate::ident::normalize_name(name));
}
}
}
out
}
#[cfg(test)]
pub(crate) fn parse(source: &str, module: &str) -> (Module, Vec<Diagnostic>) {
let mut idents = IdentMap::new();
idents.insert(module.to_string(), scan_identities(source));
parse_with(source, module, &idents)
}
pub(crate) fn parse_with(
source: &str,
module: &str,
idents: &IdentMap,
) -> (Module, Vec<Diagnostic>) {
let mut p = Parser::new(source, module, idents);
p.parse_module();
(p.out, p.diags)
}
pub(crate) fn parse_repl_body(
source: &str,
module: &str,
idents: &IdentMap,
) -> (Vec<Stmt>, Vec<Diagnostic>) {
let mut p = Parser::new(source, module, idents);
p.join_lines = false;
let mut body = Vec::new();
while p.i < p.lines.len() {
let raw = p.lines[p.i];
let no = p.i as u32 + 1;
p.i += 1;
let off = indent_len(raw);
let t = &raw[off..];
if t.is_empty() || t.starts_with("//") {
continue;
}
if t.starts_with('}') {
let e = PErr {
code: DiagCode::UnexpectedClose,
args: Vec::new(),
pos: Line::new(raw, no).pos(off),
};
p.push(e);
continue;
}
let joined = match p.join_statement(raw, no, off, off) {
Ok(j) => j,
Err(e) => {
p.push(e);
continue;
}
};
let last = p.lines[p.i - 1];
let line = match &joined {
Some(j) => Line::joined(&j.text, no, j.segs.clone()),
None => Line::new(raw, no),
};
match p.parse_stmt(&line, off) {
Ok(Some(st)) => body.push(st),
Ok(None) => {}
Err(e) => {
if starts_block(first_word(t)) && opens_block(last) {
p.skip_block();
}
p.push(e);
}
}
}
(body, p.diags)
}
pub(crate) fn parse_expr_line(text: &str, module: &str, line_no: u32) -> Result<Expr, Diagnostic> {
let line = Line::new(text, line_no);
let mut ep = ExprParser::new(&line, 0);
let res = (|| {
let e = ep.parse_expr()?;
ep.expect_end()?;
Ok(e)
})();
res.map_err(|e: PErr| Diagnostic {
module: module.to_string(),
line: e.pos.line,
col: e.pos.col,
severity: Severity::Error,
code: e.code,
args: e.args,
})
}
enum OptionBody {
Block(Pos),
Inline(usize, Pos),
Stmt(usize),
}
#[inline(never)]
fn classify_option_body(jl: &Line<'_>, body_at: usize) -> PResult<OptionBody> {
let mut ep = ExprParser::new(jl, body_at);
if ep.peek() == Some('{') {
let open = ep.here();
ep.bump();
let after = ep.pos;
Ok(if ep.at_end()? {
OptionBody::Block(open)
} else {
OptionBody::Inline(after, open)
})
} else if ep.at_end()? {
unexpected_eol(ep.here())
} else {
Ok(OptionBody::Stmt(ep.pos))
}
}
enum TailKind {
End,
Else(Pos),
ElseIf(Expr, Pos),
}
enum Tail<'a> {
End,
Else(Vec<Stmt>),
ElseIf(Expr, Vec<Stmt>, Option<Close<'a>>),
}
#[derive(Clone, Copy)]
struct Close<'a> {
raw: &'a str,
no: u32,
off: usize,
}
struct Parser<'a> {
module: &'a str,
idents: &'a IdentMap,
lines: Vec<&'a str>,
i: usize,
diags: Vec<Diagnostic>,
out: Module,
depth: u32,
aborted: bool,
seen_decl: bool,
join_lines: bool,
}
impl<'a> Parser<'a> {
fn new(source: &'a str, module: &'a str, idents: &'a IdentMap) -> Self {
let source = source.strip_prefix('\u{feff}').unwrap_or(source);
Parser {
module,
idents,
lines: source
.split('\n')
.map(|l| l.strip_suffix('\r').unwrap_or(l))
.collect(),
i: 0,
diags: Vec::new(),
out: Module::default(),
depth: 0,
aborted: false,
seen_decl: false,
join_lines: true,
}
}
}
const MAX_BLOCK_DEPTH: u32 = 64;
fn indent_len(raw: &str) -> usize {
raw.len() - raw.trim_start_matches([' ', '\t']).len()
}
fn strip_comment(s: &str) -> &str {
let mut in_str = false;
let mut esc = false;
let b = s.as_bytes();
let mut i = 0;
while i < b.len() {
let c = b[i];
if in_str {
if esc {
esc = false;
} else if c == b'\\' {
esc = true;
} else if c == b'"' {
in_str = false;
}
} else if c == b'"' {
in_str = true;
} else if c == b'/' && b.get(i + 1) == Some(&b'/') {
return &s[..i];
}
i += 1;
}
s
}
fn opens_block(s: &str) -> bool {
strip_comment(s).trim_end().ends_with('{')
}
fn assign_op_at(rest: &str) -> Option<(AssignOp, usize)> {
let r = rest.trim_start_matches([' ', '\t']);
let skipped = rest.len() - r.len();
let table: [(&str, AssignOp); 6] = [
("+=", AssignOp::Add),
("-=", AssignOp::Sub),
("*=", AssignOp::Mul),
("/=", AssignOp::Div),
("%=", AssignOp::Rem),
("=", AssignOp::Set),
];
for (s, op) in table {
if r.starts_with(s) {
if op == AssignOp::Set && (r.starts_with("==") || r.starts_with("=>")) {
return None;
}
return Some((op, skipped + s.len()));
}
}
None
}
impl<'a> Parser<'a> {
fn push(&mut self, e: PErr) {
self.diags.push(Diagnostic {
module: self.module.to_string(),
line: e.pos.line,
col: e.pos.col,
severity: Severity::Error,
code: e.code,
args: e.args,
});
}
fn is_identity(&self, name: &str) -> bool {
if self.idents.contains_key(ANY_IDENTITY) {
return true;
}
let has = |m: &str| self.idents.get(m).is_some_and(|s| s.contains(name));
has(self.module) || self.out.imports.iter().any(|i| has(&i.path))
}
fn is_qualified_identity(&self, module: &str, name: &str) -> bool {
self.idents.contains_key(ANY_IDENTITY)
|| self.idents.get(module).is_some_and(|s| s.contains(name))
}
fn try_quote(&mut self, line: &Line<'_>, off: usize) -> PResult<Option<Stmt>> {
let t = &line.text[off..];
let pos = line.pos(off);
let (identity, quote_at) = if t.starts_with('「') {
(None, off)
} else {
let word = first_word(t);
if word.is_empty() {
return Ok(None);
}
let after = &t[word.len()..];
if after.starts_with('「') && self.is_identity(&crate::ident::nfc(word)) {
let ident = IdentRef {
module: None,
name: crate::ident::normalize_name(word),
pos,
};
(Some(ident), off + word.len())
} else if let Some(r) = after.strip_prefix('.')
&& r.chars().next().is_some_and(is_ident_start)
{
let member = first_word(r);
if r[member.len()..].starts_with('「')
&& self
.is_qualified_identity(&crate::ident::nfc(word), &crate::ident::nfc(member))
{
let ident = IdentRef {
module: Some((crate::ident::normalize_name(word), pos)),
name: crate::ident::normalize_name(member),
pos: line.pos(off + word.len() + 1),
};
(Some(ident), off + word.len() + 1 + member.len())
} else {
return Ok(None);
}
} else {
return Ok(None);
}
};
let quote_pos = line.pos(quote_at);
let following = &self.lines[self.i.min(self.lines.len())..];
let Some(joined) = join_quote(line, quote_at, following, self.i as u32 + 1) else {
return match identity {
Some(_) => err(DiagCode::UnterminatedQuote, vec![], quote_pos),
None => Ok(None),
};
};
let jl = Line::joined(&joined.text, line.no, joined.segs.clone());
let rest = &joined.text[joined.end..];
let trimmed = rest.trim_start_matches([' ', '\t']);
let tail_at = joined.end + (rest.len() - trimmed.len());
let id_text = trimmed.trim_end_matches([' ', '\t']);
let id = if id_text.is_empty() {
None
} else if let Some(name) = id_text.strip_prefix('#')
&& is_stable_id_shape(name)
{
let id_pos = jl.pos(tail_at);
if let Err(e) = check_stable_id(name, id_pos) {
self.i += joined.consumed;
return Err(e);
}
Some(StableId {
id: name.to_string(),
pos: id_pos,
})
} else {
if identity.is_none() {
return Ok(None);
}
self.i += joined.consumed;
return err(DiagCode::TextAfterQuote, vec![], jl.pos(tail_at));
};
self.i += joined.consumed;
let parts = parse_body(&jl, quote_at, joined.end)?;
Ok(Some(Stmt::Quote {
identity,
parts,
id,
id_at: jl.pos(joined.end),
pos,
}))
}
}
fn parse_target_stmt(line: &Line<'_>, off: usize, word: &str, pos: Pos) -> PResult<Option<Stmt>> {
let t = &line.text[off..];
let after = &t[word.len()..];
if !(after.starts_with('.') || after.starts_with('[') || assign_op_at(after).is_some()) {
return Ok(None);
}
let mut ep = ExprParser::new(line, off + word.len());
let mut module: Option<(String, Pos)> = None;
let word_nfc = crate::ident::normalize_name(word);
let mut name = word_nfc.clone();
let mut path: Vec<PathElem> = Vec::new();
loop {
match ep.peek() {
Some('.') if after_dot_is_ident(&ep) => {
ep.bump();
let Some((member, mpos)) = ep.ident().ok().flatten() else {
return Ok(None);
};
if ep.peek() == Some('(') {
if !STATEMENT_METHODS.contains(&member.as_str()) {
return Ok(None);
}
if module.is_none() && path.is_empty() && is_reserved(&name) {
return Ok(None);
}
ep.bump();
let args = ep.parse_call_args()?;
ep.expect_end()?;
return Ok(Some(Stmt::ListMethod {
module,
name,
path,
method: member,
method_pos: mpos,
args,
pos,
}));
}
if module.is_none() && path.is_empty() && name == word_nfc {
module = Some((std::mem::replace(&mut name, member), pos));
} else {
path.push(PathElem::Field(member, mpos));
}
}
Some('[') => {
let at = ep.here();
ep.bump();
let Ok(idx) = ep.parse_expr() else {
return Ok(None);
};
if ep.expect_char(']').is_err() {
return Ok(None);
}
path.push(PathElem::Index(idx, at));
}
_ => break,
}
}
let Some((op, len)) = assign_op_at(ep.rest()) else {
return Ok(None);
};
ep.pos += len;
let value = if op == AssignOp::Set {
parse_value(&mut ep)?
} else {
ep.parse_expr()?
};
ep.expect_end()?;
Ok(Some(Stmt::Assign {
module,
name,
path,
op,
value,
pos,
}))
}
fn after_dot_is_ident(ep: &ExprParser<'_>) -> bool {
ep.rest()[1..].chars().next().is_some_and(is_ident_start)
}
fn brace_balance(s: &str) -> i32 {
let mut d = 0;
let mut in_str = false;
let mut esc = false;
for c in s.chars() {
if in_str {
if esc {
esc = false;
} else if c == '\\' {
esc = true;
} else if c == '"' {
in_str = false;
}
continue;
}
match c {
'"' => in_str = true,
'{' => d += 1,
'}' => d -= 1,
_ => {}
}
}
d
}
fn parse_fields(ep: &mut ExprParser<'_>) -> PResult<Vec<(String, Pos, TyName)>> {
let mut fields = Vec::new();
ep.skip_ws()?;
if ep.peek() == Some('}') {
ep.bump();
return Ok(fields);
}
loop {
let (name, pos) = ep.expect_name()?;
ep.expect_char(':')?;
let ty = ep.parse_type_name()?;
fields.push((name, pos, ty));
ep.skip_ws()?;
if ep.peek() == Some(',') {
ep.bump();
ep.skip_ws()?;
if ep.peek() == Some('}') {
break;
}
} else {
break;
}
}
ep.expect_char('}')?;
Ok(fields)
}
fn parse_variants(ep: &mut ExprParser<'_>) -> PResult<Vec<VariantAst>> {
let mut variants = Vec::new();
loop {
let (name, pos) = ep.expect_name()?;
let mut args = Vec::new();
ep.skip_ws()?;
if ep.peek() == Some('(') {
ep.bump();
loop {
args.push(ep.parse_type_name()?);
ep.skip_ws()?;
if ep.peek() == Some(',') {
ep.bump();
} else {
break;
}
}
ep.expect_char(')')?;
}
variants.push(VariantAst { name, pos, args });
ep.skip_ws()?;
if ep.peek() == Some(',') {
ep.bump();
ep.skip_ws()?;
if ep.peek() == Some('}') {
break;
}
} else {
break;
}
}
ep.expect_char('}')?;
Ok(variants)
}
#[inline(never)]
fn parse_arm_head(line: &Line<'_>, off: usize) -> PResult<(super::ast::Pattern, usize)> {
let mut ep = ExprParser::new(line, off);
let pattern = ep.parse_pattern()?;
ep.skip_ws()?;
if ep.rest().starts_with("=>") {
ep.pos += 2;
} else if ep.rest().is_empty() {
return unexpected_eol(ep.here());
} else {
return unexpected(&ep.found_here(), ep.here());
}
ep.skip_ws()?;
Ok((pattern, ep.pos))
}
fn parse_value(ep: &mut ExprParser<'_>) -> PResult<Expr> {
ep.skip_ws()?;
if ep.peek() == Some('@') {
let mut call = ep.parse_command_call()?;
call.id = read_stable_id(ep)?;
return Ok(Expr::Command(call));
}
if first_word(ep.rest()) == "call" {
let pos = ep.here();
ep.pos += "call".len();
return Ok(Expr::LabelCall(parse_label_call(ep, pos)?));
}
ep.parse_expr()
}
fn parse_label_ref(ep: &mut ExprParser<'_>) -> PResult<LabelRef> {
let (first, first_pos) = ep.expect_name()?;
if ep.peek() == Some('.') {
ep.bump();
let (name, _) = ep.expect_name()?;
return Ok(LabelRef {
module: Some((first, first_pos)),
name,
});
}
Ok(LabelRef {
module: None,
name: first,
})
}
fn parse_label_call(ep: &mut ExprParser<'_>, pos: Pos) -> PResult<LabelCall> {
let target = parse_label_ref(ep)?;
ep.expect_char('(')?;
let args = ep.parse_call_args()?;
Ok(LabelCall { target, args, pos })
}
fn parse_type(ep: &mut ExprParser<'_>) -> PResult<TyName> {
ep.parse_type_name()
}
fn parse_params(ep: &mut ExprParser<'_>) -> PResult<Vec<Param>> {
let mut params = Vec::new();
ep.skip_ws()?;
if ep.peek() == Some(')') {
ep.bump();
return Ok(params);
}
loop {
let (name, pos) = ep.expect_name()?;
ep.expect_char(':')?;
let ty = parse_type(ep)?;
ep.skip_ws()?;
let default = if ep.peek() == Some('=') {
ep.bump();
Some(ep.parse_expr()?)
} else {
None
};
params.push(Param {
name,
pos,
ty,
default,
});
ep.skip_ws()?;
if ep.peek() == Some(',') {
ep.bump();
} else {
break;
}
}
ep.expect_char(')')?;
Ok(params)
}
fn check_stable_id(id: &str, pos: Pos) -> PResult<()> {
let ok = !id.is_empty()
&& id.chars().count() <= 32
&& id
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_');
if ok {
Ok(())
} else {
err(
DiagCode::InvalidStableId,
vec![("id", DiagArg::Str(id.to_string()))],
pos,
)
}
}
fn is_stable_id_shape(s: &str) -> bool {
!s.is_empty()
&& s.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
}
fn read_stable_id(ep: &mut ExprParser<'_>) -> PResult<Option<StableId>> {
ep.skip_ws()?;
if ep.peek() != Some('#') {
return Ok(None);
}
let p = ep.here();
ep.bump();
let start = ep.pos;
while let Some(c) = ep.peek() {
if c.is_ascii_alphanumeric() || c == '_' {
ep.bump();
} else {
break;
}
}
let id = &ep.line.text[start..ep.pos];
check_stable_id(id, p)?;
Ok(Some(StableId {
id: id.to_string(),
pos: p,
}))
}
const MAX_QUOTE_LINES: usize = 256;
struct Joined {
text: String,
segs: Vec<Seg>,
end: usize,
consumed: usize,
}
fn join_quote(line: &Line<'_>, from: usize, following: &[&str], first_no: u32) -> Option<Joined> {
let mut text = line.text.to_string();
let mut segs = line.segments();
let mut pos = from;
let mut depth = 0u32;
let mut escaped = false;
let mut consumed = 0;
loop {
while pos < text.len() {
let c = text[pos..].chars().next().expect("範囲の中");
pos += c.len_utf8();
if escaped {
escaped = false;
continue;
}
match c {
'\\' => escaped = true,
'「' => depth += 1,
'」' => {
depth = depth.saturating_sub(1);
if depth == 0 {
return Some(Joined {
text,
segs,
end: pos,
consumed,
});
}
}
_ => {}
}
}
if line.single || consumed >= following.len() || consumed >= MAX_QUOTE_LINES {
return None;
}
let raw = following[consumed];
let stripped = raw.trim_start_matches([' ', '\t']);
segs.push(Seg {
start: text.len(),
line: first_no + consumed as u32,
col_base: (raw.len() - stripped.len()) as u32,
});
text.push_str(stripped);
consumed += 1;
}
}
fn split_statements(s: &str) -> Vec<(usize, usize)> {
let mut out = Vec::new();
let mut depth = 0i32;
let mut in_str = false;
let mut esc = false;
let mut start = 0;
let mut push = |a: usize, b: usize| {
let part = &s[a..b];
let t = part.trim_start_matches([' ', '\t']);
let lead = part.len() - t.len();
let t = t.trim_end_matches([' ', '\t']);
if !t.is_empty() {
out.push((a + lead, a + lead + t.len()));
}
};
for (i, c) in s.char_indices() {
if in_str {
if esc {
esc = false;
} else if c == '\\' {
esc = true;
} else if c == '"' {
in_str = false;
}
continue;
}
match c {
'"' => in_str = true,
'(' | '{' | '[' => depth += 1,
')' | '}' | ']' => depth -= 1,
';' if depth == 0 => {
push(start, i);
start = i + 1;
}
_ => {}
}
}
push(start, s.len());
out
}
fn split_stable_id<'t>(
t: &'t str,
line: &Line<'_>,
off: usize,
) -> PResult<(&'t str, Option<StableId>)> {
let trimmed = t.trim_end_matches([' ', '\t']);
if let Some(idx) = trimmed.rfind('#')
&& idx > 0
{
let before = trimmed.as_bytes()[idx - 1];
let after = &trimmed[idx + 1..];
let shape_ok = is_stable_id_shape(after);
if (before == b' ' || before == b'\t') && shape_ok {
let pos = line.pos(off + idx);
check_stable_id(after, pos)?;
let body = trimmed[..idx].trim_end_matches([' ', '\t']);
return Ok((
body,
Some(StableId {
id: after.to_string(),
pos,
}),
));
}
}
Ok((trimmed, None))
}
fn parse_body(line: &Line<'_>, start: usize, end: usize) -> PResult<Vec<StrPart>> {
let mut parts = Vec::new();
let mut text = String::new();
let mut written = String::new();
let mut pos = start;
while pos < end {
let c = line.text[pos..].chars().next().expect("範囲の中");
let here = pos;
pos += c.len_utf8();
match c {
'\\' => {
let n = line.text[pos..end].chars().next();
match n {
Some(n @ ('「' | '」' | '$' | '\\' | '=' | '#')) => {
text.push(n);
written.push('\\');
written.push(n);
pos += n.len_utf8();
}
Some(n) => {
return err(
DiagCode::InvalidEscape,
vec![("found", DiagArg::Str(format!("\\{n}")))],
line.pos(here),
);
}
None => {
return err(
DiagCode::InvalidEscape,
vec![("found", DiagArg::Str("\\".to_string()))],
line.pos(here),
);
}
}
}
'$' if line.text[pos..end].starts_with('{') => {
if !text.is_empty() {
parts.push(StrPart::Text(
std::mem::take(&mut text),
std::mem::take(&mut written),
));
}
let mut ep = ExprParser::new(line, pos + 1);
let e = ep.parse_expr()?;
ep.expect_char('}')?;
pos = ep.pos;
parts.push(StrPart::Interp(e));
}
c => {
text.push(c);
written.push(c);
}
}
}
if !text.is_empty() || parts.is_empty() {
parts.push(StrPart::Text(text, written));
}
Ok(parts)
}