Skip to content

Commit 2c5b138

Browse files
authored
Merge pull request #184 from tree-sitter/mj-no-string-blank
Get nodes for string fragments
2 parents ae9bc67 + 9d505df commit 2c5b138

File tree

9 files changed

+219
-165
lines changed

9 files changed

+219
-165
lines changed

grammar.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,21 +866,32 @@ module.exports = grammar({
866866
seq(
867867
'"',
868868
repeat(choice(
869-
token.immediate(prec(1, /[^"\\]+/)),
869+
alias($.unescaped_double_string_fragment, $.string_fragment),
870870
$.escape_sequence
871871
)),
872872
'"'
873873
),
874874
seq(
875875
"'",
876876
repeat(choice(
877-
token.immediate(prec(1, /[^'\\]+/)),
877+
alias($.unescaped_single_string_fragment, $.string_fragment),
878878
$.escape_sequence
879879
)),
880880
"'"
881881
)
882882
),
883883

884+
// Workaround to https://github.com/tree-sitter/tree-sitter/issues/1156
885+
// We give names to the token() constructs containing a regexp
886+
// so as to obtain a node in the CST.
887+
//
888+
unescaped_double_string_fragment: $ =>
889+
token.immediate(prec(1, /[^"\\]+/)),
890+
891+
// same here
892+
unescaped_single_string_fragment: $ =>
893+
token.immediate(prec(1, /[^'\\]+/)),
894+
884895
escape_sequence: $ => token.immediate(seq(
885896
'\\',
886897
choice(

src/grammar.json

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4721,15 +4721,13 @@
47214721
"type": "CHOICE",
47224722
"members": [
47234723
{
4724-
"type": "IMMEDIATE_TOKEN",
4724+
"type": "ALIAS",
47254725
"content": {
4726-
"type": "PREC",
4727-
"value": 1,
4728-
"content": {
4729-
"type": "PATTERN",
4730-
"value": "[^\"\\\\]+"
4731-
}
4732-
}
4726+
"type": "SYMBOL",
4727+
"name": "unescaped_double_string_fragment"
4728+
},
4729+
"named": true,
4730+
"value": "string_fragment"
47334731
},
47344732
{
47354733
"type": "SYMBOL",
@@ -4757,15 +4755,13 @@
47574755
"type": "CHOICE",
47584756
"members": [
47594757
{
4760-
"type": "IMMEDIATE_TOKEN",
4758+
"type": "ALIAS",
47614759
"content": {
4762-
"type": "PREC",
4763-
"value": 1,
4764-
"content": {
4765-
"type": "PATTERN",
4766-
"value": "[^'\\\\]+"
4767-
}
4768-
}
4760+
"type": "SYMBOL",
4761+
"name": "unescaped_single_string_fragment"
4762+
},
4763+
"named": true,
4764+
"value": "string_fragment"
47694765
},
47704766
{
47714767
"type": "SYMBOL",
@@ -4782,6 +4778,28 @@
47824778
}
47834779
]
47844780
},
4781+
"unescaped_double_string_fragment": {
4782+
"type": "IMMEDIATE_TOKEN",
4783+
"content": {
4784+
"type": "PREC",
4785+
"value": 1,
4786+
"content": {
4787+
"type": "PATTERN",
4788+
"value": "[^\"\\\\]+"
4789+
}
4790+
}
4791+
},
4792+
"unescaped_single_string_fragment": {
4793+
"type": "IMMEDIATE_TOKEN",
4794+
"content": {
4795+
"type": "PREC",
4796+
"value": 1,
4797+
"content": {
4798+
"type": "PATTERN",
4799+
"value": "[^'\\\\]+"
4800+
}
4801+
}
4802+
},
47854803
"escape_sequence": {
47864804
"type": "IMMEDIATE_TOKEN",
47874805
"content": {

src/node-types.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,10 @@
23782378
{
23792379
"type": "escape_sequence",
23802380
"named": true
2381+
},
2382+
{
2383+
"type": "string_fragment",
2384+
"named": true
23812385
}
23822386
]
23832387
}
@@ -3231,6 +3235,10 @@
32313235
"type": "static",
32323236
"named": false
32333237
},
3238+
{
3239+
"type": "string_fragment",
3240+
"named": true
3241+
},
32343242
{
32353243
"type": "super",
32363244
"named": true

0 commit comments

Comments
 (0)