205 lines
9.6 KiB
Plaintext
205 lines
9.6 KiB
Plaintext
#! @file lexer.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the tests of the lexer: the kinds of tokens text makes, where they
|
|
#! are, and what it reports.
|
|
|
|
import std::testing;
|
|
module sakura::analyzer;
|
|
|
|
|
|
@test def keywords_lex_as_their_kinds(t: &mut tester): void {
|
|
t.expects(lexes_as("def mut import continue null", tk::kw_def, tk::kw_mut, tk::kw_imp, tk::kw_cont, tk::kw_null));
|
|
t.expects(lexes_as("is not as where when", tk::kw_is, tk::kw_not, tk::kw_as, tk::kw_where, tk::kw_when));
|
|
}
|
|
|
|
@test def words_keep_their_ids(t: &mut tester): void {
|
|
val lexing := lex("this get set type_of thing thing");
|
|
val syntax := &lexing.syntax;
|
|
t.expects(syntax.token(0, &lexing.names).kind == tk::name && syntax.token(0, &lexing.names).name == names::this_id);
|
|
t.expects(syntax.token(1, &lexing.names).name == names::get_id);
|
|
t.expects(syntax.token(2, &lexing.names).name == names::set_id);
|
|
t.expects(syntax.token(3, &lexing.names).name == names::type_of_id);
|
|
|
|
# A name is interned once, so each place it is written has the same ID.
|
|
t.expects(syntax.token(4, &lexing.names).name == syntax.token(5, &lexing.names).name && syntax.token(4, &lexing.names).name > names::type_of_id);
|
|
t.expects(syntax.token(6, &lexing.names).kind == tk::eof);
|
|
}
|
|
|
|
@test def tokens_know_where_they_are(t: &mut tester): void {
|
|
val lexing := lex("val name\n >>= 12u");
|
|
val syntax := &lexing.syntax;
|
|
t.expects(syntax.token(0, &lexing.names).offset == 0 && syntax.token(0, &lexing.names).length == 3);
|
|
t.expects(syntax.token(1, &lexing.names).offset == 5 && syntax.token(1, &lexing.names).length == 4);
|
|
t.expects(syntax.token(2, &lexing.names).offset == 12 && syntax.token(2, &lexing.names).length == 3);
|
|
|
|
# `>>=` is followed by tokens of what is left of it, `>=` and `=`.
|
|
t.expects(syntax.token(3, &lexing.names).offset == 13 && syntax.token(3, &lexing.names).length == 2);
|
|
t.expects(syntax.token(4, &lexing.names).offset == 14 && syntax.token(4, &lexing.names).length == 1);
|
|
t.expects(syntax.token(5, &lexing.names).offset == 16 && syntax.token(5, &lexing.names).length == 3);
|
|
t.expects(lexing.source.lines.length == 2);
|
|
}
|
|
|
|
@test def sources_find_where_their_lines_begin(t: &mut tester): void {
|
|
val made := source::from("lines.sakura", "one\n\nthree\n");
|
|
t.expects(made.lines.length == 4);
|
|
t.expects(made.lines.at(1) == 4 && made.lines.at(2) == 5 && made.lines.at(3) == 11);
|
|
|
|
val at := made.lines.place_of(6);
|
|
t.expects(at.line == 3 && at.column == 2);
|
|
}
|
|
|
|
@test def windows_line_breaks_read_as_those_of_other_systems(t: &mut tester): void {
|
|
val made := source::from("lines.sakura", "one\r\n\r\nthree\r\n");
|
|
t.expects(made.length == 11);
|
|
t.expects(made.lines.length == 4);
|
|
t.expects(made.lines.at(1) == 4 && made.lines.at(2) == 5 && made.lines.at(3) == 11);
|
|
|
|
# A CR on its own is no line break, and stays.
|
|
val alone := source::from("alone.sakura", "a\rb\r");
|
|
t.expects(alone.length == 4 && alone.lines.length == 1);
|
|
|
|
# A literal spanning lines holds the LF alone, wherever its text came from.
|
|
val crlf := lex("val a := \"\"\"\r\n x\r\n \"\"\";\r\n# note\r\nval b := 1;\r\n");
|
|
val lf := lex("val a := \"\"\"\n x\n \"\"\";\n# note\nval b := 1;\n");
|
|
t.expects(crlf.syntax.token_kinds.length == lf.syntax.token_kinds.length);
|
|
for i in 0..lf.syntax.token_kinds.length {
|
|
val ours := crlf.syntax.token(i as u32, &crlf.names);
|
|
val theirs := lf.syntax.token(i as u32, &lf.names);
|
|
t.expects(ours.kind == theirs.kind && ours.offset == theirs.offset
|
|
&& crlf.source.make_lexeme(ours.offset as u64, ours.length as u64) == lf.source.make_lexeme(theirs.offset as u64, theirs.length as u64));
|
|
}
|
|
}
|
|
|
|
@test def numbers(t: &mut tester): void {
|
|
t.expects(lexes_as("1 12u 1.5 2f 0xff 0o17 0b101", tk::signed, tk::unsigned, tk::float, tk::float, tk::signed, tk::signed, tk::signed));
|
|
|
|
t.expects(lexes_as("0xFFu 0o17u 0b101u", tk::unsigned, tk::unsigned, tk::unsigned));
|
|
|
|
# An exponent makes a float, with or without a fraction or a sign, and `e` among
|
|
# hexadecimal digits is a digit.
|
|
t.expects(lexes_as("6.02e23 1e-9 2E+10 3e5f 0x1e5", tk::float, tk::float, tk::float, tk::float, tk::signed));
|
|
t.expects(lexes_as("1e", tk::bad_number));
|
|
t.expects(lexes_as("2ex", tk::bad_number));
|
|
t.expects(lexes_as("1e5u", tk::bad_number));
|
|
|
|
# A fraction cannot be unsigned, and a suffix comes once.
|
|
t.expects(lexes_as("1.5u", tk::bad_number));
|
|
t.expects(lexes_as("1uf", tk::bad_number));
|
|
t.expects(lexes_as("2fu", tk::bad_number));
|
|
|
|
# `0..n` is a range, not a number with a fraction.
|
|
t.expects(lexes_as("0..n", tk::signed, tk::cascade, tk::name));
|
|
|
|
val bad := lex("val a := 0x;\nval b := 12ab;");
|
|
t.expects(bad.syntax.token_kinds.at(4) == tk::bad_number);
|
|
t.expects(lex_noted(&bad, 1, 10, ec::token));
|
|
t.expects(lex_noted(&bad, 2, 10, ec::token));
|
|
}
|
|
|
|
@test def operators_munch_as_much_as_they_can(t: &mut tester): void {
|
|
t.expects(lexes_as("... .. :: := => <<= <", tk::spread, tk::cascade, tk::scope, tk::colon, tk::assign, tk::arrow, tk::lshifteq, tk::less));
|
|
t.expects(lexes_as("a++ && b-- || !c != d", tk::name, tk::increment, tk::logand, tk::name, tk::decrement, tk::logor, tk::negate, tk::name, tk::noteq, tk::name));
|
|
}
|
|
|
|
@test def a_greater_than_is_followed_by_what_is_left_of_it(t: &mut tester): void {
|
|
# Each `>` and `=` after the first `>` of an operator begins a token of what is
|
|
# left of it, so a `>` closing type arguments always has a token of its own.
|
|
t.expects(lexes_as(">", tk::more));
|
|
t.expects(lexes_as(">>", tk::rshift, tk::more));
|
|
t.expects(lexes_as(">=", tk::moreeq, tk::assign));
|
|
t.expects(lexes_as(">>=", tk::rshifteq, tk::moreeq, tk::assign));
|
|
t.expects(lexes_as(">>>=", tk::rshift, tk::more, tk::moreeq, tk::assign));
|
|
|
|
# What is left ends where the operator does.
|
|
t.expects(lexes_as(">==", tk::moreeq, tk::assign, tk::assign));
|
|
t.expects(lexes_as("> > =", tk::more, tk::more, tk::assign));
|
|
}
|
|
|
|
@test def comments_are_skipped(t: &mut tester): void {
|
|
t.expects(lexes_as("a # a comment\n#! documentation\nb", tk::name, tk::name));
|
|
|
|
# Both are kept for the token after them, documentation apart from the rest.
|
|
val lexing := lex("a # a comment\n#! documentation\nb");
|
|
val syntax := &lexing.syntax;
|
|
t.expects(syntax.comments.length == 1 && syntax.docs.length == 1);
|
|
t.expects(lexing.source.make_lexeme(syntax.comments.at(0).offset, syntax.comments.at(0).length) == "# a comment");
|
|
t.expects(syntax.comments.at(0).token == 1 && syntax.docs.at(0).token == 1);
|
|
}
|
|
|
|
@test def strings_and_characters(t: &mut tester): void {
|
|
t.expects(lexes_as("\"text\" 'c' '\\n' \"\\x41\\{\"", tk::string, tk::char, tk::char, tk::string));
|
|
|
|
val lexing := lex("\"\\q\" 'ab' '' 'é'");
|
|
t.expects(lex_noted(&lexing, 1, 2, ec::escape));
|
|
t.expects(lex_noted(&lexing, 1, 6, ec::char));
|
|
t.expects(lex_noted(&lexing, 1, 11, ec::char));
|
|
t.expects(lexing.notes.length == 3);
|
|
}
|
|
|
|
@test def text_that_is_never_closed(t: &mut tester): void {
|
|
val lexing := lex("val s := \"open\nval c := 'x\n");
|
|
t.expects(lexing.syntax.token_kinds.at(4) == tk::bad_quote);
|
|
t.expects(lex_noted(&lexing, 1, 10, ec::token));
|
|
t.expects(lex_noted(&lexing, 2, 10, ec::token));
|
|
}
|
|
|
|
@test def text_spanning_lines(t: &mut tester): void {
|
|
val good := lex("val s := \"\"\"\n one\n\n two\n \"\"\";");
|
|
t.expects(good.notes.length == 0);
|
|
t.expects(good.syntax.token_kinds.at(4) == tk::string);
|
|
|
|
# Text on the opening line, the closing quotes after text, and a line indented
|
|
# less than the closing quotes.
|
|
t.expects(lex_noted(&lex("val s := \"\"\" x\n \"\"\";"), 1, 10, ec::block));
|
|
t.expects(lex_noted(&lex("val s := \"\"\"\n x \"\"\";"), 2, 1, ec::block));
|
|
t.expects(lex_noted(&lex("val s := \"\"\"\n x\n \"\"\";"), 2, 1, ec::block));
|
|
}
|
|
|
|
@test def interpolated_strings_split_at_their_holes(t: &mut tester): void {
|
|
t.expects(lexes_as("$\"plain\"", tk::istring));
|
|
t.expects(lexes_as("$\"a{b}c{d}e\"", tk::istring_beg, tk::name, tk::istring_mid, tk::name, tk::istring_end));
|
|
|
|
# Braces within a hole are counted, so a struct literal there closes nothing.
|
|
t.expects(lexes_as("$\"{p{ x = 1 }}\"", tk::istring_beg, tk::name, tk::lbrace, tk::name, tk::assign, tk::signed, tk::rbrace, tk::istring_end));
|
|
|
|
# A string within a hole holds holes of its own.
|
|
t.expects(lexes_as("$\"a{$\"b{c}\"}\"", tk::istring_beg, tk::istring_beg, tk::name, tk::istring_end, tk::istring_end));
|
|
|
|
# A block's holes may span lines, where a line's may not.
|
|
t.expects(lexes_as("$\"\"\"\n {a +\n b}\n \"\"\"", tk::istring_beg, tk::name, tk::plus, tk::name, tk::istring_end));
|
|
t.expects(lex("$\"\"\"\n {a}\n \"\"\"").notes.length == 0);
|
|
}
|
|
|
|
@test def escapes_within_interpolated_strings(t: &mut tester): void {
|
|
val lexing := lex("$\"\\{ {a} \\q\"");
|
|
t.expects(lexing.notes.length == 1);
|
|
t.expects(lex_noted(&lexing, 1, 10, ec::escape));
|
|
}
|
|
|
|
@test def unknown_characters_are_reported_and_dropped(t: &mut tester): void {
|
|
val lexing := lex("a ` b");
|
|
t.expects(lex_noted(&lexing, 1, 3, ec::token));
|
|
t.expects(lexes_as("a ` b", tk::name, tk::name));
|
|
|
|
# A character beyond ASCII is reported once, however many bytes it takes.
|
|
val wide := lex("a é b");
|
|
t.expects(wide.notes.length == 1 && lex_noted(&wide, 1, 3, ec::token));
|
|
t.expects(wide.notes.at(0).length == 2);
|
|
t.expects(lexes_as("a é b", tk::name, tk::name));
|
|
}
|
|
|
|
@test def names_outlive_the_text_they_were_read_in(t: &mut tester): void {
|
|
var table := names::new();
|
|
var text := string::from("fresh_name", nothing {});
|
|
val view := text.view();
|
|
val id := table.intern(&view);
|
|
|
|
# The text a name was read in may change or go, as an editor's does.
|
|
text.at(0) = 'X';
|
|
val again: strview = "fresh_name";
|
|
t.expects(table.intern(&again) == id);
|
|
}
|