Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARM ASM #531

Merged
merged 3 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions lexers/a/armasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package a

import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)

var ArmAsm = internal.Register(MustNewLazyLexer(
&Config{
Name: "ArmAsm",
Aliases: []string{"armasm"},
EnsureNL: true,
Filenames: []string{"*.s", "*.S"},
MimeTypes: []string{"text/x-armasm", "text/x-asm"},
},
armasmRules,
))

func armasmRules() Rules {
return Rules{
"commentsandwhitespace": {
{`\s+`, Text, nil},
{`[@;].*?\n`, CommentSingle, nil},
{`/\*.*?\*/`, CommentMultiline, nil},
},
"literal": {
// Binary
{`0b[01]+`, NumberBin, Pop(1)},
// Hex
{`0x\w{1,8}`, NumberHex, Pop(1)},
// Octal
{`0\d+`, NumberOct, Pop(1)},
// Float
{`\d+?\.\d+?`, NumberFloat, Pop(1)},
// Integer
{`\d+`, NumberInteger, Pop(1)},
// String
{`(")(.+)(")`, ByGroups(Punctuation, StringDouble, Punctuation), Pop(1)},
// Char
{`(')(.{1}|\\.{1})(')`, ByGroups(Punctuation, StringChar, Punctuation), Pop(1)},
},
"opcode": {
// Escape at line end
{`\n`, Text, Pop(1)},
// Comment
{`(@|;).*\n`, CommentSingle, Pop(1)},
// Whitespace
{`(\s+|,)`, Text, nil},
// Register by number
{`[rapcfxwbhsdqv]\d{1,2}`, NameClass, nil},
// Address by hex
{`=0x\w+`, ByGroups(Text, NameLabel), nil},
// Pseudo address by label
{`(=)(\w+)`, ByGroups(Text, NameLabel), nil},
// Immediate
{`#`, Text, Push("literal")},
},
"root": {
Include("commentsandwhitespace"),
// Directive with optional param
{`(\.\w+)([ \t]+\w+\s+?)?`, ByGroups(KeywordNamespace, NameLabel), nil},
// Label with data
{`(\w+)(:)(\s+\.\w+\s+)`, ByGroups(NameLabel, Punctuation, KeywordNamespace), Push("literal")},
// Label
{`(\w+)(:)`, ByGroups(NameLabel, Punctuation), nil},
// Syscall Op
{`svc\s+\w+`, NameNamespace, nil},
// Opcode
{`[a-zA-Z]+`, Text, Push("opcode")},
},
}
}
17 changes: 17 additions & 0 deletions lexers/testdata/armasm.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@ Hello World in ARM Assembly for Linux system

.global _start

_start:
mov r7, #4 @ Setup service call 4 (write)
mov r0, #1 @ param 1 - File descriptor 1 = stdout
ldr r1, =hello @ param 2 - address of string to print
mov r2, #13 @ param 3 - length of hello world string
svc 0 @ ask linux to write to stdout

mov r7, #1 @ Setup service call 1 (exit)
mov r0, #0 @ param 1 - 0 = normal exit
svc 0 @ ask linux to terminate us

.data
hello: .ascii "Hello World!\n"
62 changes: 62 additions & 0 deletions lexers/testdata/armasm.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{"type":"CommentSingle","value":"@ Hello World in ARM Assembly for Linux system\n"},
{"type":"Text","value":"\n"},
{"type":"KeywordNamespace","value":".global"},
{"type":"NameLabel","value":" _start\n"},
{"type":"Text","value":"\n"},
{"type":"NameLabel","value":"_start"},
{"type":"Punctuation","value":":"},
{"type":"Text","value":"\n mov "},
{"type":"NameClass","value":"r7"},
{"type":"Text","value":", #"},
{"type":"LiteralNumberInteger","value":"4"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"@ Setup service call 4 (write)\n"},
{"type":"Text","value":" mov "},
{"type":"NameClass","value":"r0"},
{"type":"Text","value":", #"},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"@ param 1 - File descriptor 1 = stdout\n"},
{"type":"Text","value":" ldr "},
{"type":"NameClass","value":"r1"},
{"type":"Text","value":", ="},
{"type":"NameLabel","value":"hello"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"@ param 2 - address of string to print\n"},
{"type":"Text","value":" mov "},
{"type":"NameClass","value":"r2"},
{"type":"Text","value":", #"},
{"type":"LiteralNumberInteger","value":"13"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"@ param 3 - length of hello world string\n"},
{"type":"Text","value":" "},
{"type":"NameNamespace","value":"svc 0"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"@ ask linux to write to stdout\n"},
{"type":"Text","value":"\n mov "},
{"type":"NameClass","value":"r7"},
{"type":"Text","value":", #"},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"@ Setup service call 1 (exit)\n"},
{"type":"Text","value":" mov "},
{"type":"NameClass","value":"r0"},
{"type":"Text","value":", #"},
{"type":"LiteralNumberInteger","value":"0"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"@ param 1 - 0 = normal exit\n"},
{"type":"Text","value":" "},
{"type":"NameNamespace","value":"svc 0"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"@ ask linux to terminate us\n"},
{"type":"Text","value":"\n"},
{"type":"KeywordNamespace","value":".data"},
{"type":"Text","value":"\n"},
{"type":"NameLabel","value":"hello"},
{"type":"Punctuation","value":":"},
{"type":"KeywordNamespace","value":" .ascii "},
{"type":"Punctuation","value":"\""},
{"type":"LiteralStringDouble","value":"Hello World!\\n"},
{"type":"Punctuation","value":"\""}
]