2012年3月8日 星期四

Source Insight的語法

目前的source code是使用EDK2的code base,
常常會有一些變數的定義與EDK1的不一樣,
因此,在Source Insight會沒有辦法辦識出哪一些是變數,
這個時候,就要研究如何設定Source Insight的語法,讓我可以去辦識特定的文件。

而我想Source Insight做的到的,VIM應該都做的到,在Source Insight設定完後,就要試著移值到vim啦。

這裡說明一下,Source Insight的成員
1. 語法 (Language)
在Source Insight有語法(Language)就是表示,在什麼時候文字檔的內容是要亮什麼顏色、什麼情況下代表變數、什麼情況下代表常數。
2. Document Type
Source Insight提供了一個Document Type,可以設定成許多副檔名算是在同一類。
像是*.c與*.h就是同一類的語法

新增一個語法 (Language)
Options→Document Options→Parsing
選擇Language這一個button

這個時候可以點選Language裡面的許多語法,去看裡面的設定,或是修改一些設定~
當然也可以新增一個Language,這裡可以試著新增一個叫做TEST的語法。

我們先新增comment的條件,就是當什麼情況下,它會判斷成是comment的語法。

目前以#當作註解,設定如下

並且記得要把Use Syntax Formatting勾選起來,這樣註解的顏色才會變成註解的顏色!

==========
下面有Language的Options的簡單說明~
Basic Language Options
Each language has basic options that govern how Source Insight treats files with this language. Built-in languages, such as C/C++ have fewer options in this page than do custom languages.

Contains program source code
If checked, then Source Insight will consider this a programming language. Certain features are altered when a programming language is used, as opposed to a simple textual language. For instance, references to declared symbols are displayed in the “Ref to …” styles.

Case sensitive text
This indicates whether the language is case sensitive or not. This affects how keywords are matched, as well as how symbols names are resolved in the symbol lookup engine.

Uses C preprocessor
If checked, then Source Insight will recognize #if and #ifdef preprocessor directives.

Allow smart tab
If checked, then the Smart Tab feature will be enabled when editing this type of language. If not checked, then Smart Tab will perform like a simple tab.

Use Syntax Formatting
If checked, then Syntax Formatting will be used when displaying files in this language.

Detect comment styles
If checked, then special comment styles will be detected. See also “Comment Styles”.

Non-AlphaNum identifier chars
This text box contains the set of all valid non-alpha-numeric identifier characters. Alpha-numeric strings are always considered identifiers.

Detect numbers
If checked, then numbers found in the text are formatted with the “Number” style. The check boxes following this enable special number formats for hex and octal numbers.
==========

新增一個Document Type
Options→Document Options→Add Type...

新增一個名為TEST的Document Type,
File filter選擇規類為這個Document Type的檔名名稱,
而語法(Language)則設定為剛剛新增的Language,
如此一來,就可以把剛剛設定的語法顯示出來了。

再來就是把特定的副檔名的檔案加入到選定的Document Type
另外一提的是,只有副檔名在這一些Document Type裡面的檔案同時Include when adding to projects這一個選項有被勾選時,符合File filter的檔案名稱,才會在search檔案時,才會加到這一個Project裡面。

==========
如果在A這一個檔案定義了一個全域常數,要讓其它檔案就可以透過Source Insight的Context Windows直接就可以查到這一個全域常數的值,但是,很不巧的是,我們定義這一個全域常數的語法,並沒有在Source Insight的資料庫內,這個時候,就必需要使用自定的語法了~

例如我們在test.test檔案裡面定義了以下的內容

  DEFINE FIRST_VARIABLE      = TRUE
DEFINE SECOND_VARIABLE = FALSE
DEFINE THIRD_VARIABLE = FALSE


那麼我們就在Language的Custom Parsing加入以下的內容,並且記得要把Use regular expressions for parsing勾選起來。

這裡面填的語法是
\w*DEFINE\w+\([A-Za-z0-9_]+\)
\w表示是space或是tab鍵
*是表示前面的字元出現0次或1次以上
DEFINE這裡就是表示是有這一個字串
\w同上面的解釋
+表示前面的字元出現1次以上
[A-Za-z0-9_]是表是一個字元,而其字元有可能是由所有的字母或是數字或是_其中一個

到目前為止,都是正規表示式的語法,接下這一個是非常重要的。
括號包起來的部分,就是我們要找的變數
這樣我們才可以由A這一個檔案去依這一個變數,去尋找到B這一個檔案是如何定義這一個變數的。
這樣就會有以下的效果啦~~這樣在Trace Code會比較方便~

另外一提,我有試著用兩個括號,看能不能有兩個關鍵字,結果看來是沒有辦法的~

另外,不知道為什麼,若把User C preprocessor選取後,就必需要用\w*DEFINE\w+\([A-Za-z0-9_]+\)才可以認得到
  DEFINE FIRST_VARIABLE      = TRUE
DEFINE SECOND_VARIABLE = FALSE
DEFINE THIRD_VARIABLE = FALSE

若把User C preprocessor取消選取後,使用DEFINE\s+\([A-Za-z0-9_]+\)就可以識別出這一個變數了。
反正,這一個檔案不支援User C preprocessor,應該也不需要勾選了。
==========

參考資料:
Q: How can I add my own parsing?
Source Insight使用之添加自定义语言
SourceInsight

沒有留言: