LoginSignup
9

More than 5 years have passed since last update.

coffee-scriptコードリーディングメモ

Last updated at Posted at 2013-03-10

半分ぐらい読んだ https://github.com/jashkenas/coffee-script
mizchi/coffee-scriptはコメントを日本語訳しつつ注釈つけたもの

$ cake build:jison で grammer.coffeeをjisonでコンパイルしてparser.jsを生成
https://github.com/mizchi/coffee-script/blob/master/src/grammar.coffee

jisonはbisonのJS実装。改良BNF。

以下coffee -c hello.coffeeしたときの挙動

commands.coffee
ソース文字列とオプション貰ってコンパイルにかける

CoffeeScript#compile
文字列をjsに変換する。具体的には以下の操作を行う

Lexer#tokenize
https://github.com/mizchi/coffee-script/blob/master/src/lexer.coffee
ソース文字列からトークン列を生成。
正規表現にかけて、トークンを一個ずつ取り出しながら現在の読み込みモード(forの中か、式か、リテラルか)を変更しつつトークン列にpushする

Rewriter#rewrite
https://github.com/mizchi/coffee-script/blob/master/src/rewriter.coffee
トークン列を修正して補完(暗黙の関数適用とか不要な改行の除去とか)
addImplicit~が怖くて全然読めなかった

Parser#parse
で加工されたトークン列からJisonParserを通してJS生成
Jisonのドキュメントを読め

全体的にSourcemapサポートがとってつけた感がある。目玉の新規機能だから仕方ないのか…

なお他のCoffeeScript実装ではJisonを使っていない模様
coffeeでのJison実装は正直ださいので、弄りたいなら他の実装読んだほうがいい気がした

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9