Add `parsed_to_ast` fn in `sway_core`
adds a `parsed_to_ast` function. This avoids reparsing the whole program again in the language server when doing the type-checking phase as we can call `parsed_to_ast` with our already obtained `CompileResult<ParseProgram>`.
@@ -205,30 +205,13 @@ pub enum BytecodeCompilationResult {
},
}
-pub fn compile_to_ast(
- input: Arc<str>,
+pub fn parsed_to_ast(
+ parse_program: ParseProgram,
initial_namespace: namespace::Module,
- build_config: Option<&BuildConfig>,
) -> CompileAstResult {
let mut warnings = Vec::new();
let mut errors = Vec::new();
- let CompileResult {
- value: parse_program_opt,
- warnings: new_warnings,
- errors: new_errors,
- } = parse(input, build_config);
- warnings.extend(new_warnings);
- errors.extend(new_errors);
- let parse_program = match parse_program_opt {
- Some(parse_program) => parse_program,
- None => {
- errors = dedup_unsorted(errors);
- warnings = dedup_unsorted(warnings);
- return CompileAstResult::Failure { errors, warnings };
- }
- };
-
let CompileResult {
value: typed_program_result,
warnings: new_warnings,
@@ -278,6 +261,56 @@ pub fn compile_to_ast(
}
}
+pub fn compile_to_ast(
+ input: Arc<str>,
+ initial_namespace: namespace::Module,
+ build_config: Option<&BuildConfig>,
+) -> CompileAstResult {
+ let mut warnings = Vec::new();
+ let mut errors = Vec::new();
+
+ let CompileResult {
+ value: parse_program_opt,
+ warnings: new_warnings,
+ errors: new_errors,
+ } = parse(input, build_config);
+
+ warnings.extend(new_warnings);
+ errors.extend(new_errors);
+ let parse_program = match parse_program_opt {
+ Some(parse_program) => parse_program,
+ None => {
+ errors = dedup_unsorted(errors);
+ warnings = dedup_unsorted(warnings);
+ return CompileAstResult::Failure { errors, warnings };
+ }
+ };
+
+ match parsed_to_ast(parse_program, initial_namespace) {
+ CompileAstResult::Success {
+ typed_program,
+ warnings: new_warnings,
+ } => {
+ warnings.extend(new_warnings);
+ warnings = dedup_unsorted(warnings);
+ CompileAstResult::Success {
+ typed_program,
+ warnings,
+ }
+ }
+ CompileAstResult::Failure {
+ warnings: new_warnings,
+ errors: new_errors,
+ } => {
+ warnings.extend(new_warnings);
+ errors.extend(new_errors);
+ errors = dedup_unsorted(errors);
+ warnings = dedup_unsorted(warnings);
+ CompileAstResult::Failure { errors, warnings }
+ }
+ }
+}
+
/// Given input Sway source code, compile to a [CompilationResult] which contains the asm in opcode
/// form (not raw bytes/bytecode).
pub fn compile_to_asm(
