Forums: 公開討議 (Thread #36197)

動的にシンタックスハイライトのキーワードを追加したい。 (2014-11-12 21:34 by 國島惇 #74812)

例外に困っています。
シンタックスハイライトをPHPに対応させようと考えています。
今作っているのはPHPの変数 $◯◯ という変数に色を付けることです。

以下の様なコードを作成したのですが、

using Color = System.Drawing.Color;
using Sgry.Azuki;
using System.Collections;
using Sgry.Azuki.Highlighter;

namespace AzukiListTest{
public partial class Form1 : Form{
public static List<String> KeywordsList;
public Form1()
{
InitializeComponent();
     azukiControl1.Text = "azuki $aaa $ddd";
KeywordsList = new List<string>();
KeywordsList.Add("azuki");

//変数を探す
try
{
int aa = azukiControl1.Text.IndexOf("$");//$は何文字目か
String toSpaceStr = azukiControl1.Text;
while (aa != -1)
{
toSpaceStr = toSpaceStr.Substring(aa);
int toSpaceInt = toSpaceStr.IndexOf(" ");//空白の位置を取得(変数の後ろには必ず空白があるのもとする。)
String variable = toSpaceStr.Substring(0, toSpaceInt);//変数取得
MessageBox.Show(variable);
KeywordsList.Add(variable);//見つけた変数名をリストに追加する。
toSpaceStr = toSpaceStr.Replace(variable, " ");//登録した変数を消す。
aa = toSpaceStr.IndexOf("$");//$はあるか?
}
}
catch { }

//リストを配列に変更
string[] array = new string[KeywordsList.Count];
int i = 0;
foreach (String x in KeywordsList){
array[i] = x;
i++;
}

KeywordHighlighter key = new KeywordHighlighter();
key.AddKeywordSet(array, CharClass.Keyword);
azukiControl1.Highlighter = key;
}
}






以下の例外が発生してしまいます。

型 'System.ArgumentException' のハンドルされていない例外が Azuki.dll で発生しました
追加情報:Keywords must be sorted alphabetically; '$mes1' is expected to be greater than 'while' but not greater.


発生箇所は以下の
AddKeywordSet(array, CharClass.Keyword);
なのですが、理由などがお分かりになりませんか?


開発環境はVS2013  win8です。







Reply to #74812×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

Re: 動的にシンタックスハイライトのキーワードを追加したい。 (2014-11-13 00:17 by sgry #74814)

國島さん

山本(sgry)です。Azukiをお使いいただきありがとうございます。

KeywordHighlighterはキーワードとして、アルファベット順にソートされた
キーワードのリストしか受け付けられません。そのため例外が発生しているのだと思います。
あらかじめキーワードをソートしてから指定してみてください。
# 例外メッセージからすると while より $mes1 が後に来ているのでエラーになっています

なおPHPの変数ということでしたら「$に英数文字が続く」といったパターン指定を使った方が
便利かと思います。その場合、KeywordHighlighterのAddRegexメソッドを使って、
PHPの変数すべてにマッチするような正規表現を登録できます。
こちらも検討してみてください。


よろしくお願いします。
Reply to #74812

Reply to #74814×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

Re: 動的にシンタックスハイライトのキーワードを追加したい。 (2014-11-13 00:20 by 國島惇 #74815)

早速のご回答ありがとうございます。

>なおPHPの変数ということでしたら「$に英数文字が続く」といったパターン指定を使った>方が
>便利かと思います。その場合、KeywordHighlighterのAddRegexメソッドを使って、
>PHPの変数すべてにマッチするような正規表現を登録できます。

いい事聞きました。
こちらで試してみたいと思います。
ありがとうございました。



[メッセージ #74814 への返信]
> 國島さん
>
> 山本(sgry)です。Azukiをお使いいただきありがとうございます。
>
> KeywordHighlighterはキーワードとして、アルファベット順にソートされた
> キーワードのリストしか受け付けられません。そのため例外が発生しているのだと思います。
> あらかじめキーワードをソートしてから指定してみてください。
> # 例外メッセージからすると while より $mes1 が後に来ているのでエラーになっています
>
> なおPHPの変数ということでしたら「$に英数文字が続く」といったパターン指定を使った方が
> 便利かと思います。その場合、KeywordHighlighterのAddRegexメソッドを使って、
> PHPの変数すべてにマッチするような正規表現を登録できます。
> こちらも検討してみてください。
>
>
> よろしくお願いします。
Reply to #74814

Reply to #74815×

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login