]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexAsm.cxx
17c938420a043e80c0f2bccef6333edaaae5ab88
[wxWidgets.git] / src / stc / scintilla / src / LexAsm.cxx
1 // Scintilla source code edit control
2 /** @file LexAsm.cxx
3 ** Lexer for Assembler, just for the MASM syntax
4 ** Written by The Black Horus
5 ** Enhancements and NASM stuff by Kein-Hong Man, 2003-10
6 ** SCE_ASM_COMMENTBLOCK and SCE_ASM_CHARACTER are for future GNU as colouring
7 **/
8 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
9 // The License.txt file describes the conditions under which this software may be distributed.
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <stdio.h>
15 #include <stdarg.h>
16
17 #include "Platform.h"
18
19 #include "PropSet.h"
20 #include "Accessor.h"
21 #include "StyleContext.h"
22 #include "KeyWords.h"
23 #include "Scintilla.h"
24 #include "SciLexer.h"
25
26 #ifdef SCI_NAMESPACE
27 using namespace Scintilla;
28 #endif
29
30 static inline bool IsAWordChar(const int ch) {
31 return (ch < 0x80) && (isalnum(ch) || ch == '.' ||
32 ch == '_' || ch == '?');
33 }
34
35 static inline bool IsAWordStart(const int ch) {
36 return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '.' ||
37 ch == '%' || ch == '@' || ch == '$' || ch == '?');
38 }
39
40 static inline bool IsAsmOperator(const int ch) {
41 if ((ch < 0x80) && (isalnum(ch)))
42 return false;
43 // '.' left out as it is used to make up numbers
44 if (ch == '*' || ch == '/' || ch == '-' || ch == '+' ||
45 ch == '(' || ch == ')' || ch == '=' || ch == '^' ||
46 ch == '[' || ch == ']' || ch == '<' || ch == '&' ||
47 ch == '>' || ch == ',' || ch == '|' || ch == '~' ||
48 ch == '%' || ch == ':')
49 return true;
50 return false;
51 }
52
53 static void ColouriseAsmDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
54 Accessor &styler) {
55
56 WordList &cpuInstruction = *keywordlists[0];
57 WordList &mathInstruction = *keywordlists[1];
58 WordList &registers = *keywordlists[2];
59 WordList &directive = *keywordlists[3];
60 WordList &directiveOperand = *keywordlists[4];
61 WordList &extInstruction = *keywordlists[5];
62
63 // Do not leak onto next line
64 if (initStyle == SCE_ASM_STRINGEOL)
65 initStyle = SCE_ASM_DEFAULT;
66
67 StyleContext sc(startPos, length, initStyle, styler);
68
69 for (; sc.More(); sc.Forward())
70 {
71
72 // Prevent SCE_ASM_STRINGEOL from leaking back to previous line
73 if (sc.atLineStart && (sc.state == SCE_ASM_STRING)) {
74 sc.SetState(SCE_ASM_STRING);
75 } else if (sc.atLineStart && (sc.state == SCE_ASM_CHARACTER)) {
76 sc.SetState(SCE_ASM_CHARACTER);
77 }
78
79 // Handle line continuation generically.
80 if (sc.ch == '\\') {
81 if (sc.chNext == '\n' || sc.chNext == '\r') {
82 sc.Forward();
83 if (sc.ch == '\r' && sc.chNext == '\n') {
84 sc.Forward();
85 }
86 continue;
87 }
88 }
89
90 // Determine if the current state should terminate.
91 if (sc.state == SCE_ASM_OPERATOR) {
92 if (!IsAsmOperator(sc.ch)) {
93 sc.SetState(SCE_ASM_DEFAULT);
94 }
95 }else if (sc.state == SCE_ASM_NUMBER) {
96 if (!IsAWordChar(sc.ch)) {
97 sc.SetState(SCE_ASM_DEFAULT);
98 }
99 } else if (sc.state == SCE_ASM_IDENTIFIER) {
100 if (!IsAWordChar(sc.ch) ) {
101 char s[100];
102 sc.GetCurrentLowered(s, sizeof(s));
103
104 if (cpuInstruction.InList(s)) {
105 sc.ChangeState(SCE_ASM_CPUINSTRUCTION);
106 } else if (mathInstruction.InList(s)) {
107 sc.ChangeState(SCE_ASM_MATHINSTRUCTION);
108 } else if (registers.InList(s)) {
109 sc.ChangeState(SCE_ASM_REGISTER);
110 } else if (directive.InList(s)) {
111 sc.ChangeState(SCE_ASM_DIRECTIVE);
112 } else if (directiveOperand.InList(s)) {
113 sc.ChangeState(SCE_ASM_DIRECTIVEOPERAND);
114 } else if (extInstruction.InList(s)) {
115 sc.ChangeState(SCE_ASM_EXTINSTRUCTION);
116 }
117 sc.SetState(SCE_ASM_DEFAULT);
118 }
119 }
120 else if (sc.state == SCE_ASM_COMMENT ) {
121 if (sc.atLineEnd) {
122 sc.SetState(SCE_ASM_DEFAULT);
123 }
124 } else if (sc.state == SCE_ASM_STRING) {
125 if (sc.ch == '\\') {
126 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
127 sc.Forward();
128 }
129 } else if (sc.ch == '\"') {
130 sc.ForwardSetState(SCE_ASM_DEFAULT);
131 } else if (sc.atLineEnd) {
132 sc.ChangeState(SCE_ASM_STRINGEOL);
133 sc.ForwardSetState(SCE_ASM_DEFAULT);
134 }
135 } else if (sc.state == SCE_ASM_CHARACTER) {
136 if (sc.ch == '\\') {
137 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
138 sc.Forward();
139 }
140 } else if (sc.ch == '\'') {
141 sc.ForwardSetState(SCE_ASM_DEFAULT);
142 } else if (sc.atLineEnd) {
143 sc.ChangeState(SCE_ASM_STRINGEOL);
144 sc.ForwardSetState(SCE_ASM_DEFAULT);
145 }
146 }
147
148 // Determine if a new state should be entered.
149 if (sc.state == SCE_ASM_DEFAULT) {
150 if (sc.ch == ';'){
151 sc.SetState(SCE_ASM_COMMENT);
152 } else if (isascii(sc.ch) && (isdigit(sc.ch) || (sc.ch == '.' && isascii(sc.chNext) && isdigit(sc.chNext)))) {
153 sc.SetState(SCE_ASM_NUMBER);
154 } else if (IsAWordStart(sc.ch)) {
155 sc.SetState(SCE_ASM_IDENTIFIER);
156 } else if (sc.ch == '\"') {
157 sc.SetState(SCE_ASM_STRING);
158 } else if (sc.ch == '\'') {
159 sc.SetState(SCE_ASM_CHARACTER);
160 } else if (IsAsmOperator(sc.ch)) {
161 sc.SetState(SCE_ASM_OPERATOR);
162 }
163 }
164
165 }
166 sc.Complete();
167 }
168
169 static const char * const asmWordListDesc[] = {
170 "CPU instructions",
171 "FPU instructions",
172 "Registers",
173 "Directives",
174 "Directive operands",
175 "Extended instructions",
176 0
177 };
178
179 LexerModule lmAsm(SCLEX_ASM, ColouriseAsmDoc, "asm", 0, asmWordListDesc);
180