LexAsn1.cpp 5.2 KB

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