Read a WhileyFile from the token stream. If the stream is invalid in some way (e.g. contains a syntax error, etc) then a SyntaxError is thrown. @return
()
| 165 | * @return |
| 166 | */ |
| 167 | public boolean read() { |
| 168 | boolean status = true; |
| 169 | ArrayList<Decl> declarations = new ArrayList<>(); |
| 170 | Name name = new Name(new Identifier(path.last())); |
| 171 | try { |
| 172 | name = parseModuleName(path); |
| 173 | skipWhiteSpace(); |
| 174 | while (index < tokens.size()) { |
| 175 | // Parse next logical declaration |
| 176 | declarations.add(parseDeclaration()); |
| 177 | skipWhiteSpace(); |
| 178 | } |
| 179 | } catch (ParseError e) { |
| 180 | // Allocate an unknown declaration to represent this parse error. |
| 181 | Decl d = target.allocate(new Decl.Unknown()); |
| 182 | // Add to declarations for enclosing unit |
| 183 | declarations.add(d); |
| 184 | // Give the unknown declaration a span corresponding to exact point of error. |
| 185 | target.allocate(new Attribute.Span(d, e.getStart(), e.getEnd())); |
| 186 | // Generate a syntax error which identifies the parse error |
| 187 | ErrorMessages.syntaxError(d, e.getErrorCode(), e.context); |
| 188 | // Signal that we have failed |
| 189 | status = false; |
| 190 | } |
| 191 | // Finally, construct the new file. |
| 192 | Tuple<Decl> decls = new Tuple<>(declarations); |
| 193 | Decl.Unit module = new Decl.Unit(name, decls); |
| 194 | // |
| 195 | Decl.Unit nunit = target.allocate(module); |
| 196 | Decl.Unit ounit = target.getModule().putUnit(nunit); |
| 197 | if (ounit != null) { |
| 198 | target.replace(ounit, nunit); |
| 199 | } |
| 200 | return status; |
| 201 | } |
| 202 | |
| 203 | private Name parseModuleName(Trie id) { |
| 204 | ArrayList<Identifier> components = new ArrayList<>(); |
no test coverage detected