Initial Commit
This commit is contained in:
782 files changed
+145156
No files matched your search
@@ -0,0 +1,274 @@
|
||||
#! @file token.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the token type, which is a kind of token and the text it was read
|
||||
#! from, and `tk`, the kinds of token.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
#! Defines a source code token.
|
||||
#!
|
||||
#! A token is the kind and location of a piece of text within some source code. The
|
||||
#! type is used in the parser to identify constructs and build them into an
|
||||
#! abstract syntax tree. The offset of the token points to the index within the
|
||||
#! source code that token resides. The offset can be used with the
|
||||
#! `source::lines` vector to identify a place within the source code that the
|
||||
#! token resides.
|
||||
pub struct token {
|
||||
var offset: u32; #! Where it begins in the source text.
|
||||
var length: u32; #! How many bytes of the source text it spans.
|
||||
var name: u32; #! The ID of the word it is, or 0 where it is no word.
|
||||
var kind: tk; #! Its kind.
|
||||
}
|
||||
|
||||
#! The kinds of token.
|
||||
#!
|
||||
#! They are grouped by what they are: the keywords, then the literals, the
|
||||
#! pieces of an interpolated string, and the punctuators. `true`, `false`, and
|
||||
#! `null` are keywords that are also literals, and they are the last keywords,
|
||||
#! so `kw_beg` to `kw_end` spans every keyword.
|
||||
#!
|
||||
#! `eof` marks the end of the source, and `bad_number` and `bad_quote` are what
|
||||
#! the lexer gives for a number or a quote it could not make sense of, so it
|
||||
#! goes on and the parser reports them where they are. The lexer almost never
|
||||
#! fails otherwise.
|
||||
pub enum tk : u8 {
|
||||
# Keywords
|
||||
kw_imp, kw_mod, kw_extern, kw_pub, kw_const, kw_var, kw_val, kw_def,
|
||||
kw_mut, kw_ret, kw_if, kw_else, kw_for, kw_in, kw_while, kw_break,
|
||||
kw_cont, kw_type, kw_enum, kw_struct, kw_union, kw_trait, kw_as, kw_is,
|
||||
kw_not, kw_match, kw_when, kw_where, kw_true, kw_false, kw_null,
|
||||
|
||||
# Literals
|
||||
name, unknown, signed, unsigned, float, char, string, istring,
|
||||
|
||||
# Pieces of an interpolated string with holes, between which come the tokens of
|
||||
# each hole: `$"text{`, then `}text{` between two holes, then `}text"`.
|
||||
istring_beg, istring_mid, istring_end,
|
||||
|
||||
# Punctuators
|
||||
lparen, rparen, lbrace, rbrace, lbracket, rbracket, semicolon, comma,
|
||||
dot, cascade, spread, colon, quest, attr, assign, arrow,
|
||||
equals, negate, noteq, less, lesseq, more, moreeq, plus,
|
||||
minus, star, slash, modulo, logand, logor, bitand, bitor,
|
||||
bitxor, bitnot, lshift, rshift, scope, pluseq, minuseq, stareq,
|
||||
slasheq, moduloeq, logandeq, logoreq, bitandeq, bitoreq, bitxoreq, bitnoteq,
|
||||
lshifteq, rshifteq, increment, decrement, cascadeeq,
|
||||
|
||||
# End of file marker.
|
||||
#! The end of the source.
|
||||
eof,
|
||||
|
||||
# Error tokens.
|
||||
#! A number written wrongly.
|
||||
bad_number,
|
||||
#! A string or character with no closing quote.
|
||||
bad_quote,
|
||||
|
||||
# Not a kind: how many there are, which sizes the tables indexed by kind.
|
||||
#! How many kinds there are, which sizes the tables indexed by kind; not a
|
||||
#! kind itself.
|
||||
count,
|
||||
|
||||
# Ranges.
|
||||
#! The first keyword.
|
||||
kw_beg = tk::kw_imp,
|
||||
#! The last keyword.
|
||||
kw_end = tk::kw_null,
|
||||
}
|
||||
|
||||
#! How many bytes a token of the kind covers, where its kind says: those of a
|
||||
#! keyword or a punctuator, or 0 for a kind of any length, as a name or a
|
||||
#! literal.
|
||||
#! @param this The kind of token.
|
||||
#! @returns How many.
|
||||
pub val tk::width: u32 { get => token_widths[this as u64] as u32; }
|
||||
|
||||
#! How many bytes a token of each kind covers, where its kind says, worked out
|
||||
#! while compiling: a keyword's word, and a punctuator's bytes.
|
||||
const token_widths: u8[tk::count as u64] = make_token_widths();
|
||||
|
||||
const def make_token_widths(): u8[tk::count as u64] {
|
||||
var made: u8[tk::count as u64];
|
||||
for &each in _keywords { made[each.kind as u64] = each.text.length as u8; }
|
||||
for each in [
|
||||
tk::lparen, tk::rparen, tk::lbrace, tk::rbrace, tk::lbracket, tk::rbracket, tk::semicolon,
|
||||
tk::comma, tk::dot, tk::colon, tk::quest, tk::attr, tk::assign, tk::negate,
|
||||
tk::less, tk::more, tk::plus, tk::minus, tk::star, tk::slash, tk::modulo,
|
||||
tk::bitand, tk::bitor, tk::bitxor, tk::bitnot
|
||||
] { made[each as u64] = 1; }
|
||||
for each in [
|
||||
tk::cascade, tk::scope, tk::arrow, tk::equals, tk::noteq, tk::lesseq,
|
||||
tk::moreeq, tk::logand, tk::logor, tk::lshift, tk::rshift, tk::pluseq,
|
||||
tk::minuseq, tk::stareq, tk::slasheq, tk::moduloeq, tk::bitandeq, tk::bitoreq,
|
||||
tk::bitxoreq, tk::bitnoteq, tk::increment, tk::decrement
|
||||
] { made[each as u64] = 2; }
|
||||
for each in [tk::spread, tk::cascadeeq, tk::logandeq, tk::logoreq, tk::lshifteq, tk::rshifteq] {
|
||||
made[each as u64] = 3;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Whether the token compares, as `==` or `<` do.
|
||||
#! @param this The token.
|
||||
#! @returns Whether it does.
|
||||
pub def tk::compares(&this): bool {
|
||||
return this == tk::equals || this == tk::noteq || this == tk::less
|
||||
|| this == tk::lesseq || this == tk::more || this == tk::moreeq;
|
||||
}
|
||||
|
||||
#! The operator a compound assignment applies, as `+` of `+=`, or the token.
|
||||
#! @param this The token.
|
||||
#! @returns The operator.
|
||||
pub def tk::applied(&this): tk {
|
||||
return match this {
|
||||
tk::pluseq => tk::plus,
|
||||
tk::minuseq => tk::minus,
|
||||
tk::stareq => tk::star,
|
||||
tk::slasheq => tk::slash,
|
||||
tk::moduloeq => tk::modulo,
|
||||
tk::bitandeq => tk::bitand,
|
||||
tk::bitoreq => tk::bitor,
|
||||
tk::bitxoreq => tk::bitxor,
|
||||
tk::lshifteq => tk::lshift,
|
||||
tk::rshifteq => tk::rshift,
|
||||
else => this
|
||||
};
|
||||
}
|
||||
|
||||
# Match based token kind extensions.
|
||||
# Given the current token kind and the next character, produces the next token
|
||||
# kind. This assists the lexer in matching greedily. `>>`, `>=`, and `>>=` are
|
||||
# matched as well, though they may close type arguments instead, so the lexer
|
||||
# follows each with tokens of what is left of it, as `lexer` says.
|
||||
def tk::from(&this, char: u8): tk {
|
||||
return match char {
|
||||
# Single
|
||||
'(' => tk::lparen,
|
||||
')' => tk::rparen,
|
||||
'{' => tk::lbrace,
|
||||
'}' => tk::rbrace,
|
||||
'[' => tk::lbracket,
|
||||
']' => tk::rbracket,
|
||||
';' => tk::semicolon,
|
||||
',' => tk::comma,
|
||||
'?' => tk::quest,
|
||||
'@' => tk::attr,
|
||||
|
||||
# Multi
|
||||
'.' => tk::dot,
|
||||
':' => tk::colon,
|
||||
'=' => tk::assign,
|
||||
'!' => tk::negate,
|
||||
'<' => tk::less,
|
||||
'>' => tk::more,
|
||||
'+' => tk::plus,
|
||||
'-' => tk::minus,
|
||||
'*' => tk::star,
|
||||
'/' => tk::slash,
|
||||
'%' => tk::modulo,
|
||||
'&' => tk::bitand,
|
||||
'|' => tk::bitor,
|
||||
'^' => tk::bitxor,
|
||||
'~' => tk::bitnot,
|
||||
else => tk::unknown
|
||||
} if this == tk::unknown;
|
||||
|
||||
return match this {
|
||||
# Doubles
|
||||
tk::dot => match char {
|
||||
'.' => tk::cascade,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::colon => match char {
|
||||
':' => tk::scope,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::assign => match char {
|
||||
'=' => tk::equals,
|
||||
'>' => tk::arrow,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::negate => match char {
|
||||
'=' => tk::noteq,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::less => match char {
|
||||
'=' => tk::lesseq,
|
||||
'<' => tk::lshift,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::more => match char {
|
||||
'=' => tk::moreeq,
|
||||
'>' => tk::rshift,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::plus => match char {
|
||||
'=' => tk::pluseq,
|
||||
'+' => tk::increment,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::minus => match char {
|
||||
'=' => tk::minuseq,
|
||||
'-' => tk::decrement,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::star => match char {
|
||||
'=' => tk::stareq,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::slash => match char {
|
||||
'=' => tk::slasheq,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::modulo => match char {
|
||||
'=' => tk::moduloeq,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::bitand => match char {
|
||||
'=' => tk::bitandeq,
|
||||
'&' => tk::logand,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::bitor => match char {
|
||||
'=' => tk::bitoreq,
|
||||
'|' => tk::logor,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::bitxor => match char {
|
||||
'=' => tk::bitxoreq,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::bitnot => match char {
|
||||
'=' => tk::bitnoteq,
|
||||
else => tk::unknown
|
||||
},
|
||||
|
||||
# Triples
|
||||
tk::cascade => match char {
|
||||
'.' => tk::spread,
|
||||
'=' => tk::cascadeeq,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::lshift => match char {
|
||||
'=' => tk::lshifteq,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::rshift => match char {
|
||||
'=' => tk::rshifteq,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::logand => match char {
|
||||
'=' => tk::logandeq,
|
||||
else => tk::unknown
|
||||
},
|
||||
tk::logor => match char {
|
||||
'=' => tk::logoreq,
|
||||
else => tk::unknown
|
||||
},
|
||||
else => tk::unknown
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user