use super::{
AstChildren, AstNode,
module::{Index, TypeUse},
support::*,
ty::{HeapType, RefType},
};
use crate::{SyntaxKind, SyntaxNode, SyntaxToken};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BlockBlock {
syntax: SyntaxNode,
}
impl BlockBlock {
#[inline]
pub fn l_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::L_PAREN)
}
#[inline]
pub fn keyword(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::KEYWORD)
}
#[inline]
pub fn ident_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::IDENT)
}
#[inline]
pub fn type_use(&self) -> Option<TypeUse> {
child(&self.syntax)
}
#[inline]
pub fn instrs(&self) -> AstChildren<Instr> {
children(&self.syntax)
}
#[inline]
pub fn r_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::R_PAREN)
}
#[inline]
pub fn end_keyword(&self) -> Option<SyntaxToken> {
self.syntax
.tokens_by_kind(SyntaxKind::KEYWORD)
.find(|token| token.text() == "end")
}
#[inline]
pub fn end_ident_token(&self) -> Option<SyntaxToken> {
self.syntax.tokens_by_kind(SyntaxKind::IDENT).nth(1)
}
}
impl AstNode for BlockBlock {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
kind == SyntaxKind::BLOCK_BLOCK
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
if Self::can_cast(syntax.kind()) {
Some(BlockBlock { syntax })
} else {
None
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BlockIf {
syntax: SyntaxNode,
}
impl BlockIf {
#[inline]
pub fn l_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::L_PAREN)
}
#[inline]
pub fn keyword(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::KEYWORD)
}
#[inline]
pub fn ident_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::IDENT)
}
#[inline]
pub fn type_use(&self) -> Option<TypeUse> {
child(&self.syntax)
}
#[inline]
pub fn instrs(&self) -> AstChildren<Instr> {
children(&self.syntax)
}
#[inline]
pub fn then_block(&self) -> Option<BlockIfThen> {
child(&self.syntax)
}
#[inline]
pub fn else_block(&self) -> Option<BlockIfElse> {
child(&self.syntax)
}
#[inline]
pub fn r_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::R_PAREN)
}
#[inline]
pub fn end_keyword(&self) -> Option<SyntaxToken> {
self.syntax
.tokens_by_kind(SyntaxKind::KEYWORD)
.find(|token| token.text() == "end")
}
#[inline]
pub fn end_ident_token(&self) -> Option<SyntaxToken> {
self.syntax.tokens_by_kind(SyntaxKind::IDENT).nth(1)
}
}
impl AstNode for BlockIf {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
kind == SyntaxKind::BLOCK_IF
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
if Self::can_cast(syntax.kind()) {
Some(BlockIf { syntax })
} else {
None
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BlockIfElse {
syntax: SyntaxNode,
}
impl BlockIfElse {
#[inline]
pub fn l_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::L_PAREN)
}
#[inline]
pub fn keyword(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::KEYWORD)
}
#[inline]
pub fn ident_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::IDENT)
}
#[inline]
pub fn instrs(&self) -> AstChildren<Instr> {
children(&self.syntax)
}
#[inline]
pub fn r_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::R_PAREN)
}
}
impl AstNode for BlockIfElse {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
kind == SyntaxKind::BLOCK_IF_ELSE
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
if Self::can_cast(syntax.kind()) {
Some(BlockIfElse { syntax })
} else {
None
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BlockIfThen {
syntax: SyntaxNode,
}
impl BlockIfThen {
#[inline]
pub fn l_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::L_PAREN)
}
#[inline]
pub fn keyword(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::KEYWORD)
}
#[inline]
pub fn ident_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::IDENT)
}
#[inline]
pub fn instrs(&self) -> AstChildren<Instr> {
children(&self.syntax)
}
#[inline]
pub fn r_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::R_PAREN)
}
}
impl AstNode for BlockIfThen {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
kind == SyntaxKind::BLOCK_IF_THEN
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
if Self::can_cast(syntax.kind()) {
Some(BlockIfThen { syntax })
} else {
None
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BlockInstr {
Block(BlockBlock),
Loop(BlockLoop),
If(BlockIf),
TryTable(BlockTryTable),
}
impl AstNode for BlockInstr {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
matches!(
kind,
SyntaxKind::BLOCK_BLOCK | SyntaxKind::BLOCK_LOOP | SyntaxKind::BLOCK_IF | SyntaxKind::BLOCK_TRY_TABLE
)
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
match syntax.kind() {
SyntaxKind::BLOCK_BLOCK => Some(BlockInstr::Block(BlockBlock { syntax })),
SyntaxKind::BLOCK_LOOP => Some(BlockInstr::Loop(BlockLoop { syntax })),
SyntaxKind::BLOCK_IF => Some(BlockInstr::If(BlockIf { syntax })),
SyntaxKind::BLOCK_TRY_TABLE => Some(BlockInstr::TryTable(BlockTryTable { syntax })),
_ => None,
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
match self {
BlockInstr::Block(it) => it.syntax(),
BlockInstr::Loop(it) => it.syntax(),
BlockInstr::If(it) => it.syntax(),
BlockInstr::TryTable(it) => it.syntax(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BlockLoop {
syntax: SyntaxNode,
}
impl BlockLoop {
#[inline]
pub fn l_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::L_PAREN)
}
#[inline]
pub fn keyword(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::KEYWORD)
}
#[inline]
pub fn ident_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::IDENT)
}
#[inline]
pub fn type_use(&self) -> Option<TypeUse> {
child(&self.syntax)
}
#[inline]
pub fn instrs(&self) -> AstChildren<Instr> {
children(&self.syntax)
}
#[inline]
pub fn r_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::R_PAREN)
}
#[inline]
pub fn end_keyword(&self) -> Option<SyntaxToken> {
self.syntax
.tokens_by_kind(SyntaxKind::KEYWORD)
.find(|token| token.text() == "end")
}
#[inline]
pub fn end_ident_token(&self) -> Option<SyntaxToken> {
self.syntax.tokens_by_kind(SyntaxKind::IDENT).nth(1)
}
}
impl AstNode for BlockLoop {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
kind == SyntaxKind::BLOCK_LOOP
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
if Self::can_cast(syntax.kind()) {
Some(BlockLoop { syntax })
} else {
None
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BlockTryTable {
syntax: SyntaxNode,
}
impl BlockTryTable {
#[inline]
pub fn l_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::L_PAREN)
}
#[inline]
pub fn keyword(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::KEYWORD)
}
#[inline]
pub fn ident_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::IDENT)
}
#[inline]
pub fn type_use(&self) -> Option<TypeUse> {
child(&self.syntax)
}
#[inline]
pub fn catches(&self) -> AstChildren<Cat> {
children(&self.syntax)
}
#[inline]
pub fn instrs(&self) -> AstChildren<Instr> {
children(&self.syntax)
}
#[inline]
pub fn r_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::R_PAREN)
}
#[inline]
pub fn end_keyword(&self) -> Option<SyntaxToken> {
self.syntax
.tokens_by_kind(SyntaxKind::KEYWORD)
.find(|token| token.text() == "end")
}
#[inline]
pub fn end_ident_token(&self) -> Option<SyntaxToken> {
self.syntax.tokens_by_kind(SyntaxKind::IDENT).nth(1)
}
}
impl AstNode for BlockTryTable {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
kind == SyntaxKind::BLOCK_TRY_TABLE
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
if Self::can_cast(syntax.kind()) {
Some(BlockTryTable { syntax })
} else {
None
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Cat {
Catch(Catch),
CatchAll(CatchAll),
}
impl AstNode for Cat {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
matches!(kind, SyntaxKind::CATCH | SyntaxKind::CATCH_ALL)
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
match syntax.kind() {
SyntaxKind::CATCH => Some(Cat::Catch(Catch { syntax })),
SyntaxKind::CATCH_ALL => Some(Cat::CatchAll(CatchAll { syntax })),
_ => None,
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
match self {
Cat::Catch(it) => it.syntax(),
Cat::CatchAll(it) => it.syntax(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Catch {
syntax: SyntaxNode,
}
impl Catch {
#[inline]
pub fn l_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::L_PAREN)
}
#[inline]
pub fn keyword(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::KEYWORD)
}
#[inline]
pub fn tag_index(&self) -> Option<Index> {
child(&self.syntax)
}
#[inline]
pub fn label_index(&self) -> Option<Index> {
children(&self.syntax).nth(1)
}
#[inline]
pub fn r_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::R_PAREN)
}
}
impl AstNode for Catch {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
kind == SyntaxKind::CATCH
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
if Self::can_cast(syntax.kind()) {
Some(Catch { syntax })
} else {
None
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CatchAll {
syntax: SyntaxNode,
}
impl CatchAll {
#[inline]
pub fn l_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::L_PAREN)
}
#[inline]
pub fn keyword(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::KEYWORD)
}
#[inline]
pub fn label_index(&self) -> Option<Index> {
child(&self.syntax)
}
#[inline]
pub fn r_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::R_PAREN)
}
}
impl AstNode for CatchAll {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
kind == SyntaxKind::CATCH_ALL
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
if Self::can_cast(syntax.kind()) {
Some(CatchAll { syntax })
} else {
None
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Instr {
Block(BlockInstr),
Plain(PlainInstr),
}
impl AstNode for Instr {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
matches!(
kind,
SyntaxKind::PLAIN_INSTR
| SyntaxKind::BLOCK_BLOCK
| SyntaxKind::BLOCK_LOOP
| SyntaxKind::BLOCK_IF
| SyntaxKind::BLOCK_TRY_TABLE
)
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
match syntax.kind() {
SyntaxKind::PLAIN_INSTR => Some(Instr::Plain(PlainInstr { syntax })),
SyntaxKind::BLOCK_BLOCK => Some(Instr::Block(BlockInstr::Block(BlockBlock { syntax }))),
SyntaxKind::BLOCK_LOOP => Some(Instr::Block(BlockInstr::Loop(BlockLoop { syntax }))),
SyntaxKind::BLOCK_IF => Some(Instr::Block(BlockInstr::If(BlockIf { syntax }))),
SyntaxKind::BLOCK_TRY_TABLE => Some(Instr::Block(BlockInstr::TryTable(BlockTryTable { syntax }))),
_ => None,
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
match self {
Instr::Block(it) => it.syntax(),
Instr::Plain(it) => it.syntax(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Immediate {
syntax: SyntaxNode,
}
impl Immediate {
#[inline]
pub fn float(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::FLOAT)
}
#[inline]
pub fn int(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::INT)
}
#[inline]
pub fn string(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::STRING)
}
#[inline]
pub fn ident(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::IDENT)
}
#[inline]
pub fn shape_descriptor(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::SHAPE_DESCRIPTOR)
}
#[inline]
pub fn type_use(&self) -> Option<TypeUse> {
child(&self.syntax)
}
#[inline]
pub fn mem_arg(&self) -> Option<MemArg> {
child(&self.syntax)
}
#[inline]
pub fn heap_type(&self) -> Option<HeapType> {
child(&self.syntax)
}
#[inline]
pub fn ref_type(&self) -> Option<RefType> {
child(&self.syntax)
}
#[inline]
pub fn on_clause(&self) -> Option<OnClause> {
child(&self.syntax)
}
}
impl AstNode for Immediate {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
kind == SyntaxKind::IMMEDIATE
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
if Self::can_cast(syntax.kind()) {
Some(Immediate { syntax })
} else {
None
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MemArg {
syntax: SyntaxNode,
}
impl MemArg {
#[inline]
pub fn mem_arg_keyword(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::MEM_ARG_KEYWORD)
}
#[inline]
pub fn eq_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::EQ)
}
#[inline]
pub fn unsigned_int(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::UNSIGNED_INT)
}
}
impl AstNode for MemArg {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
kind == SyntaxKind::MEM_ARG
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
if Self::can_cast(syntax.kind()) {
Some(MemArg { syntax })
} else {
None
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OnClause {
syntax: SyntaxNode,
}
impl OnClause {
#[inline]
pub fn l_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::L_PAREN)
}
#[inline]
pub fn keyword(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::KEYWORD)
}
#[inline]
pub fn tag_index(&self) -> Option<Index> {
child(&self.syntax)
}
#[inline]
pub fn label_index(&self) -> Option<Index> {
children(&self.syntax).nth(1)
}
#[inline]
pub fn switch_keyword(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::MODIFIER_KEYWORD)
}
#[inline]
pub fn r_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::R_PAREN)
}
}
impl AstNode for OnClause {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
kind == SyntaxKind::ON_CLAUSE
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
if Self::can_cast(syntax.kind()) {
Some(OnClause { syntax })
} else {
None
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PlainInstr {
syntax: SyntaxNode,
}
impl PlainInstr {
#[inline]
pub fn l_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::L_PAREN)
}
#[inline]
pub fn instr_name(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::INSTR_NAME)
}
#[inline]
pub fn immediates(&self) -> AstChildren<Immediate> {
children(&self.syntax)
}
#[inline]
pub fn instrs(&self) -> AstChildren<Instr> {
children(&self.syntax)
}
#[inline]
pub fn r_paren_token(&self) -> Option<SyntaxToken> {
token(&self.syntax, SyntaxKind::R_PAREN)
}
}
impl AstNode for PlainInstr {
#[inline]
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized,
{
kind == SyntaxKind::PLAIN_INSTR
}
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized,
{
if Self::can_cast(syntax.kind()) {
Some(PlainInstr { syntax })
} else {
None
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}