Skip to content

Commit

Permalink
Rename to ARM to not conflict with upstream ASM parser
Browse files Browse the repository at this point in the history
- Also add include statement support
  • Loading branch information
SethBarberee committed Feb 16, 2024
1 parent 0ac5741 commit eee056f
Show file tree
Hide file tree
Showing 9 changed files with 2,195 additions and 2,009 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ The start of a ARM/Thumb ASM tree-sitter parser. This isn't fully complete yet s
1) Add the following snippet to your init.lua:
```
local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
parser_config.asm = {
parser_config.arm = {
install_info = {
url = <PATH to REPO>,
files = {"src/parser.c"},
-- optional entries:
generate_requires_npm = false, -- if stand-alone parser without npm dependencies
requires_generate_from_grammar = false, -- if folder contains pre-generated src/parser.c
},
filetype = "asm", -- if filetype does not match the parser name
filetype = "arm", -- if filetype does not match the parser name
}
```
2) Run `TSInstallFromGrammar asm`
2) Run `TSInstallFromGrammar arm`

3) Copy `queries/highlights.scm` to `<path to nvim>/queries/after/asm/highlights.scm` (Optional, if you want the highlighting)
3) Copy `queries/highlights.scm` to `<path to nvim>/queries/after/arm/highlights.scm` (Optional, if you want the highlighting)
40 changes: 23 additions & 17 deletions grammar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = grammar({
name: "asm",
name: "arm", // have to use this since upstream has asm now

word: ($) => $.identifier,

Expand All @@ -9,7 +9,8 @@ module.exports = grammar({
rules: {
source_file: ($) => optional($._statement),

_statement: ($) => repeat1(choice($.function_definition, $.directive)),
_statement: ($) =>
repeat1(choice($.function_definition, $.directive, $.include_statement)),

function_definition: ($) =>
seq(
Expand All @@ -23,10 +24,10 @@ module.exports = grammar({
$.ldm_statement,
$.push_statement,
$.pool_statement,
$.label
)
$.label,
),
),
$.return_statement
$.return_statement,
),

math_statement: ($) =>
Expand All @@ -36,7 +37,7 @@ module.exports = grammar({
",",
field("Rn", $.register),
",",
field("operand2", choice($.register, $.constant))
field("operand2", choice($.register, $.constant)),
),

// We handle when [] or just a label and offsets
Expand All @@ -60,12 +61,12 @@ module.exports = grammar({
choice(
field("offset", $.constant),
field("regoffset", $.register),
$.offset_statement
)
$.offset_statement,
),
),
"]"
)
)
"]",
),
),
),

ldm_statement: ($) =>
Expand All @@ -76,7 +77,7 @@ module.exports = grammar({
",",
"{",
field("registers", commaSep($.reg_list)),
"}"
"}",
),

ldm_opcode: ($) => choice(/ldm([a-z]+)?/, /stm([a-z]+)?/),
Expand All @@ -102,7 +103,7 @@ module.exports = grammar({
$.opcode,
field("Rd", $.register),
optional(","),
field("operand2", optional(choice($.register, $.constant)))
field("operand2", optional(choice($.register, $.constant))),
),

// TODO: fairly limited and only to ARM/THUMB for now since that's all I use
Expand Down Expand Up @@ -133,7 +134,7 @@ module.exports = grammar({
/umlal([a-z]+)?/,
/smull([a-z]+)?/,
/smlal([a-z]+)?/,
/nop/
/nop/,
),

return_statement: ($) => seq($.branch_opcode, field("Rm", $.register)),
Expand All @@ -152,7 +153,7 @@ module.exports = grammar({
/(bpl)\s+/,
/(bx)\s+/,
/(bl([a-z]+)?)\s+/,
/(bg([a-z]+)?)\s+/
/(bg([a-z]+)?)\s+/,
),

//label: ($) => /(.*?):/,
Expand All @@ -163,12 +164,17 @@ module.exports = grammar({
directive_statement: ($) => seq($.directive, $.constant),
directive: ($) => token(/[.][0-9a-zA-Z]+/),

string: ($) => seq('"', $.identifier, '"'),

// TODO parse the file as a string
include_statement: ($) => seq("#include", $.string),

comment: ($) =>
token(
choice(
seq("@", /(\\(.|\r?\n)|[^\\\n])*/),
seq(";", /(\\(.|\r?\n)|[^\\\n])*/)
)
seq(";", /(\\(.|\r?\n)|[^\\\n])*/),
),
),

// Used in ldr/str and directives
Expand Down
2 changes: 2 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
(func_name (identifier)) @function
(label (identifier)) @label
(directive) @preproc
(include_statement) @preproc
(string) @string

[
(load_opcode)
Expand Down
36 changes: 35 additions & 1 deletion src/grammar.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "asm",
"name": "arm",
"word": "identifier",
"rules": {
"source_file": {
Expand All @@ -26,6 +26,10 @@
{
"type": "SYMBOL",
"name": "directive"
},
{
"type": "SYMBOL",
"name": "include_statement"
}
]
}
Expand Down Expand Up @@ -762,6 +766,36 @@
"value": "[.][0-9a-zA-Z]+"
}
},
"string": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\""
},
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": "\""
}
]
},
"include_statement": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "#include"
},
{
"type": "SYMBOL",
"name": "string"
}
]
},
"comment": {
"type": "TOKEN",
"content": {
Expand Down
42 changes: 42 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@
]
}
},
{
"type": "include_statement",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "string",
"named": true
}
]
}
},
{
"type": "label",
"named": true,
Expand Down Expand Up @@ -460,6 +475,25 @@
{
"type": "function_definition",
"named": true
},
{
"type": "include_statement",
"named": true
}
]
}
},
{
"type": "string",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
}
Expand All @@ -468,6 +502,14 @@
"type": "!",
"named": false
},
{
"type": "\"",
"named": false
},
{
"type": "#include",
"named": false
},
{
"type": ",",
"named": false
Expand Down
Loading

0 comments on commit eee056f

Please sign in to comment.