1 // Scintilla source code edit control
2 /** @file LexAsn1.cxx
3 ** Lexer for ASN.1
4 **/
5 // Copyright 2004 by Herr Pfarrer rpfarrer <at> yahoo <dot> de
6 // Last Updated: 20/07/2004
7 // The License.txt file describes the conditions under which this software may be distributed.
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <assert.h>
14
15 #include "ILexer.h"
16 #include "Scintilla.h"
17 #include "SciLexer.h"
18
19 #include "WordList.h"
20 #include "LexAccessor.h"
21 #include "Accessor.h"
22 #include "StyleContext.h"
23 #include "CharacterSet.h"
24 #include "LexerModule.h"
25
26 using namespace Scintilla;
27
28 // Some char test functions
isAsn1Number(int ch)29 static bool isAsn1Number(int ch)
30 {
31 return (ch >= '0' && ch <= '9');
32 }
33
isAsn1Letter(int ch)34 static bool isAsn1Letter(int ch)
35 {
36 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
37 }
38
isAsn1Char(int ch)39 static bool isAsn1Char(int ch)
40 {
41 return (ch == '-' ) || isAsn1Number(ch) || isAsn1Letter (ch);
42 }
43
44 //
45 // Function determining the color of a given code portion
46 // Based on a "state"
47 //
ColouriseAsn1Doc(Sci_PositionU startPos,Sci_Position length,int initStyle,WordList * keywordLists[],Accessor & styler)48 static void ColouriseAsn1Doc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordLists[], Accessor &styler)
49 {
50 // The keywords
51 WordList &Keywords = *keywordLists[0];
52 WordList &Attributes = *keywordLists[1];
53 WordList &Descriptors = *keywordLists[2];
54 WordList &Types = *keywordLists[3];
55
56 // Parse the whole buffer character by character using StyleContext
57 StyleContext sc(startPos, length, initStyle, styler);
58 for (; sc.More(); sc.Forward())
59 {
60 // The state engine
61 switch (sc.state)
62 {
63 case SCE_ASN1_DEFAULT: // Plain characters
64 asn1_default:
65 if (sc.ch == '-' && sc.chNext == '-')
66 // A comment begins here
67 sc.SetState(SCE_ASN1_COMMENT);
68 else if (sc.ch == '"')
69 // A string begins here
70 sc.SetState(SCE_ASN1_STRING);
71 else if (isAsn1Number (sc.ch))
72 // A number starts here (identifier should start with a letter in ASN.1)
73 sc.SetState(SCE_ASN1_SCALAR);
74 else if (isAsn1Char (sc.ch))
75 // An identifier starts here (identifier always start with a letter)
76 sc.SetState(SCE_ASN1_IDENTIFIER);
77 else if (sc.ch == ':')
78 // A ::= operator starts here
79 sc.SetState(SCE_ASN1_OPERATOR);
80 break;
81 case SCE_ASN1_COMMENT: // A comment
82 if (sc.ch == '\r' || sc.ch == '\n')
83 // A comment ends here
84 sc.SetState(SCE_ASN1_DEFAULT);
85 break;
86 case SCE_ASN1_IDENTIFIER: // An identifier (keyword, attribute, descriptor or type)
87 if (!isAsn1Char (sc.ch))
88 {
89 // The end of identifier is here: we can look for it in lists by now and change its state
90 char s[100];
91 sc.GetCurrent(s, sizeof(s));
92 if (Keywords.InList(s))
93 // It's a keyword, change its state
94 sc.ChangeState(SCE_ASN1_KEYWORD);
95 else if (Attributes.InList(s))
96 // It's an attribute, change its state
97 sc.ChangeState(SCE_ASN1_ATTRIBUTE);
98 else if (Descriptors.InList(s))
99 // It's a descriptor, change its state
100 sc.ChangeState(SCE_ASN1_DESCRIPTOR);
101 else if (Types.InList(s))
102 // It's a type, change its state
103 sc.ChangeState(SCE_ASN1_TYPE);
104
105 // Set to default now
106 sc.SetState(SCE_ASN1_DEFAULT);
107 }
108 break;
109 case SCE_ASN1_STRING: // A string delimited by ""
110 if (sc.ch == '"')
111 {
112 // A string ends here
113 sc.ForwardSetState(SCE_ASN1_DEFAULT);
114
115 // To correctly manage a char sticking to the string quote
116 goto asn1_default;
117 }
118 break;
119 case SCE_ASN1_SCALAR: // A plain number
120 if (!isAsn1Number (sc.ch))
121 // A number ends here
122 sc.SetState(SCE_ASN1_DEFAULT);
123 break;
124 case SCE_ASN1_OPERATOR: // The affectation operator ::= and wath follows (eg: ::= { org 6 } OID or ::= 12 trap)
125 if (sc.ch == '{')
126 {
127 // An OID definition starts here: enter the sub loop
128 for (; sc.More(); sc.Forward())
129 {
130 if (isAsn1Number (sc.ch) && (!isAsn1Char (sc.chPrev) || isAsn1Number (sc.chPrev)))
131 // The OID number is highlighted
132 sc.SetState(SCE_ASN1_OID);
133 else if (isAsn1Char (sc.ch))
134 // The OID parent identifier is plain
135 sc.SetState(SCE_ASN1_IDENTIFIER);
136 else
137 sc.SetState(SCE_ASN1_DEFAULT);
138
139 if (sc.ch == '}')
140 // Here ends the OID and the operator sub loop: go back to main loop
141 break;
142 }
143 }
144 else if (isAsn1Number (sc.ch))
145 {
146 // A trap number definition starts here: enter the sub loop
147 for (; sc.More(); sc.Forward())
148 {
149 if (isAsn1Number (sc.ch))
150 // The trap number is highlighted
151 sc.SetState(SCE_ASN1_OID);
152 else
153 {
154 // The number ends here: go back to main loop
155 sc.SetState(SCE_ASN1_DEFAULT);
156 break;
157 }
158 }
159 }
160 else if (sc.ch != ':' && sc.ch != '=' && sc.ch != ' ')
161 // The operator doesn't imply an OID definition nor a trap, back to main loop
162 goto asn1_default; // To be sure to handle actually the state change
163 break;
164 }
165 }
166 sc.Complete();
167 }
168
FoldAsn1Doc(Sci_PositionU,Sci_Position,int,WordList * [],Accessor & styler)169 static void FoldAsn1Doc(Sci_PositionU, Sci_Position, int, WordList *[], Accessor &styler)
170 {
171 // No folding enabled, no reason to continue...
172 if( styler.GetPropertyInt("fold") == 0 )
173 return;
174
175 // No folding implemented: doesn't make sense for ASN.1
176 }
177
178 static const char * const asn1WordLists[] = {
179 "Keywords",
180 "Attributes",
181 "Descriptors",
182 "Types",
183 0, };
184
185
186 LexerModule lmAsn1(SCLEX_ASN1, ColouriseAsn1Doc, "asn1", FoldAsn1Doc, asn1WordLists);
187