Resolves a file syntax tree in the specified environment and compiles it to a Program. This operation mutates the syntax tree, both by resolving identifiers and recording local variables, and in case of error, by appending to file.errors(). @throws SyntaxError.Exception in case of resolutio
(StarlarkFile file, Resolver.Module env)
| 93 | * contained syntax scan/parse errors. Resolution errors are added to {@code file.errors()}. |
| 94 | */ |
| 95 | public static Program compileFile(StarlarkFile file, Resolver.Module env) |
| 96 | throws SyntaxError.Exception { |
| 97 | Resolver.resolveFile(file, env); |
| 98 | if (!file.ok()) { |
| 99 | throw new SyntaxError.Exception(file.errors()); |
| 100 | } |
| 101 | |
| 102 | // Extract load statements. |
| 103 | ImmutableList.Builder<String> loads = ImmutableList.builder(); |
| 104 | ImmutableList.Builder<Location> loadLocations = ImmutableList.builder(); |
| 105 | for (Statement stmt : file.getStatements()) { |
| 106 | if (stmt instanceof LoadStatement load) { |
| 107 | String module = load.getImport().getValue(); |
| 108 | loads.add(module); |
| 109 | loadLocations.add(load.getImport().getLocation()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Find unused doc comments. |
| 114 | ImmutableMap<String, DocComments> docCommentsMap = ImmutableMap.copyOf(file.docCommentsMap); |
| 115 | HashSet<Comment> usedDocCommentLines = new HashSet<>(); |
| 116 | for (DocComments docComments : docCommentsMap.values()) { |
| 117 | usedDocCommentLines.addAll(docComments.getLines()); |
| 118 | } |
| 119 | ImmutableList<Comment> unusedDocCommentLines = |
| 120 | file.getComments().stream() |
| 121 | .filter(c -> c.hasDocCommentPrefix() && !usedDocCommentLines.contains(c)) |
| 122 | .collect(toImmutableList()); |
| 123 | |
| 124 | return new Program( |
| 125 | file.getResolvedFunction(), |
| 126 | loads.build(), |
| 127 | loadLocations.build(), |
| 128 | docCommentsMap, |
| 129 | unusedDocCommentLines); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Resolves an expression syntax tree in the specified environment and compiles it to a Program. |