#! @file tables.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the tables the parser reads tokens by, built while compiling: the #! traits of each kind of token, as whether it begins a declaration or closes a #! list, and the binding power of each operator. module sakura::analyzer; #! What the parser asks of a kind of token, as bits of `tk::traits`. Each kind's #! answers are worked out once, while compiling, so asking is one load. enum tt : u32 { assigns = 0x0001, #! `=`, `+=`, and the rest. declares = 0x0002, #! Begins a declaration at the top level of a file. imports = 0x0004, #! `import` or `module`. states = 0x0008, #! Begins a statement a block resumes at after an error. fields = 0x0010, #! Begins a member a struct or union resumes at. members = 0x0020, #! Begins a member a trait resumes at. opens = 0x0040, #! Opens what a later token closes. closes = 0x0080, #! Closes what an earlier token opened. text = 0x0100, #! Its text says something its node's kind does not: a name, a literal, or an operator. piece = 0x0200, #! May be part of a type before its `<`, as `&mut a::b`. angle = 0x0400, #! Begins with a `>` that may close type arguments: `>`, `>>`, `>=`, or `>>=`. } #! What the parser asks of each kind of token. const token_traits: u32[tk::count as u64] = make_token_traits(); #! How tightly each kind of token binds as an operator, `none` where it is not #! one. const token_powers: parser::bp[tk::count as u64] = make_token_powers(); #! The answers of a kind of token, as bits of `tt`. #! @param this The kind of token. #! @returns The bits. val tk::traits: u32 { get => token_traits[this as u64]; } #! Whether a kind of token has the trait. #! @param this The kind of token. #! @param which The trait. #! @returns Whether it has. def tk::has(&this, which: tt): bool { return (this.traits & which as u32) != 0; } const def make_token_traits(): u32[tk::count as u64] { var made: u32[tk::count as u64]; for each in [ tk::assign, tk::pluseq, tk::minuseq, tk::stareq, tk::slasheq, tk::moduloeq, tk::bitandeq, tk::bitoreq, tk::bitxoreq, tk::lshifteq, tk::rshifteq ] { made[each as u64] |= tt::assigns as u32; } for each in [ tk::kw_pub, tk::attr, tk::kw_when, tk::kw_enum, tk::kw_def, tk::kw_const, tk::kw_struct, tk::kw_union, tk::kw_trait, tk::kw_type, tk::kw_val, tk::kw_var ] { made[each as u64] |= tt::declares as u32; } for each in [tk::kw_imp, tk::kw_mod, tk::kw_extern] { made[each as u64] |= tt::imports as u32; } for each in [ tk::kw_val, tk::kw_var, tk::kw_const, tk::kw_if, tk::kw_while, tk::kw_for, tk::kw_match, tk::kw_when, tk::kw_ret, tk::kw_break, tk::kw_cont ] { made[each as u64] |= tt::states as u32; } for each in [tk::kw_val, tk::kw_var, tk::attr, tk::kw_pub] { made[each as u64] |= tt::fields as u32; } for each in [tk::kw_def, tk::kw_const, tk::attr] { made[each as u64] |= tt::members as u32; } for each in [tk::lparen, tk::lbracket, tk::lbrace, tk::istring_beg] { made[each as u64] |= tt::opens as u32; } for each in [tk::rparen, tk::rbracket, tk::rbrace, tk::istring_end] { made[each as u64] |= tt::closes as u32; } for each in [ tk::name, tk::unknown, tk::signed, tk::unsigned, tk::float, tk::char, tk::string, tk::istring, tk::istring_beg, tk::istring_mid, tk::istring_end, tk::kw_true, tk::kw_false, tk::kw_null, tk::assign, tk::equals, tk::negate, tk::noteq, tk::less, tk::lesseq, tk::more, tk::moreeq, tk::plus, tk::minus, tk::star, tk::slash, tk::modulo, tk::logand, tk::logor, tk::bitand, tk::bitor, tk::bitxor, tk::bitnot, tk::lshift, tk::rshift, tk::pluseq, tk::minuseq, tk::stareq, tk::slasheq, tk::moduloeq, tk::bitandeq, tk::bitoreq, tk::bitxoreq, tk::lshifteq, tk::rshifteq, tk::increment, tk::decrement, tk::cascade, tk::cascadeeq ] { made[each as u64] |= tt::text as u32; } for each in [tk::name, tk::scope, tk::kw_mut, tk::bitand, tk::star, tk::kw_not] { made[each as u64] |= tt::piece as u32; } for each in [tk::more, tk::rshift, tk::moreeq, tk::rshifteq] { made[each as u64] |= tt::angle as u32; } return made; } const def make_token_powers(): parser::bp[tk::count as u64] { var made: parser::bp[tk::count as u64]; for each in [ tk::assign, tk::pluseq, tk::minuseq, tk::stareq, tk::slasheq, tk::moduloeq, tk::bitandeq, tk::bitoreq, tk::bitxoreq, tk::lshifteq, tk::rshifteq ] { made[each as u64] = parser::bp::assign; } made[tk::kw_else as u64] = parser::bp::fallback; made[tk::quest as u64] = parser::bp::ternary; made[tk::cascade as u64] = parser::bp::range; made[tk::cascadeeq as u64] = parser::bp::range; made[tk::logor as u64] = parser::bp::logor; made[tk::logand as u64] = parser::bp::logand; made[tk::bitor as u64] = parser::bp::bitor; made[tk::bitxor as u64] = parser::bp::bitxor; made[tk::bitand as u64] = parser::bp::bitand; for each in [tk::equals, tk::noteq] { made[each as u64] = parser::bp::equality; } for each in [tk::less, tk::lesseq, tk::more, tk::moreeq] { made[each as u64] = parser::bp::compare; } for each in [tk::lshift, tk::rshift] { made[each as u64] = parser::bp::shift; } for each in [tk::plus, tk::minus] { made[each as u64] = parser::bp::sum; } for each in [tk::star, tk::slash, tk::modulo] { made[each as u64] = parser::bp::product; } for each in [tk::kw_as, tk::kw_is] { made[each as u64] = parser::bp::cast; } for each in [tk::lparen, tk::lbracket, tk::dot, tk::increment, tk::decrement, tk::scope] { made[each as u64] = parser::bp::postfix; } return made; }