1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-01-28 03:29:41 +02:00

Add Lexer for NDISASM (#933)

The Netwide Disassembler outputs text that is assembly prefixed with
memory offsets and opcode bytes.
This commit is contained in:
Andrzej Lichnerowicz 2024-02-27 14:17:29 +01:00 committed by GitHub
parent 6dd9f269ef
commit fe5dde8cf4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 243 additions and 0 deletions

123
lexers/embedded/ndisasm.xml Normal file
View File

@ -0,0 +1,123 @@
<lexer>
<config>
<name>NDISASM</name>
<alias>ndisasm</alias>
<mime_type>text/x-disasm</mime_type>
<case_insensitive>true</case_insensitive>
<priority>0.5</priority> <!-- Lower than NASM -->
</config>
<rules>
<state name="root">
<rule pattern="^[0-9A-Za-z]+">
<token type="CommentSpecial"/>
<push state="offset"/>
</rule>
</state>
<state name="offset">
<rule pattern="[0-9A-Za-z]+">
<token type="CommentSpecial"/>
<push state="assembly"/>
</rule>
<rule>
<include state="whitespace"/>
</rule>
</state>
<state name="punctuation">
<rule pattern="[,():\[\]]+">
<token type="Punctuation"/>
</rule>
<rule pattern="[&amp;|^&lt;&gt;+*/%~-]+">
<token type="Operator"/>
</rule>
<rule pattern="[$]+">
<token type="KeywordConstant"/>
</rule>
<rule pattern="seg|wrt|strict">
<token type="OperatorWord"/>
</rule>
<rule pattern="byte|[dq]?word">
<token type="KeywordType"/>
</rule>
</state>
<state name="assembly">
<rule>
<include state="whitespace"/>
</rule>
<rule pattern="[a-z$._?][\w$.?#@~]*:">
<token type="NameLabel"/>
</rule>
<rule pattern="([a-z$._?][\w$.?#@~]*)(\s+)(equ)">
<bygroups>
<token type="NameConstant"/>
<token type="KeywordDeclaration"/>
<token type="KeywordDeclaration"/>
</bygroups>
<push state="instruction-args"/>
</rule>
<rule pattern="BITS|USE16|USE32|SECTION|SEGMENT|ABSOLUTE|EXTERN|GLOBAL|ORG|ALIGN|STRUC|ENDSTRUC|COMMON|CPU|GROUP|UPPERCASE|IMPORT|EXPORT|LIBRARY|MODULE">
<token type="Keyword"/>
<push state="instruction-args"/>
</rule>
<rule pattern="(?:res|d)[bwdqt]|times">
<token type="KeywordDeclaration"/>
<push state="instruction-args"/>
</rule>
<rule pattern="[a-z$._?][\w$.?#@~]*">
<token type="NameFunction"/>
<push state="instruction-args"/>
</rule>
<rule pattern="[\r\n]+">
<token type="Text"/>
<pop depth="2"/>
</rule>
</state>
<state name="instruction-args">
<rule pattern="&#34;(\\&#34;|[^&#34;\n])*&#34;|&#39;(\\&#39;|[^&#39;\n])*&#39;|`(\\`|[^`\n])*`">
<token type="LiteralString"/>
</rule>
<rule pattern="(?:0x[0-9a-f]+|$0[0-9a-f]*|[0-9]+[0-9a-f]*h)">
<token type="LiteralNumberHex"/>
</rule>
<rule pattern="[0-7]+q">
<token type="LiteralNumberOct"/>
</rule>
<rule pattern="[01]+b">
<token type="LiteralNumberBin"/>
</rule>
<rule pattern="[0-9]+\.e?[0-9]+">
<token type="LiteralNumberFloat"/>
</rule>
<rule pattern="[0-9]+">
<token type="LiteralNumberInteger"/>
</rule>
<rule>
<include state="punctuation"/>
</rule>
<rule pattern="r[0-9][0-5]?[bwd]|[a-d][lh]|[er]?[a-d]x|[er]?[sb]p|[er]?[sd]i|[c-gs]s|st[0-7]|mm[0-7]|cr[0-4]|dr[0-367]|tr[3-7]">
<token type="NameBuiltin"/>
</rule>
<rule pattern="[a-z$._?][\w$.?#@~]*">
<token type="NameVariable"/>
</rule>
<rule pattern="[\r\n]+">
<token type="Text"/>
<pop depth="3"/>
</rule>
<rule>
<include state="whitespace"/>
</rule>
</state>
<state name="whitespace">
<rule pattern="\n">
<token type="Text"/>
<pop depth="2"/>
</rule>
<rule pattern="[ \t]+">
<token type="Text"/>
</rule>
<rule pattern=";.*">
<token type="CommentSingle"/>
</rule>
</state>
</rules>
</lexer>

12
lexers/testdata/ndisasm.actual vendored Normal file
View File

@ -0,0 +1,12 @@
00000000 B013 mov al,0x13
00000002 CD10 int 0x10
00000004 B9027D mov cx,0x7d02
00000007 51 push cx
00000008 6800A0 push word 0xa000
0000000B 07 pop es
0000000C B00F mov al,0xf
0000000E F3AA rep stosb
00000010 59 pop cx
00000011 B004 mov al,0x4
00000013 F3AA rep stosb
00000015 CD16 int 0x16

108
lexers/testdata/ndisasm.expected vendored Normal file
View File

@ -0,0 +1,108 @@
[
{"type":"CommentSpecial","value":"00000000"},
{"type":"Text","value":" "},
{"type":"CommentSpecial","value":"B013"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"mov"},
{"type":"Text","value":" "},
{"type":"NameBuiltin","value":"al"},
{"type":"Punctuation","value":","},
{"type":"LiteralNumberHex","value":"0x13"},
{"type":"Text","value":"\n"},
{"type":"CommentSpecial","value":"00000002"},
{"type":"Text","value":" "},
{"type":"CommentSpecial","value":"CD10"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"int"},
{"type":"Text","value":" "},
{"type":"LiteralNumberHex","value":"0x10"},
{"type":"Text","value":"\n"},
{"type":"CommentSpecial","value":"00000004"},
{"type":"Text","value":" "},
{"type":"CommentSpecial","value":"B9027D"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"mov"},
{"type":"Text","value":" "},
{"type":"NameBuiltin","value":"cx"},
{"type":"Punctuation","value":","},
{"type":"LiteralNumberHex","value":"0x7d02"},
{"type":"Text","value":"\n"},
{"type":"CommentSpecial","value":"00000007"},
{"type":"Text","value":" "},
{"type":"CommentSpecial","value":"51"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"push"},
{"type":"Text","value":" "},
{"type":"NameBuiltin","value":"cx"},
{"type":"Text","value":"\n"},
{"type":"CommentSpecial","value":"00000008"},
{"type":"Text","value":" "},
{"type":"CommentSpecial","value":"6800A0"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"push"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"word"},
{"type":"Text","value":" "},
{"type":"LiteralNumberHex","value":"0xa000"},
{"type":"Text","value":"\n"},
{"type":"CommentSpecial","value":"0000000B"},
{"type":"Text","value":" "},
{"type":"CommentSpecial","value":"07"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"pop"},
{"type":"Text","value":" "},
{"type":"NameBuiltin","value":"es"},
{"type":"Text","value":"\n"},
{"type":"CommentSpecial","value":"0000000C"},
{"type":"Text","value":" "},
{"type":"CommentSpecial","value":"B00F"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"mov"},
{"type":"Text","value":" "},
{"type":"NameBuiltin","value":"al"},
{"type":"Punctuation","value":","},
{"type":"LiteralNumberHex","value":"0xf"},
{"type":"Text","value":"\n"},
{"type":"CommentSpecial","value":"0000000E"},
{"type":"Text","value":" "},
{"type":"CommentSpecial","value":"F3AA"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"rep"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"stosb"},
{"type":"Text","value":"\n"},
{"type":"CommentSpecial","value":"00000010"},
{"type":"Text","value":" "},
{"type":"CommentSpecial","value":"59"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"pop"},
{"type":"Text","value":" "},
{"type":"NameBuiltin","value":"cx"},
{"type":"Text","value":"\n"},
{"type":"CommentSpecial","value":"00000011"},
{"type":"Text","value":" "},
{"type":"CommentSpecial","value":"B004"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"mov"},
{"type":"Text","value":" "},
{"type":"NameBuiltin","value":"al"},
{"type":"Punctuation","value":","},
{"type":"LiteralNumberHex","value":"0x4"},
{"type":"Text","value":"\n"},
{"type":"CommentSpecial","value":"00000013"},
{"type":"Text","value":" "},
{"type":"CommentSpecial","value":"F3AA"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"rep"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"stosb"},
{"type":"Text","value":"\n"},
{"type":"CommentSpecial","value":"00000015"},
{"type":"Text","value":" "},
{"type":"CommentSpecial","value":"CD16"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"int"},
{"type":"Text","value":" "},
{"type":"LiteralNumberHex","value":"0x16"},
{"type":"Text","value":"\n"}
]