1use chumsky::error::Simple;
10use chumsky::extra;
11use chumsky::prelude::*;
12use vihaco_parser::{Ident, QuotedString};
13
14use vihaco_bytecode::SSTHeader;
15use vihaco_bytecode::SSTSectionView;
16
17use crate::ParsedModule;
18
19type E<'src> = extra::Err<Simple<'src, char>>;
20
21pub fn skip<'src>() -> impl Parser<'src, &'src str, (), E<'src>> + Clone {
26 vihaco_parser::trivia()
27}
28
29pub fn string_literal<'src>() -> impl Parser<'src, &'src str, QuotedString, E<'src>> + Clone {
31 let escape = just('\\').ignore_then(choice((
32 just('"').to('"'),
33 just('\\').to('\\'),
34 just('n').to('\n'),
35 just('t').to('\t'),
36 just('r').to('\r'),
37 just('0').to('\0'),
38 )));
39 let char_or_escape = choice((
40 escape,
41 any().and_is(just('"').not()).and_is(just('\\').not()),
42 ));
43 just('"')
44 .ignore_then(char_or_escape.repeated().collect::<String>())
45 .then_ignore(just('"'))
46 .map(QuotedString)
47}
48
49pub fn symbol_ref<'src>() -> impl Parser<'src, &'src str, Ident, E<'src>> + Clone {
51 just('@').ignore_then(vihaco_parser::ident().map(Ident))
52}
53
54pub fn block_i64_flat<'src>() -> impl Parser<'src, &'src str, Vec<i64>, E<'src>> + Clone {
61 let item = just('-')
62 .or_not()
63 .then(text::int(10))
64 .to_slice()
65 .map(|s: &str| s.parse::<i64>().unwrap());
66 let ws = any().filter(|c: &char| c.is_whitespace()).repeated();
68 ws.ignore_then(item)
69 .repeated()
70 .collect::<Vec<_>>()
71 .then_ignore(ws)
72}
73
74pub fn block_i64_pairs<'src>() -> impl Parser<'src, &'src str, Vec<(i64, i64)>, E<'src>> + Clone {
82 let signed_int = just('-')
83 .or_not()
84 .then(text::int(10))
85 .to_slice()
86 .map(|s: &str| s.parse::<i64>().unwrap());
87 let inline_ws = any()
88 .filter(|c: &char| c.is_whitespace() && *c != '\n')
89 .repeated()
90 .at_least(1);
91 let ws = any().filter(|c: &char| c.is_whitespace()).repeated();
92
93 let pair = signed_int.then_ignore(inline_ws).then(signed_int);
94 ws.ignore_then(pair)
95 .repeated()
96 .collect::<Vec<_>>()
97 .then_ignore(ws)
98}
99
100pub fn symbol_definition<'src>() -> impl Parser<'src, &'src str, String, E<'src>> + Clone {
103 just('@')
104 .ignore_then(
105 any()
106 .filter(|c: &char| {
107 !c.is_whitespace()
108 && !matches!(
109 *c,
110 ':' | '@'
111 | ','
112 | ';'
113 | '('
114 | ')'
115 | '{'
116 | '}'
117 | '['
118 | ']'
119 | '"'
120 | '\''
121 | '`'
122 )
123 })
124 .repeated()
125 .at_least(1)
126 .collect::<String>(),
127 )
128 .then_ignore(just(':'))
129}
130
131impl<I, H> ParsedModule<I, H> {
132 pub fn parse_section<'src, C>(section: SSTSectionView<'src, C>) -> eyre::Result<Self>
139 where
140 H: SSTHeader,
141 I: vihaco_parser::Parse<'src> + 'src,
142 {
143 let header = section.parse_header::<H>()?;
144 let (first_line, _) =
145 crate::spanned::line_column(section.file_text(), section.text_span().start);
146 let parsed = crate::spanned::parse_text_at::<I>(section.text(), first_line);
147 if let Some(first) = parsed.errors.first() {
148 let offset = section.text_span().start + first.span.start;
149 let (line, column) = crate::spanned::line_column(section.file_text(), offset);
150 let more = match parsed.errors.len() - 1 {
151 0 => String::new(),
152 1 => " (and 1 more error)".to_owned(),
153 n => format!(" (and {n} more errors)"),
154 };
155 eyre::bail!(
156 "failed to parse `.text` of section `{}`: line {line}, column {column}: {first}{more}",
157 section.name(),
158 );
159 }
160 Ok(Self {
161 header,
162 code: parsed.code.into_iter().map(|item| item.value).collect(),
163 symbols: parsed.symbols.into_iter().map(|item| item.value).collect(),
164 })
165 }
166}