1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
mod lexer;
pub(crate) mod syntax;
pub mod ast;
pub mod comments;
pub(crate) mod merge_spec_modules;
pub(crate) mod sources_shadow_deps;
use crate::{
diagnostics::{Diagnostics, FilesSourceText},
parser,
parser::syntax::parse_file_string,
shared::CompilationEnv,
};
use anyhow::anyhow;
use comments::*;
use move_command_line_common::files::find_move_filenames;
use move_symbol_pool::Symbol;
use std::{
collections::{BTreeSet, HashMap},
fs::File,
io::Read,
};
pub(crate) fn parse_program(
compilation_env: &CompilationEnv,
targets: &[String],
deps: &[String],
) -> anyhow::Result<(
FilesSourceText,
Result<(parser::ast::Program, CommentMap), Diagnostics>,
)> {
let targets = find_move_filenames(targets, true)?
.into_iter()
.map(Symbol::from)
.collect::<Vec<Symbol>>();
let mut deps = find_move_filenames(deps, true)?
.into_iter()
.map(Symbol::from)
.collect::<Vec<Symbol>>();
ensure_targets_deps_dont_intersect(compilation_env, &targets, &mut deps)?;
let mut files: FilesSourceText = HashMap::new();
let mut source_definitions = Vec::new();
let mut source_comments = CommentMap::new();
let mut lib_definitions = Vec::new();
let mut diags: Diagnostics = Diagnostics::new();
for fname in targets {
let (defs, comments, ds) = parse_file(&mut files, fname)?;
source_definitions.extend(defs);
source_comments.insert(fname, comments);
diags.extend(ds);
}
for fname in deps {
let (defs, _, ds) = parse_file(&mut files, fname)?;
lib_definitions.extend(defs);
diags.extend(ds);
}
let res = if diags.is_empty() {
let pprog = parser::ast::Program {
source_definitions,
lib_definitions,
};
Ok((pprog, source_comments))
} else {
Err(diags)
};
Ok((files, res))
}
fn ensure_targets_deps_dont_intersect(
compilation_env: &CompilationEnv,
targets: &[Symbol],
deps: &mut Vec<Symbol>,
) -> anyhow::Result<()> {
fn canonicalize(path: &Symbol) -> String {
let p = path.as_str();
match std::fs::canonicalize(p) {
Ok(s) => s.to_string_lossy().to_string(),
Err(_) => p.to_owned(),
}
}
let target_set = targets.iter().map(canonicalize).collect::<BTreeSet<_>>();
let dep_set = deps.iter().map(canonicalize).collect::<BTreeSet<_>>();
let intersection = target_set.intersection(&dep_set).collect::<Vec<_>>();
if intersection.is_empty() {
return Ok(());
}
if compilation_env.flags().sources_shadow_deps() {
deps.retain(|fname| !intersection.contains(&&canonicalize(fname)));
return Ok(());
}
let all_files = intersection
.into_iter()
.map(|s| format!(" {}", s))
.collect::<Vec<_>>()
.join("\n");
Err(anyhow!(
"The following files were marked as both targets and dependencies:\n{}",
all_files
))
}
fn parse_file(
files: &mut FilesSourceText,
fname: Symbol,
) -> anyhow::Result<(
Vec<parser::ast::Definition>,
MatchedFileCommentMap,
Diagnostics,
)> {
let mut diags = Diagnostics::new();
let mut f = File::open(fname.as_str())
.map_err(|err| std::io::Error::new(err.kind(), format!("{}: {}", err, fname)))?;
let mut source_buffer = String::new();
f.read_to_string(&mut source_buffer)?;
let buffer = match verify_string(fname, &source_buffer) {
Err(ds) => {
diags.extend(ds);
files.insert(fname, source_buffer);
return Ok((vec![], MatchedFileCommentMap::new(), diags));
}
Ok(()) => &source_buffer,
};
let (defs, comments) = match parse_file_string(fname, buffer) {
Ok(defs_and_comments) => defs_and_comments,
Err(ds) => {
diags.extend(ds);
(vec![], MatchedFileCommentMap::new())
}
};
files.insert(fname, source_buffer);
Ok((defs, comments, diags))
}