]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexAsm.cxx
1eecf9b31a99b8570297e32ea9d12dad72009773
[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 **/
6 // Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
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 <ctype.h>
12 #include <stdio.h>
13 #include <stdarg.h>
14
15 #include "Platform.h"
16
17 #include "PropSet.h"
18 #include "Accessor.h"
19 #include "StyleContext.h"
20 #include "KeyWords.h"
21 #include "Scintilla.h"
22 #include "SciLexer.h"
23
24
25
26 static inline bool IsAWordChar(const int ch) {
27 return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_' || ch =='\\');
28 }
29
30 static inline bool IsAWordStart(const int ch) {
31 return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '.');
32 }
33
34 inline bool isAsmOperator(char ch) {
35 if (isalnum(ch))
36 return false;
37 // '.' left out as it is used to make up numbers
38 if (ch == '*' || ch == '/' || ch == '-' || ch == '+' ||
39 ch == '(' || ch == ')' || ch == '=' ||
40 ch == '[' || ch == ']' || ch == '<' ||
41 ch == '>' || ch == ',' ||
42 ch == '.' || ch == '%' || ch == ':')
43 return true;
44 return false;
45 }
46
47 static void ColouriseAsmDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
48 Accessor &styler) {
49
50 WordList &cpuInstruction = *keywordlists[0];
51 WordList &mathInstruction = *keywordlists[1];
52 WordList &registers = *keywordlists[2];
53 WordList &directive = *keywordlists[3];
54 WordList &directiveOperand = *keywordlists[4];
55
56 StyleContext sc(startPos, length, initStyle, styler);
57
58 for (; sc.More(); sc.Forward())
59 {
60 // Handle line continuation generically.
61 if (sc.ch == '\\') {
62 if (sc.Match("\\\n")) {
63 sc.Forward();
64 continue;
65 }
66 if (sc.Match("\\\r\n")) {
67 sc.Forward();
68 sc.Forward();
69 continue;
70 }
71 }
72
73 // Determine if the current state should terminate.
74 if (sc.state == SCE_ASM_OPERATOR) {
75 sc.SetState(SCE_ASM_DEFAULT);
76 }else if (sc.state == SCE_ASM_NUMBER) {
77 if (!IsAWordChar(sc.ch)) {
78 sc.SetState(SCE_ASM_DEFAULT);
79 }
80 } else if (sc.state == SCE_ASM_IDENTIFIER) {
81 if (!IsAWordChar(sc.ch) ) {
82 char s[100];
83 sc.GetCurrentLowered(s, sizeof(s));
84
85 if (cpuInstruction.InList(s)) {
86 sc.ChangeState(SCE_ASM_CPUINSTRUCTION);
87 } else if (mathInstruction.InList(s)) {
88 sc.ChangeState(SCE_ASM_MATHINSTRUCTION);
89 } else if (registers.InList(s)) {
90 sc.ChangeState(SCE_ASM_REGISTER);
91 } else if (directive.InList(s)) {
92 sc.ChangeState(SCE_ASM_DIRECTIVE);
93 } else if (directiveOperand.InList(s)) {
94 sc.ChangeState(SCE_ASM_DIRECTIVEOPERAND);
95 }
96 sc.SetState(SCE_ASM_DEFAULT);
97 }
98 }
99 else if (sc.state == SCE_ASM_COMMENT ) {
100 if (sc.atLineEnd) {
101 sc.SetState(SCE_ASM_DEFAULT);
102 }
103 } else if (sc.state == SCE_ASM_STRING) {
104 if (sc.ch == '\\') {
105 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
106 sc.Forward();
107 }
108 } else if (sc.ch == '\"') {
109 sc.ForwardSetState(SCE_ASM_DEFAULT);
110 } else if (sc.atLineEnd) {
111 sc.ForwardSetState(SCE_ASM_DEFAULT);
112 }
113 }
114
115 // Determine if a new state should be entered.
116 else if (sc.state == SCE_ASM_DEFAULT) {
117 if (sc.ch == ';'){
118 sc.SetState(SCE_ASM_COMMENT);
119 } else if (isdigit(sc.ch) || (sc.ch == '.' && isdigit(sc.chNext))) {
120 sc.SetState(SCE_ASM_NUMBER);
121 } else if (IsAWordStart(sc.ch)) {
122 sc.SetState(SCE_ASM_IDENTIFIER);
123 } else if (sc.Match('\"')) {
124 sc.SetState(SCE_ASM_STRING);
125 }
126 }
127
128 }
129 sc.Complete();
130 }
131
132 static const char * const asmWordListDesc[] = {
133 "CPU instructions",
134 "FPU instructions",
135 "Registers",
136 "Directives",
137 "Directive operands",
138 0
139 };
140
141 LexerModule lmAsm(SCLEX_ASM, ColouriseAsmDoc, "asm", 0, asmWordListDesc);
142