目次

bol

xpeg::bol : Element(Element:TYPE_BOL);

Begin of Line の略です。

Example1

ファイルを読み込み、先頭が”#“の行のみイテレートします。

C++

Xtal

inherit(xpeg);
if (fs : FileStream("text.txt", "r")){
    fs.scan(bol >> "#" >> cap(comment: any/0) >> eol){
        it["comment"].p;
    }
    fs.close;
}

Input

# comment
a b c def # not comment
#comment2
adc

Output

 comment
comment2

Example2

ファイルを読み込み、先頭が"//"の時はコメントとして、それ以外のときはそれ以外として、出力します。

Xtal側のバグなのかそもそもこういう書き方できないのかしらないけど割とうまくいかない。

C++

Xtal

inherit(xpeg);
if (fs : FileStream("text.txt", "r")){
    fs.scan((bol >> "//" >> cap(com: any/0) >> eol)|(bol >> cap(lin: any/0) >> eol)){
        if (it["com"]){
            ("comment : "~it["com"]).p;
        }
        else {    // else if (it["lin"])
            ("line    : "~it["lin"]).p;
        }
    }
    fs.close;
}

Input

// comment
this is not comment
comment janai! // hoge

Output

comment :  comment
line    : this is not comment
line    : comment janai! // hoge