原創|使用教程|編輯:郝浩|2013-10-09 15:06:51.000|閱讀 1154 次
概述:使用Intellij IDEA的時候,用戶可以根據自身需求定義語法和解析器,下面我們就對這一過程進行詳細的解釋和說明。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
使用Intellij IDEA的時候,用戶可以根據自身需求定義語法和解析器,下面我們就對這一過程進行詳細的解釋和說明。
1 復制解析器工具類
將語法插件需要的GeneratedParserUtilBase類復制到你的源文件中。
2 定義單點登錄類型
package com.simpleplugin.psi; import com.intellij.psi.tree.IElementType; import com.simpleplugin.SimpleLanguage; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; public class SimpleTokenType extends IElementType { public SimpleTokenType(@NotNull @NonNls String debugName) { super(debugName, SimpleLanguage.INSTANCE); } @Override public String toString() { return "SimpleTokenType." + super.toString(); } }
3 定義元素類型
package com.simpleplugin.psi; import com.intellij.psi.tree.IElementType; import com.simpleplugin.SimpleLanguage; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; public class SimpleElementType extends IElementType { public SimpleElementType(@NotNull @NonNls String debugName) { super(debugName, SimpleLanguage.INSTANCE); } }
4 定義語法
以/com/simpleplugin/Simple.bnf文件定義屬性語言語法。
{ parserClass="com.simpleplugin.parser.SimpleParser" stubParserClass="com.simpleplugin.parser.GeneratedParserUtilBase" extends="com.intellij.extapi.psi.ASTWrapperPsiElement" psiClassPrefix="Simple" psiImplClassSuffix="Impl" psiPackage="com.simpleplugin.psi" psiImplPackage="com.simpleplugin.psi.impl" elementTypeHolderClass="com.simpleplugin.psi.SimpleTypes" elementTypeClass="com.simpleplugin.psi.SimpleElementType" tokenTypeClass="com.simpleplugin.psi.SimpleTokenType" } simpleFile ::= item_* private item_ ::= (property|COMMENT|CRLF) property ::= (KEY? SEPARATOR VALUE?) | KEY
這樣一個屬性文件就可以包含屬性、評論和換行了。
語法定義可以使語言支持變得簡單,我們制定的屬性有值也可能沒有值。我們指定一個屬性可能有也可能沒有鍵和值。這讓IDE仍然能夠識別錯誤定義的屬性,并提供相應的代碼分析和快速修正。
5 生成分析器
當語法定義之后,用戶就可以通過右鍵菜單的Generate Parser Code或者是通過Simple.bnf文件中的??G快捷方式根據PSI類生成解析器。Grammar-Kit將在gen文件中生成一個的語法工具解析器和PSI元素。將這個文件夾作為源根,并確保編譯沒有錯誤。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網