Rustやブラウザー関連の変更を、TopcoatのCIに合わせた書式チェック、Lint、テスト、ドキュメント生成でローカル検証し、コミットやPRの前に問題を見つけます。
- Rustの変更をコミット前に検証したいとき
- ブラウザー部分のビルドとテストを確認したいとき
- CI相当のドキュメント生成を行いたいとき
Topcoat向けのRust手続きマクロを設計する際に、構文情報を保つAST、解析処理、マクロの入口とコード生成の分担を整理します。
Always use this skill before writing procedural macros for Topcoat
実行スコア 未検証
インストール方法を見るTopcoat向けのRust手続きマクロを作る際の、構文解析とコード生成の設計方針をまとめています。AST(構文を表すデータ構造)の各フィールドには入力されたトークンを保持し、位置情報も保つことで、元のソースを再構成できる形にします。解析では有効な構文かどうかを確かめ、解釈や意味付けはASTノードのメソッドに分けます。Parseの実装方法、独自キーワードや任意ノードの扱い、マクロの入口を薄く保つ構成も説明します。属性マクロでは属性側と対象項目側をそれぞれ解析し、組み合わせた型に展開処理を持たせます。
Topcoatで手続きマクロの構文やASTを設計するときに向いています。解析処理に解釈や生成ロジックが混在している場合に、役割を整理する手がかりにもなります。
Rustのproc_macro、syn、quoteを使う構成が記載されています。Topcoat向けのルールです。
この紹介文は、公開されている SKILL.md をもとに AI(OpenAI Codex)が作成しました。正確な仕様は下の原文を確認してください。
インストールする前に、エージェントに与えられる指示の中身を確認できます。
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.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.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.mod kw with syn::custom_keyword! invocations instead of parsing syn::Ident. Use input.lookahead1() if it makes sense.ParseOption and parse them with input.call(Node::parse_option)?.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.
まだレビューはありません。使ってみた感想をお寄せください。
ログインなしで書き込めます。使ってみた感想、質問、動かなかった環境などを気軽にどうぞ。
まだコメントはありません。最初のコメントを書いてみませんか。
Rustやブラウザー関連の変更を、TopcoatのCIに合わせた書式チェック、Lint、テスト、ドキュメント生成でローカル検証し、コミットやPRの前に問題を見つけます。
変更内容に合ったConventional Commits形式のコミットメッセージを作り、変更種別に応じたリリースノートやバージョン更新に役立つ履歴を残します。
Topcoatのプルリクエストについて、ブランチ全体の差分に基づくタイトルと本文を作り、変更内容や検証方法を簡潔に伝えます。
TopcoatのMarkdownガイドを、配置場所や構成、平易な英語での書き方に沿って整理し、読みやすく保守しやすいドキュメントにまとめます。
Rustのコードやドキュメントを編集するときに、モジュール構成、依存関係、文章表現、テスト設計などプロジェクトの規約に沿う方法を示します。