Initial Commit
This commit is contained in:
782 files changed
+145156
No files matched your search
@@ -0,0 +1,329 @@
|
||||
#! @file text.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how the server writes and reads what the protocol carries: JSON,
|
||||
#! written whatever a file holds, positions, whose characters are counted in
|
||||
#! UTF-16 code units, the `file:` URIs files are named by, and the Markdown
|
||||
#! documentation is shown in.
|
||||
|
||||
import std::net;
|
||||
import std::json;
|
||||
import std::serial;
|
||||
import sakura::analyzer;
|
||||
module sakura::server;
|
||||
|
||||
|
||||
#! A writer of the JSON the server sends. It is lossy, writing bytes that are not
|
||||
#! valid UTF-8 as U+FFFD, so a message stays valid whatever a file holds.
|
||||
#! @returns The writer, which holds nothing yet.
|
||||
pub def message_writer(): json::writer {
|
||||
var made := json::writer::new();
|
||||
made.set_lossy(true);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Appends what a writer wrote, once its value is written whole.
|
||||
#! @param out Where it is appended.
|
||||
#! @param written The writer, which a lossy writer given nothing but integers
|
||||
#! and text never fails at.
|
||||
pub def append_written(out: &mut string, written: &mut json::writer): void {
|
||||
val text := written.finish() else (_) { return; };
|
||||
out.append(text);
|
||||
}
|
||||
|
||||
#! Writes Markdown as the protocol's content of that kind, as
|
||||
#! `{"kind":"markdown","value":"..."}`.
|
||||
#! @param out Where it is written.
|
||||
#! @param markdown The Markdown.
|
||||
pub def write_markdown(out: &mut json::writer, markdown: strview): void {
|
||||
out.begin_object();
|
||||
out.member("kind"); out.text("markdown");
|
||||
out.member("value"); out.text(markdown);
|
||||
out.end_object();
|
||||
}
|
||||
|
||||
#! A position as the protocol gives one: a line, and a character counted in
|
||||
#! UTF-16 code units, both from 0.
|
||||
pub struct position {
|
||||
pub var line: u32; #! The line, from 0.
|
||||
pub var character: u32; #! The UTF-16 code unit within it, from 0.
|
||||
}
|
||||
|
||||
#! The byte of the text a position of the protocol is at.
|
||||
#! @param text The text.
|
||||
#! @param line The line, from 0.
|
||||
#! @param character The UTF-16 code unit within it, from 0.
|
||||
#! @returns The byte, which is the end of the line where the line is shorter.
|
||||
pub def locate(text: strview, line: u64, character: u64): u32 {
|
||||
var offset: u64 = 0;
|
||||
var at: u64 = 0;
|
||||
while at < line && offset < text.length {
|
||||
if text[offset] == '\n' {
|
||||
at++;
|
||||
}
|
||||
offset++;
|
||||
}
|
||||
var units: u64 = 0;
|
||||
return count_units(text, offset, text.length, character, &mut units) as u32;
|
||||
}
|
||||
|
||||
#! The position of the protocol a byte of the text is at.
|
||||
#! @param text The text.
|
||||
#! @param starts Where each line of the text begins, as its source found.
|
||||
#! @param offset The byte.
|
||||
#! @returns The position.
|
||||
pub def position_of(text: strview, starts: &lines, offset: u32): position {
|
||||
val end := (offset as u64) < text.length ? offset as u64 : text.length;
|
||||
val at := starts.place_of(end as u32);
|
||||
var units: u64 = 0;
|
||||
count_units(text, end + 1 - at.column as u64, end, 0xFFFFFFFFFFFFFFFF, &mut units);
|
||||
return position { line = at.line - 1, character = units as u32 };
|
||||
}
|
||||
|
||||
#! Counts the UTF-16 code units of a line's characters from a byte on, until a
|
||||
#! byte, a count of units, or the line's end, whichever comes first. A character
|
||||
#! of four bytes is two units, and a byte that breaks UTF-8 counts as a character
|
||||
#! of its own.
|
||||
#! @param text The text.
|
||||
#! @param from The byte the characters begin at.
|
||||
#! @param to The byte they end before, at the latest.
|
||||
#! @param most How many units are counted, at most.
|
||||
#! @param units Where the count is written.
|
||||
#! @returns The byte the characters counted end before.
|
||||
def count_units(text: strview, from: u64, to: u64, most: u64, units: &mut u64): u64 {
|
||||
var at := from;
|
||||
units = 0;
|
||||
while at < to && text[at] != '\n' && units < most {
|
||||
val size := serial::utf8_length(text, at);
|
||||
units += size == 4 ? 2 : 1;
|
||||
at += size != 0 ? size : 1;
|
||||
}
|
||||
return at;
|
||||
}
|
||||
|
||||
#! Writes a position of the protocol, as `{"line":0,"character":4}`.
|
||||
#! @param out Where it is written.
|
||||
#! @param text The text the position is of.
|
||||
#! @param starts Where each line of the text begins.
|
||||
#! @param offset The byte it is at.
|
||||
pub def write_position(out: &mut json::writer, text: strview, starts: &lines, offset: u32): void {
|
||||
val at := position_of(text, starts, offset);
|
||||
out.begin_object();
|
||||
out.member("line"); out.unsigned(at.line as u64);
|
||||
out.member("character"); out.unsigned(at.character as u64);
|
||||
out.end_object();
|
||||
}
|
||||
|
||||
#! Writes a range of the protocol, from one byte of the text to another.
|
||||
#! @param out Where it is written.
|
||||
#! @param text The text.
|
||||
#! @param starts Where each line of the text begins.
|
||||
#! @param start The byte it begins at.
|
||||
#! @param end The byte it ends before.
|
||||
pub def write_range(out: &mut json::writer, text: strview, starts: &lines, start: u32, end: u32): void {
|
||||
out.begin_object();
|
||||
out.member("start"); write_position(out, text, starts, start);
|
||||
out.member("end"); write_position(out, text, starts, end);
|
||||
out.end_object();
|
||||
}
|
||||
|
||||
when os == linux {
|
||||
#! The path a `file:` URI names, as files of this machine are named, with an
|
||||
#! empty authority or `localhost`. A folder given with a trailing slash is
|
||||
#! named without it, so paths compare alike. A `%` that two hexadecimal digits
|
||||
#! do not follow is kept as it is.
|
||||
#! @param uri The URI.
|
||||
#! @returns The path, or nothing where the URI is of another scheme.
|
||||
pub def uri_path(uri: strview): maybe<string> {
|
||||
return nothing {} if !uri.starts_with("file://");
|
||||
var at: u64 = 7;
|
||||
while at < uri.length && uri[at] != '/' {
|
||||
at++;
|
||||
}
|
||||
return nothing {} if at >= uri.length;
|
||||
|
||||
var made := uri::decode_lenient(uri.substr(at, uri.length - at));
|
||||
while made.length > 1 && made[made.length - 1] == '/' {
|
||||
made = string::from(made.substr(0, made.length - 1), nothing {});
|
||||
}
|
||||
return move(&mut made);
|
||||
}
|
||||
|
||||
#! Appends the `file:` URI of a path, its bytes escaped where a URI does not
|
||||
#! take them as they are.
|
||||
#! @param out Where it is written.
|
||||
#! @param path The path.
|
||||
pub def append_uri(out: &mut string, path: strview): void {
|
||||
val escaped := uri::encode(path, "/");
|
||||
out.append("file://");
|
||||
out.append(escaped.span);
|
||||
}
|
||||
}
|
||||
|
||||
when os == windows {
|
||||
#! The path a `file:` URI names, spelled as `canonical_path` spells paths, so
|
||||
#! paths compare alike however the editor spells a file. An empty authority,
|
||||
#! or `localhost` before the path of a drive, names this machine, and any
|
||||
#! other the server of a share, whose files are `//server/share/...`: VS Code
|
||||
#! names `\\localhost\c$\x` `file://localhost/c%24/x`, and escapes the name of
|
||||
#! a server as it does the path, as `file://wsl%24/x`. A `%` that two
|
||||
#! hexadecimal digits do not follow is kept as it is.
|
||||
#! @param uri The URI.
|
||||
#! @returns The path, or nothing where the URI is of another scheme, or names
|
||||
#! no path.
|
||||
pub def uri_path(uri: strview): maybe<string> {
|
||||
return nothing {} if !uri.starts_with("file://");
|
||||
val decoded := uri::decode_lenient(uri.substr(7, uri.length - 7));
|
||||
var made := string::from("//", nothing {});
|
||||
made.append(decoded.span);
|
||||
|
||||
# `/c:/x`, as VS Code writes `file:///c%3A/x`, is the path `c:/x` of a
|
||||
# drive, and `/c:` is its root, `c:/`, since `c:` alone is the drive's
|
||||
# working directory.
|
||||
val text := made.span;
|
||||
var named: u64 = 2;
|
||||
while named < text.length && text[named] != '/' {
|
||||
named++;
|
||||
}
|
||||
val authority := text.substr(2, named - 2);
|
||||
val drive := named < text.length && is_drive(text, named + 1);
|
||||
val local := authority.length == 0 || (drive && authority.lower().span == "localhost");
|
||||
val start: u64 = local ? named + (drive ? 1 : 0) : 0;
|
||||
var path := string::from(text.substr(start, text.length - start), nothing {});
|
||||
if path.length == 2 && is_drive(path.span, 0) {
|
||||
path.push('/');
|
||||
}
|
||||
return canonical_path(path.span);
|
||||
}
|
||||
|
||||
#! Appends the `file:` URI of a path, as VS Code spells it: `file:///c%3A/x`
|
||||
#! for `C:/x`, and `file://server/x` for `//server/x`, the bytes of the rest
|
||||
#! escaped where a URI does not take them as they are.
|
||||
#! @param out Where it is written.
|
||||
#! @param path The path.
|
||||
pub def append_uri(out: &mut string, path: strview): void {
|
||||
out.append("file://");
|
||||
var from: u64 = 0;
|
||||
if path.starts_with("//") {
|
||||
from = 2;
|
||||
} else if is_drive(path, 0) {
|
||||
val letter: u8 = path[0];
|
||||
out.push('/');
|
||||
out.push(letter >= 'A' && letter <= 'Z' ? letter + 32 : letter);
|
||||
out.append("%3A");
|
||||
from = 2;
|
||||
}
|
||||
val escaped := uri::encode(path.substr(from, path.length - from), "/");
|
||||
out.append(escaped.span);
|
||||
}
|
||||
|
||||
# Whether text from a byte on begins with the letter of a drive and its
|
||||
# colon, as `C:`.
|
||||
def is_drive(text: strview, at: u64): bool {
|
||||
return false if at + 2 > text.length;
|
||||
val letter: u8 = text[at];
|
||||
return ((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')) && text[at + 1] == ':';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#! Whether a file's name is that of Sakura source, ending in `.sakura`.
|
||||
#! @param name The name, or a path.
|
||||
#! @returns Whether it is.
|
||||
pub def is_sakura(name: strview): bool {
|
||||
return name.length > 7 && name.ends_with(".sakura");
|
||||
}
|
||||
|
||||
#! Appends a documentation comment as Markdown. Doxygen commands, as `@param`,
|
||||
#! begin paragraphs of their own, and the lines of a paragraph are joined.
|
||||
#! @param out Where it is written.
|
||||
#! @param documentation The comment, without its markers.
|
||||
pub def append_markdown(out: &mut string, documentation: strview): void {
|
||||
var first := true;
|
||||
var blank := false;
|
||||
var line: u64 = 0;
|
||||
while line < documentation.length {
|
||||
var stop := line;
|
||||
while stop < documentation.length && documentation[stop] != '\n' {
|
||||
stop++;
|
||||
}
|
||||
var at := line;
|
||||
while at < stop && (documentation[at] == ' ' || documentation[at] == '\t') {
|
||||
at++;
|
||||
}
|
||||
line = stop + 1;
|
||||
if at == stop {
|
||||
blank = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (documentation[at] == '@' || documentation[at] == '\\') && at + 1 < stop && is_word_byte(documentation[at + 1]) {
|
||||
at++;
|
||||
val name := at;
|
||||
while at < stop && is_word_byte(documentation[at]) {
|
||||
at++;
|
||||
}
|
||||
val command := documentation.substr(name, at - name);
|
||||
if !first {
|
||||
out.append("\n\n");
|
||||
}
|
||||
val param := command == "param" || command == "tparam";
|
||||
if param && at < stop && documentation[at] == '[' {
|
||||
while at < stop && documentation[at] != ']' {
|
||||
at++;
|
||||
}
|
||||
at++;
|
||||
}
|
||||
while at < stop && (documentation[at] == ' ' || documentation[at] == '\t') {
|
||||
at++;
|
||||
}
|
||||
|
||||
if command == "brief" {
|
||||
out.append(documentation.substr(at, stop - at));
|
||||
} else if param {
|
||||
val word := at;
|
||||
while at < stop && is_word_byte(documentation[at]) {
|
||||
at++;
|
||||
}
|
||||
out.append("*@");
|
||||
out.append(command);
|
||||
out.append("* `");
|
||||
out.append(documentation.substr(word, at - word));
|
||||
out.append("`");
|
||||
while at < stop && (documentation[at] == ' ' || documentation[at] == '\t') {
|
||||
at++;
|
||||
}
|
||||
if at < stop {
|
||||
out.append(" \xE2\x80\x94 ");
|
||||
}
|
||||
out.append(documentation.substr(at, stop - at));
|
||||
} else {
|
||||
out.append("*@");
|
||||
out.append(command);
|
||||
out.append("*");
|
||||
if at < stop {
|
||||
out.append(" \xE2\x80\x94 ");
|
||||
}
|
||||
out.append(documentation.substr(at, stop - at));
|
||||
}
|
||||
} else {
|
||||
if !first {
|
||||
out.append(blank ? "\n\n" : " ");
|
||||
}
|
||||
out.append(documentation.substr(at, stop - at));
|
||||
}
|
||||
first = false;
|
||||
blank = false;
|
||||
}
|
||||
}
|
||||
|
||||
#! Appends a signature as a block of Sakura code in Markdown.
|
||||
#! @param out Where it is written.
|
||||
#! @param signature The signature.
|
||||
pub def append_code(out: &mut string, signature: strview): void {
|
||||
out.append("```sakura\n");
|
||||
out.append(signature);
|
||||
out.append("\n```");
|
||||
}
|
||||
Reference in new issue
Block a user