.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
bits/
char/
collections/
fs/
hash/
lang/
alloc/
ast/
gen/
il/
layout/
lower/
manifest/
module/
parser/
resolver/
scanner/
sexpr/
alloc.rad
1.2 KiB
ast.rad
36.0 KiB
gen.rad
513 B
il.rad
48.5 KiB
layout.rad
6.9 KiB
lower.rad
487.6 KiB
manifest.rad
22.5 KiB
module.rad
17.8 KiB
package.rad
1.5 KiB
parser.rad
114.1 KiB
pretty.rad
103.6 KiB
resolver.rad
508.1 KiB
scanner.rad
19.3 KiB
sexpr.rad
7.1 KiB
strings.rad
3.3 KiB
types.rad
620 B
mem/
sys/
arch.rad
68 B
bits.rad
1.6 KiB
char.rad
1.5 KiB
collections.rad
39 B
fmt.rad
11.6 KiB
fs.rad
11.0 KiB
hash.rad
41 B
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
335 B
mem.rad
12.0 KiB
mem.ras
919 B
process.rad
6.2 KiB
std.manifest
4.9 KiB
std.rad
349 B
sys.rad
1.0 KiB
testing.rad
2.4 KiB
tests.rad
26.6 KiB
vec.rad
3.2 KiB
scripts/
seed/
sublime/
test/
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
lib/std/lang/manifest.rad
raw
| 1 | //! Package manifests. |
| 2 | //! |
| 3 | //! A package is a directory that holds the manifest `<name>.manifest`, the |
| 4 | //! root module `<name>.rad`, and the package's other files. The manifest |
| 5 | //! describes the package as a single Radiance expression, a `Package` record |
| 6 | //! literal: |
| 7 | //! |
| 8 | //! Package { |
| 9 | //! name: "app", |
| 10 | //! version: "1.0.0", |
| 11 | //! dependencies: [ |
| 12 | //! Dependency { name: "std", version: "0.1.0" }, |
| 13 | //! ], |
| 14 | //! modules: ["util", "util::text"], |
| 15 | //! } |
| 16 | //! |
| 17 | //! The reader walks the syntax tree without resolving names. Values are |
| 18 | //! string literals, `nil`, array literals, and record literals with or |
| 19 | //! without their type name. `name` and `version` are required; omitted lists |
| 20 | //! are empty and an omitted `startup` is `nil`. |
| 21 | //! |
| 22 | //! Modules are named by their path below the root module, and their files |
| 23 | //! are in the package directory: module `mem::heap` of the package in |
| 24 | //! `lib/std` is the file `lib/std/mem/heap.rad`. |
| 25 | //! |
| 26 | //! A dependency names a package and the oldest compatible version. A |
| 27 | //! version is compatible with the requested one when it is not older, and |
| 28 | //! has the same major version, or for major version zero, the same minor |
| 29 | //! version. |
| 30 | |
| 31 | @test mod tests; |
| 32 | |
| 33 | use std::char; |
| 34 | use std::fmt; |
| 35 | use std::fs; |
| 36 | use std::io; |
| 37 | use std::mem; |
| 38 | use std::mem::heap; |
| 39 | use std::sys; |
| 40 | use std::lang::alloc; |
| 41 | use std::lang::ast; |
| 42 | use std::lang::il::binary; |
| 43 | use std::lang::parser; |
| 44 | use std::lang::scanner; |
| 45 | |
| 46 | /// File name extension of a manifest. |
| 47 | export constant EXTENSION: *[u8] = ".manifest"; |
| 48 | /// Name of the library directories searched from a package upward. |
| 49 | export constant LIBRARY: *[u8] = "lib"; |
| 50 | /// Bytes of directory listing storage that [`find`] uses. |
| 51 | constant LIST_BYTES: u32 = 65536; |
| 52 | |
| 53 | /// Largest number of path components that [`resolve`] keeps. |
| 54 | constant MAX_COMPONENTS: u32 = 64; |
| 55 | |
| 56 | /// A package found by name. |
| 57 | export union Found: Copy { |
| 58 | /// Source package with the manifest at this path. |
| 59 | Source(*[u8]), |
| 60 | /// Compiled package in this directory. |
| 61 | Compiled(*[u8]), |
| 62 | } |
| 63 | |
| 64 | /// A package that another package imports. |
| 65 | export record Dependency: Copy { |
| 66 | /// Package name. |
| 67 | name: *[u8], |
| 68 | /// Oldest compatible version, `MAJOR.MINOR.PATCH`. |
| 69 | version: *[u8], |
| 70 | } |
| 71 | |
| 72 | /// One package. |
| 73 | export record Package: Copy { |
| 74 | /// Package name, used by `use` in clients. |
| 75 | name: *[u8], |
| 76 | /// Release version, `MAJOR.MINOR.PATCH`. |
| 77 | version: *[u8], |
| 78 | /// Packages that this package can import. |
| 79 | dependencies: *[Dependency], |
| 80 | /// Modules below the root module, by name relative to the package. |
| 81 | modules: *[*[u8]], |
| 82 | /// Modules that are compiled only for tests, named as in `modules`. |
| 83 | tests: *[*[u8]], |
| 84 | /// Assembly sources linked after generated code. |
| 85 | assembly: *[*[u8]], |
| 86 | /// Assembly source emitted before generated code. |
| 87 | startup: ?*[u8], |
| 88 | /// Files under the package root that `@embed` can read. |
| 89 | embed: *[*[u8]], |
| 90 | } |
| 91 | |
| 92 | /// Manifest reading failure. The reader prints a diagnostic before it throws, |
| 93 | /// except for failures to find or read a file. |
| 94 | export union Error: Copy { |
| 95 | /// The text is not one Radiance expression. |
| 96 | Syntax, |
| 97 | /// The value does not describe a package. |
| 98 | Invalid, |
| 99 | /// Storage ran out. |
| 100 | TooLarge, |
| 101 | /// A directory holds no manifest, or several. |
| 102 | NotFound, |
| 103 | /// A file or directory cannot be read. |
| 104 | Unreadable, |
| 105 | } |
| 106 | |
| 107 | /// Manifest text, for diagnostics. |
| 108 | record Reader: Copy { |
| 109 | /// Manifest path. |
| 110 | path: *[u8], |
| 111 | /// Manifest text. |
| 112 | text: *[u8], |
| 113 | } |
| 114 | |
| 115 | /// Read the manifest `text` from `path`. Syntax nodes go to the arena of |
| 116 | /// `syntax`; the returned strings are copied to `arena`. |
| 117 | export fn read(syntax: &mut parser::Parser, path: *[u8], text: *[u8], arena: &heap::Heap) -> Package |
| 118 | throws (Error) |
| 119 | { |
| 120 | let root = try parser::parseValue(syntax, scanner::SourceLoc::File(path), text) catch failure { |
| 121 | if failure == parser::ParseError::AllocationFailed { |
| 122 | throw Error::TooLarge; |
| 123 | } |
| 124 | throw Error::Syntax; |
| 125 | }; |
| 126 | let r = Reader { path, text }; |
| 127 | let mut package = Package { |
| 128 | name: "", |
| 129 | version: "", |
| 130 | dependencies: &[], |
| 131 | modules: &[], |
| 132 | tests: &[], |
| 133 | assembly: &[], |
| 134 | startup: nil, |
| 135 | embed: &[], |
| 136 | }; |
| 137 | let mut named = false; |
| 138 | let mut versioned = false; |
| 139 | let fields = try recordFields(r, root, "Package"); |
| 140 | for field in fields { |
| 141 | let label = try fieldName(r, field); |
| 142 | let item = fieldValue(field); |
| 143 | if mem::eq(label, "name") { |
| 144 | set package.name = try identifier(r, item, arena); |
| 145 | set named = true; |
| 146 | } else if mem::eq(label, "version") { |
| 147 | set package.version = try version(r, item, arena); |
| 148 | set versioned = true; |
| 149 | } else if mem::eq(label, "dependencies") { |
| 150 | set package.dependencies = try dependencies(r, item, arena); |
| 151 | } else if mem::eq(label, "modules") { |
| 152 | set package.modules = try moduleNames(r, item, arena); |
| 153 | } else if mem::eq(label, "tests") { |
| 154 | set package.tests = try moduleNames(r, item, arena); |
| 155 | } else if mem::eq(label, "assembly") { |
| 156 | set package.assembly = try paths(r, item, arena); |
| 157 | } else if mem::eq(label, "startup") { |
| 158 | if let case ast::NodeValue::Nil = item.value { |
| 159 | set package.startup = nil; |
| 160 | } else { |
| 161 | set package.startup = try relativePath(r, item, arena); |
| 162 | } |
| 163 | } else if mem::eq(label, "embed") { |
| 164 | set package.embed = try paths(r, item, arena); |
| 165 | } else { |
| 166 | throw invalid(r, field, &["unknown field `", label, "`"]); |
| 167 | } |
| 168 | } |
| 169 | if not named { |
| 170 | throw invalid(r, root, &["missing field `name`"]); |
| 171 | } |
| 172 | if not versioned { |
| 173 | throw invalid(r, root, &["missing field `version`"]); |
| 174 | } |
| 175 | try checkModules(r, root, package.modules, package.tests); |
| 176 | set package.modules = try orderModules(package.modules, &[], arena); |
| 177 | set package.tests = try orderModules(package.tests, package.modules, arena); |
| 178 | return package; |
| 179 | } |
| 180 | |
| 181 | /// Return the path of module `name` from the package directory, without |
| 182 | /// the `.rad` extension: `mem::heap` gives `mem/heap`. |
| 183 | export fn modulePath(name: *[u8], arena: &heap::Heap) -> *[u8] throws (Error) { |
| 184 | let mut result: *mut [u8] = &mut []; |
| 185 | let mut i: u32 = 0; |
| 186 | while i < name.len { |
| 187 | let mut byte = name[i]; |
| 188 | set i += 1; |
| 189 | if byte == ':' { |
| 190 | set byte = '/'; |
| 191 | set i += 1; |
| 192 | } |
| 193 | try result.append(byte, arena) catch { |
| 194 | throw Error::TooLarge; |
| 195 | }; |
| 196 | } |
| 197 | return &result[..]; |
| 198 | } |
| 199 | |
| 200 | /// Return the location of the manifest of package `name` in `directory`. |
| 201 | export fn file(directory: *[u8], name: *[u8], arena: &heap::Heap) -> *[u8] throws (Error) { |
| 202 | let parts: [*[u8]; 4] = [directory, "/", name, EXTENSION]; |
| 203 | let joined = try alloc::concatBytes(arena, &parts[..]) catch { |
| 204 | throw Error::TooLarge; |
| 205 | }; |
| 206 | let text: *[u8] = &joined[..]; |
| 207 | return try resolve(&text[.. directory.len + 1], &text[directory.len + 1 ..], arena); |
| 208 | } |
| 209 | |
| 210 | /// Return the library directories searched for the dependencies of the |
| 211 | /// package in `directory`: the directories `first` in order, then [`LIBRARY`] |
| 212 | /// in `directory` and in each of its parents, up to the current directory. |
| 213 | export fn libraries(directory: *[u8], first: *[*[u8]], arena: &heap::Heap) -> *[*[u8]] |
| 214 | throws (Error) |
| 215 | { |
| 216 | let mut result: *mut [*[u8]] = &mut []; |
| 217 | try result.extend(first, arena) catch { |
| 218 | throw Error::TooLarge; |
| 219 | }; |
| 220 | let mut current = try resolve("", directory, arena); |
| 221 | loop { |
| 222 | let parts: [*[u8]; 3] = [current, "/", LIBRARY]; |
| 223 | let joined = try alloc::concatBytes(arena, &parts[..]) catch { |
| 224 | throw Error::TooLarge; |
| 225 | }; |
| 226 | try result.append(try resolve("", &joined[..], arena), arena) catch { |
| 227 | throw Error::TooLarge; |
| 228 | }; |
| 229 | if mem::eq(current, ".") |
| 230 | or mem::eq(current, "/") |
| 231 | or mem::eq(current, "..") |
| 232 | or mem::endsWith(current, "/..") |
| 233 | { |
| 234 | break; |
| 235 | } |
| 236 | if let slash = mem::rfind(current, "/") { |
| 237 | set current = "/" if slash == 0 else ¤t[.. slash]; |
| 238 | } else { |
| 239 | set current = "."; |
| 240 | } |
| 241 | } |
| 242 | return &result[..]; |
| 243 | } |
| 244 | |
| 245 | /// Find package `name` in `directory`: the source package with the manifest |
| 246 | /// `directory/name.manifest`, or the compiled package `directory/name.ril.bin`. |
| 247 | export fn found(directory: *[u8], name: *[u8], arena: &heap::Heap) -> ?Found throws (Error) { |
| 248 | let manifest = try file(directory, name, arena); |
| 249 | if try exists(manifest) { |
| 250 | return Found::Source(manifest); |
| 251 | } |
| 252 | let parts: [*[u8]; 4] = [directory, "/", name, ".ril.bin"]; |
| 253 | let image = try alloc::concatBytes(arena, &parts[..]) catch { |
| 254 | throw Error::TooLarge; |
| 255 | }; |
| 256 | if try exists(&image[..]) { |
| 257 | return Found::Compiled(directory); |
| 258 | } |
| 259 | return nil; |
| 260 | } |
| 261 | |
| 262 | /// Find package `name` as the package directory `name` of the first of the |
| 263 | /// library directories `libraries` that holds it. |
| 264 | export fn lookup(name: *[u8], libraries: *[*[u8]], arena: &heap::Heap) -> ?Found throws (Error) { |
| 265 | for library in libraries { |
| 266 | let parts: [*[u8]; 3] = [library, "/", name]; |
| 267 | let joined = try alloc::concatBytes(arena, &parts[..]) catch { |
| 268 | throw Error::TooLarge; |
| 269 | }; |
| 270 | if let package = try found(try resolve("", &joined[..], arena), name, arena) { |
| 271 | return package; |
| 272 | } |
| 273 | } |
| 274 | return nil; |
| 275 | } |
| 276 | |
| 277 | /// Check whether a file or directory exists at `path`. |
| 278 | fn exists(path: *[u8]) -> bool throws (Error) { |
| 279 | let entry = try fs::stat(path) catch { |
| 280 | throw Error::Unreadable; |
| 281 | }; |
| 282 | return entry <> nil; |
| 283 | } |
| 284 | |
| 285 | /// Return the location of the one manifest in `directory`. |
| 286 | export fn find(directory: *[u8], arena: &heap::Heap) -> *[u8] throws (Error) { |
| 287 | let listing = try fs::listDirAlloc(directory, LIST_BYTES, arena) catch failure as sys::Error { |
| 288 | throw Error::Unreadable; |
| 289 | } catch failure as alloc::AllocError { |
| 290 | throw Error::TooLarge; |
| 291 | }; |
| 292 | let mut found: ?*[u8] = nil; |
| 293 | let mut names = mem::split(listing, 0); |
| 294 | while let name = names.next() { |
| 295 | if name.len > EXTENSION.len and mem::endsWith(name, EXTENSION) { |
| 296 | if found <> nil { |
| 297 | throw Error::NotFound; |
| 298 | } |
| 299 | set found = &name[.. name.len - EXTENSION.len]; |
| 300 | } |
| 301 | } |
| 302 | let name = found else throw Error::NotFound; |
| 303 | return try file(directory, name, arena); |
| 304 | } |
| 305 | |
| 306 | /// Check whether the package version `found` satisfies the dependency |
| 307 | /// version `required`. Both are valid `MAJOR.MINOR.PATCH` versions. |
| 308 | export fn compatible(required: *[u8], found: *[u8]) -> bool { |
| 309 | let want = binary::versionParts(required) else return false; |
| 310 | let have = binary::versionParts(found) else return false; |
| 311 | if not mem::eq(want[0], have[0]) |
| 312 | or (mem::eq(want[0], "0") and not mem::eq(want[1], have[1])) |
| 313 | { |
| 314 | return false; |
| 315 | } |
| 316 | for i in 1 .. 3 { |
| 317 | if have[i].len <> want[i].len { |
| 318 | return have[i].len > want[i].len; |
| 319 | } |
| 320 | let order = mem::cmp(have[i], want[i]); |
| 321 | if order <> 0 { |
| 322 | return order > 0; |
| 323 | } |
| 324 | } |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | /// Return the location of `path`, written in the manifest at `manifest`, as |
| 329 | /// a path from the current directory. `.` components are removed, and `..` |
| 330 | /// components cancel earlier ones, so that equal locations compare equal. |
| 331 | export fn resolve(manifest: *[u8], path: *[u8], arena: &heap::Heap) -> *[u8] throws (Error) { |
| 332 | let mut parts: [*[u8]; MAX_COMPONENTS] = [""; MAX_COMPONENTS]; |
| 333 | let mut count: u32 = 0; |
| 334 | let mut directory: *[u8] = ""; |
| 335 | if let slash = mem::rfind(manifest, "/") { |
| 336 | set directory = &manifest[.. slash]; |
| 337 | } |
| 338 | let rooted = path.len > 0 and path[0] == '/'; |
| 339 | let absolute = rooted or (directory.len > 0 and directory[0] == '/'); |
| 340 | let texts: [*[u8]; 2] = [directory, path]; |
| 341 | let first: u32 = 1 if rooted else 0; |
| 342 | for text in &texts[first ..] { |
| 343 | let mut components = mem::split(text, '/'); |
| 344 | while let part = components.next() { |
| 345 | if part.len == 0 or mem::eq(part, ".") { |
| 346 | continue; |
| 347 | } |
| 348 | if mem::eq(part, "..") and count > 0 and not mem::eq(parts[count - 1], "..") { |
| 349 | set count -= 1; |
| 350 | continue; |
| 351 | } |
| 352 | if mem::eq(part, "..") and absolute and count == 0 { |
| 353 | continue; |
| 354 | } |
| 355 | if count >= MAX_COMPONENTS { |
| 356 | throw Error::TooLarge; |
| 357 | } |
| 358 | set parts[count] = part; |
| 359 | set count += 1; |
| 360 | } |
| 361 | } |
| 362 | let mut result: *mut [u8] = &mut []; |
| 363 | if absolute { |
| 364 | try result.append('/', arena) catch { |
| 365 | throw Error::TooLarge; |
| 366 | }; |
| 367 | } |
| 368 | for part, i in &parts[.. count] { |
| 369 | if i > 0 { |
| 370 | try result.append('/', arena) catch { |
| 371 | throw Error::TooLarge; |
| 372 | }; |
| 373 | } |
| 374 | try result.extend(part, arena) catch { |
| 375 | throw Error::TooLarge; |
| 376 | }; |
| 377 | } |
| 378 | if result.len == 0 { |
| 379 | return "."; |
| 380 | } |
| 381 | return &result[..]; |
| 382 | } |
| 383 | |
| 384 | /// Print a diagnostic made of `parts` at `node`, and return [`Error::Invalid`]. |
| 385 | fn invalid(r: Reader, node: *ast::Node, parts: &[*[u8]]) -> Error { |
| 386 | io::printError(r.path); |
| 387 | if let loc = scanner::getLocation( |
| 388 | scanner::SourceLoc::File(r.path), r.text, node.span.offset |
| 389 | ) { |
| 390 | let mut buffer: [u8; fmt::U32_STR_LEN] = [0; fmt::U32_STR_LEN]; |
| 391 | io::printError(":"); |
| 392 | io::printError(fmt::formatU32(loc.line as u32, &mut buffer[..])); |
| 393 | io::printError(":"); |
| 394 | io::printError(fmt::formatU32(loc.col as u32, &mut buffer[..])); |
| 395 | } |
| 396 | io::printError(": error: "); |
| 397 | for part in parts { |
| 398 | io::printError(part); |
| 399 | } |
| 400 | io::printError("\n"); |
| 401 | return Error::Invalid; |
| 402 | } |
| 403 | |
| 404 | /// Return the fields of a record literal of type `typeName`, written with |
| 405 | /// that name or without one. Each field is labeled and appears once. |
| 406 | fn recordFields(r: Reader, node: *ast::Node, typeName: *[u8]) -> *[*ast::Node] |
| 407 | throws (Error) |
| 408 | { |
| 409 | let case ast::NodeValue::RecordLit(literal) = node.value else { |
| 410 | throw invalid(r, node, &["expected a `", typeName, "` record"]); |
| 411 | }; |
| 412 | if let name = literal.typeName { |
| 413 | let mut matches = false; |
| 414 | if let case ast::NodeValue::Ident(text) = name.value { |
| 415 | set matches = mem::eq(text, typeName); |
| 416 | } |
| 417 | if not matches { |
| 418 | throw invalid(r, name, &["expected a `", typeName, "` record"]); |
| 419 | } |
| 420 | } |
| 421 | if literal.ignoreRest { |
| 422 | throw invalid(r, node, &["`..` is not allowed in a manifest"]); |
| 423 | } |
| 424 | for field, i in literal.fields { |
| 425 | let label = try fieldName(r, field); |
| 426 | for previous in &literal.fields[.. i] { |
| 427 | if mem::eq(try fieldName(r, previous), label) { |
| 428 | throw invalid(r, field, &["duplicate field `", label, "`"]); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | return literal.fields; |
| 433 | } |
| 434 | |
| 435 | /// Return the label of a record literal field. |
| 436 | fn fieldName(r: Reader, field: *ast::Node) -> *[u8] throws (Error) { |
| 437 | let case ast::NodeValue::RecordLitField(arg) = field.value |
| 438 | else panic "fieldName: expected a record literal field"; |
| 439 | let label = arg.label else throw invalid(r, field, &["expected `name: value`"]); |
| 440 | let case ast::NodeValue::Ident(text) = label.value |
| 441 | else throw invalid(r, label, &["expected a field name"]); |
| 442 | return text; |
| 443 | } |
| 444 | |
| 445 | /// Return the value of a record literal field. |
| 446 | fn fieldValue(field: *ast::Node) -> *ast::Node { |
| 447 | let case ast::NodeValue::RecordLitField(arg) = field.value |
| 448 | else panic "fieldValue: expected a record literal field"; |
| 449 | return arg.value; |
| 450 | } |
| 451 | |
| 452 | /// Copy a string literal to `arena`. |
| 453 | fn string(r: Reader, node: *ast::Node, arena: &heap::Heap) -> *[u8] throws (Error) { |
| 454 | let case ast::NodeValue::String(text) = node.value |
| 455 | else throw invalid(r, node, &["expected a string"]); |
| 456 | let copy = try alloc::copyBytes(arena, text) catch { |
| 457 | throw Error::TooLarge; |
| 458 | }; |
| 459 | return ©[..]; |
| 460 | } |
| 461 | |
| 462 | /// Return the elements of an array literal. |
| 463 | fn elements(r: Reader, node: *ast::Node) -> *[*ast::Node] throws (Error) { |
| 464 | let case ast::NodeValue::ArrayLit(items) = node.value |
| 465 | else throw invalid(r, node, &["expected an array"]); |
| 466 | return items; |
| 467 | } |
| 468 | |
| 469 | /// Check that `text` is an identifier: letters, digits and `_`, not starting |
| 470 | /// with a digit. |
| 471 | fn isIdentifier(text: *[u8]) -> bool { |
| 472 | if text.len == 0 { |
| 473 | return false; |
| 474 | } |
| 475 | for byte, i in text { |
| 476 | if not (char::isAlpha(byte) or byte == '_' or (i > 0 and char::isDigit(byte))) { |
| 477 | return false; |
| 478 | } |
| 479 | } |
| 480 | return true; |
| 481 | } |
| 482 | |
| 483 | /// Read a package name. |
| 484 | fn identifier(r: Reader, node: *ast::Node, arena: &heap::Heap) -> *[u8] throws (Error) { |
| 485 | let text = try string(r, node, arena); |
| 486 | if not isIdentifier(text) { |
| 487 | throw invalid(r, node, &["invalid package name `", text, "`"]); |
| 488 | } |
| 489 | return text; |
| 490 | } |
| 491 | |
| 492 | /// Read a `MAJOR.MINOR.PATCH` version. |
| 493 | fn version(r: Reader, node: *ast::Node, arena: &heap::Heap) -> *[u8] throws (Error) { |
| 494 | let text = try string(r, node, arena); |
| 495 | if binary::versionParts(text) == nil { |
| 496 | throw invalid(r, node, &["invalid version `", text, "`, expected MAJOR.MINOR.PATCH"]); |
| 497 | } |
| 498 | return text; |
| 499 | } |
| 500 | |
| 501 | /// Read a clean relative path below the package root. |
| 502 | fn relativePath(r: Reader, node: *ast::Node, arena: &heap::Heap) -> *[u8] throws (Error) { |
| 503 | let text = try string(r, node, arena); |
| 504 | if not fs::isNormalRelativePath(text) { |
| 505 | throw invalid(r, node, &["path `", text, "` is not a clean path below the package root"]); |
| 506 | } |
| 507 | return text; |
| 508 | } |
| 509 | |
| 510 | /// Read an array of paths below the package root. |
| 511 | fn paths(r: Reader, node: *ast::Node, arena: &heap::Heap) -> *[*[u8]] throws (Error) { |
| 512 | let mut result: *mut [*[u8]] = &mut []; |
| 513 | for item in try elements(r, node) { |
| 514 | let text = try relativePath(r, item, arena); |
| 515 | try result.append(text, arena) catch { |
| 516 | throw Error::TooLarge; |
| 517 | }; |
| 518 | } |
| 519 | return &result[..]; |
| 520 | } |
| 521 | |
| 522 | /// Read an array of module names, `::`-separated identifiers. |
| 523 | fn moduleNames(r: Reader, node: *ast::Node, arena: &heap::Heap) -> *[*[u8]] throws (Error) { |
| 524 | let mut result: *mut [*[u8]] = &mut []; |
| 525 | for item in try elements(r, node) { |
| 526 | let text = try string(r, item, arena); |
| 527 | let mut valid = true; |
| 528 | let mut start: u32 = 0; |
| 529 | loop { |
| 530 | let rest = &text[start ..]; |
| 531 | let end = mem::find(rest, "::") else { |
| 532 | set valid = valid and isIdentifier(rest); |
| 533 | break; |
| 534 | }; |
| 535 | set valid = valid and isIdentifier(&rest[.. end]); |
| 536 | set start += end + 2; |
| 537 | } |
| 538 | if not valid { |
| 539 | throw invalid(r, item, &["invalid module name `", text, "`"]); |
| 540 | } |
| 541 | try result.append(text, arena) catch { |
| 542 | throw Error::TooLarge; |
| 543 | }; |
| 544 | } |
| 545 | return &result[..]; |
| 546 | } |
| 547 | |
| 548 | /// Return the parent of module `name`, or `nil` for a child of the root module. |
| 549 | fn parentModule(name: *[u8]) -> ?*[u8] { |
| 550 | let end = mem::rfind(name, "::") else return nil; |
| 551 | return &name[.. end]; |
| 552 | } |
| 553 | |
| 554 | /// Check whether `names` contains `name`. |
| 555 | fn contains(names: *[*[u8]], name: *[u8]) -> bool { |
| 556 | for item in names { |
| 557 | if mem::eq(item, name) { |
| 558 | return true; |
| 559 | } |
| 560 | } |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | /// Check that each module name appears once in `modules` and `tests`, and |
| 565 | /// that the parent of a module is listed. The parent of a test module can |
| 566 | /// also be a test module. |
| 567 | fn checkModules(r: Reader, root: *ast::Node, modules: *[*[u8]], tests: *[*[u8]]) |
| 568 | throws (Error) |
| 569 | { |
| 570 | for name, i in modules { |
| 571 | if contains(&modules[.. i], name) { |
| 572 | throw invalid(r, root, &["module `", name, "` is listed twice"]); |
| 573 | } |
| 574 | if let parent = parentModule(name); not contains(modules, parent) { |
| 575 | throw invalid(r, root, &[ |
| 576 | "module `", name, "` is listed without its parent `", parent, "`" |
| 577 | ]); |
| 578 | } |
| 579 | } |
| 580 | for name, i in tests { |
| 581 | if contains(&tests[.. i], name) or contains(modules, name) { |
| 582 | throw invalid(r, root, &["module `", name, "` is listed twice"]); |
| 583 | } |
| 584 | if let parent = parentModule(name); |
| 585 | not contains(modules, parent) and not contains(tests, parent) |
| 586 | { |
| 587 | throw invalid(r, root, &[ |
| 588 | "module `", name, "` is listed without its parent `", parent, "`" |
| 589 | ]); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | /// Put each listed parent before its children. `earlier` holds parents |
| 595 | /// already registered from another list. |
| 596 | fn orderModules(names: *[*[u8]], earlier: *[*[u8]], arena: &heap::Heap) -> *[*[u8]] |
| 597 | throws (Error) |
| 598 | { |
| 599 | let mut ordered: *mut [*[u8]] = &mut []; |
| 600 | try ordered.reserve(names.len, arena) catch { |
| 601 | throw Error::TooLarge; |
| 602 | }; |
| 603 | for name in names { |
| 604 | try orderModule(name, earlier, &mut ordered, arena); |
| 605 | } |
| 606 | return &ordered[..]; |
| 607 | } |
| 608 | |
| 609 | /// Add a listed module and any of its parents not yet in order. |
| 610 | fn orderModule(name: *[u8], earlier: *[*[u8]], ordered: &mut *mut [*[u8]], arena: &heap::Heap) |
| 611 | throws (Error) |
| 612 | { |
| 613 | if contains(&ordered[..], name) or contains(earlier, name) { |
| 614 | return; |
| 615 | } |
| 616 | if let parent = parentModule(name) { |
| 617 | try orderModule(parent, earlier, ordered, arena); |
| 618 | } |
| 619 | try ordered.append(name, arena) catch { |
| 620 | throw Error::TooLarge; |
| 621 | }; |
| 622 | } |
| 623 | |
| 624 | /// Read an array of dependencies. Each provider name appears once. |
| 625 | fn dependencies(r: Reader, node: *ast::Node, arena: &heap::Heap) -> *[Dependency] |
| 626 | throws (Error) |
| 627 | { |
| 628 | let mut result: *mut [Dependency] = &mut []; |
| 629 | for item in try elements(r, node) { |
| 630 | let entry = try dependency(r, item, arena); |
| 631 | for i in 0 .. result.len { |
| 632 | if mem::eq(result[i].name, entry.name) { |
| 633 | throw invalid(r, item, &["duplicate dependency `", entry.name, "`"]); |
| 634 | } |
| 635 | } |
| 636 | try result.append(entry, arena) catch { |
| 637 | throw Error::TooLarge; |
| 638 | }; |
| 639 | } |
| 640 | return &result[..]; |
| 641 | } |
| 642 | |
| 643 | /// Read one dependency record. |
| 644 | fn dependency(r: Reader, node: *ast::Node, arena: &heap::Heap) -> Dependency throws (Error) { |
| 645 | let mut name: ?*[u8] = nil; |
| 646 | let mut required: ?*[u8] = nil; |
| 647 | let fields = try recordFields(r, node, "Dependency"); |
| 648 | for field in fields { |
| 649 | let label = try fieldName(r, field); |
| 650 | let item = fieldValue(field); |
| 651 | if mem::eq(label, "name") { |
| 652 | set name = try identifier(r, item, arena); |
| 653 | } else if mem::eq(label, "version") { |
| 654 | set required = try version(r, item, arena); |
| 655 | } else { |
| 656 | throw invalid(r, field, &["unknown field `", label, "`"]); |
| 657 | } |
| 658 | } |
| 659 | let found = name else throw invalid(r, node, &["missing field `name`"]); |
| 660 | let oldest = required else throw invalid(r, node, &["missing field `version`"]); |
| 661 | return Dependency { name: found, version: oldest }; |
| 662 | } |