Skip to content

Commit a0e9618

Browse files
authored
ARM ASM (#531)
1 parent d402102 commit a0e9618

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

Diff for: lexers/a/armasm.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package a
2+
3+
import (
4+
. "github.com/alecthomas/chroma" // nolint
5+
"github.com/alecthomas/chroma/lexers/internal"
6+
)
7+
8+
var ArmAsm = internal.Register(MustNewLazyLexer(
9+
&Config{
10+
Name: "ArmAsm",
11+
Aliases: []string{"armasm"},
12+
EnsureNL: true,
13+
Filenames: []string{"*.s", "*.S"},
14+
MimeTypes: []string{"text/x-armasm", "text/x-asm"},
15+
},
16+
armasmRules,
17+
))
18+
19+
func armasmRules() Rules {
20+
return Rules{
21+
"commentsandwhitespace": {
22+
{`\s+`, Text, nil},
23+
{`[@;].*?\n`, CommentSingle, nil},
24+
{`/\*.*?\*/`, CommentMultiline, nil},
25+
},
26+
"literal": {
27+
// Binary
28+
{`0b[01]+`, NumberBin, Pop(1)},
29+
// Hex
30+
{`0x\w{1,8}`, NumberHex, Pop(1)},
31+
// Octal
32+
{`0\d+`, NumberOct, Pop(1)},
33+
// Float
34+
{`\d+?\.\d+?`, NumberFloat, Pop(1)},
35+
// Integer
36+
{`\d+`, NumberInteger, Pop(1)},
37+
// String
38+
{`(")(.+)(")`, ByGroups(Punctuation, StringDouble, Punctuation), Pop(1)},
39+
// Char
40+
{`(')(.{1}|\\.{1})(')`, ByGroups(Punctuation, StringChar, Punctuation), Pop(1)},
41+
},
42+
"opcode": {
43+
// Escape at line end
44+
{`\n`, Text, Pop(1)},
45+
// Comment
46+
{`(@|;).*\n`, CommentSingle, Pop(1)},
47+
// Whitespace
48+
{`(\s+|,)`, Text, nil},
49+
// Register by number
50+
{`[rapcfxwbhsdqv]\d{1,2}`, NameClass, nil},
51+
// Address by hex
52+
{`=0x\w+`, ByGroups(Text, NameLabel), nil},
53+
// Pseudo address by label
54+
{`(=)(\w+)`, ByGroups(Text, NameLabel), nil},
55+
// Immediate
56+
{`#`, Text, Push("literal")},
57+
},
58+
"root": {
59+
Include("commentsandwhitespace"),
60+
// Directive with optional param
61+
{`(\.\w+)([ \t]+\w+\s+?)?`, ByGroups(KeywordNamespace, NameLabel), nil},
62+
// Label with data
63+
{`(\w+)(:)(\s+\.\w+\s+)`, ByGroups(NameLabel, Punctuation, KeywordNamespace), Push("literal")},
64+
// Label
65+
{`(\w+)(:)`, ByGroups(NameLabel, Punctuation), nil},
66+
// Syscall Op
67+
{`svc\s+\w+`, NameNamespace, nil},
68+
// Opcode
69+
{`[a-zA-Z]+`, Text, Push("opcode")},
70+
},
71+
}
72+
}

Diff for: lexers/testdata/armasm.actual

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@ Hello World in ARM Assembly for Linux system
2+
3+
.global _start
4+
5+
_start:
6+
mov r7, #4 @ Setup service call 4 (write)
7+
mov r0, #1 @ param 1 - File descriptor 1 = stdout
8+
ldr r1, =hello @ param 2 - address of string to print
9+
mov r2, #13 @ param 3 - length of hello world string
10+
svc 0 @ ask linux to write to stdout
11+
12+
mov r7, #1 @ Setup service call 1 (exit)
13+
mov r0, #0 @ param 1 - 0 = normal exit
14+
svc 0 @ ask linux to terminate us
15+
16+
.data
17+
hello: .ascii "Hello World!\n"

Diff for: lexers/testdata/armasm.expected

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[
2+
{"type":"CommentSingle","value":"@ Hello World in ARM Assembly for Linux system\n"},
3+
{"type":"Text","value":"\n"},
4+
{"type":"KeywordNamespace","value":".global"},
5+
{"type":"NameLabel","value":" _start\n"},
6+
{"type":"Text","value":"\n"},
7+
{"type":"NameLabel","value":"_start"},
8+
{"type":"Punctuation","value":":"},
9+
{"type":"Text","value":"\n mov "},
10+
{"type":"NameClass","value":"r7"},
11+
{"type":"Text","value":", #"},
12+
{"type":"LiteralNumberInteger","value":"4"},
13+
{"type":"Text","value":" "},
14+
{"type":"CommentSingle","value":"@ Setup service call 4 (write)\n"},
15+
{"type":"Text","value":" mov "},
16+
{"type":"NameClass","value":"r0"},
17+
{"type":"Text","value":", #"},
18+
{"type":"LiteralNumberInteger","value":"1"},
19+
{"type":"Text","value":" "},
20+
{"type":"CommentSingle","value":"@ param 1 - File descriptor 1 = stdout\n"},
21+
{"type":"Text","value":" ldr "},
22+
{"type":"NameClass","value":"r1"},
23+
{"type":"Text","value":", ="},
24+
{"type":"NameLabel","value":"hello"},
25+
{"type":"Text","value":" "},
26+
{"type":"CommentSingle","value":"@ param 2 - address of string to print\n"},
27+
{"type":"Text","value":" mov "},
28+
{"type":"NameClass","value":"r2"},
29+
{"type":"Text","value":", #"},
30+
{"type":"LiteralNumberInteger","value":"13"},
31+
{"type":"Text","value":" "},
32+
{"type":"CommentSingle","value":"@ param 3 - length of hello world string\n"},
33+
{"type":"Text","value":" "},
34+
{"type":"NameNamespace","value":"svc 0"},
35+
{"type":"Text","value":" "},
36+
{"type":"CommentSingle","value":"@ ask linux to write to stdout\n"},
37+
{"type":"Text","value":"\n mov "},
38+
{"type":"NameClass","value":"r7"},
39+
{"type":"Text","value":", #"},
40+
{"type":"LiteralNumberInteger","value":"1"},
41+
{"type":"Text","value":" "},
42+
{"type":"CommentSingle","value":"@ Setup service call 1 (exit)\n"},
43+
{"type":"Text","value":" mov "},
44+
{"type":"NameClass","value":"r0"},
45+
{"type":"Text","value":", #"},
46+
{"type":"LiteralNumberInteger","value":"0"},
47+
{"type":"Text","value":" "},
48+
{"type":"CommentSingle","value":"@ param 1 - 0 = normal exit\n"},
49+
{"type":"Text","value":" "},
50+
{"type":"NameNamespace","value":"svc 0"},
51+
{"type":"Text","value":" "},
52+
{"type":"CommentSingle","value":"@ ask linux to terminate us\n"},
53+
{"type":"Text","value":"\n"},
54+
{"type":"KeywordNamespace","value":".data"},
55+
{"type":"Text","value":"\n"},
56+
{"type":"NameLabel","value":"hello"},
57+
{"type":"Punctuation","value":":"},
58+
{"type":"KeywordNamespace","value":" .ascii "},
59+
{"type":"Punctuation","value":"\""},
60+
{"type":"LiteralStringDouble","value":"Hello World!\\n"},
61+
{"type":"Punctuation","value":"\""}
62+
]

0 commit comments

Comments
 (0)