• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-341  評論-2670  文章-0  trackbacks-0
                休息了大半個月,沒寫自己的代碼了。國慶過了,再不為自己寫寫代碼就有負罪感了。這篇文章所提到的所有工具的代碼都可以在Vczh Library++ 3.0的頁面找到。上一篇文章提到了一個狀態機繪圖工具,直到最近我終于把他的第一個部分給做好了。現在可以畫圖之后產生一個可以供這里所描述的高亮控件所使用的著色器了。第一步我們要使用TokenizerBuilder繪制一個狀態機:

                然后點擊“Generate to Clipboard”按鈕,就會要求你輸入一個類名然后產生著色器插件的代碼了。上面這個是一個簡化過的C#代碼著色狀態機,其中Id有個星號代表不是所有走到這個狀態的東西都可以叫Id(其實是關鍵字,懶得改了)。中括號里面的是顏色的名稱。這些名稱最終是要拿來生成代碼的,所以必須是C#接受的可以拿來做變量名的東西,不過我也沒檢查,只管拼接字符串生成。

                我把生成后的代碼貼在了CodeForm工程的CSharpTokenizer.Generated.cs里:


                第三步就是要自己建立一個CSharpColorizer.Configuration.cs了。C#的partial class真是偉大,簡直就是為了代碼生成而設計出來的。在這里我們看看生成后的CSharpColorizer.Generated.cs的代碼:
              1 using System;
              2 using System.Collections.Generic;
              3 using System.Linq;
              4 using System.Text;
              5 using CodeBoxControl;
              6 using System.Drawing;
              7 
              8 namespace CodeForm
              9 {
             10     partial class CSharpColorizer : ITextEditorColorizer
             11     {
             12         //public const int NormalColorId = 0;
             13         public const int IdColorId = NormalColorId + 1;
             14         public const int StringColorId = NormalColorId + 2;
             15         public const int CommentColorId = NormalColorId + 3;
             16 
             17         //private readonly Color HighlightColor = Color.FromArgb(173, 214, 255);
             18         //private readonly Color NormalColor = Color.FromArgb(0, 0, 0);
             19         //private readonly Color IdColor = Color.FromArgb(0, 0, 0);
             20         //private readonly Color StringColor = Color.FromArgb(0, 0, 0);
             21         //private readonly Color CommentColor = Color.FromArgb(0, 0, 0);
             22 
             23         private const int StartState = 0;
             24         private const int NormalState = 1;
             25         private const int IdStateId = 2;
             26         private const int InStringStateId = 3;
             27         private const int InStringEscapingStateId = 4;
             28         private const int InCharStateId = 5;
             29         private const int InCharEscapingStateId = 6;
             30         private const int StringStateId = 7;
             31         private const int CommentStartStateId = 8;
             32         private const int SingleLineCommentStateId = 9;
             33         private const int InMultipleLineCommentStateId = 10;
             34         private const int InMultipleLineCommentWaitingToFinishStateId = 11;
             35         private const int MultipleLineCommentStateId = 12;
             36 
             37         private TextEditorColorItem[] colorItems = new TextEditorColorItem[NormalColorId + 4];
             38         private int[] charset = new int[65536];
             39         private int[,] transitions = new int[1311];
             40         private bool[] finalStates = new bool[13];
             41         private int[] stateColors = new int[13];
             42 
             43         public TextEditorColorItem[] ColorItems
             44         {
             45             get
             46             {
             47                 return this.colorItems;
             48             }
             49         }
             50 
             51         public CSharpColorizer()
             52         {
             53             this.colorItems[NormalColorId] = new TextEditorColorItem()
             54             {
             55                 Text = NormalColor,
             56                 HighlightText = NormalColor,
             57                 Highlight = HighlightColor
             58             };
             59             this.colorItems[IdColorId] = new TextEditorColorItem()
             60             {
             61                 Text = IdColor,
             62                 HighlightText = IdColor,
             63                 Highlight = HighlightColor
             64             };
             65             this.colorItems[StringColorId] = new TextEditorColorItem()
             66             {
             67                 Text = StringColor,
             68                 HighlightText = StringColor,
             69                 Highlight = HighlightColor
             70             };
             71             this.colorItems[CommentColorId] = new TextEditorColorItem()
             72             {
             73                 Text = CommentColor,
             74                 HighlightText = CommentColor,
             75                 Highlight = HighlightColor
             76             };
             77             // You should write your own CreateAdditionalColors() implementation to add additional colors.
             78             // You can modify the NormalColorId and put all additional colors ids before NormalColorId.
             79             // It is recommended to use another partial class to store all customized code.
             80             CreateAdditionalColors();
             81             CreateStateMachine();
             82         }
             83         public int ColorizeLine(char[] items, int length, int initialState, int[] colors)
             84         {
             85             int state = initialState;
             86             int itemStart = 0;
             87             int lastFinalState = StartState;
             88 
             89             for (int i = 0; i <= length; i++)
             90             {
             91                 if (i != length)
             92                 {
             93                     state = transitions[state, charset[items[i]]];
             94                     if (state == StartState)
             95                     {
             96                         state = transitions[state, charset[items[i]]];
             97                     }
             98                 }
             99                 else
            100                 {
            101                     lastFinalState = state;
            102                 }
            103 
            104                 if (i == length || lastFinalState != state && lastFinalState != StartState)
            105                 {
            106                     int color = stateColors[lastFinalState];
            107                     switch (lastFinalState)
            108                     {
            109                         case IdStateId:
            110                             // You should write your own IsValidId implementation.
            111                             color = IsValidId(new string(items, itemStart, Math.Min(i, length) - itemStart)) ? stateColors[lastFinalState] : NormalColorId;
            112                             break;
            113                     }
            114                     for (int j = itemStart; j < i; j++)
            115                     {
            116                         colors[j] = color;
            117                     }
            118                     itemStart = i;
            119                 }
            120                 lastFinalState = finalStates[state] ? state : StartState;
            121             }
            122 
            123             return transitions[state, charset['\n']];
            124         }
            125 
            126         private void CreateStateMachine()
            127         {
            128             for (int i = 0; i < 10; i++)
            129                 charset[i] = 0;
            130             for (int i = 10; i < 11; i++)
            131                 charset[i] = 1;
            132             for (int i = 11; i < 34; i++)
            133                 charset[i] = 0;
            134             for (int i = 34; i < 35; i++)
            135                 charset[i] = 2;
            136             for (int i = 35; i < 36; i++)
            137                 charset[i] = 3;
            138             for (int i = 36; i < 39; i++)
            139                 charset[i] = 0;
            140             for (int i = 39; i < 40; i++)
            141                 charset[i] = 4;
            142             for (int i = 40; i < 42; i++)
            143                 charset[i] = 0;
            144             for (int i = 42; i < 43; i++)
            145                 charset[i] = 5;
            146             for (int i = 43; i < 47; i++)
            147                 charset[i] = 0;
            148             for (int i = 47; i < 48; i++)
            149                 charset[i] = 6;
            150             for (int i = 48; i < 58; i++)
            151                 charset[i] = 7;
            152             for (int i = 58; i < 65; i++)
            153                 charset[i] = 0;
            154             for (int i = 65; i < 91; i++)
            155                 charset[i] = 8;
            156             for (int i = 91; i < 92; i++)
            157                 charset[i] = 0;
            158             for (int i = 92; i < 93; i++)
            159                 charset[i] = 9;
            160             for (int i = 93; i < 95; i++)
            161                 charset[i] = 0;
            162             for (int i = 95; i < 96; i++)
            163                 charset[i] = 8;
            164             for (int i = 96; i < 97; i++)
            165                 charset[i] = 0;
            166             for (int i = 97; i < 123; i++)
            167                 charset[i] = 8;
            168             for (int i = 123; i < 65536; i++)
            169                 charset[i] = 0;
            170 
            171             finalStates[0= false;
            172             finalStates[1= true;
            173             finalStates[2= true;
            174             finalStates[3= false;
            175             finalStates[4= false;
            176             finalStates[5= false;
            177             finalStates[6= false;
            178             finalStates[7= true;
            179             finalStates[8= false;
            180             finalStates[9= true;
            181             finalStates[10= false;
            182             finalStates[11= false;
            183             finalStates[12= true;
            184 
            185             stateColors[0= NormalColorId + 0;
            186             stateColors[1= NormalColorId + 0;
            187             stateColors[2= NormalColorId + 1;
            188             stateColors[3= NormalColorId + 2;
            189             stateColors[4= NormalColorId + 2;
            190             stateColors[5= NormalColorId + 2;
            191             stateColors[6= NormalColorId + 2;
            192             stateColors[7= NormalColorId + 2;
            193             stateColors[8= NormalColorId + 0;
            194             stateColors[9= NormalColorId + 3;
            195             stateColors[10= NormalColorId + 3;
            196             stateColors[11= NormalColorId + 3;
            197             stateColors[12= NormalColorId + 3;
            198 
            199             transitions[00= 1;
            200             transitions[01= 1;
            201             transitions[02= 3;
            202             transitions[03= 2;
            203             transitions[04= 5;
            204             transitions[05= 1;
            205             transitions[06= 8;
            206             transitions[07= 1;
            207             transitions[08= 2;
            208             transitions[09= 1;
            209             transitions[010= 1;
            210             transitions[10= 0;
            211             transitions[11= 0;
            212             transitions[12= 0;
            213             transitions[13= 0;
            214             transitions[14= 0;
            215             transitions[15= 0;
            216             transitions[16= 0;
            217             transitions[17= 0;
            218             transitions[18= 0;
            219             transitions[19= 0;
            220             transitions[110= 0;
            221             transitions[20= 0;
            222             transitions[21= 0;
            223             transitions[22= 0;
            224             transitions[23= 0;
            225             transitions[24= 0;
            226             transitions[25= 0;
            227             transitions[26= 0;
            228             transitions[27= 2;
            229             transitions[28= 2;
            230             transitions[29= 0;
            231             transitions[210= 0;
            232             transitions[30= 3;
            233             transitions[31= 0;
            234             transitions[32= 7;
            235             transitions[33= 3;
            236             transitions[34= 3;
            237             transitions[35= 3;
            238             transitions[36= 3;
            239             transitions[37= 3;
            240             transitions[38= 3;
            241             transitions[39= 4;
            242             transitions[310= 0;
            243             transitions[40= 3;
            244             transitions[41= 0;
            245             transitions[42= 3;
            246             transitions[43= 3;
            247             transitions[44= 3;
            248             transitions[45= 3;
            249             transitions[46= 3;
            250             transitions[47= 3;
            251             transitions[48= 3;
            252             transitions[49= 3;
            253             transitions[410= 0;
            254             transitions[50= 5;
            255             transitions[51= 0;
            256             transitions[52= 5;
            257             transitions[53= 5;
            258             transitions[54= 7;
            259             transitions[55= 5;
            260             transitions[56= 5;
            261             transitions[57= 5;
            262             transitions[58= 5;
            263             transitions[59= 6;
            264             transitions[510= 0;
            265             transitions[60= 5;
            266             transitions[61= 0;
            267             transitions[62= 5;
            268             transitions[63= 5;
            269             transitions[64= 5;
            270             transitions[65= 5;
            271             transitions[66= 5;
            272             transitions[67= 5;
            273             transitions[68= 5;
            274             transitions[69= 5;
            275             transitions[610= 0;
            276             transitions[70= 0;
            277             transitions[71= 0;
            278             transitions[72= 0;
            279             transitions[73= 0;
            280             transitions[74= 0;
            281             transitions[75= 0;
            282             transitions[76= 0;
            283             transitions[77= 0;
            284             transitions[78= 0;
            285             transitions[79= 0;
            286             transitions[710= 0;
            287             transitions[80= 1;
            288             transitions[81= 1;
            289             transitions[82= 1;
            290             transitions[83= 1;
            291             transitions[84= 1;
            292             transitions[85= 10;
            293             transitions[86= 9;
            294             transitions[87= 1;
            295             transitions[88= 1;
            296             transitions[89= 1;
            297             transitions[810= 1;
            298             transitions[90= 9;
            299             transitions[91= 0;
            300             transitions[92= 9;
            301             transitions[93= 9;
            302             transitions[94= 9;
            303             transitions[95= 9;
            304             transitions[96= 9;
            305             transitions[97= 9;
            306             transitions[98= 9;
            307             transitions[99= 9;
            308             transitions[910= 0;
            309             transitions[100= 10;
            310             transitions[101= 10;
            311             transitions[102= 10;
            312             transitions[103= 10;
            313             transitions[104= 10;
            314             transitions[105= 11;
            315             transitions[106= 0;
            316             transitions[107= 10;
            317             transitions[108= 10;
            318             transitions[109= 10;
            319             transitions[1010= 0;
            320             transitions[110= 10;
            321             transitions[111= 10;
            322             transitions[112= 10;
            323             transitions[113= 10;
            324             transitions[114= 10;
            325             transitions[115= 10;
            326             transitions[116= 12;
            327             transitions[117= 10;
            328             transitions[118= 10;
            329             transitions[119= 10;
            330             transitions[1110= 0;
            331             transitions[120= 0;
            332             transitions[121= 0;
            333             transitions[122= 0;
            334             transitions[123= 0;
            335             transitions[124= 0;
            336             transitions[125= 0;
            337             transitions[126= 0;
            338             transitions[127= 0;
            339             transitions[128= 0;
            340             transitions[129= 0;
            341             transitions[1210= 0;
            342         }
            343     }
            344 }
            345 

                這是一個典型的狀態機,里面定義了所有狀態、顏色和對外可見的顏色標號。這里使用partial class是因為顏色是要你自己填寫的,因此你可以看見我在代碼的一開始注釋掉了幾行,所以他們就會出現在CSharpColorizer.Configuration.cs里:
              1 using System;
              2 using System.Collections.Generic;
              3 using System.Linq;
              4 using System.Text;
              5 using CodeBoxControl;
              6 using System.Drawing;
              7 
              8 namespace CodeForm
              9 {
             10     partial class CSharpColorizer
             11     {
             12         public const int BreakPointColorId = 0;
             13         public const int BlockPointColorId = 1;
             14         public const int NormalColorId = 2;
             15 
             16         private readonly Color HighlightColor = Color.FromArgb(173214255);
             17         private readonly Color NormalColor = Color.FromArgb(000);
             18         private readonly Color IdColor = Color.FromArgb(00255);
             19         private readonly Color StringColor = Color.FromArgb(1632121);
             20         private readonly Color CommentColor = Color.FromArgb(01280);
             21 
             22         private readonly Color BreakPointColor = Color.FromArgb(255255255);
             23         private readonly Color BreakPointHighlightColor = Color.FromArgb(123119166);
             24         private readonly Color BlockPointColor = Color.Gray;
             25 
             26         private void CreateAdditionalColors()
             27         {
             28             this.colorItems[BreakPointColorId] = new TextEditorColorItem()
             29             {
             30                 Text = BreakPointColor,
             31                 HighlightText = BreakPointColor,
             32                 Highlight = BreakPointHighlightColor
             33             };
             34             this.colorItems[BlockPointColorId] = new TextEditorColorItem()
             35             {
             36                 Text = BlockPointColor,
             37                 HighlightText = BlockPointColor,
             38                 Highlight = HighlightColor
             39             };
             40         }
             41 
             42         private bool IsValidId(string token)
             43         {
             44             return token[0== '#' || Array.BinarySearch(keywords, token) >= 0;
             45         }
             46 
             47         private static string[] keywords ={
             48             "abstract",
             49             "as",
             50             "base",
             51             "bool",
             52             "break",
             53             "byte",
             54             "case",
             55             "catch",
             56             "char",
             57             "checked",
             58             "class",
             59             "const",
             60             "continue",
             61             "decimal",
             62             "default",
             63             "delegate",
             64             "do",
             65             "double",
             66             "else",
             67             "enum",
             68             "event",
             69             "explicit",
             70             "extern",
             71             "false",
             72             "finally",
             73             "fixed",
             74             "float",
             75             "for",
             76             "foreach",
             77             "goto",
             78             "if",
             79             "implicit",
             80             "in",
             81             "int",
             82             "interface",
             83             "internal",
             84             "is",
             85             "lock",
             86             "long",
             87             "namespace",
             88             "new",
             89             "null",
             90             "object",
             91             "operator",
             92             "out",
             93             "override",
             94             "params",
             95             "private",
             96             "protected",
             97             "public",
             98             "readonly",
             99             "ref",
            100             "return",
            101             "sbyte",
            102             "sealed",
            103             "short",
            104             "sizeof",
            105             "stackalloc",
            106             "static",
            107             "string",
            108             "struct",
            109             "switch",
            110             "this",
            111             "throw",
            112             "true",
            113             "try",
            114             "typeof",
            115             "unit",
            116             "ulong",
            117             "unchecked",
            118             "unsafe",
            119             "ushort",
            120             "using",
            121             "virtual",
            122             "void",
            123             "volatile",
            124             "while"
            125         };
            126     }
            127 }
            128 

                這個Configuration做了三件事情。第一、定義了額外的顏色。在這里我們修改了NormalColorId,這是系統顏色的第一個顏色序號。我們只需要修改了它,然后把自己的顏色加在它前面就行了。第二件事情是定義了什么是Id。我們在Id狀態里打了個星號,那么生成的代碼就會要求我們實現一個IsValidId函數。第三件事情就是我們指定了所有記號的真正的顏色。

                為什么要做成partial class呢?我們可以很容易看出來,每一次生成代碼的時候,我們只需要修改namespace和類名以及注釋掉NormalColorId和所有顏色的聲明,就可以讓我們的自定義部分得到保留。這無疑很方便我們對著色器進行修改。只需要修改一下狀態機,生成一份代碼,再做一點很小的改動就行了。

                最重要的是,這個著色器的性能跟手寫的基本一樣優秀,因此從現在開始,我們開發一個上下文無關的著色器控件就基本不需要寫代碼了,只要是用Vczh Library++ 3.0實驗庫里面的TextEditorControl,再用這個TokenizerBuilder畫個狀態機生成個著色器的代碼就搞定了,哇哈哈。下面要做的就是增強TokenizerBuilder,從狀態機生成詞法分析器了,然后就可以繼續智能提示的實驗了。
            posted on 2010-10-08 06:05 陳梓瀚(vczh) 閱讀(6780) 評論(8)  編輯 收藏 引用 所屬分類: 開發自己的IDE

            評論:
            # re: 開發自己的IDE(五) 2010-10-08 06:11 | 沙發
            沙發  回復  更多評論
              
            # re: 開發自己的IDE(五) 2010-10-08 08:25 | ladeng
            國A威武  回復  更多評論
              
            # re: 開發自己的IDE(五) 2010-10-08 17:33 | DiryBoy
            集成到VS中去吧~~  回復  更多評論
              
            # re: 開發自己的IDE(五)[未登錄] 2010-10-08 19:09 | megax
            弄個HTML里面嵌入各種語言的的試試,呵呵  回復  更多評論
              
            # re: 開發自己的IDE(五) 2010-10-08 20:54 | 陳梓瀚(vczh)
            @megax
            這是上下文有關著色哈,暫時不會為這種東西做自動生成代碼的工具,手寫就好了。  回復  更多評論
              
            # re: 開發自己的IDE(五) 2010-10-09 23:18 | 程建磊
            老大,你先在干什么呢??整的這么深奧,我是程建磊  回復  更多評論
              
            # re: 開發自己的IDE(五) 2010-10-10 18:35 | 陳梓瀚(vczh)
            @程建磊
            多直白啊,就是我做了一個代碼高亮編輯器生成器  回復  更多評論
              
            # re: 開發自己的IDE(五) 2010-10-12 07:45 |
            vczh依然是對語法分析情有獨鐘啊.......  回復  更多評論
              
            久久久久亚洲av毛片大 | 久久久久亚洲av无码专区 | 国产Av激情久久无码天堂 | 久久综合亚洲鲁鲁五月天| 免费精品久久天干天干| 久久99国产综合精品免费| 久久亚洲国产成人影院网站 | 香蕉久久夜色精品国产2020| 久久丫精品国产亚洲av| 久久久精品国产亚洲成人满18免费网站 | 久久综合香蕉国产蜜臀AV| 99热成人精品免费久久| 亚洲av成人无码久久精品| 精品久久久久久无码中文字幕 | 伊人久久免费视频| 亚洲成色WWW久久网站| 久久无码一区二区三区少妇| 丰满少妇人妻久久久久久| 免费无码国产欧美久久18| 精品久久久久中文字| 高清免费久久午夜精品| 一本色道久久88精品综合| 亚洲а∨天堂久久精品| 品成人欧美大片久久国产欧美| 久久精品无码午夜福利理论片 | 99久久国产亚洲高清观看2024 | 国产亚州精品女人久久久久久 | 国产精品久久免费| 久久久久亚洲精品天堂| 久久久久亚洲AV无码专区首JN | 国内精品久久久久影院亚洲| 性做久久久久久久久久久| 久久青青国产| 欧美色综合久久久久久| 久久综合精品国产一区二区三区 | 亚洲精品美女久久777777| 久久久久亚洲AV无码观看| 色天使久久综合网天天| 精品一二三区久久aaa片| 久久国产AVJUST麻豆| 国产69精品久久久久观看软件 |