Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1,227 @@
|
||||
#! @file source.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the source type, which is the text of a file and where its lines
|
||||
#! begin, read from disk or given from memory, along with the classes of bytes
|
||||
#! the lexer reads the text by.
|
||||
|
||||
import std::io;
|
||||
import core::swar;
|
||||
module sakura::analyzer;
|
||||
|
||||
#! Represents the source information for a file.
|
||||
#!
|
||||
#! The source keeps the path to the file, the loaded text of the file, and a
|
||||
#! `lines` vector to indicate where lines start within the source text, which
|
||||
#! it finds itself as it is made, so the text is never changed after. Each line
|
||||
#! break Windows writes, CR LF, is kept as the LF alone, as other systems write
|
||||
#! one, so the lexer, the lines, and a literal spanning lines read the same
|
||||
#! text whichever system wrote the file.
|
||||
pub struct source {
|
||||
pub var path: strview; #! The path the file is reported as.
|
||||
var text: string; #! The file's text.
|
||||
pub var lines: lines; #! Where each line of the text begins.
|
||||
}
|
||||
|
||||
#! Provides the length of the source text.
|
||||
#! @param this The source object to get the length of its text.
|
||||
#! @returns The length of the source text.
|
||||
pub val source::length: u64 {
|
||||
get => this.text.length;
|
||||
}
|
||||
|
||||
#! Creates a new source object by opening the file it points to and reading it.
|
||||
#! @param path The path to the source file to open.
|
||||
#! @returns The source object containing the file contents read.
|
||||
pub def source::open(path: strview): source {
|
||||
var result := source {
|
||||
path = path,
|
||||
text = without_mark(source::read(path)),
|
||||
lines = lines::new(nothing {})
|
||||
};
|
||||
result.finish();
|
||||
return result;
|
||||
}
|
||||
|
||||
# Reads the whole of the file at a path, giving empty text where it cannot be.
|
||||
def source::read(text: strview): string {
|
||||
val at := path::parse(text) else (_) { return string::new(nothing {}); };
|
||||
return read_file(at, nothing {}) else string::new(nothing {});
|
||||
}
|
||||
|
||||
# Finds the lines of the text, which finds whether it holds a CR, so only text
|
||||
# holding one is read again, made without its CR LFs, and its lines found again.
|
||||
def source::finish(&mut this): void {
|
||||
return if !this.find_lines();
|
||||
val held := this.text.length;
|
||||
this.text = without_returns(move(&mut this.text));
|
||||
if this.text.length != held {
|
||||
this.lines.clear();
|
||||
_ = this.find_lines();
|
||||
}
|
||||
}
|
||||
|
||||
# The text without the byte order mark some editors begin a file of UTF-8 with,
|
||||
# which is no part of the language, or the text itself where it has none.
|
||||
def without_mark(text: string): string {
|
||||
val bytes := text.view();
|
||||
return text if bytes.length < 3 || bytes[0] != 0xEF || bytes[1] != 0xBB || bytes[2] != 0xBF;
|
||||
return string::from(bytes[3:], nothing {});
|
||||
}
|
||||
|
||||
# The text with each CR LF line break made the LF alone, or the text itself
|
||||
# where it has none.
|
||||
def without_returns(text: string): string {
|
||||
val bytes := text.view();
|
||||
var found := false;
|
||||
for i in 1..bytes.length {
|
||||
if bytes[i] == '\n' && bytes[i - 1] == '\r' {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return text if !found;
|
||||
|
||||
var made := string::new(nothing {});
|
||||
made.reserve(bytes.length);
|
||||
for i in 0..bytes.length {
|
||||
if bytes[i] != '\r' || i + 1 == bytes.length || bytes[i + 1] != '\n' {
|
||||
made.push(bytes[i]);
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Finds where each line of the text begins: at its start, and past each line
|
||||
#! break, and whether the text holds a CR, which a CR LF line break begins
|
||||
#! with.
|
||||
#! @param this The source file.
|
||||
#! @returns Whether the text holds a CR.
|
||||
def source::find_lines(&mut this): bool {
|
||||
val text := this.text.view().data;
|
||||
val size := this.text.length;
|
||||
this.lines.reserve(size / 24 + 2);
|
||||
this.lines.append(0);
|
||||
|
||||
# Where vectors are held in registers, sixteen bytes at a time: the bits of
|
||||
# the bytes that are `\n`, each found from the lowest. Then eight at a time,
|
||||
# as words, each `\n` found from the first of the mask of them. The bytes
|
||||
# that are `\r` are gathered as they go.
|
||||
var at: u64 = 0;
|
||||
var returns := false;
|
||||
if lanes_held() {
|
||||
val newline := u8x16::splat('\n');
|
||||
val back := u8x16::splat('\r');
|
||||
var held: u32 = 0;
|
||||
while at + 16 <= size {
|
||||
val chunk: u8x16 = *((text + at) as *u8x16);
|
||||
var marks := chunk.equal(&newline).bits();
|
||||
while marks != 0 {
|
||||
this.lines.append((at + trailing_zeros(marks) as u64 + 1) as u32);
|
||||
marks = marks & (marks - 1);
|
||||
}
|
||||
held |= chunk.equal(&back).bits();
|
||||
at += 16;
|
||||
}
|
||||
returns = held != 0;
|
||||
}
|
||||
while at + 8 <= size {
|
||||
val word := swar::load(text + at);
|
||||
var marks := word.equal('\n');
|
||||
while marks.any() {
|
||||
this.lines.append((at + marks.first() + 1) as u32);
|
||||
marks = marks.rest();
|
||||
}
|
||||
returns = returns || word.equal('\r').any();
|
||||
at += 8;
|
||||
}
|
||||
for k in at..size {
|
||||
if text[k] == '\n' {
|
||||
this.lines.append((k + 1) as u32);
|
||||
}
|
||||
returns = returns || text[k] == '\r';
|
||||
}
|
||||
return returns;
|
||||
}
|
||||
|
||||
#! Creates a lexeme from the given start location in the source and the count.
|
||||
#! @param this The source object to create a lexeme from.
|
||||
#! @param start The starting index within the source to create the lexeme.
|
||||
#! @param count The number of characters for the lexeme to be created.
|
||||
#! @returns A strview of the lexeme requested from the source.
|
||||
pub def source::make_lexeme(&this, start: u64, count: u64): strview {
|
||||
return this.text.view().substr(start, count);
|
||||
}
|
||||
|
||||
#! The classes a byte may be of, as bits of `char_classes`.
|
||||
enum cc : u8 {
|
||||
space = 0x01, #! Spacing between tokens: ` `, and `\t` through `\r`.
|
||||
start = 0x02, #! What a word begins with: a letter or `_`.
|
||||
word = 0x04, #! What a word goes on with: a letter, a digit, or `_`.
|
||||
digit = 0x08, #! What a number begins with.
|
||||
}
|
||||
|
||||
#! The classes of each byte, as bits of `cc`, worked out while compiling, so the
|
||||
#! lexer asks what a byte is with one load.
|
||||
const char_classes: u8[256] = make_char_classes();
|
||||
|
||||
#! Whether a byte goes on a name: a letter, a digit, or `_`.
|
||||
#! @param byte The byte.
|
||||
#! @returns Whether it does.
|
||||
pub def is_word_byte(byte: u8): bool {
|
||||
return (char_classes[byte as u64] & cc::word as u8) != 0;
|
||||
}
|
||||
|
||||
const def make_char_classes(): u8[256] {
|
||||
var made: u8[256];
|
||||
for each in 0..256 {
|
||||
val byte := each as u8;
|
||||
if byte == ' ' || (byte >= '\t' && byte <= '\r') {
|
||||
made[each] |= cc::space as u8;
|
||||
}
|
||||
if (byte >= 'a' && byte <= 'z') || (byte >= 'A' && byte <= 'Z') || byte == '_' {
|
||||
made[each] |= cc::start as u8 | cc::word as u8;
|
||||
}
|
||||
if byte >= '0' && byte <= '9' {
|
||||
made[each] |= cc::digit as u8 | cc::word as u8;
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# A set of character boolean checks that are specific to sakura.
|
||||
|
||||
val u8::is_cmnt: bool { get => this == '#'; }
|
||||
val u8::is_dot: bool { get => this == '.'; }
|
||||
val u8::is_zero: bool { get => this == '0'; }
|
||||
val u8::is_squote: bool { get => this == '\''; }
|
||||
val u8::is_dquote: bool { get => this == '\"'; }
|
||||
val u8::is_format: bool { get => this == '$'; }
|
||||
val u8::is_escape: bool { get => this == '\\'; }
|
||||
val u8::is_lbrace: bool { get => this == '{'; }
|
||||
val u8::is_rbrace: bool { get => this == '}'; }
|
||||
val u8::is_brace: bool { get => this.is_lbrace || this.is_rbrace; }
|
||||
val u8::is_blank: bool { get => this == ' ' || this == '\t'; }
|
||||
|
||||
#! Whether the byte continues a character of more than one byte, which UTF-8
|
||||
#! writes as `10xxxxxx`.
|
||||
#! @param this The byte.
|
||||
#! @returns Whether it does.
|
||||
val u8::is_continuation: bool { get => (this & 0xC0) == 0x80; }
|
||||
|
||||
#! Creates a source object of text held in memory rather than read from a file,
|
||||
#! as an editor's unsaved buffer or a test's program.
|
||||
#! @param path The path to report the text as.
|
||||
#! @param text The text.
|
||||
#! @returns The source object holding a copy of the text.
|
||||
pub def source::from(path: strview, text: strview): source {
|
||||
var result := source {
|
||||
path = path,
|
||||
text = without_mark(string::from(text, nothing {})),
|
||||
lines = lines::new(nothing {})
|
||||
};
|
||||
result.finish();
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user