LexSorcus.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Scintilla source code edit control
  2. /** @file LexSorcus.cxx
  3. ** Lexer for SORCUS installation files
  4. ** Written by Eugen Bitter and Christoph Baumann at SORCUS Computer, Heidelberg Germany
  5. ** Based on the ASM Lexer by The Black Horus
  6. **/
  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 <ctype.h>
  14. #include "ILexer.h"
  15. #include "Scintilla.h"
  16. #include "SciLexer.h"
  17. #include "WordList.h"
  18. #include "LexAccessor.h"
  19. #include "Accessor.h"
  20. #include "StyleContext.h"
  21. #include "CharacterSet.h"
  22. #include "LexerModule.h"
  23. #ifdef SCI_NAMESPACE
  24. using namespace Scintilla;
  25. #endif
  26. //each character a..z and A..Z + '_' can be part of a keyword
  27. //additionally numbers that follow 'M' can be contained in a keyword
  28. static inline bool IsSWordStart(const int ch, const int prev_ch)
  29. {
  30. if (isalpha(ch) || (ch == '_') || ((isdigit(ch)) && (prev_ch == 'M')))
  31. return true;
  32. return false;
  33. }
  34. //only digits that are not preceded by 'M' count as a number
  35. static inline bool IsSorcusNumber(const int ch, const int prev_ch)
  36. {
  37. if ((isdigit(ch)) && (prev_ch != 'M'))
  38. return true;
  39. return false;
  40. }
  41. //only = is a valid operator
  42. static inline bool IsSorcusOperator(const int ch)
  43. {
  44. if (ch == '=')
  45. return true;
  46. return false;
  47. }
  48. static void ColouriseSorcusDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
  49. Accessor &styler)
  50. {
  51. WordList &Command = *keywordlists[0];
  52. WordList &Parameter = *keywordlists[1];
  53. WordList &Constant = *keywordlists[2];
  54. // Do not leak onto next line
  55. if (initStyle == SCE_SORCUS_STRINGEOL)
  56. initStyle = SCE_SORCUS_DEFAULT;
  57. StyleContext sc(startPos, length, initStyle, styler);
  58. for (; sc.More(); sc.Forward())
  59. {
  60. // Prevent SCE_SORCUS_STRINGEOL from leaking back to previous line
  61. if (sc.atLineStart && (sc.state == SCE_SORCUS_STRING))
  62. {
  63. sc.SetState(SCE_SORCUS_STRING);
  64. }
  65. // Determine if the current state should terminate.
  66. if (sc.state == SCE_SORCUS_OPERATOR)
  67. {
  68. if (!IsSorcusOperator(sc.ch))
  69. {
  70. sc.SetState(SCE_SORCUS_DEFAULT);
  71. }
  72. }
  73. else if(sc.state == SCE_SORCUS_NUMBER)
  74. {
  75. if(!IsSorcusNumber(sc.ch, sc.chPrev))
  76. {
  77. sc.SetState(SCE_SORCUS_DEFAULT);
  78. }
  79. }
  80. else if (sc.state == SCE_SORCUS_IDENTIFIER)
  81. {
  82. if (!IsSWordStart(sc.ch, sc.chPrev))
  83. {
  84. char s[100];
  85. sc.GetCurrent(s, sizeof(s));
  86. if (Command.InList(s))
  87. {
  88. sc.ChangeState(SCE_SORCUS_COMMAND);
  89. }
  90. else if (Parameter.InList(s))
  91. {
  92. sc.ChangeState(SCE_SORCUS_PARAMETER);
  93. }
  94. else if (Constant.InList(s))
  95. {
  96. sc.ChangeState(SCE_SORCUS_CONSTANT);
  97. }
  98. sc.SetState(SCE_SORCUS_DEFAULT);
  99. }
  100. }
  101. else if (sc.state == SCE_SORCUS_COMMENTLINE )
  102. {
  103. if (sc.atLineEnd)
  104. {
  105. sc.SetState(SCE_SORCUS_DEFAULT);
  106. }
  107. }
  108. else if (sc.state == SCE_SORCUS_STRING)
  109. {
  110. if (sc.ch == '\"')
  111. {
  112. sc.ForwardSetState(SCE_SORCUS_DEFAULT);
  113. }
  114. else if (sc.atLineEnd)
  115. {
  116. sc.ChangeState(SCE_SORCUS_STRINGEOL);
  117. sc.ForwardSetState(SCE_SORCUS_DEFAULT);
  118. }
  119. }
  120. // Determine if a new state should be entered.
  121. if (sc.state == SCE_SORCUS_DEFAULT)
  122. {
  123. if ((sc.ch == ';') || (sc.ch == '\''))
  124. {
  125. sc.SetState(SCE_SORCUS_COMMENTLINE);
  126. }
  127. else if (IsSWordStart(sc.ch, sc.chPrev))
  128. {
  129. sc.SetState(SCE_SORCUS_IDENTIFIER);
  130. }
  131. else if (sc.ch == '\"')
  132. {
  133. sc.SetState(SCE_SORCUS_STRING);
  134. }
  135. else if (IsSorcusOperator(sc.ch))
  136. {
  137. sc.SetState(SCE_SORCUS_OPERATOR);
  138. }
  139. else if (IsSorcusNumber(sc.ch, sc.chPrev))
  140. {
  141. sc.SetState(SCE_SORCUS_NUMBER);
  142. }
  143. }
  144. }
  145. sc.Complete();
  146. }
  147. static const char* const SorcusWordListDesc[] = {"Command","Parameter", "Constant", 0};
  148. LexerModule lmSorc(SCLEX_SORCUS, ColouriseSorcusDoc, "sorcins", 0, SorcusWordListDesc);