Skip to content

Commit 85a9705

Browse files
authored
Improve things (#1)
fix: Handle negative numbers
1 parent ae0728a commit 85a9705

File tree

7 files changed

+494
-364
lines changed

7 files changed

+494
-364
lines changed

examples/example.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name,city,state,zip,"latitude","longitude",signed
2+
Mcdonalds,"Los Angeles",CA,90001,33.973951,-118.248405,true
3+
Dennys,"New York",NY,10001,40.750634,-73.997177,true
4+
"Burger King",Chicago,IL,60601,41.884151,-87.622848,false

grammar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ module.exports = grammar({
3030
$.float,
3131
$.integer,
3232
),
33-
integer: $ => /\d+/,
33+
integer: $ => /-?\d+/,
3434
hex: $ => /0[xX][\da-fA-F]+/,
35-
float: $ => /(0|[1-9]\d*)\.\d+/,
35+
float: $ => /-?(0|[1-9]\d*)\.\d+/,
3636
string: $ => choice(
3737
seq($.quote, $.quote),
3838
seq($.quote, $.escaped, $.quote),

package.json

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
"version": "0.1.0",
44
"description": "CSV grammar for tree-sitter",
55
"main": "bindings/node",
6-
"keywords": [
7-
"parser",
8-
"tree-sitter",
9-
"csv"
10-
],
6+
"keywords": ["parser", "tree-sitter", "csv"],
117
"author": "Arnau Siches",
128
"license": "MIT",
139
"scripts": {
1410
"build": "tree-sitter generate && node-gyp build",
15-
"test": "tree-sitter test"
11+
"test": "tree-sitter test",
12+
"dev": "npm run build && tree-sitter highlight examples/*.csv"
1613
},
1714
"dependencies": {
1815
"nan": "^2.17.0",
@@ -24,9 +21,7 @@
2421
"tree-sitter": [
2522
{
2623
"scope": "source.csv",
27-
"file-types": [
28-
"csv"
29-
]
24+
"file-types": ["csv"]
3025
}
3126
]
3227
}

queries/highlights.scm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
(null)
1010
(na)
1111
] @constant.builtin
12+
1213
(string) @string
1314

1415
(escape_sequence) @constant.character.escape
1516

16-
1717
; Punctuation
1818
(delimiter) @punctuation.delimiter
1919
(quote) @punctuation.bracket
20-

src/grammar.json

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,33 +120,50 @@
120120
"value": "true|TRUE|false|FALSE"
121121
},
122122
"_number": {
123-
"type": "CHOICE",
123+
"type": "SEQ",
124124
"members": [
125125
{
126-
"type": "SYMBOL",
127-
"name": "hex"
128-
},
129-
{
130-
"type": "SYMBOL",
131-
"name": "float"
126+
"type": "CHOICE",
127+
"members": [
128+
{
129+
"type": "STRING",
130+
"value": "-"
131+
},
132+
{
133+
"type": "BLANK"
134+
}
135+
]
132136
},
133137
{
134-
"type": "SYMBOL",
135-
"name": "integer"
138+
"type": "CHOICE",
139+
"members": [
140+
{
141+
"type": "SYMBOL",
142+
"name": "hex"
143+
},
144+
{
145+
"type": "SYMBOL",
146+
"name": "float"
147+
},
148+
{
149+
"type": "SYMBOL",
150+
"name": "integer"
151+
}
152+
]
136153
}
137154
]
138155
},
139156
"integer": {
140157
"type": "PATTERN",
141-
"value": "\\d+"
158+
"value": "-?\\d+"
142159
},
143160
"hex": {
144161
"type": "PATTERN",
145162
"value": "0[xX][\\da-fA-F]+"
146163
},
147164
"float": {
148165
"type": "PATTERN",
149-
"value": "(0|[1-9]\\d*)\\.\\d+"
166+
"value": "-?(0|[1-9]\\d*)\\.\\d+"
150167
},
151168
"string": {
152169
"type": "CHOICE",

src/node-types.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@
114114
"type": "\r\n",
115115
"named": false
116116
},
117+
{
118+
"type": "-",
119+
"named": false
120+
},
117121
{
118122
"type": "boolean",
119123
"named": true

0 commit comments

Comments
 (0)