source/internal/GenerateBootstrapSeed.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
faf4a14fa3fc7e4adf8bdbff45383f9404c628ad- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
//! I190-A stage-zero maintenance entry point of the frontend seed generator.
//!
//! Maintainer-only direct-file program: it is not a MODULE initializer, no
//! Standard Library MODULE or SOURCE imports it, and it is never part of the
//! bootstrap inventory. Run it from the repository root by an explicitly
//! identified stage-zero `protos` executable whose own Standard Library
//! provides `std:source/parser`, with a Filesystem grant over the repository
//! root:
//!
//! protos --allow-filesystem=. \
//! protos/lib/source/internal/GenerateBootstrapSeed.protos \
//! <inventory> <output>
//!
//! Both arguments are "/"-separated paths relative to the granted root. The
//! inventory lists one exact repository-relative source identity per LF-ended
//! line. Every source is read as exact UTF-8 bytes (an initial byte order mark
//! and the original line endings are kept), the pure `SeedEmitter.emit`
//! generates the Java seed, and the complete result is written to `<output>`.
//! Any missing input, invalid inventory, duplicate identity, recognition
//! failure or incomplete generation fails explicitly before anything is
//! written. Only this entry point uses I/O authority; the generator does not.
SeedEmitter: import("./bootstrap/SeedEmitter.protos")
Files: import("std:io/files")
writer: TextWriter(process.stdout(), process.stdoutEncoding())
report: (line) => {
writer.writeLine(line).value()
null
}
stop: (reason) => {
report("GENERATE FAIL " + reason)
fail()
}
// Exact String of UTF-8 `bytes`. The standard decoder consumes an initial
// byte order mark, so an initial U+FEFF is decoded behind one synthetic mark
// and kept. Line endings are never normalized.
exactText: (bytes) => {
buffer: bytes
if((bytes.size() >= 3) && (bytes[0] == 239) && (bytes[1] == 187) && (bytes[2] == 191)) {
buffer = Encoding.UTF8.encode("")
buffer.add(239)
buffer.add(187)
buffer.add(191)
for(octet in bytes) {
buffer.add(octet)
}
}
Encoding.UTF8.decode(buffer)
}
// The relative Path named by "/"-separated `relative`.
relativePath: (relative) => {
path: Path.relative()
segment: Encoding.UTF8.encode("")
for(octet in Encoding.UTF8.encode(relative + "/")) {
if(octet == 47) {
(segment.size() == 0).ifFalse(() => {
path = path.child(Encoding.UTF8.decode(segment))
})
segment = Encoding.UTF8.encode("")
} else {
segment.add(octet)
}
}
path
}
// Exact bytes of the granted file at `relative`, failing explicitly.
grantedBytes: (relative) => {
result: null
Error.handle(() => {
result = Files.readAllBytes(filesystem, relativePath(relative)).value()
}, (error) => {
stop("missing or unreadable input " + relative)
})
result
}
// The identities of an LF-ended inventory, in order. Empty lines, carriage
// returns and a missing final LF are rejected.
inventoryLines: (bytes) => {
lines: []
(bytes.size() > 0).ifFalse(() => {
stop("empty inventory")
})
(bytes[bytes.size() - 1] == 10).ifFalse(() => {
stop("inventory must end with LF")
})
line: Encoding.UTF8.encode("")
for(octet in bytes) {
(octet == 13).ifTrue(() => {
stop("inventory must not contain CR")
})
if(octet == 10) {
(line.size() > 0).ifFalse(() => {
stop("inventory contains an empty line")
})
lines = [...lines, Encoding.UTF8.decode(line)]
line = Encoding.UTF8.encode("")
} else {
line.add(octet)
}
}
lines
}
arguments: process.args()
(arguments.size() == 2).ifFalse(() => {
stop("usage: GenerateBootstrapSeed.protos <inventory> <output>")
})
inventory: arguments[0]
output: arguments[1]
identities: inventoryLines(grantedBytes(inventory))
seen: Map()
for(identity in identities) {
seen.containsKey(identity).ifTrue(() => {
stop("duplicate identity " + identity)
})
seen[identity] = true
}
texts: []
for(identity in identities) {
texts = [...texts, exactText(grantedBytes(identity))]
}
java: SeedEmitter.emit(identities, texts)
(SeedEmitter.isComplete(java, identities.size())).ifFalse(() => {
stop("incomplete or non-ASCII generation")
})
Files.writeAllBytes(filesystem, relativePath(output), Encoding.UTF8.encode(java)).value()
report("GENERATED " + SeedEmitter.GENERATOR + " " + SeedEmitter.CONTRACT)
report("GENERATE PASS")