本文へ移動
cccskills
無料GitHub で公開日本語紹介

macro

Topcoat向けのRust手続きマクロを設計する際に、構文情報を保つAST、解析処理、マクロの入口とコード生成の分担を整理します。

原文原文の説明を見る

Always use this skill before writing procedural macros for Topcoat

tokio-rs/topcoatスター 6,224(GitHub 6,224 + cccskills 0)

実行スコア 未検証

インストール方法を見る

こんなときに便利

  • 手続きマクロのASTを設計したいとき
  • 入力構文を保ったまま解析したいとき
  • 解析とコード生成の役割を分けたいとき

日本語での紹介

できること

Topcoat向けのRust手続きマクロを作る際の、構文解析とコード生成の設計方針をまとめています。AST(構文を表すデータ構造)の各フィールドには入力されたトークンを保持し、位置情報も保つことで、元のソースを再構成できる形にします。解析では有効な構文かどうかを確かめ、解釈や意味付けはASTノードのメソッドに分けます。Parseの実装方法、独自キーワードや任意ノードの扱い、マクロの入口を薄く保つ構成も説明します。属性マクロでは属性側と対象項目側をそれぞれ解析し、組み合わせた型に展開処理を持たせます。

こんなときに便利

Topcoatで手続きマクロの構文やASTを設計するときに向いています。解析処理に解釈や生成ロジックが混在している場合に、役割を整理する手がかりにもなります。

使い方の例

  • 「このマクロの入力構文を保持するASTの形を考えてください」
  • 「属性マクロの解析と展開処理を分ける構成を提案してください」

注意点

Rustのproc_macro、syn、quoteを使う構成が記載されています。Topcoat向けのルールです。

この紹介文は、公開されている SKILL.md をもとに AI(OpenAI Codex)が作成しました。正確な仕様は下の原文を確認してください。

含まれるファイル(1)

  • SKILL.md3.0 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

AST nodes

  • Like syn nodes, an AST node is a lossless record of the syntax it matched: every field is pub and holds the tokens as written, spans included. The original source should be reconstructible from the node. Field order does not have to match the source, but spans must be preserved.
  • Parsing validates, it does not interpret. A Parse impl rejects what is not valid syntax and stores everything else verbatim in token fields; it never normalizes, resolves, or drops information on the way in.
  • Semantics are derived from the AST, not baked into it. Put the interpretation in methods on the node that read its own fields, so the node stays a faithful representation of the source and the meaning stays separate from it.

Parsing

  • In Parse impls, parse directly into the Self { ... } fields (Self { x: input.parse()? }) rather than through let bindings. Use a let only when a parsed value must be inspected to decide how to parse a later field.
  • To parse custom keywords that are not re-emitted into the generated code, create a private mod kw with syn::custom_keyword! invocations instead of parsing syn::Ident. Use input.lookahead1() if it makes sense.
  • Instead of manually peeking for optional AST nodes in the input stream at the call site, implement ParseOption and parse them with input.call(Node::parse_option)?.

Entry points

The macro/ crate only bridges proc_macro::TokenStream to the grammar crate that holds the AST and the codegen. Every entry point is a parse, a match, and a to_compile_error() on the error arm; no logic lives in lib.rs.

An attribute macro receives two token streams, so it gets three types: one Parse node per stream and a tuple struct pairing them, which is the node the codegen hangs off.

// grammar crate
pub struct ProcedureAttr {}
pub struct ProcedureItem { ... }

pub struct Procedure(ProcedureAttr, ProcedureItem);

impl Procedure {
    #[must_use]
    pub fn new(attr: ProcedureAttr, item: ProcedureItem) -> Self {
        Self(attr, item)
    }

    pub fn parse(attr: TokenStream, item: TokenStream) -> syn::Result<Self> {
        Ok(Self::new(syn::parse2(attr)?, syn::parse2(item)?))
    }
}

impl ToTokens for Procedure { ... }

Parse stays per stream, since neither stream alone is the macro input. The pairing type owns the inherent parse that joins them and the ToTokens impl that expands them, which keeps the entry point a one-liner:

// macro crate
#[doc = include_str!("../docs/procedure.md")]
#[proc_macro_attribute]
pub fn procedure(attr: TokenStream, item: TokenStream) -> TokenStream {
    match topcoat_runtime_grammar::procedure::Procedure::parse(attr.into(), item.into()) {
        Ok(value) => quote! { #value }.into(),
        Err(error) => error.to_compile_error().into(),
    }
}

A function-like macro has a single input, so it needs no pairing type: syn::parse_macro_input! straight into its AST node, then expand.

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

コメント

ログインなしで書き込めます。使ってみた感想、質問、動かなかった環境などを気軽にどうぞ。

まだコメントはありません。最初のコメントを書いてみませんか。

送信するとすぐに公開されます。メールアドレスや電話番号などの個人情報は書かないでください。URL は 2 つまでです。

0 / 1000 文字

このフォームは reCAPTCHA で保護されており、Google のプライバシーポリシーと利用規約が適用されます。

同じリポジトリのスキル

check

無料概要あり

Rustやブラウザー関連の変更を、TopcoatのCIに合わせた書式チェック、Lint、テスト、ドキュメント生成でローカル検証し、コミットやPRの前に問題を見つけます。

  • Rustの変更をコミット前に検証したいとき
  • ブラウザー部分のビルドとテストを確認したいとき
  • CI相当のドキュメント生成を行いたいとき
tokio-rs/topcoat6,2242026年10月25日 更新

commit

無料概要あり

変更内容に合ったConventional Commits形式のコミットメッセージを作り、変更種別に応じたリリースノートやバージョン更新に役立つ履歴を残します。

  • 変更に合うコミットメッセージを作りたいとき
  • コミットの種別やスコープを選びたいとき
  • 破壊的変更と移行方法を記録したいとき
tokio-rs/topcoat6,2242026年10月25日 更新

pr

無料概要あり

Topcoatのプルリクエストについて、ブランチ全体の差分に基づくタイトルと本文を作り、変更内容や検証方法を簡潔に伝えます。

  • ブランチ全体の差分からPR本文を作りたいとき
  • 変更概要と確認方法を簡潔に伝えたいとき
  • AIエージェントによるPRの説明を添えたいとき
tokio-rs/topcoat6,2242026年10月25日 更新

prose

無料概要あり

TopcoatのMarkdownガイドを、配置場所や構成、平易な英語での書き方に沿って整理し、読みやすく保守しやすいドキュメントにまとめます。

  • Topcoatの利用ガイドを新しく書きたいとき
  • 既存のMarkdown説明を読みやすく整えたいとき
  • 文書の配置場所と参照先を整理したいとき
tokio-rs/topcoat6,2242026年10月25日 更新

style

無料概要あり

Rustのコードやドキュメントを編集するときに、モジュール構成、依存関係、文章表現、テスト設計などプロジェクトの規約に沿う方法を示します。

  • Rustコードを規約に沿って編集したいとき
  • 利用者向けの説明文を書きたいとき
  • テストの目的を見直したいとき
tokio-rs/topcoat6,2242026年10月25日 更新

tokio-rs のスキルをすべて見る

このスキルの問題を報告する