]> git.saurik.com Git - wxWidgets.git/blame - src/stc/scintilla/src/LexAsm.cxx
fix compilation without wxUSE_STREAMS (closes #10900)
[wxWidgets.git] / src / stc / scintilla / src / LexAsm.cxx
CommitLineData
9e730a78
RD
1// Scintilla source code edit control
2/** @file LexAsm.cxx
8e54aaed 3 ** Lexer for Assembler, just for the MASM syntax
9e730a78 4 ** Written by The Black Horus
8e54aaed
RD
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
9e730a78 7 **/
8e54aaed 8// Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
9e730a78
RD
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
7e0c58e9
RD
26#ifdef SCI_NAMESPACE
27using namespace Scintilla;
28#endif
9e730a78 29
9e730a78 30static inline bool IsAWordChar(const int ch) {
8e54aaed
RD
31 return (ch < 0x80) && (isalnum(ch) || ch == '.' ||
32 ch == '_' || ch == '?');
9e730a78
RD
33}
34
35static inline bool IsAWordStart(const int ch) {
8e54aaed
RD
36 return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '.' ||
37 ch == '%' || ch == '@' || ch == '$' || ch == '?');
9e730a78
RD
38}
39
8e54aaed 40static inline bool IsAsmOperator(char ch) {
9e730a78
RD
41 if (isalnum(ch))
42 return false;
43 // '.' left out as it is used to make up numbers
44 if (ch == '*' || ch == '/' || ch == '-' || ch == '+' ||
8e54aaed
RD
45 ch == '(' || ch == ')' || ch == '=' || ch == '^' ||
46 ch == '[' || ch == ']' || ch == '<' || ch == '&' ||
47 ch == '>' || ch == ',' || ch == '|' || ch == '~' ||
48 ch == '%' || ch == ':')
9e730a78
RD
49 return true;
50 return false;
51}
52
53static 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];
8e54aaed
RD
61 WordList &extInstruction = *keywordlists[5];
62
63 // Do not leak onto next line
64 if (initStyle == SCE_ASM_STRINGEOL)
65 initStyle = SCE_ASM_DEFAULT;
9e730a78
RD
66
67 StyleContext sc(startPos, length, initStyle, styler);
68
69 for (; sc.More(); sc.Forward())
70 {
8e54aaed
RD
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
9e730a78
RD
79 // Handle line continuation generically.
80 if (sc.ch == '\\') {
8e54aaed 81 if (sc.chNext == '\n' || sc.chNext == '\r') {
9e730a78 82 sc.Forward();
8e54aaed
RD
83 if (sc.ch == '\r' && sc.chNext == '\n') {
84 sc.Forward();
85 }
9e730a78
RD
86 continue;
87 }
88 }
89
90 // Determine if the current state should terminate.
91 if (sc.state == SCE_ASM_OPERATOR) {
8e54aaed
RD
92 if (!IsAsmOperator(static_cast<char>(sc.ch))) {
93 sc.SetState(SCE_ASM_DEFAULT);
94 }
9e730a78
RD
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) ) {
8e54aaed
RD
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);
9e730a78
RD
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) {
8e54aaed
RD
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);
9e730a78
RD
144 sc.ForwardSetState(SCE_ASM_DEFAULT);
145 }
146 }
147
148 // Determine if a new state should be entered.
8e54aaed 149 if (sc.state == SCE_ASM_DEFAULT) {
9e730a78
RD
150 if (sc.ch == ';'){
151 sc.SetState(SCE_ASM_COMMENT);
152 } else if (isdigit(sc.ch) || (sc.ch == '.' && isdigit(sc.chNext))) {
153 sc.SetState(SCE_ASM_NUMBER);
154 } else if (IsAWordStart(sc.ch)) {
155 sc.SetState(SCE_ASM_IDENTIFIER);
8e54aaed 156 } else if (sc.ch == '\"') {
9e730a78 157 sc.SetState(SCE_ASM_STRING);
8e54aaed
RD
158 } else if (sc.ch == '\'') {
159 sc.SetState(SCE_ASM_CHARACTER);
160 } else if (IsAsmOperator(static_cast<char>(sc.ch))) {
161 sc.SetState(SCE_ASM_OPERATOR);
9e730a78
RD
162 }
163 }
164
165 }
166 sc.Complete();
167}
168
169static const char * const asmWordListDesc[] = {
170 "CPU instructions",
171 "FPU instructions",
172 "Registers",
173 "Directives",
174 "Directive operands",
8e54aaed 175 "Extended instructions",
9e730a78
RD
176 0
177};
178
179LexerModule lmAsm(SCLEX_ASM, ColouriseAsmDoc, "asm", 0, asmWordListDesc);
180