トップページドキュメント > Xcodeでの構文色付け定義

文法定義を伴わない単純な構文色付け定義

それでは、これまでの知識をもとに、文法定義を伴わない単純な構文色付け定義を書いて見ましょう。

まず、トップレベルの構文定義は以下のようになります。

//
// filename: sample.xclangspec
//

(
    {
        Identifier = "my.lang.sample";
        Name = "Sample";
        Description = "Sample Language Coloring";
        BasedOn = "xcode.lang.simpleColoring";
        IncludeInMenu = YES;
        Syntax = {
            IncludeRules = (
                "my.lang.sample.text1",
                "my.lang.sample.text2",
                "my.lang.sample.text3"
            );
            Type = "xcode.syntax.plain";
        };
    },
    
    ...
)

IncludeRulesの中に、この構文含める構文定義の識別子を書きます。今回は、ここに各種字句定義の識別子を並べるだけです。構文定義は、配列の頭から順番に試行されるので、優先度の高いものから順に書きます。また、トップレベルの構文タイプはxcode.syntax.plainとします。どの構文にもマッチしなかったものは、デフォルトでこの構文タイプが適用されます。

あとは、その言語に必要な字句定義を書いて行くだけですが、Xcodeには基本的な字句定義が予め組み込まれているので、それらを利用すると字句定義の記述量を減らすことができます。

字句定義 識別子 使用例
URL xcode.lang.url http://foo.com
メールアドレス xcode.lang.url.mail foo@bar.com
文字列 xcode.lang.string "Hello, world."
文字列 xcode.lang.string.singlequote 'hello, world.'
文字 xcode.lang.character 'A'
プレースホルダ xcode.lang.completionplaceholder <# hoge #>
コメント xcode.lang.comment /* comment */
コメント xcode.lang.comment.singleline // comment
コメント xcode.lang.comment.singleline.pound # comment
数値 xcode.lang.number 0.123e-3

これら、組み込み字句定義を利用すると、単純な構文色付けなら言語固有の予約語を定義するだけで済むようになります。

//
// filename: sample.xclangspec
//

(
    {
        Identifier = "my.lang.sample";
        Name = "Sample";
        Description = "Sample Language Coloring";
        BasedOn = "xcode.lang.simpleColoring";
        IncludeInMenu = YES;
        Syntax = {
            IncludeRules = (
                "xcode.lang.comment",
                "xcode.lang.comment.singleline",
                "xcode.lang.string",
                "xcode.lang.character",
                "my.lang.sample.keyword",
                "xcode.lang.number",
            );
            Type = "xcode.syntax.plain";
        };
    },
    
    {
        Identifier = "my.lang.sample.keyword";
        Syntax = {
            StartChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
            Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
            Words = (
                "function",
                "var",
                "if",
                "then",
                "else",
                "end",
                "for",
                "while",
                "do",
                "switch",
                "case"
            );
            Type = "xcode.syntax.keyword";
            AltType = "xcode.syntax.identifier";
        };
    }
)

//
// end
//

このファイルを/Developer/Library/Xcode/Specificationsフォルダに入れてXcodeを再起動すると、「構文の色付け」メニューにSampleが追加されます。

Copyright(C)2001-2010 STRIPE-NET. All Right Reserved.